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 |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 3 | * |
bellard | 84f2e8e | 2007-02-05 20:21:32 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2007 Fabrice Bellard |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 5 | * |
bellard | 1df912c | 2003-06-25 16:20:35 +0000 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 23 | */ |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 24 | #include "vl.h" |
| 25 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 26 | #include <unistd.h> |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <signal.h> |
| 29 | #include <time.h> |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 30 | #include <errno.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 31 | #include <sys/time.h> |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 32 | #include <zlib.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 33 | |
| 34 | #ifndef _WIN32 |
| 35 | #include <sys/times.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 36 | #include <sys/wait.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 37 | #include <termios.h> |
| 38 | #include <sys/poll.h> |
| 39 | #include <sys/mman.h> |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 40 | #include <sys/ioctl.h> |
| 41 | #include <sys/socket.h> |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 42 | #include <netinet/in.h> |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 43 | #include <dirent.h> |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 44 | #include <netdb.h> |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 45 | #ifdef _BSD |
| 46 | #include <sys/stat.h> |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 47 | #ifndef __APPLE__ |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 48 | #include <libutil.h> |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 49 | #endif |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 50 | #else |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 51 | #ifndef __sun__ |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 52 | #include <linux/if.h> |
| 53 | #include <linux/if_tun.h> |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 54 | #include <pty.h> |
| 55 | #include <malloc.h> |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 56 | #include <linux/rtc.h> |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 57 | #include <linux/ppdev.h> |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 58 | #include <linux/parport.h> |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 59 | #else |
| 60 | #include <sys/stat.h> |
| 61 | #include <sys/ethernet.h> |
| 62 | #include <sys/sockio.h> |
| 63 | #include <arpa/inet.h> |
| 64 | #include <netinet/arp.h> |
| 65 | #include <netinet/in.h> |
| 66 | #include <netinet/in_systm.h> |
| 67 | #include <netinet/ip.h> |
| 68 | #include <netinet/ip_icmp.h> // must come after ip.h |
| 69 | #include <netinet/udp.h> |
| 70 | #include <netinet/tcp.h> |
| 71 | #include <net/if.h> |
| 72 | #include <syslog.h> |
| 73 | #include <stropts.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 74 | #endif |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 75 | #endif |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 76 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 77 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 78 | #if defined(CONFIG_SLIRP) |
| 79 | #include "libslirp.h" |
| 80 | #endif |
| 81 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 82 | #ifdef _WIN32 |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 83 | #include <malloc.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 84 | #include <sys/timeb.h> |
| 85 | #include <windows.h> |
| 86 | #define getopt_long_only getopt_long |
| 87 | #define memalign(align, size) malloc(size) |
| 88 | #endif |
| 89 | |
bellard | 6ca957f | 2006-04-30 22:53:25 +0000 | [diff] [blame] | 90 | #include "qemu_socket.h" |
| 91 | |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 92 | #ifdef CONFIG_SDL |
bellard | 96bcd4f | 2004-07-10 16:26:15 +0000 | [diff] [blame] | 93 | #ifdef __APPLE__ |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 94 | #include <SDL/SDL.h> |
bellard | 96bcd4f | 2004-07-10 16:26:15 +0000 | [diff] [blame] | 95 | #endif |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 96 | #endif /* CONFIG_SDL */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 97 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 98 | #ifdef CONFIG_COCOA |
| 99 | #undef main |
| 100 | #define main qemu_main |
| 101 | #endif /* CONFIG_COCOA */ |
| 102 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 103 | #include "disas.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 104 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 105 | #include "exec-all.h" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 106 | |
bellard | 5a67135 | 2003-10-01 00:13:48 +0000 | [diff] [blame] | 107 | #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
pbrook | a14d6c8 | 2006-12-23 15:37:33 +0000 | [diff] [blame] | 108 | #ifdef __sun__ |
| 109 | #define SMBD_COMMAND "/usr/sfw/sbin/smbd" |
| 110 | #else |
| 111 | #define SMBD_COMMAND "/usr/sbin/smbd" |
| 112 | #endif |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 113 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 114 | //#define DEBUG_UNUSED_IOPORT |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 115 | //#define DEBUG_IOPORT |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 116 | |
bellard | bb551fa | 2004-01-24 13:42:26 +0000 | [diff] [blame] | 117 | #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024) |
bellard | 7916e22 | 2003-07-01 16:27:45 +0000 | [diff] [blame] | 118 | |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 119 | #ifdef TARGET_PPC |
| 120 | #define DEFAULT_RAM_SIZE 144 |
| 121 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 122 | #define DEFAULT_RAM_SIZE 128 |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 123 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 124 | /* in ms */ |
| 125 | #define GUI_REFRESH_INTERVAL 30 |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 126 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 127 | /* Max number of USB devices that can be specified on the commandline. */ |
| 128 | #define MAX_USB_CMDLINE 8 |
| 129 | |
bellard | 7dea1da | 2003-11-16 15:59:30 +0000 | [diff] [blame] | 130 | /* XXX: use a two level table to limit memory usage */ |
| 131 | #define MAX_IOPORTS 65536 |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 132 | |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 133 | const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 134 | char phys_ram_file[1024]; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 135 | void *ioport_opaque[MAX_IOPORTS]; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 136 | IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS]; |
| 137 | IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS]; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 138 | /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available |
| 139 | to store the VM snapshots */ |
| 140 | BlockDriverState *bs_table[MAX_DISKS + 1], *fd_table[MAX_FD]; |
| 141 | /* point to the block driver where the snapshots are managed */ |
| 142 | BlockDriverState *bs_snapshots; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 143 | int vga_ram_size; |
| 144 | static DisplayState display_state; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 145 | int nographic; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 146 | const char* keyboard_layout = NULL; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 147 | int64_t ticks_per_sec; |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 148 | int boot_device = 'c'; |
bellard | 0ced658 | 2004-05-23 21:06:12 +0000 | [diff] [blame] | 149 | int ram_size; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 150 | int pit_min_timer_count = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 151 | int nb_nics; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 152 | NICInfo nd_table[MAX_NICS]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 153 | QEMUTimer *gui_timer; |
| 154 | int vm_running; |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 155 | int rtc_utc = 1; |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 156 | int cirrus_vga_enabled = 1; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 157 | #ifdef TARGET_SPARC |
| 158 | int graphic_width = 1024; |
| 159 | int graphic_height = 768; |
| 160 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 161 | int graphic_width = 800; |
| 162 | int graphic_height = 600; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 163 | #endif |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 164 | int graphic_depth = 15; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 165 | int full_screen = 0; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 166 | int no_quit = 0; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 167 | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 168 | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 169 | #ifdef TARGET_I386 |
| 170 | int win2k_install_hack = 0; |
| 171 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 172 | int usb_enabled = 0; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 173 | static VLANState *first_vlan; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 174 | int smp_cpus = 1; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 175 | const char *vnc_display; |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 176 | #if defined(TARGET_SPARC) |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 177 | #define MAX_CPUS 16 |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 178 | #elif defined(TARGET_I386) |
| 179 | #define MAX_CPUS 255 |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 180 | #else |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 181 | #define MAX_CPUS 1 |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 182 | #endif |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 183 | int acpi_enabled = 1; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 184 | int fd_bootchk = 1; |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 185 | int no_reboot = 0; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 186 | int daemonize = 0; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 187 | const char *option_rom[MAX_OPTION_ROMS]; |
| 188 | int nb_option_roms; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 189 | int semihosting_enabled = 0; |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 190 | int autostart = 1; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 191 | |
| 192 | /***********************************************************/ |
bellard | 26aa7d7 | 2004-04-28 22:26:05 +0000 | [diff] [blame] | 193 | /* x86 ISA bus support */ |
| 194 | |
| 195 | target_phys_addr_t isa_mem_base = 0; |
bellard | 3de388f | 2005-07-02 18:11:44 +0000 | [diff] [blame] | 196 | PicState2 *isa_pic; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 197 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 198 | uint32_t default_ioport_readb(void *opaque, uint32_t address) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 199 | { |
| 200 | #ifdef DEBUG_UNUSED_IOPORT |
| 201 | fprintf(stderr, "inb: port=0x%04x\n", address); |
| 202 | #endif |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 203 | return 0xff; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 204 | } |
| 205 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 206 | void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 207 | { |
| 208 | #ifdef DEBUG_UNUSED_IOPORT |
| 209 | fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data); |
| 210 | #endif |
| 211 | } |
| 212 | |
| 213 | /* default is to make two byte accesses */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 214 | uint32_t default_ioport_readw(void *opaque, uint32_t address) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 215 | { |
| 216 | uint32_t data; |
bellard | db45c29 | 2004-05-12 19:50:26 +0000 | [diff] [blame] | 217 | data = ioport_read_table[0][address](ioport_opaque[address], address); |
| 218 | address = (address + 1) & (MAX_IOPORTS - 1); |
| 219 | data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 220 | return data; |
| 221 | } |
| 222 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 223 | void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 224 | { |
bellard | db45c29 | 2004-05-12 19:50:26 +0000 | [diff] [blame] | 225 | ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff); |
| 226 | address = (address + 1) & (MAX_IOPORTS - 1); |
| 227 | ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 228 | } |
| 229 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 230 | uint32_t default_ioport_readl(void *opaque, uint32_t address) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 231 | { |
| 232 | #ifdef DEBUG_UNUSED_IOPORT |
| 233 | fprintf(stderr, "inl: port=0x%04x\n", address); |
| 234 | #endif |
| 235 | return 0xffffffff; |
| 236 | } |
| 237 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 238 | void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 239 | { |
| 240 | #ifdef DEBUG_UNUSED_IOPORT |
| 241 | fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data); |
| 242 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void init_ioports(void) |
| 246 | { |
| 247 | int i; |
| 248 | |
| 249 | for(i = 0; i < MAX_IOPORTS; i++) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 250 | ioport_read_table[0][i] = default_ioport_readb; |
| 251 | ioport_write_table[0][i] = default_ioport_writeb; |
| 252 | ioport_read_table[1][i] = default_ioport_readw; |
| 253 | ioport_write_table[1][i] = default_ioport_writew; |
| 254 | ioport_read_table[2][i] = default_ioport_readl; |
| 255 | ioport_write_table[2][i] = default_ioport_writel; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 259 | /* size is the word size in byte */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 260 | int register_ioport_read(int start, int length, int size, |
| 261 | IOPortReadFunc *func, void *opaque) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 262 | { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 263 | int i, bsize; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 264 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 265 | if (size == 1) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 266 | bsize = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 267 | } else if (size == 2) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 268 | bsize = 1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 269 | } else if (size == 4) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 270 | bsize = 2; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 271 | } else { |
| 272 | hw_error("register_ioport_read: invalid size"); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 273 | return -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 274 | } |
| 275 | for(i = start; i < start + length; i += size) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 276 | ioport_read_table[bsize][i] = func; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 277 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
| 278 | hw_error("register_ioport_read: invalid opaque"); |
| 279 | ioport_opaque[i] = opaque; |
| 280 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 281 | return 0; |
| 282 | } |
| 283 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 284 | /* size is the word size in byte */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 285 | int register_ioport_write(int start, int length, int size, |
| 286 | IOPortWriteFunc *func, void *opaque) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 287 | { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 288 | int i, bsize; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 289 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 290 | if (size == 1) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 291 | bsize = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 292 | } else if (size == 2) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 293 | bsize = 1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 294 | } else if (size == 4) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 295 | bsize = 2; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 296 | } else { |
| 297 | hw_error("register_ioport_write: invalid size"); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 298 | return -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 299 | } |
| 300 | for(i = start; i < start + length; i += size) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 301 | ioport_write_table[bsize][i] = func; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 302 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 303 | hw_error("register_ioport_write: invalid opaque"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 304 | ioport_opaque[i] = opaque; |
| 305 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
bellard | 69b9103 | 2004-05-18 23:05:28 +0000 | [diff] [blame] | 309 | void isa_unassign_ioport(int start, int length) |
| 310 | { |
| 311 | int i; |
| 312 | |
| 313 | for(i = start; i < start + length; i++) { |
| 314 | ioport_read_table[0][i] = default_ioport_readb; |
| 315 | ioport_read_table[1][i] = default_ioport_readw; |
| 316 | ioport_read_table[2][i] = default_ioport_readl; |
| 317 | |
| 318 | ioport_write_table[0][i] = default_ioport_writeb; |
| 319 | ioport_write_table[1][i] = default_ioport_writew; |
| 320 | ioport_write_table[2][i] = default_ioport_writel; |
| 321 | } |
| 322 | } |
| 323 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 324 | /***********************************************************/ |
| 325 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 326 | void cpu_outb(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 327 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 328 | #ifdef DEBUG_IOPORT |
| 329 | if (loglevel & CPU_LOG_IOPORT) |
| 330 | fprintf(logfile, "outb: %04x %02x\n", addr, val); |
| 331 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 332 | ioport_write_table[0][addr](ioport_opaque[addr], addr, val); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 333 | #ifdef USE_KQEMU |
| 334 | if (env) |
| 335 | env->last_io_time = cpu_get_time_fast(); |
| 336 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 337 | } |
| 338 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 339 | void cpu_outw(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 340 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 341 | #ifdef DEBUG_IOPORT |
| 342 | if (loglevel & CPU_LOG_IOPORT) |
| 343 | fprintf(logfile, "outw: %04x %04x\n", addr, val); |
| 344 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 345 | ioport_write_table[1][addr](ioport_opaque[addr], addr, val); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 346 | #ifdef USE_KQEMU |
| 347 | if (env) |
| 348 | env->last_io_time = cpu_get_time_fast(); |
| 349 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 350 | } |
| 351 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 352 | void cpu_outl(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 353 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 354 | #ifdef DEBUG_IOPORT |
| 355 | if (loglevel & CPU_LOG_IOPORT) |
| 356 | fprintf(logfile, "outl: %04x %08x\n", addr, val); |
| 357 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 358 | ioport_write_table[2][addr](ioport_opaque[addr], addr, val); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 359 | #ifdef USE_KQEMU |
| 360 | if (env) |
| 361 | env->last_io_time = cpu_get_time_fast(); |
| 362 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 363 | } |
| 364 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 365 | int cpu_inb(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 366 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 367 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 368 | val = ioport_read_table[0][addr](ioport_opaque[addr], addr); |
| 369 | #ifdef DEBUG_IOPORT |
| 370 | if (loglevel & CPU_LOG_IOPORT) |
| 371 | fprintf(logfile, "inb : %04x %02x\n", addr, val); |
| 372 | #endif |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 373 | #ifdef USE_KQEMU |
| 374 | if (env) |
| 375 | env->last_io_time = cpu_get_time_fast(); |
| 376 | #endif |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 377 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 378 | } |
| 379 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 380 | int cpu_inw(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 381 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 382 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 383 | val = ioport_read_table[1][addr](ioport_opaque[addr], addr); |
| 384 | #ifdef DEBUG_IOPORT |
| 385 | if (loglevel & CPU_LOG_IOPORT) |
| 386 | fprintf(logfile, "inw : %04x %04x\n", addr, val); |
| 387 | #endif |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 388 | #ifdef USE_KQEMU |
| 389 | if (env) |
| 390 | env->last_io_time = cpu_get_time_fast(); |
| 391 | #endif |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 392 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 393 | } |
| 394 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 395 | int cpu_inl(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 396 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 397 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 398 | val = ioport_read_table[2][addr](ioport_opaque[addr], addr); |
| 399 | #ifdef DEBUG_IOPORT |
| 400 | if (loglevel & CPU_LOG_IOPORT) |
| 401 | fprintf(logfile, "inl : %04x %08x\n", addr, val); |
| 402 | #endif |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 403 | #ifdef USE_KQEMU |
| 404 | if (env) |
| 405 | env->last_io_time = cpu_get_time_fast(); |
| 406 | #endif |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 407 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /***********************************************************/ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 411 | void hw_error(const char *fmt, ...) |
| 412 | { |
| 413 | va_list ap; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 414 | CPUState *env; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 415 | |
| 416 | va_start(ap, fmt); |
| 417 | fprintf(stderr, "qemu: hardware error: "); |
| 418 | vfprintf(stderr, fmt, ap); |
| 419 | fprintf(stderr, "\n"); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 420 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 421 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 422 | #ifdef TARGET_I386 |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 423 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 424 | #else |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 425 | cpu_dump_state(env, stderr, fprintf, 0); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 426 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 427 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 428 | va_end(ap); |
| 429 | abort(); |
| 430 | } |
| 431 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 432 | /***********************************************************/ |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 433 | /* keyboard/mouse */ |
| 434 | |
| 435 | static QEMUPutKBDEvent *qemu_put_kbd_event; |
| 436 | static void *qemu_put_kbd_event_opaque; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 437 | static QEMUPutMouseEntry *qemu_put_mouse_event_head; |
| 438 | static QEMUPutMouseEntry *qemu_put_mouse_event_current; |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 439 | |
| 440 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
| 441 | { |
| 442 | qemu_put_kbd_event_opaque = opaque; |
| 443 | qemu_put_kbd_event = func; |
| 444 | } |
| 445 | |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 446 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
| 447 | void *opaque, int absolute, |
| 448 | const char *name) |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 449 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 450 | QEMUPutMouseEntry *s, *cursor; |
| 451 | |
| 452 | s = qemu_mallocz(sizeof(QEMUPutMouseEntry)); |
| 453 | if (!s) |
| 454 | return NULL; |
| 455 | |
| 456 | s->qemu_put_mouse_event = func; |
| 457 | s->qemu_put_mouse_event_opaque = opaque; |
| 458 | s->qemu_put_mouse_event_absolute = absolute; |
| 459 | s->qemu_put_mouse_event_name = qemu_strdup(name); |
| 460 | s->next = NULL; |
| 461 | |
| 462 | if (!qemu_put_mouse_event_head) { |
| 463 | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
| 464 | return s; |
| 465 | } |
| 466 | |
| 467 | cursor = qemu_put_mouse_event_head; |
| 468 | while (cursor->next != NULL) |
| 469 | cursor = cursor->next; |
| 470 | |
| 471 | cursor->next = s; |
| 472 | qemu_put_mouse_event_current = s; |
| 473 | |
| 474 | return s; |
| 475 | } |
| 476 | |
| 477 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) |
| 478 | { |
| 479 | QEMUPutMouseEntry *prev = NULL, *cursor; |
| 480 | |
| 481 | if (!qemu_put_mouse_event_head || entry == NULL) |
| 482 | return; |
| 483 | |
| 484 | cursor = qemu_put_mouse_event_head; |
| 485 | while (cursor != NULL && cursor != entry) { |
| 486 | prev = cursor; |
| 487 | cursor = cursor->next; |
| 488 | } |
| 489 | |
| 490 | if (cursor == NULL) // does not exist or list empty |
| 491 | return; |
| 492 | else if (prev == NULL) { // entry is head |
| 493 | qemu_put_mouse_event_head = cursor->next; |
| 494 | if (qemu_put_mouse_event_current == entry) |
| 495 | qemu_put_mouse_event_current = cursor->next; |
| 496 | qemu_free(entry->qemu_put_mouse_event_name); |
| 497 | qemu_free(entry); |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | prev->next = entry->next; |
| 502 | |
| 503 | if (qemu_put_mouse_event_current == entry) |
| 504 | qemu_put_mouse_event_current = prev; |
| 505 | |
| 506 | qemu_free(entry->qemu_put_mouse_event_name); |
| 507 | qemu_free(entry); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | void kbd_put_keycode(int keycode) |
| 511 | { |
| 512 | if (qemu_put_kbd_event) { |
| 513 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
| 518 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 519 | QEMUPutMouseEvent *mouse_event; |
| 520 | void *mouse_event_opaque; |
| 521 | |
| 522 | if (!qemu_put_mouse_event_current) { |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | mouse_event = |
| 527 | qemu_put_mouse_event_current->qemu_put_mouse_event; |
| 528 | mouse_event_opaque = |
| 529 | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
| 530 | |
| 531 | if (mouse_event) { |
| 532 | mouse_event(mouse_event_opaque, dx, dy, dz, buttons_state); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 536 | int kbd_mouse_is_absolute(void) |
| 537 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 538 | if (!qemu_put_mouse_event_current) |
| 539 | return 0; |
| 540 | |
| 541 | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute; |
| 542 | } |
| 543 | |
| 544 | void do_info_mice(void) |
| 545 | { |
| 546 | QEMUPutMouseEntry *cursor; |
| 547 | int index = 0; |
| 548 | |
| 549 | if (!qemu_put_mouse_event_head) { |
| 550 | term_printf("No mouse devices connected\n"); |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | term_printf("Mouse devices available:\n"); |
| 555 | cursor = qemu_put_mouse_event_head; |
| 556 | while (cursor != NULL) { |
| 557 | term_printf("%c Mouse #%d: %s\n", |
| 558 | (cursor == qemu_put_mouse_event_current ? '*' : ' '), |
| 559 | index, cursor->qemu_put_mouse_event_name); |
| 560 | index++; |
| 561 | cursor = cursor->next; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | void do_mouse_set(int index) |
| 566 | { |
| 567 | QEMUPutMouseEntry *cursor; |
| 568 | int i = 0; |
| 569 | |
| 570 | if (!qemu_put_mouse_event_head) { |
| 571 | term_printf("No mouse devices connected\n"); |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | cursor = qemu_put_mouse_event_head; |
| 576 | while (cursor != NULL && index != i) { |
| 577 | i++; |
| 578 | cursor = cursor->next; |
| 579 | } |
| 580 | |
| 581 | if (cursor != NULL) |
| 582 | qemu_put_mouse_event_current = cursor; |
| 583 | else |
| 584 | term_printf("Mouse at given index not found\n"); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 585 | } |
| 586 | |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 587 | /* compute with 96 bit intermediate result: (a*b)/c */ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 588 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 589 | { |
| 590 | union { |
| 591 | uint64_t ll; |
| 592 | struct { |
| 593 | #ifdef WORDS_BIGENDIAN |
| 594 | uint32_t high, low; |
| 595 | #else |
| 596 | uint32_t low, high; |
| 597 | #endif |
| 598 | } l; |
| 599 | } u, res; |
| 600 | uint64_t rl, rh; |
| 601 | |
| 602 | u.ll = a; |
| 603 | rl = (uint64_t)u.l.low * (uint64_t)b; |
| 604 | rh = (uint64_t)u.l.high * (uint64_t)b; |
| 605 | rh += (rl >> 32); |
| 606 | res.l.high = rh / c; |
| 607 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
| 608 | return res.ll; |
| 609 | } |
| 610 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 611 | /***********************************************************/ |
| 612 | /* real time host monotonic timer */ |
| 613 | |
| 614 | #define QEMU_TIMER_BASE 1000000000LL |
| 615 | |
| 616 | #ifdef WIN32 |
| 617 | |
| 618 | static int64_t clock_freq; |
| 619 | |
| 620 | static void init_get_clock(void) |
| 621 | { |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 622 | LARGE_INTEGER freq; |
| 623 | int ret; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 624 | ret = QueryPerformanceFrequency(&freq); |
| 625 | if (ret == 0) { |
| 626 | fprintf(stderr, "Could not calibrate ticks\n"); |
| 627 | exit(1); |
| 628 | } |
| 629 | clock_freq = freq.QuadPart; |
| 630 | } |
| 631 | |
| 632 | static int64_t get_clock(void) |
| 633 | { |
| 634 | LARGE_INTEGER ti; |
| 635 | QueryPerformanceCounter(&ti); |
| 636 | return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq); |
| 637 | } |
| 638 | |
| 639 | #else |
| 640 | |
| 641 | static int use_rt_clock; |
| 642 | |
| 643 | static void init_get_clock(void) |
| 644 | { |
| 645 | use_rt_clock = 0; |
| 646 | #if defined(__linux__) |
| 647 | { |
| 648 | struct timespec ts; |
| 649 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 650 | use_rt_clock = 1; |
| 651 | } |
| 652 | } |
| 653 | #endif |
| 654 | } |
| 655 | |
| 656 | static int64_t get_clock(void) |
| 657 | { |
| 658 | #if defined(__linux__) |
| 659 | if (use_rt_clock) { |
| 660 | struct timespec ts; |
| 661 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 662 | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
| 663 | } else |
| 664 | #endif |
| 665 | { |
| 666 | /* XXX: using gettimeofday leads to problems if the date |
| 667 | changes, so it should be avoided. */ |
| 668 | struct timeval tv; |
| 669 | gettimeofday(&tv, NULL); |
| 670 | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | #endif |
| 675 | |
| 676 | /***********************************************************/ |
| 677 | /* guest cycle counter */ |
| 678 | |
| 679 | static int64_t cpu_ticks_prev; |
| 680 | static int64_t cpu_ticks_offset; |
| 681 | static int64_t cpu_clock_offset; |
| 682 | static int cpu_ticks_enabled; |
| 683 | |
| 684 | /* return the host CPU cycle counter and handle stop/restart */ |
| 685 | int64_t cpu_get_ticks(void) |
| 686 | { |
| 687 | if (!cpu_ticks_enabled) { |
| 688 | return cpu_ticks_offset; |
| 689 | } else { |
| 690 | int64_t ticks; |
| 691 | ticks = cpu_get_real_ticks(); |
| 692 | if (cpu_ticks_prev > ticks) { |
| 693 | /* Note: non increasing ticks may happen if the host uses |
| 694 | software suspend */ |
| 695 | cpu_ticks_offset += cpu_ticks_prev - ticks; |
| 696 | } |
| 697 | cpu_ticks_prev = ticks; |
| 698 | return ticks + cpu_ticks_offset; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /* return the host CPU monotonic timer and handle stop/restart */ |
| 703 | static int64_t cpu_get_clock(void) |
| 704 | { |
| 705 | int64_t ti; |
| 706 | if (!cpu_ticks_enabled) { |
| 707 | return cpu_clock_offset; |
| 708 | } else { |
| 709 | ti = get_clock(); |
| 710 | return ti + cpu_clock_offset; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | /* enable cpu_get_ticks() */ |
| 715 | void cpu_enable_ticks(void) |
| 716 | { |
| 717 | if (!cpu_ticks_enabled) { |
| 718 | cpu_ticks_offset -= cpu_get_real_ticks(); |
| 719 | cpu_clock_offset -= get_clock(); |
| 720 | cpu_ticks_enabled = 1; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
| 725 | cpu_get_ticks() after that. */ |
| 726 | void cpu_disable_ticks(void) |
| 727 | { |
| 728 | if (cpu_ticks_enabled) { |
| 729 | cpu_ticks_offset = cpu_get_ticks(); |
| 730 | cpu_clock_offset = cpu_get_clock(); |
| 731 | cpu_ticks_enabled = 0; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | /***********************************************************/ |
| 736 | /* timers */ |
| 737 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 738 | #define QEMU_TIMER_REALTIME 0 |
| 739 | #define QEMU_TIMER_VIRTUAL 1 |
| 740 | |
| 741 | struct QEMUClock { |
| 742 | int type; |
| 743 | /* XXX: add frequency */ |
| 744 | }; |
| 745 | |
| 746 | struct QEMUTimer { |
| 747 | QEMUClock *clock; |
| 748 | int64_t expire_time; |
| 749 | QEMUTimerCB *cb; |
| 750 | void *opaque; |
| 751 | struct QEMUTimer *next; |
| 752 | }; |
| 753 | |
| 754 | QEMUClock *rt_clock; |
| 755 | QEMUClock *vm_clock; |
| 756 | |
| 757 | static QEMUTimer *active_timers[2]; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 758 | #ifdef _WIN32 |
| 759 | static MMRESULT timerID; |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 760 | static HANDLE host_alarm = NULL; |
| 761 | static unsigned int period = 1; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 762 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 763 | /* frequency of the times() clock tick */ |
| 764 | static int timer_freq; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 765 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 766 | |
| 767 | QEMUClock *qemu_new_clock(int type) |
| 768 | { |
| 769 | QEMUClock *clock; |
| 770 | clock = qemu_mallocz(sizeof(QEMUClock)); |
| 771 | if (!clock) |
| 772 | return NULL; |
| 773 | clock->type = type; |
| 774 | return clock; |
| 775 | } |
| 776 | |
| 777 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) |
| 778 | { |
| 779 | QEMUTimer *ts; |
| 780 | |
| 781 | ts = qemu_mallocz(sizeof(QEMUTimer)); |
| 782 | ts->clock = clock; |
| 783 | ts->cb = cb; |
| 784 | ts->opaque = opaque; |
| 785 | return ts; |
| 786 | } |
| 787 | |
| 788 | void qemu_free_timer(QEMUTimer *ts) |
| 789 | { |
| 790 | qemu_free(ts); |
| 791 | } |
| 792 | |
| 793 | /* stop a timer, but do not dealloc it */ |
| 794 | void qemu_del_timer(QEMUTimer *ts) |
| 795 | { |
| 796 | QEMUTimer **pt, *t; |
| 797 | |
| 798 | /* NOTE: this code must be signal safe because |
| 799 | qemu_timer_expired() can be called from a signal. */ |
| 800 | pt = &active_timers[ts->clock->type]; |
| 801 | for(;;) { |
| 802 | t = *pt; |
| 803 | if (!t) |
| 804 | break; |
| 805 | if (t == ts) { |
| 806 | *pt = t->next; |
| 807 | break; |
| 808 | } |
| 809 | pt = &t->next; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | /* modify the current timer so that it will be fired when current_time |
| 814 | >= expire_time. The corresponding callback will be called. */ |
| 815 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
| 816 | { |
| 817 | QEMUTimer **pt, *t; |
| 818 | |
| 819 | qemu_del_timer(ts); |
| 820 | |
| 821 | /* add the timer in the sorted list */ |
| 822 | /* NOTE: this code must be signal safe because |
| 823 | qemu_timer_expired() can be called from a signal. */ |
| 824 | pt = &active_timers[ts->clock->type]; |
| 825 | for(;;) { |
| 826 | t = *pt; |
| 827 | if (!t) |
| 828 | break; |
| 829 | if (t->expire_time > expire_time) |
| 830 | break; |
| 831 | pt = &t->next; |
| 832 | } |
| 833 | ts->expire_time = expire_time; |
| 834 | ts->next = *pt; |
| 835 | *pt = ts; |
| 836 | } |
| 837 | |
| 838 | int qemu_timer_pending(QEMUTimer *ts) |
| 839 | { |
| 840 | QEMUTimer *t; |
| 841 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
| 842 | if (t == ts) |
| 843 | return 1; |
| 844 | } |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
| 849 | { |
| 850 | if (!timer_head) |
| 851 | return 0; |
| 852 | return (timer_head->expire_time <= current_time); |
| 853 | } |
| 854 | |
| 855 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
| 856 | { |
| 857 | QEMUTimer *ts; |
| 858 | |
| 859 | for(;;) { |
| 860 | ts = *ptimer_head; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 861 | if (!ts || ts->expire_time > current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 862 | break; |
| 863 | /* remove timer from the list before calling the callback */ |
| 864 | *ptimer_head = ts->next; |
| 865 | ts->next = NULL; |
| 866 | |
| 867 | /* run the callback (the timer list can be modified) */ |
| 868 | ts->cb(ts->opaque); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | int64_t qemu_get_clock(QEMUClock *clock) |
| 873 | { |
| 874 | switch(clock->type) { |
| 875 | case QEMU_TIMER_REALTIME: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 876 | return get_clock() / 1000000; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 877 | default: |
| 878 | case QEMU_TIMER_VIRTUAL: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 879 | return cpu_get_clock(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 883 | static void init_timers(void) |
| 884 | { |
| 885 | init_get_clock(); |
| 886 | ticks_per_sec = QEMU_TIMER_BASE; |
| 887 | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
| 888 | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
| 889 | } |
| 890 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 891 | /* save a timer */ |
| 892 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) |
| 893 | { |
| 894 | uint64_t expire_time; |
| 895 | |
| 896 | if (qemu_timer_pending(ts)) { |
| 897 | expire_time = ts->expire_time; |
| 898 | } else { |
| 899 | expire_time = -1; |
| 900 | } |
| 901 | qemu_put_be64(f, expire_time); |
| 902 | } |
| 903 | |
| 904 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) |
| 905 | { |
| 906 | uint64_t expire_time; |
| 907 | |
| 908 | expire_time = qemu_get_be64(f); |
| 909 | if (expire_time != -1) { |
| 910 | qemu_mod_timer(ts, expire_time); |
| 911 | } else { |
| 912 | qemu_del_timer(ts); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | static void timer_save(QEMUFile *f, void *opaque) |
| 917 | { |
| 918 | if (cpu_ticks_enabled) { |
| 919 | hw_error("cannot save state if virtual timers are running"); |
| 920 | } |
| 921 | qemu_put_be64s(f, &cpu_ticks_offset); |
| 922 | qemu_put_be64s(f, &ticks_per_sec); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 923 | qemu_put_be64s(f, &cpu_clock_offset); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | static int timer_load(QEMUFile *f, void *opaque, int version_id) |
| 927 | { |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 928 | if (version_id != 1 && version_id != 2) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 929 | return -EINVAL; |
| 930 | if (cpu_ticks_enabled) { |
| 931 | return -EINVAL; |
| 932 | } |
| 933 | qemu_get_be64s(f, &cpu_ticks_offset); |
| 934 | qemu_get_be64s(f, &ticks_per_sec); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 935 | if (version_id == 2) { |
| 936 | qemu_get_be64s(f, &cpu_clock_offset); |
| 937 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 938 | return 0; |
| 939 | } |
| 940 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 941 | #ifdef _WIN32 |
| 942 | void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
| 943 | DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
| 944 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 945 | static void host_alarm_handler(int host_signum) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 946 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 947 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 948 | #if 0 |
| 949 | #define DISP_FREQ 1000 |
| 950 | { |
| 951 | static int64_t delta_min = INT64_MAX; |
| 952 | static int64_t delta_max, delta_cum, last_clock, delta, ti; |
| 953 | static int count; |
| 954 | ti = qemu_get_clock(vm_clock); |
| 955 | if (last_clock != 0) { |
| 956 | delta = ti - last_clock; |
| 957 | if (delta < delta_min) |
| 958 | delta_min = delta; |
| 959 | if (delta > delta_max) |
| 960 | delta_max = delta; |
| 961 | delta_cum += delta; |
| 962 | if (++count == DISP_FREQ) { |
bellard | 26a7646 | 2006-06-25 18:15:32 +0000 | [diff] [blame] | 963 | 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] | 964 | muldiv64(delta_min, 1000000, ticks_per_sec), |
| 965 | muldiv64(delta_max, 1000000, ticks_per_sec), |
| 966 | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec), |
| 967 | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ)); |
| 968 | count = 0; |
| 969 | delta_min = INT64_MAX; |
| 970 | delta_max = 0; |
| 971 | delta_cum = 0; |
| 972 | } |
| 973 | } |
| 974 | last_clock = ti; |
| 975 | } |
| 976 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 977 | if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
| 978 | qemu_get_clock(vm_clock)) || |
| 979 | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
| 980 | qemu_get_clock(rt_clock))) { |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 981 | #ifdef _WIN32 |
| 982 | SetEvent(host_alarm); |
| 983 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 984 | CPUState *env = cpu_single_env; |
| 985 | if (env) { |
| 986 | /* stop the currently executing cpu because a timer occured */ |
| 987 | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
bellard | a332e11 | 2005-09-03 17:55:47 +0000 | [diff] [blame] | 988 | #ifdef USE_KQEMU |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 989 | if (env->kqemu_enabled) { |
| 990 | kqemu_cpu_interrupt(env); |
| 991 | } |
bellard | a332e11 | 2005-09-03 17:55:47 +0000 | [diff] [blame] | 992 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 993 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 997 | #ifndef _WIN32 |
| 998 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 999 | #if defined(__linux__) |
| 1000 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1001 | #define RTC_FREQ 1024 |
| 1002 | |
| 1003 | static int rtc_fd; |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1004 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1005 | static int start_rtc_timer(void) |
| 1006 | { |
| 1007 | rtc_fd = open("/dev/rtc", O_RDONLY); |
| 1008 | if (rtc_fd < 0) |
| 1009 | return -1; |
| 1010 | if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { |
| 1011 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" |
| 1012 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" |
| 1013 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); |
| 1014 | goto fail; |
| 1015 | } |
| 1016 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
| 1017 | fail: |
| 1018 | close(rtc_fd); |
| 1019 | return -1; |
| 1020 | } |
| 1021 | pit_min_timer_count = PIT_FREQ / RTC_FREQ; |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1025 | #else |
| 1026 | |
| 1027 | static int start_rtc_timer(void) |
| 1028 | { |
| 1029 | return -1; |
| 1030 | } |
| 1031 | |
| 1032 | #endif /* !defined(__linux__) */ |
| 1033 | |
| 1034 | #endif /* !defined(_WIN32) */ |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1035 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1036 | static void init_timer_alarm(void) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1037 | { |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1038 | #ifdef _WIN32 |
| 1039 | { |
| 1040 | int count=0; |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1041 | TIMECAPS tc; |
| 1042 | |
| 1043 | ZeroMemory(&tc, sizeof(TIMECAPS)); |
| 1044 | timeGetDevCaps(&tc, sizeof(TIMECAPS)); |
| 1045 | if (period < tc.wPeriodMin) |
| 1046 | period = tc.wPeriodMin; |
| 1047 | timeBeginPeriod(period); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 1048 | timerID = timeSetEvent(1, // interval (ms) |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1049 | period, // resolution |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1050 | host_alarm_handler, // function |
| 1051 | (DWORD)&count, // user parameter |
| 1052 | TIME_PERIODIC | TIME_CALLBACK_FUNCTION); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1053 | if( !timerID ) { |
| 1054 | perror("failed timer alarm"); |
| 1055 | exit(1); |
| 1056 | } |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1057 | host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 1058 | if (!host_alarm) { |
| 1059 | perror("failed CreateEvent"); |
| 1060 | exit(1); |
| 1061 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 1062 | qemu_add_wait_object(host_alarm, NULL, NULL); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1063 | } |
| 1064 | pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000; |
| 1065 | #else |
| 1066 | { |
| 1067 | struct sigaction act; |
| 1068 | struct itimerval itv; |
| 1069 | |
| 1070 | /* get times() syscall frequency */ |
| 1071 | timer_freq = sysconf(_SC_CLK_TCK); |
| 1072 | |
| 1073 | /* timer signal */ |
| 1074 | sigfillset(&act.sa_mask); |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 1075 | act.sa_flags = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1076 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1077 | act.sa_flags |= SA_ONSTACK; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1078 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1079 | act.sa_handler = host_alarm_handler; |
| 1080 | sigaction(SIGALRM, &act, NULL); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1081 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1082 | itv.it_interval.tv_sec = 0; |
bellard | d79284e | 2005-01-26 21:56:26 +0000 | [diff] [blame] | 1083 | itv.it_interval.tv_usec = 999; /* for i386 kernel 2.6 to get 1 ms */ |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1084 | itv.it_value.tv_sec = 0; |
| 1085 | itv.it_value.tv_usec = 10 * 1000; |
| 1086 | setitimer(ITIMER_REAL, &itv, NULL); |
| 1087 | /* we probe the tick duration of the kernel to inform the user if |
| 1088 | the emulated kernel requested a too high timer frequency */ |
| 1089 | getitimer(ITIMER_REAL, &itv); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1090 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1091 | #if defined(__linux__) |
bellard | 29e3055 | 2006-05-01 13:28:36 +0000 | [diff] [blame] | 1092 | /* XXX: force /dev/rtc usage because even 2.6 kernels may not |
| 1093 | have timers with 1 ms resolution. The correct solution will |
| 1094 | be to use the POSIX real time timers available in recent |
| 1095 | 2.6 kernels */ |
| 1096 | if (itv.it_interval.tv_usec > 1000 || 1) { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1097 | /* try to use /dev/rtc to have a faster timer */ |
| 1098 | if (start_rtc_timer() < 0) |
| 1099 | goto use_itimer; |
| 1100 | /* disable itimer */ |
| 1101 | itv.it_interval.tv_sec = 0; |
| 1102 | itv.it_interval.tv_usec = 0; |
| 1103 | itv.it_value.tv_sec = 0; |
| 1104 | itv.it_value.tv_usec = 0; |
| 1105 | setitimer(ITIMER_REAL, &itv, NULL); |
| 1106 | |
| 1107 | /* use the RTC */ |
bellard | a1968d7 | 2004-06-25 17:06:27 +0000 | [diff] [blame] | 1108 | sigaction(SIGIO, &act, NULL); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1109 | fcntl(rtc_fd, F_SETFL, O_ASYNC); |
| 1110 | fcntl(rtc_fd, F_SETOWN, getpid()); |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1111 | } else |
| 1112 | #endif /* defined(__linux__) */ |
| 1113 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1114 | use_itimer: |
| 1115 | pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * |
| 1116 | PIT_FREQ) / 1000000; |
| 1117 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1118 | } |
| 1119 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1122 | void quit_timers(void) |
| 1123 | { |
| 1124 | #ifdef _WIN32 |
| 1125 | timeKillEvent(timerID); |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1126 | timeEndPeriod(period); |
| 1127 | if (host_alarm) { |
| 1128 | CloseHandle(host_alarm); |
| 1129 | host_alarm = NULL; |
| 1130 | } |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1131 | #endif |
| 1132 | } |
| 1133 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1134 | /***********************************************************/ |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1135 | /* character device */ |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 1136 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1137 | static void qemu_chr_event(CharDriverState *s, int event) |
| 1138 | { |
| 1139 | if (!s->chr_event) |
| 1140 | return; |
| 1141 | s->chr_event(s->handler_opaque, event); |
| 1142 | } |
| 1143 | |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1144 | static void qemu_chr_reset_bh(void *opaque) |
| 1145 | { |
| 1146 | CharDriverState *s = opaque; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1147 | qemu_chr_event(s, CHR_EVENT_RESET); |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1148 | qemu_bh_delete(s->bh); |
| 1149 | s->bh = NULL; |
| 1150 | } |
| 1151 | |
| 1152 | void qemu_chr_reset(CharDriverState *s) |
| 1153 | { |
| 1154 | if (s->bh == NULL) { |
| 1155 | s->bh = qemu_bh_new(qemu_chr_reset_bh, s); |
| 1156 | qemu_bh_schedule(s->bh); |
| 1157 | } |
| 1158 | } |
| 1159 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1160 | int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1161 | { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1162 | return s->chr_write(s, buf, len); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1165 | int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1166 | { |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1167 | if (!s->chr_ioctl) |
| 1168 | return -ENOTSUP; |
| 1169 | return s->chr_ioctl(s, cmd, arg); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1172 | int qemu_chr_can_read(CharDriverState *s) |
| 1173 | { |
| 1174 | if (!s->chr_can_read) |
| 1175 | return 0; |
| 1176 | return s->chr_can_read(s->handler_opaque); |
| 1177 | } |
| 1178 | |
| 1179 | void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len) |
| 1180 | { |
| 1181 | s->chr_read(s->handler_opaque, buf, len); |
| 1182 | } |
| 1183 | |
| 1184 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1185 | void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1186 | { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1187 | char buf[4096]; |
| 1188 | va_list ap; |
| 1189 | va_start(ap, fmt); |
| 1190 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 1191 | qemu_chr_write(s, buf, strlen(buf)); |
| 1192 | va_end(ap); |
| 1193 | } |
| 1194 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 1195 | void qemu_chr_send_event(CharDriverState *s, int event) |
| 1196 | { |
| 1197 | if (s->chr_send_event) |
| 1198 | s->chr_send_event(s, event); |
| 1199 | } |
| 1200 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1201 | void qemu_chr_add_handlers(CharDriverState *s, |
| 1202 | IOCanRWHandler *fd_can_read, |
| 1203 | IOReadHandler *fd_read, |
| 1204 | IOEventHandler *fd_event, |
| 1205 | void *opaque) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1206 | { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1207 | s->chr_can_read = fd_can_read; |
| 1208 | s->chr_read = fd_read; |
| 1209 | s->chr_event = fd_event; |
| 1210 | s->handler_opaque = opaque; |
| 1211 | if (s->chr_update_read_handler) |
| 1212 | s->chr_update_read_handler(s); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1215 | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1216 | { |
| 1217 | return len; |
| 1218 | } |
| 1219 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1220 | static CharDriverState *qemu_chr_open_null(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1221 | { |
| 1222 | CharDriverState *chr; |
| 1223 | |
| 1224 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1225 | if (!chr) |
| 1226 | return NULL; |
| 1227 | chr->chr_write = null_chr_write; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1228 | return chr; |
| 1229 | } |
| 1230 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame^] | 1231 | /* MUX driver for serial I/O splitting */ |
| 1232 | static int term_timestamps; |
| 1233 | static int64_t term_timestamps_start; |
| 1234 | #define MAX_MUX 2 |
| 1235 | typedef struct { |
| 1236 | IOCanRWHandler *chr_can_read[MAX_MUX]; |
| 1237 | IOReadHandler *chr_read[MAX_MUX]; |
| 1238 | IOEventHandler *chr_event[MAX_MUX]; |
| 1239 | void *ext_opaque[MAX_MUX]; |
| 1240 | CharDriverState *drv; |
| 1241 | int mux_cnt; |
| 1242 | int term_got_escape; |
| 1243 | int max_size; |
| 1244 | } MuxDriver; |
| 1245 | |
| 1246 | |
| 1247 | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1248 | { |
| 1249 | MuxDriver *d = chr->opaque; |
| 1250 | int ret; |
| 1251 | if (!term_timestamps) { |
| 1252 | ret = d->drv->chr_write(d->drv, buf, len); |
| 1253 | } else { |
| 1254 | int i; |
| 1255 | |
| 1256 | ret = 0; |
| 1257 | for(i = 0; i < len; i++) { |
| 1258 | ret += d->drv->chr_write(d->drv, buf+i, 1); |
| 1259 | if (buf[i] == '\n') { |
| 1260 | char buf1[64]; |
| 1261 | int64_t ti; |
| 1262 | int secs; |
| 1263 | |
| 1264 | ti = get_clock(); |
| 1265 | if (term_timestamps_start == -1) |
| 1266 | term_timestamps_start = ti; |
| 1267 | ti -= term_timestamps_start; |
| 1268 | secs = ti / 1000000000; |
| 1269 | snprintf(buf1, sizeof(buf1), |
| 1270 | "[%02d:%02d:%02d.%03d] ", |
| 1271 | secs / 3600, |
| 1272 | (secs / 60) % 60, |
| 1273 | secs % 60, |
| 1274 | (int)((ti / 1000000) % 1000)); |
| 1275 | d->drv->chr_write(d->drv, buf1, strlen(buf1)); |
| 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | return ret; |
| 1280 | } |
| 1281 | |
| 1282 | static char *mux_help[] = { |
| 1283 | "% h print this help\n\r", |
| 1284 | "% x exit emulator\n\r", |
| 1285 | "% s save disk data back to file (if -snapshot)\n\r", |
| 1286 | "% t toggle console timestamps\n\r" |
| 1287 | "% b send break (magic sysrq)\n\r", |
| 1288 | "% c switch between console and monitor\n\r", |
| 1289 | "% % sends %\n\r", |
| 1290 | NULL |
| 1291 | }; |
| 1292 | |
| 1293 | static int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
| 1294 | static void mux_print_help(CharDriverState *chr) |
| 1295 | { |
| 1296 | int i, j; |
| 1297 | char ebuf[15] = "Escape-Char"; |
| 1298 | char cbuf[50] = "\n\r"; |
| 1299 | |
| 1300 | if (term_escape_char > 0 && term_escape_char < 26) { |
| 1301 | sprintf(cbuf,"\n\r"); |
| 1302 | sprintf(ebuf,"C-%c", term_escape_char - 1 + 'a'); |
| 1303 | } else { |
| 1304 | sprintf(cbuf,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r", term_escape_char); |
| 1305 | } |
| 1306 | chr->chr_write(chr, cbuf, strlen(cbuf)); |
| 1307 | for (i = 0; mux_help[i] != NULL; i++) { |
| 1308 | for (j=0; mux_help[i][j] != '\0'; j++) { |
| 1309 | if (mux_help[i][j] == '%') |
| 1310 | chr->chr_write(chr, ebuf, strlen(ebuf)); |
| 1311 | else |
| 1312 | chr->chr_write(chr, &mux_help[i][j], 1); |
| 1313 | } |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
| 1318 | { |
| 1319 | if (d->term_got_escape) { |
| 1320 | d->term_got_escape = 0; |
| 1321 | if (ch == term_escape_char) |
| 1322 | goto send_char; |
| 1323 | switch(ch) { |
| 1324 | case '?': |
| 1325 | case 'h': |
| 1326 | mux_print_help(chr); |
| 1327 | break; |
| 1328 | case 'x': |
| 1329 | { |
| 1330 | char *term = "QEMU: Terminated\n\r"; |
| 1331 | chr->chr_write(chr,term,strlen(term)); |
| 1332 | exit(0); |
| 1333 | break; |
| 1334 | } |
| 1335 | case 's': |
| 1336 | { |
| 1337 | int i; |
| 1338 | for (i = 0; i < MAX_DISKS; i++) { |
| 1339 | if (bs_table[i]) |
| 1340 | bdrv_commit(bs_table[i]); |
| 1341 | } |
| 1342 | } |
| 1343 | break; |
| 1344 | case 'b': |
| 1345 | if (chr->chr_event) |
| 1346 | chr->chr_event(chr->opaque, CHR_EVENT_BREAK); |
| 1347 | break; |
| 1348 | case 'c': |
| 1349 | /* Switch to the next registered device */ |
| 1350 | chr->focus++; |
| 1351 | if (chr->focus >= d->mux_cnt) |
| 1352 | chr->focus = 0; |
| 1353 | break; |
| 1354 | case 't': |
| 1355 | term_timestamps = !term_timestamps; |
| 1356 | term_timestamps_start = -1; |
| 1357 | break; |
| 1358 | } |
| 1359 | } else if (ch == term_escape_char) { |
| 1360 | d->term_got_escape = 1; |
| 1361 | } else { |
| 1362 | send_char: |
| 1363 | return 1; |
| 1364 | } |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
| 1368 | static int mux_chr_can_read(void *opaque) |
| 1369 | { |
| 1370 | CharDriverState *chr = opaque; |
| 1371 | MuxDriver *d = chr->opaque; |
| 1372 | if (d->chr_can_read[chr->focus]) |
| 1373 | return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]); |
| 1374 | return 0; |
| 1375 | } |
| 1376 | |
| 1377 | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
| 1378 | { |
| 1379 | CharDriverState *chr = opaque; |
| 1380 | MuxDriver *d = chr->opaque; |
| 1381 | int i; |
| 1382 | for(i = 0; i < size; i++) |
| 1383 | if (mux_proc_byte(chr, d, buf[i])) |
| 1384 | d->chr_read[chr->focus](d->ext_opaque[chr->focus], &buf[i], 1); |
| 1385 | } |
| 1386 | |
| 1387 | static void mux_chr_event(void *opaque, int event) |
| 1388 | { |
| 1389 | CharDriverState *chr = opaque; |
| 1390 | MuxDriver *d = chr->opaque; |
| 1391 | int i; |
| 1392 | |
| 1393 | /* Send the event to all registered listeners */ |
| 1394 | for (i = 0; i < d->mux_cnt; i++) |
| 1395 | if (d->chr_event[i]) |
| 1396 | d->chr_event[i](d->ext_opaque[i], event); |
| 1397 | } |
| 1398 | |
| 1399 | static void mux_chr_update_read_handler(CharDriverState *chr) |
| 1400 | { |
| 1401 | MuxDriver *d = chr->opaque; |
| 1402 | |
| 1403 | if (d->mux_cnt >= MAX_MUX) { |
| 1404 | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n"); |
| 1405 | return; |
| 1406 | } |
| 1407 | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
| 1408 | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
| 1409 | d->chr_read[d->mux_cnt] = chr->chr_read; |
| 1410 | d->chr_event[d->mux_cnt] = chr->chr_event; |
| 1411 | /* Fix up the real driver with mux routines */ |
| 1412 | if (d->mux_cnt == 0) { |
| 1413 | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
| 1414 | mux_chr_event, chr); |
| 1415 | } |
| 1416 | chr->focus = d->mux_cnt; |
| 1417 | d->mux_cnt++; |
| 1418 | } |
| 1419 | |
| 1420 | CharDriverState *qemu_chr_open_mux(CharDriverState *drv) |
| 1421 | { |
| 1422 | CharDriverState *chr; |
| 1423 | MuxDriver *d; |
| 1424 | |
| 1425 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1426 | if (!chr) |
| 1427 | return NULL; |
| 1428 | d = qemu_mallocz(sizeof(MuxDriver)); |
| 1429 | if (!d) { |
| 1430 | free(chr); |
| 1431 | return NULL; |
| 1432 | } |
| 1433 | |
| 1434 | chr->opaque = d; |
| 1435 | d->drv = drv; |
| 1436 | chr->focus = -1; |
| 1437 | chr->chr_write = mux_chr_write; |
| 1438 | chr->chr_update_read_handler = mux_chr_update_read_handler; |
| 1439 | return chr; |
| 1440 | } |
| 1441 | |
| 1442 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1443 | #ifdef _WIN32 |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1444 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1445 | static void socket_cleanup(void) |
| 1446 | { |
| 1447 | WSACleanup(); |
| 1448 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1449 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1450 | static int socket_init(void) |
| 1451 | { |
| 1452 | WSADATA Data; |
| 1453 | int ret, err; |
| 1454 | |
| 1455 | ret = WSAStartup(MAKEWORD(2,2), &Data); |
| 1456 | if (ret != 0) { |
| 1457 | err = WSAGetLastError(); |
| 1458 | fprintf(stderr, "WSAStartup: %d\n", err); |
| 1459 | return -1; |
| 1460 | } |
| 1461 | atexit(socket_cleanup); |
| 1462 | return 0; |
| 1463 | } |
| 1464 | |
| 1465 | static int send_all(int fd, const uint8_t *buf, int len1) |
| 1466 | { |
| 1467 | int ret, len; |
| 1468 | |
| 1469 | len = len1; |
| 1470 | while (len > 0) { |
| 1471 | ret = send(fd, buf, len, 0); |
| 1472 | if (ret < 0) { |
| 1473 | int errno; |
| 1474 | errno = WSAGetLastError(); |
| 1475 | if (errno != WSAEWOULDBLOCK) { |
| 1476 | return -1; |
| 1477 | } |
| 1478 | } else if (ret == 0) { |
| 1479 | break; |
| 1480 | } else { |
| 1481 | buf += ret; |
| 1482 | len -= ret; |
| 1483 | } |
| 1484 | } |
| 1485 | return len1 - len; |
| 1486 | } |
| 1487 | |
| 1488 | void socket_set_nonblock(int fd) |
| 1489 | { |
| 1490 | unsigned long opt = 1; |
| 1491 | ioctlsocket(fd, FIONBIO, &opt); |
| 1492 | } |
| 1493 | |
| 1494 | #else |
| 1495 | |
bellard | 1d96905 | 2004-09-18 19:34:39 +0000 | [diff] [blame] | 1496 | static int unix_write(int fd, const uint8_t *buf, int len1) |
| 1497 | { |
| 1498 | int ret, len; |
| 1499 | |
| 1500 | len = len1; |
| 1501 | while (len > 0) { |
| 1502 | ret = write(fd, buf, len); |
| 1503 | if (ret < 0) { |
| 1504 | if (errno != EINTR && errno != EAGAIN) |
| 1505 | return -1; |
| 1506 | } else if (ret == 0) { |
| 1507 | break; |
| 1508 | } else { |
| 1509 | buf += ret; |
| 1510 | len -= ret; |
| 1511 | } |
| 1512 | } |
| 1513 | return len1 - len; |
| 1514 | } |
| 1515 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1516 | static inline int send_all(int fd, const uint8_t *buf, int len1) |
| 1517 | { |
| 1518 | return unix_write(fd, buf, len1); |
| 1519 | } |
| 1520 | |
| 1521 | void socket_set_nonblock(int fd) |
| 1522 | { |
| 1523 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 1524 | } |
| 1525 | #endif /* !_WIN32 */ |
| 1526 | |
| 1527 | #ifndef _WIN32 |
| 1528 | |
| 1529 | typedef struct { |
| 1530 | int fd_in, fd_out; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1531 | int max_size; |
| 1532 | } FDCharDriver; |
| 1533 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame^] | 1534 | #define STDIO_MAX_CLIENTS 1 |
| 1535 | static int stdio_nb_clients = 0; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1536 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1537 | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1538 | { |
| 1539 | FDCharDriver *s = chr->opaque; |
bellard | 1d96905 | 2004-09-18 19:34:39 +0000 | [diff] [blame] | 1540 | return unix_write(s->fd_out, buf, len); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1543 | static int fd_chr_read_poll(void *opaque) |
| 1544 | { |
| 1545 | CharDriverState *chr = opaque; |
| 1546 | FDCharDriver *s = chr->opaque; |
| 1547 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1548 | s->max_size = qemu_chr_can_read(chr); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1549 | return s->max_size; |
| 1550 | } |
| 1551 | |
| 1552 | static void fd_chr_read(void *opaque) |
| 1553 | { |
| 1554 | CharDriverState *chr = opaque; |
| 1555 | FDCharDriver *s = chr->opaque; |
| 1556 | int size, len; |
| 1557 | uint8_t buf[1024]; |
| 1558 | |
| 1559 | len = sizeof(buf); |
| 1560 | if (len > s->max_size) |
| 1561 | len = s->max_size; |
| 1562 | if (len == 0) |
| 1563 | return; |
| 1564 | size = read(s->fd_in, buf, len); |
pbrook | 188157f | 2006-11-01 01:44:16 +0000 | [diff] [blame] | 1565 | if (size == 0) { |
| 1566 | /* FD has been closed. Remove it from the active list. */ |
| 1567 | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
| 1568 | return; |
| 1569 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1570 | if (size > 0) { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1571 | qemu_chr_read(chr, buf, size); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1575 | static void fd_chr_update_read_handler(CharDriverState *chr) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1576 | { |
| 1577 | FDCharDriver *s = chr->opaque; |
| 1578 | |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1579 | if (s->fd_in >= 0) { |
| 1580 | if (nographic && s->fd_in == 0) { |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1581 | } else { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1582 | qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, |
| 1583 | fd_chr_read, NULL, chr); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1584 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1585 | } |
| 1586 | } |
| 1587 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1588 | /* open a character device to a unix fd */ |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1589 | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1590 | { |
| 1591 | CharDriverState *chr; |
| 1592 | FDCharDriver *s; |
| 1593 | |
| 1594 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1595 | if (!chr) |
| 1596 | return NULL; |
| 1597 | s = qemu_mallocz(sizeof(FDCharDriver)); |
| 1598 | if (!s) { |
| 1599 | free(chr); |
| 1600 | return NULL; |
| 1601 | } |
| 1602 | s->fd_in = fd_in; |
| 1603 | s->fd_out = fd_out; |
| 1604 | chr->opaque = s; |
| 1605 | chr->chr_write = fd_chr_write; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 1606 | chr->chr_update_read_handler = fd_chr_update_read_handler; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1607 | |
| 1608 | qemu_chr_reset(chr); |
| 1609 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1610 | return chr; |
| 1611 | } |
| 1612 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1613 | static CharDriverState *qemu_chr_open_file_out(const char *file_out) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1614 | { |
| 1615 | int fd_out; |
| 1616 | |
pbrook | 89ba1a7 | 2006-03-28 20:20:38 +0000 | [diff] [blame] | 1617 | 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] | 1618 | if (fd_out < 0) |
| 1619 | return NULL; |
| 1620 | return qemu_chr_open_fd(-1, fd_out); |
| 1621 | } |
| 1622 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1623 | static CharDriverState *qemu_chr_open_pipe(const char *filename) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1624 | { |
ths | c26c1c4 | 2006-12-22 19:25:31 +0000 | [diff] [blame] | 1625 | int fd_in, fd_out; |
| 1626 | char filename_in[256], filename_out[256]; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1627 | |
ths | c26c1c4 | 2006-12-22 19:25:31 +0000 | [diff] [blame] | 1628 | snprintf(filename_in, 256, "%s.in", filename); |
| 1629 | snprintf(filename_out, 256, "%s.out", filename); |
| 1630 | fd_in = open(filename_in, O_RDWR | O_BINARY); |
| 1631 | fd_out = open(filename_out, O_RDWR | O_BINARY); |
| 1632 | if (fd_in < 0 || fd_out < 0) { |
| 1633 | if (fd_in >= 0) |
| 1634 | close(fd_in); |
| 1635 | if (fd_out >= 0) |
| 1636 | close(fd_out); |
| 1637 | fd_in = fd_out = open(filename, O_RDWR | O_BINARY); |
| 1638 | if (fd_in < 0) |
| 1639 | return NULL; |
| 1640 | } |
| 1641 | return qemu_chr_open_fd(fd_in, fd_out); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1645 | /* for STDIO, we handle the case where several clients use it |
| 1646 | (nographic mode) */ |
| 1647 | |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1648 | #define TERM_FIFO_MAX_SIZE 1 |
| 1649 | |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1650 | static uint8_t term_fifo[TERM_FIFO_MAX_SIZE]; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1651 | static int term_fifo_size; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1652 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1653 | static int stdio_read_poll(void *opaque) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1654 | { |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame^] | 1655 | CharDriverState *chr = opaque; |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1656 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame^] | 1657 | /* try to flush the queue if needed */ |
| 1658 | if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) { |
| 1659 | qemu_chr_read(chr, term_fifo, 1); |
| 1660 | term_fifo_size = 0; |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1661 | } |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame^] | 1662 | /* see if we can absorb more chars */ |
| 1663 | if (term_fifo_size == 0) |
| 1664 | return 1; |
| 1665 | else |
| 1666 | return 0; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1669 | static void stdio_read(void *opaque) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1670 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1671 | int size; |
| 1672 | uint8_t buf[1]; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame^] | 1673 | CharDriverState *chr = opaque; |
| 1674 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1675 | size = read(0, buf, 1); |
pbrook | 519945d | 2006-09-10 14:39:54 +0000 | [diff] [blame] | 1676 | if (size == 0) { |
| 1677 | /* stdin has been closed. Remove it from the active list. */ |
| 1678 | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
| 1679 | return; |
| 1680 | } |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame^] | 1681 | if (size > 0) { |
| 1682 | if (qemu_chr_can_read(chr) > 0) { |
| 1683 | qemu_chr_read(chr, buf, 1); |
| 1684 | } else if (term_fifo_size == 0) { |
| 1685 | term_fifo[term_fifo_size++] = buf[0]; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1686 | } |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1687 | } |
| 1688 | } |
| 1689 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 1690 | /* init terminal so that we can grab keys */ |
| 1691 | static struct termios oldtty; |
| 1692 | static int old_fd0_flags; |
| 1693 | |
| 1694 | static void term_exit(void) |
| 1695 | { |
| 1696 | tcsetattr (0, TCSANOW, &oldtty); |
| 1697 | fcntl(0, F_SETFL, old_fd0_flags); |
| 1698 | } |
| 1699 | |
| 1700 | static void term_init(void) |
| 1701 | { |
| 1702 | struct termios tty; |
| 1703 | |
| 1704 | tcgetattr (0, &tty); |
| 1705 | oldtty = tty; |
| 1706 | old_fd0_flags = fcntl(0, F_GETFL); |
| 1707 | |
| 1708 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 1709 | |INLCR|IGNCR|ICRNL|IXON); |
| 1710 | tty.c_oflag |= OPOST; |
| 1711 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
| 1712 | /* if graphical mode, we allow Ctrl-C handling */ |
| 1713 | if (nographic) |
| 1714 | tty.c_lflag &= ~ISIG; |
| 1715 | tty.c_cflag &= ~(CSIZE|PARENB); |
| 1716 | tty.c_cflag |= CS8; |
| 1717 | tty.c_cc[VMIN] = 1; |
| 1718 | tty.c_cc[VTIME] = 0; |
| 1719 | |
| 1720 | tcsetattr (0, TCSANOW, &tty); |
| 1721 | |
| 1722 | atexit(term_exit); |
| 1723 | |
| 1724 | fcntl(0, F_SETFL, O_NONBLOCK); |
| 1725 | } |
| 1726 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1727 | static CharDriverState *qemu_chr_open_stdio(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1728 | { |
| 1729 | CharDriverState *chr; |
| 1730 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame^] | 1731 | if (stdio_nb_clients >= STDIO_MAX_CLIENTS) |
| 1732 | return NULL; |
| 1733 | chr = qemu_chr_open_fd(0, 1); |
| 1734 | qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr); |
| 1735 | stdio_nb_clients++; |
| 1736 | term_init(); |
| 1737 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1738 | return chr; |
| 1739 | } |
| 1740 | |
| 1741 | #if defined(__linux__) |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1742 | static CharDriverState *qemu_chr_open_pty(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1743 | { |
bellard | 91fc211 | 2005-12-18 19:09:37 +0000 | [diff] [blame] | 1744 | struct termios tty; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1745 | char slave_name[1024]; |
| 1746 | int master_fd, slave_fd; |
| 1747 | |
| 1748 | /* Not satisfying */ |
| 1749 | if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) { |
| 1750 | return NULL; |
| 1751 | } |
bellard | 91fc211 | 2005-12-18 19:09:37 +0000 | [diff] [blame] | 1752 | |
| 1753 | /* Disabling local echo and line-buffered output */ |
| 1754 | tcgetattr (master_fd, &tty); |
| 1755 | tty.c_lflag &= ~(ECHO|ICANON|ISIG); |
| 1756 | tty.c_cc[VMIN] = 1; |
| 1757 | tty.c_cc[VTIME] = 0; |
| 1758 | tcsetattr (master_fd, TCSAFLUSH, &tty); |
| 1759 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1760 | fprintf(stderr, "char device redirected to %s\n", slave_name); |
| 1761 | return qemu_chr_open_fd(master_fd, master_fd); |
| 1762 | } |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1763 | |
| 1764 | static void tty_serial_init(int fd, int speed, |
| 1765 | int parity, int data_bits, int stop_bits) |
| 1766 | { |
| 1767 | struct termios tty; |
| 1768 | speed_t spd; |
| 1769 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1770 | #if 0 |
| 1771 | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n", |
| 1772 | speed, parity, data_bits, stop_bits); |
| 1773 | #endif |
| 1774 | tcgetattr (fd, &tty); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1775 | |
| 1776 | switch(speed) { |
| 1777 | case 50: |
| 1778 | spd = B50; |
| 1779 | break; |
| 1780 | case 75: |
| 1781 | spd = B75; |
| 1782 | break; |
| 1783 | case 300: |
| 1784 | spd = B300; |
| 1785 | break; |
| 1786 | case 600: |
| 1787 | spd = B600; |
| 1788 | break; |
| 1789 | case 1200: |
| 1790 | spd = B1200; |
| 1791 | break; |
| 1792 | case 2400: |
| 1793 | spd = B2400; |
| 1794 | break; |
| 1795 | case 4800: |
| 1796 | spd = B4800; |
| 1797 | break; |
| 1798 | case 9600: |
| 1799 | spd = B9600; |
| 1800 | break; |
| 1801 | case 19200: |
| 1802 | spd = B19200; |
| 1803 | break; |
| 1804 | case 38400: |
| 1805 | spd = B38400; |
| 1806 | break; |
| 1807 | case 57600: |
| 1808 | spd = B57600; |
| 1809 | break; |
| 1810 | default: |
| 1811 | case 115200: |
| 1812 | spd = B115200; |
| 1813 | break; |
| 1814 | } |
| 1815 | |
| 1816 | cfsetispeed(&tty, spd); |
| 1817 | cfsetospeed(&tty, spd); |
| 1818 | |
| 1819 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 1820 | |INLCR|IGNCR|ICRNL|IXON); |
| 1821 | tty.c_oflag |= OPOST; |
| 1822 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
bellard | 094eed6 | 2006-09-09 11:10:18 +0000 | [diff] [blame] | 1823 | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1824 | switch(data_bits) { |
| 1825 | default: |
| 1826 | case 8: |
| 1827 | tty.c_cflag |= CS8; |
| 1828 | break; |
| 1829 | case 7: |
| 1830 | tty.c_cflag |= CS7; |
| 1831 | break; |
| 1832 | case 6: |
| 1833 | tty.c_cflag |= CS6; |
| 1834 | break; |
| 1835 | case 5: |
| 1836 | tty.c_cflag |= CS5; |
| 1837 | break; |
| 1838 | } |
| 1839 | switch(parity) { |
| 1840 | default: |
| 1841 | case 'N': |
| 1842 | break; |
| 1843 | case 'E': |
| 1844 | tty.c_cflag |= PARENB; |
| 1845 | break; |
| 1846 | case 'O': |
| 1847 | tty.c_cflag |= PARENB | PARODD; |
| 1848 | break; |
| 1849 | } |
bellard | 094eed6 | 2006-09-09 11:10:18 +0000 | [diff] [blame] | 1850 | if (stop_bits == 2) |
| 1851 | tty.c_cflag |= CSTOPB; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1852 | |
| 1853 | tcsetattr (fd, TCSANOW, &tty); |
| 1854 | } |
| 1855 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1856 | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1857 | { |
| 1858 | FDCharDriver *s = chr->opaque; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1859 | |
| 1860 | switch(cmd) { |
| 1861 | case CHR_IOCTL_SERIAL_SET_PARAMS: |
| 1862 | { |
| 1863 | QEMUSerialSetParams *ssp = arg; |
| 1864 | tty_serial_init(s->fd_in, ssp->speed, ssp->parity, |
| 1865 | ssp->data_bits, ssp->stop_bits); |
| 1866 | } |
| 1867 | break; |
| 1868 | case CHR_IOCTL_SERIAL_SET_BREAK: |
| 1869 | { |
| 1870 | int enable = *(int *)arg; |
| 1871 | if (enable) |
| 1872 | tcsendbreak(s->fd_in, 1); |
| 1873 | } |
| 1874 | break; |
| 1875 | default: |
| 1876 | return -ENOTSUP; |
| 1877 | } |
| 1878 | return 0; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1881 | static CharDriverState *qemu_chr_open_tty(const char *filename) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1882 | { |
| 1883 | CharDriverState *chr; |
| 1884 | int fd; |
| 1885 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1886 | fd = open(filename, O_RDWR | O_NONBLOCK); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1887 | if (fd < 0) |
| 1888 | return NULL; |
| 1889 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 1890 | tty_serial_init(fd, 115200, 'N', 8, 1); |
| 1891 | chr = qemu_chr_open_fd(fd, fd); |
| 1892 | if (!chr) |
| 1893 | return NULL; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1894 | chr->chr_ioctl = tty_serial_ioctl; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1895 | qemu_chr_reset(chr); |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1896 | return chr; |
| 1897 | } |
| 1898 | |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 1899 | typedef struct { |
| 1900 | int fd; |
| 1901 | int mode; |
| 1902 | } ParallelCharDriver; |
| 1903 | |
| 1904 | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
| 1905 | { |
| 1906 | if (s->mode != mode) { |
| 1907 | int m = mode; |
| 1908 | if (ioctl(s->fd, PPSETMODE, &m) < 0) |
| 1909 | return 0; |
| 1910 | s->mode = mode; |
| 1911 | } |
| 1912 | return 1; |
| 1913 | } |
| 1914 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1915 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
| 1916 | { |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 1917 | ParallelCharDriver *drv = chr->opaque; |
| 1918 | int fd = drv->fd; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1919 | uint8_t b; |
| 1920 | |
| 1921 | switch(cmd) { |
| 1922 | case CHR_IOCTL_PP_READ_DATA: |
| 1923 | if (ioctl(fd, PPRDATA, &b) < 0) |
| 1924 | return -ENOTSUP; |
| 1925 | *(uint8_t *)arg = b; |
| 1926 | break; |
| 1927 | case CHR_IOCTL_PP_WRITE_DATA: |
| 1928 | b = *(uint8_t *)arg; |
| 1929 | if (ioctl(fd, PPWDATA, &b) < 0) |
| 1930 | return -ENOTSUP; |
| 1931 | break; |
| 1932 | case CHR_IOCTL_PP_READ_CONTROL: |
| 1933 | if (ioctl(fd, PPRCONTROL, &b) < 0) |
| 1934 | return -ENOTSUP; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 1935 | /* Linux gives only the lowest bits, and no way to know data |
| 1936 | direction! For better compatibility set the fixed upper |
| 1937 | bits. */ |
| 1938 | *(uint8_t *)arg = b | 0xc0; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1939 | break; |
| 1940 | case CHR_IOCTL_PP_WRITE_CONTROL: |
| 1941 | b = *(uint8_t *)arg; |
| 1942 | if (ioctl(fd, PPWCONTROL, &b) < 0) |
| 1943 | return -ENOTSUP; |
| 1944 | break; |
| 1945 | case CHR_IOCTL_PP_READ_STATUS: |
| 1946 | if (ioctl(fd, PPRSTATUS, &b) < 0) |
| 1947 | return -ENOTSUP; |
| 1948 | *(uint8_t *)arg = b; |
| 1949 | break; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 1950 | case CHR_IOCTL_PP_EPP_READ_ADDR: |
| 1951 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { |
| 1952 | struct ParallelIOArg *parg = arg; |
| 1953 | int n = read(fd, parg->buffer, parg->count); |
| 1954 | if (n != parg->count) { |
| 1955 | return -EIO; |
| 1956 | } |
| 1957 | } |
| 1958 | break; |
| 1959 | case CHR_IOCTL_PP_EPP_READ: |
| 1960 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { |
| 1961 | struct ParallelIOArg *parg = arg; |
| 1962 | int n = read(fd, parg->buffer, parg->count); |
| 1963 | if (n != parg->count) { |
| 1964 | return -EIO; |
| 1965 | } |
| 1966 | } |
| 1967 | break; |
| 1968 | case CHR_IOCTL_PP_EPP_WRITE_ADDR: |
| 1969 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) { |
| 1970 | struct ParallelIOArg *parg = arg; |
| 1971 | int n = write(fd, parg->buffer, parg->count); |
| 1972 | if (n != parg->count) { |
| 1973 | return -EIO; |
| 1974 | } |
| 1975 | } |
| 1976 | break; |
| 1977 | case CHR_IOCTL_PP_EPP_WRITE: |
| 1978 | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) { |
| 1979 | struct ParallelIOArg *parg = arg; |
| 1980 | int n = write(fd, parg->buffer, parg->count); |
| 1981 | if (n != parg->count) { |
| 1982 | return -EIO; |
| 1983 | } |
| 1984 | } |
| 1985 | break; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1986 | default: |
| 1987 | return -ENOTSUP; |
| 1988 | } |
| 1989 | return 0; |
| 1990 | } |
| 1991 | |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 1992 | static void pp_close(CharDriverState *chr) |
| 1993 | { |
| 1994 | ParallelCharDriver *drv = chr->opaque; |
| 1995 | int fd = drv->fd; |
| 1996 | |
| 1997 | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
| 1998 | ioctl(fd, PPRELEASE); |
| 1999 | close(fd); |
| 2000 | qemu_free(drv); |
| 2001 | } |
| 2002 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2003 | static CharDriverState *qemu_chr_open_pp(const char *filename) |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2004 | { |
| 2005 | CharDriverState *chr; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2006 | ParallelCharDriver *drv; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2007 | int fd; |
| 2008 | |
| 2009 | fd = open(filename, O_RDWR); |
| 2010 | if (fd < 0) |
| 2011 | return NULL; |
| 2012 | |
| 2013 | if (ioctl(fd, PPCLAIM) < 0) { |
| 2014 | close(fd); |
| 2015 | return NULL; |
| 2016 | } |
| 2017 | |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2018 | drv = qemu_mallocz(sizeof(ParallelCharDriver)); |
| 2019 | if (!drv) { |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2020 | close(fd); |
| 2021 | return NULL; |
| 2022 | } |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2023 | drv->fd = fd; |
| 2024 | drv->mode = IEEE1284_MODE_COMPAT; |
| 2025 | |
| 2026 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2027 | if (!chr) { |
| 2028 | qemu_free(drv); |
| 2029 | close(fd); |
| 2030 | return NULL; |
| 2031 | } |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2032 | chr->chr_write = null_chr_write; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2033 | chr->chr_ioctl = pp_ioctl; |
ths | 5867c88 | 2007-02-17 23:44:43 +0000 | [diff] [blame] | 2034 | chr->chr_close = pp_close; |
| 2035 | chr->opaque = drv; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2036 | |
| 2037 | qemu_chr_reset(chr); |
| 2038 | |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2039 | return chr; |
| 2040 | } |
| 2041 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2042 | #else |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2043 | static CharDriverState *qemu_chr_open_pty(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2044 | { |
| 2045 | return NULL; |
| 2046 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 2047 | #endif |
| 2048 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2049 | #endif /* !defined(_WIN32) */ |
| 2050 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2051 | #ifdef _WIN32 |
| 2052 | typedef struct { |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2053 | int max_size; |
| 2054 | HANDLE hcom, hrecv, hsend; |
| 2055 | OVERLAPPED orecv, osend; |
| 2056 | BOOL fpipe; |
| 2057 | DWORD len; |
| 2058 | } WinCharState; |
| 2059 | |
| 2060 | #define NSENDBUF 2048 |
| 2061 | #define NRECVBUF 2048 |
| 2062 | #define MAXCONNECT 1 |
| 2063 | #define NTIMEOUT 5000 |
| 2064 | |
| 2065 | static int win_chr_poll(void *opaque); |
| 2066 | static int win_chr_pipe_poll(void *opaque); |
| 2067 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2068 | static void win_chr_close(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2069 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2070 | WinCharState *s = chr->opaque; |
| 2071 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2072 | if (s->hsend) { |
| 2073 | CloseHandle(s->hsend); |
| 2074 | s->hsend = NULL; |
| 2075 | } |
| 2076 | if (s->hrecv) { |
| 2077 | CloseHandle(s->hrecv); |
| 2078 | s->hrecv = NULL; |
| 2079 | } |
| 2080 | if (s->hcom) { |
| 2081 | CloseHandle(s->hcom); |
| 2082 | s->hcom = NULL; |
| 2083 | } |
| 2084 | if (s->fpipe) |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2085 | qemu_del_polling_cb(win_chr_pipe_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2086 | else |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2087 | qemu_del_polling_cb(win_chr_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2090 | static int win_chr_init(CharDriverState *chr, const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2091 | { |
| 2092 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2093 | COMMCONFIG comcfg; |
| 2094 | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
| 2095 | COMSTAT comstat; |
| 2096 | DWORD size; |
| 2097 | DWORD err; |
| 2098 | |
| 2099 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2100 | if (!s->hsend) { |
| 2101 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2102 | goto fail; |
| 2103 | } |
| 2104 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2105 | if (!s->hrecv) { |
| 2106 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2107 | goto fail; |
| 2108 | } |
| 2109 | |
| 2110 | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
| 2111 | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); |
| 2112 | if (s->hcom == INVALID_HANDLE_VALUE) { |
| 2113 | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError()); |
| 2114 | s->hcom = NULL; |
| 2115 | goto fail; |
| 2116 | } |
| 2117 | |
| 2118 | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) { |
| 2119 | fprintf(stderr, "Failed SetupComm\n"); |
| 2120 | goto fail; |
| 2121 | } |
| 2122 | |
| 2123 | ZeroMemory(&comcfg, sizeof(COMMCONFIG)); |
| 2124 | size = sizeof(COMMCONFIG); |
| 2125 | GetDefaultCommConfig(filename, &comcfg, &size); |
| 2126 | comcfg.dcb.DCBlength = sizeof(DCB); |
| 2127 | CommConfigDialog(filename, NULL, &comcfg); |
| 2128 | |
| 2129 | if (!SetCommState(s->hcom, &comcfg.dcb)) { |
| 2130 | fprintf(stderr, "Failed SetCommState\n"); |
| 2131 | goto fail; |
| 2132 | } |
| 2133 | |
| 2134 | if (!SetCommMask(s->hcom, EV_ERR)) { |
| 2135 | fprintf(stderr, "Failed SetCommMask\n"); |
| 2136 | goto fail; |
| 2137 | } |
| 2138 | |
| 2139 | cto.ReadIntervalTimeout = MAXDWORD; |
| 2140 | if (!SetCommTimeouts(s->hcom, &cto)) { |
| 2141 | fprintf(stderr, "Failed SetCommTimeouts\n"); |
| 2142 | goto fail; |
| 2143 | } |
| 2144 | |
| 2145 | if (!ClearCommError(s->hcom, &err, &comstat)) { |
| 2146 | fprintf(stderr, "Failed ClearCommError\n"); |
| 2147 | goto fail; |
| 2148 | } |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2149 | qemu_add_polling_cb(win_chr_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2150 | return 0; |
| 2151 | |
| 2152 | fail: |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2153 | win_chr_close(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2154 | return -1; |
| 2155 | } |
| 2156 | |
| 2157 | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
| 2158 | { |
| 2159 | WinCharState *s = chr->opaque; |
| 2160 | DWORD len, ret, size, err; |
| 2161 | |
| 2162 | len = len1; |
| 2163 | ZeroMemory(&s->osend, sizeof(s->osend)); |
| 2164 | s->osend.hEvent = s->hsend; |
| 2165 | while (len > 0) { |
| 2166 | if (s->hsend) |
| 2167 | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
| 2168 | else |
| 2169 | ret = WriteFile(s->hcom, buf, len, &size, NULL); |
| 2170 | if (!ret) { |
| 2171 | err = GetLastError(); |
| 2172 | if (err == ERROR_IO_PENDING) { |
| 2173 | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
| 2174 | if (ret) { |
| 2175 | buf += size; |
| 2176 | len -= size; |
| 2177 | } else { |
| 2178 | break; |
| 2179 | } |
| 2180 | } else { |
| 2181 | break; |
| 2182 | } |
| 2183 | } else { |
| 2184 | buf += size; |
| 2185 | len -= size; |
| 2186 | } |
| 2187 | } |
| 2188 | return len1 - len; |
| 2189 | } |
| 2190 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2191 | static int win_chr_read_poll(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2192 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2193 | WinCharState *s = chr->opaque; |
| 2194 | |
| 2195 | s->max_size = qemu_chr_can_read(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2196 | return s->max_size; |
| 2197 | } |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2198 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2199 | static void win_chr_readfile(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2200 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2201 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2202 | int ret, err; |
| 2203 | uint8_t buf[1024]; |
| 2204 | DWORD size; |
| 2205 | |
| 2206 | ZeroMemory(&s->orecv, sizeof(s->orecv)); |
| 2207 | s->orecv.hEvent = s->hrecv; |
| 2208 | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
| 2209 | if (!ret) { |
| 2210 | err = GetLastError(); |
| 2211 | if (err == ERROR_IO_PENDING) { |
| 2212 | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
| 2213 | } |
| 2214 | } |
| 2215 | |
| 2216 | if (size > 0) { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2217 | qemu_chr_read(chr, buf, size); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2221 | static void win_chr_read(CharDriverState *chr) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2222 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2223 | WinCharState *s = chr->opaque; |
| 2224 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2225 | if (s->len > s->max_size) |
| 2226 | s->len = s->max_size; |
| 2227 | if (s->len == 0) |
| 2228 | return; |
| 2229 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2230 | win_chr_readfile(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | static int win_chr_poll(void *opaque) |
| 2234 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2235 | CharDriverState *chr = opaque; |
| 2236 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2237 | COMSTAT status; |
| 2238 | DWORD comerr; |
| 2239 | |
| 2240 | ClearCommError(s->hcom, &comerr, &status); |
| 2241 | if (status.cbInQue > 0) { |
| 2242 | s->len = status.cbInQue; |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2243 | win_chr_read_poll(chr); |
| 2244 | win_chr_read(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2245 | return 1; |
| 2246 | } |
| 2247 | return 0; |
| 2248 | } |
| 2249 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2250 | static CharDriverState *qemu_chr_open_win(const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2251 | { |
| 2252 | CharDriverState *chr; |
| 2253 | WinCharState *s; |
| 2254 | |
| 2255 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2256 | if (!chr) |
| 2257 | return NULL; |
| 2258 | s = qemu_mallocz(sizeof(WinCharState)); |
| 2259 | if (!s) { |
| 2260 | free(chr); |
| 2261 | return NULL; |
| 2262 | } |
| 2263 | chr->opaque = s; |
| 2264 | chr->chr_write = win_chr_write; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2265 | chr->chr_close = win_chr_close; |
| 2266 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2267 | if (win_chr_init(chr, filename) < 0) { |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2268 | free(s); |
| 2269 | free(chr); |
| 2270 | return NULL; |
| 2271 | } |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2272 | qemu_chr_reset(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2273 | return chr; |
| 2274 | } |
| 2275 | |
| 2276 | static int win_chr_pipe_poll(void *opaque) |
| 2277 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2278 | CharDriverState *chr = opaque; |
| 2279 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2280 | DWORD size; |
| 2281 | |
| 2282 | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
| 2283 | if (size > 0) { |
| 2284 | s->len = size; |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2285 | win_chr_read_poll(chr); |
| 2286 | win_chr_read(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2287 | return 1; |
| 2288 | } |
| 2289 | return 0; |
| 2290 | } |
| 2291 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2292 | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2293 | { |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2294 | WinCharState *s = chr->opaque; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2295 | OVERLAPPED ov; |
| 2296 | int ret; |
| 2297 | DWORD size; |
| 2298 | char openname[256]; |
| 2299 | |
| 2300 | s->fpipe = TRUE; |
| 2301 | |
| 2302 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2303 | if (!s->hsend) { |
| 2304 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2305 | goto fail; |
| 2306 | } |
| 2307 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2308 | if (!s->hrecv) { |
| 2309 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2310 | goto fail; |
| 2311 | } |
| 2312 | |
| 2313 | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
| 2314 | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
| 2315 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
| 2316 | PIPE_WAIT, |
| 2317 | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); |
| 2318 | if (s->hcom == INVALID_HANDLE_VALUE) { |
| 2319 | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError()); |
| 2320 | s->hcom = NULL; |
| 2321 | goto fail; |
| 2322 | } |
| 2323 | |
| 2324 | ZeroMemory(&ov, sizeof(ov)); |
| 2325 | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2326 | ret = ConnectNamedPipe(s->hcom, &ov); |
| 2327 | if (ret) { |
| 2328 | fprintf(stderr, "Failed ConnectNamedPipe\n"); |
| 2329 | goto fail; |
| 2330 | } |
| 2331 | |
| 2332 | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
| 2333 | if (!ret) { |
| 2334 | fprintf(stderr, "Failed GetOverlappedResult\n"); |
| 2335 | if (ov.hEvent) { |
| 2336 | CloseHandle(ov.hEvent); |
| 2337 | ov.hEvent = NULL; |
| 2338 | } |
| 2339 | goto fail; |
| 2340 | } |
| 2341 | |
| 2342 | if (ov.hEvent) { |
| 2343 | CloseHandle(ov.hEvent); |
| 2344 | ov.hEvent = NULL; |
| 2345 | } |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2346 | qemu_add_polling_cb(win_chr_pipe_poll, chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2347 | return 0; |
| 2348 | |
| 2349 | fail: |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2350 | win_chr_close(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2351 | return -1; |
| 2352 | } |
| 2353 | |
| 2354 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2355 | static CharDriverState *qemu_chr_open_win_pipe(const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2356 | { |
| 2357 | CharDriverState *chr; |
| 2358 | WinCharState *s; |
| 2359 | |
| 2360 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2361 | if (!chr) |
| 2362 | return NULL; |
| 2363 | s = qemu_mallocz(sizeof(WinCharState)); |
| 2364 | if (!s) { |
| 2365 | free(chr); |
| 2366 | return NULL; |
| 2367 | } |
| 2368 | chr->opaque = s; |
| 2369 | chr->chr_write = win_chr_write; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2370 | chr->chr_close = win_chr_close; |
| 2371 | |
ths | 087f4ae | 2007-02-10 21:50:42 +0000 | [diff] [blame] | 2372 | if (win_chr_pipe_init(chr, filename) < 0) { |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2373 | free(s); |
| 2374 | free(chr); |
| 2375 | return NULL; |
| 2376 | } |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2377 | qemu_chr_reset(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2378 | return chr; |
| 2379 | } |
| 2380 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2381 | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2382 | { |
| 2383 | CharDriverState *chr; |
| 2384 | WinCharState *s; |
| 2385 | |
| 2386 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2387 | if (!chr) |
| 2388 | return NULL; |
| 2389 | s = qemu_mallocz(sizeof(WinCharState)); |
| 2390 | if (!s) { |
| 2391 | free(chr); |
| 2392 | return NULL; |
| 2393 | } |
| 2394 | s->hcom = fd_out; |
| 2395 | chr->opaque = s; |
| 2396 | chr->chr_write = win_chr_write; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2397 | qemu_chr_reset(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2398 | return chr; |
| 2399 | } |
| 2400 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2401 | static CharDriverState *qemu_chr_open_win_file_out(const char *file_out) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2402 | { |
| 2403 | HANDLE fd_out; |
| 2404 | |
| 2405 | fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL, |
| 2406 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 2407 | if (fd_out == INVALID_HANDLE_VALUE) |
| 2408 | return NULL; |
| 2409 | |
| 2410 | return qemu_chr_open_win_file(fd_out); |
| 2411 | } |
| 2412 | #endif |
| 2413 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2414 | /***********************************************************/ |
| 2415 | /* UDP Net console */ |
| 2416 | |
| 2417 | typedef struct { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2418 | int fd; |
| 2419 | struct sockaddr_in daddr; |
| 2420 | char buf[1024]; |
| 2421 | int bufcnt; |
| 2422 | int bufptr; |
| 2423 | int max_size; |
| 2424 | } NetCharDriver; |
| 2425 | |
| 2426 | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2427 | { |
| 2428 | NetCharDriver *s = chr->opaque; |
| 2429 | |
| 2430 | return sendto(s->fd, buf, len, 0, |
| 2431 | (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in)); |
| 2432 | } |
| 2433 | |
| 2434 | static int udp_chr_read_poll(void *opaque) |
| 2435 | { |
| 2436 | CharDriverState *chr = opaque; |
| 2437 | NetCharDriver *s = chr->opaque; |
| 2438 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2439 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2440 | |
| 2441 | /* If there were any stray characters in the queue process them |
| 2442 | * first |
| 2443 | */ |
| 2444 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2445 | qemu_chr_read(chr, &s->buf[s->bufptr], 1); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2446 | s->bufptr++; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2447 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2448 | } |
| 2449 | return s->max_size; |
| 2450 | } |
| 2451 | |
| 2452 | static void udp_chr_read(void *opaque) |
| 2453 | { |
| 2454 | CharDriverState *chr = opaque; |
| 2455 | NetCharDriver *s = chr->opaque; |
| 2456 | |
| 2457 | if (s->max_size == 0) |
| 2458 | return; |
| 2459 | s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0); |
| 2460 | s->bufptr = s->bufcnt; |
| 2461 | if (s->bufcnt <= 0) |
| 2462 | return; |
| 2463 | |
| 2464 | s->bufptr = 0; |
| 2465 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2466 | qemu_chr_read(chr, &s->buf[s->bufptr], 1); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2467 | s->bufptr++; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2468 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2469 | } |
| 2470 | } |
| 2471 | |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2472 | static void udp_chr_update_read_handler(CharDriverState *chr) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2473 | { |
| 2474 | NetCharDriver *s = chr->opaque; |
| 2475 | |
| 2476 | if (s->fd >= 0) { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2477 | qemu_set_fd_handler2(s->fd, udp_chr_read_poll, |
| 2478 | udp_chr_read, NULL, chr); |
| 2479 | } |
| 2480 | } |
| 2481 | |
| 2482 | int parse_host_port(struct sockaddr_in *saddr, const char *str); |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2483 | #ifndef _WIN32 |
| 2484 | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str); |
| 2485 | #endif |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2486 | int parse_host_src_port(struct sockaddr_in *haddr, |
| 2487 | struct sockaddr_in *saddr, |
| 2488 | const char *str); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2489 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2490 | static CharDriverState *qemu_chr_open_udp(const char *def) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2491 | { |
| 2492 | CharDriverState *chr = NULL; |
| 2493 | NetCharDriver *s = NULL; |
| 2494 | int fd = -1; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2495 | struct sockaddr_in saddr; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2496 | |
| 2497 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2498 | if (!chr) |
| 2499 | goto return_err; |
| 2500 | s = qemu_mallocz(sizeof(NetCharDriver)); |
| 2501 | if (!s) |
| 2502 | goto return_err; |
| 2503 | |
| 2504 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 2505 | if (fd < 0) { |
| 2506 | perror("socket(PF_INET, SOCK_DGRAM)"); |
| 2507 | goto return_err; |
| 2508 | } |
| 2509 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2510 | if (parse_host_src_port(&s->daddr, &saddr, def) < 0) { |
| 2511 | printf("Could not parse: %s\n", def); |
| 2512 | goto return_err; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2515 | if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2516 | { |
| 2517 | perror("bind"); |
| 2518 | goto return_err; |
| 2519 | } |
| 2520 | |
| 2521 | s->fd = fd; |
| 2522 | s->bufcnt = 0; |
| 2523 | s->bufptr = 0; |
| 2524 | chr->opaque = s; |
| 2525 | chr->chr_write = udp_chr_write; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2526 | chr->chr_update_read_handler = udp_chr_update_read_handler; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2527 | return chr; |
| 2528 | |
| 2529 | return_err: |
| 2530 | if (chr) |
| 2531 | free(chr); |
| 2532 | if (s) |
| 2533 | free(s); |
| 2534 | if (fd >= 0) |
| 2535 | closesocket(fd); |
| 2536 | return NULL; |
| 2537 | } |
| 2538 | |
| 2539 | /***********************************************************/ |
| 2540 | /* TCP Net console */ |
| 2541 | |
| 2542 | typedef struct { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2543 | int fd, listen_fd; |
| 2544 | int connected; |
| 2545 | int max_size; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2546 | int do_telnetopt; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2547 | int do_nodelay; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2548 | int is_unix; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2549 | } TCPCharDriver; |
| 2550 | |
| 2551 | static void tcp_chr_accept(void *opaque); |
| 2552 | |
| 2553 | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2554 | { |
| 2555 | TCPCharDriver *s = chr->opaque; |
| 2556 | if (s->connected) { |
| 2557 | return send_all(s->fd, buf, len); |
| 2558 | } else { |
| 2559 | /* XXX: indicate an error ? */ |
| 2560 | return len; |
| 2561 | } |
| 2562 | } |
| 2563 | |
| 2564 | static int tcp_chr_read_poll(void *opaque) |
| 2565 | { |
| 2566 | CharDriverState *chr = opaque; |
| 2567 | TCPCharDriver *s = chr->opaque; |
| 2568 | if (!s->connected) |
| 2569 | return 0; |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2570 | s->max_size = qemu_chr_can_read(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2571 | return s->max_size; |
| 2572 | } |
| 2573 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2574 | #define IAC 255 |
| 2575 | #define IAC_BREAK 243 |
| 2576 | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
| 2577 | TCPCharDriver *s, |
| 2578 | char *buf, int *size) |
| 2579 | { |
| 2580 | /* Handle any telnet client's basic IAC options to satisfy char by |
| 2581 | * char mode with no echo. All IAC options will be removed from |
| 2582 | * the buf and the do_telnetopt variable will be used to track the |
| 2583 | * state of the width of the IAC information. |
| 2584 | * |
| 2585 | * IAC commands come in sets of 3 bytes with the exception of the |
| 2586 | * "IAC BREAK" command and the double IAC. |
| 2587 | */ |
| 2588 | |
| 2589 | int i; |
| 2590 | int j = 0; |
| 2591 | |
| 2592 | for (i = 0; i < *size; i++) { |
| 2593 | if (s->do_telnetopt > 1) { |
| 2594 | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
| 2595 | /* Double IAC means send an IAC */ |
| 2596 | if (j != i) |
| 2597 | buf[j] = buf[i]; |
| 2598 | j++; |
| 2599 | s->do_telnetopt = 1; |
| 2600 | } else { |
| 2601 | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
| 2602 | /* Handle IAC break commands by sending a serial break */ |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2603 | qemu_chr_event(chr, CHR_EVENT_BREAK); |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2604 | s->do_telnetopt++; |
| 2605 | } |
| 2606 | s->do_telnetopt++; |
| 2607 | } |
| 2608 | if (s->do_telnetopt >= 4) { |
| 2609 | s->do_telnetopt = 1; |
| 2610 | } |
| 2611 | } else { |
| 2612 | if ((unsigned char)buf[i] == IAC) { |
| 2613 | s->do_telnetopt = 2; |
| 2614 | } else { |
| 2615 | if (j != i) |
| 2616 | buf[j] = buf[i]; |
| 2617 | j++; |
| 2618 | } |
| 2619 | } |
| 2620 | } |
| 2621 | *size = j; |
| 2622 | } |
| 2623 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2624 | static void tcp_chr_read(void *opaque) |
| 2625 | { |
| 2626 | CharDriverState *chr = opaque; |
| 2627 | TCPCharDriver *s = chr->opaque; |
| 2628 | uint8_t buf[1024]; |
| 2629 | int len, size; |
| 2630 | |
| 2631 | if (!s->connected || s->max_size <= 0) |
| 2632 | return; |
| 2633 | len = sizeof(buf); |
| 2634 | if (len > s->max_size) |
| 2635 | len = s->max_size; |
| 2636 | size = recv(s->fd, buf, len, 0); |
| 2637 | if (size == 0) { |
| 2638 | /* connection closed */ |
| 2639 | s->connected = 0; |
| 2640 | if (s->listen_fd >= 0) { |
| 2641 | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr); |
| 2642 | } |
| 2643 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 2644 | closesocket(s->fd); |
| 2645 | s->fd = -1; |
| 2646 | } else if (size > 0) { |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2647 | if (s->do_telnetopt) |
| 2648 | tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
| 2649 | if (size > 0) |
pbrook | e5b0bc4 | 2007-01-27 23:46:43 +0000 | [diff] [blame] | 2650 | qemu_chr_read(chr, buf, size); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2651 | } |
| 2652 | } |
| 2653 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2654 | static void tcp_chr_connect(void *opaque) |
| 2655 | { |
| 2656 | CharDriverState *chr = opaque; |
| 2657 | TCPCharDriver *s = chr->opaque; |
| 2658 | |
| 2659 | s->connected = 1; |
| 2660 | qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, |
| 2661 | tcp_chr_read, NULL, chr); |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2662 | qemu_chr_reset(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2665 | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
| 2666 | static void tcp_chr_telnet_init(int fd) |
| 2667 | { |
| 2668 | char buf[3]; |
| 2669 | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */ |
| 2670 | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
| 2671 | send(fd, (char *)buf, 3, 0); |
| 2672 | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
| 2673 | send(fd, (char *)buf, 3, 0); |
| 2674 | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
| 2675 | send(fd, (char *)buf, 3, 0); |
| 2676 | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
| 2677 | send(fd, (char *)buf, 3, 0); |
| 2678 | } |
| 2679 | |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2680 | static void socket_set_nodelay(int fd) |
| 2681 | { |
| 2682 | int val = 1; |
| 2683 | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
| 2684 | } |
| 2685 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2686 | static void tcp_chr_accept(void *opaque) |
| 2687 | { |
| 2688 | CharDriverState *chr = opaque; |
| 2689 | TCPCharDriver *s = chr->opaque; |
| 2690 | struct sockaddr_in saddr; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2691 | #ifndef _WIN32 |
| 2692 | struct sockaddr_un uaddr; |
| 2693 | #endif |
| 2694 | struct sockaddr *addr; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2695 | socklen_t len; |
| 2696 | int fd; |
| 2697 | |
| 2698 | for(;;) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2699 | #ifndef _WIN32 |
| 2700 | if (s->is_unix) { |
| 2701 | len = sizeof(uaddr); |
| 2702 | addr = (struct sockaddr *)&uaddr; |
| 2703 | } else |
| 2704 | #endif |
| 2705 | { |
| 2706 | len = sizeof(saddr); |
| 2707 | addr = (struct sockaddr *)&saddr; |
| 2708 | } |
| 2709 | fd = accept(s->listen_fd, addr, &len); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2710 | if (fd < 0 && errno != EINTR) { |
| 2711 | return; |
| 2712 | } else if (fd >= 0) { |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2713 | if (s->do_telnetopt) |
| 2714 | tcp_chr_telnet_init(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2715 | break; |
| 2716 | } |
| 2717 | } |
| 2718 | socket_set_nonblock(fd); |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2719 | if (s->do_nodelay) |
| 2720 | socket_set_nodelay(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2721 | s->fd = fd; |
| 2722 | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
| 2723 | tcp_chr_connect(chr); |
| 2724 | } |
| 2725 | |
| 2726 | static void tcp_chr_close(CharDriverState *chr) |
| 2727 | { |
| 2728 | TCPCharDriver *s = chr->opaque; |
| 2729 | if (s->fd >= 0) |
| 2730 | closesocket(s->fd); |
| 2731 | if (s->listen_fd >= 0) |
| 2732 | closesocket(s->listen_fd); |
| 2733 | qemu_free(s); |
| 2734 | } |
| 2735 | |
| 2736 | static CharDriverState *qemu_chr_open_tcp(const char *host_str, |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2737 | int is_telnet, |
| 2738 | int is_unix) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2739 | { |
| 2740 | CharDriverState *chr = NULL; |
| 2741 | TCPCharDriver *s = NULL; |
| 2742 | int fd = -1, ret, err, val; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2743 | int is_listen = 0; |
| 2744 | int is_waitconnect = 1; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2745 | int do_nodelay = 0; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2746 | const char *ptr; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2747 | struct sockaddr_in saddr; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2748 | #ifndef _WIN32 |
| 2749 | struct sockaddr_un uaddr; |
| 2750 | #endif |
| 2751 | struct sockaddr *addr; |
| 2752 | socklen_t addrlen; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2753 | |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2754 | #ifndef _WIN32 |
| 2755 | if (is_unix) { |
| 2756 | addr = (struct sockaddr *)&uaddr; |
| 2757 | addrlen = sizeof(uaddr); |
| 2758 | if (parse_unix_path(&uaddr, host_str) < 0) |
| 2759 | goto fail; |
| 2760 | } else |
| 2761 | #endif |
| 2762 | { |
| 2763 | addr = (struct sockaddr *)&saddr; |
| 2764 | addrlen = sizeof(saddr); |
| 2765 | if (parse_host_port(&saddr, host_str) < 0) |
| 2766 | goto fail; |
| 2767 | } |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2768 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2769 | ptr = host_str; |
| 2770 | while((ptr = strchr(ptr,','))) { |
| 2771 | ptr++; |
| 2772 | if (!strncmp(ptr,"server",6)) { |
| 2773 | is_listen = 1; |
| 2774 | } else if (!strncmp(ptr,"nowait",6)) { |
| 2775 | is_waitconnect = 0; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2776 | } else if (!strncmp(ptr,"nodelay",6)) { |
| 2777 | do_nodelay = 1; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2778 | } else { |
| 2779 | printf("Unknown option: %s\n", ptr); |
| 2780 | goto fail; |
| 2781 | } |
| 2782 | } |
| 2783 | if (!is_listen) |
| 2784 | is_waitconnect = 0; |
| 2785 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2786 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2787 | if (!chr) |
| 2788 | goto fail; |
| 2789 | s = qemu_mallocz(sizeof(TCPCharDriver)); |
| 2790 | if (!s) |
| 2791 | goto fail; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2792 | |
| 2793 | #ifndef _WIN32 |
| 2794 | if (is_unix) |
| 2795 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 2796 | else |
| 2797 | #endif |
| 2798 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 2799 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2800 | if (fd < 0) |
| 2801 | goto fail; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2802 | |
| 2803 | if (!is_waitconnect) |
| 2804 | socket_set_nonblock(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2805 | |
| 2806 | s->connected = 0; |
| 2807 | s->fd = -1; |
| 2808 | s->listen_fd = -1; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2809 | s->is_unix = is_unix; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2810 | s->do_nodelay = do_nodelay && !is_unix; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2811 | |
| 2812 | chr->opaque = s; |
| 2813 | chr->chr_write = tcp_chr_write; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2814 | chr->chr_close = tcp_chr_close; |
| 2815 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2816 | if (is_listen) { |
| 2817 | /* allow fast reuse */ |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2818 | #ifndef _WIN32 |
| 2819 | if (is_unix) { |
| 2820 | char path[109]; |
| 2821 | strncpy(path, uaddr.sun_path, 108); |
| 2822 | path[108] = 0; |
| 2823 | unlink(path); |
| 2824 | } else |
| 2825 | #endif |
| 2826 | { |
| 2827 | val = 1; |
| 2828 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
| 2829 | } |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2830 | |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2831 | ret = bind(fd, addr, addrlen); |
| 2832 | if (ret < 0) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2833 | goto fail; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2834 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2835 | ret = listen(fd, 0); |
| 2836 | if (ret < 0) |
| 2837 | goto fail; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2838 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2839 | s->listen_fd = fd; |
| 2840 | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr); |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2841 | if (is_telnet) |
| 2842 | s->do_telnetopt = 1; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2843 | } else { |
| 2844 | for(;;) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2845 | ret = connect(fd, addr, addrlen); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2846 | if (ret < 0) { |
| 2847 | err = socket_error(); |
| 2848 | if (err == EINTR || err == EWOULDBLOCK) { |
| 2849 | } else if (err == EINPROGRESS) { |
| 2850 | break; |
| 2851 | } else { |
| 2852 | goto fail; |
| 2853 | } |
| 2854 | } else { |
| 2855 | s->connected = 1; |
| 2856 | break; |
| 2857 | } |
| 2858 | } |
| 2859 | s->fd = fd; |
pbrook | f749998 | 2007-01-28 00:10:01 +0000 | [diff] [blame] | 2860 | socket_set_nodelay(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2861 | if (s->connected) |
| 2862 | tcp_chr_connect(chr); |
| 2863 | else |
| 2864 | qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr); |
| 2865 | } |
| 2866 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2867 | if (is_listen && is_waitconnect) { |
| 2868 | printf("QEMU waiting for connection on: %s\n", host_str); |
| 2869 | tcp_chr_accept(chr); |
| 2870 | socket_set_nonblock(s->listen_fd); |
| 2871 | } |
| 2872 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2873 | return chr; |
| 2874 | fail: |
| 2875 | if (fd >= 0) |
| 2876 | closesocket(fd); |
| 2877 | qemu_free(s); |
| 2878 | qemu_free(chr); |
| 2879 | return NULL; |
| 2880 | } |
| 2881 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2882 | CharDriverState *qemu_chr_open(const char *filename) |
| 2883 | { |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2884 | const char *p; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 2885 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2886 | if (!strcmp(filename, "vc")) { |
| 2887 | return text_console_init(&display_state); |
| 2888 | } else if (!strcmp(filename, "null")) { |
| 2889 | return qemu_chr_open_null(); |
bellard | 7664728 | 2005-11-27 19:10:42 +0000 | [diff] [blame] | 2890 | } else |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2891 | if (strstart(filename, "tcp:", &p)) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2892 | return qemu_chr_open_tcp(p, 0, 0); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2893 | } else |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2894 | if (strstart(filename, "telnet:", &p)) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2895 | return qemu_chr_open_tcp(p, 1, 0); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2896 | } else |
| 2897 | if (strstart(filename, "udp:", &p)) { |
| 2898 | return qemu_chr_open_udp(p); |
| 2899 | } else |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame^] | 2900 | if (strstart(filename, "mon:", &p)) { |
| 2901 | CharDriverState *drv = qemu_chr_open(p); |
| 2902 | if (drv) { |
| 2903 | drv = qemu_chr_open_mux(drv); |
| 2904 | monitor_init(drv, !nographic); |
| 2905 | return drv; |
| 2906 | } |
| 2907 | printf("Unable to open driver: %s\n", p); |
| 2908 | return 0; |
| 2909 | } else |
bellard | 7664728 | 2005-11-27 19:10:42 +0000 | [diff] [blame] | 2910 | #ifndef _WIN32 |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2911 | if (strstart(filename, "unix:", &p)) { |
| 2912 | return qemu_chr_open_tcp(p, 0, 1); |
| 2913 | } else if (strstart(filename, "file:", &p)) { |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2914 | return qemu_chr_open_file_out(p); |
| 2915 | } else if (strstart(filename, "pipe:", &p)) { |
| 2916 | return qemu_chr_open_pipe(p); |
bellard | 7664728 | 2005-11-27 19:10:42 +0000 | [diff] [blame] | 2917 | } else if (!strcmp(filename, "pty")) { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2918 | return qemu_chr_open_pty(); |
| 2919 | } else if (!strcmp(filename, "stdio")) { |
| 2920 | return qemu_chr_open_stdio(); |
| 2921 | } else |
| 2922 | #endif |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2923 | #if defined(__linux__) |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2924 | if (strstart(filename, "/dev/parport", NULL)) { |
| 2925 | return qemu_chr_open_pp(filename); |
| 2926 | } else |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2927 | if (strstart(filename, "/dev/", NULL)) { |
| 2928 | return qemu_chr_open_tty(filename); |
| 2929 | } else |
| 2930 | #endif |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2931 | #ifdef _WIN32 |
| 2932 | if (strstart(filename, "COM", NULL)) { |
| 2933 | return qemu_chr_open_win(filename); |
| 2934 | } else |
| 2935 | if (strstart(filename, "pipe:", &p)) { |
| 2936 | return qemu_chr_open_win_pipe(p); |
| 2937 | } else |
| 2938 | if (strstart(filename, "file:", &p)) { |
| 2939 | return qemu_chr_open_win_file_out(p); |
| 2940 | } |
| 2941 | #endif |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2942 | { |
| 2943 | return NULL; |
| 2944 | } |
| 2945 | } |
| 2946 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2947 | void qemu_chr_close(CharDriverState *chr) |
| 2948 | { |
| 2949 | if (chr->chr_close) |
| 2950 | chr->chr_close(chr); |
| 2951 | } |
| 2952 | |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 2953 | /***********************************************************/ |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2954 | /* network device redirectors */ |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 2955 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2956 | void hex_dump(FILE *f, const uint8_t *buf, int size) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 2957 | { |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2958 | int len, i, j, c; |
| 2959 | |
| 2960 | for(i=0;i<size;i+=16) { |
| 2961 | len = size - i; |
| 2962 | if (len > 16) |
| 2963 | len = 16; |
| 2964 | fprintf(f, "%08x ", i); |
| 2965 | for(j=0;j<16;j++) { |
| 2966 | if (j < len) |
| 2967 | fprintf(f, " %02x", buf[i+j]); |
| 2968 | else |
| 2969 | fprintf(f, " "); |
| 2970 | } |
| 2971 | fprintf(f, " "); |
| 2972 | for(j=0;j<len;j++) { |
| 2973 | c = buf[i+j]; |
| 2974 | if (c < ' ' || c > '~') |
| 2975 | c = '.'; |
| 2976 | fprintf(f, "%c", c); |
| 2977 | } |
| 2978 | fprintf(f, "\n"); |
| 2979 | } |
| 2980 | } |
| 2981 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2982 | static int parse_macaddr(uint8_t *macaddr, const char *p) |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2983 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2984 | int i; |
| 2985 | for(i = 0; i < 6; i++) { |
| 2986 | macaddr[i] = strtol(p, (char **)&p, 16); |
| 2987 | if (i == 5) { |
| 2988 | if (*p != '\0') |
| 2989 | return -1; |
| 2990 | } else { |
| 2991 | if (*p != ':') |
| 2992 | return -1; |
| 2993 | p++; |
| 2994 | } |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2995 | } |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2996 | return 0; |
| 2997 | } |
| 2998 | |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 2999 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
| 3000 | { |
| 3001 | const char *p, *p1; |
| 3002 | int len; |
| 3003 | p = *pp; |
| 3004 | p1 = strchr(p, sep); |
| 3005 | if (!p1) |
| 3006 | return -1; |
| 3007 | len = p1 - p; |
| 3008 | p1++; |
| 3009 | if (buf_size > 0) { |
| 3010 | if (len > buf_size - 1) |
| 3011 | len = buf_size - 1; |
| 3012 | memcpy(buf, p, len); |
| 3013 | buf[len] = '\0'; |
| 3014 | } |
| 3015 | *pp = p1; |
| 3016 | return 0; |
| 3017 | } |
| 3018 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 3019 | int parse_host_src_port(struct sockaddr_in *haddr, |
| 3020 | struct sockaddr_in *saddr, |
| 3021 | const char *input_str) |
| 3022 | { |
| 3023 | char *str = strdup(input_str); |
| 3024 | char *host_str = str; |
| 3025 | char *src_str; |
| 3026 | char *ptr; |
| 3027 | |
| 3028 | /* |
| 3029 | * Chop off any extra arguments at the end of the string which |
| 3030 | * would start with a comma, then fill in the src port information |
| 3031 | * if it was provided else use the "any address" and "any port". |
| 3032 | */ |
| 3033 | if ((ptr = strchr(str,','))) |
| 3034 | *ptr = '\0'; |
| 3035 | |
| 3036 | if ((src_str = strchr(input_str,'@'))) { |
| 3037 | *src_str = '\0'; |
| 3038 | src_str++; |
| 3039 | } |
| 3040 | |
| 3041 | if (parse_host_port(haddr, host_str) < 0) |
| 3042 | goto fail; |
| 3043 | |
| 3044 | if (!src_str || *src_str == '\0') |
| 3045 | src_str = ":0"; |
| 3046 | |
| 3047 | if (parse_host_port(saddr, src_str) < 0) |
| 3048 | goto fail; |
| 3049 | |
| 3050 | free(str); |
| 3051 | return(0); |
| 3052 | |
| 3053 | fail: |
| 3054 | free(str); |
| 3055 | return -1; |
| 3056 | } |
| 3057 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3058 | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
| 3059 | { |
| 3060 | char buf[512]; |
| 3061 | struct hostent *he; |
| 3062 | const char *p, *r; |
| 3063 | int port; |
| 3064 | |
| 3065 | p = str; |
| 3066 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3067 | return -1; |
| 3068 | saddr->sin_family = AF_INET; |
| 3069 | if (buf[0] == '\0') { |
| 3070 | saddr->sin_addr.s_addr = 0; |
| 3071 | } else { |
| 3072 | if (isdigit(buf[0])) { |
| 3073 | if (!inet_aton(buf, &saddr->sin_addr)) |
| 3074 | return -1; |
| 3075 | } else { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3076 | if ((he = gethostbyname(buf)) == NULL) |
| 3077 | return - 1; |
| 3078 | saddr->sin_addr = *(struct in_addr *)he->h_addr; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3079 | } |
| 3080 | } |
| 3081 | port = strtol(p, (char **)&r, 0); |
| 3082 | if (r == p) |
| 3083 | return -1; |
| 3084 | saddr->sin_port = htons(port); |
| 3085 | return 0; |
| 3086 | } |
| 3087 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 3088 | #ifndef _WIN32 |
| 3089 | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3090 | { |
| 3091 | const char *p; |
| 3092 | int len; |
| 3093 | |
| 3094 | len = MIN(108, strlen(str)); |
| 3095 | p = strchr(str, ','); |
| 3096 | if (p) |
| 3097 | len = MIN(len, p - str); |
| 3098 | |
| 3099 | memset(uaddr, 0, sizeof(*uaddr)); |
| 3100 | |
| 3101 | uaddr->sun_family = AF_UNIX; |
| 3102 | memcpy(uaddr->sun_path, str, len); |
| 3103 | |
| 3104 | return 0; |
| 3105 | } |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 3106 | #endif |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 3107 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3108 | /* find or alloc a new VLAN */ |
| 3109 | VLANState *qemu_find_vlan(int id) |
| 3110 | { |
| 3111 | VLANState **pvlan, *vlan; |
| 3112 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
| 3113 | if (vlan->id == id) |
| 3114 | return vlan; |
| 3115 | } |
| 3116 | vlan = qemu_mallocz(sizeof(VLANState)); |
| 3117 | if (!vlan) |
| 3118 | return NULL; |
| 3119 | vlan->id = id; |
| 3120 | vlan->next = NULL; |
| 3121 | pvlan = &first_vlan; |
| 3122 | while (*pvlan != NULL) |
| 3123 | pvlan = &(*pvlan)->next; |
| 3124 | *pvlan = vlan; |
| 3125 | return vlan; |
| 3126 | } |
| 3127 | |
| 3128 | VLANClientState *qemu_new_vlan_client(VLANState *vlan, |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3129 | IOReadHandler *fd_read, |
| 3130 | IOCanRWHandler *fd_can_read, |
| 3131 | void *opaque) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3132 | { |
| 3133 | VLANClientState *vc, **pvc; |
| 3134 | vc = qemu_mallocz(sizeof(VLANClientState)); |
| 3135 | if (!vc) |
| 3136 | return NULL; |
| 3137 | vc->fd_read = fd_read; |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3138 | vc->fd_can_read = fd_can_read; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3139 | vc->opaque = opaque; |
| 3140 | vc->vlan = vlan; |
| 3141 | |
| 3142 | vc->next = NULL; |
| 3143 | pvc = &vlan->first_client; |
| 3144 | while (*pvc != NULL) |
| 3145 | pvc = &(*pvc)->next; |
| 3146 | *pvc = vc; |
| 3147 | return vc; |
| 3148 | } |
| 3149 | |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3150 | int qemu_can_send_packet(VLANClientState *vc1) |
| 3151 | { |
| 3152 | VLANState *vlan = vc1->vlan; |
| 3153 | VLANClientState *vc; |
| 3154 | |
| 3155 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
| 3156 | if (vc != vc1) { |
| 3157 | if (vc->fd_can_read && !vc->fd_can_read(vc->opaque)) |
| 3158 | return 0; |
| 3159 | } |
| 3160 | } |
| 3161 | return 1; |
| 3162 | } |
| 3163 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3164 | void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size) |
| 3165 | { |
| 3166 | VLANState *vlan = vc1->vlan; |
| 3167 | VLANClientState *vc; |
| 3168 | |
| 3169 | #if 0 |
| 3170 | printf("vlan %d send:\n", vlan->id); |
| 3171 | hex_dump(stdout, buf, size); |
| 3172 | #endif |
| 3173 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
| 3174 | if (vc != vc1) { |
| 3175 | vc->fd_read(vc->opaque, buf, size); |
| 3176 | } |
| 3177 | } |
| 3178 | } |
| 3179 | |
| 3180 | #if defined(CONFIG_SLIRP) |
| 3181 | |
| 3182 | /* slirp network adapter */ |
| 3183 | |
| 3184 | static int slirp_inited; |
| 3185 | static VLANClientState *slirp_vc; |
| 3186 | |
| 3187 | int slirp_can_output(void) |
| 3188 | { |
pbrook | 3b7f5d4 | 2006-02-10 17:34:02 +0000 | [diff] [blame] | 3189 | return !slirp_vc || qemu_can_send_packet(slirp_vc); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | void slirp_output(const uint8_t *pkt, int pkt_len) |
| 3193 | { |
| 3194 | #if 0 |
| 3195 | printf("slirp output:\n"); |
| 3196 | hex_dump(stdout, pkt, pkt_len); |
| 3197 | #endif |
pbrook | 3b7f5d4 | 2006-02-10 17:34:02 +0000 | [diff] [blame] | 3198 | if (!slirp_vc) |
| 3199 | return; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3200 | qemu_send_packet(slirp_vc, pkt, pkt_len); |
| 3201 | } |
| 3202 | |
| 3203 | static void slirp_receive(void *opaque, const uint8_t *buf, int size) |
| 3204 | { |
| 3205 | #if 0 |
| 3206 | printf("slirp input:\n"); |
| 3207 | hex_dump(stdout, buf, size); |
| 3208 | #endif |
| 3209 | slirp_input(buf, size); |
| 3210 | } |
| 3211 | |
| 3212 | static int net_slirp_init(VLANState *vlan) |
| 3213 | { |
| 3214 | if (!slirp_inited) { |
| 3215 | slirp_inited = 1; |
| 3216 | slirp_init(); |
| 3217 | } |
| 3218 | slirp_vc = qemu_new_vlan_client(vlan, |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3219 | slirp_receive, NULL, NULL); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3220 | snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector"); |
| 3221 | return 0; |
| 3222 | } |
| 3223 | |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3224 | static void net_slirp_redir(const char *redir_str) |
| 3225 | { |
| 3226 | int is_udp; |
| 3227 | char buf[256], *r; |
| 3228 | const char *p; |
| 3229 | struct in_addr guest_addr; |
| 3230 | int host_port, guest_port; |
| 3231 | |
| 3232 | if (!slirp_inited) { |
| 3233 | slirp_inited = 1; |
| 3234 | slirp_init(); |
| 3235 | } |
| 3236 | |
| 3237 | p = redir_str; |
| 3238 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3239 | goto fail; |
| 3240 | if (!strcmp(buf, "tcp")) { |
| 3241 | is_udp = 0; |
| 3242 | } else if (!strcmp(buf, "udp")) { |
| 3243 | is_udp = 1; |
| 3244 | } else { |
| 3245 | goto fail; |
| 3246 | } |
| 3247 | |
| 3248 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3249 | goto fail; |
| 3250 | host_port = strtol(buf, &r, 0); |
| 3251 | if (r == buf) |
| 3252 | goto fail; |
| 3253 | |
| 3254 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3255 | goto fail; |
| 3256 | if (buf[0] == '\0') { |
| 3257 | pstrcpy(buf, sizeof(buf), "10.0.2.15"); |
| 3258 | } |
| 3259 | if (!inet_aton(buf, &guest_addr)) |
| 3260 | goto fail; |
| 3261 | |
| 3262 | guest_port = strtol(p, &r, 0); |
| 3263 | if (r == p) |
| 3264 | goto fail; |
| 3265 | |
| 3266 | if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { |
| 3267 | fprintf(stderr, "qemu: could not set up redirection\n"); |
| 3268 | exit(1); |
| 3269 | } |
| 3270 | return; |
| 3271 | fail: |
| 3272 | fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n"); |
| 3273 | exit(1); |
| 3274 | } |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3275 | |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 3276 | #ifndef _WIN32 |
| 3277 | |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3278 | char smb_dir[1024]; |
| 3279 | |
| 3280 | static void smb_exit(void) |
| 3281 | { |
| 3282 | DIR *d; |
| 3283 | struct dirent *de; |
| 3284 | char filename[1024]; |
| 3285 | |
| 3286 | /* erase all the files in the directory */ |
| 3287 | d = opendir(smb_dir); |
| 3288 | for(;;) { |
| 3289 | de = readdir(d); |
| 3290 | if (!de) |
| 3291 | break; |
| 3292 | if (strcmp(de->d_name, ".") != 0 && |
| 3293 | strcmp(de->d_name, "..") != 0) { |
| 3294 | snprintf(filename, sizeof(filename), "%s/%s", |
| 3295 | smb_dir, de->d_name); |
| 3296 | unlink(filename); |
| 3297 | } |
| 3298 | } |
bellard | 03ffbb6 | 2004-09-06 00:14:04 +0000 | [diff] [blame] | 3299 | closedir(d); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3300 | rmdir(smb_dir); |
| 3301 | } |
| 3302 | |
| 3303 | /* automatic user mode samba server configuration */ |
| 3304 | void net_slirp_smb(const char *exported_dir) |
| 3305 | { |
| 3306 | char smb_conf[1024]; |
| 3307 | char smb_cmdline[1024]; |
| 3308 | FILE *f; |
| 3309 | |
| 3310 | if (!slirp_inited) { |
| 3311 | slirp_inited = 1; |
| 3312 | slirp_init(); |
| 3313 | } |
| 3314 | |
| 3315 | /* XXX: better tmp dir construction */ |
| 3316 | snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); |
| 3317 | if (mkdir(smb_dir, 0700) < 0) { |
| 3318 | fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir); |
| 3319 | exit(1); |
| 3320 | } |
| 3321 | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); |
| 3322 | |
| 3323 | f = fopen(smb_conf, "w"); |
| 3324 | if (!f) { |
| 3325 | fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf); |
| 3326 | exit(1); |
| 3327 | } |
| 3328 | fprintf(f, |
| 3329 | "[global]\n" |
bellard | 157777e | 2005-03-01 21:28:45 +0000 | [diff] [blame] | 3330 | "private dir=%s\n" |
| 3331 | "smb ports=0\n" |
| 3332 | "socket address=127.0.0.1\n" |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3333 | "pid directory=%s\n" |
| 3334 | "lock directory=%s\n" |
| 3335 | "log file=%s/log.smbd\n" |
| 3336 | "smb passwd file=%s/smbpasswd\n" |
bellard | 03ffbb6 | 2004-09-06 00:14:04 +0000 | [diff] [blame] | 3337 | "security = share\n" |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3338 | "[qemu]\n" |
| 3339 | "path=%s\n" |
| 3340 | "read only=no\n" |
| 3341 | "guest ok=yes\n", |
| 3342 | smb_dir, |
| 3343 | smb_dir, |
| 3344 | smb_dir, |
| 3345 | smb_dir, |
bellard | 157777e | 2005-03-01 21:28:45 +0000 | [diff] [blame] | 3346 | smb_dir, |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3347 | exported_dir |
| 3348 | ); |
| 3349 | fclose(f); |
| 3350 | atexit(smb_exit); |
| 3351 | |
pbrook | a14d6c8 | 2006-12-23 15:37:33 +0000 | [diff] [blame] | 3352 | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
| 3353 | SMBD_COMMAND, smb_conf); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3354 | |
| 3355 | slirp_add_exec(0, smb_cmdline, 4, 139); |
| 3356 | } |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3357 | |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 3358 | #endif /* !defined(_WIN32) */ |
| 3359 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3360 | #endif /* CONFIG_SLIRP */ |
| 3361 | |
| 3362 | #if !defined(_WIN32) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3363 | |
| 3364 | typedef struct TAPState { |
| 3365 | VLANClientState *vc; |
| 3366 | int fd; |
| 3367 | } TAPState; |
| 3368 | |
| 3369 | static void tap_receive(void *opaque, const uint8_t *buf, int size) |
| 3370 | { |
| 3371 | TAPState *s = opaque; |
| 3372 | int ret; |
| 3373 | for(;;) { |
| 3374 | ret = write(s->fd, buf, size); |
| 3375 | if (ret < 0 && (errno == EINTR || errno == EAGAIN)) { |
| 3376 | } else { |
| 3377 | break; |
| 3378 | } |
| 3379 | } |
| 3380 | } |
| 3381 | |
| 3382 | static void tap_send(void *opaque) |
| 3383 | { |
| 3384 | TAPState *s = opaque; |
| 3385 | uint8_t buf[4096]; |
| 3386 | int size; |
| 3387 | |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 3388 | #ifdef __sun__ |
| 3389 | struct strbuf sbuf; |
| 3390 | int f = 0; |
| 3391 | sbuf.maxlen = sizeof(buf); |
| 3392 | sbuf.buf = buf; |
| 3393 | size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1; |
| 3394 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3395 | size = read(s->fd, buf, sizeof(buf)); |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 3396 | #endif |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3397 | if (size > 0) { |
| 3398 | qemu_send_packet(s->vc, buf, size); |
| 3399 | } |
| 3400 | } |
| 3401 | |
| 3402 | /* fd support */ |
| 3403 | |
| 3404 | static TAPState *net_tap_fd_init(VLANState *vlan, int fd) |
| 3405 | { |
| 3406 | TAPState *s; |
| 3407 | |
| 3408 | s = qemu_mallocz(sizeof(TAPState)); |
| 3409 | if (!s) |
| 3410 | return NULL; |
| 3411 | s->fd = fd; |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3412 | s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3413 | qemu_set_fd_handler(s->fd, tap_send, NULL, s); |
| 3414 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd); |
| 3415 | return s; |
| 3416 | } |
| 3417 | |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3418 | #ifdef _BSD |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3419 | static int tap_open(char *ifname, int ifname_size) |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3420 | { |
| 3421 | int fd; |
| 3422 | char *dev; |
| 3423 | struct stat s; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 3424 | |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3425 | fd = open("/dev/tap", O_RDWR); |
| 3426 | if (fd < 0) { |
| 3427 | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n"); |
| 3428 | return -1; |
| 3429 | } |
| 3430 | |
| 3431 | fstat(fd, &s); |
| 3432 | dev = devname(s.st_rdev, S_IFCHR); |
| 3433 | pstrcpy(ifname, ifname_size, dev); |
| 3434 | |
| 3435 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 3436 | return fd; |
| 3437 | } |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 3438 | #elif defined(__sun__) |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 3439 | #define TUNNEWPPA (('T'<<16) | 0x0001) |
| 3440 | /* |
| 3441 | * Allocate TAP device, returns opened fd. |
| 3442 | * Stores dev name in the first arg(must be large enough). |
| 3443 | */ |
| 3444 | int tap_alloc(char *dev) |
| 3445 | { |
| 3446 | int tap_fd, if_fd, ppa = -1; |
| 3447 | static int ip_fd = 0; |
| 3448 | char *ptr; |
| 3449 | |
| 3450 | static int arp_fd = 0; |
| 3451 | int ip_muxid, arp_muxid; |
| 3452 | struct strioctl strioc_if, strioc_ppa; |
| 3453 | int link_type = I_PLINK;; |
| 3454 | struct lifreq ifr; |
| 3455 | char actual_name[32] = ""; |
| 3456 | |
| 3457 | memset(&ifr, 0x0, sizeof(ifr)); |
| 3458 | |
| 3459 | if( *dev ){ |
| 3460 | ptr = dev; |
| 3461 | while( *ptr && !isdigit((int)*ptr) ) ptr++; |
| 3462 | ppa = atoi(ptr); |
| 3463 | } |
| 3464 | |
| 3465 | /* Check if IP device was opened */ |
| 3466 | if( ip_fd ) |
| 3467 | close(ip_fd); |
| 3468 | |
| 3469 | if( (ip_fd = open("/dev/udp", O_RDWR, 0)) < 0){ |
| 3470 | syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)"); |
| 3471 | return -1; |
| 3472 | } |
| 3473 | |
| 3474 | if( (tap_fd = open("/dev/tap", O_RDWR, 0)) < 0){ |
| 3475 | syslog(LOG_ERR, "Can't open /dev/tap"); |
| 3476 | return -1; |
| 3477 | } |
| 3478 | |
| 3479 | /* Assign a new PPA and get its unit number. */ |
| 3480 | strioc_ppa.ic_cmd = TUNNEWPPA; |
| 3481 | strioc_ppa.ic_timout = 0; |
| 3482 | strioc_ppa.ic_len = sizeof(ppa); |
| 3483 | strioc_ppa.ic_dp = (char *)&ppa; |
| 3484 | if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) |
| 3485 | syslog (LOG_ERR, "Can't assign new interface"); |
| 3486 | |
| 3487 | if( (if_fd = open("/dev/tap", O_RDWR, 0)) < 0){ |
| 3488 | syslog(LOG_ERR, "Can't open /dev/tap (2)"); |
| 3489 | return -1; |
| 3490 | } |
| 3491 | if(ioctl(if_fd, I_PUSH, "ip") < 0){ |
| 3492 | syslog(LOG_ERR, "Can't push IP module"); |
| 3493 | return -1; |
| 3494 | } |
| 3495 | |
| 3496 | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) |
| 3497 | syslog(LOG_ERR, "Can't get flags\n"); |
| 3498 | |
| 3499 | snprintf (actual_name, 32, "tap%d", ppa); |
| 3500 | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name)); |
| 3501 | |
| 3502 | ifr.lifr_ppa = ppa; |
| 3503 | /* Assign ppa according to the unit number returned by tun device */ |
| 3504 | |
| 3505 | if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) |
| 3506 | syslog (LOG_ERR, "Can't set PPA %d", ppa); |
| 3507 | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) |
| 3508 | syslog (LOG_ERR, "Can't get flags\n"); |
| 3509 | /* Push arp module to if_fd */ |
| 3510 | if (ioctl (if_fd, I_PUSH, "arp") < 0) |
| 3511 | syslog (LOG_ERR, "Can't push ARP module (2)"); |
| 3512 | |
| 3513 | /* Push arp module to ip_fd */ |
| 3514 | if (ioctl (ip_fd, I_POP, NULL) < 0) |
| 3515 | syslog (LOG_ERR, "I_POP failed\n"); |
| 3516 | if (ioctl (ip_fd, I_PUSH, "arp") < 0) |
| 3517 | syslog (LOG_ERR, "Can't push ARP module (3)\n"); |
| 3518 | /* Open arp_fd */ |
| 3519 | if ((arp_fd = open ("/dev/tap", O_RDWR, 0)) < 0) |
| 3520 | syslog (LOG_ERR, "Can't open %s\n", "/dev/tap"); |
| 3521 | |
| 3522 | /* Set ifname to arp */ |
| 3523 | strioc_if.ic_cmd = SIOCSLIFNAME; |
| 3524 | strioc_if.ic_timout = 0; |
| 3525 | strioc_if.ic_len = sizeof(ifr); |
| 3526 | strioc_if.ic_dp = (char *)𝔦 |
| 3527 | if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ |
| 3528 | syslog (LOG_ERR, "Can't set ifname to arp\n"); |
| 3529 | } |
| 3530 | |
| 3531 | if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ |
| 3532 | syslog(LOG_ERR, "Can't link TAP device to IP"); |
| 3533 | return -1; |
| 3534 | } |
| 3535 | |
| 3536 | if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) |
| 3537 | syslog (LOG_ERR, "Can't link TAP device to ARP"); |
| 3538 | |
| 3539 | close (if_fd); |
| 3540 | |
| 3541 | memset(&ifr, 0x0, sizeof(ifr)); |
| 3542 | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name)); |
| 3543 | ifr.lifr_ip_muxid = ip_muxid; |
| 3544 | ifr.lifr_arp_muxid = arp_muxid; |
| 3545 | |
| 3546 | if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) |
| 3547 | { |
| 3548 | ioctl (ip_fd, I_PUNLINK , arp_muxid); |
| 3549 | ioctl (ip_fd, I_PUNLINK, ip_muxid); |
| 3550 | syslog (LOG_ERR, "Can't set multiplexor id"); |
| 3551 | } |
| 3552 | |
| 3553 | sprintf(dev, "tap%d", ppa); |
| 3554 | return tap_fd; |
| 3555 | } |
| 3556 | |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 3557 | static int tap_open(char *ifname, int ifname_size) |
| 3558 | { |
ths | d5d10bc | 2007-02-17 22:54:49 +0000 | [diff] [blame] | 3559 | char dev[10]=""; |
| 3560 | int fd; |
| 3561 | if( (fd = tap_alloc(dev)) < 0 ){ |
| 3562 | fprintf(stderr, "Cannot allocate TAP device\n"); |
| 3563 | return -1; |
| 3564 | } |
| 3565 | pstrcpy(ifname, ifname_size, dev); |
| 3566 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 3567 | return fd; |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 3568 | } |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3569 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3570 | static int tap_open(char *ifname, int ifname_size) |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3571 | { |
| 3572 | struct ifreq ifr; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3573 | int fd, ret; |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3574 | |
| 3575 | fd = open("/dev/net/tun", O_RDWR); |
| 3576 | if (fd < 0) { |
| 3577 | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n"); |
| 3578 | return -1; |
| 3579 | } |
| 3580 | memset(&ifr, 0, sizeof(ifr)); |
| 3581 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3582 | if (ifname[0] != '\0') |
| 3583 | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
| 3584 | else |
| 3585 | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d"); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3586 | ret = ioctl(fd, TUNSETIFF, (void *) &ifr); |
| 3587 | if (ret != 0) { |
| 3588 | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n"); |
| 3589 | close(fd); |
| 3590 | return -1; |
| 3591 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3592 | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3593 | fcntl(fd, F_SETFL, O_NONBLOCK); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3594 | return fd; |
| 3595 | } |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3596 | #endif |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3597 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3598 | static int net_tap_init(VLANState *vlan, const char *ifname1, |
| 3599 | const char *setup_script) |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3600 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3601 | TAPState *s; |
| 3602 | int pid, status, fd; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3603 | char *args[3]; |
| 3604 | char **parg; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3605 | char ifname[128]; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3606 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3607 | if (ifname1 != NULL) |
| 3608 | pstrcpy(ifname, sizeof(ifname), ifname1); |
| 3609 | else |
| 3610 | ifname[0] = '\0'; |
| 3611 | fd = tap_open(ifname, sizeof(ifname)); |
| 3612 | if (fd < 0) |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3613 | return -1; |
| 3614 | |
ths | 6a1cbf6 | 2007-02-02 00:37:56 +0000 | [diff] [blame] | 3615 | if (!setup_script || !strcmp(setup_script, "no")) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3616 | setup_script = ""; |
| 3617 | if (setup_script[0] != '\0') { |
| 3618 | /* try to launch network init script */ |
| 3619 | pid = fork(); |
| 3620 | if (pid >= 0) { |
| 3621 | if (pid == 0) { |
| 3622 | parg = args; |
| 3623 | *parg++ = (char *)setup_script; |
| 3624 | *parg++ = ifname; |
| 3625 | *parg++ = NULL; |
| 3626 | execv(setup_script, args); |
bellard | 4a38940 | 2005-11-26 20:10:07 +0000 | [diff] [blame] | 3627 | _exit(1); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3628 | } |
| 3629 | while (waitpid(pid, &status, 0) != pid); |
| 3630 | if (!WIFEXITED(status) || |
| 3631 | WEXITSTATUS(status) != 0) { |
| 3632 | fprintf(stderr, "%s: could not launch network script\n", |
| 3633 | setup_script); |
| 3634 | return -1; |
| 3635 | } |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3636 | } |
| 3637 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3638 | s = net_tap_fd_init(vlan, fd); |
| 3639 | if (!s) |
| 3640 | return -1; |
| 3641 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 3642 | "tap: ifname=%s setup_script=%s", ifname, setup_script); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3643 | return 0; |
| 3644 | } |
| 3645 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3646 | #endif /* !_WIN32 */ |
| 3647 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3648 | /* network connection */ |
| 3649 | typedef struct NetSocketState { |
| 3650 | VLANClientState *vc; |
| 3651 | int fd; |
| 3652 | int state; /* 0 = getting length, 1 = getting data */ |
| 3653 | int index; |
| 3654 | int packet_len; |
| 3655 | uint8_t buf[4096]; |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3656 | 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] | 3657 | } NetSocketState; |
| 3658 | |
| 3659 | typedef struct NetSocketListenState { |
| 3660 | VLANState *vlan; |
| 3661 | int fd; |
| 3662 | } NetSocketListenState; |
| 3663 | |
| 3664 | /* XXX: we consider we can send the whole packet without blocking */ |
| 3665 | static void net_socket_receive(void *opaque, const uint8_t *buf, int size) |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3666 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3667 | NetSocketState *s = opaque; |
| 3668 | uint32_t len; |
| 3669 | len = htonl(size); |
| 3670 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3671 | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
| 3672 | send_all(s->fd, buf, size); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3673 | } |
| 3674 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3675 | static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size) |
| 3676 | { |
| 3677 | NetSocketState *s = opaque; |
| 3678 | sendto(s->fd, buf, size, 0, |
| 3679 | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
| 3680 | } |
| 3681 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3682 | static void net_socket_send(void *opaque) |
| 3683 | { |
| 3684 | NetSocketState *s = opaque; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3685 | int l, size, err; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3686 | uint8_t buf1[4096]; |
| 3687 | const uint8_t *buf; |
| 3688 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3689 | size = recv(s->fd, buf1, sizeof(buf1), 0); |
| 3690 | if (size < 0) { |
| 3691 | err = socket_error(); |
| 3692 | if (err != EWOULDBLOCK) |
| 3693 | goto eoc; |
| 3694 | } else if (size == 0) { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3695 | /* end of connection */ |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3696 | eoc: |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3697 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3698 | closesocket(s->fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3699 | return; |
| 3700 | } |
| 3701 | buf = buf1; |
| 3702 | while (size > 0) { |
| 3703 | /* reassemble a packet from the network */ |
| 3704 | switch(s->state) { |
| 3705 | case 0: |
| 3706 | l = 4 - s->index; |
| 3707 | if (l > size) |
| 3708 | l = size; |
| 3709 | memcpy(s->buf + s->index, buf, l); |
| 3710 | buf += l; |
| 3711 | size -= l; |
| 3712 | s->index += l; |
| 3713 | if (s->index == 4) { |
| 3714 | /* got length */ |
| 3715 | s->packet_len = ntohl(*(uint32_t *)s->buf); |
| 3716 | s->index = 0; |
| 3717 | s->state = 1; |
| 3718 | } |
| 3719 | break; |
| 3720 | case 1: |
| 3721 | l = s->packet_len - s->index; |
| 3722 | if (l > size) |
| 3723 | l = size; |
| 3724 | memcpy(s->buf + s->index, buf, l); |
| 3725 | s->index += l; |
| 3726 | buf += l; |
| 3727 | size -= l; |
| 3728 | if (s->index >= s->packet_len) { |
| 3729 | qemu_send_packet(s->vc, s->buf, s->packet_len); |
| 3730 | s->index = 0; |
| 3731 | s->state = 0; |
| 3732 | } |
| 3733 | break; |
| 3734 | } |
| 3735 | } |
| 3736 | } |
| 3737 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3738 | static void net_socket_send_dgram(void *opaque) |
| 3739 | { |
| 3740 | NetSocketState *s = opaque; |
| 3741 | int size; |
| 3742 | |
| 3743 | size = recv(s->fd, s->buf, sizeof(s->buf), 0); |
| 3744 | if (size < 0) |
| 3745 | return; |
| 3746 | if (size == 0) { |
| 3747 | /* end of connection */ |
| 3748 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 3749 | return; |
| 3750 | } |
| 3751 | qemu_send_packet(s->vc, s->buf, size); |
| 3752 | } |
| 3753 | |
| 3754 | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
| 3755 | { |
| 3756 | struct ip_mreq imr; |
| 3757 | int fd; |
| 3758 | int val, ret; |
| 3759 | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { |
| 3760 | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n", |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3761 | inet_ntoa(mcastaddr->sin_addr), |
| 3762 | (int)ntohl(mcastaddr->sin_addr.s_addr)); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3763 | return -1; |
| 3764 | |
| 3765 | } |
| 3766 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 3767 | if (fd < 0) { |
| 3768 | perror("socket(PF_INET, SOCK_DGRAM)"); |
| 3769 | return -1; |
| 3770 | } |
| 3771 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3772 | val = 1; |
| 3773 | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
| 3774 | (const char *)&val, sizeof(val)); |
| 3775 | if (ret < 0) { |
| 3776 | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); |
| 3777 | goto fail; |
| 3778 | } |
| 3779 | |
| 3780 | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
| 3781 | if (ret < 0) { |
| 3782 | perror("bind"); |
| 3783 | goto fail; |
| 3784 | } |
| 3785 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3786 | /* Add host to multicast group */ |
| 3787 | imr.imr_multiaddr = mcastaddr->sin_addr; |
| 3788 | imr.imr_interface.s_addr = htonl(INADDR_ANY); |
| 3789 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3790 | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 3791 | (const char *)&imr, sizeof(struct ip_mreq)); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3792 | if (ret < 0) { |
| 3793 | perror("setsockopt(IP_ADD_MEMBERSHIP)"); |
| 3794 | goto fail; |
| 3795 | } |
| 3796 | |
| 3797 | /* Force mcast msgs to loopback (eg. several QEMUs in same host */ |
| 3798 | val = 1; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3799 | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
| 3800 | (const char *)&val, sizeof(val)); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3801 | if (ret < 0) { |
| 3802 | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); |
| 3803 | goto fail; |
| 3804 | } |
| 3805 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3806 | socket_set_nonblock(fd); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3807 | return fd; |
| 3808 | fail: |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3809 | if (fd >= 0) |
| 3810 | closesocket(fd); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3811 | return -1; |
| 3812 | } |
| 3813 | |
| 3814 | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, |
| 3815 | int is_connected) |
| 3816 | { |
| 3817 | struct sockaddr_in saddr; |
| 3818 | int newfd; |
| 3819 | socklen_t saddr_len; |
| 3820 | NetSocketState *s; |
| 3821 | |
| 3822 | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it |
| 3823 | * Because this may be "shared" socket from a "master" process, datagrams would be recv() |
| 3824 | * by ONLY ONE process: we must "clone" this dgram socket --jjo |
| 3825 | */ |
| 3826 | |
| 3827 | if (is_connected) { |
| 3828 | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
| 3829 | /* must be bound */ |
| 3830 | if (saddr.sin_addr.s_addr==0) { |
| 3831 | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n", |
| 3832 | fd); |
| 3833 | return NULL; |
| 3834 | } |
| 3835 | /* clone dgram socket */ |
| 3836 | newfd = net_socket_mcast_create(&saddr); |
| 3837 | if (newfd < 0) { |
| 3838 | /* error already reported by net_socket_mcast_create() */ |
| 3839 | close(fd); |
| 3840 | return NULL; |
| 3841 | } |
| 3842 | /* clone newfd to fd, close newfd */ |
| 3843 | dup2(newfd, fd); |
| 3844 | close(newfd); |
| 3845 | |
| 3846 | } else { |
| 3847 | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", |
| 3848 | fd, strerror(errno)); |
| 3849 | return NULL; |
| 3850 | } |
| 3851 | } |
| 3852 | |
| 3853 | s = qemu_mallocz(sizeof(NetSocketState)); |
| 3854 | if (!s) |
| 3855 | return NULL; |
| 3856 | s->fd = fd; |
| 3857 | |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3858 | s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3859 | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); |
| 3860 | |
| 3861 | /* mcast: save bound address as dst */ |
| 3862 | if (is_connected) s->dgram_dst=saddr; |
| 3863 | |
| 3864 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 3865 | "socket: fd=%d (%s mcast=%s:%d)", |
| 3866 | fd, is_connected? "cloned" : "", |
| 3867 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 3868 | return s; |
| 3869 | } |
| 3870 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3871 | static void net_socket_connect(void *opaque) |
| 3872 | { |
| 3873 | NetSocketState *s = opaque; |
| 3874 | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s); |
| 3875 | } |
| 3876 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3877 | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3878 | int is_connected) |
| 3879 | { |
| 3880 | NetSocketState *s; |
| 3881 | s = qemu_mallocz(sizeof(NetSocketState)); |
| 3882 | if (!s) |
| 3883 | return NULL; |
| 3884 | s->fd = fd; |
| 3885 | s->vc = qemu_new_vlan_client(vlan, |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3886 | net_socket_receive, NULL, s); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3887 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 3888 | "socket: fd=%d", fd); |
| 3889 | if (is_connected) { |
| 3890 | net_socket_connect(s); |
| 3891 | } else { |
| 3892 | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); |
| 3893 | } |
| 3894 | return s; |
| 3895 | } |
| 3896 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3897 | static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, |
| 3898 | int is_connected) |
| 3899 | { |
| 3900 | int so_type=-1, optlen=sizeof(so_type); |
| 3901 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3902 | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, &optlen)< 0) { |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3903 | fprintf(stderr, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd); |
| 3904 | return NULL; |
| 3905 | } |
| 3906 | switch(so_type) { |
| 3907 | case SOCK_DGRAM: |
| 3908 | return net_socket_fd_init_dgram(vlan, fd, is_connected); |
| 3909 | case SOCK_STREAM: |
| 3910 | return net_socket_fd_init_stream(vlan, fd, is_connected); |
| 3911 | default: |
| 3912 | /* who knows ... this could be a eg. a pty, do warn and continue as stream */ |
| 3913 | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); |
| 3914 | return net_socket_fd_init_stream(vlan, fd, is_connected); |
| 3915 | } |
| 3916 | return NULL; |
| 3917 | } |
| 3918 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3919 | static void net_socket_accept(void *opaque) |
| 3920 | { |
| 3921 | NetSocketListenState *s = opaque; |
| 3922 | NetSocketState *s1; |
| 3923 | struct sockaddr_in saddr; |
| 3924 | socklen_t len; |
| 3925 | int fd; |
| 3926 | |
| 3927 | for(;;) { |
| 3928 | len = sizeof(saddr); |
| 3929 | fd = accept(s->fd, (struct sockaddr *)&saddr, &len); |
| 3930 | if (fd < 0 && errno != EINTR) { |
| 3931 | return; |
| 3932 | } else if (fd >= 0) { |
| 3933 | break; |
| 3934 | } |
| 3935 | } |
| 3936 | s1 = net_socket_fd_init(s->vlan, fd, 1); |
| 3937 | if (!s1) { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3938 | closesocket(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3939 | } else { |
| 3940 | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str), |
| 3941 | "socket: connection from %s:%d", |
| 3942 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 3943 | } |
| 3944 | } |
| 3945 | |
| 3946 | static int net_socket_listen_init(VLANState *vlan, const char *host_str) |
| 3947 | { |
| 3948 | NetSocketListenState *s; |
| 3949 | int fd, val, ret; |
| 3950 | struct sockaddr_in saddr; |
| 3951 | |
| 3952 | if (parse_host_port(&saddr, host_str) < 0) |
| 3953 | return -1; |
| 3954 | |
| 3955 | s = qemu_mallocz(sizeof(NetSocketListenState)); |
| 3956 | if (!s) |
| 3957 | return -1; |
| 3958 | |
| 3959 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 3960 | if (fd < 0) { |
| 3961 | perror("socket"); |
| 3962 | return -1; |
| 3963 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3964 | socket_set_nonblock(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3965 | |
| 3966 | /* allow fast reuse */ |
| 3967 | val = 1; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3968 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3969 | |
| 3970 | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 3971 | if (ret < 0) { |
| 3972 | perror("bind"); |
| 3973 | return -1; |
| 3974 | } |
| 3975 | ret = listen(fd, 0); |
| 3976 | if (ret < 0) { |
| 3977 | perror("listen"); |
| 3978 | return -1; |
| 3979 | } |
| 3980 | s->vlan = vlan; |
| 3981 | s->fd = fd; |
| 3982 | qemu_set_fd_handler(fd, net_socket_accept, NULL, s); |
| 3983 | return 0; |
| 3984 | } |
| 3985 | |
| 3986 | static int net_socket_connect_init(VLANState *vlan, const char *host_str) |
| 3987 | { |
| 3988 | NetSocketState *s; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3989 | int fd, connected, ret, err; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3990 | struct sockaddr_in saddr; |
| 3991 | |
| 3992 | if (parse_host_port(&saddr, host_str) < 0) |
| 3993 | return -1; |
| 3994 | |
| 3995 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 3996 | if (fd < 0) { |
| 3997 | perror("socket"); |
| 3998 | return -1; |
| 3999 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4000 | socket_set_nonblock(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4001 | |
| 4002 | connected = 0; |
| 4003 | for(;;) { |
| 4004 | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 4005 | if (ret < 0) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4006 | err = socket_error(); |
| 4007 | if (err == EINTR || err == EWOULDBLOCK) { |
| 4008 | } else if (err == EINPROGRESS) { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4009 | break; |
| 4010 | } else { |
| 4011 | perror("connect"); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4012 | closesocket(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4013 | return -1; |
| 4014 | } |
| 4015 | } else { |
| 4016 | connected = 1; |
| 4017 | break; |
| 4018 | } |
| 4019 | } |
| 4020 | s = net_socket_fd_init(vlan, fd, connected); |
| 4021 | if (!s) |
| 4022 | return -1; |
| 4023 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 4024 | "socket: connect to %s:%d", |
| 4025 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 4026 | return 0; |
| 4027 | } |
| 4028 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4029 | static int net_socket_mcast_init(VLANState *vlan, const char *host_str) |
| 4030 | { |
| 4031 | NetSocketState *s; |
| 4032 | int fd; |
| 4033 | struct sockaddr_in saddr; |
| 4034 | |
| 4035 | if (parse_host_port(&saddr, host_str) < 0) |
| 4036 | return -1; |
| 4037 | |
| 4038 | |
| 4039 | fd = net_socket_mcast_create(&saddr); |
| 4040 | if (fd < 0) |
| 4041 | return -1; |
| 4042 | |
| 4043 | s = net_socket_fd_init(vlan, fd, 0); |
| 4044 | if (!s) |
| 4045 | return -1; |
| 4046 | |
| 4047 | s->dgram_dst = saddr; |
| 4048 | |
| 4049 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 4050 | "socket: mcast=%s:%d", |
| 4051 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 4052 | return 0; |
| 4053 | |
| 4054 | } |
| 4055 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4056 | static int get_param_value(char *buf, int buf_size, |
| 4057 | const char *tag, const char *str) |
| 4058 | { |
| 4059 | const char *p; |
| 4060 | char *q; |
| 4061 | char option[128]; |
| 4062 | |
| 4063 | p = str; |
| 4064 | for(;;) { |
| 4065 | q = option; |
| 4066 | while (*p != '\0' && *p != '=') { |
| 4067 | if ((q - option) < sizeof(option) - 1) |
| 4068 | *q++ = *p; |
| 4069 | p++; |
| 4070 | } |
| 4071 | *q = '\0'; |
| 4072 | if (*p != '=') |
| 4073 | break; |
| 4074 | p++; |
| 4075 | if (!strcmp(tag, option)) { |
| 4076 | q = buf; |
| 4077 | while (*p != '\0' && *p != ',') { |
| 4078 | if ((q - buf) < buf_size - 1) |
| 4079 | *q++ = *p; |
| 4080 | p++; |
| 4081 | } |
| 4082 | *q = '\0'; |
| 4083 | return q - buf; |
| 4084 | } else { |
| 4085 | while (*p != '\0' && *p != ',') { |
| 4086 | p++; |
| 4087 | } |
| 4088 | } |
| 4089 | if (*p != ',') |
| 4090 | break; |
| 4091 | p++; |
| 4092 | } |
| 4093 | return 0; |
| 4094 | } |
| 4095 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 4096 | static int net_client_init(const char *str) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4097 | { |
| 4098 | const char *p; |
| 4099 | char *q; |
| 4100 | char device[64]; |
| 4101 | char buf[1024]; |
| 4102 | int vlan_id, ret; |
| 4103 | VLANState *vlan; |
| 4104 | |
| 4105 | p = str; |
| 4106 | q = device; |
| 4107 | while (*p != '\0' && *p != ',') { |
| 4108 | if ((q - device) < sizeof(device) - 1) |
| 4109 | *q++ = *p; |
| 4110 | p++; |
| 4111 | } |
| 4112 | *q = '\0'; |
| 4113 | if (*p == ',') |
| 4114 | p++; |
| 4115 | vlan_id = 0; |
| 4116 | if (get_param_value(buf, sizeof(buf), "vlan", p)) { |
| 4117 | vlan_id = strtol(buf, NULL, 0); |
| 4118 | } |
| 4119 | vlan = qemu_find_vlan(vlan_id); |
| 4120 | if (!vlan) { |
| 4121 | fprintf(stderr, "Could not create vlan %d\n", vlan_id); |
| 4122 | return -1; |
| 4123 | } |
| 4124 | if (!strcmp(device, "nic")) { |
| 4125 | NICInfo *nd; |
| 4126 | uint8_t *macaddr; |
| 4127 | |
| 4128 | if (nb_nics >= MAX_NICS) { |
| 4129 | fprintf(stderr, "Too Many NICs\n"); |
| 4130 | return -1; |
| 4131 | } |
| 4132 | nd = &nd_table[nb_nics]; |
| 4133 | macaddr = nd->macaddr; |
| 4134 | macaddr[0] = 0x52; |
| 4135 | macaddr[1] = 0x54; |
| 4136 | macaddr[2] = 0x00; |
| 4137 | macaddr[3] = 0x12; |
| 4138 | macaddr[4] = 0x34; |
| 4139 | macaddr[5] = 0x56 + nb_nics; |
| 4140 | |
| 4141 | if (get_param_value(buf, sizeof(buf), "macaddr", p)) { |
| 4142 | if (parse_macaddr(macaddr, buf) < 0) { |
| 4143 | fprintf(stderr, "invalid syntax for ethernet address\n"); |
| 4144 | return -1; |
| 4145 | } |
| 4146 | } |
pbrook | a41b2ff | 2006-02-05 04:14:41 +0000 | [diff] [blame] | 4147 | if (get_param_value(buf, sizeof(buf), "model", p)) { |
| 4148 | nd->model = strdup(buf); |
| 4149 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4150 | nd->vlan = vlan; |
| 4151 | nb_nics++; |
| 4152 | ret = 0; |
| 4153 | } else |
| 4154 | if (!strcmp(device, "none")) { |
| 4155 | /* does nothing. It is needed to signal that no network cards |
| 4156 | are wanted */ |
| 4157 | ret = 0; |
| 4158 | } else |
| 4159 | #ifdef CONFIG_SLIRP |
| 4160 | if (!strcmp(device, "user")) { |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 4161 | if (get_param_value(buf, sizeof(buf), "hostname", p)) { |
bellard | 3f423c9 | 2006-04-30 21:34:15 +0000 | [diff] [blame] | 4162 | pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf); |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 4163 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4164 | ret = net_slirp_init(vlan); |
| 4165 | } else |
| 4166 | #endif |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 4167 | #ifdef _WIN32 |
| 4168 | if (!strcmp(device, "tap")) { |
| 4169 | char ifname[64]; |
| 4170 | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
| 4171 | fprintf(stderr, "tap: no interface name\n"); |
| 4172 | return -1; |
| 4173 | } |
| 4174 | ret = tap_win32_init(vlan, ifname); |
| 4175 | } else |
| 4176 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4177 | if (!strcmp(device, "tap")) { |
| 4178 | char ifname[64]; |
| 4179 | char setup_script[1024]; |
| 4180 | int fd; |
| 4181 | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
| 4182 | fd = strtol(buf, NULL, 0); |
| 4183 | ret = -1; |
| 4184 | if (net_tap_fd_init(vlan, fd)) |
| 4185 | ret = 0; |
| 4186 | } else { |
bellard | bf8c534 | 2007-01-09 19:44:41 +0000 | [diff] [blame] | 4187 | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
| 4188 | ifname[0] = '\0'; |
| 4189 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4190 | if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) { |
| 4191 | pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT); |
| 4192 | } |
| 4193 | ret = net_tap_init(vlan, ifname, setup_script); |
| 4194 | } |
| 4195 | } else |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4196 | #endif |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4197 | if (!strcmp(device, "socket")) { |
| 4198 | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
| 4199 | int fd; |
| 4200 | fd = strtol(buf, NULL, 0); |
| 4201 | ret = -1; |
| 4202 | if (net_socket_fd_init(vlan, fd, 1)) |
| 4203 | ret = 0; |
| 4204 | } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) { |
| 4205 | ret = net_socket_listen_init(vlan, buf); |
| 4206 | } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { |
| 4207 | ret = net_socket_connect_init(vlan, buf); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 4208 | } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { |
| 4209 | ret = net_socket_mcast_init(vlan, buf); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4210 | } else { |
| 4211 | fprintf(stderr, "Unknown socket options: %s\n", p); |
| 4212 | return -1; |
| 4213 | } |
| 4214 | } else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4215 | { |
| 4216 | fprintf(stderr, "Unknown network device: %s\n", device); |
| 4217 | return -1; |
| 4218 | } |
| 4219 | if (ret < 0) { |
| 4220 | fprintf(stderr, "Could not initialize device '%s'\n", device); |
| 4221 | } |
| 4222 | |
| 4223 | return ret; |
| 4224 | } |
| 4225 | |
| 4226 | void do_info_network(void) |
| 4227 | { |
| 4228 | VLANState *vlan; |
| 4229 | VLANClientState *vc; |
| 4230 | |
| 4231 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
| 4232 | term_printf("VLAN %d devices:\n", vlan->id); |
| 4233 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) |
| 4234 | term_printf(" %s\n", vc->info_str); |
| 4235 | } |
| 4236 | } |
ths | 42550fd | 2006-12-22 16:34:12 +0000 | [diff] [blame] | 4237 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 4238 | /***********************************************************/ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4239 | /* USB devices */ |
| 4240 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4241 | static USBPort *used_usb_ports; |
| 4242 | static USBPort *free_usb_ports; |
| 4243 | |
| 4244 | /* ??? Maybe change this to register a hub to keep track of the topology. */ |
| 4245 | void qemu_register_usb_port(USBPort *port, void *opaque, int index, |
| 4246 | usb_attachfn attach) |
| 4247 | { |
| 4248 | port->opaque = opaque; |
| 4249 | port->index = index; |
| 4250 | port->attach = attach; |
| 4251 | port->next = free_usb_ports; |
| 4252 | free_usb_ports = port; |
| 4253 | } |
| 4254 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4255 | static int usb_device_add(const char *devname) |
| 4256 | { |
| 4257 | const char *p; |
| 4258 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4259 | USBPort *port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4260 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4261 | if (!free_usb_ports) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4262 | return -1; |
| 4263 | |
| 4264 | if (strstart(devname, "host:", &p)) { |
| 4265 | dev = usb_host_device_open(p); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4266 | } else if (!strcmp(devname, "mouse")) { |
| 4267 | dev = usb_mouse_init(); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 4268 | } else if (!strcmp(devname, "tablet")) { |
| 4269 | dev = usb_tablet_init(); |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 4270 | } else if (strstart(devname, "disk:", &p)) { |
| 4271 | dev = usb_msd_init(p); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4272 | } else { |
| 4273 | return -1; |
| 4274 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4275 | if (!dev) |
| 4276 | return -1; |
| 4277 | |
| 4278 | /* Find a USB port to add the device to. */ |
| 4279 | port = free_usb_ports; |
| 4280 | if (!port->next) { |
| 4281 | USBDevice *hub; |
| 4282 | |
| 4283 | /* Create a new hub and chain it on. */ |
| 4284 | free_usb_ports = NULL; |
| 4285 | port->next = used_usb_ports; |
| 4286 | used_usb_ports = port; |
| 4287 | |
| 4288 | hub = usb_hub_init(VM_USB_HUB_SIZE); |
| 4289 | usb_attach(port, hub); |
| 4290 | port = free_usb_ports; |
| 4291 | } |
| 4292 | |
| 4293 | free_usb_ports = port->next; |
| 4294 | port->next = used_usb_ports; |
| 4295 | used_usb_ports = port; |
| 4296 | usb_attach(port, dev); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4297 | return 0; |
| 4298 | } |
| 4299 | |
| 4300 | static int usb_device_del(const char *devname) |
| 4301 | { |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4302 | USBPort *port; |
| 4303 | USBPort **lastp; |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 4304 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4305 | int bus_num, addr; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4306 | const char *p; |
| 4307 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4308 | if (!used_usb_ports) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4309 | return -1; |
| 4310 | |
| 4311 | p = strchr(devname, '.'); |
| 4312 | if (!p) |
| 4313 | return -1; |
| 4314 | bus_num = strtoul(devname, NULL, 0); |
| 4315 | addr = strtoul(p + 1, NULL, 0); |
| 4316 | if (bus_num != 0) |
| 4317 | return -1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4318 | |
| 4319 | lastp = &used_usb_ports; |
| 4320 | port = used_usb_ports; |
| 4321 | while (port && port->dev->addr != addr) { |
| 4322 | lastp = &port->next; |
| 4323 | port = port->next; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4324 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4325 | |
| 4326 | if (!port) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4327 | return -1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4328 | |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 4329 | dev = port->dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4330 | *lastp = port->next; |
| 4331 | usb_attach(port, NULL); |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 4332 | dev->handle_destroy(dev); |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4333 | port->next = free_usb_ports; |
| 4334 | free_usb_ports = port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4335 | return 0; |
| 4336 | } |
| 4337 | |
| 4338 | void do_usb_add(const char *devname) |
| 4339 | { |
| 4340 | int ret; |
| 4341 | ret = usb_device_add(devname); |
| 4342 | if (ret < 0) |
| 4343 | term_printf("Could not add USB device '%s'\n", devname); |
| 4344 | } |
| 4345 | |
| 4346 | void do_usb_del(const char *devname) |
| 4347 | { |
| 4348 | int ret; |
| 4349 | ret = usb_device_del(devname); |
| 4350 | if (ret < 0) |
| 4351 | term_printf("Could not remove USB device '%s'\n", devname); |
| 4352 | } |
| 4353 | |
| 4354 | void usb_info(void) |
| 4355 | { |
| 4356 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4357 | USBPort *port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4358 | const char *speed_str; |
| 4359 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4360 | if (!usb_enabled) { |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4361 | term_printf("USB support not enabled\n"); |
| 4362 | return; |
| 4363 | } |
| 4364 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4365 | for (port = used_usb_ports; port; port = port->next) { |
| 4366 | dev = port->dev; |
| 4367 | if (!dev) |
| 4368 | continue; |
| 4369 | switch(dev->speed) { |
| 4370 | case USB_SPEED_LOW: |
| 4371 | speed_str = "1.5"; |
| 4372 | break; |
| 4373 | case USB_SPEED_FULL: |
| 4374 | speed_str = "12"; |
| 4375 | break; |
| 4376 | case USB_SPEED_HIGH: |
| 4377 | speed_str = "480"; |
| 4378 | break; |
| 4379 | default: |
| 4380 | speed_str = "?"; |
| 4381 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4382 | } |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 4383 | term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n", |
| 4384 | 0, dev->addr, speed_str, dev->devname); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4385 | } |
| 4386 | } |
| 4387 | |
| 4388 | /***********************************************************/ |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 4389 | /* pid file */ |
| 4390 | |
| 4391 | static char *pid_filename; |
| 4392 | |
| 4393 | /* Remove PID file. Called on normal exit */ |
| 4394 | |
| 4395 | static void remove_pidfile(void) |
| 4396 | { |
| 4397 | unlink (pid_filename); |
| 4398 | } |
| 4399 | |
| 4400 | static void create_pidfile(const char *filename) |
| 4401 | { |
| 4402 | struct stat pidstat; |
| 4403 | FILE *f; |
| 4404 | |
| 4405 | /* Try to write our PID to the named file */ |
| 4406 | if (stat(filename, &pidstat) < 0) { |
| 4407 | if (errno == ENOENT) { |
| 4408 | if ((f = fopen (filename, "w")) == NULL) { |
| 4409 | perror("Opening pidfile"); |
| 4410 | exit(1); |
| 4411 | } |
| 4412 | fprintf(f, "%d\n", getpid()); |
| 4413 | fclose(f); |
| 4414 | pid_filename = qemu_strdup(filename); |
| 4415 | if (!pid_filename) { |
| 4416 | fprintf(stderr, "Could not save PID filename"); |
| 4417 | exit(1); |
| 4418 | } |
| 4419 | atexit(remove_pidfile); |
| 4420 | } |
| 4421 | } else { |
| 4422 | fprintf(stderr, "%s already exists. Remove it and try again.\n", |
| 4423 | filename); |
| 4424 | exit(1); |
| 4425 | } |
| 4426 | } |
| 4427 | |
| 4428 | /***********************************************************/ |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4429 | /* dumb display */ |
| 4430 | |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4431 | static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
| 4432 | { |
| 4433 | } |
| 4434 | |
| 4435 | static void dumb_resize(DisplayState *ds, int w, int h) |
| 4436 | { |
| 4437 | } |
| 4438 | |
| 4439 | static void dumb_refresh(DisplayState *ds) |
| 4440 | { |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 4441 | vga_hw_update(); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | void dumb_display_init(DisplayState *ds) |
| 4445 | { |
| 4446 | ds->data = NULL; |
| 4447 | ds->linesize = 0; |
| 4448 | ds->depth = 0; |
| 4449 | ds->dpy_update = dumb_update; |
| 4450 | ds->dpy_resize = dumb_resize; |
| 4451 | ds->dpy_refresh = dumb_refresh; |
| 4452 | } |
| 4453 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4454 | /***********************************************************/ |
| 4455 | /* I/O handling */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4456 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4457 | #define MAX_IO_HANDLERS 64 |
| 4458 | |
| 4459 | typedef struct IOHandlerRecord { |
| 4460 | int fd; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4461 | IOCanRWHandler *fd_read_poll; |
| 4462 | IOHandler *fd_read; |
| 4463 | IOHandler *fd_write; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4464 | void *opaque; |
| 4465 | /* temporary data */ |
| 4466 | struct pollfd *ufd; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4467 | struct IOHandlerRecord *next; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4468 | } IOHandlerRecord; |
| 4469 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4470 | static IOHandlerRecord *first_io_handler; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4471 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4472 | /* XXX: fd_read_poll should be suppressed, but an API change is |
| 4473 | necessary in the character devices to suppress fd_can_read(). */ |
| 4474 | int qemu_set_fd_handler2(int fd, |
| 4475 | IOCanRWHandler *fd_read_poll, |
| 4476 | IOHandler *fd_read, |
| 4477 | IOHandler *fd_write, |
| 4478 | void *opaque) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4479 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4480 | IOHandlerRecord **pioh, *ioh; |
| 4481 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4482 | if (!fd_read && !fd_write) { |
| 4483 | pioh = &first_io_handler; |
| 4484 | for(;;) { |
| 4485 | ioh = *pioh; |
| 4486 | if (ioh == NULL) |
| 4487 | break; |
| 4488 | if (ioh->fd == fd) { |
| 4489 | *pioh = ioh->next; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4490 | qemu_free(ioh); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4491 | break; |
| 4492 | } |
| 4493 | pioh = &ioh->next; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4494 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4495 | } else { |
| 4496 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 4497 | if (ioh->fd == fd) |
| 4498 | goto found; |
| 4499 | } |
| 4500 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); |
| 4501 | if (!ioh) |
| 4502 | return -1; |
| 4503 | ioh->next = first_io_handler; |
| 4504 | first_io_handler = ioh; |
| 4505 | found: |
| 4506 | ioh->fd = fd; |
| 4507 | ioh->fd_read_poll = fd_read_poll; |
| 4508 | ioh->fd_read = fd_read; |
| 4509 | ioh->fd_write = fd_write; |
| 4510 | ioh->opaque = opaque; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4511 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4512 | return 0; |
| 4513 | } |
| 4514 | |
| 4515 | int qemu_set_fd_handler(int fd, |
| 4516 | IOHandler *fd_read, |
| 4517 | IOHandler *fd_write, |
| 4518 | void *opaque) |
| 4519 | { |
| 4520 | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4521 | } |
| 4522 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4523 | /***********************************************************/ |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 4524 | /* Polling handling */ |
| 4525 | |
| 4526 | typedef struct PollingEntry { |
| 4527 | PollingFunc *func; |
| 4528 | void *opaque; |
| 4529 | struct PollingEntry *next; |
| 4530 | } PollingEntry; |
| 4531 | |
| 4532 | static PollingEntry *first_polling_entry; |
| 4533 | |
| 4534 | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
| 4535 | { |
| 4536 | PollingEntry **ppe, *pe; |
| 4537 | pe = qemu_mallocz(sizeof(PollingEntry)); |
| 4538 | if (!pe) |
| 4539 | return -1; |
| 4540 | pe->func = func; |
| 4541 | pe->opaque = opaque; |
| 4542 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
| 4543 | *ppe = pe; |
| 4544 | return 0; |
| 4545 | } |
| 4546 | |
| 4547 | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
| 4548 | { |
| 4549 | PollingEntry **ppe, *pe; |
| 4550 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
| 4551 | pe = *ppe; |
| 4552 | if (pe->func == func && pe->opaque == opaque) { |
| 4553 | *ppe = pe->next; |
| 4554 | qemu_free(pe); |
| 4555 | break; |
| 4556 | } |
| 4557 | } |
| 4558 | } |
| 4559 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 4560 | #ifdef _WIN32 |
| 4561 | /***********************************************************/ |
| 4562 | /* Wait objects support */ |
| 4563 | typedef struct WaitObjects { |
| 4564 | int num; |
| 4565 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; |
| 4566 | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1]; |
| 4567 | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
| 4568 | } WaitObjects; |
| 4569 | |
| 4570 | static WaitObjects wait_objects = {0}; |
| 4571 | |
| 4572 | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 4573 | { |
| 4574 | WaitObjects *w = &wait_objects; |
| 4575 | |
| 4576 | if (w->num >= MAXIMUM_WAIT_OBJECTS) |
| 4577 | return -1; |
| 4578 | w->events[w->num] = handle; |
| 4579 | w->func[w->num] = func; |
| 4580 | w->opaque[w->num] = opaque; |
| 4581 | w->num++; |
| 4582 | return 0; |
| 4583 | } |
| 4584 | |
| 4585 | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 4586 | { |
| 4587 | int i, found; |
| 4588 | WaitObjects *w = &wait_objects; |
| 4589 | |
| 4590 | found = 0; |
| 4591 | for (i = 0; i < w->num; i++) { |
| 4592 | if (w->events[i] == handle) |
| 4593 | found = 1; |
| 4594 | if (found) { |
| 4595 | w->events[i] = w->events[i + 1]; |
| 4596 | w->func[i] = w->func[i + 1]; |
| 4597 | w->opaque[i] = w->opaque[i + 1]; |
| 4598 | } |
| 4599 | } |
| 4600 | if (found) |
| 4601 | w->num--; |
| 4602 | } |
| 4603 | #endif |
| 4604 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 4605 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4606 | /* savevm/loadvm support */ |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4607 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4608 | #define IO_BUF_SIZE 32768 |
| 4609 | |
| 4610 | struct QEMUFile { |
| 4611 | FILE *outfile; |
| 4612 | BlockDriverState *bs; |
| 4613 | int is_file; |
| 4614 | int is_writable; |
| 4615 | int64_t base_offset; |
| 4616 | int64_t buf_offset; /* start of buffer when writing, end of buffer |
| 4617 | when reading */ |
| 4618 | int buf_index; |
| 4619 | int buf_size; /* 0 when writing */ |
| 4620 | uint8_t buf[IO_BUF_SIZE]; |
| 4621 | }; |
| 4622 | |
| 4623 | QEMUFile *qemu_fopen(const char *filename, const char *mode) |
| 4624 | { |
| 4625 | QEMUFile *f; |
| 4626 | |
| 4627 | f = qemu_mallocz(sizeof(QEMUFile)); |
| 4628 | if (!f) |
| 4629 | return NULL; |
| 4630 | if (!strcmp(mode, "wb")) { |
| 4631 | f->is_writable = 1; |
| 4632 | } else if (!strcmp(mode, "rb")) { |
| 4633 | f->is_writable = 0; |
| 4634 | } else { |
| 4635 | goto fail; |
| 4636 | } |
| 4637 | f->outfile = fopen(filename, mode); |
| 4638 | if (!f->outfile) |
| 4639 | goto fail; |
| 4640 | f->is_file = 1; |
| 4641 | return f; |
| 4642 | fail: |
| 4643 | if (f->outfile) |
| 4644 | fclose(f->outfile); |
| 4645 | qemu_free(f); |
| 4646 | return NULL; |
| 4647 | } |
| 4648 | |
| 4649 | QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable) |
| 4650 | { |
| 4651 | QEMUFile *f; |
| 4652 | |
| 4653 | f = qemu_mallocz(sizeof(QEMUFile)); |
| 4654 | if (!f) |
| 4655 | return NULL; |
| 4656 | f->is_file = 0; |
| 4657 | f->bs = bs; |
| 4658 | f->is_writable = is_writable; |
| 4659 | f->base_offset = offset; |
| 4660 | return f; |
| 4661 | } |
| 4662 | |
| 4663 | void qemu_fflush(QEMUFile *f) |
| 4664 | { |
| 4665 | if (!f->is_writable) |
| 4666 | return; |
| 4667 | if (f->buf_index > 0) { |
| 4668 | if (f->is_file) { |
| 4669 | fseek(f->outfile, f->buf_offset, SEEK_SET); |
| 4670 | fwrite(f->buf, 1, f->buf_index, f->outfile); |
| 4671 | } else { |
| 4672 | bdrv_pwrite(f->bs, f->base_offset + f->buf_offset, |
| 4673 | f->buf, f->buf_index); |
| 4674 | } |
| 4675 | f->buf_offset += f->buf_index; |
| 4676 | f->buf_index = 0; |
| 4677 | } |
| 4678 | } |
| 4679 | |
| 4680 | static void qemu_fill_buffer(QEMUFile *f) |
| 4681 | { |
| 4682 | int len; |
| 4683 | |
| 4684 | if (f->is_writable) |
| 4685 | return; |
| 4686 | if (f->is_file) { |
| 4687 | fseek(f->outfile, f->buf_offset, SEEK_SET); |
| 4688 | len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile); |
| 4689 | if (len < 0) |
| 4690 | len = 0; |
| 4691 | } else { |
| 4692 | len = bdrv_pread(f->bs, f->base_offset + f->buf_offset, |
| 4693 | f->buf, IO_BUF_SIZE); |
| 4694 | if (len < 0) |
| 4695 | len = 0; |
| 4696 | } |
| 4697 | f->buf_index = 0; |
| 4698 | f->buf_size = len; |
| 4699 | f->buf_offset += len; |
| 4700 | } |
| 4701 | |
| 4702 | void qemu_fclose(QEMUFile *f) |
| 4703 | { |
| 4704 | if (f->is_writable) |
| 4705 | qemu_fflush(f); |
| 4706 | if (f->is_file) { |
| 4707 | fclose(f->outfile); |
| 4708 | } |
| 4709 | qemu_free(f); |
| 4710 | } |
| 4711 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4712 | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
| 4713 | { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4714 | int l; |
| 4715 | while (size > 0) { |
| 4716 | l = IO_BUF_SIZE - f->buf_index; |
| 4717 | if (l > size) |
| 4718 | l = size; |
| 4719 | memcpy(f->buf + f->buf_index, buf, l); |
| 4720 | f->buf_index += l; |
| 4721 | buf += l; |
| 4722 | size -= l; |
| 4723 | if (f->buf_index >= IO_BUF_SIZE) |
| 4724 | qemu_fflush(f); |
| 4725 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4726 | } |
| 4727 | |
| 4728 | void qemu_put_byte(QEMUFile *f, int v) |
| 4729 | { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4730 | f->buf[f->buf_index++] = v; |
| 4731 | if (f->buf_index >= IO_BUF_SIZE) |
| 4732 | qemu_fflush(f); |
| 4733 | } |
| 4734 | |
| 4735 | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1) |
| 4736 | { |
| 4737 | int size, l; |
| 4738 | |
| 4739 | size = size1; |
| 4740 | while (size > 0) { |
| 4741 | l = f->buf_size - f->buf_index; |
| 4742 | if (l == 0) { |
| 4743 | qemu_fill_buffer(f); |
| 4744 | l = f->buf_size - f->buf_index; |
| 4745 | if (l == 0) |
| 4746 | break; |
| 4747 | } |
| 4748 | if (l > size) |
| 4749 | l = size; |
| 4750 | memcpy(buf, f->buf + f->buf_index, l); |
| 4751 | f->buf_index += l; |
| 4752 | buf += l; |
| 4753 | size -= l; |
| 4754 | } |
| 4755 | return size1 - size; |
| 4756 | } |
| 4757 | |
| 4758 | int qemu_get_byte(QEMUFile *f) |
| 4759 | { |
| 4760 | if (f->buf_index >= f->buf_size) { |
| 4761 | qemu_fill_buffer(f); |
| 4762 | if (f->buf_index >= f->buf_size) |
| 4763 | return 0; |
| 4764 | } |
| 4765 | return f->buf[f->buf_index++]; |
| 4766 | } |
| 4767 | |
| 4768 | int64_t qemu_ftell(QEMUFile *f) |
| 4769 | { |
| 4770 | return f->buf_offset - f->buf_size + f->buf_index; |
| 4771 | } |
| 4772 | |
| 4773 | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence) |
| 4774 | { |
| 4775 | if (whence == SEEK_SET) { |
| 4776 | /* nothing to do */ |
| 4777 | } else if (whence == SEEK_CUR) { |
| 4778 | pos += qemu_ftell(f); |
| 4779 | } else { |
| 4780 | /* SEEK_END not supported */ |
| 4781 | return -1; |
| 4782 | } |
| 4783 | if (f->is_writable) { |
| 4784 | qemu_fflush(f); |
| 4785 | f->buf_offset = pos; |
| 4786 | } else { |
| 4787 | f->buf_offset = pos; |
| 4788 | f->buf_index = 0; |
| 4789 | f->buf_size = 0; |
| 4790 | } |
| 4791 | return pos; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
| 4794 | void qemu_put_be16(QEMUFile *f, unsigned int v) |
| 4795 | { |
| 4796 | qemu_put_byte(f, v >> 8); |
| 4797 | qemu_put_byte(f, v); |
| 4798 | } |
| 4799 | |
| 4800 | void qemu_put_be32(QEMUFile *f, unsigned int v) |
| 4801 | { |
| 4802 | qemu_put_byte(f, v >> 24); |
| 4803 | qemu_put_byte(f, v >> 16); |
| 4804 | qemu_put_byte(f, v >> 8); |
| 4805 | qemu_put_byte(f, v); |
| 4806 | } |
| 4807 | |
| 4808 | void qemu_put_be64(QEMUFile *f, uint64_t v) |
| 4809 | { |
| 4810 | qemu_put_be32(f, v >> 32); |
| 4811 | qemu_put_be32(f, v); |
| 4812 | } |
| 4813 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4814 | unsigned int qemu_get_be16(QEMUFile *f) |
| 4815 | { |
| 4816 | unsigned int v; |
| 4817 | v = qemu_get_byte(f) << 8; |
| 4818 | v |= qemu_get_byte(f); |
| 4819 | return v; |
| 4820 | } |
| 4821 | |
| 4822 | unsigned int qemu_get_be32(QEMUFile *f) |
| 4823 | { |
| 4824 | unsigned int v; |
| 4825 | v = qemu_get_byte(f) << 24; |
| 4826 | v |= qemu_get_byte(f) << 16; |
| 4827 | v |= qemu_get_byte(f) << 8; |
| 4828 | v |= qemu_get_byte(f); |
| 4829 | return v; |
| 4830 | } |
| 4831 | |
| 4832 | uint64_t qemu_get_be64(QEMUFile *f) |
| 4833 | { |
| 4834 | uint64_t v; |
| 4835 | v = (uint64_t)qemu_get_be32(f) << 32; |
| 4836 | v |= qemu_get_be32(f); |
| 4837 | return v; |
| 4838 | } |
| 4839 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4840 | typedef struct SaveStateEntry { |
| 4841 | char idstr[256]; |
| 4842 | int instance_id; |
| 4843 | int version_id; |
| 4844 | SaveStateHandler *save_state; |
| 4845 | LoadStateHandler *load_state; |
| 4846 | void *opaque; |
| 4847 | struct SaveStateEntry *next; |
| 4848 | } SaveStateEntry; |
| 4849 | |
| 4850 | static SaveStateEntry *first_se; |
| 4851 | |
| 4852 | int register_savevm(const char *idstr, |
| 4853 | int instance_id, |
| 4854 | int version_id, |
| 4855 | SaveStateHandler *save_state, |
| 4856 | LoadStateHandler *load_state, |
| 4857 | void *opaque) |
| 4858 | { |
| 4859 | SaveStateEntry *se, **pse; |
| 4860 | |
| 4861 | se = qemu_malloc(sizeof(SaveStateEntry)); |
| 4862 | if (!se) |
| 4863 | return -1; |
| 4864 | pstrcpy(se->idstr, sizeof(se->idstr), idstr); |
| 4865 | se->instance_id = instance_id; |
| 4866 | se->version_id = version_id; |
| 4867 | se->save_state = save_state; |
| 4868 | se->load_state = load_state; |
| 4869 | se->opaque = opaque; |
| 4870 | se->next = NULL; |
| 4871 | |
| 4872 | /* add at the end of list */ |
| 4873 | pse = &first_se; |
| 4874 | while (*pse != NULL) |
| 4875 | pse = &(*pse)->next; |
| 4876 | *pse = se; |
| 4877 | return 0; |
| 4878 | } |
| 4879 | |
| 4880 | #define QEMU_VM_FILE_MAGIC 0x5145564d |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4881 | #define QEMU_VM_FILE_VERSION 0x00000002 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4882 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4883 | int qemu_savevm_state(QEMUFile *f) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4884 | { |
| 4885 | SaveStateEntry *se; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4886 | int len, ret; |
| 4887 | int64_t cur_pos, len_pos, total_len_pos; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4888 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4889 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
| 4890 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4891 | total_len_pos = qemu_ftell(f); |
| 4892 | qemu_put_be64(f, 0); /* total size */ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4893 | |
| 4894 | for(se = first_se; se != NULL; se = se->next) { |
| 4895 | /* ID string */ |
| 4896 | len = strlen(se->idstr); |
| 4897 | qemu_put_byte(f, len); |
| 4898 | qemu_put_buffer(f, se->idstr, len); |
| 4899 | |
| 4900 | qemu_put_be32(f, se->instance_id); |
| 4901 | qemu_put_be32(f, se->version_id); |
| 4902 | |
| 4903 | /* record size: filled later */ |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4904 | len_pos = qemu_ftell(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4905 | qemu_put_be32(f, 0); |
| 4906 | |
| 4907 | se->save_state(f, se->opaque); |
| 4908 | |
| 4909 | /* fill record size */ |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4910 | cur_pos = qemu_ftell(f); |
| 4911 | len = cur_pos - len_pos - 4; |
| 4912 | qemu_fseek(f, len_pos, SEEK_SET); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4913 | qemu_put_be32(f, len); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4914 | qemu_fseek(f, cur_pos, SEEK_SET); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4915 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4916 | cur_pos = qemu_ftell(f); |
| 4917 | qemu_fseek(f, total_len_pos, SEEK_SET); |
| 4918 | qemu_put_be64(f, cur_pos - total_len_pos - 8); |
| 4919 | qemu_fseek(f, cur_pos, SEEK_SET); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4920 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4921 | ret = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4922 | return ret; |
| 4923 | } |
| 4924 | |
| 4925 | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
| 4926 | { |
| 4927 | SaveStateEntry *se; |
| 4928 | |
| 4929 | for(se = first_se; se != NULL; se = se->next) { |
| 4930 | if (!strcmp(se->idstr, idstr) && |
| 4931 | instance_id == se->instance_id) |
| 4932 | return se; |
| 4933 | } |
| 4934 | return NULL; |
| 4935 | } |
| 4936 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4937 | int qemu_loadvm_state(QEMUFile *f) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4938 | { |
| 4939 | SaveStateEntry *se; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4940 | int len, ret, instance_id, record_len, version_id; |
| 4941 | int64_t total_len, end_pos, cur_pos; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4942 | unsigned int v; |
| 4943 | char idstr[256]; |
| 4944 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4945 | v = qemu_get_be32(f); |
| 4946 | if (v != QEMU_VM_FILE_MAGIC) |
| 4947 | goto fail; |
| 4948 | v = qemu_get_be32(f); |
| 4949 | if (v != QEMU_VM_FILE_VERSION) { |
| 4950 | fail: |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4951 | ret = -1; |
| 4952 | goto the_end; |
| 4953 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4954 | total_len = qemu_get_be64(f); |
| 4955 | end_pos = total_len + qemu_ftell(f); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4956 | for(;;) { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4957 | if (qemu_ftell(f) >= end_pos) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4958 | break; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4959 | len = qemu_get_byte(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4960 | qemu_get_buffer(f, idstr, len); |
| 4961 | idstr[len] = '\0'; |
| 4962 | instance_id = qemu_get_be32(f); |
| 4963 | version_id = qemu_get_be32(f); |
| 4964 | record_len = qemu_get_be32(f); |
| 4965 | #if 0 |
| 4966 | printf("idstr=%s instance=0x%x version=%d len=%d\n", |
| 4967 | idstr, instance_id, version_id, record_len); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 4968 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4969 | cur_pos = qemu_ftell(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4970 | se = find_se(idstr, instance_id); |
| 4971 | if (!se) { |
| 4972 | fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n", |
| 4973 | instance_id, idstr); |
| 4974 | } else { |
| 4975 | ret = se->load_state(f, se->opaque, version_id); |
| 4976 | if (ret < 0) { |
| 4977 | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n", |
| 4978 | instance_id, idstr); |
| 4979 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 4980 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4981 | /* always seek to exact end of record */ |
| 4982 | qemu_fseek(f, cur_pos + record_len, SEEK_SET); |
| 4983 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4984 | ret = 0; |
| 4985 | the_end: |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4986 | return ret; |
| 4987 | } |
| 4988 | |
| 4989 | /* device can contain snapshots */ |
| 4990 | static int bdrv_can_snapshot(BlockDriverState *bs) |
| 4991 | { |
| 4992 | return (bs && |
| 4993 | !bdrv_is_removable(bs) && |
| 4994 | !bdrv_is_read_only(bs)); |
| 4995 | } |
| 4996 | |
| 4997 | /* device must be snapshots in order to have a reliable snapshot */ |
| 4998 | static int bdrv_has_snapshot(BlockDriverState *bs) |
| 4999 | { |
| 5000 | return (bs && |
| 5001 | !bdrv_is_removable(bs) && |
| 5002 | !bdrv_is_read_only(bs)); |
| 5003 | } |
| 5004 | |
| 5005 | static BlockDriverState *get_bs_snapshots(void) |
| 5006 | { |
| 5007 | BlockDriverState *bs; |
| 5008 | int i; |
| 5009 | |
| 5010 | if (bs_snapshots) |
| 5011 | return bs_snapshots; |
| 5012 | for(i = 0; i <= MAX_DISKS; i++) { |
| 5013 | bs = bs_table[i]; |
| 5014 | if (bdrv_can_snapshot(bs)) |
| 5015 | goto ok; |
| 5016 | } |
| 5017 | return NULL; |
| 5018 | ok: |
| 5019 | bs_snapshots = bs; |
| 5020 | return bs; |
| 5021 | } |
| 5022 | |
| 5023 | static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
| 5024 | const char *name) |
| 5025 | { |
| 5026 | QEMUSnapshotInfo *sn_tab, *sn; |
| 5027 | int nb_sns, i, ret; |
| 5028 | |
| 5029 | ret = -ENOENT; |
| 5030 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 5031 | if (nb_sns < 0) |
| 5032 | return ret; |
| 5033 | for(i = 0; i < nb_sns; i++) { |
| 5034 | sn = &sn_tab[i]; |
| 5035 | if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) { |
| 5036 | *sn_info = *sn; |
| 5037 | ret = 0; |
| 5038 | break; |
| 5039 | } |
| 5040 | } |
| 5041 | qemu_free(sn_tab); |
| 5042 | return ret; |
| 5043 | } |
| 5044 | |
| 5045 | void do_savevm(const char *name) |
| 5046 | { |
| 5047 | BlockDriverState *bs, *bs1; |
| 5048 | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; |
| 5049 | int must_delete, ret, i; |
| 5050 | BlockDriverInfo bdi1, *bdi = &bdi1; |
| 5051 | QEMUFile *f; |
| 5052 | int saved_vm_running; |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 5053 | #ifdef _WIN32 |
| 5054 | struct _timeb tb; |
| 5055 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5056 | struct timeval tv; |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 5057 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5058 | |
| 5059 | bs = get_bs_snapshots(); |
| 5060 | if (!bs) { |
| 5061 | term_printf("No block device can accept snapshots\n"); |
| 5062 | return; |
| 5063 | } |
| 5064 | |
pbrook | 6192bc3 | 2006-09-03 12:08:37 +0000 | [diff] [blame] | 5065 | /* ??? Should this occur after vm_stop? */ |
| 5066 | qemu_aio_flush(); |
| 5067 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5068 | saved_vm_running = vm_running; |
| 5069 | vm_stop(0); |
| 5070 | |
| 5071 | must_delete = 0; |
| 5072 | if (name) { |
| 5073 | ret = bdrv_snapshot_find(bs, old_sn, name); |
| 5074 | if (ret >= 0) { |
| 5075 | must_delete = 1; |
| 5076 | } |
| 5077 | } |
| 5078 | memset(sn, 0, sizeof(*sn)); |
| 5079 | if (must_delete) { |
| 5080 | pstrcpy(sn->name, sizeof(sn->name), old_sn->name); |
| 5081 | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str); |
| 5082 | } else { |
| 5083 | if (name) |
| 5084 | pstrcpy(sn->name, sizeof(sn->name), name); |
| 5085 | } |
| 5086 | |
| 5087 | /* fill auxiliary fields */ |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 5088 | #ifdef _WIN32 |
| 5089 | _ftime(&tb); |
| 5090 | sn->date_sec = tb.time; |
| 5091 | sn->date_nsec = tb.millitm * 1000000; |
| 5092 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5093 | gettimeofday(&tv, NULL); |
| 5094 | sn->date_sec = tv.tv_sec; |
| 5095 | sn->date_nsec = tv.tv_usec * 1000; |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 5096 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5097 | sn->vm_clock_nsec = qemu_get_clock(vm_clock); |
| 5098 | |
| 5099 | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
| 5100 | term_printf("Device %s does not support VM state snapshots\n", |
| 5101 | bdrv_get_device_name(bs)); |
| 5102 | goto the_end; |
| 5103 | } |
| 5104 | |
| 5105 | /* save the VM state */ |
| 5106 | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1); |
| 5107 | if (!f) { |
| 5108 | term_printf("Could not open VM state file\n"); |
| 5109 | goto the_end; |
| 5110 | } |
| 5111 | ret = qemu_savevm_state(f); |
| 5112 | sn->vm_state_size = qemu_ftell(f); |
| 5113 | qemu_fclose(f); |
| 5114 | if (ret < 0) { |
| 5115 | term_printf("Error %d while writing VM\n", ret); |
| 5116 | goto the_end; |
| 5117 | } |
| 5118 | |
| 5119 | /* create the snapshots */ |
| 5120 | |
| 5121 | for(i = 0; i < MAX_DISKS; i++) { |
| 5122 | bs1 = bs_table[i]; |
| 5123 | if (bdrv_has_snapshot(bs1)) { |
| 5124 | if (must_delete) { |
| 5125 | ret = bdrv_snapshot_delete(bs1, old_sn->id_str); |
| 5126 | if (ret < 0) { |
| 5127 | term_printf("Error while deleting snapshot on '%s'\n", |
| 5128 | bdrv_get_device_name(bs1)); |
| 5129 | } |
| 5130 | } |
| 5131 | ret = bdrv_snapshot_create(bs1, sn); |
| 5132 | if (ret < 0) { |
| 5133 | term_printf("Error while creating snapshot on '%s'\n", |
| 5134 | bdrv_get_device_name(bs1)); |
| 5135 | } |
| 5136 | } |
| 5137 | } |
| 5138 | |
| 5139 | the_end: |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5140 | if (saved_vm_running) |
| 5141 | vm_start(); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5142 | } |
| 5143 | |
| 5144 | void do_loadvm(const char *name) |
| 5145 | { |
| 5146 | BlockDriverState *bs, *bs1; |
| 5147 | BlockDriverInfo bdi1, *bdi = &bdi1; |
| 5148 | QEMUFile *f; |
| 5149 | int i, ret; |
| 5150 | int saved_vm_running; |
| 5151 | |
| 5152 | bs = get_bs_snapshots(); |
| 5153 | if (!bs) { |
| 5154 | term_printf("No block device supports snapshots\n"); |
| 5155 | return; |
| 5156 | } |
| 5157 | |
pbrook | 6192bc3 | 2006-09-03 12:08:37 +0000 | [diff] [blame] | 5158 | /* Flush all IO requests so they don't interfere with the new state. */ |
| 5159 | qemu_aio_flush(); |
| 5160 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 5161 | saved_vm_running = vm_running; |
| 5162 | vm_stop(0); |
| 5163 | |
| 5164 | for(i = 0; i <= MAX_DISKS; i++) { |
| 5165 | bs1 = bs_table[i]; |
| 5166 | if (bdrv_has_snapshot(bs1)) { |
| 5167 | ret = bdrv_snapshot_goto(bs1, name); |
| 5168 | if (ret < 0) { |
| 5169 | if (bs != bs1) |
| 5170 | term_printf("Warning: "); |
| 5171 | switch(ret) { |
| 5172 | case -ENOTSUP: |
| 5173 | term_printf("Snapshots not supported on device '%s'\n", |
| 5174 | bdrv_get_device_name(bs1)); |
| 5175 | break; |
| 5176 | case -ENOENT: |
| 5177 | term_printf("Could not find snapshot '%s' on device '%s'\n", |
| 5178 | name, bdrv_get_device_name(bs1)); |
| 5179 | break; |
| 5180 | default: |
| 5181 | term_printf("Error %d while activating snapshot on '%s'\n", |
| 5182 | ret, bdrv_get_device_name(bs1)); |
| 5183 | break; |
| 5184 | } |
| 5185 | /* fatal on snapshot block device */ |
| 5186 | if (bs == bs1) |
| 5187 | goto the_end; |
| 5188 | } |
| 5189 | } |
| 5190 | } |
| 5191 | |
| 5192 | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
| 5193 | term_printf("Device %s does not support VM state snapshots\n", |
| 5194 | bdrv_get_device_name(bs)); |
| 5195 | return; |
| 5196 | } |
| 5197 | |
| 5198 | /* restore the VM state */ |
| 5199 | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0); |
| 5200 | if (!f) { |
| 5201 | term_printf("Could not open VM state file\n"); |
| 5202 | goto the_end; |
| 5203 | } |
| 5204 | ret = qemu_loadvm_state(f); |
| 5205 | qemu_fclose(f); |
| 5206 | if (ret < 0) { |
| 5207 | term_printf("Error %d while loading VM state\n", ret); |
| 5208 | } |
| 5209 | the_end: |
| 5210 | if (saved_vm_running) |
| 5211 | vm_start(); |
| 5212 | } |
| 5213 | |
| 5214 | void do_delvm(const char *name) |
| 5215 | { |
| 5216 | BlockDriverState *bs, *bs1; |
| 5217 | int i, ret; |
| 5218 | |
| 5219 | bs = get_bs_snapshots(); |
| 5220 | if (!bs) { |
| 5221 | term_printf("No block device supports snapshots\n"); |
| 5222 | return; |
| 5223 | } |
| 5224 | |
| 5225 | for(i = 0; i <= MAX_DISKS; i++) { |
| 5226 | bs1 = bs_table[i]; |
| 5227 | if (bdrv_has_snapshot(bs1)) { |
| 5228 | ret = bdrv_snapshot_delete(bs1, name); |
| 5229 | if (ret < 0) { |
| 5230 | if (ret == -ENOTSUP) |
| 5231 | term_printf("Snapshots not supported on device '%s'\n", |
| 5232 | bdrv_get_device_name(bs1)); |
| 5233 | else |
| 5234 | term_printf("Error %d while deleting snapshot on '%s'\n", |
| 5235 | ret, bdrv_get_device_name(bs1)); |
| 5236 | } |
| 5237 | } |
| 5238 | } |
| 5239 | } |
| 5240 | |
| 5241 | void do_info_snapshots(void) |
| 5242 | { |
| 5243 | BlockDriverState *bs, *bs1; |
| 5244 | QEMUSnapshotInfo *sn_tab, *sn; |
| 5245 | int nb_sns, i; |
| 5246 | char buf[256]; |
| 5247 | |
| 5248 | bs = get_bs_snapshots(); |
| 5249 | if (!bs) { |
| 5250 | term_printf("No available block device supports snapshots\n"); |
| 5251 | return; |
| 5252 | } |
| 5253 | term_printf("Snapshot devices:"); |
| 5254 | for(i = 0; i <= MAX_DISKS; i++) { |
| 5255 | bs1 = bs_table[i]; |
| 5256 | if (bdrv_has_snapshot(bs1)) { |
| 5257 | if (bs == bs1) |
| 5258 | term_printf(" %s", bdrv_get_device_name(bs1)); |
| 5259 | } |
| 5260 | } |
| 5261 | term_printf("\n"); |
| 5262 | |
| 5263 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 5264 | if (nb_sns < 0) { |
| 5265 | term_printf("bdrv_snapshot_list: error %d\n", nb_sns); |
| 5266 | return; |
| 5267 | } |
| 5268 | term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs)); |
| 5269 | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
| 5270 | for(i = 0; i < nb_sns; i++) { |
| 5271 | sn = &sn_tab[i]; |
| 5272 | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
| 5273 | } |
| 5274 | qemu_free(sn_tab); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5275 | } |
| 5276 | |
| 5277 | /***********************************************************/ |
| 5278 | /* cpu save/restore */ |
| 5279 | |
| 5280 | #if defined(TARGET_I386) |
| 5281 | |
| 5282 | static void cpu_put_seg(QEMUFile *f, SegmentCache *dt) |
| 5283 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 5284 | qemu_put_be32(f, dt->selector); |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5285 | qemu_put_betl(f, dt->base); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5286 | qemu_put_be32(f, dt->limit); |
| 5287 | qemu_put_be32(f, dt->flags); |
| 5288 | } |
| 5289 | |
| 5290 | static void cpu_get_seg(QEMUFile *f, SegmentCache *dt) |
| 5291 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 5292 | dt->selector = qemu_get_be32(f); |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5293 | dt->base = qemu_get_betl(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5294 | dt->limit = qemu_get_be32(f); |
| 5295 | dt->flags = qemu_get_be32(f); |
| 5296 | } |
| 5297 | |
| 5298 | void cpu_save(QEMUFile *f, void *opaque) |
| 5299 | { |
| 5300 | CPUState *env = opaque; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5301 | uint16_t fptag, fpus, fpuc, fpregs_format; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5302 | uint32_t hflags; |
| 5303 | int i; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5304 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5305 | for(i = 0; i < CPU_NB_REGS; i++) |
| 5306 | qemu_put_betls(f, &env->regs[i]); |
| 5307 | qemu_put_betls(f, &env->eip); |
| 5308 | qemu_put_betls(f, &env->eflags); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5309 | hflags = env->hflags; /* XXX: suppress most of the redundant hflags */ |
| 5310 | qemu_put_be32s(f, &hflags); |
| 5311 | |
| 5312 | /* FPU */ |
| 5313 | fpuc = env->fpuc; |
| 5314 | fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; |
| 5315 | fptag = 0; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5316 | for(i = 0; i < 8; i++) { |
| 5317 | fptag |= ((!env->fptags[i]) << i); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5318 | } |
| 5319 | |
| 5320 | qemu_put_be16s(f, &fpuc); |
| 5321 | qemu_put_be16s(f, &fpus); |
| 5322 | qemu_put_be16s(f, &fptag); |
| 5323 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5324 | #ifdef USE_X86LDOUBLE |
| 5325 | fpregs_format = 0; |
| 5326 | #else |
| 5327 | fpregs_format = 1; |
| 5328 | #endif |
| 5329 | qemu_put_be16s(f, &fpregs_format); |
| 5330 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5331 | for(i = 0; i < 8; i++) { |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5332 | #ifdef USE_X86LDOUBLE |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5333 | { |
| 5334 | uint64_t mant; |
| 5335 | uint16_t exp; |
| 5336 | /* we save the real CPU data (in case of MMX usage only 'mant' |
| 5337 | contains the MMX register */ |
| 5338 | cpu_get_fp80(&mant, &exp, env->fpregs[i].d); |
| 5339 | qemu_put_be64(f, mant); |
| 5340 | qemu_put_be16(f, exp); |
| 5341 | } |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5342 | #else |
| 5343 | /* if we use doubles for float emulation, we save the doubles to |
| 5344 | avoid losing information in case of MMX usage. It can give |
| 5345 | problems if the image is restored on a CPU where long |
| 5346 | doubles are used instead. */ |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5347 | qemu_put_be64(f, env->fpregs[i].mmx.MMX_Q(0)); |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5348 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5349 | } |
| 5350 | |
| 5351 | for(i = 0; i < 6; i++) |
| 5352 | cpu_put_seg(f, &env->segs[i]); |
| 5353 | cpu_put_seg(f, &env->ldt); |
| 5354 | cpu_put_seg(f, &env->tr); |
| 5355 | cpu_put_seg(f, &env->gdt); |
| 5356 | cpu_put_seg(f, &env->idt); |
| 5357 | |
| 5358 | qemu_put_be32s(f, &env->sysenter_cs); |
| 5359 | qemu_put_be32s(f, &env->sysenter_esp); |
| 5360 | qemu_put_be32s(f, &env->sysenter_eip); |
| 5361 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5362 | qemu_put_betls(f, &env->cr[0]); |
| 5363 | qemu_put_betls(f, &env->cr[2]); |
| 5364 | qemu_put_betls(f, &env->cr[3]); |
| 5365 | qemu_put_betls(f, &env->cr[4]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5366 | |
| 5367 | for(i = 0; i < 8; i++) |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5368 | qemu_put_betls(f, &env->dr[i]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5369 | |
| 5370 | /* MMU */ |
| 5371 | qemu_put_be32s(f, &env->a20_mask); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5372 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5373 | /* XMM */ |
| 5374 | qemu_put_be32s(f, &env->mxcsr); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5375 | for(i = 0; i < CPU_NB_REGS; i++) { |
| 5376 | qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(0)); |
| 5377 | qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(1)); |
| 5378 | } |
| 5379 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5380 | #ifdef TARGET_X86_64 |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5381 | qemu_put_be64s(f, &env->efer); |
| 5382 | qemu_put_be64s(f, &env->star); |
| 5383 | qemu_put_be64s(f, &env->lstar); |
| 5384 | qemu_put_be64s(f, &env->cstar); |
| 5385 | qemu_put_be64s(f, &env->fmask); |
| 5386 | qemu_put_be64s(f, &env->kernelgsbase); |
| 5387 | #endif |
bellard | 3b21e03 | 2006-09-24 18:41:56 +0000 | [diff] [blame] | 5388 | qemu_put_be32s(f, &env->smbase); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5389 | } |
| 5390 | |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5391 | #ifdef USE_X86LDOUBLE |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5392 | /* XXX: add that in a FPU generic layer */ |
| 5393 | union x86_longdouble { |
| 5394 | uint64_t mant; |
| 5395 | uint16_t exp; |
| 5396 | }; |
| 5397 | |
| 5398 | #define MANTD1(fp) (fp & ((1LL << 52) - 1)) |
| 5399 | #define EXPBIAS1 1023 |
| 5400 | #define EXPD1(fp) ((fp >> 52) & 0x7FF) |
| 5401 | #define SIGND1(fp) ((fp >> 32) & 0x80000000) |
| 5402 | |
| 5403 | static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp) |
| 5404 | { |
| 5405 | int e; |
| 5406 | /* mantissa */ |
| 5407 | p->mant = (MANTD1(temp) << 11) | (1LL << 63); |
| 5408 | /* exponent + sign */ |
| 5409 | e = EXPD1(temp) - EXPBIAS1 + 16383; |
| 5410 | e |= SIGND1(temp) >> 16; |
| 5411 | p->exp = e; |
| 5412 | } |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5413 | #endif |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5414 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5415 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5416 | { |
| 5417 | CPUState *env = opaque; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5418 | int i, guess_mmx; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5419 | uint32_t hflags; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5420 | uint16_t fpus, fpuc, fptag, fpregs_format; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5421 | |
bellard | 3b21e03 | 2006-09-24 18:41:56 +0000 | [diff] [blame] | 5422 | if (version_id != 3 && version_id != 4) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5423 | return -EINVAL; |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5424 | for(i = 0; i < CPU_NB_REGS; i++) |
| 5425 | qemu_get_betls(f, &env->regs[i]); |
| 5426 | qemu_get_betls(f, &env->eip); |
| 5427 | qemu_get_betls(f, &env->eflags); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5428 | qemu_get_be32s(f, &hflags); |
| 5429 | |
| 5430 | qemu_get_be16s(f, &fpuc); |
| 5431 | qemu_get_be16s(f, &fpus); |
| 5432 | qemu_get_be16s(f, &fptag); |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5433 | qemu_get_be16s(f, &fpregs_format); |
| 5434 | |
| 5435 | /* NOTE: we cannot always restore the FPU state if the image come |
| 5436 | from a host with a different 'USE_X86LDOUBLE' define. We guess |
| 5437 | if we are in an MMX state to restore correctly in that case. */ |
| 5438 | guess_mmx = ((fptag == 0xff) && (fpus & 0x3800) == 0); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5439 | for(i = 0; i < 8; i++) { |
| 5440 | uint64_t mant; |
| 5441 | uint16_t exp; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5442 | |
| 5443 | switch(fpregs_format) { |
| 5444 | case 0: |
| 5445 | mant = qemu_get_be64(f); |
| 5446 | exp = qemu_get_be16(f); |
| 5447 | #ifdef USE_X86LDOUBLE |
| 5448 | env->fpregs[i].d = cpu_set_fp80(mant, exp); |
| 5449 | #else |
| 5450 | /* difficult case */ |
| 5451 | if (guess_mmx) |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5452 | env->fpregs[i].mmx.MMX_Q(0) = mant; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5453 | else |
| 5454 | env->fpregs[i].d = cpu_set_fp80(mant, exp); |
| 5455 | #endif |
| 5456 | break; |
| 5457 | case 1: |
| 5458 | mant = qemu_get_be64(f); |
| 5459 | #ifdef USE_X86LDOUBLE |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5460 | { |
| 5461 | union x86_longdouble *p; |
| 5462 | /* difficult case */ |
| 5463 | p = (void *)&env->fpregs[i]; |
| 5464 | if (guess_mmx) { |
| 5465 | p->mant = mant; |
| 5466 | p->exp = 0xffff; |
| 5467 | } else { |
| 5468 | fp64_to_fp80(p, mant); |
| 5469 | } |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5470 | } |
| 5471 | #else |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5472 | env->fpregs[i].mmx.MMX_Q(0) = mant; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5473 | #endif |
| 5474 | break; |
| 5475 | default: |
| 5476 | return -EINVAL; |
| 5477 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5478 | } |
| 5479 | |
| 5480 | env->fpuc = fpuc; |
bellard | 7a0e1f4 | 2005-03-13 17:01:47 +0000 | [diff] [blame] | 5481 | /* XXX: restore FPU round state */ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5482 | env->fpstt = (fpus >> 11) & 7; |
| 5483 | env->fpus = fpus & ~0x3800; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5484 | fptag ^= 0xff; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5485 | for(i = 0; i < 8; i++) { |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5486 | env->fptags[i] = (fptag >> i) & 1; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5487 | } |
| 5488 | |
| 5489 | for(i = 0; i < 6; i++) |
| 5490 | cpu_get_seg(f, &env->segs[i]); |
| 5491 | cpu_get_seg(f, &env->ldt); |
| 5492 | cpu_get_seg(f, &env->tr); |
| 5493 | cpu_get_seg(f, &env->gdt); |
| 5494 | cpu_get_seg(f, &env->idt); |
| 5495 | |
| 5496 | qemu_get_be32s(f, &env->sysenter_cs); |
| 5497 | qemu_get_be32s(f, &env->sysenter_esp); |
| 5498 | qemu_get_be32s(f, &env->sysenter_eip); |
| 5499 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5500 | qemu_get_betls(f, &env->cr[0]); |
| 5501 | qemu_get_betls(f, &env->cr[2]); |
| 5502 | qemu_get_betls(f, &env->cr[3]); |
| 5503 | qemu_get_betls(f, &env->cr[4]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5504 | |
| 5505 | for(i = 0; i < 8; i++) |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5506 | qemu_get_betls(f, &env->dr[i]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5507 | |
| 5508 | /* MMU */ |
| 5509 | qemu_get_be32s(f, &env->a20_mask); |
| 5510 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5511 | qemu_get_be32s(f, &env->mxcsr); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5512 | for(i = 0; i < CPU_NB_REGS; i++) { |
| 5513 | qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(0)); |
| 5514 | qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(1)); |
| 5515 | } |
| 5516 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5517 | #ifdef TARGET_X86_64 |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5518 | qemu_get_be64s(f, &env->efer); |
| 5519 | qemu_get_be64s(f, &env->star); |
| 5520 | qemu_get_be64s(f, &env->lstar); |
| 5521 | qemu_get_be64s(f, &env->cstar); |
| 5522 | qemu_get_be64s(f, &env->fmask); |
| 5523 | qemu_get_be64s(f, &env->kernelgsbase); |
| 5524 | #endif |
bellard | 3b21e03 | 2006-09-24 18:41:56 +0000 | [diff] [blame] | 5525 | if (version_id >= 4) |
| 5526 | qemu_get_be32s(f, &env->smbase); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5527 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5528 | /* XXX: compute hflags from scratch, except for CPL and IIF */ |
| 5529 | env->hflags = hflags; |
| 5530 | tlb_flush(env, 1); |
| 5531 | return 0; |
| 5532 | } |
| 5533 | |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 5534 | #elif defined(TARGET_PPC) |
| 5535 | void cpu_save(QEMUFile *f, void *opaque) |
| 5536 | { |
| 5537 | } |
| 5538 | |
| 5539 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5540 | { |
| 5541 | return 0; |
| 5542 | } |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 5543 | |
| 5544 | #elif defined(TARGET_MIPS) |
| 5545 | void cpu_save(QEMUFile *f, void *opaque) |
| 5546 | { |
| 5547 | } |
| 5548 | |
| 5549 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5550 | { |
| 5551 | return 0; |
| 5552 | } |
| 5553 | |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 5554 | #elif defined(TARGET_SPARC) |
| 5555 | void cpu_save(QEMUFile *f, void *opaque) |
| 5556 | { |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5557 | CPUState *env = opaque; |
| 5558 | int i; |
| 5559 | uint32_t tmp; |
| 5560 | |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5561 | for(i = 0; i < 8; i++) |
| 5562 | qemu_put_betls(f, &env->gregs[i]); |
| 5563 | for(i = 0; i < NWINDOWS * 16; i++) |
| 5564 | qemu_put_betls(f, &env->regbase[i]); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5565 | |
| 5566 | /* FPU */ |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5567 | for(i = 0; i < TARGET_FPREGS; i++) { |
| 5568 | union { |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5569 | float32 f; |
| 5570 | uint32_t i; |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5571 | } u; |
| 5572 | u.f = env->fpr[i]; |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5573 | qemu_put_be32(f, u.i); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5574 | } |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5575 | |
| 5576 | qemu_put_betls(f, &env->pc); |
| 5577 | qemu_put_betls(f, &env->npc); |
| 5578 | qemu_put_betls(f, &env->y); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5579 | tmp = GET_PSR(env); |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5580 | qemu_put_be32(f, tmp); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5581 | qemu_put_betls(f, &env->fsr); |
| 5582 | qemu_put_betls(f, &env->tbr); |
| 5583 | #ifndef TARGET_SPARC64 |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5584 | qemu_put_be32s(f, &env->wim); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5585 | /* MMU */ |
| 5586 | for(i = 0; i < 16; i++) |
| 5587 | qemu_put_be32s(f, &env->mmuregs[i]); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5588 | #endif |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 5589 | } |
| 5590 | |
| 5591 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5592 | { |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5593 | CPUState *env = opaque; |
| 5594 | int i; |
| 5595 | uint32_t tmp; |
| 5596 | |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5597 | for(i = 0; i < 8; i++) |
| 5598 | qemu_get_betls(f, &env->gregs[i]); |
| 5599 | for(i = 0; i < NWINDOWS * 16; i++) |
| 5600 | qemu_get_betls(f, &env->regbase[i]); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5601 | |
| 5602 | /* FPU */ |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5603 | for(i = 0; i < TARGET_FPREGS; i++) { |
| 5604 | union { |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5605 | float32 f; |
| 5606 | uint32_t i; |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5607 | } u; |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5608 | u.i = qemu_get_be32(f); |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5609 | env->fpr[i] = u.f; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5610 | } |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5611 | |
| 5612 | qemu_get_betls(f, &env->pc); |
| 5613 | qemu_get_betls(f, &env->npc); |
| 5614 | qemu_get_betls(f, &env->y); |
| 5615 | tmp = qemu_get_be32(f); |
| 5616 | env->cwp = 0; /* needed to ensure that the wrapping registers are |
| 5617 | correctly updated */ |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5618 | PUT_PSR(env, tmp); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5619 | qemu_get_betls(f, &env->fsr); |
| 5620 | qemu_get_betls(f, &env->tbr); |
| 5621 | #ifndef TARGET_SPARC64 |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5622 | qemu_get_be32s(f, &env->wim); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5623 | /* MMU */ |
| 5624 | for(i = 0; i < 16; i++) |
| 5625 | qemu_get_be32s(f, &env->mmuregs[i]); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5626 | #endif |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5627 | tlb_flush(env, 1); |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 5628 | return 0; |
| 5629 | } |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 5630 | |
| 5631 | #elif defined(TARGET_ARM) |
| 5632 | |
| 5633 | /* ??? Need to implement these. */ |
| 5634 | void cpu_save(QEMUFile *f, void *opaque) |
| 5635 | { |
| 5636 | } |
| 5637 | |
| 5638 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5639 | { |
| 5640 | return 0; |
| 5641 | } |
| 5642 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5643 | #else |
| 5644 | |
| 5645 | #warning No CPU save/restore functions |
| 5646 | |
| 5647 | #endif |
| 5648 | |
| 5649 | /***********************************************************/ |
| 5650 | /* ram save/restore */ |
| 5651 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5652 | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
| 5653 | { |
| 5654 | int v; |
| 5655 | |
| 5656 | v = qemu_get_byte(f); |
| 5657 | switch(v) { |
| 5658 | case 0: |
| 5659 | if (qemu_get_buffer(f, buf, len) != len) |
| 5660 | return -EIO; |
| 5661 | break; |
| 5662 | case 1: |
| 5663 | v = qemu_get_byte(f); |
| 5664 | memset(buf, v, len); |
| 5665 | break; |
| 5666 | default: |
| 5667 | return -EINVAL; |
| 5668 | } |
| 5669 | return 0; |
| 5670 | } |
| 5671 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 5672 | static int ram_load_v1(QEMUFile *f, void *opaque) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5673 | { |
| 5674 | int i, ret; |
| 5675 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5676 | if (qemu_get_be32(f) != phys_ram_size) |
| 5677 | return -EINVAL; |
| 5678 | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
| 5679 | ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
| 5680 | if (ret) |
| 5681 | return ret; |
| 5682 | } |
| 5683 | return 0; |
| 5684 | } |
| 5685 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 5686 | #define BDRV_HASH_BLOCK_SIZE 1024 |
| 5687 | #define IOBUF_SIZE 4096 |
| 5688 | #define RAM_CBLOCK_MAGIC 0xfabe |
| 5689 | |
| 5690 | typedef struct RamCompressState { |
| 5691 | z_stream zstream; |
| 5692 | QEMUFile *f; |
| 5693 | uint8_t buf[IOBUF_SIZE]; |
| 5694 | } RamCompressState; |
| 5695 | |
| 5696 | static int ram_compress_open(RamCompressState *s, QEMUFile *f) |
| 5697 | { |
| 5698 | int ret; |
| 5699 | memset(s, 0, sizeof(*s)); |
| 5700 | s->f = f; |
| 5701 | ret = deflateInit2(&s->zstream, 1, |
| 5702 | Z_DEFLATED, 15, |
| 5703 | 9, Z_DEFAULT_STRATEGY); |
| 5704 | if (ret != Z_OK) |
| 5705 | return -1; |
| 5706 | s->zstream.avail_out = IOBUF_SIZE; |
| 5707 | s->zstream.next_out = s->buf; |
| 5708 | return 0; |
| 5709 | } |
| 5710 | |
| 5711 | static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len) |
| 5712 | { |
| 5713 | qemu_put_be16(s->f, RAM_CBLOCK_MAGIC); |
| 5714 | qemu_put_be16(s->f, len); |
| 5715 | qemu_put_buffer(s->f, buf, len); |
| 5716 | } |
| 5717 | |
| 5718 | static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len) |
| 5719 | { |
| 5720 | int ret; |
| 5721 | |
| 5722 | s->zstream.avail_in = len; |
| 5723 | s->zstream.next_in = (uint8_t *)buf; |
| 5724 | while (s->zstream.avail_in > 0) { |
| 5725 | ret = deflate(&s->zstream, Z_NO_FLUSH); |
| 5726 | if (ret != Z_OK) |
| 5727 | return -1; |
| 5728 | if (s->zstream.avail_out == 0) { |
| 5729 | ram_put_cblock(s, s->buf, IOBUF_SIZE); |
| 5730 | s->zstream.avail_out = IOBUF_SIZE; |
| 5731 | s->zstream.next_out = s->buf; |
| 5732 | } |
| 5733 | } |
| 5734 | return 0; |
| 5735 | } |
| 5736 | |
| 5737 | static void ram_compress_close(RamCompressState *s) |
| 5738 | { |
| 5739 | int len, ret; |
| 5740 | |
| 5741 | /* compress last bytes */ |
| 5742 | for(;;) { |
| 5743 | ret = deflate(&s->zstream, Z_FINISH); |
| 5744 | if (ret == Z_OK || ret == Z_STREAM_END) { |
| 5745 | len = IOBUF_SIZE - s->zstream.avail_out; |
| 5746 | if (len > 0) { |
| 5747 | ram_put_cblock(s, s->buf, len); |
| 5748 | } |
| 5749 | s->zstream.avail_out = IOBUF_SIZE; |
| 5750 | s->zstream.next_out = s->buf; |
| 5751 | if (ret == Z_STREAM_END) |
| 5752 | break; |
| 5753 | } else { |
| 5754 | goto fail; |
| 5755 | } |
| 5756 | } |
| 5757 | fail: |
| 5758 | deflateEnd(&s->zstream); |
| 5759 | } |
| 5760 | |
| 5761 | typedef struct RamDecompressState { |
| 5762 | z_stream zstream; |
| 5763 | QEMUFile *f; |
| 5764 | uint8_t buf[IOBUF_SIZE]; |
| 5765 | } RamDecompressState; |
| 5766 | |
| 5767 | static int ram_decompress_open(RamDecompressState *s, QEMUFile *f) |
| 5768 | { |
| 5769 | int ret; |
| 5770 | memset(s, 0, sizeof(*s)); |
| 5771 | s->f = f; |
| 5772 | ret = inflateInit(&s->zstream); |
| 5773 | if (ret != Z_OK) |
| 5774 | return -1; |
| 5775 | return 0; |
| 5776 | } |
| 5777 | |
| 5778 | static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len) |
| 5779 | { |
| 5780 | int ret, clen; |
| 5781 | |
| 5782 | s->zstream.avail_out = len; |
| 5783 | s->zstream.next_out = buf; |
| 5784 | while (s->zstream.avail_out > 0) { |
| 5785 | if (s->zstream.avail_in == 0) { |
| 5786 | if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC) |
| 5787 | return -1; |
| 5788 | clen = qemu_get_be16(s->f); |
| 5789 | if (clen > IOBUF_SIZE) |
| 5790 | return -1; |
| 5791 | qemu_get_buffer(s->f, s->buf, clen); |
| 5792 | s->zstream.avail_in = clen; |
| 5793 | s->zstream.next_in = s->buf; |
| 5794 | } |
| 5795 | ret = inflate(&s->zstream, Z_PARTIAL_FLUSH); |
| 5796 | if (ret != Z_OK && ret != Z_STREAM_END) { |
| 5797 | return -1; |
| 5798 | } |
| 5799 | } |
| 5800 | return 0; |
| 5801 | } |
| 5802 | |
| 5803 | static void ram_decompress_close(RamDecompressState *s) |
| 5804 | { |
| 5805 | inflateEnd(&s->zstream); |
| 5806 | } |
| 5807 | |
| 5808 | static void ram_save(QEMUFile *f, void *opaque) |
| 5809 | { |
| 5810 | int i; |
| 5811 | RamCompressState s1, *s = &s1; |
| 5812 | uint8_t buf[10]; |
| 5813 | |
| 5814 | qemu_put_be32(f, phys_ram_size); |
| 5815 | if (ram_compress_open(s, f) < 0) |
| 5816 | return; |
| 5817 | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
| 5818 | #if 0 |
| 5819 | if (tight_savevm_enabled) { |
| 5820 | int64_t sector_num; |
| 5821 | int j; |
| 5822 | |
| 5823 | /* find if the memory block is available on a virtual |
| 5824 | block device */ |
| 5825 | sector_num = -1; |
| 5826 | for(j = 0; j < MAX_DISKS; j++) { |
| 5827 | if (bs_table[j]) { |
| 5828 | sector_num = bdrv_hash_find(bs_table[j], |
| 5829 | phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); |
| 5830 | if (sector_num >= 0) |
| 5831 | break; |
| 5832 | } |
| 5833 | } |
| 5834 | if (j == MAX_DISKS) |
| 5835 | goto normal_compress; |
| 5836 | buf[0] = 1; |
| 5837 | buf[1] = j; |
| 5838 | cpu_to_be64wu((uint64_t *)(buf + 2), sector_num); |
| 5839 | ram_compress_buf(s, buf, 10); |
| 5840 | } else |
| 5841 | #endif |
| 5842 | { |
| 5843 | // normal_compress: |
| 5844 | buf[0] = 0; |
| 5845 | ram_compress_buf(s, buf, 1); |
| 5846 | ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); |
| 5847 | } |
| 5848 | } |
| 5849 | ram_compress_close(s); |
| 5850 | } |
| 5851 | |
| 5852 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 5853 | { |
| 5854 | RamDecompressState s1, *s = &s1; |
| 5855 | uint8_t buf[10]; |
| 5856 | int i; |
| 5857 | |
| 5858 | if (version_id == 1) |
| 5859 | return ram_load_v1(f, opaque); |
| 5860 | if (version_id != 2) |
| 5861 | return -EINVAL; |
| 5862 | if (qemu_get_be32(f) != phys_ram_size) |
| 5863 | return -EINVAL; |
| 5864 | if (ram_decompress_open(s, f) < 0) |
| 5865 | return -EINVAL; |
| 5866 | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
| 5867 | if (ram_decompress_buf(s, buf, 1) < 0) { |
| 5868 | fprintf(stderr, "Error while reading ram block header\n"); |
| 5869 | goto error; |
| 5870 | } |
| 5871 | if (buf[0] == 0) { |
| 5872 | if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) { |
| 5873 | fprintf(stderr, "Error while reading ram block address=0x%08x", i); |
| 5874 | goto error; |
| 5875 | } |
| 5876 | } else |
| 5877 | #if 0 |
| 5878 | if (buf[0] == 1) { |
| 5879 | int bs_index; |
| 5880 | int64_t sector_num; |
| 5881 | |
| 5882 | ram_decompress_buf(s, buf + 1, 9); |
| 5883 | bs_index = buf[1]; |
| 5884 | sector_num = be64_to_cpupu((const uint64_t *)(buf + 2)); |
| 5885 | if (bs_index >= MAX_DISKS || bs_table[bs_index] == NULL) { |
| 5886 | fprintf(stderr, "Invalid block device index %d\n", bs_index); |
| 5887 | goto error; |
| 5888 | } |
| 5889 | if (bdrv_read(bs_table[bs_index], sector_num, phys_ram_base + i, |
| 5890 | BDRV_HASH_BLOCK_SIZE / 512) < 0) { |
| 5891 | fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n", |
| 5892 | bs_index, sector_num); |
| 5893 | goto error; |
| 5894 | } |
| 5895 | } else |
| 5896 | #endif |
| 5897 | { |
| 5898 | error: |
| 5899 | printf("Error block header\n"); |
| 5900 | return -EINVAL; |
| 5901 | } |
| 5902 | } |
| 5903 | ram_decompress_close(s); |
| 5904 | return 0; |
| 5905 | } |
| 5906 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5907 | /***********************************************************/ |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5908 | /* bottom halves (can be seen as timers which expire ASAP) */ |
| 5909 | |
| 5910 | struct QEMUBH { |
| 5911 | QEMUBHFunc *cb; |
| 5912 | void *opaque; |
| 5913 | int scheduled; |
| 5914 | QEMUBH *next; |
| 5915 | }; |
| 5916 | |
| 5917 | static QEMUBH *first_bh = NULL; |
| 5918 | |
| 5919 | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque) |
| 5920 | { |
| 5921 | QEMUBH *bh; |
| 5922 | bh = qemu_mallocz(sizeof(QEMUBH)); |
| 5923 | if (!bh) |
| 5924 | return NULL; |
| 5925 | bh->cb = cb; |
| 5926 | bh->opaque = opaque; |
| 5927 | return bh; |
| 5928 | } |
| 5929 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5930 | int qemu_bh_poll(void) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5931 | { |
| 5932 | QEMUBH *bh, **pbh; |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5933 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5934 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5935 | ret = 0; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5936 | for(;;) { |
| 5937 | pbh = &first_bh; |
| 5938 | bh = *pbh; |
| 5939 | if (!bh) |
| 5940 | break; |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5941 | ret = 1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5942 | *pbh = bh->next; |
| 5943 | bh->scheduled = 0; |
| 5944 | bh->cb(bh->opaque); |
| 5945 | } |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5946 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5947 | } |
| 5948 | |
| 5949 | void qemu_bh_schedule(QEMUBH *bh) |
| 5950 | { |
| 5951 | CPUState *env = cpu_single_env; |
| 5952 | if (bh->scheduled) |
| 5953 | return; |
| 5954 | bh->scheduled = 1; |
| 5955 | bh->next = first_bh; |
| 5956 | first_bh = bh; |
| 5957 | |
| 5958 | /* stop the currently executing CPU to execute the BH ASAP */ |
| 5959 | if (env) { |
| 5960 | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
| 5961 | } |
| 5962 | } |
| 5963 | |
| 5964 | void qemu_bh_cancel(QEMUBH *bh) |
| 5965 | { |
| 5966 | QEMUBH **pbh; |
| 5967 | if (bh->scheduled) { |
| 5968 | pbh = &first_bh; |
| 5969 | while (*pbh != bh) |
| 5970 | pbh = &(*pbh)->next; |
| 5971 | *pbh = bh->next; |
| 5972 | bh->scheduled = 0; |
| 5973 | } |
| 5974 | } |
| 5975 | |
| 5976 | void qemu_bh_delete(QEMUBH *bh) |
| 5977 | { |
| 5978 | qemu_bh_cancel(bh); |
| 5979 | qemu_free(bh); |
| 5980 | } |
| 5981 | |
| 5982 | /***********************************************************/ |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 5983 | /* machine registration */ |
| 5984 | |
| 5985 | QEMUMachine *first_machine = NULL; |
| 5986 | |
| 5987 | int qemu_register_machine(QEMUMachine *m) |
| 5988 | { |
| 5989 | QEMUMachine **pm; |
| 5990 | pm = &first_machine; |
| 5991 | while (*pm != NULL) |
| 5992 | pm = &(*pm)->next; |
| 5993 | m->next = NULL; |
| 5994 | *pm = m; |
| 5995 | return 0; |
| 5996 | } |
| 5997 | |
| 5998 | QEMUMachine *find_machine(const char *name) |
| 5999 | { |
| 6000 | QEMUMachine *m; |
| 6001 | |
| 6002 | for(m = first_machine; m != NULL; m = m->next) { |
| 6003 | if (!strcmp(m->name, name)) |
| 6004 | return m; |
| 6005 | } |
| 6006 | return NULL; |
| 6007 | } |
| 6008 | |
| 6009 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6010 | /* main execution loop */ |
| 6011 | |
| 6012 | void gui_update(void *opaque) |
| 6013 | { |
| 6014 | display_state.dpy_refresh(&display_state); |
| 6015 | qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock)); |
| 6016 | } |
| 6017 | |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6018 | struct vm_change_state_entry { |
| 6019 | VMChangeStateHandler *cb; |
| 6020 | void *opaque; |
| 6021 | LIST_ENTRY (vm_change_state_entry) entries; |
| 6022 | }; |
| 6023 | |
| 6024 | static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head; |
| 6025 | |
| 6026 | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
| 6027 | void *opaque) |
| 6028 | { |
| 6029 | VMChangeStateEntry *e; |
| 6030 | |
| 6031 | e = qemu_mallocz(sizeof (*e)); |
| 6032 | if (!e) |
| 6033 | return NULL; |
| 6034 | |
| 6035 | e->cb = cb; |
| 6036 | e->opaque = opaque; |
| 6037 | LIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
| 6038 | return e; |
| 6039 | } |
| 6040 | |
| 6041 | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) |
| 6042 | { |
| 6043 | LIST_REMOVE (e, entries); |
| 6044 | qemu_free (e); |
| 6045 | } |
| 6046 | |
| 6047 | static void vm_state_notify(int running) |
| 6048 | { |
| 6049 | VMChangeStateEntry *e; |
| 6050 | |
| 6051 | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) { |
| 6052 | e->cb(e->opaque, running); |
| 6053 | } |
| 6054 | } |
| 6055 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6056 | /* XXX: support several handlers */ |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6057 | static VMStopHandler *vm_stop_cb; |
| 6058 | static void *vm_stop_opaque; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6059 | |
| 6060 | int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) |
| 6061 | { |
| 6062 | vm_stop_cb = cb; |
| 6063 | vm_stop_opaque = opaque; |
| 6064 | return 0; |
| 6065 | } |
| 6066 | |
| 6067 | void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) |
| 6068 | { |
| 6069 | vm_stop_cb = NULL; |
| 6070 | } |
| 6071 | |
| 6072 | void vm_start(void) |
| 6073 | { |
| 6074 | if (!vm_running) { |
| 6075 | cpu_enable_ticks(); |
| 6076 | vm_running = 1; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6077 | vm_state_notify(1); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6078 | } |
| 6079 | } |
| 6080 | |
| 6081 | void vm_stop(int reason) |
| 6082 | { |
| 6083 | if (vm_running) { |
| 6084 | cpu_disable_ticks(); |
| 6085 | vm_running = 0; |
| 6086 | if (reason != 0) { |
| 6087 | if (vm_stop_cb) { |
| 6088 | vm_stop_cb(vm_stop_opaque, reason); |
| 6089 | } |
| 6090 | } |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6091 | vm_state_notify(0); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6092 | } |
| 6093 | } |
| 6094 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6095 | /* reset/shutdown handler */ |
| 6096 | |
| 6097 | typedef struct QEMUResetEntry { |
| 6098 | QEMUResetHandler *func; |
| 6099 | void *opaque; |
| 6100 | struct QEMUResetEntry *next; |
| 6101 | } QEMUResetEntry; |
| 6102 | |
| 6103 | static QEMUResetEntry *first_reset_entry; |
| 6104 | static int reset_requested; |
| 6105 | static int shutdown_requested; |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6106 | static int powerdown_requested; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6107 | |
| 6108 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
| 6109 | { |
| 6110 | QEMUResetEntry **pre, *re; |
| 6111 | |
| 6112 | pre = &first_reset_entry; |
| 6113 | while (*pre != NULL) |
| 6114 | pre = &(*pre)->next; |
| 6115 | re = qemu_mallocz(sizeof(QEMUResetEntry)); |
| 6116 | re->func = func; |
| 6117 | re->opaque = opaque; |
| 6118 | re->next = NULL; |
| 6119 | *pre = re; |
| 6120 | } |
| 6121 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 6122 | static void qemu_system_reset(void) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6123 | { |
| 6124 | QEMUResetEntry *re; |
| 6125 | |
| 6126 | /* reset all devices */ |
| 6127 | for(re = first_reset_entry; re != NULL; re = re->next) { |
| 6128 | re->func(re->opaque); |
| 6129 | } |
| 6130 | } |
| 6131 | |
| 6132 | void qemu_system_reset_request(void) |
| 6133 | { |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6134 | if (no_reboot) { |
| 6135 | shutdown_requested = 1; |
| 6136 | } else { |
| 6137 | reset_requested = 1; |
| 6138 | } |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6139 | if (cpu_single_env) |
| 6140 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6141 | } |
| 6142 | |
| 6143 | void qemu_system_shutdown_request(void) |
| 6144 | { |
| 6145 | shutdown_requested = 1; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6146 | if (cpu_single_env) |
| 6147 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6148 | } |
| 6149 | |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6150 | void qemu_system_powerdown_request(void) |
| 6151 | { |
| 6152 | powerdown_requested = 1; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6153 | if (cpu_single_env) |
| 6154 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6155 | } |
| 6156 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6157 | void main_loop_wait(int timeout) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6158 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 6159 | IOHandlerRecord *ioh, *ioh_next; |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 6160 | fd_set rfds, wfds, xfds; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6161 | int ret, nfds; |
| 6162 | struct timeval tv; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 6163 | PollingEntry *pe; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6164 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 6165 | |
| 6166 | /* XXX: need to suppress polling by better using win32 events */ |
| 6167 | ret = 0; |
| 6168 | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
| 6169 | ret |= pe->func(pe->opaque); |
| 6170 | } |
bellard | 38e205a | 2004-04-06 19:29:17 +0000 | [diff] [blame] | 6171 | #ifdef _WIN32 |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 6172 | if (ret == 0 && timeout > 0) { |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 6173 | int err; |
| 6174 | WaitObjects *w = &wait_objects; |
| 6175 | |
| 6176 | ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout); |
| 6177 | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
| 6178 | if (w->func[ret - WAIT_OBJECT_0]) |
| 6179 | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
| 6180 | } else if (ret == WAIT_TIMEOUT) { |
| 6181 | } else { |
| 6182 | err = GetLastError(); |
| 6183 | fprintf(stderr, "Wait error %d %d\n", ret, err); |
| 6184 | } |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 6185 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6186 | #endif |
| 6187 | /* poll any events */ |
| 6188 | /* XXX: separate device handlers from system ones */ |
| 6189 | nfds = -1; |
| 6190 | FD_ZERO(&rfds); |
| 6191 | FD_ZERO(&wfds); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 6192 | FD_ZERO(&xfds); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6193 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 6194 | if (ioh->fd_read && |
| 6195 | (!ioh->fd_read_poll || |
| 6196 | ioh->fd_read_poll(ioh->opaque) != 0)) { |
| 6197 | FD_SET(ioh->fd, &rfds); |
| 6198 | if (ioh->fd > nfds) |
| 6199 | nfds = ioh->fd; |
| 6200 | } |
| 6201 | if (ioh->fd_write) { |
| 6202 | FD_SET(ioh->fd, &wfds); |
| 6203 | if (ioh->fd > nfds) |
| 6204 | nfds = ioh->fd; |
| 6205 | } |
| 6206 | } |
| 6207 | |
| 6208 | tv.tv_sec = 0; |
| 6209 | #ifdef _WIN32 |
| 6210 | tv.tv_usec = 0; |
bellard | 38e205a | 2004-04-06 19:29:17 +0000 | [diff] [blame] | 6211 | #else |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6212 | tv.tv_usec = timeout * 1000; |
| 6213 | #endif |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 6214 | #if defined(CONFIG_SLIRP) |
| 6215 | if (slirp_inited) { |
| 6216 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
| 6217 | } |
| 6218 | #endif |
| 6219 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6220 | if (ret > 0) { |
| 6221 | /* XXX: better handling of removal */ |
| 6222 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) { |
| 6223 | ioh_next = ioh->next; |
| 6224 | if (FD_ISSET(ioh->fd, &rfds)) { |
| 6225 | ioh->fd_read(ioh->opaque); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6226 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6227 | if (FD_ISSET(ioh->fd, &wfds)) { |
| 6228 | ioh->fd_write(ioh->opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6229 | } |
| 6230 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6231 | } |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 6232 | #if defined(CONFIG_SLIRP) |
| 6233 | if (slirp_inited) { |
| 6234 | if (ret < 0) { |
| 6235 | FD_ZERO(&rfds); |
| 6236 | FD_ZERO(&wfds); |
| 6237 | FD_ZERO(&xfds); |
| 6238 | } |
| 6239 | slirp_select_poll(&rfds, &wfds, &xfds); |
| 6240 | } |
| 6241 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 6242 | qemu_aio_poll(); |
| 6243 | qemu_bh_poll(); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 6244 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 6245 | if (vm_running) { |
| 6246 | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
| 6247 | qemu_get_clock(vm_clock)); |
| 6248 | /* run dma transfers, if any */ |
| 6249 | DMA_run(); |
| 6250 | } |
| 6251 | |
| 6252 | /* real time timers */ |
| 6253 | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
| 6254 | qemu_get_clock(rt_clock)); |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6255 | } |
| 6256 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6257 | static CPUState *cur_cpu; |
| 6258 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6259 | int main_loop(void) |
| 6260 | { |
| 6261 | int ret, timeout; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6262 | #ifdef CONFIG_PROFILER |
| 6263 | int64_t ti; |
| 6264 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6265 | CPUState *env; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6266 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6267 | cur_cpu = first_cpu; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6268 | for(;;) { |
| 6269 | if (vm_running) { |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 6270 | |
| 6271 | env = cur_cpu; |
| 6272 | for(;;) { |
| 6273 | /* get next cpu */ |
| 6274 | env = env->next_cpu; |
| 6275 | if (!env) |
| 6276 | env = first_cpu; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6277 | #ifdef CONFIG_PROFILER |
| 6278 | ti = profile_getclock(); |
| 6279 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6280 | ret = cpu_exec(env); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6281 | #ifdef CONFIG_PROFILER |
| 6282 | qemu_time += profile_getclock() - ti; |
| 6283 | #endif |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 6284 | if (ret != EXCP_HALTED) |
| 6285 | break; |
| 6286 | /* all CPUs are halted ? */ |
| 6287 | if (env == cur_cpu) { |
| 6288 | ret = EXCP_HLT; |
| 6289 | break; |
| 6290 | } |
| 6291 | } |
| 6292 | cur_cpu = env; |
| 6293 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6294 | if (shutdown_requested) { |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6295 | ret = EXCP_INTERRUPT; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6296 | break; |
| 6297 | } |
| 6298 | if (reset_requested) { |
| 6299 | reset_requested = 0; |
| 6300 | qemu_system_reset(); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6301 | ret = EXCP_INTERRUPT; |
| 6302 | } |
| 6303 | if (powerdown_requested) { |
| 6304 | powerdown_requested = 0; |
| 6305 | qemu_system_powerdown(); |
| 6306 | ret = EXCP_INTERRUPT; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6307 | } |
| 6308 | if (ret == EXCP_DEBUG) { |
| 6309 | vm_stop(EXCP_DEBUG); |
| 6310 | } |
| 6311 | /* if hlt instruction, we wait until the next IRQ */ |
| 6312 | /* XXX: use timeout computed from timers */ |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6313 | if (ret == EXCP_HLT) |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6314 | timeout = 10; |
| 6315 | else |
| 6316 | timeout = 0; |
| 6317 | } else { |
| 6318 | timeout = 10; |
| 6319 | } |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6320 | #ifdef CONFIG_PROFILER |
| 6321 | ti = profile_getclock(); |
| 6322 | #endif |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6323 | main_loop_wait(timeout); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6324 | #ifdef CONFIG_PROFILER |
| 6325 | dev_time += profile_getclock() - ti; |
| 6326 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6327 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 6328 | cpu_disable_ticks(); |
| 6329 | return ret; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6330 | } |
| 6331 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6332 | void help(void) |
| 6333 | { |
bellard | 84f2e8e | 2007-02-05 20:21:32 +0000 | [diff] [blame] | 6334 | printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2007 Fabrice Bellard\n" |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 6335 | "usage: %s [options] [disk_image]\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6336 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6337 | "'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] | 6338 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6339 | "Standard options:\n" |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6340 | "-M machine select emulated machine (-M ? for list)\n" |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 6341 | "-fda/-fdb file use 'file' as floppy disk 0/1 image\n" |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 6342 | "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n" |
| 6343 | "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6344 | "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n" |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 6345 | "-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] | 6346 | "-snapshot write to temporary files instead of disk image files\n" |
| 6347 | #ifdef CONFIG_SDL |
| 6348 | "-no-quit disable SDL window close capability\n" |
| 6349 | #endif |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 6350 | #ifdef TARGET_I386 |
| 6351 | "-no-fd-bootchk disable boot signature checking for floppy disks\n" |
| 6352 | #endif |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6353 | "-m megs set virtual RAM size to megs MB [default=%d]\n" |
bellard | 91fc211 | 2005-12-18 19:09:37 +0000 | [diff] [blame] | 6354 | "-smp n set the number of CPUs to 'n' [default=1]\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6355 | "-nographic disable graphical output and redirect serial I/Os to console\n" |
bellard | 4ca0074 | 2004-12-12 22:20:04 +0000 | [diff] [blame] | 6356 | #ifndef _WIN32 |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6357 | "-k language use keyboard layout (for example \"fr\" for French)\n" |
bellard | 4ca0074 | 2004-12-12 22:20:04 +0000 | [diff] [blame] | 6358 | #endif |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6359 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6360 | "-audio-help print list of audio drivers and their options\n" |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 6361 | "-soundhw c1,... enable audio support\n" |
| 6362 | " and only specified sound cards (comma separated list)\n" |
| 6363 | " use -soundhw ? to get the list of supported cards\n" |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6364 | " use -soundhw all to enable all of them\n" |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6365 | #endif |
bellard | 8998028 | 2004-06-03 14:04:03 +0000 | [diff] [blame] | 6366 | "-localtime set the real time clock to local time [default=utc]\n" |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6367 | "-full-screen start in full screen\n" |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 6368 | #ifdef TARGET_I386 |
| 6369 | "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n" |
| 6370 | #endif |
bellard | b389dbf | 2005-11-06 16:49:55 +0000 | [diff] [blame] | 6371 | "-usb enable the USB driver (will be the default soon)\n" |
| 6372 | "-usbdevice name add the host or guest USB device 'name'\n" |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 6373 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
| 6374 | "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n" |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6375 | #endif |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6376 | "\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6377 | "Network options:\n" |
pbrook | a41b2ff | 2006-02-05 04:14:41 +0000 | [diff] [blame] | 6378 | "-net nic[,vlan=n][,macaddr=addr][,model=type]\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6379 | " create a new Network Interface Card and connect it to VLAN 'n'\n" |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 6380 | #ifdef CONFIG_SLIRP |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 6381 | "-net user[,vlan=n][,hostname=host]\n" |
| 6382 | " connect the user mode network stack to VLAN 'n' and send\n" |
| 6383 | " hostname 'host' to DHCP clients\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6384 | #endif |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 6385 | #ifdef _WIN32 |
| 6386 | "-net tap[,vlan=n],ifname=name\n" |
| 6387 | " connect the host TAP network interface to VLAN 'n'\n" |
| 6388 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6389 | "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n" |
| 6390 | " connect the host TAP network interface to VLAN 'n' and use\n" |
| 6391 | " the network script 'file' (default=%s);\n" |
ths | 6a1cbf6 | 2007-02-02 00:37:56 +0000 | [diff] [blame] | 6392 | " use 'script=no' to disable script execution;\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6393 | " use 'fd=h' to connect to an already opened TAP interface\n" |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 6394 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6395 | "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6396 | " connect the vlan 'n' to another VLAN using a socket connection\n" |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 6397 | "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n" |
| 6398 | " connect the vlan 'n' to multicast maddr and port\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6399 | "-net none use it alone to have zero network devices; if no -net option\n" |
| 6400 | " is provided, the default is '-net nic -net user'\n" |
| 6401 | "\n" |
| 6402 | #ifdef CONFIG_SLIRP |
| 6403 | "-tftp prefix allow tftp access to files starting with prefix [-net user]\n" |
| 6404 | #ifndef _WIN32 |
| 6405 | "-smb dir allow SMB access to files in 'dir' [-net user]\n" |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 6406 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 6407 | "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6408 | " redirect TCP or UDP connections from host to guest [-net user]\n" |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 6409 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6410 | "\n" |
| 6411 | "Linux boot specific:\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6412 | "-kernel bzImage use 'bzImage' as kernel image\n" |
| 6413 | "-append cmdline use 'cmdline' as kernel command line\n" |
| 6414 | "-initrd file use 'file' as initial ram disk\n" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6415 | "\n" |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 6416 | "Debug/Expert options:\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6417 | "-monitor dev redirect the monitor to char device 'dev'\n" |
| 6418 | "-serial dev redirect the serial port to char device 'dev'\n" |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6419 | "-parallel dev redirect the parallel port to char device 'dev'\n" |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 6420 | "-pidfile file Write PID to 'file'\n" |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6421 | "-S freeze CPU at startup (use 'c' to start execution)\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6422 | "-s wait gdb connection to port %d\n" |
| 6423 | "-p port change gdb connection port\n" |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 6424 | "-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] | 6425 | "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n" |
| 6426 | " translation (t=none or lba) (usually qemu can guess them)\n" |
bellard | 87b4735 | 2006-08-17 17:22:54 +0000 | [diff] [blame] | 6427 | "-L path set the directory for the BIOS, VGA BIOS and keymaps\n" |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6428 | #ifdef USE_KQEMU |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6429 | "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n" |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6430 | "-no-kqemu disable KQEMU kernel module usage\n" |
| 6431 | #endif |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 6432 | #ifdef USE_CODE_COPY |
| 6433 | "-no-code-copy disable code copy acceleration\n" |
| 6434 | #endif |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6435 | #ifdef TARGET_I386 |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 6436 | "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n" |
| 6437 | " (default is CL-GD5446 PCI VGA)\n" |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6438 | "-no-acpi disable ACPI\n" |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6439 | #endif |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6440 | "-no-reboot exit instead of rebooting\n" |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6441 | "-loadvm file start right away with a saved state (loadvm in monitor)\n" |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 6442 | "-vnc display start a VNC server on display\n" |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6443 | #ifndef _WIN32 |
| 6444 | "-daemonize daemonize QEMU after initializing\n" |
| 6445 | #endif |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 6446 | "-option-rom rom load a file, rom, into the option ROM space\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6447 | "\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6448 | "During emulation, the following keys are useful:\n" |
bellard | 032a8c9 | 2004-10-09 22:56:44 +0000 | [diff] [blame] | 6449 | "ctrl-alt-f toggle full screen\n" |
| 6450 | "ctrl-alt-n switch to virtual console 'n'\n" |
| 6451 | "ctrl-alt toggle mouse and keyboard grab\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6452 | "\n" |
| 6453 | "When using -nographic, press 'ctrl-a h' to get some help.\n" |
| 6454 | , |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 6455 | "qemu", |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6456 | DEFAULT_RAM_SIZE, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6457 | #ifndef _WIN32 |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6458 | DEFAULT_NETWORK_SCRIPT, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6459 | #endif |
bellard | 6e44ba7 | 2004-01-18 21:56:49 +0000 | [diff] [blame] | 6460 | DEFAULT_GDBSTUB_PORT, |
| 6461 | "/tmp/qemu.log"); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6462 | exit(1); |
| 6463 | } |
| 6464 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6465 | #define HAS_ARG 0x0001 |
| 6466 | |
| 6467 | enum { |
| 6468 | QEMU_OPTION_h, |
| 6469 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6470 | QEMU_OPTION_M, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6471 | QEMU_OPTION_fda, |
| 6472 | QEMU_OPTION_fdb, |
| 6473 | QEMU_OPTION_hda, |
| 6474 | QEMU_OPTION_hdb, |
| 6475 | QEMU_OPTION_hdc, |
| 6476 | QEMU_OPTION_hdd, |
| 6477 | QEMU_OPTION_cdrom, |
| 6478 | QEMU_OPTION_boot, |
| 6479 | QEMU_OPTION_snapshot, |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 6480 | #ifdef TARGET_I386 |
| 6481 | QEMU_OPTION_no_fd_bootchk, |
| 6482 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6483 | QEMU_OPTION_m, |
| 6484 | QEMU_OPTION_nographic, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6485 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6486 | QEMU_OPTION_audio_help, |
| 6487 | QEMU_OPTION_soundhw, |
| 6488 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6489 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6490 | QEMU_OPTION_net, |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 6491 | QEMU_OPTION_tftp, |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 6492 | QEMU_OPTION_smb, |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 6493 | QEMU_OPTION_redir, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6494 | |
| 6495 | QEMU_OPTION_kernel, |
| 6496 | QEMU_OPTION_append, |
| 6497 | QEMU_OPTION_initrd, |
| 6498 | |
| 6499 | QEMU_OPTION_S, |
| 6500 | QEMU_OPTION_s, |
| 6501 | QEMU_OPTION_p, |
| 6502 | QEMU_OPTION_d, |
| 6503 | QEMU_OPTION_hdachs, |
| 6504 | QEMU_OPTION_L, |
| 6505 | QEMU_OPTION_no_code_copy, |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 6506 | QEMU_OPTION_k, |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 6507 | QEMU_OPTION_localtime, |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 6508 | QEMU_OPTION_cirrusvga, |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 6509 | QEMU_OPTION_g, |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 6510 | QEMU_OPTION_std_vga, |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame^] | 6511 | QEMU_OPTION_echr, |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6512 | QEMU_OPTION_monitor, |
| 6513 | QEMU_OPTION_serial, |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6514 | QEMU_OPTION_parallel, |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6515 | QEMU_OPTION_loadvm, |
| 6516 | QEMU_OPTION_full_screen, |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6517 | QEMU_OPTION_no_quit, |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 6518 | QEMU_OPTION_pidfile, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6519 | QEMU_OPTION_no_kqemu, |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6520 | QEMU_OPTION_kernel_kqemu, |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 6521 | QEMU_OPTION_win2k_hack, |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 6522 | QEMU_OPTION_usb, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6523 | QEMU_OPTION_usbdevice, |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6524 | QEMU_OPTION_smp, |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 6525 | QEMU_OPTION_vnc, |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6526 | QEMU_OPTION_no_acpi, |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6527 | QEMU_OPTION_no_reboot, |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6528 | QEMU_OPTION_daemonize, |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 6529 | QEMU_OPTION_option_rom, |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 6530 | QEMU_OPTION_semihosting |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6531 | }; |
| 6532 | |
| 6533 | typedef struct QEMUOption { |
| 6534 | const char *name; |
| 6535 | int flags; |
| 6536 | int index; |
| 6537 | } QEMUOption; |
| 6538 | |
| 6539 | const QEMUOption qemu_options[] = { |
| 6540 | { "h", 0, QEMU_OPTION_h }, |
pbrook | 64423fb | 2007-01-27 17:11:41 +0000 | [diff] [blame] | 6541 | { "help", 0, QEMU_OPTION_h }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6542 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6543 | { "M", HAS_ARG, QEMU_OPTION_M }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6544 | { "fda", HAS_ARG, QEMU_OPTION_fda }, |
| 6545 | { "fdb", HAS_ARG, QEMU_OPTION_fdb }, |
| 6546 | { "hda", HAS_ARG, QEMU_OPTION_hda }, |
| 6547 | { "hdb", HAS_ARG, QEMU_OPTION_hdb }, |
| 6548 | { "hdc", HAS_ARG, QEMU_OPTION_hdc }, |
| 6549 | { "hdd", HAS_ARG, QEMU_OPTION_hdd }, |
| 6550 | { "cdrom", HAS_ARG, QEMU_OPTION_cdrom }, |
| 6551 | { "boot", HAS_ARG, QEMU_OPTION_boot }, |
| 6552 | { "snapshot", 0, QEMU_OPTION_snapshot }, |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 6553 | #ifdef TARGET_I386 |
| 6554 | { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk }, |
| 6555 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6556 | { "m", HAS_ARG, QEMU_OPTION_m }, |
| 6557 | { "nographic", 0, QEMU_OPTION_nographic }, |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 6558 | { "k", HAS_ARG, QEMU_OPTION_k }, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6559 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6560 | { "audio-help", 0, QEMU_OPTION_audio_help }, |
| 6561 | { "soundhw", HAS_ARG, QEMU_OPTION_soundhw }, |
| 6562 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6563 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6564 | { "net", HAS_ARG, QEMU_OPTION_net}, |
bellard | 158156d | 2004-05-17 21:13:42 +0000 | [diff] [blame] | 6565 | #ifdef CONFIG_SLIRP |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 6566 | { "tftp", HAS_ARG, QEMU_OPTION_tftp }, |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 6567 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 6568 | { "smb", HAS_ARG, QEMU_OPTION_smb }, |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 6569 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 6570 | { "redir", HAS_ARG, QEMU_OPTION_redir }, |
bellard | 158156d | 2004-05-17 21:13:42 +0000 | [diff] [blame] | 6571 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6572 | |
| 6573 | { "kernel", HAS_ARG, QEMU_OPTION_kernel }, |
| 6574 | { "append", HAS_ARG, QEMU_OPTION_append }, |
| 6575 | { "initrd", HAS_ARG, QEMU_OPTION_initrd }, |
| 6576 | |
| 6577 | { "S", 0, QEMU_OPTION_S }, |
| 6578 | { "s", 0, QEMU_OPTION_s }, |
| 6579 | { "p", HAS_ARG, QEMU_OPTION_p }, |
| 6580 | { "d", HAS_ARG, QEMU_OPTION_d }, |
| 6581 | { "hdachs", HAS_ARG, QEMU_OPTION_hdachs }, |
| 6582 | { "L", HAS_ARG, QEMU_OPTION_L }, |
| 6583 | { "no-code-copy", 0, QEMU_OPTION_no_code_copy }, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6584 | #ifdef USE_KQEMU |
| 6585 | { "no-kqemu", 0, QEMU_OPTION_no_kqemu }, |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6586 | { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu }, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6587 | #endif |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 6588 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 6589 | { "g", 1, QEMU_OPTION_g }, |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 6590 | #endif |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 6591 | { "localtime", 0, QEMU_OPTION_localtime }, |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 6592 | { "std-vga", 0, QEMU_OPTION_std_vga }, |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame^] | 6593 | { "echr", 1, QEMU_OPTION_echr }, |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6594 | { "monitor", 1, QEMU_OPTION_monitor }, |
| 6595 | { "serial", 1, QEMU_OPTION_serial }, |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6596 | { "parallel", 1, QEMU_OPTION_parallel }, |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6597 | { "loadvm", HAS_ARG, QEMU_OPTION_loadvm }, |
| 6598 | { "full-screen", 0, QEMU_OPTION_full_screen }, |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6599 | #ifdef CONFIG_SDL |
| 6600 | { "no-quit", 0, QEMU_OPTION_no_quit }, |
| 6601 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 6602 | { "pidfile", HAS_ARG, QEMU_OPTION_pidfile }, |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 6603 | { "win2k-hack", 0, QEMU_OPTION_win2k_hack }, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6604 | { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice }, |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6605 | { "smp", HAS_ARG, QEMU_OPTION_smp }, |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 6606 | { "vnc", HAS_ARG, QEMU_OPTION_vnc }, |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 6607 | |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 6608 | /* temporary options */ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6609 | { "usb", 0, QEMU_OPTION_usb }, |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 6610 | { "cirrusvga", 0, QEMU_OPTION_cirrusvga }, |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6611 | { "no-acpi", 0, QEMU_OPTION_no_acpi }, |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6612 | { "no-reboot", 0, QEMU_OPTION_no_reboot }, |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6613 | { "daemonize", 0, QEMU_OPTION_daemonize }, |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 6614 | { "option-rom", HAS_ARG, QEMU_OPTION_option_rom }, |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 6615 | #if defined(TARGET_ARM) |
| 6616 | { "semihosting", 0, QEMU_OPTION_semihosting }, |
| 6617 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6618 | { NULL }, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6619 | }; |
| 6620 | |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 6621 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
| 6622 | |
| 6623 | /* this stack is only used during signal handling */ |
| 6624 | #define SIGNAL_STACK_SIZE 32768 |
| 6625 | |
| 6626 | static uint8_t *signal_stack; |
| 6627 | |
| 6628 | #endif |
| 6629 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6630 | /* password input */ |
| 6631 | |
| 6632 | static BlockDriverState *get_bdrv(int index) |
| 6633 | { |
| 6634 | BlockDriverState *bs; |
| 6635 | |
| 6636 | if (index < 4) { |
| 6637 | bs = bs_table[index]; |
| 6638 | } else if (index < 6) { |
| 6639 | bs = fd_table[index - 4]; |
| 6640 | } else { |
| 6641 | bs = NULL; |
| 6642 | } |
| 6643 | return bs; |
| 6644 | } |
| 6645 | |
| 6646 | static void read_passwords(void) |
| 6647 | { |
| 6648 | BlockDriverState *bs; |
| 6649 | int i, j; |
| 6650 | char password[256]; |
| 6651 | |
| 6652 | for(i = 0; i < 6; i++) { |
| 6653 | bs = get_bdrv(i); |
| 6654 | if (bs && bdrv_is_encrypted(bs)) { |
| 6655 | term_printf("%s is encrypted.\n", bdrv_get_device_name(bs)); |
| 6656 | for(j = 0; j < 3; j++) { |
| 6657 | monitor_readline("Password: ", |
| 6658 | 1, password, sizeof(password)); |
| 6659 | if (bdrv_set_key(bs, password) == 0) |
| 6660 | break; |
| 6661 | term_printf("invalid password\n"); |
| 6662 | } |
| 6663 | } |
| 6664 | } |
| 6665 | } |
| 6666 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6667 | /* XXX: currently we cannot use simultaneously different CPUs */ |
| 6668 | void register_machines(void) |
| 6669 | { |
| 6670 | #if defined(TARGET_I386) |
| 6671 | qemu_register_machine(&pc_machine); |
bellard | 3dbbdc2 | 2005-11-06 18:20:37 +0000 | [diff] [blame] | 6672 | qemu_register_machine(&isapc_machine); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6673 | #elif defined(TARGET_PPC) |
| 6674 | qemu_register_machine(&heathrow_machine); |
| 6675 | qemu_register_machine(&core99_machine); |
| 6676 | qemu_register_machine(&prep_machine); |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 6677 | #elif defined(TARGET_MIPS) |
| 6678 | qemu_register_machine(&mips_machine); |
ths | 5856de8 | 2007-01-15 23:58:11 +0000 | [diff] [blame] | 6679 | qemu_register_machine(&mips_malta_machine); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6680 | #elif defined(TARGET_SPARC) |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6681 | #ifdef TARGET_SPARC64 |
| 6682 | qemu_register_machine(&sun4u_machine); |
| 6683 | #else |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6684 | qemu_register_machine(&sun4m_machine); |
| 6685 | #endif |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 6686 | #elif defined(TARGET_ARM) |
pbrook | 40f137e | 2006-02-20 00:33:36 +0000 | [diff] [blame] | 6687 | qemu_register_machine(&integratorcp926_machine); |
| 6688 | qemu_register_machine(&integratorcp1026_machine); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 6689 | qemu_register_machine(&versatilepb_machine); |
pbrook | 1640695 | 2006-04-27 23:15:07 +0000 | [diff] [blame] | 6690 | qemu_register_machine(&versatileab_machine); |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 6691 | qemu_register_machine(&realview_machine); |
bellard | 27c7ca7 | 2006-04-27 21:32:09 +0000 | [diff] [blame] | 6692 | #elif defined(TARGET_SH4) |
| 6693 | qemu_register_machine(&shix_machine); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 6694 | #else |
| 6695 | #error unsupported CPU |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6696 | #endif |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6697 | } |
| 6698 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6699 | #ifdef HAS_AUDIO |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6700 | struct soundhw soundhw[] = { |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 6701 | #ifdef TARGET_I386 |
| 6702 | { |
| 6703 | "pcspk", |
| 6704 | "PC speaker", |
| 6705 | 0, |
| 6706 | 1, |
| 6707 | { .init_isa = pcspk_audio_init } |
| 6708 | }, |
| 6709 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6710 | { |
| 6711 | "sb16", |
| 6712 | "Creative Sound Blaster 16", |
| 6713 | 0, |
| 6714 | 1, |
| 6715 | { .init_isa = SB16_init } |
| 6716 | }, |
| 6717 | |
| 6718 | #ifdef CONFIG_ADLIB |
| 6719 | { |
| 6720 | "adlib", |
| 6721 | #ifdef HAS_YMF262 |
| 6722 | "Yamaha YMF262 (OPL3)", |
| 6723 | #else |
| 6724 | "Yamaha YM3812 (OPL2)", |
| 6725 | #endif |
| 6726 | 0, |
| 6727 | 1, |
| 6728 | { .init_isa = Adlib_init } |
| 6729 | }, |
| 6730 | #endif |
| 6731 | |
| 6732 | #ifdef CONFIG_GUS |
| 6733 | { |
| 6734 | "gus", |
| 6735 | "Gravis Ultrasound GF1", |
| 6736 | 0, |
| 6737 | 1, |
| 6738 | { .init_isa = GUS_init } |
| 6739 | }, |
| 6740 | #endif |
| 6741 | |
| 6742 | { |
| 6743 | "es1370", |
| 6744 | "ENSONIQ AudioPCI ES1370", |
| 6745 | 0, |
| 6746 | 0, |
| 6747 | { .init_pci = es1370_init } |
| 6748 | }, |
| 6749 | |
| 6750 | { NULL, NULL, 0, 0, { NULL } } |
| 6751 | }; |
| 6752 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6753 | static void select_soundhw (const char *optarg) |
| 6754 | { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6755 | struct soundhw *c; |
| 6756 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6757 | if (*optarg == '?') { |
| 6758 | show_valid_cards: |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6759 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6760 | printf ("Valid sound card names (comma separated):\n"); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6761 | for (c = soundhw; c->name; ++c) { |
| 6762 | printf ("%-11s %s\n", c->name, c->descr); |
| 6763 | } |
| 6764 | printf ("\n-soundhw all will enable all of the above\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6765 | exit (*optarg != '?'); |
| 6766 | } |
| 6767 | else { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6768 | size_t l; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6769 | const char *p; |
| 6770 | char *e; |
| 6771 | int bad_card = 0; |
| 6772 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6773 | if (!strcmp (optarg, "all")) { |
| 6774 | for (c = soundhw; c->name; ++c) { |
| 6775 | c->enabled = 1; |
| 6776 | } |
| 6777 | return; |
| 6778 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6779 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6780 | p = optarg; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6781 | while (*p) { |
| 6782 | e = strchr (p, ','); |
| 6783 | l = !e ? strlen (p) : (size_t) (e - p); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6784 | |
| 6785 | for (c = soundhw; c->name; ++c) { |
| 6786 | if (!strncmp (c->name, p, l)) { |
| 6787 | c->enabled = 1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6788 | break; |
| 6789 | } |
| 6790 | } |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6791 | |
| 6792 | if (!c->name) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6793 | if (l > 80) { |
| 6794 | fprintf (stderr, |
| 6795 | "Unknown sound card name (too big to show)\n"); |
| 6796 | } |
| 6797 | else { |
| 6798 | fprintf (stderr, "Unknown sound card name `%.*s'\n", |
| 6799 | (int) l, p); |
| 6800 | } |
| 6801 | bad_card = 1; |
| 6802 | } |
| 6803 | p += l + (e != NULL); |
| 6804 | } |
| 6805 | |
| 6806 | if (bad_card) |
| 6807 | goto show_valid_cards; |
| 6808 | } |
| 6809 | } |
| 6810 | #endif |
| 6811 | |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 6812 | #ifdef _WIN32 |
| 6813 | static BOOL WINAPI qemu_ctrl_handler(DWORD type) |
| 6814 | { |
| 6815 | exit(STATUS_CONTROL_C_EXIT); |
| 6816 | return TRUE; |
| 6817 | } |
| 6818 | #endif |
| 6819 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6820 | #define MAX_NET_CLIENTS 32 |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 6821 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6822 | int main(int argc, char **argv) |
| 6823 | { |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6824 | #ifdef CONFIG_GDBSTUB |
bellard | c636bb6 | 2007-02-05 20:46:05 +0000 | [diff] [blame] | 6825 | int use_gdbstub, gdbstub_port; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6826 | #endif |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6827 | int i, cdrom_index; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 6828 | int snapshot, linux_boot; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 6829 | const char *initrd_filename; |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 6830 | const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD]; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6831 | const char *kernel_filename, *kernel_cmdline; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 6832 | DisplayState *ds = &display_state; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 6833 | int cyls, heads, secs, translation; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6834 | char net_clients[MAX_NET_CLIENTS][256]; |
| 6835 | int nb_net_clients; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6836 | int optind; |
| 6837 | const char *r, *optarg; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6838 | CharDriverState *monitor_hd; |
| 6839 | char monitor_device[128]; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 6840 | char serial_devices[MAX_SERIAL_PORTS][128]; |
| 6841 | int serial_device_index; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6842 | char parallel_devices[MAX_PARALLEL_PORTS][128]; |
| 6843 | int parallel_device_index; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6844 | const char *loadvm = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6845 | QEMUMachine *machine; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 6846 | char usb_devices[MAX_USB_CMDLINE][128]; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6847 | int usb_devices_index; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6848 | int fds[2]; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6849 | |
| 6850 | LIST_INIT (&vm_change_state_head); |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 6851 | #ifndef _WIN32 |
| 6852 | { |
| 6853 | struct sigaction act; |
| 6854 | sigfillset(&act.sa_mask); |
| 6855 | act.sa_flags = 0; |
| 6856 | act.sa_handler = SIG_IGN; |
| 6857 | sigaction(SIGPIPE, &act, NULL); |
| 6858 | } |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 6859 | #else |
| 6860 | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 6861 | /* Note: cpu_interrupt() is currently not SMP safe, so we force |
| 6862 | QEMU to run on a single CPU */ |
| 6863 | { |
| 6864 | HANDLE h; |
| 6865 | DWORD mask, smask; |
| 6866 | int i; |
| 6867 | h = GetCurrentProcess(); |
| 6868 | if (GetProcessAffinityMask(h, &mask, &smask)) { |
| 6869 | for(i = 0; i < 32; i++) { |
| 6870 | if (mask & (1 << i)) |
| 6871 | break; |
| 6872 | } |
| 6873 | if (i != 32) { |
| 6874 | mask = 1 << i; |
| 6875 | SetProcessAffinityMask(h, mask); |
| 6876 | } |
| 6877 | } |
| 6878 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6879 | #endif |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 6880 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6881 | register_machines(); |
| 6882 | machine = first_machine; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6883 | initrd_filename = NULL; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 6884 | for(i = 0; i < MAX_FD; i++) |
| 6885 | fd_filename[i] = NULL; |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 6886 | for(i = 0; i < MAX_DISKS; i++) |
| 6887 | hd_filename[i] = NULL; |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6888 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 6889 | vga_ram_size = VGA_RAM_SIZE; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6890 | #ifdef CONFIG_GDBSTUB |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6891 | use_gdbstub = 0; |
bellard | c636bb6 | 2007-02-05 20:46:05 +0000 | [diff] [blame] | 6892 | gdbstub_port = DEFAULT_GDBSTUB_PORT; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6893 | #endif |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 6894 | snapshot = 0; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6895 | nographic = 0; |
| 6896 | kernel_filename = NULL; |
| 6897 | kernel_cmdline = ""; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6898 | #ifdef TARGET_PPC |
| 6899 | cdrom_index = 1; |
| 6900 | #else |
| 6901 | cdrom_index = 2; |
| 6902 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6903 | cyls = heads = secs = 0; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 6904 | translation = BIOS_ATA_TRANSLATION_AUTO; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6905 | pstrcpy(monitor_device, sizeof(monitor_device), "vc"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6906 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 6907 | pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc"); |
| 6908 | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
| 6909 | serial_devices[i][0] = '\0'; |
| 6910 | serial_device_index = 0; |
| 6911 | |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6912 | pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc"); |
| 6913 | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
| 6914 | parallel_devices[i][0] = '\0'; |
| 6915 | parallel_device_index = 0; |
| 6916 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6917 | usb_devices_index = 0; |
| 6918 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6919 | nb_net_clients = 0; |
| 6920 | |
| 6921 | nb_nics = 0; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 6922 | /* default mac address of the first network interface */ |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6923 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6924 | optind = 1; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6925 | for(;;) { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6926 | if (optind >= argc) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6927 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6928 | r = argv[optind]; |
| 6929 | if (r[0] != '-') { |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 6930 | hd_filename[0] = argv[optind++]; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6931 | } else { |
| 6932 | const QEMUOption *popt; |
| 6933 | |
| 6934 | optind++; |
pbrook | dff5efc | 2007-01-27 17:19:39 +0000 | [diff] [blame] | 6935 | /* Treat --foo the same as -foo. */ |
| 6936 | if (r[1] == '-') |
| 6937 | r++; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6938 | popt = qemu_options; |
| 6939 | for(;;) { |
| 6940 | if (!popt->name) { |
| 6941 | fprintf(stderr, "%s: invalid option -- '%s'\n", |
| 6942 | argv[0], r); |
| 6943 | exit(1); |
| 6944 | } |
| 6945 | if (!strcmp(popt->name, r + 1)) |
| 6946 | break; |
| 6947 | popt++; |
| 6948 | } |
| 6949 | if (popt->flags & HAS_ARG) { |
| 6950 | if (optind >= argc) { |
| 6951 | fprintf(stderr, "%s: option '%s' requires an argument\n", |
| 6952 | argv[0], r); |
| 6953 | exit(1); |
| 6954 | } |
| 6955 | optarg = argv[optind++]; |
| 6956 | } else { |
| 6957 | optarg = NULL; |
| 6958 | } |
| 6959 | |
| 6960 | switch(popt->index) { |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6961 | case QEMU_OPTION_M: |
| 6962 | machine = find_machine(optarg); |
| 6963 | if (!machine) { |
| 6964 | QEMUMachine *m; |
| 6965 | printf("Supported machines are:\n"); |
| 6966 | for(m = first_machine; m != NULL; m = m->next) { |
| 6967 | printf("%-10s %s%s\n", |
| 6968 | m->name, m->desc, |
| 6969 | m == first_machine ? " (default)" : ""); |
| 6970 | } |
| 6971 | exit(1); |
| 6972 | } |
| 6973 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6974 | case QEMU_OPTION_initrd: |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6975 | initrd_filename = optarg; |
| 6976 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6977 | case QEMU_OPTION_hda: |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6978 | case QEMU_OPTION_hdb: |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6979 | case QEMU_OPTION_hdc: |
| 6980 | case QEMU_OPTION_hdd: |
| 6981 | { |
| 6982 | int hd_index; |
| 6983 | hd_index = popt->index - QEMU_OPTION_hda; |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 6984 | hd_filename[hd_index] = optarg; |
| 6985 | if (hd_index == cdrom_index) |
| 6986 | cdrom_index = -1; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6987 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6988 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6989 | case QEMU_OPTION_snapshot: |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 6990 | snapshot = 1; |
| 6991 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6992 | case QEMU_OPTION_hdachs: |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 6993 | { |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 6994 | const char *p; |
| 6995 | p = optarg; |
| 6996 | cyls = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 6997 | if (cyls < 1 || cyls > 16383) |
| 6998 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 6999 | if (*p != ',') |
| 7000 | goto chs_fail; |
| 7001 | p++; |
| 7002 | heads = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7003 | if (heads < 1 || heads > 16) |
| 7004 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7005 | if (*p != ',') |
| 7006 | goto chs_fail; |
| 7007 | p++; |
| 7008 | secs = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7009 | if (secs < 1 || secs > 63) |
| 7010 | goto chs_fail; |
| 7011 | if (*p == ',') { |
| 7012 | p++; |
| 7013 | if (!strcmp(p, "none")) |
| 7014 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 7015 | else if (!strcmp(p, "lba")) |
| 7016 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 7017 | else if (!strcmp(p, "auto")) |
| 7018 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 7019 | else |
| 7020 | goto chs_fail; |
| 7021 | } else if (*p != '\0') { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7022 | chs_fail: |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 7023 | fprintf(stderr, "qemu: invalid physical CHS format\n"); |
| 7024 | exit(1); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7025 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7026 | } |
| 7027 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7028 | case QEMU_OPTION_nographic: |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7029 | pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio"); |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame^] | 7030 | pstrcpy(monitor_device, sizeof(monitor_device), "stdio"); |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7031 | nographic = 1; |
| 7032 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7033 | case QEMU_OPTION_kernel: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7034 | kernel_filename = optarg; |
| 7035 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7036 | case QEMU_OPTION_append: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7037 | kernel_cmdline = optarg; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7038 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7039 | case QEMU_OPTION_cdrom: |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7040 | if (cdrom_index >= 0) { |
| 7041 | hd_filename[cdrom_index] = optarg; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7042 | } |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 7043 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7044 | case QEMU_OPTION_boot: |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 7045 | boot_device = optarg[0]; |
bellard | 9e89a4b | 2004-11-14 15:42:27 +0000 | [diff] [blame] | 7046 | if (boot_device != 'a' && |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 7047 | #if defined(TARGET_SPARC) || defined(TARGET_I386) |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 7048 | // Network boot |
| 7049 | boot_device != 'n' && |
| 7050 | #endif |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 7051 | boot_device != 'c' && boot_device != 'd') { |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 7052 | fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device); |
| 7053 | exit(1); |
| 7054 | } |
| 7055 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7056 | case QEMU_OPTION_fda: |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 7057 | fd_filename[0] = optarg; |
| 7058 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7059 | case QEMU_OPTION_fdb: |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 7060 | fd_filename[1] = optarg; |
| 7061 | break; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 7062 | #ifdef TARGET_I386 |
| 7063 | case QEMU_OPTION_no_fd_bootchk: |
| 7064 | fd_bootchk = 0; |
| 7065 | break; |
| 7066 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7067 | case QEMU_OPTION_no_code_copy: |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 7068 | code_copy_enabled = 0; |
| 7069 | break; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7070 | case QEMU_OPTION_net: |
| 7071 | if (nb_net_clients >= MAX_NET_CLIENTS) { |
| 7072 | fprintf(stderr, "qemu: too many network clients\n"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7073 | exit(1); |
| 7074 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7075 | pstrcpy(net_clients[nb_net_clients], |
| 7076 | sizeof(net_clients[0]), |
| 7077 | optarg); |
| 7078 | nb_net_clients++; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 7079 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 7080 | #ifdef CONFIG_SLIRP |
| 7081 | case QEMU_OPTION_tftp: |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 7082 | tftp_prefix = optarg; |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 7083 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 7084 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 7085 | case QEMU_OPTION_smb: |
| 7086 | net_slirp_smb(optarg); |
| 7087 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 7088 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 7089 | case QEMU_OPTION_redir: |
| 7090 | net_slirp_redir(optarg); |
| 7091 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 7092 | #endif |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7093 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 7094 | case QEMU_OPTION_audio_help: |
| 7095 | AUD_help (); |
| 7096 | exit (0); |
| 7097 | break; |
| 7098 | case QEMU_OPTION_soundhw: |
| 7099 | select_soundhw (optarg); |
| 7100 | break; |
| 7101 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7102 | case QEMU_OPTION_h: |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7103 | help(); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7104 | break; |
| 7105 | case QEMU_OPTION_m: |
| 7106 | ram_size = atoi(optarg) * 1024 * 1024; |
| 7107 | if (ram_size <= 0) |
| 7108 | help(); |
| 7109 | if (ram_size > PHYS_RAM_MAX_SIZE) { |
| 7110 | fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n", |
| 7111 | PHYS_RAM_MAX_SIZE / (1024 * 1024)); |
| 7112 | exit(1); |
| 7113 | } |
| 7114 | break; |
| 7115 | case QEMU_OPTION_d: |
| 7116 | { |
| 7117 | int mask; |
| 7118 | CPULogItem *item; |
| 7119 | |
| 7120 | mask = cpu_str_to_log_mask(optarg); |
| 7121 | if (!mask) { |
| 7122 | printf("Log items (comma separated):\n"); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 7123 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 7124 | printf("%-10s %s\n", item->name, item->help); |
| 7125 | } |
| 7126 | exit(1); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7127 | } |
| 7128 | cpu_set_log(mask); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 7129 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7130 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7131 | #ifdef CONFIG_GDBSTUB |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7132 | case QEMU_OPTION_s: |
| 7133 | use_gdbstub = 1; |
| 7134 | break; |
| 7135 | case QEMU_OPTION_p: |
bellard | c636bb6 | 2007-02-05 20:46:05 +0000 | [diff] [blame] | 7136 | gdbstub_port = atoi(optarg); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7137 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7138 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7139 | case QEMU_OPTION_L: |
| 7140 | bios_dir = optarg; |
| 7141 | break; |
| 7142 | case QEMU_OPTION_S: |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 7143 | autostart = 0; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7144 | break; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 7145 | case QEMU_OPTION_k: |
| 7146 | keyboard_layout = optarg; |
| 7147 | break; |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 7148 | case QEMU_OPTION_localtime: |
| 7149 | rtc_utc = 0; |
| 7150 | break; |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 7151 | case QEMU_OPTION_cirrusvga: |
| 7152 | cirrus_vga_enabled = 1; |
| 7153 | break; |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 7154 | case QEMU_OPTION_std_vga: |
| 7155 | cirrus_vga_enabled = 0; |
| 7156 | break; |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 7157 | case QEMU_OPTION_g: |
| 7158 | { |
| 7159 | const char *p; |
| 7160 | int w, h, depth; |
| 7161 | p = optarg; |
| 7162 | w = strtol(p, (char **)&p, 10); |
| 7163 | if (w <= 0) { |
| 7164 | graphic_error: |
| 7165 | fprintf(stderr, "qemu: invalid resolution or depth\n"); |
| 7166 | exit(1); |
| 7167 | } |
| 7168 | if (*p != 'x') |
| 7169 | goto graphic_error; |
| 7170 | p++; |
| 7171 | h = strtol(p, (char **)&p, 10); |
| 7172 | if (h <= 0) |
| 7173 | goto graphic_error; |
| 7174 | if (*p == 'x') { |
| 7175 | p++; |
| 7176 | depth = strtol(p, (char **)&p, 10); |
| 7177 | if (depth != 8 && depth != 15 && depth != 16 && |
| 7178 | depth != 24 && depth != 32) |
| 7179 | goto graphic_error; |
| 7180 | } else if (*p == '\0') { |
| 7181 | depth = graphic_depth; |
| 7182 | } else { |
| 7183 | goto graphic_error; |
| 7184 | } |
| 7185 | |
| 7186 | graphic_width = w; |
| 7187 | graphic_height = h; |
| 7188 | graphic_depth = depth; |
| 7189 | } |
| 7190 | break; |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame^] | 7191 | case QEMU_OPTION_echr: |
| 7192 | { |
| 7193 | char *r; |
| 7194 | term_escape_char = strtol(optarg, &r, 0); |
| 7195 | if (r == optarg) |
| 7196 | printf("Bad argument to echr\n"); |
| 7197 | break; |
| 7198 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7199 | case QEMU_OPTION_monitor: |
| 7200 | pstrcpy(monitor_device, sizeof(monitor_device), optarg); |
| 7201 | break; |
| 7202 | case QEMU_OPTION_serial: |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7203 | if (serial_device_index >= MAX_SERIAL_PORTS) { |
| 7204 | fprintf(stderr, "qemu: too many serial ports\n"); |
| 7205 | exit(1); |
| 7206 | } |
| 7207 | pstrcpy(serial_devices[serial_device_index], |
| 7208 | sizeof(serial_devices[0]), optarg); |
| 7209 | serial_device_index++; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7210 | break; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7211 | case QEMU_OPTION_parallel: |
| 7212 | if (parallel_device_index >= MAX_PARALLEL_PORTS) { |
| 7213 | fprintf(stderr, "qemu: too many parallel ports\n"); |
| 7214 | exit(1); |
| 7215 | } |
| 7216 | pstrcpy(parallel_devices[parallel_device_index], |
| 7217 | sizeof(parallel_devices[0]), optarg); |
| 7218 | parallel_device_index++; |
| 7219 | break; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7220 | case QEMU_OPTION_loadvm: |
| 7221 | loadvm = optarg; |
| 7222 | break; |
| 7223 | case QEMU_OPTION_full_screen: |
| 7224 | full_screen = 1; |
| 7225 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 7226 | #ifdef CONFIG_SDL |
| 7227 | case QEMU_OPTION_no_quit: |
| 7228 | no_quit = 1; |
| 7229 | break; |
| 7230 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 7231 | case QEMU_OPTION_pidfile: |
| 7232 | create_pidfile(optarg); |
| 7233 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 7234 | #ifdef TARGET_I386 |
| 7235 | case QEMU_OPTION_win2k_hack: |
| 7236 | win2k_install_hack = 1; |
| 7237 | break; |
| 7238 | #endif |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 7239 | #ifdef USE_KQEMU |
| 7240 | case QEMU_OPTION_no_kqemu: |
| 7241 | kqemu_allowed = 0; |
| 7242 | break; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 7243 | case QEMU_OPTION_kernel_kqemu: |
| 7244 | kqemu_allowed = 2; |
| 7245 | break; |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 7246 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 7247 | case QEMU_OPTION_usb: |
| 7248 | usb_enabled = 1; |
| 7249 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 7250 | case QEMU_OPTION_usbdevice: |
| 7251 | usb_enabled = 1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 7252 | if (usb_devices_index >= MAX_USB_CMDLINE) { |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 7253 | fprintf(stderr, "Too many USB devices\n"); |
| 7254 | exit(1); |
| 7255 | } |
| 7256 | pstrcpy(usb_devices[usb_devices_index], |
| 7257 | sizeof(usb_devices[usb_devices_index]), |
| 7258 | optarg); |
| 7259 | usb_devices_index++; |
| 7260 | break; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 7261 | case QEMU_OPTION_smp: |
| 7262 | smp_cpus = atoi(optarg); |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 7263 | if (smp_cpus < 1 || smp_cpus > MAX_CPUS) { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 7264 | fprintf(stderr, "Invalid number of CPUs\n"); |
| 7265 | exit(1); |
| 7266 | } |
| 7267 | break; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 7268 | case QEMU_OPTION_vnc: |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 7269 | vnc_display = optarg; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 7270 | break; |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 7271 | case QEMU_OPTION_no_acpi: |
| 7272 | acpi_enabled = 0; |
| 7273 | break; |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 7274 | case QEMU_OPTION_no_reboot: |
| 7275 | no_reboot = 1; |
| 7276 | break; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7277 | case QEMU_OPTION_daemonize: |
| 7278 | daemonize = 1; |
| 7279 | break; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 7280 | case QEMU_OPTION_option_rom: |
| 7281 | if (nb_option_roms >= MAX_OPTION_ROMS) { |
| 7282 | fprintf(stderr, "Too many option ROMs\n"); |
| 7283 | exit(1); |
| 7284 | } |
| 7285 | option_rom[nb_option_roms] = optarg; |
| 7286 | nb_option_roms++; |
| 7287 | break; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 7288 | case QEMU_OPTION_semihosting: |
| 7289 | semihosting_enabled = 1; |
| 7290 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7291 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7292 | } |
| 7293 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7294 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7295 | #ifndef _WIN32 |
| 7296 | if (daemonize && !nographic && vnc_display == NULL) { |
| 7297 | fprintf(stderr, "Can only daemonize if using -nographic or -vnc\n"); |
| 7298 | daemonize = 0; |
| 7299 | } |
| 7300 | |
| 7301 | if (daemonize) { |
| 7302 | pid_t pid; |
| 7303 | |
| 7304 | if (pipe(fds) == -1) |
| 7305 | exit(1); |
| 7306 | |
| 7307 | pid = fork(); |
| 7308 | if (pid > 0) { |
| 7309 | uint8_t status; |
| 7310 | ssize_t len; |
| 7311 | |
| 7312 | close(fds[1]); |
| 7313 | |
| 7314 | again: |
| 7315 | len = read(fds[0], &status, 1); |
| 7316 | if (len == -1 && (errno == EINTR)) |
| 7317 | goto again; |
| 7318 | |
| 7319 | if (len != 1 || status != 0) |
| 7320 | exit(1); |
| 7321 | else |
| 7322 | exit(0); |
| 7323 | } else if (pid < 0) |
| 7324 | exit(1); |
| 7325 | |
| 7326 | setsid(); |
| 7327 | |
| 7328 | pid = fork(); |
| 7329 | if (pid > 0) |
| 7330 | exit(0); |
| 7331 | else if (pid < 0) |
| 7332 | exit(1); |
| 7333 | |
| 7334 | umask(027); |
| 7335 | chdir("/"); |
| 7336 | |
| 7337 | signal(SIGTSTP, SIG_IGN); |
| 7338 | signal(SIGTTOU, SIG_IGN); |
| 7339 | signal(SIGTTIN, SIG_IGN); |
| 7340 | } |
| 7341 | #endif |
| 7342 | |
bellard | ff3fbb3 | 2006-01-08 10:53:14 +0000 | [diff] [blame] | 7343 | #ifdef USE_KQEMU |
| 7344 | if (smp_cpus > 1) |
| 7345 | kqemu_allowed = 0; |
| 7346 | #endif |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7347 | linux_boot = (kernel_filename != NULL); |
ths | 42550fd | 2006-12-22 16:34:12 +0000 | [diff] [blame] | 7348 | |
| 7349 | if (!linux_boot && |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7350 | hd_filename[0] == '\0' && |
| 7351 | (cdrom_index >= 0 && hd_filename[cdrom_index] == '\0') && |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 7352 | fd_filename[0] == '\0') |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7353 | help(); |
| 7354 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7355 | /* boot to floppy or the default cd if no hard disk defined yet */ |
| 7356 | if (hd_filename[0] == '\0' && boot_device == 'c') { |
| 7357 | if (fd_filename[0] != '\0') |
| 7358 | boot_device = 'a'; |
| 7359 | else |
| 7360 | boot_device = 'd'; |
| 7361 | } |
| 7362 | |
bellard | b118d61 | 2003-06-30 23:36:21 +0000 | [diff] [blame] | 7363 | setvbuf(stdout, NULL, _IOLBF, 0); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7364 | |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 7365 | init_timers(); |
| 7366 | init_timer_alarm(); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 7367 | qemu_aio_init(); |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 7368 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 7369 | #ifdef _WIN32 |
| 7370 | socket_init(); |
| 7371 | #endif |
| 7372 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7373 | /* init network clients */ |
| 7374 | if (nb_net_clients == 0) { |
| 7375 | /* if no clients, we use a default config */ |
| 7376 | pstrcpy(net_clients[0], sizeof(net_clients[0]), |
| 7377 | "nic"); |
| 7378 | pstrcpy(net_clients[1], sizeof(net_clients[0]), |
| 7379 | "user"); |
| 7380 | nb_net_clients = 2; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 7381 | } |
| 7382 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7383 | for(i = 0;i < nb_net_clients; i++) { |
| 7384 | if (net_client_init(net_clients[i]) < 0) |
| 7385 | exit(1); |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 7386 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 7387 | |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 7388 | #ifdef TARGET_I386 |
| 7389 | if (boot_device == 'n') { |
| 7390 | for (i = 0; i < nb_nics; i++) { |
| 7391 | const char *model = nd_table[i].model; |
| 7392 | char buf[1024]; |
| 7393 | if (model == NULL) |
| 7394 | model = "ne2k_pci"; |
| 7395 | snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model); |
| 7396 | if (get_image_size(buf) > 0) { |
| 7397 | option_rom[nb_option_roms] = strdup(buf); |
| 7398 | nb_option_roms++; |
| 7399 | break; |
| 7400 | } |
| 7401 | } |
| 7402 | if (i == nb_nics) { |
| 7403 | fprintf(stderr, "No valid PXE rom found for network device\n"); |
| 7404 | exit(1); |
| 7405 | } |
| 7406 | boot_device = 'c'; /* to prevent confusion by the BIOS */ |
| 7407 | } |
| 7408 | #endif |
| 7409 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7410 | /* init the memory */ |
bellard | 970ac5a | 2007-02-08 23:09:59 +0000 | [diff] [blame] | 7411 | phys_ram_size = ram_size + vga_ram_size + MAX_BIOS_SIZE; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 7412 | |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 7413 | phys_ram_base = qemu_vmalloc(phys_ram_size); |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 7414 | if (!phys_ram_base) { |
| 7415 | fprintf(stderr, "Could not allocate physical memory\n"); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7416 | exit(1); |
| 7417 | } |
| 7418 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7419 | /* we always create the cdrom drive, even if no disk is there */ |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7420 | bdrv_init(); |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7421 | if (cdrom_index >= 0) { |
| 7422 | bs_table[cdrom_index] = bdrv_new("cdrom"); |
| 7423 | bdrv_set_type_hint(bs_table[cdrom_index], BDRV_TYPE_CDROM); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7424 | } |
| 7425 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame] | 7426 | /* open the virtual block devices */ |
| 7427 | for(i = 0; i < MAX_DISKS; i++) { |
| 7428 | if (hd_filename[i]) { |
| 7429 | if (!bs_table[i]) { |
| 7430 | char buf[64]; |
| 7431 | snprintf(buf, sizeof(buf), "hd%c", i + 'a'); |
| 7432 | bs_table[i] = bdrv_new(buf); |
| 7433 | } |
| 7434 | if (bdrv_open(bs_table[i], hd_filename[i], snapshot ? BDRV_O_SNAPSHOT : 0) < 0) { |
| 7435 | fprintf(stderr, "qemu: could not open hard disk image '%s'\n", |
| 7436 | hd_filename[i]); |
| 7437 | exit(1); |
| 7438 | } |
| 7439 | if (i == 0 && cyls != 0) { |
| 7440 | bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs); |
| 7441 | bdrv_set_translation_hint(bs_table[i], translation); |
| 7442 | } |
| 7443 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7444 | } |
| 7445 | |
| 7446 | /* we always create at least one floppy disk */ |
| 7447 | fd_table[0] = bdrv_new("fda"); |
| 7448 | bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY); |
| 7449 | |
| 7450 | for(i = 0; i < MAX_FD; i++) { |
| 7451 | if (fd_filename[i]) { |
| 7452 | if (!fd_table[i]) { |
| 7453 | char buf[64]; |
| 7454 | snprintf(buf, sizeof(buf), "fd%c", i + 'a'); |
| 7455 | fd_table[i] = bdrv_new(buf); |
| 7456 | bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY); |
| 7457 | } |
| 7458 | if (fd_filename[i] != '\0') { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 7459 | if (bdrv_open(fd_table[i], fd_filename[i], |
| 7460 | snapshot ? BDRV_O_SNAPSHOT : 0) < 0) { |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 7461 | fprintf(stderr, "qemu: could not open floppy disk image '%s'\n", |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7462 | fd_filename[i]); |
| 7463 | exit(1); |
| 7464 | } |
| 7465 | } |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 7466 | } |
| 7467 | } |
| 7468 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 7469 | register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
| 7470 | register_savevm("ram", 0, 2, ram_save, ram_load, NULL); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7471 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7472 | init_ioports(); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7473 | |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7474 | /* terminal init */ |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7475 | if (nographic) { |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7476 | dumb_display_init(ds); |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 7477 | } else if (vnc_display != NULL) { |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 7478 | vnc_display_init(ds, vnc_display); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7479 | } else { |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 7480 | #if defined(CONFIG_SDL) |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7481 | sdl_display_init(ds, full_screen); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 7482 | #elif defined(CONFIG_COCOA) |
| 7483 | cocoa_display_init(ds, full_screen); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7484 | #else |
| 7485 | dumb_display_init(ds); |
| 7486 | #endif |
| 7487 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7488 | |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame^] | 7489 | /* Maintain compatibility with multiple stdio monitors */ |
| 7490 | if (!strcmp(monitor_device,"stdio")) { |
| 7491 | for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
| 7492 | if (!strcmp(serial_devices[i],"mon:stdio")) { |
| 7493 | monitor_device[0] = '\0'; |
| 7494 | break; |
| 7495 | } else if (!strcmp(serial_devices[i],"stdio")) { |
| 7496 | monitor_device[0] = '\0'; |
| 7497 | pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "mon:stdio"); |
| 7498 | break; |
| 7499 | } |
| 7500 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7501 | } |
ths | 20d8a3e | 2007-02-18 17:04:49 +0000 | [diff] [blame^] | 7502 | if (monitor_device[0] != '\0') { |
| 7503 | monitor_hd = qemu_chr_open(monitor_device); |
| 7504 | if (!monitor_hd) { |
| 7505 | fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device); |
| 7506 | exit(1); |
| 7507 | } |
| 7508 | monitor_init(monitor_hd, !nographic); |
| 7509 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7510 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7511 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7512 | const char *devname = serial_devices[i]; |
| 7513 | if (devname[0] != '\0' && strcmp(devname, "none")) { |
| 7514 | serial_hds[i] = qemu_chr_open(devname); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7515 | if (!serial_hds[i]) { |
| 7516 | fprintf(stderr, "qemu: could not open serial device '%s'\n", |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7517 | devname); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7518 | exit(1); |
| 7519 | } |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7520 | if (!strcmp(devname, "vc")) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 7521 | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7522 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7523 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7524 | |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7525 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7526 | const char *devname = parallel_devices[i]; |
| 7527 | if (devname[0] != '\0' && strcmp(devname, "none")) { |
| 7528 | parallel_hds[i] = qemu_chr_open(devname); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7529 | if (!parallel_hds[i]) { |
| 7530 | fprintf(stderr, "qemu: could not open parallel device '%s'\n", |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7531 | devname); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7532 | exit(1); |
| 7533 | } |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7534 | if (!strcmp(devname, "vc")) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 7535 | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7536 | } |
| 7537 | } |
| 7538 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7539 | machine->init(ram_size, vga_ram_size, boot_device, |
| 7540 | ds, fd_filename, snapshot, |
| 7541 | kernel_filename, kernel_cmdline, initrd_filename); |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 7542 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 7543 | /* init USB devices */ |
| 7544 | if (usb_enabled) { |
| 7545 | for(i = 0; i < usb_devices_index; i++) { |
| 7546 | if (usb_device_add(usb_devices[i]) < 0) { |
| 7547 | fprintf(stderr, "Warning: could not add USB device %s\n", |
| 7548 | usb_devices[i]); |
| 7549 | } |
| 7550 | } |
| 7551 | } |
| 7552 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7553 | gui_timer = qemu_new_timer(rt_clock, gui_update, NULL); |
| 7554 | qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock)); |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 7555 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7556 | #ifdef CONFIG_GDBSTUB |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 7557 | if (use_gdbstub) { |
bellard | c636bb6 | 2007-02-05 20:46:05 +0000 | [diff] [blame] | 7558 | /* XXX: use standard host:port notation and modify options |
| 7559 | accordingly. */ |
| 7560 | if (gdbserver_start_port(gdbstub_port) < 0) { |
| 7561 | fprintf(stderr, "qemu: could not open gdbstub device on port '%d'\n", |
| 7562 | gdbstub_port); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7563 | exit(1); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7564 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7565 | } else |
| 7566 | #endif |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7567 | if (loadvm) |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 7568 | do_loadvm(loadvm); |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7569 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7570 | { |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7571 | /* XXX: simplify init */ |
| 7572 | read_passwords(); |
pbrook | 3c07f8e | 2007-01-21 16:47:01 +0000 | [diff] [blame] | 7573 | if (autostart) { |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7574 | vm_start(); |
| 7575 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7576 | } |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 7577 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7578 | if (daemonize) { |
| 7579 | uint8_t status = 0; |
| 7580 | ssize_t len; |
| 7581 | int fd; |
| 7582 | |
| 7583 | again1: |
| 7584 | len = write(fds[1], &status, 1); |
| 7585 | if (len == -1 && (errno == EINTR)) |
| 7586 | goto again1; |
| 7587 | |
| 7588 | if (len != 1) |
| 7589 | exit(1); |
| 7590 | |
| 7591 | fd = open("/dev/null", O_RDWR); |
| 7592 | if (fd == -1) |
| 7593 | exit(1); |
| 7594 | |
| 7595 | dup2(fd, 0); |
| 7596 | dup2(fd, 1); |
| 7597 | dup2(fd, 2); |
| 7598 | |
| 7599 | close(fd); |
| 7600 | } |
| 7601 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7602 | main_loop(); |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 7603 | quit_timers(); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7604 | return 0; |
| 7605 | } |