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 | fb43f4d | 2006-08-07 21:34:46 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2006 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> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 58 | #endif |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 59 | #endif |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 60 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 61 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 62 | #if defined(CONFIG_SLIRP) |
| 63 | #include "libslirp.h" |
| 64 | #endif |
| 65 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 66 | #ifdef _WIN32 |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 67 | #include <malloc.h> |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 68 | #include <sys/timeb.h> |
| 69 | #include <windows.h> |
| 70 | #define getopt_long_only getopt_long |
| 71 | #define memalign(align, size) malloc(size) |
| 72 | #endif |
| 73 | |
bellard | 6ca957f | 2006-04-30 22:53:25 +0000 | [diff] [blame] | 74 | #include "qemu_socket.h" |
| 75 | |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 76 | #ifdef CONFIG_SDL |
bellard | 96bcd4f | 2004-07-10 16:26:15 +0000 | [diff] [blame] | 77 | #ifdef __APPLE__ |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 78 | #include <SDL/SDL.h> |
bellard | 96bcd4f | 2004-07-10 16:26:15 +0000 | [diff] [blame] | 79 | #endif |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 80 | #endif /* CONFIG_SDL */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 81 | |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 82 | #ifdef CONFIG_COCOA |
| 83 | #undef main |
| 84 | #define main qemu_main |
| 85 | #endif /* CONFIG_COCOA */ |
| 86 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 87 | #include "disas.h" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 88 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 89 | #include "exec-all.h" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 90 | |
bellard | 5a67135 | 2003-10-01 00:13:48 +0000 | [diff] [blame] | 91 | #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
pbrook | a14d6c8 | 2006-12-23 15:37:33 +0000 | [diff] [blame] | 92 | #ifdef __sun__ |
| 93 | #define SMBD_COMMAND "/usr/sfw/sbin/smbd" |
| 94 | #else |
| 95 | #define SMBD_COMMAND "/usr/sbin/smbd" |
| 96 | #endif |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 97 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 98 | //#define DEBUG_UNUSED_IOPORT |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 99 | //#define DEBUG_IOPORT |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 100 | |
bellard | bb551fa | 2004-01-24 13:42:26 +0000 | [diff] [blame] | 101 | #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024) |
bellard | 7916e22 | 2003-07-01 16:27:45 +0000 | [diff] [blame] | 102 | |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 103 | #ifdef TARGET_PPC |
| 104 | #define DEFAULT_RAM_SIZE 144 |
| 105 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 106 | #define DEFAULT_RAM_SIZE 128 |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 107 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 108 | /* in ms */ |
| 109 | #define GUI_REFRESH_INTERVAL 30 |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 110 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 111 | /* Max number of USB devices that can be specified on the commandline. */ |
| 112 | #define MAX_USB_CMDLINE 8 |
| 113 | |
bellard | 7dea1da | 2003-11-16 15:59:30 +0000 | [diff] [blame] | 114 | /* XXX: use a two level table to limit memory usage */ |
| 115 | #define MAX_IOPORTS 65536 |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 116 | |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 117 | const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 118 | char phys_ram_file[1024]; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 119 | void *ioport_opaque[MAX_IOPORTS]; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 120 | IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS]; |
| 121 | IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS]; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 122 | /* Note: bs_table[MAX_DISKS] is a dummy block driver if none available |
| 123 | to store the VM snapshots */ |
| 124 | BlockDriverState *bs_table[MAX_DISKS + 1], *fd_table[MAX_FD]; |
| 125 | /* point to the block driver where the snapshots are managed */ |
| 126 | BlockDriverState *bs_snapshots; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 127 | int vga_ram_size; |
bellard | 0ced658 | 2004-05-23 21:06:12 +0000 | [diff] [blame] | 128 | int bios_size; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 129 | static DisplayState display_state; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 130 | int nographic; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 131 | const char* keyboard_layout = NULL; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 132 | int64_t ticks_per_sec; |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 133 | int boot_device = 'c'; |
bellard | 0ced658 | 2004-05-23 21:06:12 +0000 | [diff] [blame] | 134 | int ram_size; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 135 | int pit_min_timer_count = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 136 | int nb_nics; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 137 | NICInfo nd_table[MAX_NICS]; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 138 | QEMUTimer *gui_timer; |
| 139 | int vm_running; |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 140 | int rtc_utc = 1; |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 141 | int cirrus_vga_enabled = 1; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 142 | #ifdef TARGET_SPARC |
| 143 | int graphic_width = 1024; |
| 144 | int graphic_height = 768; |
| 145 | #else |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 146 | int graphic_width = 800; |
| 147 | int graphic_height = 600; |
bellard | d827220 | 2005-04-06 20:32:23 +0000 | [diff] [blame] | 148 | #endif |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 149 | int graphic_depth = 15; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 150 | int full_screen = 0; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 151 | int no_quit = 0; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 152 | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 153 | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 154 | #ifdef TARGET_I386 |
| 155 | int win2k_install_hack = 0; |
| 156 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 157 | int usb_enabled = 0; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 158 | static VLANState *first_vlan; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 159 | int smp_cpus = 1; |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 160 | const char *vnc_display; |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 161 | #if defined(TARGET_SPARC) |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 162 | #define MAX_CPUS 16 |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 163 | #elif defined(TARGET_I386) |
| 164 | #define MAX_CPUS 255 |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 165 | #else |
bellard | d3e9db9 | 2005-12-17 01:27:28 +0000 | [diff] [blame] | 166 | #define MAX_CPUS 1 |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 167 | #endif |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 168 | int acpi_enabled = 1; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 169 | int fd_bootchk = 1; |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 170 | int no_reboot = 0; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 171 | int daemonize = 0; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 172 | const char *option_rom[MAX_OPTION_ROMS]; |
| 173 | int nb_option_roms; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 174 | |
| 175 | /***********************************************************/ |
bellard | 26aa7d7 | 2004-04-28 22:26:05 +0000 | [diff] [blame] | 176 | /* x86 ISA bus support */ |
| 177 | |
| 178 | target_phys_addr_t isa_mem_base = 0; |
bellard | 3de388f | 2005-07-02 18:11:44 +0000 | [diff] [blame] | 179 | PicState2 *isa_pic; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 180 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 181 | uint32_t default_ioport_readb(void *opaque, uint32_t address) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 182 | { |
| 183 | #ifdef DEBUG_UNUSED_IOPORT |
| 184 | fprintf(stderr, "inb: port=0x%04x\n", address); |
| 185 | #endif |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 186 | return 0xff; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 187 | } |
| 188 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 189 | void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 190 | { |
| 191 | #ifdef DEBUG_UNUSED_IOPORT |
| 192 | fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data); |
| 193 | #endif |
| 194 | } |
| 195 | |
| 196 | /* default is to make two byte accesses */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 197 | uint32_t default_ioport_readw(void *opaque, uint32_t address) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 198 | { |
| 199 | uint32_t data; |
bellard | db45c29 | 2004-05-12 19:50:26 +0000 | [diff] [blame] | 200 | data = ioport_read_table[0][address](ioport_opaque[address], address); |
| 201 | address = (address + 1) & (MAX_IOPORTS - 1); |
| 202 | data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 203 | return data; |
| 204 | } |
| 205 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 206 | void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 207 | { |
bellard | db45c29 | 2004-05-12 19:50:26 +0000 | [diff] [blame] | 208 | ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff); |
| 209 | address = (address + 1) & (MAX_IOPORTS - 1); |
| 210 | ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 211 | } |
| 212 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 213 | uint32_t default_ioport_readl(void *opaque, uint32_t address) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 214 | { |
| 215 | #ifdef DEBUG_UNUSED_IOPORT |
| 216 | fprintf(stderr, "inl: port=0x%04x\n", address); |
| 217 | #endif |
| 218 | return 0xffffffff; |
| 219 | } |
| 220 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 221 | void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 222 | { |
| 223 | #ifdef DEBUG_UNUSED_IOPORT |
| 224 | fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data); |
| 225 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void init_ioports(void) |
| 229 | { |
| 230 | int i; |
| 231 | |
| 232 | for(i = 0; i < MAX_IOPORTS; i++) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 233 | ioport_read_table[0][i] = default_ioport_readb; |
| 234 | ioport_write_table[0][i] = default_ioport_writeb; |
| 235 | ioport_read_table[1][i] = default_ioport_readw; |
| 236 | ioport_write_table[1][i] = default_ioport_writew; |
| 237 | ioport_read_table[2][i] = default_ioport_readl; |
| 238 | ioport_write_table[2][i] = default_ioport_writel; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 242 | /* size is the word size in byte */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 243 | int register_ioport_read(int start, int length, int size, |
| 244 | IOPortReadFunc *func, void *opaque) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 245 | { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 246 | int i, bsize; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 247 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 248 | if (size == 1) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 249 | bsize = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 250 | } else if (size == 2) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 251 | bsize = 1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 252 | } else if (size == 4) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 253 | bsize = 2; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 254 | } else { |
| 255 | hw_error("register_ioport_read: invalid size"); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 256 | return -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 257 | } |
| 258 | for(i = start; i < start + length; i += size) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 259 | ioport_read_table[bsize][i] = func; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 260 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
| 261 | hw_error("register_ioport_read: invalid opaque"); |
| 262 | ioport_opaque[i] = opaque; |
| 263 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 264 | return 0; |
| 265 | } |
| 266 | |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 267 | /* size is the word size in byte */ |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 268 | int register_ioport_write(int start, int length, int size, |
| 269 | IOPortWriteFunc *func, void *opaque) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 270 | { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 271 | int i, bsize; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 272 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 273 | if (size == 1) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 274 | bsize = 0; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 275 | } else if (size == 2) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 276 | bsize = 1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 277 | } else if (size == 4) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 278 | bsize = 2; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 279 | } else { |
| 280 | hw_error("register_ioport_write: invalid size"); |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 281 | return -1; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 282 | } |
| 283 | for(i = start; i < start + length; i += size) { |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 284 | ioport_write_table[bsize][i] = func; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 285 | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 286 | hw_error("register_ioport_write: invalid opaque"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 287 | ioport_opaque[i] = opaque; |
| 288 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
bellard | 69b9103 | 2004-05-18 23:05:28 +0000 | [diff] [blame] | 292 | void isa_unassign_ioport(int start, int length) |
| 293 | { |
| 294 | int i; |
| 295 | |
| 296 | for(i = start; i < start + length; i++) { |
| 297 | ioport_read_table[0][i] = default_ioport_readb; |
| 298 | ioport_read_table[1][i] = default_ioport_readw; |
| 299 | ioport_read_table[2][i] = default_ioport_readl; |
| 300 | |
| 301 | ioport_write_table[0][i] = default_ioport_writeb; |
| 302 | ioport_write_table[1][i] = default_ioport_writew; |
| 303 | ioport_write_table[2][i] = default_ioport_writel; |
| 304 | } |
| 305 | } |
| 306 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 307 | /***********************************************************/ |
| 308 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 309 | void pstrcpy(char *buf, int buf_size, const char *str) |
| 310 | { |
| 311 | int c; |
| 312 | char *q = buf; |
| 313 | |
| 314 | if (buf_size <= 0) |
| 315 | return; |
| 316 | |
| 317 | for(;;) { |
| 318 | c = *str++; |
| 319 | if (c == 0 || q >= buf + buf_size - 1) |
| 320 | break; |
| 321 | *q++ = c; |
| 322 | } |
| 323 | *q = '\0'; |
| 324 | } |
| 325 | |
| 326 | /* strcat and truncate. */ |
| 327 | char *pstrcat(char *buf, int buf_size, const char *s) |
| 328 | { |
| 329 | int len; |
| 330 | len = strlen(buf); |
| 331 | if (len < buf_size) |
| 332 | pstrcpy(buf + len, buf_size - len, s); |
| 333 | return buf; |
| 334 | } |
| 335 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 336 | int strstart(const char *str, const char *val, const char **ptr) |
| 337 | { |
| 338 | const char *p, *q; |
| 339 | p = str; |
| 340 | q = val; |
| 341 | while (*q != '\0') { |
| 342 | if (*p != *q) |
| 343 | return 0; |
| 344 | p++; |
| 345 | q++; |
| 346 | } |
| 347 | if (ptr) |
| 348 | *ptr = p; |
| 349 | return 1; |
| 350 | } |
| 351 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 352 | void cpu_outb(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, "outb: %04x %02x\n", addr, val); |
| 357 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 358 | ioport_write_table[0][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 | void cpu_outw(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 366 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 367 | #ifdef DEBUG_IOPORT |
| 368 | if (loglevel & CPU_LOG_IOPORT) |
| 369 | fprintf(logfile, "outw: %04x %04x\n", addr, val); |
| 370 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 371 | ioport_write_table[1][addr](ioport_opaque[addr], addr, val); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 372 | #ifdef USE_KQEMU |
| 373 | if (env) |
| 374 | env->last_io_time = cpu_get_time_fast(); |
| 375 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 376 | } |
| 377 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 378 | void cpu_outl(CPUState *env, int addr, int val) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 379 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 380 | #ifdef DEBUG_IOPORT |
| 381 | if (loglevel & CPU_LOG_IOPORT) |
| 382 | fprintf(logfile, "outl: %04x %08x\n", addr, val); |
| 383 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 384 | ioport_write_table[2][addr](ioport_opaque[addr], addr, val); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 385 | #ifdef USE_KQEMU |
| 386 | if (env) |
| 387 | env->last_io_time = cpu_get_time_fast(); |
| 388 | #endif |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 389 | } |
| 390 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 391 | int cpu_inb(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 392 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 393 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 394 | val = ioport_read_table[0][addr](ioport_opaque[addr], addr); |
| 395 | #ifdef DEBUG_IOPORT |
| 396 | if (loglevel & CPU_LOG_IOPORT) |
| 397 | fprintf(logfile, "inb : %04x %02x\n", addr, val); |
| 398 | #endif |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 399 | #ifdef USE_KQEMU |
| 400 | if (env) |
| 401 | env->last_io_time = cpu_get_time_fast(); |
| 402 | #endif |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 403 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 404 | } |
| 405 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 406 | int cpu_inw(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 407 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 408 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 409 | val = ioport_read_table[1][addr](ioport_opaque[addr], addr); |
| 410 | #ifdef DEBUG_IOPORT |
| 411 | if (loglevel & CPU_LOG_IOPORT) |
| 412 | fprintf(logfile, "inw : %04x %04x\n", addr, val); |
| 413 | #endif |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 414 | #ifdef USE_KQEMU |
| 415 | if (env) |
| 416 | env->last_io_time = cpu_get_time_fast(); |
| 417 | #endif |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 418 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 419 | } |
| 420 | |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 421 | int cpu_inl(CPUState *env, int addr) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 422 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 423 | int val; |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 424 | val = ioport_read_table[2][addr](ioport_opaque[addr], addr); |
| 425 | #ifdef DEBUG_IOPORT |
| 426 | if (loglevel & CPU_LOG_IOPORT) |
| 427 | fprintf(logfile, "inl : %04x %08x\n", addr, val); |
| 428 | #endif |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 429 | #ifdef USE_KQEMU |
| 430 | if (env) |
| 431 | env->last_io_time = cpu_get_time_fast(); |
| 432 | #endif |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 433 | return val; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /***********************************************************/ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 437 | void hw_error(const char *fmt, ...) |
| 438 | { |
| 439 | va_list ap; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 440 | CPUState *env; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 441 | |
| 442 | va_start(ap, fmt); |
| 443 | fprintf(stderr, "qemu: hardware error: "); |
| 444 | vfprintf(stderr, fmt, ap); |
| 445 | fprintf(stderr, "\n"); |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 446 | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
| 447 | fprintf(stderr, "CPU #%d:\n", env->cpu_index); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 448 | #ifdef TARGET_I386 |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 449 | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 450 | #else |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 451 | cpu_dump_state(env, stderr, fprintf, 0); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 452 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 453 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 454 | va_end(ap); |
| 455 | abort(); |
| 456 | } |
| 457 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 458 | /***********************************************************/ |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 459 | /* keyboard/mouse */ |
| 460 | |
| 461 | static QEMUPutKBDEvent *qemu_put_kbd_event; |
| 462 | static void *qemu_put_kbd_event_opaque; |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 463 | static QEMUPutMouseEntry *qemu_put_mouse_event_head; |
| 464 | static QEMUPutMouseEntry *qemu_put_mouse_event_current; |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 465 | |
| 466 | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
| 467 | { |
| 468 | qemu_put_kbd_event_opaque = opaque; |
| 469 | qemu_put_kbd_event = func; |
| 470 | } |
| 471 | |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 472 | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
| 473 | void *opaque, int absolute, |
| 474 | const char *name) |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 475 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 476 | QEMUPutMouseEntry *s, *cursor; |
| 477 | |
| 478 | s = qemu_mallocz(sizeof(QEMUPutMouseEntry)); |
| 479 | if (!s) |
| 480 | return NULL; |
| 481 | |
| 482 | s->qemu_put_mouse_event = func; |
| 483 | s->qemu_put_mouse_event_opaque = opaque; |
| 484 | s->qemu_put_mouse_event_absolute = absolute; |
| 485 | s->qemu_put_mouse_event_name = qemu_strdup(name); |
| 486 | s->next = NULL; |
| 487 | |
| 488 | if (!qemu_put_mouse_event_head) { |
| 489 | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
| 490 | return s; |
| 491 | } |
| 492 | |
| 493 | cursor = qemu_put_mouse_event_head; |
| 494 | while (cursor->next != NULL) |
| 495 | cursor = cursor->next; |
| 496 | |
| 497 | cursor->next = s; |
| 498 | qemu_put_mouse_event_current = s; |
| 499 | |
| 500 | return s; |
| 501 | } |
| 502 | |
| 503 | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry) |
| 504 | { |
| 505 | QEMUPutMouseEntry *prev = NULL, *cursor; |
| 506 | |
| 507 | if (!qemu_put_mouse_event_head || entry == NULL) |
| 508 | return; |
| 509 | |
| 510 | cursor = qemu_put_mouse_event_head; |
| 511 | while (cursor != NULL && cursor != entry) { |
| 512 | prev = cursor; |
| 513 | cursor = cursor->next; |
| 514 | } |
| 515 | |
| 516 | if (cursor == NULL) // does not exist or list empty |
| 517 | return; |
| 518 | else if (prev == NULL) { // entry is head |
| 519 | qemu_put_mouse_event_head = cursor->next; |
| 520 | if (qemu_put_mouse_event_current == entry) |
| 521 | qemu_put_mouse_event_current = cursor->next; |
| 522 | qemu_free(entry->qemu_put_mouse_event_name); |
| 523 | qemu_free(entry); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | prev->next = entry->next; |
| 528 | |
| 529 | if (qemu_put_mouse_event_current == entry) |
| 530 | qemu_put_mouse_event_current = prev; |
| 531 | |
| 532 | qemu_free(entry->qemu_put_mouse_event_name); |
| 533 | qemu_free(entry); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | void kbd_put_keycode(int keycode) |
| 537 | { |
| 538 | if (qemu_put_kbd_event) { |
| 539 | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
| 544 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 545 | QEMUPutMouseEvent *mouse_event; |
| 546 | void *mouse_event_opaque; |
| 547 | |
| 548 | if (!qemu_put_mouse_event_current) { |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | mouse_event = |
| 553 | qemu_put_mouse_event_current->qemu_put_mouse_event; |
| 554 | mouse_event_opaque = |
| 555 | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
| 556 | |
| 557 | if (mouse_event) { |
| 558 | mouse_event(mouse_event_opaque, dx, dy, dz, buttons_state); |
bellard | 63066f4 | 2004-06-03 18:45:02 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 562 | int kbd_mouse_is_absolute(void) |
| 563 | { |
ths | 455204e | 2007-01-05 16:42:13 +0000 | [diff] [blame] | 564 | if (!qemu_put_mouse_event_current) |
| 565 | return 0; |
| 566 | |
| 567 | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute; |
| 568 | } |
| 569 | |
| 570 | void do_info_mice(void) |
| 571 | { |
| 572 | QEMUPutMouseEntry *cursor; |
| 573 | int index = 0; |
| 574 | |
| 575 | if (!qemu_put_mouse_event_head) { |
| 576 | term_printf("No mouse devices connected\n"); |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | term_printf("Mouse devices available:\n"); |
| 581 | cursor = qemu_put_mouse_event_head; |
| 582 | while (cursor != NULL) { |
| 583 | term_printf("%c Mouse #%d: %s\n", |
| 584 | (cursor == qemu_put_mouse_event_current ? '*' : ' '), |
| 585 | index, cursor->qemu_put_mouse_event_name); |
| 586 | index++; |
| 587 | cursor = cursor->next; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | void do_mouse_set(int index) |
| 592 | { |
| 593 | QEMUPutMouseEntry *cursor; |
| 594 | int i = 0; |
| 595 | |
| 596 | if (!qemu_put_mouse_event_head) { |
| 597 | term_printf("No mouse devices connected\n"); |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | cursor = qemu_put_mouse_event_head; |
| 602 | while (cursor != NULL && index != i) { |
| 603 | i++; |
| 604 | cursor = cursor->next; |
| 605 | } |
| 606 | |
| 607 | if (cursor != NULL) |
| 608 | qemu_put_mouse_event_current = cursor; |
| 609 | else |
| 610 | term_printf("Mouse at given index not found\n"); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 611 | } |
| 612 | |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 613 | /* compute with 96 bit intermediate result: (a*b)/c */ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 614 | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
bellard | 87858c8 | 2003-06-27 12:01:39 +0000 | [diff] [blame] | 615 | { |
| 616 | union { |
| 617 | uint64_t ll; |
| 618 | struct { |
| 619 | #ifdef WORDS_BIGENDIAN |
| 620 | uint32_t high, low; |
| 621 | #else |
| 622 | uint32_t low, high; |
| 623 | #endif |
| 624 | } l; |
| 625 | } u, res; |
| 626 | uint64_t rl, rh; |
| 627 | |
| 628 | u.ll = a; |
| 629 | rl = (uint64_t)u.l.low * (uint64_t)b; |
| 630 | rh = (uint64_t)u.l.high * (uint64_t)b; |
| 631 | rh += (rl >> 32); |
| 632 | res.l.high = rh / c; |
| 633 | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
| 634 | return res.ll; |
| 635 | } |
| 636 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 637 | /***********************************************************/ |
| 638 | /* real time host monotonic timer */ |
| 639 | |
| 640 | #define QEMU_TIMER_BASE 1000000000LL |
| 641 | |
| 642 | #ifdef WIN32 |
| 643 | |
| 644 | static int64_t clock_freq; |
| 645 | |
| 646 | static void init_get_clock(void) |
| 647 | { |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 648 | LARGE_INTEGER freq; |
| 649 | int ret; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 650 | ret = QueryPerformanceFrequency(&freq); |
| 651 | if (ret == 0) { |
| 652 | fprintf(stderr, "Could not calibrate ticks\n"); |
| 653 | exit(1); |
| 654 | } |
| 655 | clock_freq = freq.QuadPart; |
| 656 | } |
| 657 | |
| 658 | static int64_t get_clock(void) |
| 659 | { |
| 660 | LARGE_INTEGER ti; |
| 661 | QueryPerformanceCounter(&ti); |
| 662 | return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq); |
| 663 | } |
| 664 | |
| 665 | #else |
| 666 | |
| 667 | static int use_rt_clock; |
| 668 | |
| 669 | static void init_get_clock(void) |
| 670 | { |
| 671 | use_rt_clock = 0; |
| 672 | #if defined(__linux__) |
| 673 | { |
| 674 | struct timespec ts; |
| 675 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 676 | use_rt_clock = 1; |
| 677 | } |
| 678 | } |
| 679 | #endif |
| 680 | } |
| 681 | |
| 682 | static int64_t get_clock(void) |
| 683 | { |
| 684 | #if defined(__linux__) |
| 685 | if (use_rt_clock) { |
| 686 | struct timespec ts; |
| 687 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 688 | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
| 689 | } else |
| 690 | #endif |
| 691 | { |
| 692 | /* XXX: using gettimeofday leads to problems if the date |
| 693 | changes, so it should be avoided. */ |
| 694 | struct timeval tv; |
| 695 | gettimeofday(&tv, NULL); |
| 696 | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | #endif |
| 701 | |
| 702 | /***********************************************************/ |
| 703 | /* guest cycle counter */ |
| 704 | |
| 705 | static int64_t cpu_ticks_prev; |
| 706 | static int64_t cpu_ticks_offset; |
| 707 | static int64_t cpu_clock_offset; |
| 708 | static int cpu_ticks_enabled; |
| 709 | |
| 710 | /* return the host CPU cycle counter and handle stop/restart */ |
| 711 | int64_t cpu_get_ticks(void) |
| 712 | { |
| 713 | if (!cpu_ticks_enabled) { |
| 714 | return cpu_ticks_offset; |
| 715 | } else { |
| 716 | int64_t ticks; |
| 717 | ticks = cpu_get_real_ticks(); |
| 718 | if (cpu_ticks_prev > ticks) { |
| 719 | /* Note: non increasing ticks may happen if the host uses |
| 720 | software suspend */ |
| 721 | cpu_ticks_offset += cpu_ticks_prev - ticks; |
| 722 | } |
| 723 | cpu_ticks_prev = ticks; |
| 724 | return ticks + cpu_ticks_offset; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | /* return the host CPU monotonic timer and handle stop/restart */ |
| 729 | static int64_t cpu_get_clock(void) |
| 730 | { |
| 731 | int64_t ti; |
| 732 | if (!cpu_ticks_enabled) { |
| 733 | return cpu_clock_offset; |
| 734 | } else { |
| 735 | ti = get_clock(); |
| 736 | return ti + cpu_clock_offset; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | /* enable cpu_get_ticks() */ |
| 741 | void cpu_enable_ticks(void) |
| 742 | { |
| 743 | if (!cpu_ticks_enabled) { |
| 744 | cpu_ticks_offset -= cpu_get_real_ticks(); |
| 745 | cpu_clock_offset -= get_clock(); |
| 746 | cpu_ticks_enabled = 1; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
| 751 | cpu_get_ticks() after that. */ |
| 752 | void cpu_disable_ticks(void) |
| 753 | { |
| 754 | if (cpu_ticks_enabled) { |
| 755 | cpu_ticks_offset = cpu_get_ticks(); |
| 756 | cpu_clock_offset = cpu_get_clock(); |
| 757 | cpu_ticks_enabled = 0; |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | /***********************************************************/ |
| 762 | /* timers */ |
| 763 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 764 | #define QEMU_TIMER_REALTIME 0 |
| 765 | #define QEMU_TIMER_VIRTUAL 1 |
| 766 | |
| 767 | struct QEMUClock { |
| 768 | int type; |
| 769 | /* XXX: add frequency */ |
| 770 | }; |
| 771 | |
| 772 | struct QEMUTimer { |
| 773 | QEMUClock *clock; |
| 774 | int64_t expire_time; |
| 775 | QEMUTimerCB *cb; |
| 776 | void *opaque; |
| 777 | struct QEMUTimer *next; |
| 778 | }; |
| 779 | |
| 780 | QEMUClock *rt_clock; |
| 781 | QEMUClock *vm_clock; |
| 782 | |
| 783 | static QEMUTimer *active_timers[2]; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 784 | #ifdef _WIN32 |
| 785 | static MMRESULT timerID; |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 786 | static HANDLE host_alarm = NULL; |
| 787 | static unsigned int period = 1; |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 788 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 789 | /* frequency of the times() clock tick */ |
| 790 | static int timer_freq; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 791 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 792 | |
| 793 | QEMUClock *qemu_new_clock(int type) |
| 794 | { |
| 795 | QEMUClock *clock; |
| 796 | clock = qemu_mallocz(sizeof(QEMUClock)); |
| 797 | if (!clock) |
| 798 | return NULL; |
| 799 | clock->type = type; |
| 800 | return clock; |
| 801 | } |
| 802 | |
| 803 | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque) |
| 804 | { |
| 805 | QEMUTimer *ts; |
| 806 | |
| 807 | ts = qemu_mallocz(sizeof(QEMUTimer)); |
| 808 | ts->clock = clock; |
| 809 | ts->cb = cb; |
| 810 | ts->opaque = opaque; |
| 811 | return ts; |
| 812 | } |
| 813 | |
| 814 | void qemu_free_timer(QEMUTimer *ts) |
| 815 | { |
| 816 | qemu_free(ts); |
| 817 | } |
| 818 | |
| 819 | /* stop a timer, but do not dealloc it */ |
| 820 | void qemu_del_timer(QEMUTimer *ts) |
| 821 | { |
| 822 | QEMUTimer **pt, *t; |
| 823 | |
| 824 | /* NOTE: this code must be signal safe because |
| 825 | qemu_timer_expired() can be called from a signal. */ |
| 826 | pt = &active_timers[ts->clock->type]; |
| 827 | for(;;) { |
| 828 | t = *pt; |
| 829 | if (!t) |
| 830 | break; |
| 831 | if (t == ts) { |
| 832 | *pt = t->next; |
| 833 | break; |
| 834 | } |
| 835 | pt = &t->next; |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | /* modify the current timer so that it will be fired when current_time |
| 840 | >= expire_time. The corresponding callback will be called. */ |
| 841 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
| 842 | { |
| 843 | QEMUTimer **pt, *t; |
| 844 | |
| 845 | qemu_del_timer(ts); |
| 846 | |
| 847 | /* add the timer in the sorted list */ |
| 848 | /* NOTE: this code must be signal safe because |
| 849 | qemu_timer_expired() can be called from a signal. */ |
| 850 | pt = &active_timers[ts->clock->type]; |
| 851 | for(;;) { |
| 852 | t = *pt; |
| 853 | if (!t) |
| 854 | break; |
| 855 | if (t->expire_time > expire_time) |
| 856 | break; |
| 857 | pt = &t->next; |
| 858 | } |
| 859 | ts->expire_time = expire_time; |
| 860 | ts->next = *pt; |
| 861 | *pt = ts; |
| 862 | } |
| 863 | |
| 864 | int qemu_timer_pending(QEMUTimer *ts) |
| 865 | { |
| 866 | QEMUTimer *t; |
| 867 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
| 868 | if (t == ts) |
| 869 | return 1; |
| 870 | } |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
| 875 | { |
| 876 | if (!timer_head) |
| 877 | return 0; |
| 878 | return (timer_head->expire_time <= current_time); |
| 879 | } |
| 880 | |
| 881 | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
| 882 | { |
| 883 | QEMUTimer *ts; |
| 884 | |
| 885 | for(;;) { |
| 886 | ts = *ptimer_head; |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 887 | if (!ts || ts->expire_time > current_time) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 888 | break; |
| 889 | /* remove timer from the list before calling the callback */ |
| 890 | *ptimer_head = ts->next; |
| 891 | ts->next = NULL; |
| 892 | |
| 893 | /* run the callback (the timer list can be modified) */ |
| 894 | ts->cb(ts->opaque); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | int64_t qemu_get_clock(QEMUClock *clock) |
| 899 | { |
| 900 | switch(clock->type) { |
| 901 | case QEMU_TIMER_REALTIME: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 902 | return get_clock() / 1000000; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 903 | default: |
| 904 | case QEMU_TIMER_VIRTUAL: |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 905 | return cpu_get_clock(); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 909 | static void init_timers(void) |
| 910 | { |
| 911 | init_get_clock(); |
| 912 | ticks_per_sec = QEMU_TIMER_BASE; |
| 913 | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
| 914 | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
| 915 | } |
| 916 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 917 | /* save a timer */ |
| 918 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) |
| 919 | { |
| 920 | uint64_t expire_time; |
| 921 | |
| 922 | if (qemu_timer_pending(ts)) { |
| 923 | expire_time = ts->expire_time; |
| 924 | } else { |
| 925 | expire_time = -1; |
| 926 | } |
| 927 | qemu_put_be64(f, expire_time); |
| 928 | } |
| 929 | |
| 930 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) |
| 931 | { |
| 932 | uint64_t expire_time; |
| 933 | |
| 934 | expire_time = qemu_get_be64(f); |
| 935 | if (expire_time != -1) { |
| 936 | qemu_mod_timer(ts, expire_time); |
| 937 | } else { |
| 938 | qemu_del_timer(ts); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | static void timer_save(QEMUFile *f, void *opaque) |
| 943 | { |
| 944 | if (cpu_ticks_enabled) { |
| 945 | hw_error("cannot save state if virtual timers are running"); |
| 946 | } |
| 947 | qemu_put_be64s(f, &cpu_ticks_offset); |
| 948 | qemu_put_be64s(f, &ticks_per_sec); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 949 | qemu_put_be64s(f, &cpu_clock_offset); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | static int timer_load(QEMUFile *f, void *opaque, int version_id) |
| 953 | { |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 954 | if (version_id != 1 && version_id != 2) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 955 | return -EINVAL; |
| 956 | if (cpu_ticks_enabled) { |
| 957 | return -EINVAL; |
| 958 | } |
| 959 | qemu_get_be64s(f, &cpu_ticks_offset); |
| 960 | qemu_get_be64s(f, &ticks_per_sec); |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 961 | if (version_id == 2) { |
| 962 | qemu_get_be64s(f, &cpu_clock_offset); |
| 963 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 964 | return 0; |
| 965 | } |
| 966 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 967 | #ifdef _WIN32 |
| 968 | void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
| 969 | DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
| 970 | #else |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 971 | static void host_alarm_handler(int host_signum) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 972 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 973 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 974 | #if 0 |
| 975 | #define DISP_FREQ 1000 |
| 976 | { |
| 977 | static int64_t delta_min = INT64_MAX; |
| 978 | static int64_t delta_max, delta_cum, last_clock, delta, ti; |
| 979 | static int count; |
| 980 | ti = qemu_get_clock(vm_clock); |
| 981 | if (last_clock != 0) { |
| 982 | delta = ti - last_clock; |
| 983 | if (delta < delta_min) |
| 984 | delta_min = delta; |
| 985 | if (delta > delta_max) |
| 986 | delta_max = delta; |
| 987 | delta_cum += delta; |
| 988 | if (++count == DISP_FREQ) { |
bellard | 26a7646 | 2006-06-25 18:15:32 +0000 | [diff] [blame] | 989 | 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] | 990 | muldiv64(delta_min, 1000000, ticks_per_sec), |
| 991 | muldiv64(delta_max, 1000000, ticks_per_sec), |
| 992 | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec), |
| 993 | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ)); |
| 994 | count = 0; |
| 995 | delta_min = INT64_MAX; |
| 996 | delta_max = 0; |
| 997 | delta_cum = 0; |
| 998 | } |
| 999 | } |
| 1000 | last_clock = ti; |
| 1001 | } |
| 1002 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1003 | if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
| 1004 | qemu_get_clock(vm_clock)) || |
| 1005 | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
| 1006 | qemu_get_clock(rt_clock))) { |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1007 | #ifdef _WIN32 |
| 1008 | SetEvent(host_alarm); |
| 1009 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1010 | CPUState *env = cpu_single_env; |
| 1011 | if (env) { |
| 1012 | /* stop the currently executing cpu because a timer occured */ |
| 1013 | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
bellard | a332e11 | 2005-09-03 17:55:47 +0000 | [diff] [blame] | 1014 | #ifdef USE_KQEMU |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1015 | if (env->kqemu_enabled) { |
| 1016 | kqemu_cpu_interrupt(env); |
| 1017 | } |
bellard | a332e11 | 2005-09-03 17:55:47 +0000 | [diff] [blame] | 1018 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 1019 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1023 | #ifndef _WIN32 |
| 1024 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1025 | #if defined(__linux__) |
| 1026 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1027 | #define RTC_FREQ 1024 |
| 1028 | |
| 1029 | static int rtc_fd; |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1030 | |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1031 | static int start_rtc_timer(void) |
| 1032 | { |
| 1033 | rtc_fd = open("/dev/rtc", O_RDONLY); |
| 1034 | if (rtc_fd < 0) |
| 1035 | return -1; |
| 1036 | if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { |
| 1037 | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n" |
| 1038 | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n" |
| 1039 | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n"); |
| 1040 | goto fail; |
| 1041 | } |
| 1042 | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
| 1043 | fail: |
| 1044 | close(rtc_fd); |
| 1045 | return -1; |
| 1046 | } |
| 1047 | pit_min_timer_count = PIT_FREQ / RTC_FREQ; |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
bellard | 829309c | 2004-05-20 13:20:12 +0000 | [diff] [blame] | 1051 | #else |
| 1052 | |
| 1053 | static int start_rtc_timer(void) |
| 1054 | { |
| 1055 | return -1; |
| 1056 | } |
| 1057 | |
| 1058 | #endif /* !defined(__linux__) */ |
| 1059 | |
| 1060 | #endif /* !defined(_WIN32) */ |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1061 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1062 | static void init_timer_alarm(void) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1063 | { |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1064 | #ifdef _WIN32 |
| 1065 | { |
| 1066 | int count=0; |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1067 | TIMECAPS tc; |
| 1068 | |
| 1069 | ZeroMemory(&tc, sizeof(TIMECAPS)); |
| 1070 | timeGetDevCaps(&tc, sizeof(TIMECAPS)); |
| 1071 | if (period < tc.wPeriodMin) |
| 1072 | period = tc.wPeriodMin; |
| 1073 | timeBeginPeriod(period); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 1074 | timerID = timeSetEvent(1, // interval (ms) |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1075 | period, // resolution |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1076 | host_alarm_handler, // function |
| 1077 | (DWORD)&count, // user parameter |
| 1078 | TIME_PERIODIC | TIME_CALLBACK_FUNCTION); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1079 | if( !timerID ) { |
| 1080 | perror("failed timer alarm"); |
| 1081 | exit(1); |
| 1082 | } |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1083 | host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 1084 | if (!host_alarm) { |
| 1085 | perror("failed CreateEvent"); |
| 1086 | exit(1); |
| 1087 | } |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 1088 | qemu_add_wait_object(host_alarm, NULL, NULL); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1089 | } |
| 1090 | pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000; |
| 1091 | #else |
| 1092 | { |
| 1093 | struct sigaction act; |
| 1094 | struct itimerval itv; |
| 1095 | |
| 1096 | /* get times() syscall frequency */ |
| 1097 | timer_freq = sysconf(_SC_CLK_TCK); |
| 1098 | |
| 1099 | /* timer signal */ |
| 1100 | sigfillset(&act.sa_mask); |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 1101 | act.sa_flags = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1102 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1103 | act.sa_flags |= SA_ONSTACK; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1104 | #endif |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1105 | act.sa_handler = host_alarm_handler; |
| 1106 | sigaction(SIGALRM, &act, NULL); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1107 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1108 | itv.it_interval.tv_sec = 0; |
bellard | d79284e | 2005-01-26 21:56:26 +0000 | [diff] [blame] | 1109 | 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] | 1110 | itv.it_value.tv_sec = 0; |
| 1111 | itv.it_value.tv_usec = 10 * 1000; |
| 1112 | setitimer(ITIMER_REAL, &itv, NULL); |
| 1113 | /* we probe the tick duration of the kernel to inform the user if |
| 1114 | the emulated kernel requested a too high timer frequency */ |
| 1115 | getitimer(ITIMER_REAL, &itv); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1116 | |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1117 | #if defined(__linux__) |
bellard | 29e3055 | 2006-05-01 13:28:36 +0000 | [diff] [blame] | 1118 | /* XXX: force /dev/rtc usage because even 2.6 kernels may not |
| 1119 | have timers with 1 ms resolution. The correct solution will |
| 1120 | be to use the POSIX real time timers available in recent |
| 1121 | 2.6 kernels */ |
| 1122 | if (itv.it_interval.tv_usec > 1000 || 1) { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1123 | /* try to use /dev/rtc to have a faster timer */ |
| 1124 | if (start_rtc_timer() < 0) |
| 1125 | goto use_itimer; |
| 1126 | /* disable itimer */ |
| 1127 | itv.it_interval.tv_sec = 0; |
| 1128 | itv.it_interval.tv_usec = 0; |
| 1129 | itv.it_value.tv_sec = 0; |
| 1130 | itv.it_value.tv_usec = 0; |
| 1131 | setitimer(ITIMER_REAL, &itv, NULL); |
| 1132 | |
| 1133 | /* use the RTC */ |
bellard | a1968d7 | 2004-06-25 17:06:27 +0000 | [diff] [blame] | 1134 | sigaction(SIGIO, &act, NULL); |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1135 | fcntl(rtc_fd, F_SETFL, O_ASYNC); |
| 1136 | fcntl(rtc_fd, F_SETOWN, getpid()); |
bellard | 83fb7ad | 2004-07-05 21:25:26 +0000 | [diff] [blame] | 1137 | } else |
| 1138 | #endif /* defined(__linux__) */ |
| 1139 | { |
bellard | fd87259 | 2004-05-12 19:11:15 +0000 | [diff] [blame] | 1140 | use_itimer: |
| 1141 | pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * |
| 1142 | PIT_FREQ) / 1000000; |
| 1143 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1144 | } |
| 1145 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1148 | void quit_timers(void) |
| 1149 | { |
| 1150 | #ifdef _WIN32 |
| 1151 | timeKillEvent(timerID); |
bellard | 06d9f2f | 2006-05-01 13:23:04 +0000 | [diff] [blame] | 1152 | timeEndPeriod(period); |
| 1153 | if (host_alarm) { |
| 1154 | CloseHandle(host_alarm); |
| 1155 | host_alarm = NULL; |
| 1156 | } |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 1157 | #endif |
| 1158 | } |
| 1159 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 1160 | /***********************************************************/ |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1161 | /* character device */ |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 1162 | |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1163 | static void qemu_chr_reset_bh(void *opaque) |
| 1164 | { |
| 1165 | CharDriverState *s = opaque; |
| 1166 | if (s->chr_event) |
| 1167 | s->chr_event(s, CHR_EVENT_RESET); |
| 1168 | qemu_bh_delete(s->bh); |
| 1169 | s->bh = NULL; |
| 1170 | } |
| 1171 | |
| 1172 | void qemu_chr_reset(CharDriverState *s) |
| 1173 | { |
| 1174 | if (s->bh == NULL) { |
| 1175 | s->bh = qemu_bh_new(qemu_chr_reset_bh, s); |
| 1176 | qemu_bh_schedule(s->bh); |
| 1177 | } |
| 1178 | } |
| 1179 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1180 | int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1181 | { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1182 | return s->chr_write(s, buf, len); |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1185 | int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1186 | { |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1187 | if (!s->chr_ioctl) |
| 1188 | return -ENOTSUP; |
| 1189 | return s->chr_ioctl(s, cmd, arg); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1192 | void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1193 | { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1194 | char buf[4096]; |
| 1195 | va_list ap; |
| 1196 | va_start(ap, fmt); |
| 1197 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 1198 | qemu_chr_write(s, buf, strlen(buf)); |
| 1199 | va_end(ap); |
| 1200 | } |
| 1201 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 1202 | void qemu_chr_send_event(CharDriverState *s, int event) |
| 1203 | { |
| 1204 | if (s->chr_send_event) |
| 1205 | s->chr_send_event(s, event); |
| 1206 | } |
| 1207 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1208 | void qemu_chr_add_read_handler(CharDriverState *s, |
| 1209 | IOCanRWHandler *fd_can_read, |
| 1210 | IOReadHandler *fd_read, void *opaque) |
| 1211 | { |
| 1212 | s->chr_add_read_handler(s, fd_can_read, fd_read, opaque); |
| 1213 | } |
| 1214 | |
| 1215 | void qemu_chr_add_event_handler(CharDriverState *s, IOEventHandler *chr_event) |
| 1216 | { |
| 1217 | s->chr_event = chr_event; |
| 1218 | } |
| 1219 | |
| 1220 | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1221 | { |
| 1222 | return len; |
| 1223 | } |
| 1224 | |
| 1225 | static void null_chr_add_read_handler(CharDriverState *chr, |
| 1226 | IOCanRWHandler *fd_can_read, |
| 1227 | IOReadHandler *fd_read, void *opaque) |
| 1228 | { |
| 1229 | } |
| 1230 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1231 | static CharDriverState *qemu_chr_open_null(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1232 | { |
| 1233 | CharDriverState *chr; |
| 1234 | |
| 1235 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1236 | if (!chr) |
| 1237 | return NULL; |
| 1238 | chr->chr_write = null_chr_write; |
| 1239 | chr->chr_add_read_handler = null_chr_add_read_handler; |
| 1240 | return chr; |
| 1241 | } |
| 1242 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1243 | #ifdef _WIN32 |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1244 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1245 | static void socket_cleanup(void) |
| 1246 | { |
| 1247 | WSACleanup(); |
| 1248 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1249 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1250 | static int socket_init(void) |
| 1251 | { |
| 1252 | WSADATA Data; |
| 1253 | int ret, err; |
| 1254 | |
| 1255 | ret = WSAStartup(MAKEWORD(2,2), &Data); |
| 1256 | if (ret != 0) { |
| 1257 | err = WSAGetLastError(); |
| 1258 | fprintf(stderr, "WSAStartup: %d\n", err); |
| 1259 | return -1; |
| 1260 | } |
| 1261 | atexit(socket_cleanup); |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | static int send_all(int fd, const uint8_t *buf, int len1) |
| 1266 | { |
| 1267 | int ret, len; |
| 1268 | |
| 1269 | len = len1; |
| 1270 | while (len > 0) { |
| 1271 | ret = send(fd, buf, len, 0); |
| 1272 | if (ret < 0) { |
| 1273 | int errno; |
| 1274 | errno = WSAGetLastError(); |
| 1275 | if (errno != WSAEWOULDBLOCK) { |
| 1276 | return -1; |
| 1277 | } |
| 1278 | } else if (ret == 0) { |
| 1279 | break; |
| 1280 | } else { |
| 1281 | buf += ret; |
| 1282 | len -= ret; |
| 1283 | } |
| 1284 | } |
| 1285 | return len1 - len; |
| 1286 | } |
| 1287 | |
| 1288 | void socket_set_nonblock(int fd) |
| 1289 | { |
| 1290 | unsigned long opt = 1; |
| 1291 | ioctlsocket(fd, FIONBIO, &opt); |
| 1292 | } |
| 1293 | |
| 1294 | #else |
| 1295 | |
bellard | 1d96905 | 2004-09-18 19:34:39 +0000 | [diff] [blame] | 1296 | static int unix_write(int fd, const uint8_t *buf, int len1) |
| 1297 | { |
| 1298 | int ret, len; |
| 1299 | |
| 1300 | len = len1; |
| 1301 | while (len > 0) { |
| 1302 | ret = write(fd, buf, len); |
| 1303 | if (ret < 0) { |
| 1304 | if (errno != EINTR && errno != EAGAIN) |
| 1305 | return -1; |
| 1306 | } else if (ret == 0) { |
| 1307 | break; |
| 1308 | } else { |
| 1309 | buf += ret; |
| 1310 | len -= ret; |
| 1311 | } |
| 1312 | } |
| 1313 | return len1 - len; |
| 1314 | } |
| 1315 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 1316 | static inline int send_all(int fd, const uint8_t *buf, int len1) |
| 1317 | { |
| 1318 | return unix_write(fd, buf, len1); |
| 1319 | } |
| 1320 | |
| 1321 | void socket_set_nonblock(int fd) |
| 1322 | { |
| 1323 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 1324 | } |
| 1325 | #endif /* !_WIN32 */ |
| 1326 | |
| 1327 | #ifndef _WIN32 |
| 1328 | |
| 1329 | typedef struct { |
| 1330 | int fd_in, fd_out; |
| 1331 | IOCanRWHandler *fd_can_read; |
| 1332 | IOReadHandler *fd_read; |
| 1333 | void *fd_opaque; |
| 1334 | int max_size; |
| 1335 | } FDCharDriver; |
| 1336 | |
| 1337 | #define STDIO_MAX_CLIENTS 2 |
| 1338 | |
| 1339 | static int stdio_nb_clients; |
| 1340 | static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS]; |
| 1341 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1342 | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1343 | { |
| 1344 | FDCharDriver *s = chr->opaque; |
bellard | 1d96905 | 2004-09-18 19:34:39 +0000 | [diff] [blame] | 1345 | return unix_write(s->fd_out, buf, len); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1348 | static int fd_chr_read_poll(void *opaque) |
| 1349 | { |
| 1350 | CharDriverState *chr = opaque; |
| 1351 | FDCharDriver *s = chr->opaque; |
| 1352 | |
| 1353 | s->max_size = s->fd_can_read(s->fd_opaque); |
| 1354 | return s->max_size; |
| 1355 | } |
| 1356 | |
| 1357 | static void fd_chr_read(void *opaque) |
| 1358 | { |
| 1359 | CharDriverState *chr = opaque; |
| 1360 | FDCharDriver *s = chr->opaque; |
| 1361 | int size, len; |
| 1362 | uint8_t buf[1024]; |
| 1363 | |
| 1364 | len = sizeof(buf); |
| 1365 | if (len > s->max_size) |
| 1366 | len = s->max_size; |
| 1367 | if (len == 0) |
| 1368 | return; |
| 1369 | size = read(s->fd_in, buf, len); |
pbrook | 188157f | 2006-11-01 01:44:16 +0000 | [diff] [blame] | 1370 | if (size == 0) { |
| 1371 | /* FD has been closed. Remove it from the active list. */ |
| 1372 | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
| 1373 | return; |
| 1374 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1375 | if (size > 0) { |
| 1376 | s->fd_read(s->fd_opaque, buf, size); |
| 1377 | } |
| 1378 | } |
| 1379 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1380 | static void fd_chr_add_read_handler(CharDriverState *chr, |
| 1381 | IOCanRWHandler *fd_can_read, |
| 1382 | IOReadHandler *fd_read, void *opaque) |
| 1383 | { |
| 1384 | FDCharDriver *s = chr->opaque; |
| 1385 | |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1386 | if (s->fd_in >= 0) { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1387 | s->fd_can_read = fd_can_read; |
| 1388 | s->fd_read = fd_read; |
| 1389 | s->fd_opaque = opaque; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1390 | if (nographic && s->fd_in == 0) { |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1391 | } else { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1392 | qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, |
| 1393 | fd_chr_read, NULL, chr); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1394 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 1395 | } |
| 1396 | } |
| 1397 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1398 | /* open a character device to a unix fd */ |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1399 | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1400 | { |
| 1401 | CharDriverState *chr; |
| 1402 | FDCharDriver *s; |
| 1403 | |
| 1404 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1405 | if (!chr) |
| 1406 | return NULL; |
| 1407 | s = qemu_mallocz(sizeof(FDCharDriver)); |
| 1408 | if (!s) { |
| 1409 | free(chr); |
| 1410 | return NULL; |
| 1411 | } |
| 1412 | s->fd_in = fd_in; |
| 1413 | s->fd_out = fd_out; |
| 1414 | chr->opaque = s; |
| 1415 | chr->chr_write = fd_chr_write; |
| 1416 | chr->chr_add_read_handler = fd_chr_add_read_handler; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1417 | |
| 1418 | qemu_chr_reset(chr); |
| 1419 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1420 | return chr; |
| 1421 | } |
| 1422 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1423 | static CharDriverState *qemu_chr_open_file_out(const char *file_out) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1424 | { |
| 1425 | int fd_out; |
| 1426 | |
pbrook | 89ba1a7 | 2006-03-28 20:20:38 +0000 | [diff] [blame] | 1427 | 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] | 1428 | if (fd_out < 0) |
| 1429 | return NULL; |
| 1430 | return qemu_chr_open_fd(-1, fd_out); |
| 1431 | } |
| 1432 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1433 | static CharDriverState *qemu_chr_open_pipe(const char *filename) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1434 | { |
ths | c26c1c4 | 2006-12-22 19:25:31 +0000 | [diff] [blame] | 1435 | int fd_in, fd_out; |
| 1436 | char filename_in[256], filename_out[256]; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1437 | |
ths | c26c1c4 | 2006-12-22 19:25:31 +0000 | [diff] [blame] | 1438 | snprintf(filename_in, 256, "%s.in", filename); |
| 1439 | snprintf(filename_out, 256, "%s.out", filename); |
| 1440 | fd_in = open(filename_in, O_RDWR | O_BINARY); |
| 1441 | fd_out = open(filename_out, O_RDWR | O_BINARY); |
| 1442 | if (fd_in < 0 || fd_out < 0) { |
| 1443 | if (fd_in >= 0) |
| 1444 | close(fd_in); |
| 1445 | if (fd_out >= 0) |
| 1446 | close(fd_out); |
| 1447 | fd_in = fd_out = open(filename, O_RDWR | O_BINARY); |
| 1448 | if (fd_in < 0) |
| 1449 | return NULL; |
| 1450 | } |
| 1451 | return qemu_chr_open_fd(fd_in, fd_out); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1455 | /* for STDIO, we handle the case where several clients use it |
| 1456 | (nographic mode) */ |
| 1457 | |
| 1458 | #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */ |
| 1459 | |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1460 | #define TERM_FIFO_MAX_SIZE 1 |
| 1461 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1462 | static int term_got_escape, client_index; |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1463 | static uint8_t term_fifo[TERM_FIFO_MAX_SIZE]; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1464 | static int term_fifo_size; |
| 1465 | static int term_timestamps; |
| 1466 | static int64_t term_timestamps_start; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1467 | |
| 1468 | void term_print_help(void) |
| 1469 | { |
| 1470 | printf("\n" |
| 1471 | "C-a h print this help\n" |
| 1472 | "C-a x exit emulator\n" |
| 1473 | "C-a s save disk data back to file (if -snapshot)\n" |
| 1474 | "C-a b send break (magic sysrq)\n" |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1475 | "C-a t toggle console timestamps\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1476 | "C-a c switch between console and monitor\n" |
| 1477 | "C-a C-a send C-a\n" |
| 1478 | ); |
| 1479 | } |
| 1480 | |
| 1481 | /* called when a char is received */ |
| 1482 | static void stdio_received_byte(int ch) |
| 1483 | { |
| 1484 | if (term_got_escape) { |
| 1485 | term_got_escape = 0; |
| 1486 | switch(ch) { |
| 1487 | case 'h': |
| 1488 | term_print_help(); |
| 1489 | break; |
| 1490 | case 'x': |
| 1491 | exit(0); |
| 1492 | break; |
| 1493 | case 's': |
| 1494 | { |
| 1495 | int i; |
| 1496 | for (i = 0; i < MAX_DISKS; i++) { |
| 1497 | if (bs_table[i]) |
| 1498 | bdrv_commit(bs_table[i]); |
| 1499 | } |
| 1500 | } |
| 1501 | break; |
| 1502 | case 'b': |
| 1503 | if (client_index < stdio_nb_clients) { |
| 1504 | CharDriverState *chr; |
| 1505 | FDCharDriver *s; |
| 1506 | |
| 1507 | chr = stdio_clients[client_index]; |
| 1508 | s = chr->opaque; |
| 1509 | chr->chr_event(s->fd_opaque, CHR_EVENT_BREAK); |
| 1510 | } |
| 1511 | break; |
| 1512 | case 'c': |
| 1513 | client_index++; |
| 1514 | if (client_index >= stdio_nb_clients) |
| 1515 | client_index = 0; |
| 1516 | if (client_index == 0) { |
| 1517 | /* send a new line in the monitor to get the prompt */ |
| 1518 | ch = '\r'; |
| 1519 | goto send_char; |
| 1520 | } |
| 1521 | break; |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1522 | case 't': |
| 1523 | term_timestamps = !term_timestamps; |
| 1524 | term_timestamps_start = -1; |
| 1525 | break; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1526 | case TERM_ESCAPE: |
| 1527 | goto send_char; |
| 1528 | } |
| 1529 | } else if (ch == TERM_ESCAPE) { |
| 1530 | term_got_escape = 1; |
| 1531 | } else { |
| 1532 | send_char: |
| 1533 | if (client_index < stdio_nb_clients) { |
| 1534 | uint8_t buf[1]; |
| 1535 | CharDriverState *chr; |
| 1536 | FDCharDriver *s; |
| 1537 | |
| 1538 | chr = stdio_clients[client_index]; |
| 1539 | s = chr->opaque; |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1540 | if (s->fd_can_read(s->fd_opaque) > 0) { |
| 1541 | buf[0] = ch; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1542 | s->fd_read(s->fd_opaque, buf, 1); |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1543 | } else if (term_fifo_size == 0) { |
| 1544 | term_fifo[term_fifo_size++] = ch; |
| 1545 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1546 | } |
| 1547 | } |
| 1548 | } |
| 1549 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1550 | static int stdio_read_poll(void *opaque) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1551 | { |
bellard | aa0bc6b | 2005-09-03 15:28:58 +0000 | [diff] [blame] | 1552 | CharDriverState *chr; |
| 1553 | FDCharDriver *s; |
| 1554 | |
| 1555 | if (client_index < stdio_nb_clients) { |
| 1556 | chr = stdio_clients[client_index]; |
| 1557 | s = chr->opaque; |
| 1558 | /* try to flush the queue if needed */ |
| 1559 | if (term_fifo_size != 0 && s->fd_can_read(s->fd_opaque) > 0) { |
| 1560 | s->fd_read(s->fd_opaque, term_fifo, 1); |
| 1561 | term_fifo_size = 0; |
| 1562 | } |
| 1563 | /* see if we can absorb more chars */ |
| 1564 | if (term_fifo_size == 0) |
| 1565 | return 1; |
| 1566 | else |
| 1567 | return 0; |
| 1568 | } else { |
| 1569 | return 1; |
| 1570 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1573 | static void stdio_read(void *opaque) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1574 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1575 | int size; |
| 1576 | uint8_t buf[1]; |
| 1577 | |
| 1578 | size = read(0, buf, 1); |
pbrook | 519945d | 2006-09-10 14:39:54 +0000 | [diff] [blame] | 1579 | if (size == 0) { |
| 1580 | /* stdin has been closed. Remove it from the active list. */ |
| 1581 | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
| 1582 | return; |
| 1583 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1584 | if (size > 0) |
| 1585 | stdio_received_byte(buf[0]); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1588 | static int stdio_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 1589 | { |
| 1590 | FDCharDriver *s = chr->opaque; |
| 1591 | if (!term_timestamps) { |
| 1592 | return unix_write(s->fd_out, buf, len); |
| 1593 | } else { |
| 1594 | int i; |
| 1595 | char buf1[64]; |
| 1596 | |
| 1597 | for(i = 0; i < len; i++) { |
| 1598 | unix_write(s->fd_out, buf + i, 1); |
| 1599 | if (buf[i] == '\n') { |
| 1600 | int64_t ti; |
| 1601 | int secs; |
| 1602 | |
| 1603 | ti = get_clock(); |
| 1604 | if (term_timestamps_start == -1) |
| 1605 | term_timestamps_start = ti; |
| 1606 | ti -= term_timestamps_start; |
| 1607 | secs = ti / 1000000000; |
| 1608 | snprintf(buf1, sizeof(buf1), |
| 1609 | "[%02d:%02d:%02d.%03d] ", |
| 1610 | secs / 3600, |
| 1611 | (secs / 60) % 60, |
| 1612 | secs % 60, |
| 1613 | (int)((ti / 1000000) % 1000)); |
| 1614 | unix_write(s->fd_out, buf1, strlen(buf1)); |
| 1615 | } |
| 1616 | } |
| 1617 | return len; |
| 1618 | } |
| 1619 | } |
| 1620 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 1621 | /* init terminal so that we can grab keys */ |
| 1622 | static struct termios oldtty; |
| 1623 | static int old_fd0_flags; |
| 1624 | |
| 1625 | static void term_exit(void) |
| 1626 | { |
| 1627 | tcsetattr (0, TCSANOW, &oldtty); |
| 1628 | fcntl(0, F_SETFL, old_fd0_flags); |
| 1629 | } |
| 1630 | |
| 1631 | static void term_init(void) |
| 1632 | { |
| 1633 | struct termios tty; |
| 1634 | |
| 1635 | tcgetattr (0, &tty); |
| 1636 | oldtty = tty; |
| 1637 | old_fd0_flags = fcntl(0, F_GETFL); |
| 1638 | |
| 1639 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 1640 | |INLCR|IGNCR|ICRNL|IXON); |
| 1641 | tty.c_oflag |= OPOST; |
| 1642 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
| 1643 | /* if graphical mode, we allow Ctrl-C handling */ |
| 1644 | if (nographic) |
| 1645 | tty.c_lflag &= ~ISIG; |
| 1646 | tty.c_cflag &= ~(CSIZE|PARENB); |
| 1647 | tty.c_cflag |= CS8; |
| 1648 | tty.c_cc[VMIN] = 1; |
| 1649 | tty.c_cc[VTIME] = 0; |
| 1650 | |
| 1651 | tcsetattr (0, TCSANOW, &tty); |
| 1652 | |
| 1653 | atexit(term_exit); |
| 1654 | |
| 1655 | fcntl(0, F_SETFL, O_NONBLOCK); |
| 1656 | } |
| 1657 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1658 | static CharDriverState *qemu_chr_open_stdio(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1659 | { |
| 1660 | CharDriverState *chr; |
| 1661 | |
| 1662 | if (nographic) { |
| 1663 | if (stdio_nb_clients >= STDIO_MAX_CLIENTS) |
| 1664 | return NULL; |
| 1665 | chr = qemu_chr_open_fd(0, 1); |
bellard | 1dce7c3 | 2006-07-13 23:20:22 +0000 | [diff] [blame] | 1666 | chr->chr_write = stdio_write; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1667 | if (stdio_nb_clients == 0) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 1668 | qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, NULL); |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1669 | client_index = stdio_nb_clients; |
| 1670 | } else { |
| 1671 | if (stdio_nb_clients != 0) |
| 1672 | return NULL; |
| 1673 | chr = qemu_chr_open_fd(0, 1); |
| 1674 | } |
| 1675 | stdio_clients[stdio_nb_clients++] = chr; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 1676 | if (stdio_nb_clients == 1) { |
| 1677 | /* set the terminal in raw mode */ |
| 1678 | term_init(); |
| 1679 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1680 | return chr; |
| 1681 | } |
| 1682 | |
| 1683 | #if defined(__linux__) |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1684 | static CharDriverState *qemu_chr_open_pty(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1685 | { |
bellard | 91fc211 | 2005-12-18 19:09:37 +0000 | [diff] [blame] | 1686 | struct termios tty; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1687 | char slave_name[1024]; |
| 1688 | int master_fd, slave_fd; |
| 1689 | |
| 1690 | /* Not satisfying */ |
| 1691 | if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) { |
| 1692 | return NULL; |
| 1693 | } |
bellard | 91fc211 | 2005-12-18 19:09:37 +0000 | [diff] [blame] | 1694 | |
| 1695 | /* Disabling local echo and line-buffered output */ |
| 1696 | tcgetattr (master_fd, &tty); |
| 1697 | tty.c_lflag &= ~(ECHO|ICANON|ISIG); |
| 1698 | tty.c_cc[VMIN] = 1; |
| 1699 | tty.c_cc[VTIME] = 0; |
| 1700 | tcsetattr (master_fd, TCSAFLUSH, &tty); |
| 1701 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1702 | fprintf(stderr, "char device redirected to %s\n", slave_name); |
| 1703 | return qemu_chr_open_fd(master_fd, master_fd); |
| 1704 | } |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1705 | |
| 1706 | static void tty_serial_init(int fd, int speed, |
| 1707 | int parity, int data_bits, int stop_bits) |
| 1708 | { |
| 1709 | struct termios tty; |
| 1710 | speed_t spd; |
| 1711 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1712 | #if 0 |
| 1713 | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n", |
| 1714 | speed, parity, data_bits, stop_bits); |
| 1715 | #endif |
| 1716 | tcgetattr (fd, &tty); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1717 | |
| 1718 | switch(speed) { |
| 1719 | case 50: |
| 1720 | spd = B50; |
| 1721 | break; |
| 1722 | case 75: |
| 1723 | spd = B75; |
| 1724 | break; |
| 1725 | case 300: |
| 1726 | spd = B300; |
| 1727 | break; |
| 1728 | case 600: |
| 1729 | spd = B600; |
| 1730 | break; |
| 1731 | case 1200: |
| 1732 | spd = B1200; |
| 1733 | break; |
| 1734 | case 2400: |
| 1735 | spd = B2400; |
| 1736 | break; |
| 1737 | case 4800: |
| 1738 | spd = B4800; |
| 1739 | break; |
| 1740 | case 9600: |
| 1741 | spd = B9600; |
| 1742 | break; |
| 1743 | case 19200: |
| 1744 | spd = B19200; |
| 1745 | break; |
| 1746 | case 38400: |
| 1747 | spd = B38400; |
| 1748 | break; |
| 1749 | case 57600: |
| 1750 | spd = B57600; |
| 1751 | break; |
| 1752 | default: |
| 1753 | case 115200: |
| 1754 | spd = B115200; |
| 1755 | break; |
| 1756 | } |
| 1757 | |
| 1758 | cfsetispeed(&tty, spd); |
| 1759 | cfsetospeed(&tty, spd); |
| 1760 | |
| 1761 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 1762 | |INLCR|IGNCR|ICRNL|IXON); |
| 1763 | tty.c_oflag |= OPOST; |
| 1764 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
bellard | 094eed6 | 2006-09-09 11:10:18 +0000 | [diff] [blame] | 1765 | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1766 | switch(data_bits) { |
| 1767 | default: |
| 1768 | case 8: |
| 1769 | tty.c_cflag |= CS8; |
| 1770 | break; |
| 1771 | case 7: |
| 1772 | tty.c_cflag |= CS7; |
| 1773 | break; |
| 1774 | case 6: |
| 1775 | tty.c_cflag |= CS6; |
| 1776 | break; |
| 1777 | case 5: |
| 1778 | tty.c_cflag |= CS5; |
| 1779 | break; |
| 1780 | } |
| 1781 | switch(parity) { |
| 1782 | default: |
| 1783 | case 'N': |
| 1784 | break; |
| 1785 | case 'E': |
| 1786 | tty.c_cflag |= PARENB; |
| 1787 | break; |
| 1788 | case 'O': |
| 1789 | tty.c_cflag |= PARENB | PARODD; |
| 1790 | break; |
| 1791 | } |
bellard | 094eed6 | 2006-09-09 11:10:18 +0000 | [diff] [blame] | 1792 | if (stop_bits == 2) |
| 1793 | tty.c_cflag |= CSTOPB; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1794 | |
| 1795 | tcsetattr (fd, TCSANOW, &tty); |
| 1796 | } |
| 1797 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1798 | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1799 | { |
| 1800 | FDCharDriver *s = chr->opaque; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1801 | |
| 1802 | switch(cmd) { |
| 1803 | case CHR_IOCTL_SERIAL_SET_PARAMS: |
| 1804 | { |
| 1805 | QEMUSerialSetParams *ssp = arg; |
| 1806 | tty_serial_init(s->fd_in, ssp->speed, ssp->parity, |
| 1807 | ssp->data_bits, ssp->stop_bits); |
| 1808 | } |
| 1809 | break; |
| 1810 | case CHR_IOCTL_SERIAL_SET_BREAK: |
| 1811 | { |
| 1812 | int enable = *(int *)arg; |
| 1813 | if (enable) |
| 1814 | tcsendbreak(s->fd_in, 1); |
| 1815 | } |
| 1816 | break; |
| 1817 | default: |
| 1818 | return -ENOTSUP; |
| 1819 | } |
| 1820 | return 0; |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1823 | static CharDriverState *qemu_chr_open_tty(const char *filename) |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1824 | { |
| 1825 | CharDriverState *chr; |
| 1826 | int fd; |
| 1827 | |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1828 | fd = open(filename, O_RDWR | O_NONBLOCK); |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1829 | if (fd < 0) |
| 1830 | return NULL; |
| 1831 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 1832 | tty_serial_init(fd, 115200, 'N', 8, 1); |
| 1833 | chr = qemu_chr_open_fd(fd, fd); |
| 1834 | if (!chr) |
| 1835 | return NULL; |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1836 | chr->chr_ioctl = tty_serial_ioctl; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1837 | qemu_chr_reset(chr); |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1838 | return chr; |
| 1839 | } |
| 1840 | |
| 1841 | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
| 1842 | { |
| 1843 | int fd = (int)chr->opaque; |
| 1844 | uint8_t b; |
| 1845 | |
| 1846 | switch(cmd) { |
| 1847 | case CHR_IOCTL_PP_READ_DATA: |
| 1848 | if (ioctl(fd, PPRDATA, &b) < 0) |
| 1849 | return -ENOTSUP; |
| 1850 | *(uint8_t *)arg = b; |
| 1851 | break; |
| 1852 | case CHR_IOCTL_PP_WRITE_DATA: |
| 1853 | b = *(uint8_t *)arg; |
| 1854 | if (ioctl(fd, PPWDATA, &b) < 0) |
| 1855 | return -ENOTSUP; |
| 1856 | break; |
| 1857 | case CHR_IOCTL_PP_READ_CONTROL: |
| 1858 | if (ioctl(fd, PPRCONTROL, &b) < 0) |
| 1859 | return -ENOTSUP; |
| 1860 | *(uint8_t *)arg = b; |
| 1861 | break; |
| 1862 | case CHR_IOCTL_PP_WRITE_CONTROL: |
| 1863 | b = *(uint8_t *)arg; |
| 1864 | if (ioctl(fd, PPWCONTROL, &b) < 0) |
| 1865 | return -ENOTSUP; |
| 1866 | break; |
| 1867 | case CHR_IOCTL_PP_READ_STATUS: |
| 1868 | if (ioctl(fd, PPRSTATUS, &b) < 0) |
| 1869 | return -ENOTSUP; |
| 1870 | *(uint8_t *)arg = b; |
| 1871 | break; |
| 1872 | default: |
| 1873 | return -ENOTSUP; |
| 1874 | } |
| 1875 | return 0; |
| 1876 | } |
| 1877 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1878 | static CharDriverState *qemu_chr_open_pp(const char *filename) |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 1879 | { |
| 1880 | CharDriverState *chr; |
| 1881 | int fd; |
| 1882 | |
| 1883 | fd = open(filename, O_RDWR); |
| 1884 | if (fd < 0) |
| 1885 | return NULL; |
| 1886 | |
| 1887 | if (ioctl(fd, PPCLAIM) < 0) { |
| 1888 | close(fd); |
| 1889 | return NULL; |
| 1890 | } |
| 1891 | |
| 1892 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 1893 | if (!chr) { |
| 1894 | close(fd); |
| 1895 | return NULL; |
| 1896 | } |
| 1897 | chr->opaque = (void *)fd; |
| 1898 | chr->chr_write = null_chr_write; |
| 1899 | chr->chr_add_read_handler = null_chr_add_read_handler; |
| 1900 | chr->chr_ioctl = pp_ioctl; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 1901 | |
| 1902 | qemu_chr_reset(chr); |
| 1903 | |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 1904 | return chr; |
| 1905 | } |
| 1906 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1907 | #else |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 1908 | static CharDriverState *qemu_chr_open_pty(void) |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1909 | { |
| 1910 | return NULL; |
| 1911 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 1912 | #endif |
| 1913 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 1914 | #endif /* !defined(_WIN32) */ |
| 1915 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 1916 | #ifdef _WIN32 |
| 1917 | typedef struct { |
| 1918 | IOCanRWHandler *fd_can_read; |
| 1919 | IOReadHandler *fd_read; |
| 1920 | void *win_opaque; |
| 1921 | int max_size; |
| 1922 | HANDLE hcom, hrecv, hsend; |
| 1923 | OVERLAPPED orecv, osend; |
| 1924 | BOOL fpipe; |
| 1925 | DWORD len; |
| 1926 | } WinCharState; |
| 1927 | |
| 1928 | #define NSENDBUF 2048 |
| 1929 | #define NRECVBUF 2048 |
| 1930 | #define MAXCONNECT 1 |
| 1931 | #define NTIMEOUT 5000 |
| 1932 | |
| 1933 | static int win_chr_poll(void *opaque); |
| 1934 | static int win_chr_pipe_poll(void *opaque); |
| 1935 | |
| 1936 | static void win_chr_close2(WinCharState *s) |
| 1937 | { |
| 1938 | if (s->hsend) { |
| 1939 | CloseHandle(s->hsend); |
| 1940 | s->hsend = NULL; |
| 1941 | } |
| 1942 | if (s->hrecv) { |
| 1943 | CloseHandle(s->hrecv); |
| 1944 | s->hrecv = NULL; |
| 1945 | } |
| 1946 | if (s->hcom) { |
| 1947 | CloseHandle(s->hcom); |
| 1948 | s->hcom = NULL; |
| 1949 | } |
| 1950 | if (s->fpipe) |
| 1951 | qemu_del_polling_cb(win_chr_pipe_poll, s); |
| 1952 | else |
| 1953 | qemu_del_polling_cb(win_chr_poll, s); |
| 1954 | } |
| 1955 | |
| 1956 | static void win_chr_close(CharDriverState *chr) |
| 1957 | { |
| 1958 | WinCharState *s = chr->opaque; |
| 1959 | win_chr_close2(s); |
| 1960 | } |
| 1961 | |
| 1962 | static int win_chr_init(WinCharState *s, const char *filename) |
| 1963 | { |
| 1964 | COMMCONFIG comcfg; |
| 1965 | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
| 1966 | COMSTAT comstat; |
| 1967 | DWORD size; |
| 1968 | DWORD err; |
| 1969 | |
| 1970 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1971 | if (!s->hsend) { |
| 1972 | fprintf(stderr, "Failed CreateEvent\n"); |
| 1973 | goto fail; |
| 1974 | } |
| 1975 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1976 | if (!s->hrecv) { |
| 1977 | fprintf(stderr, "Failed CreateEvent\n"); |
| 1978 | goto fail; |
| 1979 | } |
| 1980 | |
| 1981 | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
| 1982 | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); |
| 1983 | if (s->hcom == INVALID_HANDLE_VALUE) { |
| 1984 | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError()); |
| 1985 | s->hcom = NULL; |
| 1986 | goto fail; |
| 1987 | } |
| 1988 | |
| 1989 | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) { |
| 1990 | fprintf(stderr, "Failed SetupComm\n"); |
| 1991 | goto fail; |
| 1992 | } |
| 1993 | |
| 1994 | ZeroMemory(&comcfg, sizeof(COMMCONFIG)); |
| 1995 | size = sizeof(COMMCONFIG); |
| 1996 | GetDefaultCommConfig(filename, &comcfg, &size); |
| 1997 | comcfg.dcb.DCBlength = sizeof(DCB); |
| 1998 | CommConfigDialog(filename, NULL, &comcfg); |
| 1999 | |
| 2000 | if (!SetCommState(s->hcom, &comcfg.dcb)) { |
| 2001 | fprintf(stderr, "Failed SetCommState\n"); |
| 2002 | goto fail; |
| 2003 | } |
| 2004 | |
| 2005 | if (!SetCommMask(s->hcom, EV_ERR)) { |
| 2006 | fprintf(stderr, "Failed SetCommMask\n"); |
| 2007 | goto fail; |
| 2008 | } |
| 2009 | |
| 2010 | cto.ReadIntervalTimeout = MAXDWORD; |
| 2011 | if (!SetCommTimeouts(s->hcom, &cto)) { |
| 2012 | fprintf(stderr, "Failed SetCommTimeouts\n"); |
| 2013 | goto fail; |
| 2014 | } |
| 2015 | |
| 2016 | if (!ClearCommError(s->hcom, &err, &comstat)) { |
| 2017 | fprintf(stderr, "Failed ClearCommError\n"); |
| 2018 | goto fail; |
| 2019 | } |
| 2020 | qemu_add_polling_cb(win_chr_poll, s); |
| 2021 | return 0; |
| 2022 | |
| 2023 | fail: |
| 2024 | win_chr_close2(s); |
| 2025 | return -1; |
| 2026 | } |
| 2027 | |
| 2028 | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
| 2029 | { |
| 2030 | WinCharState *s = chr->opaque; |
| 2031 | DWORD len, ret, size, err; |
| 2032 | |
| 2033 | len = len1; |
| 2034 | ZeroMemory(&s->osend, sizeof(s->osend)); |
| 2035 | s->osend.hEvent = s->hsend; |
| 2036 | while (len > 0) { |
| 2037 | if (s->hsend) |
| 2038 | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
| 2039 | else |
| 2040 | ret = WriteFile(s->hcom, buf, len, &size, NULL); |
| 2041 | if (!ret) { |
| 2042 | err = GetLastError(); |
| 2043 | if (err == ERROR_IO_PENDING) { |
| 2044 | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
| 2045 | if (ret) { |
| 2046 | buf += size; |
| 2047 | len -= size; |
| 2048 | } else { |
| 2049 | break; |
| 2050 | } |
| 2051 | } else { |
| 2052 | break; |
| 2053 | } |
| 2054 | } else { |
| 2055 | buf += size; |
| 2056 | len -= size; |
| 2057 | } |
| 2058 | } |
| 2059 | return len1 - len; |
| 2060 | } |
| 2061 | |
| 2062 | static int win_chr_read_poll(WinCharState *s) |
| 2063 | { |
| 2064 | s->max_size = s->fd_can_read(s->win_opaque); |
| 2065 | return s->max_size; |
| 2066 | } |
| 2067 | |
| 2068 | static void win_chr_readfile(WinCharState *s) |
| 2069 | { |
| 2070 | int ret, err; |
| 2071 | uint8_t buf[1024]; |
| 2072 | DWORD size; |
| 2073 | |
| 2074 | ZeroMemory(&s->orecv, sizeof(s->orecv)); |
| 2075 | s->orecv.hEvent = s->hrecv; |
| 2076 | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
| 2077 | if (!ret) { |
| 2078 | err = GetLastError(); |
| 2079 | if (err == ERROR_IO_PENDING) { |
| 2080 | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | if (size > 0) { |
| 2085 | s->fd_read(s->win_opaque, buf, size); |
| 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | static void win_chr_read(WinCharState *s) |
| 2090 | { |
| 2091 | if (s->len > s->max_size) |
| 2092 | s->len = s->max_size; |
| 2093 | if (s->len == 0) |
| 2094 | return; |
| 2095 | |
| 2096 | win_chr_readfile(s); |
| 2097 | } |
| 2098 | |
| 2099 | static int win_chr_poll(void *opaque) |
| 2100 | { |
| 2101 | WinCharState *s = opaque; |
| 2102 | COMSTAT status; |
| 2103 | DWORD comerr; |
| 2104 | |
| 2105 | ClearCommError(s->hcom, &comerr, &status); |
| 2106 | if (status.cbInQue > 0) { |
| 2107 | s->len = status.cbInQue; |
| 2108 | win_chr_read_poll(s); |
| 2109 | win_chr_read(s); |
| 2110 | return 1; |
| 2111 | } |
| 2112 | return 0; |
| 2113 | } |
| 2114 | |
| 2115 | static void win_chr_add_read_handler(CharDriverState *chr, |
| 2116 | IOCanRWHandler *fd_can_read, |
| 2117 | IOReadHandler *fd_read, void *opaque) |
| 2118 | { |
| 2119 | WinCharState *s = chr->opaque; |
| 2120 | |
| 2121 | s->fd_can_read = fd_can_read; |
| 2122 | s->fd_read = fd_read; |
| 2123 | s->win_opaque = opaque; |
| 2124 | } |
| 2125 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2126 | static CharDriverState *qemu_chr_open_win(const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2127 | { |
| 2128 | CharDriverState *chr; |
| 2129 | WinCharState *s; |
| 2130 | |
| 2131 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2132 | if (!chr) |
| 2133 | return NULL; |
| 2134 | s = qemu_mallocz(sizeof(WinCharState)); |
| 2135 | if (!s) { |
| 2136 | free(chr); |
| 2137 | return NULL; |
| 2138 | } |
| 2139 | chr->opaque = s; |
| 2140 | chr->chr_write = win_chr_write; |
| 2141 | chr->chr_add_read_handler = win_chr_add_read_handler; |
| 2142 | chr->chr_close = win_chr_close; |
| 2143 | |
| 2144 | if (win_chr_init(s, filename) < 0) { |
| 2145 | free(s); |
| 2146 | free(chr); |
| 2147 | return NULL; |
| 2148 | } |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2149 | qemu_chr_reset(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2150 | return chr; |
| 2151 | } |
| 2152 | |
| 2153 | static int win_chr_pipe_poll(void *opaque) |
| 2154 | { |
| 2155 | WinCharState *s = opaque; |
| 2156 | DWORD size; |
| 2157 | |
| 2158 | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
| 2159 | if (size > 0) { |
| 2160 | s->len = size; |
| 2161 | win_chr_read_poll(s); |
| 2162 | win_chr_read(s); |
| 2163 | return 1; |
| 2164 | } |
| 2165 | return 0; |
| 2166 | } |
| 2167 | |
| 2168 | static int win_chr_pipe_init(WinCharState *s, const char *filename) |
| 2169 | { |
| 2170 | OVERLAPPED ov; |
| 2171 | int ret; |
| 2172 | DWORD size; |
| 2173 | char openname[256]; |
| 2174 | |
| 2175 | s->fpipe = TRUE; |
| 2176 | |
| 2177 | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2178 | if (!s->hsend) { |
| 2179 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2180 | goto fail; |
| 2181 | } |
| 2182 | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2183 | if (!s->hrecv) { |
| 2184 | fprintf(stderr, "Failed CreateEvent\n"); |
| 2185 | goto fail; |
| 2186 | } |
| 2187 | |
| 2188 | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
| 2189 | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
| 2190 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
| 2191 | PIPE_WAIT, |
| 2192 | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL); |
| 2193 | if (s->hcom == INVALID_HANDLE_VALUE) { |
| 2194 | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError()); |
| 2195 | s->hcom = NULL; |
| 2196 | goto fail; |
| 2197 | } |
| 2198 | |
| 2199 | ZeroMemory(&ov, sizeof(ov)); |
| 2200 | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 2201 | ret = ConnectNamedPipe(s->hcom, &ov); |
| 2202 | if (ret) { |
| 2203 | fprintf(stderr, "Failed ConnectNamedPipe\n"); |
| 2204 | goto fail; |
| 2205 | } |
| 2206 | |
| 2207 | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
| 2208 | if (!ret) { |
| 2209 | fprintf(stderr, "Failed GetOverlappedResult\n"); |
| 2210 | if (ov.hEvent) { |
| 2211 | CloseHandle(ov.hEvent); |
| 2212 | ov.hEvent = NULL; |
| 2213 | } |
| 2214 | goto fail; |
| 2215 | } |
| 2216 | |
| 2217 | if (ov.hEvent) { |
| 2218 | CloseHandle(ov.hEvent); |
| 2219 | ov.hEvent = NULL; |
| 2220 | } |
| 2221 | qemu_add_polling_cb(win_chr_pipe_poll, s); |
| 2222 | return 0; |
| 2223 | |
| 2224 | fail: |
| 2225 | win_chr_close2(s); |
| 2226 | return -1; |
| 2227 | } |
| 2228 | |
| 2229 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2230 | static CharDriverState *qemu_chr_open_win_pipe(const char *filename) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2231 | { |
| 2232 | CharDriverState *chr; |
| 2233 | WinCharState *s; |
| 2234 | |
| 2235 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2236 | if (!chr) |
| 2237 | return NULL; |
| 2238 | s = qemu_mallocz(sizeof(WinCharState)); |
| 2239 | if (!s) { |
| 2240 | free(chr); |
| 2241 | return NULL; |
| 2242 | } |
| 2243 | chr->opaque = s; |
| 2244 | chr->chr_write = win_chr_write; |
| 2245 | chr->chr_add_read_handler = win_chr_add_read_handler; |
| 2246 | chr->chr_close = win_chr_close; |
| 2247 | |
| 2248 | if (win_chr_pipe_init(s, filename) < 0) { |
| 2249 | free(s); |
| 2250 | free(chr); |
| 2251 | return NULL; |
| 2252 | } |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2253 | qemu_chr_reset(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2254 | return chr; |
| 2255 | } |
| 2256 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2257 | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2258 | { |
| 2259 | CharDriverState *chr; |
| 2260 | WinCharState *s; |
| 2261 | |
| 2262 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2263 | if (!chr) |
| 2264 | return NULL; |
| 2265 | s = qemu_mallocz(sizeof(WinCharState)); |
| 2266 | if (!s) { |
| 2267 | free(chr); |
| 2268 | return NULL; |
| 2269 | } |
| 2270 | s->hcom = fd_out; |
| 2271 | chr->opaque = s; |
| 2272 | chr->chr_write = win_chr_write; |
| 2273 | chr->chr_add_read_handler = win_chr_add_read_handler; |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2274 | qemu_chr_reset(chr); |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2275 | return chr; |
| 2276 | } |
| 2277 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2278 | static CharDriverState *qemu_chr_open_win_file_out(const char *file_out) |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2279 | { |
| 2280 | HANDLE fd_out; |
| 2281 | |
| 2282 | fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL, |
| 2283 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 2284 | if (fd_out == INVALID_HANDLE_VALUE) |
| 2285 | return NULL; |
| 2286 | |
| 2287 | return qemu_chr_open_win_file(fd_out); |
| 2288 | } |
| 2289 | #endif |
| 2290 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2291 | /***********************************************************/ |
| 2292 | /* UDP Net console */ |
| 2293 | |
| 2294 | typedef struct { |
| 2295 | IOCanRWHandler *fd_can_read; |
| 2296 | IOReadHandler *fd_read; |
| 2297 | void *fd_opaque; |
| 2298 | int fd; |
| 2299 | struct sockaddr_in daddr; |
| 2300 | char buf[1024]; |
| 2301 | int bufcnt; |
| 2302 | int bufptr; |
| 2303 | int max_size; |
| 2304 | } NetCharDriver; |
| 2305 | |
| 2306 | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2307 | { |
| 2308 | NetCharDriver *s = chr->opaque; |
| 2309 | |
| 2310 | return sendto(s->fd, buf, len, 0, |
| 2311 | (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in)); |
| 2312 | } |
| 2313 | |
| 2314 | static int udp_chr_read_poll(void *opaque) |
| 2315 | { |
| 2316 | CharDriverState *chr = opaque; |
| 2317 | NetCharDriver *s = chr->opaque; |
| 2318 | |
| 2319 | s->max_size = s->fd_can_read(s->fd_opaque); |
| 2320 | |
| 2321 | /* If there were any stray characters in the queue process them |
| 2322 | * first |
| 2323 | */ |
| 2324 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
| 2325 | s->fd_read(s->fd_opaque, &s->buf[s->bufptr], 1); |
| 2326 | s->bufptr++; |
| 2327 | s->max_size = s->fd_can_read(s->fd_opaque); |
| 2328 | } |
| 2329 | return s->max_size; |
| 2330 | } |
| 2331 | |
| 2332 | static void udp_chr_read(void *opaque) |
| 2333 | { |
| 2334 | CharDriverState *chr = opaque; |
| 2335 | NetCharDriver *s = chr->opaque; |
| 2336 | |
| 2337 | if (s->max_size == 0) |
| 2338 | return; |
| 2339 | s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0); |
| 2340 | s->bufptr = s->bufcnt; |
| 2341 | if (s->bufcnt <= 0) |
| 2342 | return; |
| 2343 | |
| 2344 | s->bufptr = 0; |
| 2345 | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
| 2346 | s->fd_read(s->fd_opaque, &s->buf[s->bufptr], 1); |
| 2347 | s->bufptr++; |
| 2348 | s->max_size = s->fd_can_read(s->fd_opaque); |
| 2349 | } |
| 2350 | } |
| 2351 | |
| 2352 | static void udp_chr_add_read_handler(CharDriverState *chr, |
| 2353 | IOCanRWHandler *fd_can_read, |
| 2354 | IOReadHandler *fd_read, void *opaque) |
| 2355 | { |
| 2356 | NetCharDriver *s = chr->opaque; |
| 2357 | |
| 2358 | if (s->fd >= 0) { |
| 2359 | s->fd_can_read = fd_can_read; |
| 2360 | s->fd_read = fd_read; |
| 2361 | s->fd_opaque = opaque; |
| 2362 | qemu_set_fd_handler2(s->fd, udp_chr_read_poll, |
| 2363 | udp_chr_read, NULL, chr); |
| 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | int parse_host_port(struct sockaddr_in *saddr, const char *str); |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2368 | #ifndef _WIN32 |
| 2369 | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str); |
| 2370 | #endif |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2371 | int parse_host_src_port(struct sockaddr_in *haddr, |
| 2372 | struct sockaddr_in *saddr, |
| 2373 | const char *str); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2374 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2375 | static CharDriverState *qemu_chr_open_udp(const char *def) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2376 | { |
| 2377 | CharDriverState *chr = NULL; |
| 2378 | NetCharDriver *s = NULL; |
| 2379 | int fd = -1; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2380 | struct sockaddr_in saddr; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2381 | |
| 2382 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2383 | if (!chr) |
| 2384 | goto return_err; |
| 2385 | s = qemu_mallocz(sizeof(NetCharDriver)); |
| 2386 | if (!s) |
| 2387 | goto return_err; |
| 2388 | |
| 2389 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 2390 | if (fd < 0) { |
| 2391 | perror("socket(PF_INET, SOCK_DGRAM)"); |
| 2392 | goto return_err; |
| 2393 | } |
| 2394 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2395 | if (parse_host_src_port(&s->daddr, &saddr, def) < 0) { |
| 2396 | printf("Could not parse: %s\n", def); |
| 2397 | goto return_err; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2400 | if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2401 | { |
| 2402 | perror("bind"); |
| 2403 | goto return_err; |
| 2404 | } |
| 2405 | |
| 2406 | s->fd = fd; |
| 2407 | s->bufcnt = 0; |
| 2408 | s->bufptr = 0; |
| 2409 | chr->opaque = s; |
| 2410 | chr->chr_write = udp_chr_write; |
| 2411 | chr->chr_add_read_handler = udp_chr_add_read_handler; |
| 2412 | return chr; |
| 2413 | |
| 2414 | return_err: |
| 2415 | if (chr) |
| 2416 | free(chr); |
| 2417 | if (s) |
| 2418 | free(s); |
| 2419 | if (fd >= 0) |
| 2420 | closesocket(fd); |
| 2421 | return NULL; |
| 2422 | } |
| 2423 | |
| 2424 | /***********************************************************/ |
| 2425 | /* TCP Net console */ |
| 2426 | |
| 2427 | typedef struct { |
| 2428 | IOCanRWHandler *fd_can_read; |
| 2429 | IOReadHandler *fd_read; |
| 2430 | void *fd_opaque; |
| 2431 | int fd, listen_fd; |
| 2432 | int connected; |
| 2433 | int max_size; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2434 | int do_telnetopt; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2435 | int is_unix; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2436 | } TCPCharDriver; |
| 2437 | |
| 2438 | static void tcp_chr_accept(void *opaque); |
| 2439 | |
| 2440 | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
| 2441 | { |
| 2442 | TCPCharDriver *s = chr->opaque; |
| 2443 | if (s->connected) { |
| 2444 | return send_all(s->fd, buf, len); |
| 2445 | } else { |
| 2446 | /* XXX: indicate an error ? */ |
| 2447 | return len; |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | static int tcp_chr_read_poll(void *opaque) |
| 2452 | { |
| 2453 | CharDriverState *chr = opaque; |
| 2454 | TCPCharDriver *s = chr->opaque; |
| 2455 | if (!s->connected) |
| 2456 | return 0; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2457 | if (!s->fd_can_read) |
| 2458 | return 0; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2459 | s->max_size = s->fd_can_read(s->fd_opaque); |
| 2460 | return s->max_size; |
| 2461 | } |
| 2462 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2463 | #define IAC 255 |
| 2464 | #define IAC_BREAK 243 |
| 2465 | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
| 2466 | TCPCharDriver *s, |
| 2467 | char *buf, int *size) |
| 2468 | { |
| 2469 | /* Handle any telnet client's basic IAC options to satisfy char by |
| 2470 | * char mode with no echo. All IAC options will be removed from |
| 2471 | * the buf and the do_telnetopt variable will be used to track the |
| 2472 | * state of the width of the IAC information. |
| 2473 | * |
| 2474 | * IAC commands come in sets of 3 bytes with the exception of the |
| 2475 | * "IAC BREAK" command and the double IAC. |
| 2476 | */ |
| 2477 | |
| 2478 | int i; |
| 2479 | int j = 0; |
| 2480 | |
| 2481 | for (i = 0; i < *size; i++) { |
| 2482 | if (s->do_telnetopt > 1) { |
| 2483 | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
| 2484 | /* Double IAC means send an IAC */ |
| 2485 | if (j != i) |
| 2486 | buf[j] = buf[i]; |
| 2487 | j++; |
| 2488 | s->do_telnetopt = 1; |
| 2489 | } else { |
| 2490 | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
| 2491 | /* Handle IAC break commands by sending a serial break */ |
| 2492 | chr->chr_event(s->fd_opaque, CHR_EVENT_BREAK); |
| 2493 | s->do_telnetopt++; |
| 2494 | } |
| 2495 | s->do_telnetopt++; |
| 2496 | } |
| 2497 | if (s->do_telnetopt >= 4) { |
| 2498 | s->do_telnetopt = 1; |
| 2499 | } |
| 2500 | } else { |
| 2501 | if ((unsigned char)buf[i] == IAC) { |
| 2502 | s->do_telnetopt = 2; |
| 2503 | } else { |
| 2504 | if (j != i) |
| 2505 | buf[j] = buf[i]; |
| 2506 | j++; |
| 2507 | } |
| 2508 | } |
| 2509 | } |
| 2510 | *size = j; |
| 2511 | } |
| 2512 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2513 | static void tcp_chr_read(void *opaque) |
| 2514 | { |
| 2515 | CharDriverState *chr = opaque; |
| 2516 | TCPCharDriver *s = chr->opaque; |
| 2517 | uint8_t buf[1024]; |
| 2518 | int len, size; |
| 2519 | |
| 2520 | if (!s->connected || s->max_size <= 0) |
| 2521 | return; |
| 2522 | len = sizeof(buf); |
| 2523 | if (len > s->max_size) |
| 2524 | len = s->max_size; |
| 2525 | size = recv(s->fd, buf, len, 0); |
| 2526 | if (size == 0) { |
| 2527 | /* connection closed */ |
| 2528 | s->connected = 0; |
| 2529 | if (s->listen_fd >= 0) { |
| 2530 | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr); |
| 2531 | } |
| 2532 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 2533 | closesocket(s->fd); |
| 2534 | s->fd = -1; |
| 2535 | } else if (size > 0) { |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2536 | if (s->do_telnetopt) |
| 2537 | tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
| 2538 | if (size > 0) |
| 2539 | s->fd_read(s->fd_opaque, buf, size); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2540 | } |
| 2541 | } |
| 2542 | |
| 2543 | static void tcp_chr_add_read_handler(CharDriverState *chr, |
| 2544 | IOCanRWHandler *fd_can_read, |
| 2545 | IOReadHandler *fd_read, void *opaque) |
| 2546 | { |
| 2547 | TCPCharDriver *s = chr->opaque; |
| 2548 | |
| 2549 | s->fd_can_read = fd_can_read; |
| 2550 | s->fd_read = fd_read; |
| 2551 | s->fd_opaque = opaque; |
| 2552 | } |
| 2553 | |
| 2554 | static void tcp_chr_connect(void *opaque) |
| 2555 | { |
| 2556 | CharDriverState *chr = opaque; |
| 2557 | TCPCharDriver *s = chr->opaque; |
| 2558 | |
| 2559 | s->connected = 1; |
| 2560 | qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, |
| 2561 | tcp_chr_read, NULL, chr); |
ths | 86e94de | 2007-01-05 22:01:59 +0000 | [diff] [blame] | 2562 | qemu_chr_reset(chr); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2565 | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
| 2566 | static void tcp_chr_telnet_init(int fd) |
| 2567 | { |
| 2568 | char buf[3]; |
| 2569 | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */ |
| 2570 | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
| 2571 | send(fd, (char *)buf, 3, 0); |
| 2572 | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
| 2573 | send(fd, (char *)buf, 3, 0); |
| 2574 | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
| 2575 | send(fd, (char *)buf, 3, 0); |
| 2576 | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
| 2577 | send(fd, (char *)buf, 3, 0); |
| 2578 | } |
| 2579 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2580 | static void tcp_chr_accept(void *opaque) |
| 2581 | { |
| 2582 | CharDriverState *chr = opaque; |
| 2583 | TCPCharDriver *s = chr->opaque; |
| 2584 | struct sockaddr_in saddr; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2585 | #ifndef _WIN32 |
| 2586 | struct sockaddr_un uaddr; |
| 2587 | #endif |
| 2588 | struct sockaddr *addr; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2589 | socklen_t len; |
| 2590 | int fd; |
| 2591 | |
| 2592 | for(;;) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2593 | #ifndef _WIN32 |
| 2594 | if (s->is_unix) { |
| 2595 | len = sizeof(uaddr); |
| 2596 | addr = (struct sockaddr *)&uaddr; |
| 2597 | } else |
| 2598 | #endif |
| 2599 | { |
| 2600 | len = sizeof(saddr); |
| 2601 | addr = (struct sockaddr *)&saddr; |
| 2602 | } |
| 2603 | fd = accept(s->listen_fd, addr, &len); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2604 | if (fd < 0 && errno != EINTR) { |
| 2605 | return; |
| 2606 | } else if (fd >= 0) { |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2607 | if (s->do_telnetopt) |
| 2608 | tcp_chr_telnet_init(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2609 | break; |
| 2610 | } |
| 2611 | } |
| 2612 | socket_set_nonblock(fd); |
| 2613 | s->fd = fd; |
| 2614 | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
| 2615 | tcp_chr_connect(chr); |
| 2616 | } |
| 2617 | |
| 2618 | static void tcp_chr_close(CharDriverState *chr) |
| 2619 | { |
| 2620 | TCPCharDriver *s = chr->opaque; |
| 2621 | if (s->fd >= 0) |
| 2622 | closesocket(s->fd); |
| 2623 | if (s->listen_fd >= 0) |
| 2624 | closesocket(s->listen_fd); |
| 2625 | qemu_free(s); |
| 2626 | } |
| 2627 | |
| 2628 | static CharDriverState *qemu_chr_open_tcp(const char *host_str, |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2629 | int is_telnet, |
| 2630 | int is_unix) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2631 | { |
| 2632 | CharDriverState *chr = NULL; |
| 2633 | TCPCharDriver *s = NULL; |
| 2634 | int fd = -1, ret, err, val; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2635 | int is_listen = 0; |
| 2636 | int is_waitconnect = 1; |
| 2637 | const char *ptr; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2638 | struct sockaddr_in saddr; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2639 | #ifndef _WIN32 |
| 2640 | struct sockaddr_un uaddr; |
| 2641 | #endif |
| 2642 | struct sockaddr *addr; |
| 2643 | socklen_t addrlen; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2644 | |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2645 | #ifndef _WIN32 |
| 2646 | if (is_unix) { |
| 2647 | addr = (struct sockaddr *)&uaddr; |
| 2648 | addrlen = sizeof(uaddr); |
| 2649 | if (parse_unix_path(&uaddr, host_str) < 0) |
| 2650 | goto fail; |
| 2651 | } else |
| 2652 | #endif |
| 2653 | { |
| 2654 | addr = (struct sockaddr *)&saddr; |
| 2655 | addrlen = sizeof(saddr); |
| 2656 | if (parse_host_port(&saddr, host_str) < 0) |
| 2657 | goto fail; |
| 2658 | } |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2659 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2660 | ptr = host_str; |
| 2661 | while((ptr = strchr(ptr,','))) { |
| 2662 | ptr++; |
| 2663 | if (!strncmp(ptr,"server",6)) { |
| 2664 | is_listen = 1; |
| 2665 | } else if (!strncmp(ptr,"nowait",6)) { |
| 2666 | is_waitconnect = 0; |
| 2667 | } else { |
| 2668 | printf("Unknown option: %s\n", ptr); |
| 2669 | goto fail; |
| 2670 | } |
| 2671 | } |
| 2672 | if (!is_listen) |
| 2673 | is_waitconnect = 0; |
| 2674 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2675 | chr = qemu_mallocz(sizeof(CharDriverState)); |
| 2676 | if (!chr) |
| 2677 | goto fail; |
| 2678 | s = qemu_mallocz(sizeof(TCPCharDriver)); |
| 2679 | if (!s) |
| 2680 | goto fail; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2681 | |
| 2682 | #ifndef _WIN32 |
| 2683 | if (is_unix) |
| 2684 | fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 2685 | else |
| 2686 | #endif |
| 2687 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 2688 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2689 | if (fd < 0) |
| 2690 | goto fail; |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2691 | |
| 2692 | if (!is_waitconnect) |
| 2693 | socket_set_nonblock(fd); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2694 | |
| 2695 | s->connected = 0; |
| 2696 | s->fd = -1; |
| 2697 | s->listen_fd = -1; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2698 | s->is_unix = is_unix; |
| 2699 | |
| 2700 | chr->opaque = s; |
| 2701 | chr->chr_write = tcp_chr_write; |
| 2702 | chr->chr_add_read_handler = tcp_chr_add_read_handler; |
| 2703 | chr->chr_close = tcp_chr_close; |
| 2704 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2705 | if (is_listen) { |
| 2706 | /* allow fast reuse */ |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2707 | #ifndef _WIN32 |
| 2708 | if (is_unix) { |
| 2709 | char path[109]; |
| 2710 | strncpy(path, uaddr.sun_path, 108); |
| 2711 | path[108] = 0; |
| 2712 | unlink(path); |
| 2713 | } else |
| 2714 | #endif |
| 2715 | { |
| 2716 | val = 1; |
| 2717 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
| 2718 | } |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2719 | |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2720 | ret = bind(fd, addr, addrlen); |
| 2721 | if (ret < 0) |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2722 | goto fail; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2723 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2724 | ret = listen(fd, 0); |
| 2725 | if (ret < 0) |
| 2726 | goto fail; |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2727 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2728 | s->listen_fd = fd; |
| 2729 | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr); |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2730 | if (is_telnet) |
| 2731 | s->do_telnetopt = 1; |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2732 | } else { |
| 2733 | for(;;) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2734 | ret = connect(fd, addr, addrlen); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2735 | if (ret < 0) { |
| 2736 | err = socket_error(); |
| 2737 | if (err == EINTR || err == EWOULDBLOCK) { |
| 2738 | } else if (err == EINPROGRESS) { |
| 2739 | break; |
| 2740 | } else { |
| 2741 | goto fail; |
| 2742 | } |
| 2743 | } else { |
| 2744 | s->connected = 1; |
| 2745 | break; |
| 2746 | } |
| 2747 | } |
| 2748 | s->fd = fd; |
| 2749 | if (s->connected) |
| 2750 | tcp_chr_connect(chr); |
| 2751 | else |
| 2752 | qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr); |
| 2753 | } |
| 2754 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2755 | if (is_listen && is_waitconnect) { |
| 2756 | printf("QEMU waiting for connection on: %s\n", host_str); |
| 2757 | tcp_chr_accept(chr); |
| 2758 | socket_set_nonblock(s->listen_fd); |
| 2759 | } |
| 2760 | |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2761 | return chr; |
| 2762 | fail: |
| 2763 | if (fd >= 0) |
| 2764 | closesocket(fd); |
| 2765 | qemu_free(s); |
| 2766 | qemu_free(chr); |
| 2767 | return NULL; |
| 2768 | } |
| 2769 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2770 | CharDriverState *qemu_chr_open(const char *filename) |
| 2771 | { |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2772 | const char *p; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 2773 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2774 | if (!strcmp(filename, "vc")) { |
| 2775 | return text_console_init(&display_state); |
| 2776 | } else if (!strcmp(filename, "null")) { |
| 2777 | return qemu_chr_open_null(); |
bellard | 7664728 | 2005-11-27 19:10:42 +0000 | [diff] [blame] | 2778 | } else |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2779 | if (strstart(filename, "tcp:", &p)) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2780 | return qemu_chr_open_tcp(p, 0, 0); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2781 | } else |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2782 | if (strstart(filename, "telnet:", &p)) { |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2783 | return qemu_chr_open_tcp(p, 1, 0); |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 2784 | } else |
| 2785 | if (strstart(filename, "udp:", &p)) { |
| 2786 | return qemu_chr_open_udp(p); |
| 2787 | } else |
bellard | 7664728 | 2005-11-27 19:10:42 +0000 | [diff] [blame] | 2788 | #ifndef _WIN32 |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2789 | if (strstart(filename, "unix:", &p)) { |
| 2790 | return qemu_chr_open_tcp(p, 0, 1); |
| 2791 | } else if (strstart(filename, "file:", &p)) { |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2792 | return qemu_chr_open_file_out(p); |
| 2793 | } else if (strstart(filename, "pipe:", &p)) { |
| 2794 | return qemu_chr_open_pipe(p); |
bellard | 7664728 | 2005-11-27 19:10:42 +0000 | [diff] [blame] | 2795 | } else if (!strcmp(filename, "pty")) { |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2796 | return qemu_chr_open_pty(); |
| 2797 | } else if (!strcmp(filename, "stdio")) { |
| 2798 | return qemu_chr_open_stdio(); |
| 2799 | } else |
| 2800 | #endif |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2801 | #if defined(__linux__) |
bellard | e57a8c0 | 2005-11-10 23:58:52 +0000 | [diff] [blame] | 2802 | if (strstart(filename, "/dev/parport", NULL)) { |
| 2803 | return qemu_chr_open_pp(filename); |
| 2804 | } else |
bellard | f8d179e | 2005-11-08 22:30:36 +0000 | [diff] [blame] | 2805 | if (strstart(filename, "/dev/", NULL)) { |
| 2806 | return qemu_chr_open_tty(filename); |
| 2807 | } else |
| 2808 | #endif |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2809 | #ifdef _WIN32 |
| 2810 | if (strstart(filename, "COM", NULL)) { |
| 2811 | return qemu_chr_open_win(filename); |
| 2812 | } else |
| 2813 | if (strstart(filename, "pipe:", &p)) { |
| 2814 | return qemu_chr_open_win_pipe(p); |
| 2815 | } else |
| 2816 | if (strstart(filename, "file:", &p)) { |
| 2817 | return qemu_chr_open_win_file_out(p); |
| 2818 | } |
| 2819 | #endif |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 2820 | { |
| 2821 | return NULL; |
| 2822 | } |
| 2823 | } |
| 2824 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 2825 | void qemu_chr_close(CharDriverState *chr) |
| 2826 | { |
| 2827 | if (chr->chr_close) |
| 2828 | chr->chr_close(chr); |
| 2829 | } |
| 2830 | |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 2831 | /***********************************************************/ |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2832 | /* network device redirectors */ |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 2833 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2834 | void hex_dump(FILE *f, const uint8_t *buf, int size) |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 2835 | { |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2836 | int len, i, j, c; |
| 2837 | |
| 2838 | for(i=0;i<size;i+=16) { |
| 2839 | len = size - i; |
| 2840 | if (len > 16) |
| 2841 | len = 16; |
| 2842 | fprintf(f, "%08x ", i); |
| 2843 | for(j=0;j<16;j++) { |
| 2844 | if (j < len) |
| 2845 | fprintf(f, " %02x", buf[i+j]); |
| 2846 | else |
| 2847 | fprintf(f, " "); |
| 2848 | } |
| 2849 | fprintf(f, " "); |
| 2850 | for(j=0;j<len;j++) { |
| 2851 | c = buf[i+j]; |
| 2852 | if (c < ' ' || c > '~') |
| 2853 | c = '.'; |
| 2854 | fprintf(f, "%c", c); |
| 2855 | } |
| 2856 | fprintf(f, "\n"); |
| 2857 | } |
| 2858 | } |
| 2859 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2860 | static int parse_macaddr(uint8_t *macaddr, const char *p) |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2861 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2862 | int i; |
| 2863 | for(i = 0; i < 6; i++) { |
| 2864 | macaddr[i] = strtol(p, (char **)&p, 16); |
| 2865 | if (i == 5) { |
| 2866 | if (*p != '\0') |
| 2867 | return -1; |
| 2868 | } else { |
| 2869 | if (*p != ':') |
| 2870 | return -1; |
| 2871 | p++; |
| 2872 | } |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2873 | } |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 2874 | return 0; |
| 2875 | } |
| 2876 | |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 2877 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
| 2878 | { |
| 2879 | const char *p, *p1; |
| 2880 | int len; |
| 2881 | p = *pp; |
| 2882 | p1 = strchr(p, sep); |
| 2883 | if (!p1) |
| 2884 | return -1; |
| 2885 | len = p1 - p; |
| 2886 | p1++; |
| 2887 | if (buf_size > 0) { |
| 2888 | if (len > buf_size - 1) |
| 2889 | len = buf_size - 1; |
| 2890 | memcpy(buf, p, len); |
| 2891 | buf[len] = '\0'; |
| 2892 | } |
| 2893 | *pp = p1; |
| 2894 | return 0; |
| 2895 | } |
| 2896 | |
bellard | 951f135 | 2006-06-27 21:02:43 +0000 | [diff] [blame] | 2897 | int parse_host_src_port(struct sockaddr_in *haddr, |
| 2898 | struct sockaddr_in *saddr, |
| 2899 | const char *input_str) |
| 2900 | { |
| 2901 | char *str = strdup(input_str); |
| 2902 | char *host_str = str; |
| 2903 | char *src_str; |
| 2904 | char *ptr; |
| 2905 | |
| 2906 | /* |
| 2907 | * Chop off any extra arguments at the end of the string which |
| 2908 | * would start with a comma, then fill in the src port information |
| 2909 | * if it was provided else use the "any address" and "any port". |
| 2910 | */ |
| 2911 | if ((ptr = strchr(str,','))) |
| 2912 | *ptr = '\0'; |
| 2913 | |
| 2914 | if ((src_str = strchr(input_str,'@'))) { |
| 2915 | *src_str = '\0'; |
| 2916 | src_str++; |
| 2917 | } |
| 2918 | |
| 2919 | if (parse_host_port(haddr, host_str) < 0) |
| 2920 | goto fail; |
| 2921 | |
| 2922 | if (!src_str || *src_str == '\0') |
| 2923 | src_str = ":0"; |
| 2924 | |
| 2925 | if (parse_host_port(saddr, src_str) < 0) |
| 2926 | goto fail; |
| 2927 | |
| 2928 | free(str); |
| 2929 | return(0); |
| 2930 | |
| 2931 | fail: |
| 2932 | free(str); |
| 2933 | return -1; |
| 2934 | } |
| 2935 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2936 | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
| 2937 | { |
| 2938 | char buf[512]; |
| 2939 | struct hostent *he; |
| 2940 | const char *p, *r; |
| 2941 | int port; |
| 2942 | |
| 2943 | p = str; |
| 2944 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 2945 | return -1; |
| 2946 | saddr->sin_family = AF_INET; |
| 2947 | if (buf[0] == '\0') { |
| 2948 | saddr->sin_addr.s_addr = 0; |
| 2949 | } else { |
| 2950 | if (isdigit(buf[0])) { |
| 2951 | if (!inet_aton(buf, &saddr->sin_addr)) |
| 2952 | return -1; |
| 2953 | } else { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2954 | if ((he = gethostbyname(buf)) == NULL) |
| 2955 | return - 1; |
| 2956 | saddr->sin_addr = *(struct in_addr *)he->h_addr; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2957 | } |
| 2958 | } |
| 2959 | port = strtol(p, (char **)&r, 0); |
| 2960 | if (r == p) |
| 2961 | return -1; |
| 2962 | saddr->sin_port = htons(port); |
| 2963 | return 0; |
| 2964 | } |
| 2965 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2966 | #ifndef _WIN32 |
| 2967 | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2968 | { |
| 2969 | const char *p; |
| 2970 | int len; |
| 2971 | |
| 2972 | len = MIN(108, strlen(str)); |
| 2973 | p = strchr(str, ','); |
| 2974 | if (p) |
| 2975 | len = MIN(len, p - str); |
| 2976 | |
| 2977 | memset(uaddr, 0, sizeof(*uaddr)); |
| 2978 | |
| 2979 | uaddr->sun_family = AF_UNIX; |
| 2980 | memcpy(uaddr->sun_path, str, len); |
| 2981 | |
| 2982 | return 0; |
| 2983 | } |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 2984 | #endif |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 2985 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 2986 | /* find or alloc a new VLAN */ |
| 2987 | VLANState *qemu_find_vlan(int id) |
| 2988 | { |
| 2989 | VLANState **pvlan, *vlan; |
| 2990 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
| 2991 | if (vlan->id == id) |
| 2992 | return vlan; |
| 2993 | } |
| 2994 | vlan = qemu_mallocz(sizeof(VLANState)); |
| 2995 | if (!vlan) |
| 2996 | return NULL; |
| 2997 | vlan->id = id; |
| 2998 | vlan->next = NULL; |
| 2999 | pvlan = &first_vlan; |
| 3000 | while (*pvlan != NULL) |
| 3001 | pvlan = &(*pvlan)->next; |
| 3002 | *pvlan = vlan; |
| 3003 | return vlan; |
| 3004 | } |
| 3005 | |
| 3006 | VLANClientState *qemu_new_vlan_client(VLANState *vlan, |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3007 | IOReadHandler *fd_read, |
| 3008 | IOCanRWHandler *fd_can_read, |
| 3009 | void *opaque) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3010 | { |
| 3011 | VLANClientState *vc, **pvc; |
| 3012 | vc = qemu_mallocz(sizeof(VLANClientState)); |
| 3013 | if (!vc) |
| 3014 | return NULL; |
| 3015 | vc->fd_read = fd_read; |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3016 | vc->fd_can_read = fd_can_read; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3017 | vc->opaque = opaque; |
| 3018 | vc->vlan = vlan; |
| 3019 | |
| 3020 | vc->next = NULL; |
| 3021 | pvc = &vlan->first_client; |
| 3022 | while (*pvc != NULL) |
| 3023 | pvc = &(*pvc)->next; |
| 3024 | *pvc = vc; |
| 3025 | return vc; |
| 3026 | } |
| 3027 | |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3028 | int qemu_can_send_packet(VLANClientState *vc1) |
| 3029 | { |
| 3030 | VLANState *vlan = vc1->vlan; |
| 3031 | VLANClientState *vc; |
| 3032 | |
| 3033 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
| 3034 | if (vc != vc1) { |
| 3035 | if (vc->fd_can_read && !vc->fd_can_read(vc->opaque)) |
| 3036 | return 0; |
| 3037 | } |
| 3038 | } |
| 3039 | return 1; |
| 3040 | } |
| 3041 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3042 | void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size) |
| 3043 | { |
| 3044 | VLANState *vlan = vc1->vlan; |
| 3045 | VLANClientState *vc; |
| 3046 | |
| 3047 | #if 0 |
| 3048 | printf("vlan %d send:\n", vlan->id); |
| 3049 | hex_dump(stdout, buf, size); |
| 3050 | #endif |
| 3051 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
| 3052 | if (vc != vc1) { |
| 3053 | vc->fd_read(vc->opaque, buf, size); |
| 3054 | } |
| 3055 | } |
| 3056 | } |
| 3057 | |
| 3058 | #if defined(CONFIG_SLIRP) |
| 3059 | |
| 3060 | /* slirp network adapter */ |
| 3061 | |
| 3062 | static int slirp_inited; |
| 3063 | static VLANClientState *slirp_vc; |
| 3064 | |
| 3065 | int slirp_can_output(void) |
| 3066 | { |
pbrook | 3b7f5d4 | 2006-02-10 17:34:02 +0000 | [diff] [blame] | 3067 | return !slirp_vc || qemu_can_send_packet(slirp_vc); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
| 3070 | void slirp_output(const uint8_t *pkt, int pkt_len) |
| 3071 | { |
| 3072 | #if 0 |
| 3073 | printf("slirp output:\n"); |
| 3074 | hex_dump(stdout, pkt, pkt_len); |
| 3075 | #endif |
pbrook | 3b7f5d4 | 2006-02-10 17:34:02 +0000 | [diff] [blame] | 3076 | if (!slirp_vc) |
| 3077 | return; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3078 | qemu_send_packet(slirp_vc, pkt, pkt_len); |
| 3079 | } |
| 3080 | |
| 3081 | static void slirp_receive(void *opaque, const uint8_t *buf, int size) |
| 3082 | { |
| 3083 | #if 0 |
| 3084 | printf("slirp input:\n"); |
| 3085 | hex_dump(stdout, buf, size); |
| 3086 | #endif |
| 3087 | slirp_input(buf, size); |
| 3088 | } |
| 3089 | |
| 3090 | static int net_slirp_init(VLANState *vlan) |
| 3091 | { |
| 3092 | if (!slirp_inited) { |
| 3093 | slirp_inited = 1; |
| 3094 | slirp_init(); |
| 3095 | } |
| 3096 | slirp_vc = qemu_new_vlan_client(vlan, |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3097 | slirp_receive, NULL, NULL); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3098 | snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector"); |
| 3099 | return 0; |
| 3100 | } |
| 3101 | |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3102 | static void net_slirp_redir(const char *redir_str) |
| 3103 | { |
| 3104 | int is_udp; |
| 3105 | char buf[256], *r; |
| 3106 | const char *p; |
| 3107 | struct in_addr guest_addr; |
| 3108 | int host_port, guest_port; |
| 3109 | |
| 3110 | if (!slirp_inited) { |
| 3111 | slirp_inited = 1; |
| 3112 | slirp_init(); |
| 3113 | } |
| 3114 | |
| 3115 | p = redir_str; |
| 3116 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3117 | goto fail; |
| 3118 | if (!strcmp(buf, "tcp")) { |
| 3119 | is_udp = 0; |
| 3120 | } else if (!strcmp(buf, "udp")) { |
| 3121 | is_udp = 1; |
| 3122 | } else { |
| 3123 | goto fail; |
| 3124 | } |
| 3125 | |
| 3126 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3127 | goto fail; |
| 3128 | host_port = strtol(buf, &r, 0); |
| 3129 | if (r == buf) |
| 3130 | goto fail; |
| 3131 | |
| 3132 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
| 3133 | goto fail; |
| 3134 | if (buf[0] == '\0') { |
| 3135 | pstrcpy(buf, sizeof(buf), "10.0.2.15"); |
| 3136 | } |
| 3137 | if (!inet_aton(buf, &guest_addr)) |
| 3138 | goto fail; |
| 3139 | |
| 3140 | guest_port = strtol(p, &r, 0); |
| 3141 | if (r == p) |
| 3142 | goto fail; |
| 3143 | |
| 3144 | if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { |
| 3145 | fprintf(stderr, "qemu: could not set up redirection\n"); |
| 3146 | exit(1); |
| 3147 | } |
| 3148 | return; |
| 3149 | fail: |
| 3150 | fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n"); |
| 3151 | exit(1); |
| 3152 | } |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3153 | |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 3154 | #ifndef _WIN32 |
| 3155 | |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3156 | char smb_dir[1024]; |
| 3157 | |
| 3158 | static void smb_exit(void) |
| 3159 | { |
| 3160 | DIR *d; |
| 3161 | struct dirent *de; |
| 3162 | char filename[1024]; |
| 3163 | |
| 3164 | /* erase all the files in the directory */ |
| 3165 | d = opendir(smb_dir); |
| 3166 | for(;;) { |
| 3167 | de = readdir(d); |
| 3168 | if (!de) |
| 3169 | break; |
| 3170 | if (strcmp(de->d_name, ".") != 0 && |
| 3171 | strcmp(de->d_name, "..") != 0) { |
| 3172 | snprintf(filename, sizeof(filename), "%s/%s", |
| 3173 | smb_dir, de->d_name); |
| 3174 | unlink(filename); |
| 3175 | } |
| 3176 | } |
bellard | 03ffbb6 | 2004-09-06 00:14:04 +0000 | [diff] [blame] | 3177 | closedir(d); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3178 | rmdir(smb_dir); |
| 3179 | } |
| 3180 | |
| 3181 | /* automatic user mode samba server configuration */ |
| 3182 | void net_slirp_smb(const char *exported_dir) |
| 3183 | { |
| 3184 | char smb_conf[1024]; |
| 3185 | char smb_cmdline[1024]; |
| 3186 | FILE *f; |
| 3187 | |
| 3188 | if (!slirp_inited) { |
| 3189 | slirp_inited = 1; |
| 3190 | slirp_init(); |
| 3191 | } |
| 3192 | |
| 3193 | /* XXX: better tmp dir construction */ |
| 3194 | snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); |
| 3195 | if (mkdir(smb_dir, 0700) < 0) { |
| 3196 | fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir); |
| 3197 | exit(1); |
| 3198 | } |
| 3199 | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); |
| 3200 | |
| 3201 | f = fopen(smb_conf, "w"); |
| 3202 | if (!f) { |
| 3203 | fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf); |
| 3204 | exit(1); |
| 3205 | } |
| 3206 | fprintf(f, |
| 3207 | "[global]\n" |
bellard | 157777e | 2005-03-01 21:28:45 +0000 | [diff] [blame] | 3208 | "private dir=%s\n" |
| 3209 | "smb ports=0\n" |
| 3210 | "socket address=127.0.0.1\n" |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3211 | "pid directory=%s\n" |
| 3212 | "lock directory=%s\n" |
| 3213 | "log file=%s/log.smbd\n" |
| 3214 | "smb passwd file=%s/smbpasswd\n" |
bellard | 03ffbb6 | 2004-09-06 00:14:04 +0000 | [diff] [blame] | 3215 | "security = share\n" |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3216 | "[qemu]\n" |
| 3217 | "path=%s\n" |
| 3218 | "read only=no\n" |
| 3219 | "guest ok=yes\n", |
| 3220 | smb_dir, |
| 3221 | smb_dir, |
| 3222 | smb_dir, |
| 3223 | smb_dir, |
bellard | 157777e | 2005-03-01 21:28:45 +0000 | [diff] [blame] | 3224 | smb_dir, |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3225 | exported_dir |
| 3226 | ); |
| 3227 | fclose(f); |
| 3228 | atexit(smb_exit); |
| 3229 | |
pbrook | a14d6c8 | 2006-12-23 15:37:33 +0000 | [diff] [blame] | 3230 | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
| 3231 | SMBD_COMMAND, smb_conf); |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 3232 | |
| 3233 | slirp_add_exec(0, smb_cmdline, 4, 139); |
| 3234 | } |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 3235 | |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 3236 | #endif /* !defined(_WIN32) */ |
| 3237 | |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3238 | #endif /* CONFIG_SLIRP */ |
| 3239 | |
| 3240 | #if !defined(_WIN32) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3241 | |
| 3242 | typedef struct TAPState { |
| 3243 | VLANClientState *vc; |
| 3244 | int fd; |
| 3245 | } TAPState; |
| 3246 | |
| 3247 | static void tap_receive(void *opaque, const uint8_t *buf, int size) |
| 3248 | { |
| 3249 | TAPState *s = opaque; |
| 3250 | int ret; |
| 3251 | for(;;) { |
| 3252 | ret = write(s->fd, buf, size); |
| 3253 | if (ret < 0 && (errno == EINTR || errno == EAGAIN)) { |
| 3254 | } else { |
| 3255 | break; |
| 3256 | } |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | static void tap_send(void *opaque) |
| 3261 | { |
| 3262 | TAPState *s = opaque; |
| 3263 | uint8_t buf[4096]; |
| 3264 | int size; |
| 3265 | |
| 3266 | size = read(s->fd, buf, sizeof(buf)); |
| 3267 | if (size > 0) { |
| 3268 | qemu_send_packet(s->vc, buf, size); |
| 3269 | } |
| 3270 | } |
| 3271 | |
| 3272 | /* fd support */ |
| 3273 | |
| 3274 | static TAPState *net_tap_fd_init(VLANState *vlan, int fd) |
| 3275 | { |
| 3276 | TAPState *s; |
| 3277 | |
| 3278 | s = qemu_mallocz(sizeof(TAPState)); |
| 3279 | if (!s) |
| 3280 | return NULL; |
| 3281 | s->fd = fd; |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3282 | s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3283 | qemu_set_fd_handler(s->fd, tap_send, NULL, s); |
| 3284 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd); |
| 3285 | return s; |
| 3286 | } |
| 3287 | |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3288 | #ifdef _BSD |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3289 | static int tap_open(char *ifname, int ifname_size) |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3290 | { |
| 3291 | int fd; |
| 3292 | char *dev; |
| 3293 | struct stat s; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 3294 | |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3295 | fd = open("/dev/tap", O_RDWR); |
| 3296 | if (fd < 0) { |
| 3297 | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n"); |
| 3298 | return -1; |
| 3299 | } |
| 3300 | |
| 3301 | fstat(fd, &s); |
| 3302 | dev = devname(s.st_rdev, S_IFCHR); |
| 3303 | pstrcpy(ifname, ifname_size, dev); |
| 3304 | |
| 3305 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 3306 | return fd; |
| 3307 | } |
bellard | ec530c8 | 2006-04-25 22:36:06 +0000 | [diff] [blame] | 3308 | #elif defined(__sun__) |
| 3309 | static int tap_open(char *ifname, int ifname_size) |
| 3310 | { |
| 3311 | fprintf(stderr, "warning: tap_open not yet implemented\n"); |
| 3312 | return -1; |
| 3313 | } |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3314 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3315 | static int tap_open(char *ifname, int ifname_size) |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3316 | { |
| 3317 | struct ifreq ifr; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3318 | int fd, ret; |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3319 | |
| 3320 | fd = open("/dev/net/tun", O_RDWR); |
| 3321 | if (fd < 0) { |
| 3322 | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n"); |
| 3323 | return -1; |
| 3324 | } |
| 3325 | memset(&ifr, 0, sizeof(ifr)); |
| 3326 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3327 | if (ifname[0] != '\0') |
| 3328 | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
| 3329 | else |
| 3330 | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d"); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3331 | ret = ioctl(fd, TUNSETIFF, (void *) &ifr); |
| 3332 | if (ret != 0) { |
| 3333 | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n"); |
| 3334 | close(fd); |
| 3335 | return -1; |
| 3336 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3337 | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3338 | fcntl(fd, F_SETFL, O_NONBLOCK); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3339 | return fd; |
| 3340 | } |
bellard | 7d3505c | 2004-05-12 19:32:15 +0000 | [diff] [blame] | 3341 | #endif |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 3342 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3343 | static int net_tap_init(VLANState *vlan, const char *ifname1, |
| 3344 | const char *setup_script) |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 3345 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3346 | TAPState *s; |
| 3347 | int pid, status, fd; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3348 | char *args[3]; |
| 3349 | char **parg; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3350 | char ifname[128]; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3351 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3352 | if (ifname1 != NULL) |
| 3353 | pstrcpy(ifname, sizeof(ifname), ifname1); |
| 3354 | else |
| 3355 | ifname[0] = '\0'; |
| 3356 | fd = tap_open(ifname, sizeof(ifname)); |
| 3357 | if (fd < 0) |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3358 | return -1; |
| 3359 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3360 | if (!setup_script) |
| 3361 | setup_script = ""; |
| 3362 | if (setup_script[0] != '\0') { |
| 3363 | /* try to launch network init script */ |
| 3364 | pid = fork(); |
| 3365 | if (pid >= 0) { |
| 3366 | if (pid == 0) { |
| 3367 | parg = args; |
| 3368 | *parg++ = (char *)setup_script; |
| 3369 | *parg++ = ifname; |
| 3370 | *parg++ = NULL; |
| 3371 | execv(setup_script, args); |
bellard | 4a38940 | 2005-11-26 20:10:07 +0000 | [diff] [blame] | 3372 | _exit(1); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3373 | } |
| 3374 | while (waitpid(pid, &status, 0) != pid); |
| 3375 | if (!WIFEXITED(status) || |
| 3376 | WEXITSTATUS(status) != 0) { |
| 3377 | fprintf(stderr, "%s: could not launch network script\n", |
| 3378 | setup_script); |
| 3379 | return -1; |
| 3380 | } |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3381 | } |
| 3382 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3383 | s = net_tap_fd_init(vlan, fd); |
| 3384 | if (!s) |
| 3385 | return -1; |
| 3386 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 3387 | "tap: ifname=%s setup_script=%s", ifname, setup_script); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3388 | return 0; |
| 3389 | } |
| 3390 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3391 | #endif /* !_WIN32 */ |
| 3392 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3393 | /* network connection */ |
| 3394 | typedef struct NetSocketState { |
| 3395 | VLANClientState *vc; |
| 3396 | int fd; |
| 3397 | int state; /* 0 = getting length, 1 = getting data */ |
| 3398 | int index; |
| 3399 | int packet_len; |
| 3400 | uint8_t buf[4096]; |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3401 | 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] | 3402 | } NetSocketState; |
| 3403 | |
| 3404 | typedef struct NetSocketListenState { |
| 3405 | VLANState *vlan; |
| 3406 | int fd; |
| 3407 | } NetSocketListenState; |
| 3408 | |
| 3409 | /* XXX: we consider we can send the whole packet without blocking */ |
| 3410 | static void net_socket_receive(void *opaque, const uint8_t *buf, int size) |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3411 | { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3412 | NetSocketState *s = opaque; |
| 3413 | uint32_t len; |
| 3414 | len = htonl(size); |
| 3415 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3416 | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
| 3417 | send_all(s->fd, buf, size); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3418 | } |
| 3419 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3420 | static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size) |
| 3421 | { |
| 3422 | NetSocketState *s = opaque; |
| 3423 | sendto(s->fd, buf, size, 0, |
| 3424 | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
| 3425 | } |
| 3426 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3427 | static void net_socket_send(void *opaque) |
| 3428 | { |
| 3429 | NetSocketState *s = opaque; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3430 | int l, size, err; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3431 | uint8_t buf1[4096]; |
| 3432 | const uint8_t *buf; |
| 3433 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3434 | size = recv(s->fd, buf1, sizeof(buf1), 0); |
| 3435 | if (size < 0) { |
| 3436 | err = socket_error(); |
| 3437 | if (err != EWOULDBLOCK) |
| 3438 | goto eoc; |
| 3439 | } else if (size == 0) { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3440 | /* end of connection */ |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3441 | eoc: |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3442 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3443 | closesocket(s->fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3444 | return; |
| 3445 | } |
| 3446 | buf = buf1; |
| 3447 | while (size > 0) { |
| 3448 | /* reassemble a packet from the network */ |
| 3449 | switch(s->state) { |
| 3450 | case 0: |
| 3451 | l = 4 - s->index; |
| 3452 | if (l > size) |
| 3453 | l = size; |
| 3454 | memcpy(s->buf + s->index, buf, l); |
| 3455 | buf += l; |
| 3456 | size -= l; |
| 3457 | s->index += l; |
| 3458 | if (s->index == 4) { |
| 3459 | /* got length */ |
| 3460 | s->packet_len = ntohl(*(uint32_t *)s->buf); |
| 3461 | s->index = 0; |
| 3462 | s->state = 1; |
| 3463 | } |
| 3464 | break; |
| 3465 | case 1: |
| 3466 | l = s->packet_len - s->index; |
| 3467 | if (l > size) |
| 3468 | l = size; |
| 3469 | memcpy(s->buf + s->index, buf, l); |
| 3470 | s->index += l; |
| 3471 | buf += l; |
| 3472 | size -= l; |
| 3473 | if (s->index >= s->packet_len) { |
| 3474 | qemu_send_packet(s->vc, s->buf, s->packet_len); |
| 3475 | s->index = 0; |
| 3476 | s->state = 0; |
| 3477 | } |
| 3478 | break; |
| 3479 | } |
| 3480 | } |
| 3481 | } |
| 3482 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3483 | static void net_socket_send_dgram(void *opaque) |
| 3484 | { |
| 3485 | NetSocketState *s = opaque; |
| 3486 | int size; |
| 3487 | |
| 3488 | size = recv(s->fd, s->buf, sizeof(s->buf), 0); |
| 3489 | if (size < 0) |
| 3490 | return; |
| 3491 | if (size == 0) { |
| 3492 | /* end of connection */ |
| 3493 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 3494 | return; |
| 3495 | } |
| 3496 | qemu_send_packet(s->vc, s->buf, size); |
| 3497 | } |
| 3498 | |
| 3499 | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
| 3500 | { |
| 3501 | struct ip_mreq imr; |
| 3502 | int fd; |
| 3503 | int val, ret; |
| 3504 | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { |
| 3505 | 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] | 3506 | inet_ntoa(mcastaddr->sin_addr), |
| 3507 | (int)ntohl(mcastaddr->sin_addr.s_addr)); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3508 | return -1; |
| 3509 | |
| 3510 | } |
| 3511 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 3512 | if (fd < 0) { |
| 3513 | perror("socket(PF_INET, SOCK_DGRAM)"); |
| 3514 | return -1; |
| 3515 | } |
| 3516 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3517 | val = 1; |
| 3518 | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
| 3519 | (const char *)&val, sizeof(val)); |
| 3520 | if (ret < 0) { |
| 3521 | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); |
| 3522 | goto fail; |
| 3523 | } |
| 3524 | |
| 3525 | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
| 3526 | if (ret < 0) { |
| 3527 | perror("bind"); |
| 3528 | goto fail; |
| 3529 | } |
| 3530 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3531 | /* Add host to multicast group */ |
| 3532 | imr.imr_multiaddr = mcastaddr->sin_addr; |
| 3533 | imr.imr_interface.s_addr = htonl(INADDR_ANY); |
| 3534 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3535 | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 3536 | (const char *)&imr, sizeof(struct ip_mreq)); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3537 | if (ret < 0) { |
| 3538 | perror("setsockopt(IP_ADD_MEMBERSHIP)"); |
| 3539 | goto fail; |
| 3540 | } |
| 3541 | |
| 3542 | /* Force mcast msgs to loopback (eg. several QEMUs in same host */ |
| 3543 | val = 1; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3544 | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
| 3545 | (const char *)&val, sizeof(val)); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3546 | if (ret < 0) { |
| 3547 | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); |
| 3548 | goto fail; |
| 3549 | } |
| 3550 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3551 | socket_set_nonblock(fd); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3552 | return fd; |
| 3553 | fail: |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3554 | if (fd >= 0) |
| 3555 | closesocket(fd); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3556 | return -1; |
| 3557 | } |
| 3558 | |
| 3559 | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, |
| 3560 | int is_connected) |
| 3561 | { |
| 3562 | struct sockaddr_in saddr; |
| 3563 | int newfd; |
| 3564 | socklen_t saddr_len; |
| 3565 | NetSocketState *s; |
| 3566 | |
| 3567 | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it |
| 3568 | * Because this may be "shared" socket from a "master" process, datagrams would be recv() |
| 3569 | * by ONLY ONE process: we must "clone" this dgram socket --jjo |
| 3570 | */ |
| 3571 | |
| 3572 | if (is_connected) { |
| 3573 | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
| 3574 | /* must be bound */ |
| 3575 | if (saddr.sin_addr.s_addr==0) { |
| 3576 | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n", |
| 3577 | fd); |
| 3578 | return NULL; |
| 3579 | } |
| 3580 | /* clone dgram socket */ |
| 3581 | newfd = net_socket_mcast_create(&saddr); |
| 3582 | if (newfd < 0) { |
| 3583 | /* error already reported by net_socket_mcast_create() */ |
| 3584 | close(fd); |
| 3585 | return NULL; |
| 3586 | } |
| 3587 | /* clone newfd to fd, close newfd */ |
| 3588 | dup2(newfd, fd); |
| 3589 | close(newfd); |
| 3590 | |
| 3591 | } else { |
| 3592 | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", |
| 3593 | fd, strerror(errno)); |
| 3594 | return NULL; |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | s = qemu_mallocz(sizeof(NetSocketState)); |
| 3599 | if (!s) |
| 3600 | return NULL; |
| 3601 | s->fd = fd; |
| 3602 | |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3603 | s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3604 | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); |
| 3605 | |
| 3606 | /* mcast: save bound address as dst */ |
| 3607 | if (is_connected) s->dgram_dst=saddr; |
| 3608 | |
| 3609 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 3610 | "socket: fd=%d (%s mcast=%s:%d)", |
| 3611 | fd, is_connected? "cloned" : "", |
| 3612 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 3613 | return s; |
| 3614 | } |
| 3615 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3616 | static void net_socket_connect(void *opaque) |
| 3617 | { |
| 3618 | NetSocketState *s = opaque; |
| 3619 | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s); |
| 3620 | } |
| 3621 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3622 | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3623 | int is_connected) |
| 3624 | { |
| 3625 | NetSocketState *s; |
| 3626 | s = qemu_mallocz(sizeof(NetSocketState)); |
| 3627 | if (!s) |
| 3628 | return NULL; |
| 3629 | s->fd = fd; |
| 3630 | s->vc = qemu_new_vlan_client(vlan, |
pbrook | d861b05 | 2006-02-04 22:15:28 +0000 | [diff] [blame] | 3631 | net_socket_receive, NULL, s); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3632 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 3633 | "socket: fd=%d", fd); |
| 3634 | if (is_connected) { |
| 3635 | net_socket_connect(s); |
| 3636 | } else { |
| 3637 | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); |
| 3638 | } |
| 3639 | return s; |
| 3640 | } |
| 3641 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3642 | static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, |
| 3643 | int is_connected) |
| 3644 | { |
| 3645 | int so_type=-1, optlen=sizeof(so_type); |
| 3646 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3647 | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, &optlen)< 0) { |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3648 | fprintf(stderr, "qemu: error: setsockopt(SO_TYPE) for fd=%d failed\n", fd); |
| 3649 | return NULL; |
| 3650 | } |
| 3651 | switch(so_type) { |
| 3652 | case SOCK_DGRAM: |
| 3653 | return net_socket_fd_init_dgram(vlan, fd, is_connected); |
| 3654 | case SOCK_STREAM: |
| 3655 | return net_socket_fd_init_stream(vlan, fd, is_connected); |
| 3656 | default: |
| 3657 | /* who knows ... this could be a eg. a pty, do warn and continue as stream */ |
| 3658 | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); |
| 3659 | return net_socket_fd_init_stream(vlan, fd, is_connected); |
| 3660 | } |
| 3661 | return NULL; |
| 3662 | } |
| 3663 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3664 | static void net_socket_accept(void *opaque) |
| 3665 | { |
| 3666 | NetSocketListenState *s = opaque; |
| 3667 | NetSocketState *s1; |
| 3668 | struct sockaddr_in saddr; |
| 3669 | socklen_t len; |
| 3670 | int fd; |
| 3671 | |
| 3672 | for(;;) { |
| 3673 | len = sizeof(saddr); |
| 3674 | fd = accept(s->fd, (struct sockaddr *)&saddr, &len); |
| 3675 | if (fd < 0 && errno != EINTR) { |
| 3676 | return; |
| 3677 | } else if (fd >= 0) { |
| 3678 | break; |
| 3679 | } |
| 3680 | } |
| 3681 | s1 = net_socket_fd_init(s->vlan, fd, 1); |
| 3682 | if (!s1) { |
bellard | 0bab00f | 2006-06-25 14:49:44 +0000 | [diff] [blame] | 3683 | closesocket(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3684 | } else { |
| 3685 | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str), |
| 3686 | "socket: connection from %s:%d", |
| 3687 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 3688 | } |
| 3689 | } |
| 3690 | |
| 3691 | static int net_socket_listen_init(VLANState *vlan, const char *host_str) |
| 3692 | { |
| 3693 | NetSocketListenState *s; |
| 3694 | int fd, val, ret; |
| 3695 | struct sockaddr_in saddr; |
| 3696 | |
| 3697 | if (parse_host_port(&saddr, host_str) < 0) |
| 3698 | return -1; |
| 3699 | |
| 3700 | s = qemu_mallocz(sizeof(NetSocketListenState)); |
| 3701 | if (!s) |
| 3702 | return -1; |
| 3703 | |
| 3704 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 3705 | if (fd < 0) { |
| 3706 | perror("socket"); |
| 3707 | return -1; |
| 3708 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3709 | socket_set_nonblock(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3710 | |
| 3711 | /* allow fast reuse */ |
| 3712 | val = 1; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3713 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3714 | |
| 3715 | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 3716 | if (ret < 0) { |
| 3717 | perror("bind"); |
| 3718 | return -1; |
| 3719 | } |
| 3720 | ret = listen(fd, 0); |
| 3721 | if (ret < 0) { |
| 3722 | perror("listen"); |
| 3723 | return -1; |
| 3724 | } |
| 3725 | s->vlan = vlan; |
| 3726 | s->fd = fd; |
| 3727 | qemu_set_fd_handler(fd, net_socket_accept, NULL, s); |
| 3728 | return 0; |
| 3729 | } |
| 3730 | |
| 3731 | static int net_socket_connect_init(VLANState *vlan, const char *host_str) |
| 3732 | { |
| 3733 | NetSocketState *s; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3734 | int fd, connected, ret, err; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3735 | struct sockaddr_in saddr; |
| 3736 | |
| 3737 | if (parse_host_port(&saddr, host_str) < 0) |
| 3738 | return -1; |
| 3739 | |
| 3740 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 3741 | if (fd < 0) { |
| 3742 | perror("socket"); |
| 3743 | return -1; |
| 3744 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3745 | socket_set_nonblock(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3746 | |
| 3747 | connected = 0; |
| 3748 | for(;;) { |
| 3749 | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 3750 | if (ret < 0) { |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3751 | err = socket_error(); |
| 3752 | if (err == EINTR || err == EWOULDBLOCK) { |
| 3753 | } else if (err == EINPROGRESS) { |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3754 | break; |
| 3755 | } else { |
| 3756 | perror("connect"); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3757 | closesocket(fd); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3758 | return -1; |
| 3759 | } |
| 3760 | } else { |
| 3761 | connected = 1; |
| 3762 | break; |
| 3763 | } |
| 3764 | } |
| 3765 | s = net_socket_fd_init(vlan, fd, connected); |
| 3766 | if (!s) |
| 3767 | return -1; |
| 3768 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 3769 | "socket: connect to %s:%d", |
| 3770 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 3771 | return 0; |
| 3772 | } |
| 3773 | |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3774 | static int net_socket_mcast_init(VLANState *vlan, const char *host_str) |
| 3775 | { |
| 3776 | NetSocketState *s; |
| 3777 | int fd; |
| 3778 | struct sockaddr_in saddr; |
| 3779 | |
| 3780 | if (parse_host_port(&saddr, host_str) < 0) |
| 3781 | return -1; |
| 3782 | |
| 3783 | |
| 3784 | fd = net_socket_mcast_create(&saddr); |
| 3785 | if (fd < 0) |
| 3786 | return -1; |
| 3787 | |
| 3788 | s = net_socket_fd_init(vlan, fd, 0); |
| 3789 | if (!s) |
| 3790 | return -1; |
| 3791 | |
| 3792 | s->dgram_dst = saddr; |
| 3793 | |
| 3794 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
| 3795 | "socket: mcast=%s:%d", |
| 3796 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 3797 | return 0; |
| 3798 | |
| 3799 | } |
| 3800 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3801 | static int get_param_value(char *buf, int buf_size, |
| 3802 | const char *tag, const char *str) |
| 3803 | { |
| 3804 | const char *p; |
| 3805 | char *q; |
| 3806 | char option[128]; |
| 3807 | |
| 3808 | p = str; |
| 3809 | for(;;) { |
| 3810 | q = option; |
| 3811 | while (*p != '\0' && *p != '=') { |
| 3812 | if ((q - option) < sizeof(option) - 1) |
| 3813 | *q++ = *p; |
| 3814 | p++; |
| 3815 | } |
| 3816 | *q = '\0'; |
| 3817 | if (*p != '=') |
| 3818 | break; |
| 3819 | p++; |
| 3820 | if (!strcmp(tag, option)) { |
| 3821 | q = buf; |
| 3822 | while (*p != '\0' && *p != ',') { |
| 3823 | if ((q - buf) < buf_size - 1) |
| 3824 | *q++ = *p; |
| 3825 | p++; |
| 3826 | } |
| 3827 | *q = '\0'; |
| 3828 | return q - buf; |
| 3829 | } else { |
| 3830 | while (*p != '\0' && *p != ',') { |
| 3831 | p++; |
| 3832 | } |
| 3833 | } |
| 3834 | if (*p != ',') |
| 3835 | break; |
| 3836 | p++; |
| 3837 | } |
| 3838 | return 0; |
| 3839 | } |
| 3840 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 3841 | static int net_client_init(const char *str) |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3842 | { |
| 3843 | const char *p; |
| 3844 | char *q; |
| 3845 | char device[64]; |
| 3846 | char buf[1024]; |
| 3847 | int vlan_id, ret; |
| 3848 | VLANState *vlan; |
| 3849 | |
| 3850 | p = str; |
| 3851 | q = device; |
| 3852 | while (*p != '\0' && *p != ',') { |
| 3853 | if ((q - device) < sizeof(device) - 1) |
| 3854 | *q++ = *p; |
| 3855 | p++; |
| 3856 | } |
| 3857 | *q = '\0'; |
| 3858 | if (*p == ',') |
| 3859 | p++; |
| 3860 | vlan_id = 0; |
| 3861 | if (get_param_value(buf, sizeof(buf), "vlan", p)) { |
| 3862 | vlan_id = strtol(buf, NULL, 0); |
| 3863 | } |
| 3864 | vlan = qemu_find_vlan(vlan_id); |
| 3865 | if (!vlan) { |
| 3866 | fprintf(stderr, "Could not create vlan %d\n", vlan_id); |
| 3867 | return -1; |
| 3868 | } |
| 3869 | if (!strcmp(device, "nic")) { |
| 3870 | NICInfo *nd; |
| 3871 | uint8_t *macaddr; |
| 3872 | |
| 3873 | if (nb_nics >= MAX_NICS) { |
| 3874 | fprintf(stderr, "Too Many NICs\n"); |
| 3875 | return -1; |
| 3876 | } |
| 3877 | nd = &nd_table[nb_nics]; |
| 3878 | macaddr = nd->macaddr; |
| 3879 | macaddr[0] = 0x52; |
| 3880 | macaddr[1] = 0x54; |
| 3881 | macaddr[2] = 0x00; |
| 3882 | macaddr[3] = 0x12; |
| 3883 | macaddr[4] = 0x34; |
| 3884 | macaddr[5] = 0x56 + nb_nics; |
| 3885 | |
| 3886 | if (get_param_value(buf, sizeof(buf), "macaddr", p)) { |
| 3887 | if (parse_macaddr(macaddr, buf) < 0) { |
| 3888 | fprintf(stderr, "invalid syntax for ethernet address\n"); |
| 3889 | return -1; |
| 3890 | } |
| 3891 | } |
pbrook | a41b2ff | 2006-02-05 04:14:41 +0000 | [diff] [blame] | 3892 | if (get_param_value(buf, sizeof(buf), "model", p)) { |
| 3893 | nd->model = strdup(buf); |
| 3894 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3895 | nd->vlan = vlan; |
| 3896 | nb_nics++; |
| 3897 | ret = 0; |
| 3898 | } else |
| 3899 | if (!strcmp(device, "none")) { |
| 3900 | /* does nothing. It is needed to signal that no network cards |
| 3901 | are wanted */ |
| 3902 | ret = 0; |
| 3903 | } else |
| 3904 | #ifdef CONFIG_SLIRP |
| 3905 | if (!strcmp(device, "user")) { |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 3906 | if (get_param_value(buf, sizeof(buf), "hostname", p)) { |
bellard | 3f423c9 | 2006-04-30 21:34:15 +0000 | [diff] [blame] | 3907 | pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf); |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 3908 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3909 | ret = net_slirp_init(vlan); |
| 3910 | } else |
| 3911 | #endif |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 3912 | #ifdef _WIN32 |
| 3913 | if (!strcmp(device, "tap")) { |
| 3914 | char ifname[64]; |
| 3915 | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
| 3916 | fprintf(stderr, "tap: no interface name\n"); |
| 3917 | return -1; |
| 3918 | } |
| 3919 | ret = tap_win32_init(vlan, ifname); |
| 3920 | } else |
| 3921 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3922 | if (!strcmp(device, "tap")) { |
| 3923 | char ifname[64]; |
| 3924 | char setup_script[1024]; |
| 3925 | int fd; |
| 3926 | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
| 3927 | fd = strtol(buf, NULL, 0); |
| 3928 | ret = -1; |
| 3929 | if (net_tap_fd_init(vlan, fd)) |
| 3930 | ret = 0; |
| 3931 | } else { |
| 3932 | get_param_value(ifname, sizeof(ifname), "ifname", p); |
| 3933 | if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) { |
| 3934 | pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT); |
| 3935 | } |
| 3936 | ret = net_tap_init(vlan, ifname, setup_script); |
| 3937 | } |
| 3938 | } else |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 3939 | #endif |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3940 | if (!strcmp(device, "socket")) { |
| 3941 | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
| 3942 | int fd; |
| 3943 | fd = strtol(buf, NULL, 0); |
| 3944 | ret = -1; |
| 3945 | if (net_socket_fd_init(vlan, fd, 1)) |
| 3946 | ret = 0; |
| 3947 | } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) { |
| 3948 | ret = net_socket_listen_init(vlan, buf); |
| 3949 | } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { |
| 3950 | ret = net_socket_connect_init(vlan, buf); |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 3951 | } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { |
| 3952 | ret = net_socket_mcast_init(vlan, buf); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3953 | } else { |
| 3954 | fprintf(stderr, "Unknown socket options: %s\n", p); |
| 3955 | return -1; |
| 3956 | } |
| 3957 | } else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 3958 | { |
| 3959 | fprintf(stderr, "Unknown network device: %s\n", device); |
| 3960 | return -1; |
| 3961 | } |
| 3962 | if (ret < 0) { |
| 3963 | fprintf(stderr, "Could not initialize device '%s'\n", device); |
| 3964 | } |
| 3965 | |
| 3966 | return ret; |
| 3967 | } |
| 3968 | |
| 3969 | void do_info_network(void) |
| 3970 | { |
| 3971 | VLANState *vlan; |
| 3972 | VLANClientState *vc; |
| 3973 | |
| 3974 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
| 3975 | term_printf("VLAN %d devices:\n", vlan->id); |
| 3976 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) |
| 3977 | term_printf(" %s\n", vc->info_str); |
| 3978 | } |
| 3979 | } |
ths | 42550fd | 2006-12-22 16:34:12 +0000 | [diff] [blame] | 3980 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 3981 | /***********************************************************/ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 3982 | /* USB devices */ |
| 3983 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 3984 | static USBPort *used_usb_ports; |
| 3985 | static USBPort *free_usb_ports; |
| 3986 | |
| 3987 | /* ??? Maybe change this to register a hub to keep track of the topology. */ |
| 3988 | void qemu_register_usb_port(USBPort *port, void *opaque, int index, |
| 3989 | usb_attachfn attach) |
| 3990 | { |
| 3991 | port->opaque = opaque; |
| 3992 | port->index = index; |
| 3993 | port->attach = attach; |
| 3994 | port->next = free_usb_ports; |
| 3995 | free_usb_ports = port; |
| 3996 | } |
| 3997 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 3998 | static int usb_device_add(const char *devname) |
| 3999 | { |
| 4000 | const char *p; |
| 4001 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4002 | USBPort *port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4003 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4004 | if (!free_usb_ports) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4005 | return -1; |
| 4006 | |
| 4007 | if (strstart(devname, "host:", &p)) { |
| 4008 | dev = usb_host_device_open(p); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4009 | } else if (!strcmp(devname, "mouse")) { |
| 4010 | dev = usb_mouse_init(); |
bellard | 09b26c5 | 2006-04-12 21:09:08 +0000 | [diff] [blame] | 4011 | } else if (!strcmp(devname, "tablet")) { |
| 4012 | dev = usb_tablet_init(); |
pbrook | 2e5d83b | 2006-05-25 23:58:51 +0000 | [diff] [blame] | 4013 | } else if (strstart(devname, "disk:", &p)) { |
| 4014 | dev = usb_msd_init(p); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4015 | } else { |
| 4016 | return -1; |
| 4017 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4018 | if (!dev) |
| 4019 | return -1; |
| 4020 | |
| 4021 | /* Find a USB port to add the device to. */ |
| 4022 | port = free_usb_ports; |
| 4023 | if (!port->next) { |
| 4024 | USBDevice *hub; |
| 4025 | |
| 4026 | /* Create a new hub and chain it on. */ |
| 4027 | free_usb_ports = NULL; |
| 4028 | port->next = used_usb_ports; |
| 4029 | used_usb_ports = port; |
| 4030 | |
| 4031 | hub = usb_hub_init(VM_USB_HUB_SIZE); |
| 4032 | usb_attach(port, hub); |
| 4033 | port = free_usb_ports; |
| 4034 | } |
| 4035 | |
| 4036 | free_usb_ports = port->next; |
| 4037 | port->next = used_usb_ports; |
| 4038 | used_usb_ports = port; |
| 4039 | usb_attach(port, dev); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4040 | return 0; |
| 4041 | } |
| 4042 | |
| 4043 | static int usb_device_del(const char *devname) |
| 4044 | { |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4045 | USBPort *port; |
| 4046 | USBPort **lastp; |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 4047 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4048 | int bus_num, addr; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4049 | const char *p; |
| 4050 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4051 | if (!used_usb_ports) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4052 | return -1; |
| 4053 | |
| 4054 | p = strchr(devname, '.'); |
| 4055 | if (!p) |
| 4056 | return -1; |
| 4057 | bus_num = strtoul(devname, NULL, 0); |
| 4058 | addr = strtoul(p + 1, NULL, 0); |
| 4059 | if (bus_num != 0) |
| 4060 | return -1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4061 | |
| 4062 | lastp = &used_usb_ports; |
| 4063 | port = used_usb_ports; |
| 4064 | while (port && port->dev->addr != addr) { |
| 4065 | lastp = &port->next; |
| 4066 | port = port->next; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4067 | } |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4068 | |
| 4069 | if (!port) |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4070 | return -1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4071 | |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 4072 | dev = port->dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4073 | *lastp = port->next; |
| 4074 | usb_attach(port, NULL); |
bellard | 059809e | 2006-07-19 18:06:15 +0000 | [diff] [blame] | 4075 | dev->handle_destroy(dev); |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4076 | port->next = free_usb_ports; |
| 4077 | free_usb_ports = port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4078 | return 0; |
| 4079 | } |
| 4080 | |
| 4081 | void do_usb_add(const char *devname) |
| 4082 | { |
| 4083 | int ret; |
| 4084 | ret = usb_device_add(devname); |
| 4085 | if (ret < 0) |
| 4086 | term_printf("Could not add USB device '%s'\n", devname); |
| 4087 | } |
| 4088 | |
| 4089 | void do_usb_del(const char *devname) |
| 4090 | { |
| 4091 | int ret; |
| 4092 | ret = usb_device_del(devname); |
| 4093 | if (ret < 0) |
| 4094 | term_printf("Could not remove USB device '%s'\n", devname); |
| 4095 | } |
| 4096 | |
| 4097 | void usb_info(void) |
| 4098 | { |
| 4099 | USBDevice *dev; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4100 | USBPort *port; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4101 | const char *speed_str; |
| 4102 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4103 | if (!usb_enabled) { |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4104 | term_printf("USB support not enabled\n"); |
| 4105 | return; |
| 4106 | } |
| 4107 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 4108 | for (port = used_usb_ports; port; port = port->next) { |
| 4109 | dev = port->dev; |
| 4110 | if (!dev) |
| 4111 | continue; |
| 4112 | switch(dev->speed) { |
| 4113 | case USB_SPEED_LOW: |
| 4114 | speed_str = "1.5"; |
| 4115 | break; |
| 4116 | case USB_SPEED_FULL: |
| 4117 | speed_str = "12"; |
| 4118 | break; |
| 4119 | case USB_SPEED_HIGH: |
| 4120 | speed_str = "480"; |
| 4121 | break; |
| 4122 | default: |
| 4123 | speed_str = "?"; |
| 4124 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4125 | } |
bellard | 1f6e24e | 2006-06-26 21:00:51 +0000 | [diff] [blame] | 4126 | term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n", |
| 4127 | 0, dev->addr, speed_str, dev->devname); |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 4128 | } |
| 4129 | } |
| 4130 | |
| 4131 | /***********************************************************/ |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 4132 | /* pid file */ |
| 4133 | |
| 4134 | static char *pid_filename; |
| 4135 | |
| 4136 | /* Remove PID file. Called on normal exit */ |
| 4137 | |
| 4138 | static void remove_pidfile(void) |
| 4139 | { |
| 4140 | unlink (pid_filename); |
| 4141 | } |
| 4142 | |
| 4143 | static void create_pidfile(const char *filename) |
| 4144 | { |
| 4145 | struct stat pidstat; |
| 4146 | FILE *f; |
| 4147 | |
| 4148 | /* Try to write our PID to the named file */ |
| 4149 | if (stat(filename, &pidstat) < 0) { |
| 4150 | if (errno == ENOENT) { |
| 4151 | if ((f = fopen (filename, "w")) == NULL) { |
| 4152 | perror("Opening pidfile"); |
| 4153 | exit(1); |
| 4154 | } |
| 4155 | fprintf(f, "%d\n", getpid()); |
| 4156 | fclose(f); |
| 4157 | pid_filename = qemu_strdup(filename); |
| 4158 | if (!pid_filename) { |
| 4159 | fprintf(stderr, "Could not save PID filename"); |
| 4160 | exit(1); |
| 4161 | } |
| 4162 | atexit(remove_pidfile); |
| 4163 | } |
| 4164 | } else { |
| 4165 | fprintf(stderr, "%s already exists. Remove it and try again.\n", |
| 4166 | filename); |
| 4167 | exit(1); |
| 4168 | } |
| 4169 | } |
| 4170 | |
| 4171 | /***********************************************************/ |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4172 | /* dumb display */ |
| 4173 | |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4174 | static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
| 4175 | { |
| 4176 | } |
| 4177 | |
| 4178 | static void dumb_resize(DisplayState *ds, int w, int h) |
| 4179 | { |
| 4180 | } |
| 4181 | |
| 4182 | static void dumb_refresh(DisplayState *ds) |
| 4183 | { |
pbrook | 9521989 | 2006-04-09 01:06:34 +0000 | [diff] [blame] | 4184 | vga_hw_update(); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | void dumb_display_init(DisplayState *ds) |
| 4188 | { |
| 4189 | ds->data = NULL; |
| 4190 | ds->linesize = 0; |
| 4191 | ds->depth = 0; |
| 4192 | ds->dpy_update = dumb_update; |
| 4193 | ds->dpy_resize = dumb_resize; |
| 4194 | ds->dpy_refresh = dumb_refresh; |
| 4195 | } |
| 4196 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4197 | /***********************************************************/ |
| 4198 | /* I/O handling */ |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 4199 | |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4200 | #define MAX_IO_HANDLERS 64 |
| 4201 | |
| 4202 | typedef struct IOHandlerRecord { |
| 4203 | int fd; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4204 | IOCanRWHandler *fd_read_poll; |
| 4205 | IOHandler *fd_read; |
| 4206 | IOHandler *fd_write; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4207 | void *opaque; |
| 4208 | /* temporary data */ |
| 4209 | struct pollfd *ufd; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4210 | struct IOHandlerRecord *next; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4211 | } IOHandlerRecord; |
| 4212 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4213 | static IOHandlerRecord *first_io_handler; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 4214 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4215 | /* XXX: fd_read_poll should be suppressed, but an API change is |
| 4216 | necessary in the character devices to suppress fd_can_read(). */ |
| 4217 | int qemu_set_fd_handler2(int fd, |
| 4218 | IOCanRWHandler *fd_read_poll, |
| 4219 | IOHandler *fd_read, |
| 4220 | IOHandler *fd_write, |
| 4221 | void *opaque) |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4222 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4223 | IOHandlerRecord **pioh, *ioh; |
| 4224 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4225 | if (!fd_read && !fd_write) { |
| 4226 | pioh = &first_io_handler; |
| 4227 | for(;;) { |
| 4228 | ioh = *pioh; |
| 4229 | if (ioh == NULL) |
| 4230 | break; |
| 4231 | if (ioh->fd == fd) { |
| 4232 | *pioh = ioh->next; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 4233 | qemu_free(ioh); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4234 | break; |
| 4235 | } |
| 4236 | pioh = &ioh->next; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4237 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4238 | } else { |
| 4239 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 4240 | if (ioh->fd == fd) |
| 4241 | goto found; |
| 4242 | } |
| 4243 | ioh = qemu_mallocz(sizeof(IOHandlerRecord)); |
| 4244 | if (!ioh) |
| 4245 | return -1; |
| 4246 | ioh->next = first_io_handler; |
| 4247 | first_io_handler = ioh; |
| 4248 | found: |
| 4249 | ioh->fd = fd; |
| 4250 | ioh->fd_read_poll = fd_read_poll; |
| 4251 | ioh->fd_read = fd_read; |
| 4252 | ioh->fd_write = fd_write; |
| 4253 | ioh->opaque = opaque; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4254 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 4255 | return 0; |
| 4256 | } |
| 4257 | |
| 4258 | int qemu_set_fd_handler(int fd, |
| 4259 | IOHandler *fd_read, |
| 4260 | IOHandler *fd_write, |
| 4261 | void *opaque) |
| 4262 | { |
| 4263 | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4266 | /***********************************************************/ |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 4267 | /* Polling handling */ |
| 4268 | |
| 4269 | typedef struct PollingEntry { |
| 4270 | PollingFunc *func; |
| 4271 | void *opaque; |
| 4272 | struct PollingEntry *next; |
| 4273 | } PollingEntry; |
| 4274 | |
| 4275 | static PollingEntry *first_polling_entry; |
| 4276 | |
| 4277 | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
| 4278 | { |
| 4279 | PollingEntry **ppe, *pe; |
| 4280 | pe = qemu_mallocz(sizeof(PollingEntry)); |
| 4281 | if (!pe) |
| 4282 | return -1; |
| 4283 | pe->func = func; |
| 4284 | pe->opaque = opaque; |
| 4285 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
| 4286 | *ppe = pe; |
| 4287 | return 0; |
| 4288 | } |
| 4289 | |
| 4290 | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
| 4291 | { |
| 4292 | PollingEntry **ppe, *pe; |
| 4293 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
| 4294 | pe = *ppe; |
| 4295 | if (pe->func == func && pe->opaque == opaque) { |
| 4296 | *ppe = pe->next; |
| 4297 | qemu_free(pe); |
| 4298 | break; |
| 4299 | } |
| 4300 | } |
| 4301 | } |
| 4302 | |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 4303 | #ifdef _WIN32 |
| 4304 | /***********************************************************/ |
| 4305 | /* Wait objects support */ |
| 4306 | typedef struct WaitObjects { |
| 4307 | int num; |
| 4308 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; |
| 4309 | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1]; |
| 4310 | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
| 4311 | } WaitObjects; |
| 4312 | |
| 4313 | static WaitObjects wait_objects = {0}; |
| 4314 | |
| 4315 | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 4316 | { |
| 4317 | WaitObjects *w = &wait_objects; |
| 4318 | |
| 4319 | if (w->num >= MAXIMUM_WAIT_OBJECTS) |
| 4320 | return -1; |
| 4321 | w->events[w->num] = handle; |
| 4322 | w->func[w->num] = func; |
| 4323 | w->opaque[w->num] = opaque; |
| 4324 | w->num++; |
| 4325 | return 0; |
| 4326 | } |
| 4327 | |
| 4328 | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
| 4329 | { |
| 4330 | int i, found; |
| 4331 | WaitObjects *w = &wait_objects; |
| 4332 | |
| 4333 | found = 0; |
| 4334 | for (i = 0; i < w->num; i++) { |
| 4335 | if (w->events[i] == handle) |
| 4336 | found = 1; |
| 4337 | if (found) { |
| 4338 | w->events[i] = w->events[i + 1]; |
| 4339 | w->func[i] = w->func[i + 1]; |
| 4340 | w->opaque[i] = w->opaque[i + 1]; |
| 4341 | } |
| 4342 | } |
| 4343 | if (found) |
| 4344 | w->num--; |
| 4345 | } |
| 4346 | #endif |
| 4347 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 4348 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4349 | /* savevm/loadvm support */ |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4350 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4351 | #define IO_BUF_SIZE 32768 |
| 4352 | |
| 4353 | struct QEMUFile { |
| 4354 | FILE *outfile; |
| 4355 | BlockDriverState *bs; |
| 4356 | int is_file; |
| 4357 | int is_writable; |
| 4358 | int64_t base_offset; |
| 4359 | int64_t buf_offset; /* start of buffer when writing, end of buffer |
| 4360 | when reading */ |
| 4361 | int buf_index; |
| 4362 | int buf_size; /* 0 when writing */ |
| 4363 | uint8_t buf[IO_BUF_SIZE]; |
| 4364 | }; |
| 4365 | |
| 4366 | QEMUFile *qemu_fopen(const char *filename, const char *mode) |
| 4367 | { |
| 4368 | QEMUFile *f; |
| 4369 | |
| 4370 | f = qemu_mallocz(sizeof(QEMUFile)); |
| 4371 | if (!f) |
| 4372 | return NULL; |
| 4373 | if (!strcmp(mode, "wb")) { |
| 4374 | f->is_writable = 1; |
| 4375 | } else if (!strcmp(mode, "rb")) { |
| 4376 | f->is_writable = 0; |
| 4377 | } else { |
| 4378 | goto fail; |
| 4379 | } |
| 4380 | f->outfile = fopen(filename, mode); |
| 4381 | if (!f->outfile) |
| 4382 | goto fail; |
| 4383 | f->is_file = 1; |
| 4384 | return f; |
| 4385 | fail: |
| 4386 | if (f->outfile) |
| 4387 | fclose(f->outfile); |
| 4388 | qemu_free(f); |
| 4389 | return NULL; |
| 4390 | } |
| 4391 | |
| 4392 | QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable) |
| 4393 | { |
| 4394 | QEMUFile *f; |
| 4395 | |
| 4396 | f = qemu_mallocz(sizeof(QEMUFile)); |
| 4397 | if (!f) |
| 4398 | return NULL; |
| 4399 | f->is_file = 0; |
| 4400 | f->bs = bs; |
| 4401 | f->is_writable = is_writable; |
| 4402 | f->base_offset = offset; |
| 4403 | return f; |
| 4404 | } |
| 4405 | |
| 4406 | void qemu_fflush(QEMUFile *f) |
| 4407 | { |
| 4408 | if (!f->is_writable) |
| 4409 | return; |
| 4410 | if (f->buf_index > 0) { |
| 4411 | if (f->is_file) { |
| 4412 | fseek(f->outfile, f->buf_offset, SEEK_SET); |
| 4413 | fwrite(f->buf, 1, f->buf_index, f->outfile); |
| 4414 | } else { |
| 4415 | bdrv_pwrite(f->bs, f->base_offset + f->buf_offset, |
| 4416 | f->buf, f->buf_index); |
| 4417 | } |
| 4418 | f->buf_offset += f->buf_index; |
| 4419 | f->buf_index = 0; |
| 4420 | } |
| 4421 | } |
| 4422 | |
| 4423 | static void qemu_fill_buffer(QEMUFile *f) |
| 4424 | { |
| 4425 | int len; |
| 4426 | |
| 4427 | if (f->is_writable) |
| 4428 | return; |
| 4429 | if (f->is_file) { |
| 4430 | fseek(f->outfile, f->buf_offset, SEEK_SET); |
| 4431 | len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile); |
| 4432 | if (len < 0) |
| 4433 | len = 0; |
| 4434 | } else { |
| 4435 | len = bdrv_pread(f->bs, f->base_offset + f->buf_offset, |
| 4436 | f->buf, IO_BUF_SIZE); |
| 4437 | if (len < 0) |
| 4438 | len = 0; |
| 4439 | } |
| 4440 | f->buf_index = 0; |
| 4441 | f->buf_size = len; |
| 4442 | f->buf_offset += len; |
| 4443 | } |
| 4444 | |
| 4445 | void qemu_fclose(QEMUFile *f) |
| 4446 | { |
| 4447 | if (f->is_writable) |
| 4448 | qemu_fflush(f); |
| 4449 | if (f->is_file) { |
| 4450 | fclose(f->outfile); |
| 4451 | } |
| 4452 | qemu_free(f); |
| 4453 | } |
| 4454 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4455 | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
| 4456 | { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4457 | int l; |
| 4458 | while (size > 0) { |
| 4459 | l = IO_BUF_SIZE - f->buf_index; |
| 4460 | if (l > size) |
| 4461 | l = size; |
| 4462 | memcpy(f->buf + f->buf_index, buf, l); |
| 4463 | f->buf_index += l; |
| 4464 | buf += l; |
| 4465 | size -= l; |
| 4466 | if (f->buf_index >= IO_BUF_SIZE) |
| 4467 | qemu_fflush(f); |
| 4468 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4469 | } |
| 4470 | |
| 4471 | void qemu_put_byte(QEMUFile *f, int v) |
| 4472 | { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4473 | f->buf[f->buf_index++] = v; |
| 4474 | if (f->buf_index >= IO_BUF_SIZE) |
| 4475 | qemu_fflush(f); |
| 4476 | } |
| 4477 | |
| 4478 | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1) |
| 4479 | { |
| 4480 | int size, l; |
| 4481 | |
| 4482 | size = size1; |
| 4483 | while (size > 0) { |
| 4484 | l = f->buf_size - f->buf_index; |
| 4485 | if (l == 0) { |
| 4486 | qemu_fill_buffer(f); |
| 4487 | l = f->buf_size - f->buf_index; |
| 4488 | if (l == 0) |
| 4489 | break; |
| 4490 | } |
| 4491 | if (l > size) |
| 4492 | l = size; |
| 4493 | memcpy(buf, f->buf + f->buf_index, l); |
| 4494 | f->buf_index += l; |
| 4495 | buf += l; |
| 4496 | size -= l; |
| 4497 | } |
| 4498 | return size1 - size; |
| 4499 | } |
| 4500 | |
| 4501 | int qemu_get_byte(QEMUFile *f) |
| 4502 | { |
| 4503 | if (f->buf_index >= f->buf_size) { |
| 4504 | qemu_fill_buffer(f); |
| 4505 | if (f->buf_index >= f->buf_size) |
| 4506 | return 0; |
| 4507 | } |
| 4508 | return f->buf[f->buf_index++]; |
| 4509 | } |
| 4510 | |
| 4511 | int64_t qemu_ftell(QEMUFile *f) |
| 4512 | { |
| 4513 | return f->buf_offset - f->buf_size + f->buf_index; |
| 4514 | } |
| 4515 | |
| 4516 | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence) |
| 4517 | { |
| 4518 | if (whence == SEEK_SET) { |
| 4519 | /* nothing to do */ |
| 4520 | } else if (whence == SEEK_CUR) { |
| 4521 | pos += qemu_ftell(f); |
| 4522 | } else { |
| 4523 | /* SEEK_END not supported */ |
| 4524 | return -1; |
| 4525 | } |
| 4526 | if (f->is_writable) { |
| 4527 | qemu_fflush(f); |
| 4528 | f->buf_offset = pos; |
| 4529 | } else { |
| 4530 | f->buf_offset = pos; |
| 4531 | f->buf_index = 0; |
| 4532 | f->buf_size = 0; |
| 4533 | } |
| 4534 | return pos; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
| 4537 | void qemu_put_be16(QEMUFile *f, unsigned int v) |
| 4538 | { |
| 4539 | qemu_put_byte(f, v >> 8); |
| 4540 | qemu_put_byte(f, v); |
| 4541 | } |
| 4542 | |
| 4543 | void qemu_put_be32(QEMUFile *f, unsigned int v) |
| 4544 | { |
| 4545 | qemu_put_byte(f, v >> 24); |
| 4546 | qemu_put_byte(f, v >> 16); |
| 4547 | qemu_put_byte(f, v >> 8); |
| 4548 | qemu_put_byte(f, v); |
| 4549 | } |
| 4550 | |
| 4551 | void qemu_put_be64(QEMUFile *f, uint64_t v) |
| 4552 | { |
| 4553 | qemu_put_be32(f, v >> 32); |
| 4554 | qemu_put_be32(f, v); |
| 4555 | } |
| 4556 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4557 | unsigned int qemu_get_be16(QEMUFile *f) |
| 4558 | { |
| 4559 | unsigned int v; |
| 4560 | v = qemu_get_byte(f) << 8; |
| 4561 | v |= qemu_get_byte(f); |
| 4562 | return v; |
| 4563 | } |
| 4564 | |
| 4565 | unsigned int qemu_get_be32(QEMUFile *f) |
| 4566 | { |
| 4567 | unsigned int v; |
| 4568 | v = qemu_get_byte(f) << 24; |
| 4569 | v |= qemu_get_byte(f) << 16; |
| 4570 | v |= qemu_get_byte(f) << 8; |
| 4571 | v |= qemu_get_byte(f); |
| 4572 | return v; |
| 4573 | } |
| 4574 | |
| 4575 | uint64_t qemu_get_be64(QEMUFile *f) |
| 4576 | { |
| 4577 | uint64_t v; |
| 4578 | v = (uint64_t)qemu_get_be32(f) << 32; |
| 4579 | v |= qemu_get_be32(f); |
| 4580 | return v; |
| 4581 | } |
| 4582 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4583 | typedef struct SaveStateEntry { |
| 4584 | char idstr[256]; |
| 4585 | int instance_id; |
| 4586 | int version_id; |
| 4587 | SaveStateHandler *save_state; |
| 4588 | LoadStateHandler *load_state; |
| 4589 | void *opaque; |
| 4590 | struct SaveStateEntry *next; |
| 4591 | } SaveStateEntry; |
| 4592 | |
| 4593 | static SaveStateEntry *first_se; |
| 4594 | |
| 4595 | int register_savevm(const char *idstr, |
| 4596 | int instance_id, |
| 4597 | int version_id, |
| 4598 | SaveStateHandler *save_state, |
| 4599 | LoadStateHandler *load_state, |
| 4600 | void *opaque) |
| 4601 | { |
| 4602 | SaveStateEntry *se, **pse; |
| 4603 | |
| 4604 | se = qemu_malloc(sizeof(SaveStateEntry)); |
| 4605 | if (!se) |
| 4606 | return -1; |
| 4607 | pstrcpy(se->idstr, sizeof(se->idstr), idstr); |
| 4608 | se->instance_id = instance_id; |
| 4609 | se->version_id = version_id; |
| 4610 | se->save_state = save_state; |
| 4611 | se->load_state = load_state; |
| 4612 | se->opaque = opaque; |
| 4613 | se->next = NULL; |
| 4614 | |
| 4615 | /* add at the end of list */ |
| 4616 | pse = &first_se; |
| 4617 | while (*pse != NULL) |
| 4618 | pse = &(*pse)->next; |
| 4619 | *pse = se; |
| 4620 | return 0; |
| 4621 | } |
| 4622 | |
| 4623 | #define QEMU_VM_FILE_MAGIC 0x5145564d |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4624 | #define QEMU_VM_FILE_VERSION 0x00000002 |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4625 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4626 | int qemu_savevm_state(QEMUFile *f) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4627 | { |
| 4628 | SaveStateEntry *se; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4629 | int len, ret; |
| 4630 | int64_t cur_pos, len_pos, total_len_pos; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 4631 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4632 | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
| 4633 | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4634 | total_len_pos = qemu_ftell(f); |
| 4635 | qemu_put_be64(f, 0); /* total size */ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4636 | |
| 4637 | for(se = first_se; se != NULL; se = se->next) { |
| 4638 | /* ID string */ |
| 4639 | len = strlen(se->idstr); |
| 4640 | qemu_put_byte(f, len); |
| 4641 | qemu_put_buffer(f, se->idstr, len); |
| 4642 | |
| 4643 | qemu_put_be32(f, se->instance_id); |
| 4644 | qemu_put_be32(f, se->version_id); |
| 4645 | |
| 4646 | /* record size: filled later */ |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4647 | len_pos = qemu_ftell(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4648 | qemu_put_be32(f, 0); |
| 4649 | |
| 4650 | se->save_state(f, se->opaque); |
| 4651 | |
| 4652 | /* fill record size */ |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4653 | cur_pos = qemu_ftell(f); |
| 4654 | len = cur_pos - len_pos - 4; |
| 4655 | qemu_fseek(f, len_pos, SEEK_SET); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4656 | qemu_put_be32(f, len); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4657 | qemu_fseek(f, cur_pos, SEEK_SET); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4658 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4659 | cur_pos = qemu_ftell(f); |
| 4660 | qemu_fseek(f, total_len_pos, SEEK_SET); |
| 4661 | qemu_put_be64(f, cur_pos - total_len_pos - 8); |
| 4662 | qemu_fseek(f, cur_pos, SEEK_SET); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4663 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4664 | ret = 0; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4665 | return ret; |
| 4666 | } |
| 4667 | |
| 4668 | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
| 4669 | { |
| 4670 | SaveStateEntry *se; |
| 4671 | |
| 4672 | for(se = first_se; se != NULL; se = se->next) { |
| 4673 | if (!strcmp(se->idstr, idstr) && |
| 4674 | instance_id == se->instance_id) |
| 4675 | return se; |
| 4676 | } |
| 4677 | return NULL; |
| 4678 | } |
| 4679 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4680 | int qemu_loadvm_state(QEMUFile *f) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4681 | { |
| 4682 | SaveStateEntry *se; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4683 | int len, ret, instance_id, record_len, version_id; |
| 4684 | int64_t total_len, end_pos, cur_pos; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4685 | unsigned int v; |
| 4686 | char idstr[256]; |
| 4687 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4688 | v = qemu_get_be32(f); |
| 4689 | if (v != QEMU_VM_FILE_MAGIC) |
| 4690 | goto fail; |
| 4691 | v = qemu_get_be32(f); |
| 4692 | if (v != QEMU_VM_FILE_VERSION) { |
| 4693 | fail: |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4694 | ret = -1; |
| 4695 | goto the_end; |
| 4696 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4697 | total_len = qemu_get_be64(f); |
| 4698 | end_pos = total_len + qemu_ftell(f); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 4699 | for(;;) { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4700 | if (qemu_ftell(f) >= end_pos) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4701 | break; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4702 | len = qemu_get_byte(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4703 | qemu_get_buffer(f, idstr, len); |
| 4704 | idstr[len] = '\0'; |
| 4705 | instance_id = qemu_get_be32(f); |
| 4706 | version_id = qemu_get_be32(f); |
| 4707 | record_len = qemu_get_be32(f); |
| 4708 | #if 0 |
| 4709 | printf("idstr=%s instance=0x%x version=%d len=%d\n", |
| 4710 | idstr, instance_id, version_id, record_len); |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 4711 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4712 | cur_pos = qemu_ftell(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4713 | se = find_se(idstr, instance_id); |
| 4714 | if (!se) { |
| 4715 | fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n", |
| 4716 | instance_id, idstr); |
| 4717 | } else { |
| 4718 | ret = se->load_state(f, se->opaque, version_id); |
| 4719 | if (ret < 0) { |
| 4720 | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n", |
| 4721 | instance_id, idstr); |
| 4722 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 4723 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4724 | /* always seek to exact end of record */ |
| 4725 | qemu_fseek(f, cur_pos + record_len, SEEK_SET); |
| 4726 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4727 | ret = 0; |
| 4728 | the_end: |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4729 | return ret; |
| 4730 | } |
| 4731 | |
| 4732 | /* device can contain snapshots */ |
| 4733 | static int bdrv_can_snapshot(BlockDriverState *bs) |
| 4734 | { |
| 4735 | return (bs && |
| 4736 | !bdrv_is_removable(bs) && |
| 4737 | !bdrv_is_read_only(bs)); |
| 4738 | } |
| 4739 | |
| 4740 | /* device must be snapshots in order to have a reliable snapshot */ |
| 4741 | static int bdrv_has_snapshot(BlockDriverState *bs) |
| 4742 | { |
| 4743 | return (bs && |
| 4744 | !bdrv_is_removable(bs) && |
| 4745 | !bdrv_is_read_only(bs)); |
| 4746 | } |
| 4747 | |
| 4748 | static BlockDriverState *get_bs_snapshots(void) |
| 4749 | { |
| 4750 | BlockDriverState *bs; |
| 4751 | int i; |
| 4752 | |
| 4753 | if (bs_snapshots) |
| 4754 | return bs_snapshots; |
| 4755 | for(i = 0; i <= MAX_DISKS; i++) { |
| 4756 | bs = bs_table[i]; |
| 4757 | if (bdrv_can_snapshot(bs)) |
| 4758 | goto ok; |
| 4759 | } |
| 4760 | return NULL; |
| 4761 | ok: |
| 4762 | bs_snapshots = bs; |
| 4763 | return bs; |
| 4764 | } |
| 4765 | |
| 4766 | static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
| 4767 | const char *name) |
| 4768 | { |
| 4769 | QEMUSnapshotInfo *sn_tab, *sn; |
| 4770 | int nb_sns, i, ret; |
| 4771 | |
| 4772 | ret = -ENOENT; |
| 4773 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 4774 | if (nb_sns < 0) |
| 4775 | return ret; |
| 4776 | for(i = 0; i < nb_sns; i++) { |
| 4777 | sn = &sn_tab[i]; |
| 4778 | if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) { |
| 4779 | *sn_info = *sn; |
| 4780 | ret = 0; |
| 4781 | break; |
| 4782 | } |
| 4783 | } |
| 4784 | qemu_free(sn_tab); |
| 4785 | return ret; |
| 4786 | } |
| 4787 | |
| 4788 | void do_savevm(const char *name) |
| 4789 | { |
| 4790 | BlockDriverState *bs, *bs1; |
| 4791 | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; |
| 4792 | int must_delete, ret, i; |
| 4793 | BlockDriverInfo bdi1, *bdi = &bdi1; |
| 4794 | QEMUFile *f; |
| 4795 | int saved_vm_running; |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 4796 | #ifdef _WIN32 |
| 4797 | struct _timeb tb; |
| 4798 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4799 | struct timeval tv; |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 4800 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4801 | |
| 4802 | bs = get_bs_snapshots(); |
| 4803 | if (!bs) { |
| 4804 | term_printf("No block device can accept snapshots\n"); |
| 4805 | return; |
| 4806 | } |
| 4807 | |
pbrook | 6192bc3 | 2006-09-03 12:08:37 +0000 | [diff] [blame] | 4808 | /* ??? Should this occur after vm_stop? */ |
| 4809 | qemu_aio_flush(); |
| 4810 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4811 | saved_vm_running = vm_running; |
| 4812 | vm_stop(0); |
| 4813 | |
| 4814 | must_delete = 0; |
| 4815 | if (name) { |
| 4816 | ret = bdrv_snapshot_find(bs, old_sn, name); |
| 4817 | if (ret >= 0) { |
| 4818 | must_delete = 1; |
| 4819 | } |
| 4820 | } |
| 4821 | memset(sn, 0, sizeof(*sn)); |
| 4822 | if (must_delete) { |
| 4823 | pstrcpy(sn->name, sizeof(sn->name), old_sn->name); |
| 4824 | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str); |
| 4825 | } else { |
| 4826 | if (name) |
| 4827 | pstrcpy(sn->name, sizeof(sn->name), name); |
| 4828 | } |
| 4829 | |
| 4830 | /* fill auxiliary fields */ |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 4831 | #ifdef _WIN32 |
| 4832 | _ftime(&tb); |
| 4833 | sn->date_sec = tb.time; |
| 4834 | sn->date_nsec = tb.millitm * 1000000; |
| 4835 | #else |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4836 | gettimeofday(&tv, NULL); |
| 4837 | sn->date_sec = tv.tv_sec; |
| 4838 | sn->date_nsec = tv.tv_usec * 1000; |
bellard | 4c279bd | 2006-08-17 09:43:50 +0000 | [diff] [blame] | 4839 | #endif |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4840 | sn->vm_clock_nsec = qemu_get_clock(vm_clock); |
| 4841 | |
| 4842 | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
| 4843 | term_printf("Device %s does not support VM state snapshots\n", |
| 4844 | bdrv_get_device_name(bs)); |
| 4845 | goto the_end; |
| 4846 | } |
| 4847 | |
| 4848 | /* save the VM state */ |
| 4849 | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1); |
| 4850 | if (!f) { |
| 4851 | term_printf("Could not open VM state file\n"); |
| 4852 | goto the_end; |
| 4853 | } |
| 4854 | ret = qemu_savevm_state(f); |
| 4855 | sn->vm_state_size = qemu_ftell(f); |
| 4856 | qemu_fclose(f); |
| 4857 | if (ret < 0) { |
| 4858 | term_printf("Error %d while writing VM\n", ret); |
| 4859 | goto the_end; |
| 4860 | } |
| 4861 | |
| 4862 | /* create the snapshots */ |
| 4863 | |
| 4864 | for(i = 0; i < MAX_DISKS; i++) { |
| 4865 | bs1 = bs_table[i]; |
| 4866 | if (bdrv_has_snapshot(bs1)) { |
| 4867 | if (must_delete) { |
| 4868 | ret = bdrv_snapshot_delete(bs1, old_sn->id_str); |
| 4869 | if (ret < 0) { |
| 4870 | term_printf("Error while deleting snapshot on '%s'\n", |
| 4871 | bdrv_get_device_name(bs1)); |
| 4872 | } |
| 4873 | } |
| 4874 | ret = bdrv_snapshot_create(bs1, sn); |
| 4875 | if (ret < 0) { |
| 4876 | term_printf("Error while creating snapshot on '%s'\n", |
| 4877 | bdrv_get_device_name(bs1)); |
| 4878 | } |
| 4879 | } |
| 4880 | } |
| 4881 | |
| 4882 | the_end: |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 4883 | if (saved_vm_running) |
| 4884 | vm_start(); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4885 | } |
| 4886 | |
| 4887 | void do_loadvm(const char *name) |
| 4888 | { |
| 4889 | BlockDriverState *bs, *bs1; |
| 4890 | BlockDriverInfo bdi1, *bdi = &bdi1; |
| 4891 | QEMUFile *f; |
| 4892 | int i, ret; |
| 4893 | int saved_vm_running; |
| 4894 | |
| 4895 | bs = get_bs_snapshots(); |
| 4896 | if (!bs) { |
| 4897 | term_printf("No block device supports snapshots\n"); |
| 4898 | return; |
| 4899 | } |
| 4900 | |
pbrook | 6192bc3 | 2006-09-03 12:08:37 +0000 | [diff] [blame] | 4901 | /* Flush all IO requests so they don't interfere with the new state. */ |
| 4902 | qemu_aio_flush(); |
| 4903 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 4904 | saved_vm_running = vm_running; |
| 4905 | vm_stop(0); |
| 4906 | |
| 4907 | for(i = 0; i <= MAX_DISKS; i++) { |
| 4908 | bs1 = bs_table[i]; |
| 4909 | if (bdrv_has_snapshot(bs1)) { |
| 4910 | ret = bdrv_snapshot_goto(bs1, name); |
| 4911 | if (ret < 0) { |
| 4912 | if (bs != bs1) |
| 4913 | term_printf("Warning: "); |
| 4914 | switch(ret) { |
| 4915 | case -ENOTSUP: |
| 4916 | term_printf("Snapshots not supported on device '%s'\n", |
| 4917 | bdrv_get_device_name(bs1)); |
| 4918 | break; |
| 4919 | case -ENOENT: |
| 4920 | term_printf("Could not find snapshot '%s' on device '%s'\n", |
| 4921 | name, bdrv_get_device_name(bs1)); |
| 4922 | break; |
| 4923 | default: |
| 4924 | term_printf("Error %d while activating snapshot on '%s'\n", |
| 4925 | ret, bdrv_get_device_name(bs1)); |
| 4926 | break; |
| 4927 | } |
| 4928 | /* fatal on snapshot block device */ |
| 4929 | if (bs == bs1) |
| 4930 | goto the_end; |
| 4931 | } |
| 4932 | } |
| 4933 | } |
| 4934 | |
| 4935 | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
| 4936 | term_printf("Device %s does not support VM state snapshots\n", |
| 4937 | bdrv_get_device_name(bs)); |
| 4938 | return; |
| 4939 | } |
| 4940 | |
| 4941 | /* restore the VM state */ |
| 4942 | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0); |
| 4943 | if (!f) { |
| 4944 | term_printf("Could not open VM state file\n"); |
| 4945 | goto the_end; |
| 4946 | } |
| 4947 | ret = qemu_loadvm_state(f); |
| 4948 | qemu_fclose(f); |
| 4949 | if (ret < 0) { |
| 4950 | term_printf("Error %d while loading VM state\n", ret); |
| 4951 | } |
| 4952 | the_end: |
| 4953 | if (saved_vm_running) |
| 4954 | vm_start(); |
| 4955 | } |
| 4956 | |
| 4957 | void do_delvm(const char *name) |
| 4958 | { |
| 4959 | BlockDriverState *bs, *bs1; |
| 4960 | int i, ret; |
| 4961 | |
| 4962 | bs = get_bs_snapshots(); |
| 4963 | if (!bs) { |
| 4964 | term_printf("No block device supports snapshots\n"); |
| 4965 | return; |
| 4966 | } |
| 4967 | |
| 4968 | for(i = 0; i <= MAX_DISKS; i++) { |
| 4969 | bs1 = bs_table[i]; |
| 4970 | if (bdrv_has_snapshot(bs1)) { |
| 4971 | ret = bdrv_snapshot_delete(bs1, name); |
| 4972 | if (ret < 0) { |
| 4973 | if (ret == -ENOTSUP) |
| 4974 | term_printf("Snapshots not supported on device '%s'\n", |
| 4975 | bdrv_get_device_name(bs1)); |
| 4976 | else |
| 4977 | term_printf("Error %d while deleting snapshot on '%s'\n", |
| 4978 | ret, bdrv_get_device_name(bs1)); |
| 4979 | } |
| 4980 | } |
| 4981 | } |
| 4982 | } |
| 4983 | |
| 4984 | void do_info_snapshots(void) |
| 4985 | { |
| 4986 | BlockDriverState *bs, *bs1; |
| 4987 | QEMUSnapshotInfo *sn_tab, *sn; |
| 4988 | int nb_sns, i; |
| 4989 | char buf[256]; |
| 4990 | |
| 4991 | bs = get_bs_snapshots(); |
| 4992 | if (!bs) { |
| 4993 | term_printf("No available block device supports snapshots\n"); |
| 4994 | return; |
| 4995 | } |
| 4996 | term_printf("Snapshot devices:"); |
| 4997 | for(i = 0; i <= MAX_DISKS; i++) { |
| 4998 | bs1 = bs_table[i]; |
| 4999 | if (bdrv_has_snapshot(bs1)) { |
| 5000 | if (bs == bs1) |
| 5001 | term_printf(" %s", bdrv_get_device_name(bs1)); |
| 5002 | } |
| 5003 | } |
| 5004 | term_printf("\n"); |
| 5005 | |
| 5006 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 5007 | if (nb_sns < 0) { |
| 5008 | term_printf("bdrv_snapshot_list: error %d\n", nb_sns); |
| 5009 | return; |
| 5010 | } |
| 5011 | term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs)); |
| 5012 | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
| 5013 | for(i = 0; i < nb_sns; i++) { |
| 5014 | sn = &sn_tab[i]; |
| 5015 | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
| 5016 | } |
| 5017 | qemu_free(sn_tab); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5018 | } |
| 5019 | |
| 5020 | /***********************************************************/ |
| 5021 | /* cpu save/restore */ |
| 5022 | |
| 5023 | #if defined(TARGET_I386) |
| 5024 | |
| 5025 | static void cpu_put_seg(QEMUFile *f, SegmentCache *dt) |
| 5026 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 5027 | qemu_put_be32(f, dt->selector); |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5028 | qemu_put_betl(f, dt->base); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5029 | qemu_put_be32(f, dt->limit); |
| 5030 | qemu_put_be32(f, dt->flags); |
| 5031 | } |
| 5032 | |
| 5033 | static void cpu_get_seg(QEMUFile *f, SegmentCache *dt) |
| 5034 | { |
bellard | 02ba45c | 2004-06-25 14:46:23 +0000 | [diff] [blame] | 5035 | dt->selector = qemu_get_be32(f); |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5036 | dt->base = qemu_get_betl(f); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5037 | dt->limit = qemu_get_be32(f); |
| 5038 | dt->flags = qemu_get_be32(f); |
| 5039 | } |
| 5040 | |
| 5041 | void cpu_save(QEMUFile *f, void *opaque) |
| 5042 | { |
| 5043 | CPUState *env = opaque; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5044 | uint16_t fptag, fpus, fpuc, fpregs_format; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5045 | uint32_t hflags; |
| 5046 | int i; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5047 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5048 | for(i = 0; i < CPU_NB_REGS; i++) |
| 5049 | qemu_put_betls(f, &env->regs[i]); |
| 5050 | qemu_put_betls(f, &env->eip); |
| 5051 | qemu_put_betls(f, &env->eflags); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5052 | hflags = env->hflags; /* XXX: suppress most of the redundant hflags */ |
| 5053 | qemu_put_be32s(f, &hflags); |
| 5054 | |
| 5055 | /* FPU */ |
| 5056 | fpuc = env->fpuc; |
| 5057 | fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; |
| 5058 | fptag = 0; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5059 | for(i = 0; i < 8; i++) { |
| 5060 | fptag |= ((!env->fptags[i]) << i); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5061 | } |
| 5062 | |
| 5063 | qemu_put_be16s(f, &fpuc); |
| 5064 | qemu_put_be16s(f, &fpus); |
| 5065 | qemu_put_be16s(f, &fptag); |
| 5066 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5067 | #ifdef USE_X86LDOUBLE |
| 5068 | fpregs_format = 0; |
| 5069 | #else |
| 5070 | fpregs_format = 1; |
| 5071 | #endif |
| 5072 | qemu_put_be16s(f, &fpregs_format); |
| 5073 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5074 | for(i = 0; i < 8; i++) { |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5075 | #ifdef USE_X86LDOUBLE |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5076 | { |
| 5077 | uint64_t mant; |
| 5078 | uint16_t exp; |
| 5079 | /* we save the real CPU data (in case of MMX usage only 'mant' |
| 5080 | contains the MMX register */ |
| 5081 | cpu_get_fp80(&mant, &exp, env->fpregs[i].d); |
| 5082 | qemu_put_be64(f, mant); |
| 5083 | qemu_put_be16(f, exp); |
| 5084 | } |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5085 | #else |
| 5086 | /* if we use doubles for float emulation, we save the doubles to |
| 5087 | avoid losing information in case of MMX usage. It can give |
| 5088 | problems if the image is restored on a CPU where long |
| 5089 | doubles are used instead. */ |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5090 | qemu_put_be64(f, env->fpregs[i].mmx.MMX_Q(0)); |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5091 | #endif |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5092 | } |
| 5093 | |
| 5094 | for(i = 0; i < 6; i++) |
| 5095 | cpu_put_seg(f, &env->segs[i]); |
| 5096 | cpu_put_seg(f, &env->ldt); |
| 5097 | cpu_put_seg(f, &env->tr); |
| 5098 | cpu_put_seg(f, &env->gdt); |
| 5099 | cpu_put_seg(f, &env->idt); |
| 5100 | |
| 5101 | qemu_put_be32s(f, &env->sysenter_cs); |
| 5102 | qemu_put_be32s(f, &env->sysenter_esp); |
| 5103 | qemu_put_be32s(f, &env->sysenter_eip); |
| 5104 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5105 | qemu_put_betls(f, &env->cr[0]); |
| 5106 | qemu_put_betls(f, &env->cr[2]); |
| 5107 | qemu_put_betls(f, &env->cr[3]); |
| 5108 | qemu_put_betls(f, &env->cr[4]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5109 | |
| 5110 | for(i = 0; i < 8; i++) |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5111 | qemu_put_betls(f, &env->dr[i]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5112 | |
| 5113 | /* MMU */ |
| 5114 | qemu_put_be32s(f, &env->a20_mask); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5115 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5116 | /* XMM */ |
| 5117 | qemu_put_be32s(f, &env->mxcsr); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5118 | for(i = 0; i < CPU_NB_REGS; i++) { |
| 5119 | qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(0)); |
| 5120 | qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(1)); |
| 5121 | } |
| 5122 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5123 | #ifdef TARGET_X86_64 |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5124 | qemu_put_be64s(f, &env->efer); |
| 5125 | qemu_put_be64s(f, &env->star); |
| 5126 | qemu_put_be64s(f, &env->lstar); |
| 5127 | qemu_put_be64s(f, &env->cstar); |
| 5128 | qemu_put_be64s(f, &env->fmask); |
| 5129 | qemu_put_be64s(f, &env->kernelgsbase); |
| 5130 | #endif |
bellard | 3b21e03 | 2006-09-24 18:41:56 +0000 | [diff] [blame] | 5131 | qemu_put_be32s(f, &env->smbase); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5132 | } |
| 5133 | |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5134 | #ifdef USE_X86LDOUBLE |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5135 | /* XXX: add that in a FPU generic layer */ |
| 5136 | union x86_longdouble { |
| 5137 | uint64_t mant; |
| 5138 | uint16_t exp; |
| 5139 | }; |
| 5140 | |
| 5141 | #define MANTD1(fp) (fp & ((1LL << 52) - 1)) |
| 5142 | #define EXPBIAS1 1023 |
| 5143 | #define EXPD1(fp) ((fp >> 52) & 0x7FF) |
| 5144 | #define SIGND1(fp) ((fp >> 32) & 0x80000000) |
| 5145 | |
| 5146 | static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp) |
| 5147 | { |
| 5148 | int e; |
| 5149 | /* mantissa */ |
| 5150 | p->mant = (MANTD1(temp) << 11) | (1LL << 63); |
| 5151 | /* exponent + sign */ |
| 5152 | e = EXPD1(temp) - EXPBIAS1 + 16383; |
| 5153 | e |= SIGND1(temp) >> 16; |
| 5154 | p->exp = e; |
| 5155 | } |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5156 | #endif |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5157 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5158 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5159 | { |
| 5160 | CPUState *env = opaque; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5161 | int i, guess_mmx; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5162 | uint32_t hflags; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5163 | uint16_t fpus, fpuc, fptag, fpregs_format; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5164 | |
bellard | 3b21e03 | 2006-09-24 18:41:56 +0000 | [diff] [blame] | 5165 | if (version_id != 3 && version_id != 4) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5166 | return -EINVAL; |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5167 | for(i = 0; i < CPU_NB_REGS; i++) |
| 5168 | qemu_get_betls(f, &env->regs[i]); |
| 5169 | qemu_get_betls(f, &env->eip); |
| 5170 | qemu_get_betls(f, &env->eflags); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5171 | qemu_get_be32s(f, &hflags); |
| 5172 | |
| 5173 | qemu_get_be16s(f, &fpuc); |
| 5174 | qemu_get_be16s(f, &fpus); |
| 5175 | qemu_get_be16s(f, &fptag); |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5176 | qemu_get_be16s(f, &fpregs_format); |
| 5177 | |
| 5178 | /* NOTE: we cannot always restore the FPU state if the image come |
| 5179 | from a host with a different 'USE_X86LDOUBLE' define. We guess |
| 5180 | if we are in an MMX state to restore correctly in that case. */ |
| 5181 | guess_mmx = ((fptag == 0xff) && (fpus & 0x3800) == 0); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5182 | for(i = 0; i < 8; i++) { |
| 5183 | uint64_t mant; |
| 5184 | uint16_t exp; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5185 | |
| 5186 | switch(fpregs_format) { |
| 5187 | case 0: |
| 5188 | mant = qemu_get_be64(f); |
| 5189 | exp = qemu_get_be16(f); |
| 5190 | #ifdef USE_X86LDOUBLE |
| 5191 | env->fpregs[i].d = cpu_set_fp80(mant, exp); |
| 5192 | #else |
| 5193 | /* difficult case */ |
| 5194 | if (guess_mmx) |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5195 | env->fpregs[i].mmx.MMX_Q(0) = mant; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5196 | else |
| 5197 | env->fpregs[i].d = cpu_set_fp80(mant, exp); |
| 5198 | #endif |
| 5199 | break; |
| 5200 | case 1: |
| 5201 | mant = qemu_get_be64(f); |
| 5202 | #ifdef USE_X86LDOUBLE |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5203 | { |
| 5204 | union x86_longdouble *p; |
| 5205 | /* difficult case */ |
| 5206 | p = (void *)&env->fpregs[i]; |
| 5207 | if (guess_mmx) { |
| 5208 | p->mant = mant; |
| 5209 | p->exp = 0xffff; |
| 5210 | } else { |
| 5211 | fp64_to_fp80(p, mant); |
| 5212 | } |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5213 | } |
| 5214 | #else |
bellard | 8636b5d | 2005-01-09 00:03:14 +0000 | [diff] [blame] | 5215 | env->fpregs[i].mmx.MMX_Q(0) = mant; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5216 | #endif |
| 5217 | break; |
| 5218 | default: |
| 5219 | return -EINVAL; |
| 5220 | } |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5221 | } |
| 5222 | |
| 5223 | env->fpuc = fpuc; |
bellard | 7a0e1f4 | 2005-03-13 17:01:47 +0000 | [diff] [blame] | 5224 | /* XXX: restore FPU round state */ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5225 | env->fpstt = (fpus >> 11) & 7; |
| 5226 | env->fpus = fpus & ~0x3800; |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5227 | fptag ^= 0xff; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5228 | for(i = 0; i < 8; i++) { |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5229 | env->fptags[i] = (fptag >> i) & 1; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5230 | } |
| 5231 | |
| 5232 | for(i = 0; i < 6; i++) |
| 5233 | cpu_get_seg(f, &env->segs[i]); |
| 5234 | cpu_get_seg(f, &env->ldt); |
| 5235 | cpu_get_seg(f, &env->tr); |
| 5236 | cpu_get_seg(f, &env->gdt); |
| 5237 | cpu_get_seg(f, &env->idt); |
| 5238 | |
| 5239 | qemu_get_be32s(f, &env->sysenter_cs); |
| 5240 | qemu_get_be32s(f, &env->sysenter_esp); |
| 5241 | qemu_get_be32s(f, &env->sysenter_eip); |
| 5242 | |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5243 | qemu_get_betls(f, &env->cr[0]); |
| 5244 | qemu_get_betls(f, &env->cr[2]); |
| 5245 | qemu_get_betls(f, &env->cr[3]); |
| 5246 | qemu_get_betls(f, &env->cr[4]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5247 | |
| 5248 | for(i = 0; i < 8; i++) |
bellard | 20f3228 | 2005-01-03 23:36:21 +0000 | [diff] [blame] | 5249 | qemu_get_betls(f, &env->dr[i]); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5250 | |
| 5251 | /* MMU */ |
| 5252 | qemu_get_be32s(f, &env->a20_mask); |
| 5253 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5254 | qemu_get_be32s(f, &env->mxcsr); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5255 | for(i = 0; i < CPU_NB_REGS; i++) { |
| 5256 | qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(0)); |
| 5257 | qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(1)); |
| 5258 | } |
| 5259 | |
bellard | 664e0f1 | 2005-01-08 18:58:29 +0000 | [diff] [blame] | 5260 | #ifdef TARGET_X86_64 |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5261 | qemu_get_be64s(f, &env->efer); |
| 5262 | qemu_get_be64s(f, &env->star); |
| 5263 | qemu_get_be64s(f, &env->lstar); |
| 5264 | qemu_get_be64s(f, &env->cstar); |
| 5265 | qemu_get_be64s(f, &env->fmask); |
| 5266 | qemu_get_be64s(f, &env->kernelgsbase); |
| 5267 | #endif |
bellard | 3b21e03 | 2006-09-24 18:41:56 +0000 | [diff] [blame] | 5268 | if (version_id >= 4) |
| 5269 | qemu_get_be32s(f, &env->smbase); |
bellard | 02536f8 | 2005-01-06 20:43:38 +0000 | [diff] [blame] | 5270 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5271 | /* XXX: compute hflags from scratch, except for CPL and IIF */ |
| 5272 | env->hflags = hflags; |
| 5273 | tlb_flush(env, 1); |
| 5274 | return 0; |
| 5275 | } |
| 5276 | |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 5277 | #elif defined(TARGET_PPC) |
| 5278 | void cpu_save(QEMUFile *f, void *opaque) |
| 5279 | { |
| 5280 | } |
| 5281 | |
| 5282 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5283 | { |
| 5284 | return 0; |
| 5285 | } |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 5286 | |
| 5287 | #elif defined(TARGET_MIPS) |
| 5288 | void cpu_save(QEMUFile *f, void *opaque) |
| 5289 | { |
| 5290 | } |
| 5291 | |
| 5292 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5293 | { |
| 5294 | return 0; |
| 5295 | } |
| 5296 | |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 5297 | #elif defined(TARGET_SPARC) |
| 5298 | void cpu_save(QEMUFile *f, void *opaque) |
| 5299 | { |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5300 | CPUState *env = opaque; |
| 5301 | int i; |
| 5302 | uint32_t tmp; |
| 5303 | |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5304 | for(i = 0; i < 8; i++) |
| 5305 | qemu_put_betls(f, &env->gregs[i]); |
| 5306 | for(i = 0; i < NWINDOWS * 16; i++) |
| 5307 | qemu_put_betls(f, &env->regbase[i]); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5308 | |
| 5309 | /* FPU */ |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5310 | for(i = 0; i < TARGET_FPREGS; i++) { |
| 5311 | union { |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5312 | float32 f; |
| 5313 | uint32_t i; |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5314 | } u; |
| 5315 | u.f = env->fpr[i]; |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5316 | qemu_put_be32(f, u.i); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5317 | } |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5318 | |
| 5319 | qemu_put_betls(f, &env->pc); |
| 5320 | qemu_put_betls(f, &env->npc); |
| 5321 | qemu_put_betls(f, &env->y); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5322 | tmp = GET_PSR(env); |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5323 | qemu_put_be32(f, tmp); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5324 | qemu_put_betls(f, &env->fsr); |
| 5325 | qemu_put_betls(f, &env->tbr); |
| 5326 | #ifndef TARGET_SPARC64 |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5327 | qemu_put_be32s(f, &env->wim); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5328 | /* MMU */ |
| 5329 | for(i = 0; i < 16; i++) |
| 5330 | qemu_put_be32s(f, &env->mmuregs[i]); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5331 | #endif |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 5332 | } |
| 5333 | |
| 5334 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5335 | { |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5336 | CPUState *env = opaque; |
| 5337 | int i; |
| 5338 | uint32_t tmp; |
| 5339 | |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5340 | for(i = 0; i < 8; i++) |
| 5341 | qemu_get_betls(f, &env->gregs[i]); |
| 5342 | for(i = 0; i < NWINDOWS * 16; i++) |
| 5343 | qemu_get_betls(f, &env->regbase[i]); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5344 | |
| 5345 | /* FPU */ |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5346 | for(i = 0; i < TARGET_FPREGS; i++) { |
| 5347 | union { |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5348 | float32 f; |
| 5349 | uint32_t i; |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5350 | } u; |
bellard | 1bdb68e | 2006-06-21 18:48:01 +0000 | [diff] [blame] | 5351 | u.i = qemu_get_be32(f); |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5352 | env->fpr[i] = u.f; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5353 | } |
bellard | 4fa5d77 | 2005-01-30 22:57:54 +0000 | [diff] [blame] | 5354 | |
| 5355 | qemu_get_betls(f, &env->pc); |
| 5356 | qemu_get_betls(f, &env->npc); |
| 5357 | qemu_get_betls(f, &env->y); |
| 5358 | tmp = qemu_get_be32(f); |
| 5359 | env->cwp = 0; /* needed to ensure that the wrapping registers are |
| 5360 | correctly updated */ |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5361 | PUT_PSR(env, tmp); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5362 | qemu_get_betls(f, &env->fsr); |
| 5363 | qemu_get_betls(f, &env->tbr); |
| 5364 | #ifndef TARGET_SPARC64 |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5365 | qemu_get_be32s(f, &env->wim); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5366 | /* MMU */ |
| 5367 | for(i = 0; i < 16; i++) |
| 5368 | qemu_get_be32s(f, &env->mmuregs[i]); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5369 | #endif |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 5370 | tlb_flush(env, 1); |
bellard | e95c8d5 | 2004-09-30 22:22:08 +0000 | [diff] [blame] | 5371 | return 0; |
| 5372 | } |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 5373 | |
| 5374 | #elif defined(TARGET_ARM) |
| 5375 | |
| 5376 | /* ??? Need to implement these. */ |
| 5377 | void cpu_save(QEMUFile *f, void *opaque) |
| 5378 | { |
| 5379 | } |
| 5380 | |
| 5381 | int cpu_load(QEMUFile *f, void *opaque, int version_id) |
| 5382 | { |
| 5383 | return 0; |
| 5384 | } |
| 5385 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5386 | #else |
| 5387 | |
| 5388 | #warning No CPU save/restore functions |
| 5389 | |
| 5390 | #endif |
| 5391 | |
| 5392 | /***********************************************************/ |
| 5393 | /* ram save/restore */ |
| 5394 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5395 | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
| 5396 | { |
| 5397 | int v; |
| 5398 | |
| 5399 | v = qemu_get_byte(f); |
| 5400 | switch(v) { |
| 5401 | case 0: |
| 5402 | if (qemu_get_buffer(f, buf, len) != len) |
| 5403 | return -EIO; |
| 5404 | break; |
| 5405 | case 1: |
| 5406 | v = qemu_get_byte(f); |
| 5407 | memset(buf, v, len); |
| 5408 | break; |
| 5409 | default: |
| 5410 | return -EINVAL; |
| 5411 | } |
| 5412 | return 0; |
| 5413 | } |
| 5414 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 5415 | static int ram_load_v1(QEMUFile *f, void *opaque) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5416 | { |
| 5417 | int i, ret; |
| 5418 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5419 | if (qemu_get_be32(f) != phys_ram_size) |
| 5420 | return -EINVAL; |
| 5421 | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
| 5422 | ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
| 5423 | if (ret) |
| 5424 | return ret; |
| 5425 | } |
| 5426 | return 0; |
| 5427 | } |
| 5428 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 5429 | #define BDRV_HASH_BLOCK_SIZE 1024 |
| 5430 | #define IOBUF_SIZE 4096 |
| 5431 | #define RAM_CBLOCK_MAGIC 0xfabe |
| 5432 | |
| 5433 | typedef struct RamCompressState { |
| 5434 | z_stream zstream; |
| 5435 | QEMUFile *f; |
| 5436 | uint8_t buf[IOBUF_SIZE]; |
| 5437 | } RamCompressState; |
| 5438 | |
| 5439 | static int ram_compress_open(RamCompressState *s, QEMUFile *f) |
| 5440 | { |
| 5441 | int ret; |
| 5442 | memset(s, 0, sizeof(*s)); |
| 5443 | s->f = f; |
| 5444 | ret = deflateInit2(&s->zstream, 1, |
| 5445 | Z_DEFLATED, 15, |
| 5446 | 9, Z_DEFAULT_STRATEGY); |
| 5447 | if (ret != Z_OK) |
| 5448 | return -1; |
| 5449 | s->zstream.avail_out = IOBUF_SIZE; |
| 5450 | s->zstream.next_out = s->buf; |
| 5451 | return 0; |
| 5452 | } |
| 5453 | |
| 5454 | static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len) |
| 5455 | { |
| 5456 | qemu_put_be16(s->f, RAM_CBLOCK_MAGIC); |
| 5457 | qemu_put_be16(s->f, len); |
| 5458 | qemu_put_buffer(s->f, buf, len); |
| 5459 | } |
| 5460 | |
| 5461 | static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len) |
| 5462 | { |
| 5463 | int ret; |
| 5464 | |
| 5465 | s->zstream.avail_in = len; |
| 5466 | s->zstream.next_in = (uint8_t *)buf; |
| 5467 | while (s->zstream.avail_in > 0) { |
| 5468 | ret = deflate(&s->zstream, Z_NO_FLUSH); |
| 5469 | if (ret != Z_OK) |
| 5470 | return -1; |
| 5471 | if (s->zstream.avail_out == 0) { |
| 5472 | ram_put_cblock(s, s->buf, IOBUF_SIZE); |
| 5473 | s->zstream.avail_out = IOBUF_SIZE; |
| 5474 | s->zstream.next_out = s->buf; |
| 5475 | } |
| 5476 | } |
| 5477 | return 0; |
| 5478 | } |
| 5479 | |
| 5480 | static void ram_compress_close(RamCompressState *s) |
| 5481 | { |
| 5482 | int len, ret; |
| 5483 | |
| 5484 | /* compress last bytes */ |
| 5485 | for(;;) { |
| 5486 | ret = deflate(&s->zstream, Z_FINISH); |
| 5487 | if (ret == Z_OK || ret == Z_STREAM_END) { |
| 5488 | len = IOBUF_SIZE - s->zstream.avail_out; |
| 5489 | if (len > 0) { |
| 5490 | ram_put_cblock(s, s->buf, len); |
| 5491 | } |
| 5492 | s->zstream.avail_out = IOBUF_SIZE; |
| 5493 | s->zstream.next_out = s->buf; |
| 5494 | if (ret == Z_STREAM_END) |
| 5495 | break; |
| 5496 | } else { |
| 5497 | goto fail; |
| 5498 | } |
| 5499 | } |
| 5500 | fail: |
| 5501 | deflateEnd(&s->zstream); |
| 5502 | } |
| 5503 | |
| 5504 | typedef struct RamDecompressState { |
| 5505 | z_stream zstream; |
| 5506 | QEMUFile *f; |
| 5507 | uint8_t buf[IOBUF_SIZE]; |
| 5508 | } RamDecompressState; |
| 5509 | |
| 5510 | static int ram_decompress_open(RamDecompressState *s, QEMUFile *f) |
| 5511 | { |
| 5512 | int ret; |
| 5513 | memset(s, 0, sizeof(*s)); |
| 5514 | s->f = f; |
| 5515 | ret = inflateInit(&s->zstream); |
| 5516 | if (ret != Z_OK) |
| 5517 | return -1; |
| 5518 | return 0; |
| 5519 | } |
| 5520 | |
| 5521 | static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len) |
| 5522 | { |
| 5523 | int ret, clen; |
| 5524 | |
| 5525 | s->zstream.avail_out = len; |
| 5526 | s->zstream.next_out = buf; |
| 5527 | while (s->zstream.avail_out > 0) { |
| 5528 | if (s->zstream.avail_in == 0) { |
| 5529 | if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC) |
| 5530 | return -1; |
| 5531 | clen = qemu_get_be16(s->f); |
| 5532 | if (clen > IOBUF_SIZE) |
| 5533 | return -1; |
| 5534 | qemu_get_buffer(s->f, s->buf, clen); |
| 5535 | s->zstream.avail_in = clen; |
| 5536 | s->zstream.next_in = s->buf; |
| 5537 | } |
| 5538 | ret = inflate(&s->zstream, Z_PARTIAL_FLUSH); |
| 5539 | if (ret != Z_OK && ret != Z_STREAM_END) { |
| 5540 | return -1; |
| 5541 | } |
| 5542 | } |
| 5543 | return 0; |
| 5544 | } |
| 5545 | |
| 5546 | static void ram_decompress_close(RamDecompressState *s) |
| 5547 | { |
| 5548 | inflateEnd(&s->zstream); |
| 5549 | } |
| 5550 | |
| 5551 | static void ram_save(QEMUFile *f, void *opaque) |
| 5552 | { |
| 5553 | int i; |
| 5554 | RamCompressState s1, *s = &s1; |
| 5555 | uint8_t buf[10]; |
| 5556 | |
| 5557 | qemu_put_be32(f, phys_ram_size); |
| 5558 | if (ram_compress_open(s, f) < 0) |
| 5559 | return; |
| 5560 | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
| 5561 | #if 0 |
| 5562 | if (tight_savevm_enabled) { |
| 5563 | int64_t sector_num; |
| 5564 | int j; |
| 5565 | |
| 5566 | /* find if the memory block is available on a virtual |
| 5567 | block device */ |
| 5568 | sector_num = -1; |
| 5569 | for(j = 0; j < MAX_DISKS; j++) { |
| 5570 | if (bs_table[j]) { |
| 5571 | sector_num = bdrv_hash_find(bs_table[j], |
| 5572 | phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); |
| 5573 | if (sector_num >= 0) |
| 5574 | break; |
| 5575 | } |
| 5576 | } |
| 5577 | if (j == MAX_DISKS) |
| 5578 | goto normal_compress; |
| 5579 | buf[0] = 1; |
| 5580 | buf[1] = j; |
| 5581 | cpu_to_be64wu((uint64_t *)(buf + 2), sector_num); |
| 5582 | ram_compress_buf(s, buf, 10); |
| 5583 | } else |
| 5584 | #endif |
| 5585 | { |
| 5586 | // normal_compress: |
| 5587 | buf[0] = 0; |
| 5588 | ram_compress_buf(s, buf, 1); |
| 5589 | ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); |
| 5590 | } |
| 5591 | } |
| 5592 | ram_compress_close(s); |
| 5593 | } |
| 5594 | |
| 5595 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 5596 | { |
| 5597 | RamDecompressState s1, *s = &s1; |
| 5598 | uint8_t buf[10]; |
| 5599 | int i; |
| 5600 | |
| 5601 | if (version_id == 1) |
| 5602 | return ram_load_v1(f, opaque); |
| 5603 | if (version_id != 2) |
| 5604 | return -EINVAL; |
| 5605 | if (qemu_get_be32(f) != phys_ram_size) |
| 5606 | return -EINVAL; |
| 5607 | if (ram_decompress_open(s, f) < 0) |
| 5608 | return -EINVAL; |
| 5609 | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
| 5610 | if (ram_decompress_buf(s, buf, 1) < 0) { |
| 5611 | fprintf(stderr, "Error while reading ram block header\n"); |
| 5612 | goto error; |
| 5613 | } |
| 5614 | if (buf[0] == 0) { |
| 5615 | if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) { |
| 5616 | fprintf(stderr, "Error while reading ram block address=0x%08x", i); |
| 5617 | goto error; |
| 5618 | } |
| 5619 | } else |
| 5620 | #if 0 |
| 5621 | if (buf[0] == 1) { |
| 5622 | int bs_index; |
| 5623 | int64_t sector_num; |
| 5624 | |
| 5625 | ram_decompress_buf(s, buf + 1, 9); |
| 5626 | bs_index = buf[1]; |
| 5627 | sector_num = be64_to_cpupu((const uint64_t *)(buf + 2)); |
| 5628 | if (bs_index >= MAX_DISKS || bs_table[bs_index] == NULL) { |
| 5629 | fprintf(stderr, "Invalid block device index %d\n", bs_index); |
| 5630 | goto error; |
| 5631 | } |
| 5632 | if (bdrv_read(bs_table[bs_index], sector_num, phys_ram_base + i, |
| 5633 | BDRV_HASH_BLOCK_SIZE / 512) < 0) { |
| 5634 | fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n", |
| 5635 | bs_index, sector_num); |
| 5636 | goto error; |
| 5637 | } |
| 5638 | } else |
| 5639 | #endif |
| 5640 | { |
| 5641 | error: |
| 5642 | printf("Error block header\n"); |
| 5643 | return -EINVAL; |
| 5644 | } |
| 5645 | } |
| 5646 | ram_decompress_close(s); |
| 5647 | return 0; |
| 5648 | } |
| 5649 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5650 | /***********************************************************/ |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5651 | /* bottom halves (can be seen as timers which expire ASAP) */ |
| 5652 | |
| 5653 | struct QEMUBH { |
| 5654 | QEMUBHFunc *cb; |
| 5655 | void *opaque; |
| 5656 | int scheduled; |
| 5657 | QEMUBH *next; |
| 5658 | }; |
| 5659 | |
| 5660 | static QEMUBH *first_bh = NULL; |
| 5661 | |
| 5662 | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque) |
| 5663 | { |
| 5664 | QEMUBH *bh; |
| 5665 | bh = qemu_mallocz(sizeof(QEMUBH)); |
| 5666 | if (!bh) |
| 5667 | return NULL; |
| 5668 | bh->cb = cb; |
| 5669 | bh->opaque = opaque; |
| 5670 | return bh; |
| 5671 | } |
| 5672 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5673 | int qemu_bh_poll(void) |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5674 | { |
| 5675 | QEMUBH *bh, **pbh; |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5676 | int ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5677 | |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5678 | ret = 0; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5679 | for(;;) { |
| 5680 | pbh = &first_bh; |
| 5681 | bh = *pbh; |
| 5682 | if (!bh) |
| 5683 | break; |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5684 | ret = 1; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5685 | *pbh = bh->next; |
| 5686 | bh->scheduled = 0; |
| 5687 | bh->cb(bh->opaque); |
| 5688 | } |
bellard | 6eb5733 | 2006-08-06 09:51:25 +0000 | [diff] [blame] | 5689 | return ret; |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5690 | } |
| 5691 | |
| 5692 | void qemu_bh_schedule(QEMUBH *bh) |
| 5693 | { |
| 5694 | CPUState *env = cpu_single_env; |
| 5695 | if (bh->scheduled) |
| 5696 | return; |
| 5697 | bh->scheduled = 1; |
| 5698 | bh->next = first_bh; |
| 5699 | first_bh = bh; |
| 5700 | |
| 5701 | /* stop the currently executing CPU to execute the BH ASAP */ |
| 5702 | if (env) { |
| 5703 | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
| 5704 | } |
| 5705 | } |
| 5706 | |
| 5707 | void qemu_bh_cancel(QEMUBH *bh) |
| 5708 | { |
| 5709 | QEMUBH **pbh; |
| 5710 | if (bh->scheduled) { |
| 5711 | pbh = &first_bh; |
| 5712 | while (*pbh != bh) |
| 5713 | pbh = &(*pbh)->next; |
| 5714 | *pbh = bh->next; |
| 5715 | bh->scheduled = 0; |
| 5716 | } |
| 5717 | } |
| 5718 | |
| 5719 | void qemu_bh_delete(QEMUBH *bh) |
| 5720 | { |
| 5721 | qemu_bh_cancel(bh); |
| 5722 | qemu_free(bh); |
| 5723 | } |
| 5724 | |
| 5725 | /***********************************************************/ |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 5726 | /* machine registration */ |
| 5727 | |
| 5728 | QEMUMachine *first_machine = NULL; |
| 5729 | |
| 5730 | int qemu_register_machine(QEMUMachine *m) |
| 5731 | { |
| 5732 | QEMUMachine **pm; |
| 5733 | pm = &first_machine; |
| 5734 | while (*pm != NULL) |
| 5735 | pm = &(*pm)->next; |
| 5736 | m->next = NULL; |
| 5737 | *pm = m; |
| 5738 | return 0; |
| 5739 | } |
| 5740 | |
| 5741 | QEMUMachine *find_machine(const char *name) |
| 5742 | { |
| 5743 | QEMUMachine *m; |
| 5744 | |
| 5745 | for(m = first_machine; m != NULL; m = m->next) { |
| 5746 | if (!strcmp(m->name, name)) |
| 5747 | return m; |
| 5748 | } |
| 5749 | return NULL; |
| 5750 | } |
| 5751 | |
| 5752 | /***********************************************************/ |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5753 | /* main execution loop */ |
| 5754 | |
| 5755 | void gui_update(void *opaque) |
| 5756 | { |
| 5757 | display_state.dpy_refresh(&display_state); |
| 5758 | qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock)); |
| 5759 | } |
| 5760 | |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 5761 | struct vm_change_state_entry { |
| 5762 | VMChangeStateHandler *cb; |
| 5763 | void *opaque; |
| 5764 | LIST_ENTRY (vm_change_state_entry) entries; |
| 5765 | }; |
| 5766 | |
| 5767 | static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head; |
| 5768 | |
| 5769 | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
| 5770 | void *opaque) |
| 5771 | { |
| 5772 | VMChangeStateEntry *e; |
| 5773 | |
| 5774 | e = qemu_mallocz(sizeof (*e)); |
| 5775 | if (!e) |
| 5776 | return NULL; |
| 5777 | |
| 5778 | e->cb = cb; |
| 5779 | e->opaque = opaque; |
| 5780 | LIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
| 5781 | return e; |
| 5782 | } |
| 5783 | |
| 5784 | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) |
| 5785 | { |
| 5786 | LIST_REMOVE (e, entries); |
| 5787 | qemu_free (e); |
| 5788 | } |
| 5789 | |
| 5790 | static void vm_state_notify(int running) |
| 5791 | { |
| 5792 | VMChangeStateEntry *e; |
| 5793 | |
| 5794 | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) { |
| 5795 | e->cb(e->opaque, running); |
| 5796 | } |
| 5797 | } |
| 5798 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5799 | /* XXX: support several handlers */ |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 5800 | static VMStopHandler *vm_stop_cb; |
| 5801 | static void *vm_stop_opaque; |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5802 | |
| 5803 | int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) |
| 5804 | { |
| 5805 | vm_stop_cb = cb; |
| 5806 | vm_stop_opaque = opaque; |
| 5807 | return 0; |
| 5808 | } |
| 5809 | |
| 5810 | void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) |
| 5811 | { |
| 5812 | vm_stop_cb = NULL; |
| 5813 | } |
| 5814 | |
| 5815 | void vm_start(void) |
| 5816 | { |
| 5817 | if (!vm_running) { |
| 5818 | cpu_enable_ticks(); |
| 5819 | vm_running = 1; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 5820 | vm_state_notify(1); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5821 | } |
| 5822 | } |
| 5823 | |
| 5824 | void vm_stop(int reason) |
| 5825 | { |
| 5826 | if (vm_running) { |
| 5827 | cpu_disable_ticks(); |
| 5828 | vm_running = 0; |
| 5829 | if (reason != 0) { |
| 5830 | if (vm_stop_cb) { |
| 5831 | vm_stop_cb(vm_stop_opaque, reason); |
| 5832 | } |
| 5833 | } |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 5834 | vm_state_notify(0); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5835 | } |
| 5836 | } |
| 5837 | |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 5838 | /* reset/shutdown handler */ |
| 5839 | |
| 5840 | typedef struct QEMUResetEntry { |
| 5841 | QEMUResetHandler *func; |
| 5842 | void *opaque; |
| 5843 | struct QEMUResetEntry *next; |
| 5844 | } QEMUResetEntry; |
| 5845 | |
| 5846 | static QEMUResetEntry *first_reset_entry; |
| 5847 | static int reset_requested; |
| 5848 | static int shutdown_requested; |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5849 | static int powerdown_requested; |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 5850 | |
| 5851 | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
| 5852 | { |
| 5853 | QEMUResetEntry **pre, *re; |
| 5854 | |
| 5855 | pre = &first_reset_entry; |
| 5856 | while (*pre != NULL) |
| 5857 | pre = &(*pre)->next; |
| 5858 | re = qemu_mallocz(sizeof(QEMUResetEntry)); |
| 5859 | re->func = func; |
| 5860 | re->opaque = opaque; |
| 5861 | re->next = NULL; |
| 5862 | *pre = re; |
| 5863 | } |
| 5864 | |
ths | 52f61fd | 2006-12-22 21:20:52 +0000 | [diff] [blame] | 5865 | static void qemu_system_reset(void) |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 5866 | { |
| 5867 | QEMUResetEntry *re; |
| 5868 | |
| 5869 | /* reset all devices */ |
| 5870 | for(re = first_reset_entry; re != NULL; re = re->next) { |
| 5871 | re->func(re->opaque); |
| 5872 | } |
| 5873 | } |
| 5874 | |
| 5875 | void qemu_system_reset_request(void) |
| 5876 | { |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 5877 | if (no_reboot) { |
| 5878 | shutdown_requested = 1; |
| 5879 | } else { |
| 5880 | reset_requested = 1; |
| 5881 | } |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5882 | if (cpu_single_env) |
| 5883 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 5884 | } |
| 5885 | |
| 5886 | void qemu_system_shutdown_request(void) |
| 5887 | { |
| 5888 | shutdown_requested = 1; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5889 | if (cpu_single_env) |
| 5890 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 5891 | } |
| 5892 | |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 5893 | void qemu_system_powerdown_request(void) |
| 5894 | { |
| 5895 | powerdown_requested = 1; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 5896 | if (cpu_single_env) |
| 5897 | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 5898 | } |
| 5899 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 5900 | void main_loop_wait(int timeout) |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5901 | { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 5902 | IOHandlerRecord *ioh, *ioh_next; |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 5903 | fd_set rfds, wfds, xfds; |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 5904 | int ret, nfds; |
| 5905 | struct timeval tv; |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 5906 | PollingEntry *pe; |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5907 | |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 5908 | |
| 5909 | /* XXX: need to suppress polling by better using win32 events */ |
| 5910 | ret = 0; |
| 5911 | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
| 5912 | ret |= pe->func(pe->opaque); |
| 5913 | } |
bellard | 38e205a | 2004-04-06 19:29:17 +0000 | [diff] [blame] | 5914 | #ifdef _WIN32 |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 5915 | if (ret == 0 && timeout > 0) { |
bellard | a18e524 | 2006-06-25 17:18:27 +0000 | [diff] [blame] | 5916 | int err; |
| 5917 | WaitObjects *w = &wait_objects; |
| 5918 | |
| 5919 | ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout); |
| 5920 | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
| 5921 | if (w->func[ret - WAIT_OBJECT_0]) |
| 5922 | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
| 5923 | } else if (ret == WAIT_TIMEOUT) { |
| 5924 | } else { |
| 5925 | err = GetLastError(); |
| 5926 | fprintf(stderr, "Wait error %d %d\n", ret, err); |
| 5927 | } |
bellard | f331110 | 2006-04-12 20:21:17 +0000 | [diff] [blame] | 5928 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 5929 | #endif |
| 5930 | /* poll any events */ |
| 5931 | /* XXX: separate device handlers from system ones */ |
| 5932 | nfds = -1; |
| 5933 | FD_ZERO(&rfds); |
| 5934 | FD_ZERO(&wfds); |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 5935 | FD_ZERO(&xfds); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 5936 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
| 5937 | if (ioh->fd_read && |
| 5938 | (!ioh->fd_read_poll || |
| 5939 | ioh->fd_read_poll(ioh->opaque) != 0)) { |
| 5940 | FD_SET(ioh->fd, &rfds); |
| 5941 | if (ioh->fd > nfds) |
| 5942 | nfds = ioh->fd; |
| 5943 | } |
| 5944 | if (ioh->fd_write) { |
| 5945 | FD_SET(ioh->fd, &wfds); |
| 5946 | if (ioh->fd > nfds) |
| 5947 | nfds = ioh->fd; |
| 5948 | } |
| 5949 | } |
| 5950 | |
| 5951 | tv.tv_sec = 0; |
| 5952 | #ifdef _WIN32 |
| 5953 | tv.tv_usec = 0; |
bellard | 38e205a | 2004-04-06 19:29:17 +0000 | [diff] [blame] | 5954 | #else |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 5955 | tv.tv_usec = timeout * 1000; |
| 5956 | #endif |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 5957 | #if defined(CONFIG_SLIRP) |
| 5958 | if (slirp_inited) { |
| 5959 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
| 5960 | } |
| 5961 | #endif |
| 5962 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv); |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 5963 | if (ret > 0) { |
| 5964 | /* XXX: better handling of removal */ |
| 5965 | for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) { |
| 5966 | ioh_next = ioh->next; |
| 5967 | if (FD_ISSET(ioh->fd, &rfds)) { |
| 5968 | ioh->fd_read(ioh->opaque); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 5969 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 5970 | if (FD_ISSET(ioh->fd, &wfds)) { |
| 5971 | ioh->fd_write(ioh->opaque); |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 5972 | } |
| 5973 | } |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 5974 | } |
bellard | e035649 | 2006-05-01 13:33:02 +0000 | [diff] [blame] | 5975 | #if defined(CONFIG_SLIRP) |
| 5976 | if (slirp_inited) { |
| 5977 | if (ret < 0) { |
| 5978 | FD_ZERO(&rfds); |
| 5979 | FD_ZERO(&wfds); |
| 5980 | FD_ZERO(&xfds); |
| 5981 | } |
| 5982 | slirp_select_poll(&rfds, &wfds, &xfds); |
| 5983 | } |
| 5984 | #endif |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 5985 | qemu_aio_poll(); |
| 5986 | qemu_bh_poll(); |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 5987 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 5988 | if (vm_running) { |
| 5989 | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
| 5990 | qemu_get_clock(vm_clock)); |
| 5991 | /* run dma transfers, if any */ |
| 5992 | DMA_run(); |
| 5993 | } |
| 5994 | |
| 5995 | /* real time timers */ |
| 5996 | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
| 5997 | qemu_get_clock(rt_clock)); |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 5998 | } |
| 5999 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6000 | static CPUState *cur_cpu; |
| 6001 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6002 | int main_loop(void) |
| 6003 | { |
| 6004 | int ret, timeout; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6005 | #ifdef CONFIG_PROFILER |
| 6006 | int64_t ti; |
| 6007 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6008 | CPUState *env; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6009 | |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6010 | cur_cpu = first_cpu; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6011 | for(;;) { |
| 6012 | if (vm_running) { |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 6013 | |
| 6014 | env = cur_cpu; |
| 6015 | for(;;) { |
| 6016 | /* get next cpu */ |
| 6017 | env = env->next_cpu; |
| 6018 | if (!env) |
| 6019 | env = first_cpu; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6020 | #ifdef CONFIG_PROFILER |
| 6021 | ti = profile_getclock(); |
| 6022 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6023 | ret = cpu_exec(env); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6024 | #ifdef CONFIG_PROFILER |
| 6025 | qemu_time += profile_getclock() - ti; |
| 6026 | #endif |
bellard | 15a7644 | 2005-11-23 21:01:03 +0000 | [diff] [blame] | 6027 | if (ret != EXCP_HALTED) |
| 6028 | break; |
| 6029 | /* all CPUs are halted ? */ |
| 6030 | if (env == cur_cpu) { |
| 6031 | ret = EXCP_HLT; |
| 6032 | break; |
| 6033 | } |
| 6034 | } |
| 6035 | cur_cpu = env; |
| 6036 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6037 | if (shutdown_requested) { |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6038 | ret = EXCP_INTERRUPT; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6039 | break; |
| 6040 | } |
| 6041 | if (reset_requested) { |
| 6042 | reset_requested = 0; |
| 6043 | qemu_system_reset(); |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6044 | ret = EXCP_INTERRUPT; |
| 6045 | } |
| 6046 | if (powerdown_requested) { |
| 6047 | powerdown_requested = 0; |
| 6048 | qemu_system_powerdown(); |
| 6049 | ret = EXCP_INTERRUPT; |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6050 | } |
| 6051 | if (ret == EXCP_DEBUG) { |
| 6052 | vm_stop(EXCP_DEBUG); |
| 6053 | } |
| 6054 | /* if hlt instruction, we wait until the next IRQ */ |
| 6055 | /* XXX: use timeout computed from timers */ |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6056 | if (ret == EXCP_HLT) |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6057 | timeout = 10; |
| 6058 | else |
| 6059 | timeout = 0; |
| 6060 | } else { |
| 6061 | timeout = 10; |
| 6062 | } |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6063 | #ifdef CONFIG_PROFILER |
| 6064 | ti = profile_getclock(); |
| 6065 | #endif |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6066 | main_loop_wait(timeout); |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6067 | #ifdef CONFIG_PROFILER |
| 6068 | dev_time += profile_getclock() - ti; |
| 6069 | #endif |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6070 | } |
bellard | 3486513 | 2003-10-05 14:28:56 +0000 | [diff] [blame] | 6071 | cpu_disable_ticks(); |
| 6072 | return ret; |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6073 | } |
| 6074 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6075 | void help(void) |
| 6076 | { |
bellard | fb43f4d | 2006-08-07 21:34:46 +0000 | [diff] [blame] | 6077 | printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2006 Fabrice Bellard\n" |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 6078 | "usage: %s [options] [disk_image]\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6079 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6080 | "'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] | 6081 | "\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6082 | "Standard options:\n" |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6083 | "-M machine select emulated machine (-M ? for list)\n" |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 6084 | "-fda/-fdb file use 'file' as floppy disk 0/1 image\n" |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 6085 | "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n" |
| 6086 | "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6087 | "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n" |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 6088 | "-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] | 6089 | "-snapshot write to temporary files instead of disk image files\n" |
| 6090 | #ifdef CONFIG_SDL |
| 6091 | "-no-quit disable SDL window close capability\n" |
| 6092 | #endif |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 6093 | #ifdef TARGET_I386 |
| 6094 | "-no-fd-bootchk disable boot signature checking for floppy disks\n" |
| 6095 | #endif |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6096 | "-m megs set virtual RAM size to megs MB [default=%d]\n" |
bellard | 91fc211 | 2005-12-18 19:09:37 +0000 | [diff] [blame] | 6097 | "-smp n set the number of CPUs to 'n' [default=1]\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6098 | "-nographic disable graphical output and redirect serial I/Os to console\n" |
bellard | 4ca0074 | 2004-12-12 22:20:04 +0000 | [diff] [blame] | 6099 | #ifndef _WIN32 |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6100 | "-k language use keyboard layout (for example \"fr\" for French)\n" |
bellard | 4ca0074 | 2004-12-12 22:20:04 +0000 | [diff] [blame] | 6101 | #endif |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6102 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6103 | "-audio-help print list of audio drivers and their options\n" |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 6104 | "-soundhw c1,... enable audio support\n" |
| 6105 | " and only specified sound cards (comma separated list)\n" |
| 6106 | " use -soundhw ? to get the list of supported cards\n" |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6107 | " use -soundhw all to enable all of them\n" |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6108 | #endif |
bellard | 8998028 | 2004-06-03 14:04:03 +0000 | [diff] [blame] | 6109 | "-localtime set the real time clock to local time [default=utc]\n" |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6110 | "-full-screen start in full screen\n" |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 6111 | #ifdef TARGET_I386 |
| 6112 | "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n" |
| 6113 | #endif |
bellard | b389dbf | 2005-11-06 16:49:55 +0000 | [diff] [blame] | 6114 | "-usb enable the USB driver (will be the default soon)\n" |
| 6115 | "-usbdevice name add the host or guest USB device 'name'\n" |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 6116 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
| 6117 | "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n" |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6118 | #endif |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6119 | "\n" |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6120 | "Network options:\n" |
pbrook | a41b2ff | 2006-02-05 04:14:41 +0000 | [diff] [blame] | 6121 | "-net nic[,vlan=n][,macaddr=addr][,model=type]\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6122 | " create a new Network Interface Card and connect it to VLAN 'n'\n" |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 6123 | #ifdef CONFIG_SLIRP |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 6124 | "-net user[,vlan=n][,hostname=host]\n" |
| 6125 | " connect the user mode network stack to VLAN 'n' and send\n" |
| 6126 | " hostname 'host' to DHCP clients\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6127 | #endif |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 6128 | #ifdef _WIN32 |
| 6129 | "-net tap[,vlan=n],ifname=name\n" |
| 6130 | " connect the host TAP network interface to VLAN 'n'\n" |
| 6131 | #else |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6132 | "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file]\n" |
| 6133 | " connect the host TAP network interface to VLAN 'n' and use\n" |
| 6134 | " the network script 'file' (default=%s);\n" |
| 6135 | " use 'fd=h' to connect to an already opened TAP interface\n" |
bellard | 7fb843f | 2006-02-01 23:06:55 +0000 | [diff] [blame] | 6136 | #endif |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6137 | "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6138 | " connect the vlan 'n' to another VLAN using a socket connection\n" |
bellard | 3d83045 | 2005-12-18 16:36:49 +0000 | [diff] [blame] | 6139 | "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n" |
| 6140 | " connect the vlan 'n' to multicast maddr and port\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6141 | "-net none use it alone to have zero network devices; if no -net option\n" |
| 6142 | " is provided, the default is '-net nic -net user'\n" |
| 6143 | "\n" |
| 6144 | #ifdef CONFIG_SLIRP |
| 6145 | "-tftp prefix allow tftp access to files starting with prefix [-net user]\n" |
| 6146 | #ifndef _WIN32 |
| 6147 | "-smb dir allow SMB access to files in 'dir' [-net user]\n" |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 6148 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 6149 | "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n" |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6150 | " redirect TCP or UDP connections from host to guest [-net user]\n" |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 6151 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6152 | "\n" |
| 6153 | "Linux boot specific:\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6154 | "-kernel bzImage use 'bzImage' as kernel image\n" |
| 6155 | "-append cmdline use 'cmdline' as kernel command line\n" |
| 6156 | "-initrd file use 'file' as initial ram disk\n" |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6157 | "\n" |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 6158 | "Debug/Expert options:\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6159 | "-monitor dev redirect the monitor to char device 'dev'\n" |
| 6160 | "-serial dev redirect the serial port to char device 'dev'\n" |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6161 | "-parallel dev redirect the parallel port to char device 'dev'\n" |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 6162 | "-pidfile file Write PID to 'file'\n" |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6163 | "-S freeze CPU at startup (use 'c' to start execution)\n" |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6164 | "-s wait gdb connection to port %d\n" |
| 6165 | "-p port change gdb connection port\n" |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 6166 | "-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] | 6167 | "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n" |
| 6168 | " translation (t=none or lba) (usually qemu can guess them)\n" |
bellard | 87b4735 | 2006-08-17 17:22:54 +0000 | [diff] [blame] | 6169 | "-L path set the directory for the BIOS, VGA BIOS and keymaps\n" |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6170 | #ifdef USE_KQEMU |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6171 | "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n" |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6172 | "-no-kqemu disable KQEMU kernel module usage\n" |
| 6173 | #endif |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 6174 | #ifdef USE_CODE_COPY |
| 6175 | "-no-code-copy disable code copy acceleration\n" |
| 6176 | #endif |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6177 | #ifdef TARGET_I386 |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 6178 | "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n" |
| 6179 | " (default is CL-GD5446 PCI VGA)\n" |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6180 | "-no-acpi disable ACPI\n" |
bellard | bb0c672 | 2004-06-20 12:37:32 +0000 | [diff] [blame] | 6181 | #endif |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6182 | "-no-reboot exit instead of rebooting\n" |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6183 | "-loadvm file start right away with a saved state (loadvm in monitor)\n" |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 6184 | "-vnc display start a VNC server on display\n" |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6185 | #ifndef _WIN32 |
| 6186 | "-daemonize daemonize QEMU after initializing\n" |
| 6187 | #endif |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 6188 | "-option-rom rom load a file, rom, into the option ROM space\n" |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6189 | "\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6190 | "During emulation, the following keys are useful:\n" |
bellard | 032a8c9 | 2004-10-09 22:56:44 +0000 | [diff] [blame] | 6191 | "ctrl-alt-f toggle full screen\n" |
| 6192 | "ctrl-alt-n switch to virtual console 'n'\n" |
| 6193 | "ctrl-alt toggle mouse and keyboard grab\n" |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6194 | "\n" |
| 6195 | "When using -nographic, press 'ctrl-a h' to get some help.\n" |
| 6196 | , |
bellard | 0db6347 | 2003-10-27 21:37:46 +0000 | [diff] [blame] | 6197 | "qemu", |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6198 | DEFAULT_RAM_SIZE, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6199 | #ifndef _WIN32 |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6200 | DEFAULT_NETWORK_SCRIPT, |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6201 | #endif |
bellard | 6e44ba7 | 2004-01-18 21:56:49 +0000 | [diff] [blame] | 6202 | DEFAULT_GDBSTUB_PORT, |
| 6203 | "/tmp/qemu.log"); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6204 | exit(1); |
| 6205 | } |
| 6206 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6207 | #define HAS_ARG 0x0001 |
| 6208 | |
| 6209 | enum { |
| 6210 | QEMU_OPTION_h, |
| 6211 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6212 | QEMU_OPTION_M, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6213 | QEMU_OPTION_fda, |
| 6214 | QEMU_OPTION_fdb, |
| 6215 | QEMU_OPTION_hda, |
| 6216 | QEMU_OPTION_hdb, |
| 6217 | QEMU_OPTION_hdc, |
| 6218 | QEMU_OPTION_hdd, |
| 6219 | QEMU_OPTION_cdrom, |
| 6220 | QEMU_OPTION_boot, |
| 6221 | QEMU_OPTION_snapshot, |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 6222 | #ifdef TARGET_I386 |
| 6223 | QEMU_OPTION_no_fd_bootchk, |
| 6224 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6225 | QEMU_OPTION_m, |
| 6226 | QEMU_OPTION_nographic, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6227 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6228 | QEMU_OPTION_audio_help, |
| 6229 | QEMU_OPTION_soundhw, |
| 6230 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6231 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6232 | QEMU_OPTION_net, |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 6233 | QEMU_OPTION_tftp, |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 6234 | QEMU_OPTION_smb, |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 6235 | QEMU_OPTION_redir, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6236 | |
| 6237 | QEMU_OPTION_kernel, |
| 6238 | QEMU_OPTION_append, |
| 6239 | QEMU_OPTION_initrd, |
| 6240 | |
| 6241 | QEMU_OPTION_S, |
| 6242 | QEMU_OPTION_s, |
| 6243 | QEMU_OPTION_p, |
| 6244 | QEMU_OPTION_d, |
| 6245 | QEMU_OPTION_hdachs, |
| 6246 | QEMU_OPTION_L, |
| 6247 | QEMU_OPTION_no_code_copy, |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 6248 | QEMU_OPTION_k, |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 6249 | QEMU_OPTION_localtime, |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 6250 | QEMU_OPTION_cirrusvga, |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 6251 | QEMU_OPTION_g, |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 6252 | QEMU_OPTION_std_vga, |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6253 | QEMU_OPTION_monitor, |
| 6254 | QEMU_OPTION_serial, |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6255 | QEMU_OPTION_parallel, |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6256 | QEMU_OPTION_loadvm, |
| 6257 | QEMU_OPTION_full_screen, |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6258 | QEMU_OPTION_no_quit, |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 6259 | QEMU_OPTION_pidfile, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6260 | QEMU_OPTION_no_kqemu, |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6261 | QEMU_OPTION_kernel_kqemu, |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 6262 | QEMU_OPTION_win2k_hack, |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 6263 | QEMU_OPTION_usb, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6264 | QEMU_OPTION_usbdevice, |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6265 | QEMU_OPTION_smp, |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 6266 | QEMU_OPTION_vnc, |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6267 | QEMU_OPTION_no_acpi, |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6268 | QEMU_OPTION_no_reboot, |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6269 | QEMU_OPTION_daemonize, |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 6270 | QEMU_OPTION_option_rom, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6271 | }; |
| 6272 | |
| 6273 | typedef struct QEMUOption { |
| 6274 | const char *name; |
| 6275 | int flags; |
| 6276 | int index; |
| 6277 | } QEMUOption; |
| 6278 | |
| 6279 | const QEMUOption qemu_options[] = { |
| 6280 | { "h", 0, QEMU_OPTION_h }, |
| 6281 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6282 | { "M", HAS_ARG, QEMU_OPTION_M }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6283 | { "fda", HAS_ARG, QEMU_OPTION_fda }, |
| 6284 | { "fdb", HAS_ARG, QEMU_OPTION_fdb }, |
| 6285 | { "hda", HAS_ARG, QEMU_OPTION_hda }, |
| 6286 | { "hdb", HAS_ARG, QEMU_OPTION_hdb }, |
| 6287 | { "hdc", HAS_ARG, QEMU_OPTION_hdc }, |
| 6288 | { "hdd", HAS_ARG, QEMU_OPTION_hdd }, |
| 6289 | { "cdrom", HAS_ARG, QEMU_OPTION_cdrom }, |
| 6290 | { "boot", HAS_ARG, QEMU_OPTION_boot }, |
| 6291 | { "snapshot", 0, QEMU_OPTION_snapshot }, |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 6292 | #ifdef TARGET_I386 |
| 6293 | { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk }, |
| 6294 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6295 | { "m", HAS_ARG, QEMU_OPTION_m }, |
| 6296 | { "nographic", 0, QEMU_OPTION_nographic }, |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 6297 | { "k", HAS_ARG, QEMU_OPTION_k }, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6298 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6299 | { "audio-help", 0, QEMU_OPTION_audio_help }, |
| 6300 | { "soundhw", HAS_ARG, QEMU_OPTION_soundhw }, |
| 6301 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6302 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6303 | { "net", HAS_ARG, QEMU_OPTION_net}, |
bellard | 158156d | 2004-05-17 21:13:42 +0000 | [diff] [blame] | 6304 | #ifdef CONFIG_SLIRP |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 6305 | { "tftp", HAS_ARG, QEMU_OPTION_tftp }, |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 6306 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 6307 | { "smb", HAS_ARG, QEMU_OPTION_smb }, |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 6308 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 6309 | { "redir", HAS_ARG, QEMU_OPTION_redir }, |
bellard | 158156d | 2004-05-17 21:13:42 +0000 | [diff] [blame] | 6310 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6311 | |
| 6312 | { "kernel", HAS_ARG, QEMU_OPTION_kernel }, |
| 6313 | { "append", HAS_ARG, QEMU_OPTION_append }, |
| 6314 | { "initrd", HAS_ARG, QEMU_OPTION_initrd }, |
| 6315 | |
| 6316 | { "S", 0, QEMU_OPTION_S }, |
| 6317 | { "s", 0, QEMU_OPTION_s }, |
| 6318 | { "p", HAS_ARG, QEMU_OPTION_p }, |
| 6319 | { "d", HAS_ARG, QEMU_OPTION_d }, |
| 6320 | { "hdachs", HAS_ARG, QEMU_OPTION_hdachs }, |
| 6321 | { "L", HAS_ARG, QEMU_OPTION_L }, |
| 6322 | { "no-code-copy", 0, QEMU_OPTION_no_code_copy }, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6323 | #ifdef USE_KQEMU |
| 6324 | { "no-kqemu", 0, QEMU_OPTION_no_kqemu }, |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6325 | { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu }, |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6326 | #endif |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 6327 | #if defined(TARGET_PPC) || defined(TARGET_SPARC) |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 6328 | { "g", 1, QEMU_OPTION_g }, |
bellard | 77d4bc3 | 2004-05-26 22:13:53 +0000 | [diff] [blame] | 6329 | #endif |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 6330 | { "localtime", 0, QEMU_OPTION_localtime }, |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 6331 | { "std-vga", 0, QEMU_OPTION_std_vga }, |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6332 | { "monitor", 1, QEMU_OPTION_monitor }, |
| 6333 | { "serial", 1, QEMU_OPTION_serial }, |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6334 | { "parallel", 1, QEMU_OPTION_parallel }, |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6335 | { "loadvm", HAS_ARG, QEMU_OPTION_loadvm }, |
| 6336 | { "full-screen", 0, QEMU_OPTION_full_screen }, |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6337 | #ifdef CONFIG_SDL |
| 6338 | { "no-quit", 0, QEMU_OPTION_no_quit }, |
| 6339 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 6340 | { "pidfile", HAS_ARG, QEMU_OPTION_pidfile }, |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 6341 | { "win2k-hack", 0, QEMU_OPTION_win2k_hack }, |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6342 | { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice }, |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6343 | { "smp", HAS_ARG, QEMU_OPTION_smp }, |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 6344 | { "vnc", HAS_ARG, QEMU_OPTION_vnc }, |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame^] | 6345 | |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 6346 | /* temporary options */ |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6347 | { "usb", 0, QEMU_OPTION_usb }, |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 6348 | { "cirrusvga", 0, QEMU_OPTION_cirrusvga }, |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6349 | { "no-acpi", 0, QEMU_OPTION_no_acpi }, |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6350 | { "no-reboot", 0, QEMU_OPTION_no_reboot }, |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6351 | { "daemonize", 0, QEMU_OPTION_daemonize }, |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 6352 | { "option-rom", HAS_ARG, QEMU_OPTION_option_rom }, |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6353 | { NULL }, |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6354 | }; |
| 6355 | |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 6356 | #if defined (TARGET_I386) && defined(USE_CODE_COPY) |
| 6357 | |
| 6358 | /* this stack is only used during signal handling */ |
| 6359 | #define SIGNAL_STACK_SIZE 32768 |
| 6360 | |
| 6361 | static uint8_t *signal_stack; |
| 6362 | |
| 6363 | #endif |
| 6364 | |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 6365 | /* password input */ |
| 6366 | |
| 6367 | static BlockDriverState *get_bdrv(int index) |
| 6368 | { |
| 6369 | BlockDriverState *bs; |
| 6370 | |
| 6371 | if (index < 4) { |
| 6372 | bs = bs_table[index]; |
| 6373 | } else if (index < 6) { |
| 6374 | bs = fd_table[index - 4]; |
| 6375 | } else { |
| 6376 | bs = NULL; |
| 6377 | } |
| 6378 | return bs; |
| 6379 | } |
| 6380 | |
| 6381 | static void read_passwords(void) |
| 6382 | { |
| 6383 | BlockDriverState *bs; |
| 6384 | int i, j; |
| 6385 | char password[256]; |
| 6386 | |
| 6387 | for(i = 0; i < 6; i++) { |
| 6388 | bs = get_bdrv(i); |
| 6389 | if (bs && bdrv_is_encrypted(bs)) { |
| 6390 | term_printf("%s is encrypted.\n", bdrv_get_device_name(bs)); |
| 6391 | for(j = 0; j < 3; j++) { |
| 6392 | monitor_readline("Password: ", |
| 6393 | 1, password, sizeof(password)); |
| 6394 | if (bdrv_set_key(bs, password) == 0) |
| 6395 | break; |
| 6396 | term_printf("invalid password\n"); |
| 6397 | } |
| 6398 | } |
| 6399 | } |
| 6400 | } |
| 6401 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6402 | /* XXX: currently we cannot use simultaneously different CPUs */ |
| 6403 | void register_machines(void) |
| 6404 | { |
| 6405 | #if defined(TARGET_I386) |
| 6406 | qemu_register_machine(&pc_machine); |
bellard | 3dbbdc2 | 2005-11-06 18:20:37 +0000 | [diff] [blame] | 6407 | qemu_register_machine(&isapc_machine); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6408 | #elif defined(TARGET_PPC) |
| 6409 | qemu_register_machine(&heathrow_machine); |
| 6410 | qemu_register_machine(&core99_machine); |
| 6411 | qemu_register_machine(&prep_machine); |
bellard | 6af0bf9 | 2005-07-02 14:58:51 +0000 | [diff] [blame] | 6412 | #elif defined(TARGET_MIPS) |
| 6413 | qemu_register_machine(&mips_machine); |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6414 | #elif defined(TARGET_SPARC) |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6415 | #ifdef TARGET_SPARC64 |
| 6416 | qemu_register_machine(&sun4u_machine); |
| 6417 | #else |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6418 | qemu_register_machine(&sun4m_machine); |
| 6419 | #endif |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 6420 | #elif defined(TARGET_ARM) |
pbrook | 40f137e | 2006-02-20 00:33:36 +0000 | [diff] [blame] | 6421 | qemu_register_machine(&integratorcp926_machine); |
| 6422 | qemu_register_machine(&integratorcp1026_machine); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 6423 | qemu_register_machine(&versatilepb_machine); |
pbrook | 1640695 | 2006-04-27 23:15:07 +0000 | [diff] [blame] | 6424 | qemu_register_machine(&versatileab_machine); |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 6425 | qemu_register_machine(&realview_machine); |
bellard | 27c7ca7 | 2006-04-27 21:32:09 +0000 | [diff] [blame] | 6426 | #elif defined(TARGET_SH4) |
| 6427 | qemu_register_machine(&shix_machine); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 6428 | #else |
| 6429 | #error unsupported CPU |
bellard | 3475187 | 2005-07-02 14:31:34 +0000 | [diff] [blame] | 6430 | #endif |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6431 | } |
| 6432 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6433 | #ifdef HAS_AUDIO |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6434 | struct soundhw soundhw[] = { |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 6435 | #ifdef TARGET_I386 |
| 6436 | { |
| 6437 | "pcspk", |
| 6438 | "PC speaker", |
| 6439 | 0, |
| 6440 | 1, |
| 6441 | { .init_isa = pcspk_audio_init } |
| 6442 | }, |
| 6443 | #endif |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6444 | { |
| 6445 | "sb16", |
| 6446 | "Creative Sound Blaster 16", |
| 6447 | 0, |
| 6448 | 1, |
| 6449 | { .init_isa = SB16_init } |
| 6450 | }, |
| 6451 | |
| 6452 | #ifdef CONFIG_ADLIB |
| 6453 | { |
| 6454 | "adlib", |
| 6455 | #ifdef HAS_YMF262 |
| 6456 | "Yamaha YMF262 (OPL3)", |
| 6457 | #else |
| 6458 | "Yamaha YM3812 (OPL2)", |
| 6459 | #endif |
| 6460 | 0, |
| 6461 | 1, |
| 6462 | { .init_isa = Adlib_init } |
| 6463 | }, |
| 6464 | #endif |
| 6465 | |
| 6466 | #ifdef CONFIG_GUS |
| 6467 | { |
| 6468 | "gus", |
| 6469 | "Gravis Ultrasound GF1", |
| 6470 | 0, |
| 6471 | 1, |
| 6472 | { .init_isa = GUS_init } |
| 6473 | }, |
| 6474 | #endif |
| 6475 | |
| 6476 | { |
| 6477 | "es1370", |
| 6478 | "ENSONIQ AudioPCI ES1370", |
| 6479 | 0, |
| 6480 | 0, |
| 6481 | { .init_pci = es1370_init } |
| 6482 | }, |
| 6483 | |
| 6484 | { NULL, NULL, 0, 0, { NULL } } |
| 6485 | }; |
| 6486 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6487 | static void select_soundhw (const char *optarg) |
| 6488 | { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6489 | struct soundhw *c; |
| 6490 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6491 | if (*optarg == '?') { |
| 6492 | show_valid_cards: |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6493 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6494 | printf ("Valid sound card names (comma separated):\n"); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6495 | for (c = soundhw; c->name; ++c) { |
| 6496 | printf ("%-11s %s\n", c->name, c->descr); |
| 6497 | } |
| 6498 | printf ("\n-soundhw all will enable all of the above\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6499 | exit (*optarg != '?'); |
| 6500 | } |
| 6501 | else { |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6502 | size_t l; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6503 | const char *p; |
| 6504 | char *e; |
| 6505 | int bad_card = 0; |
| 6506 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6507 | if (!strcmp (optarg, "all")) { |
| 6508 | for (c = soundhw; c->name; ++c) { |
| 6509 | c->enabled = 1; |
| 6510 | } |
| 6511 | return; |
| 6512 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6513 | |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6514 | p = optarg; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6515 | while (*p) { |
| 6516 | e = strchr (p, ','); |
| 6517 | l = !e ? strlen (p) : (size_t) (e - p); |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6518 | |
| 6519 | for (c = soundhw; c->name; ++c) { |
| 6520 | if (!strncmp (c->name, p, l)) { |
| 6521 | c->enabled = 1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6522 | break; |
| 6523 | } |
| 6524 | } |
bellard | 6a36d84 | 2005-12-18 20:34:32 +0000 | [diff] [blame] | 6525 | |
| 6526 | if (!c->name) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6527 | if (l > 80) { |
| 6528 | fprintf (stderr, |
| 6529 | "Unknown sound card name (too big to show)\n"); |
| 6530 | } |
| 6531 | else { |
| 6532 | fprintf (stderr, "Unknown sound card name `%.*s'\n", |
| 6533 | (int) l, p); |
| 6534 | } |
| 6535 | bad_card = 1; |
| 6536 | } |
| 6537 | p += l + (e != NULL); |
| 6538 | } |
| 6539 | |
| 6540 | if (bad_card) |
| 6541 | goto show_valid_cards; |
| 6542 | } |
| 6543 | } |
| 6544 | #endif |
| 6545 | |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 6546 | #ifdef _WIN32 |
| 6547 | static BOOL WINAPI qemu_ctrl_handler(DWORD type) |
| 6548 | { |
| 6549 | exit(STATUS_CONTROL_C_EXIT); |
| 6550 | return TRUE; |
| 6551 | } |
| 6552 | #endif |
| 6553 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6554 | #define MAX_NET_CLIENTS 32 |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 6555 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6556 | int main(int argc, char **argv) |
| 6557 | { |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6558 | #ifdef CONFIG_GDBSTUB |
| 6559 | int use_gdbstub, gdbstub_port; |
| 6560 | #endif |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6561 | int i, cdrom_index; |
bellard | 1ccde1c | 2004-02-06 19:46:14 +0000 | [diff] [blame] | 6562 | int snapshot, linux_boot; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 6563 | const char *initrd_filename; |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame^] | 6564 | const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD]; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6565 | const char *kernel_filename, *kernel_cmdline; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 6566 | DisplayState *ds = &display_state; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 6567 | int cyls, heads, secs, translation; |
bellard | a541f29 | 2004-04-12 20:39:29 +0000 | [diff] [blame] | 6568 | int start_emulation = 1; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6569 | char net_clients[MAX_NET_CLIENTS][256]; |
| 6570 | int nb_net_clients; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6571 | int optind; |
| 6572 | const char *r, *optarg; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6573 | CharDriverState *monitor_hd; |
| 6574 | char monitor_device[128]; |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 6575 | char serial_devices[MAX_SERIAL_PORTS][128]; |
| 6576 | int serial_device_index; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6577 | char parallel_devices[MAX_PARALLEL_PORTS][128]; |
| 6578 | int parallel_device_index; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6579 | const char *loadvm = NULL; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6580 | QEMUMachine *machine; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 6581 | char usb_devices[MAX_USB_CMDLINE][128]; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6582 | int usb_devices_index; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 6583 | int fds[2]; |
bellard | 0bd4885 | 2005-11-11 00:00:47 +0000 | [diff] [blame] | 6584 | |
| 6585 | LIST_INIT (&vm_change_state_head); |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 6586 | #ifndef _WIN32 |
| 6587 | { |
| 6588 | struct sigaction act; |
| 6589 | sigfillset(&act.sa_mask); |
| 6590 | act.sa_flags = 0; |
| 6591 | act.sa_handler = SIG_IGN; |
| 6592 | sigaction(SIGPIPE, &act, NULL); |
| 6593 | } |
bellard | 3587d7e | 2006-06-26 20:03:44 +0000 | [diff] [blame] | 6594 | #else |
| 6595 | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
bellard | a8e5ac3 | 2006-07-14 09:36:13 +0000 | [diff] [blame] | 6596 | /* Note: cpu_interrupt() is currently not SMP safe, so we force |
| 6597 | QEMU to run on a single CPU */ |
| 6598 | { |
| 6599 | HANDLE h; |
| 6600 | DWORD mask, smask; |
| 6601 | int i; |
| 6602 | h = GetCurrentProcess(); |
| 6603 | if (GetProcessAffinityMask(h, &mask, &smask)) { |
| 6604 | for(i = 0; i < 32; i++) { |
| 6605 | if (mask & (1 << i)) |
| 6606 | break; |
| 6607 | } |
| 6608 | if (i != 32) { |
| 6609 | mask = 1 << i; |
| 6610 | SetProcessAffinityMask(h, mask); |
| 6611 | } |
| 6612 | } |
| 6613 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6614 | #endif |
bellard | be995c2 | 2006-06-25 16:25:21 +0000 | [diff] [blame] | 6615 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6616 | register_machines(); |
| 6617 | machine = first_machine; |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6618 | initrd_filename = NULL; |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 6619 | for(i = 0; i < MAX_FD; i++) |
| 6620 | fd_filename[i] = NULL; |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame^] | 6621 | for(i = 0; i < MAX_DISKS; i++) |
| 6622 | hd_filename[i] = NULL; |
bellard | a00bad7 | 2004-05-22 21:39:06 +0000 | [diff] [blame] | 6623 | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 6624 | vga_ram_size = VGA_RAM_SIZE; |
bellard | 0ced658 | 2004-05-23 21:06:12 +0000 | [diff] [blame] | 6625 | bios_size = BIOS_SIZE; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6626 | #ifdef CONFIG_GDBSTUB |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 6627 | use_gdbstub = 0; |
| 6628 | gdbstub_port = DEFAULT_GDBSTUB_PORT; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6629 | #endif |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 6630 | snapshot = 0; |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6631 | nographic = 0; |
| 6632 | kernel_filename = NULL; |
| 6633 | kernel_cmdline = ""; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6634 | #ifdef TARGET_PPC |
| 6635 | cdrom_index = 1; |
| 6636 | #else |
| 6637 | cdrom_index = 2; |
| 6638 | #endif |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6639 | cyls = heads = secs = 0; |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 6640 | translation = BIOS_ATA_TRANSLATION_AUTO; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6641 | pstrcpy(monitor_device, sizeof(monitor_device), "vc"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6642 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 6643 | pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc"); |
| 6644 | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
| 6645 | serial_devices[i][0] = '\0'; |
| 6646 | serial_device_index = 0; |
| 6647 | |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6648 | pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc"); |
| 6649 | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
| 6650 | parallel_devices[i][0] = '\0'; |
| 6651 | parallel_device_index = 0; |
| 6652 | |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6653 | usb_devices_index = 0; |
| 6654 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6655 | nb_net_clients = 0; |
| 6656 | |
| 6657 | nb_nics = 0; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 6658 | /* default mac address of the first network interface */ |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6659 | |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6660 | optind = 1; |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6661 | for(;;) { |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6662 | if (optind >= argc) |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6663 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6664 | r = argv[optind]; |
| 6665 | if (r[0] != '-') { |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame^] | 6666 | hd_filename[0] = argv[optind++]; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6667 | } else { |
| 6668 | const QEMUOption *popt; |
| 6669 | |
| 6670 | optind++; |
| 6671 | popt = qemu_options; |
| 6672 | for(;;) { |
| 6673 | if (!popt->name) { |
| 6674 | fprintf(stderr, "%s: invalid option -- '%s'\n", |
| 6675 | argv[0], r); |
| 6676 | exit(1); |
| 6677 | } |
| 6678 | if (!strcmp(popt->name, r + 1)) |
| 6679 | break; |
| 6680 | popt++; |
| 6681 | } |
| 6682 | if (popt->flags & HAS_ARG) { |
| 6683 | if (optind >= argc) { |
| 6684 | fprintf(stderr, "%s: option '%s' requires an argument\n", |
| 6685 | argv[0], r); |
| 6686 | exit(1); |
| 6687 | } |
| 6688 | optarg = argv[optind++]; |
| 6689 | } else { |
| 6690 | optarg = NULL; |
| 6691 | } |
| 6692 | |
| 6693 | switch(popt->index) { |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6694 | case QEMU_OPTION_M: |
| 6695 | machine = find_machine(optarg); |
| 6696 | if (!machine) { |
| 6697 | QEMUMachine *m; |
| 6698 | printf("Supported machines are:\n"); |
| 6699 | for(m = first_machine; m != NULL; m = m->next) { |
| 6700 | printf("%-10s %s%s\n", |
| 6701 | m->name, m->desc, |
| 6702 | m == first_machine ? " (default)" : ""); |
| 6703 | } |
| 6704 | exit(1); |
| 6705 | } |
| 6706 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6707 | case QEMU_OPTION_initrd: |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6708 | initrd_filename = optarg; |
| 6709 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6710 | case QEMU_OPTION_hda: |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6711 | case QEMU_OPTION_hdb: |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6712 | case QEMU_OPTION_hdc: |
| 6713 | case QEMU_OPTION_hdd: |
| 6714 | { |
| 6715 | int hd_index; |
| 6716 | hd_index = popt->index - QEMU_OPTION_hda; |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame^] | 6717 | hd_filename[hd_index] = optarg; |
| 6718 | if (hd_index == cdrom_index) |
| 6719 | cdrom_index = -1; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6720 | } |
bellard | fc01f7e | 2003-06-30 10:03:06 +0000 | [diff] [blame] | 6721 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6722 | case QEMU_OPTION_snapshot: |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 6723 | snapshot = 1; |
| 6724 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6725 | case QEMU_OPTION_hdachs: |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 6726 | { |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 6727 | const char *p; |
| 6728 | p = optarg; |
| 6729 | cyls = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 6730 | if (cyls < 1 || cyls > 16383) |
| 6731 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 6732 | if (*p != ',') |
| 6733 | goto chs_fail; |
| 6734 | p++; |
| 6735 | heads = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 6736 | if (heads < 1 || heads > 16) |
| 6737 | goto chs_fail; |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 6738 | if (*p != ',') |
| 6739 | goto chs_fail; |
| 6740 | p++; |
| 6741 | secs = strtol(p, (char **)&p, 0); |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 6742 | if (secs < 1 || secs > 63) |
| 6743 | goto chs_fail; |
| 6744 | if (*p == ',') { |
| 6745 | p++; |
| 6746 | if (!strcmp(p, "none")) |
| 6747 | translation = BIOS_ATA_TRANSLATION_NONE; |
| 6748 | else if (!strcmp(p, "lba")) |
| 6749 | translation = BIOS_ATA_TRANSLATION_LBA; |
| 6750 | else if (!strcmp(p, "auto")) |
| 6751 | translation = BIOS_ATA_TRANSLATION_AUTO; |
| 6752 | else |
| 6753 | goto chs_fail; |
| 6754 | } else if (*p != '\0') { |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6755 | chs_fail: |
bellard | 46d4767 | 2004-11-16 01:45:27 +0000 | [diff] [blame] | 6756 | fprintf(stderr, "qemu: invalid physical CHS format\n"); |
| 6757 | exit(1); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6758 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 6759 | } |
| 6760 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6761 | case QEMU_OPTION_nographic: |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6762 | pstrcpy(monitor_device, sizeof(monitor_device), "stdio"); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 6763 | pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio"); |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6764 | nographic = 1; |
| 6765 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6766 | case QEMU_OPTION_kernel: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6767 | kernel_filename = optarg; |
| 6768 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6769 | case QEMU_OPTION_append: |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 6770 | kernel_cmdline = optarg; |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 6771 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6772 | case QEMU_OPTION_cdrom: |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame^] | 6773 | if (cdrom_index >= 0) { |
| 6774 | hd_filename[cdrom_index] = optarg; |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 6775 | } |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 6776 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6777 | case QEMU_OPTION_boot: |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 6778 | boot_device = optarg[0]; |
bellard | 9e89a4b | 2004-11-14 15:42:27 +0000 | [diff] [blame] | 6779 | if (boot_device != 'a' && |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 6780 | #if defined(TARGET_SPARC) || defined(TARGET_I386) |
bellard | 6f7e9ae | 2005-03-13 09:43:36 +0000 | [diff] [blame] | 6781 | // Network boot |
| 6782 | boot_device != 'n' && |
| 6783 | #endif |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 6784 | boot_device != 'c' && boot_device != 'd') { |
bellard | 36b486b | 2003-11-11 13:36:08 +0000 | [diff] [blame] | 6785 | fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device); |
| 6786 | exit(1); |
| 6787 | } |
| 6788 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6789 | case QEMU_OPTION_fda: |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 6790 | fd_filename[0] = optarg; |
| 6791 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6792 | case QEMU_OPTION_fdb: |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 6793 | fd_filename[1] = optarg; |
| 6794 | break; |
bellard | 52ca8d6 | 2006-06-14 16:03:05 +0000 | [diff] [blame] | 6795 | #ifdef TARGET_I386 |
| 6796 | case QEMU_OPTION_no_fd_bootchk: |
| 6797 | fd_bootchk = 0; |
| 6798 | break; |
| 6799 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6800 | case QEMU_OPTION_no_code_copy: |
bellard | 77fef8c | 2004-02-16 22:05:46 +0000 | [diff] [blame] | 6801 | code_copy_enabled = 0; |
| 6802 | break; |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6803 | case QEMU_OPTION_net: |
| 6804 | if (nb_net_clients >= MAX_NET_CLIENTS) { |
| 6805 | fprintf(stderr, "qemu: too many network clients\n"); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 6806 | exit(1); |
| 6807 | } |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 6808 | pstrcpy(net_clients[nb_net_clients], |
| 6809 | sizeof(net_clients[0]), |
| 6810 | optarg); |
| 6811 | nb_net_clients++; |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 6812 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 6813 | #ifdef CONFIG_SLIRP |
| 6814 | case QEMU_OPTION_tftp: |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 6815 | tftp_prefix = optarg; |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 6816 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 6817 | #ifndef _WIN32 |
bellard | 9d728e8 | 2004-09-05 23:09:03 +0000 | [diff] [blame] | 6818 | case QEMU_OPTION_smb: |
| 6819 | net_slirp_smb(optarg); |
| 6820 | break; |
bellard | c94c8d6 | 2004-09-13 21:37:34 +0000 | [diff] [blame] | 6821 | #endif |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 6822 | case QEMU_OPTION_redir: |
| 6823 | net_slirp_redir(optarg); |
| 6824 | break; |
bellard | c7f7464 | 2004-08-24 21:57:12 +0000 | [diff] [blame] | 6825 | #endif |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6826 | #ifdef HAS_AUDIO |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 6827 | case QEMU_OPTION_audio_help: |
| 6828 | AUD_help (); |
| 6829 | exit (0); |
| 6830 | break; |
| 6831 | case QEMU_OPTION_soundhw: |
| 6832 | select_soundhw (optarg); |
| 6833 | break; |
| 6834 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6835 | case QEMU_OPTION_h: |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 6836 | help(); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6837 | break; |
| 6838 | case QEMU_OPTION_m: |
| 6839 | ram_size = atoi(optarg) * 1024 * 1024; |
| 6840 | if (ram_size <= 0) |
| 6841 | help(); |
| 6842 | if (ram_size > PHYS_RAM_MAX_SIZE) { |
| 6843 | fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n", |
| 6844 | PHYS_RAM_MAX_SIZE / (1024 * 1024)); |
| 6845 | exit(1); |
| 6846 | } |
| 6847 | break; |
| 6848 | case QEMU_OPTION_d: |
| 6849 | { |
| 6850 | int mask; |
| 6851 | CPULogItem *item; |
| 6852 | |
| 6853 | mask = cpu_str_to_log_mask(optarg); |
| 6854 | if (!mask) { |
| 6855 | printf("Log items (comma separated):\n"); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 6856 | for(item = cpu_log_items; item->mask != 0; item++) { |
| 6857 | printf("%-10s %s\n", item->name, item->help); |
| 6858 | } |
| 6859 | exit(1); |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6860 | } |
| 6861 | cpu_set_log(mask); |
bellard | f193c79 | 2004-03-21 17:06:25 +0000 | [diff] [blame] | 6862 | } |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6863 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6864 | #ifdef CONFIG_GDBSTUB |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6865 | case QEMU_OPTION_s: |
| 6866 | use_gdbstub = 1; |
| 6867 | break; |
| 6868 | case QEMU_OPTION_p: |
| 6869 | gdbstub_port = atoi(optarg); |
| 6870 | break; |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 6871 | #endif |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 6872 | case QEMU_OPTION_L: |
| 6873 | bios_dir = optarg; |
| 6874 | break; |
| 6875 | case QEMU_OPTION_S: |
| 6876 | start_emulation = 0; |
| 6877 | break; |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 6878 | case QEMU_OPTION_k: |
| 6879 | keyboard_layout = optarg; |
| 6880 | break; |
bellard | ee22c2f | 2004-06-03 12:49:50 +0000 | [diff] [blame] | 6881 | case QEMU_OPTION_localtime: |
| 6882 | rtc_utc = 0; |
| 6883 | break; |
bellard | 1f04275 | 2004-06-05 13:46:47 +0000 | [diff] [blame] | 6884 | case QEMU_OPTION_cirrusvga: |
| 6885 | cirrus_vga_enabled = 1; |
| 6886 | break; |
bellard | 1bfe856 | 2004-07-08 21:17:50 +0000 | [diff] [blame] | 6887 | case QEMU_OPTION_std_vga: |
| 6888 | cirrus_vga_enabled = 0; |
| 6889 | break; |
bellard | e9b137c | 2004-06-21 16:46:10 +0000 | [diff] [blame] | 6890 | case QEMU_OPTION_g: |
| 6891 | { |
| 6892 | const char *p; |
| 6893 | int w, h, depth; |
| 6894 | p = optarg; |
| 6895 | w = strtol(p, (char **)&p, 10); |
| 6896 | if (w <= 0) { |
| 6897 | graphic_error: |
| 6898 | fprintf(stderr, "qemu: invalid resolution or depth\n"); |
| 6899 | exit(1); |
| 6900 | } |
| 6901 | if (*p != 'x') |
| 6902 | goto graphic_error; |
| 6903 | p++; |
| 6904 | h = strtol(p, (char **)&p, 10); |
| 6905 | if (h <= 0) |
| 6906 | goto graphic_error; |
| 6907 | if (*p == 'x') { |
| 6908 | p++; |
| 6909 | depth = strtol(p, (char **)&p, 10); |
| 6910 | if (depth != 8 && depth != 15 && depth != 16 && |
| 6911 | depth != 24 && depth != 32) |
| 6912 | goto graphic_error; |
| 6913 | } else if (*p == '\0') { |
| 6914 | depth = graphic_depth; |
| 6915 | } else { |
| 6916 | goto graphic_error; |
| 6917 | } |
| 6918 | |
| 6919 | graphic_width = w; |
| 6920 | graphic_height = h; |
| 6921 | graphic_depth = depth; |
| 6922 | } |
| 6923 | break; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6924 | case QEMU_OPTION_monitor: |
| 6925 | pstrcpy(monitor_device, sizeof(monitor_device), optarg); |
| 6926 | break; |
| 6927 | case QEMU_OPTION_serial: |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 6928 | if (serial_device_index >= MAX_SERIAL_PORTS) { |
| 6929 | fprintf(stderr, "qemu: too many serial ports\n"); |
| 6930 | exit(1); |
| 6931 | } |
| 6932 | pstrcpy(serial_devices[serial_device_index], |
| 6933 | sizeof(serial_devices[0]), optarg); |
| 6934 | serial_device_index++; |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 6935 | break; |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 6936 | case QEMU_OPTION_parallel: |
| 6937 | if (parallel_device_index >= MAX_PARALLEL_PORTS) { |
| 6938 | fprintf(stderr, "qemu: too many parallel ports\n"); |
| 6939 | exit(1); |
| 6940 | } |
| 6941 | pstrcpy(parallel_devices[parallel_device_index], |
| 6942 | sizeof(parallel_devices[0]), optarg); |
| 6943 | parallel_device_index++; |
| 6944 | break; |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 6945 | case QEMU_OPTION_loadvm: |
| 6946 | loadvm = optarg; |
| 6947 | break; |
| 6948 | case QEMU_OPTION_full_screen: |
| 6949 | full_screen = 1; |
| 6950 | break; |
ths | 667acca | 2006-12-11 02:08:05 +0000 | [diff] [blame] | 6951 | #ifdef CONFIG_SDL |
| 6952 | case QEMU_OPTION_no_quit: |
| 6953 | no_quit = 1; |
| 6954 | break; |
| 6955 | #endif |
bellard | f7cce89 | 2004-12-08 22:21:25 +0000 | [diff] [blame] | 6956 | case QEMU_OPTION_pidfile: |
| 6957 | create_pidfile(optarg); |
| 6958 | break; |
bellard | a09db21 | 2005-04-30 16:10:35 +0000 | [diff] [blame] | 6959 | #ifdef TARGET_I386 |
| 6960 | case QEMU_OPTION_win2k_hack: |
| 6961 | win2k_install_hack = 1; |
| 6962 | break; |
| 6963 | #endif |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6964 | #ifdef USE_KQEMU |
| 6965 | case QEMU_OPTION_no_kqemu: |
| 6966 | kqemu_allowed = 0; |
| 6967 | break; |
bellard | 89bfc10 | 2006-02-08 22:46:31 +0000 | [diff] [blame] | 6968 | case QEMU_OPTION_kernel_kqemu: |
| 6969 | kqemu_allowed = 2; |
| 6970 | break; |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 6971 | #endif |
bellard | bb36d47 | 2005-11-05 14:22:28 +0000 | [diff] [blame] | 6972 | case QEMU_OPTION_usb: |
| 6973 | usb_enabled = 1; |
| 6974 | break; |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6975 | case QEMU_OPTION_usbdevice: |
| 6976 | usb_enabled = 1; |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 6977 | if (usb_devices_index >= MAX_USB_CMDLINE) { |
bellard | a594cfb | 2005-11-06 16:13:29 +0000 | [diff] [blame] | 6978 | fprintf(stderr, "Too many USB devices\n"); |
| 6979 | exit(1); |
| 6980 | } |
| 6981 | pstrcpy(usb_devices[usb_devices_index], |
| 6982 | sizeof(usb_devices[usb_devices_index]), |
| 6983 | optarg); |
| 6984 | usb_devices_index++; |
| 6985 | break; |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6986 | case QEMU_OPTION_smp: |
| 6987 | smp_cpus = atoi(optarg); |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 6988 | if (smp_cpus < 1 || smp_cpus > MAX_CPUS) { |
bellard | 6a00d60 | 2005-11-21 23:25:50 +0000 | [diff] [blame] | 6989 | fprintf(stderr, "Invalid number of CPUs\n"); |
| 6990 | exit(1); |
| 6991 | } |
| 6992 | break; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 6993 | case QEMU_OPTION_vnc: |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 6994 | vnc_display = optarg; |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 6995 | break; |
bellard | 6515b20 | 2006-05-03 22:02:44 +0000 | [diff] [blame] | 6996 | case QEMU_OPTION_no_acpi: |
| 6997 | acpi_enabled = 0; |
| 6998 | break; |
bellard | d1beab8 | 2006-10-02 19:44:22 +0000 | [diff] [blame] | 6999 | case QEMU_OPTION_no_reboot: |
| 7000 | no_reboot = 1; |
| 7001 | break; |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7002 | case QEMU_OPTION_daemonize: |
| 7003 | daemonize = 1; |
| 7004 | break; |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 7005 | case QEMU_OPTION_option_rom: |
| 7006 | if (nb_option_roms >= MAX_OPTION_ROMS) { |
| 7007 | fprintf(stderr, "Too many option ROMs\n"); |
| 7008 | exit(1); |
| 7009 | } |
| 7010 | option_rom[nb_option_roms] = optarg; |
| 7011 | nb_option_roms++; |
| 7012 | break; |
bellard | cd6f116 | 2004-05-13 22:02:20 +0000 | [diff] [blame] | 7013 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7014 | } |
| 7015 | } |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7016 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7017 | #ifndef _WIN32 |
| 7018 | if (daemonize && !nographic && vnc_display == NULL) { |
| 7019 | fprintf(stderr, "Can only daemonize if using -nographic or -vnc\n"); |
| 7020 | daemonize = 0; |
| 7021 | } |
| 7022 | |
| 7023 | if (daemonize) { |
| 7024 | pid_t pid; |
| 7025 | |
| 7026 | if (pipe(fds) == -1) |
| 7027 | exit(1); |
| 7028 | |
| 7029 | pid = fork(); |
| 7030 | if (pid > 0) { |
| 7031 | uint8_t status; |
| 7032 | ssize_t len; |
| 7033 | |
| 7034 | close(fds[1]); |
| 7035 | |
| 7036 | again: |
| 7037 | len = read(fds[0], &status, 1); |
| 7038 | if (len == -1 && (errno == EINTR)) |
| 7039 | goto again; |
| 7040 | |
| 7041 | if (len != 1 || status != 0) |
| 7042 | exit(1); |
| 7043 | else |
| 7044 | exit(0); |
| 7045 | } else if (pid < 0) |
| 7046 | exit(1); |
| 7047 | |
| 7048 | setsid(); |
| 7049 | |
| 7050 | pid = fork(); |
| 7051 | if (pid > 0) |
| 7052 | exit(0); |
| 7053 | else if (pid < 0) |
| 7054 | exit(1); |
| 7055 | |
| 7056 | umask(027); |
| 7057 | chdir("/"); |
| 7058 | |
| 7059 | signal(SIGTSTP, SIG_IGN); |
| 7060 | signal(SIGTTOU, SIG_IGN); |
| 7061 | signal(SIGTTIN, SIG_IGN); |
| 7062 | } |
| 7063 | #endif |
| 7064 | |
bellard | ff3fbb3 | 2006-01-08 10:53:14 +0000 | [diff] [blame] | 7065 | #ifdef USE_KQEMU |
| 7066 | if (smp_cpus > 1) |
| 7067 | kqemu_allowed = 0; |
| 7068 | #endif |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7069 | linux_boot = (kernel_filename != NULL); |
ths | 42550fd | 2006-12-22 16:34:12 +0000 | [diff] [blame] | 7070 | |
| 7071 | if (!linux_boot && |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame^] | 7072 | hd_filename[0] == '\0' && |
| 7073 | (cdrom_index >= 0 && hd_filename[cdrom_index] == '\0') && |
bellard | c45886d | 2004-01-05 00:02:06 +0000 | [diff] [blame] | 7074 | fd_filename[0] == '\0') |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7075 | help(); |
| 7076 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame^] | 7077 | /* boot to floppy or the default cd if no hard disk defined yet */ |
| 7078 | if (hd_filename[0] == '\0' && boot_device == 'c') { |
| 7079 | if (fd_filename[0] != '\0') |
| 7080 | boot_device = 'a'; |
| 7081 | else |
| 7082 | boot_device = 'd'; |
| 7083 | } |
| 7084 | |
bellard | b118d61 | 2003-06-30 23:36:21 +0000 | [diff] [blame] | 7085 | setvbuf(stdout, NULL, _IOLBF, 0); |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7086 | |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 7087 | init_timers(); |
| 7088 | init_timer_alarm(); |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 7089 | qemu_aio_init(); |
pbrook | 634fce9 | 2006-07-15 17:40:09 +0000 | [diff] [blame] | 7090 | |
bellard | fd1dff4 | 2006-02-01 21:29:26 +0000 | [diff] [blame] | 7091 | #ifdef _WIN32 |
| 7092 | socket_init(); |
| 7093 | #endif |
| 7094 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7095 | /* init network clients */ |
| 7096 | if (nb_net_clients == 0) { |
| 7097 | /* if no clients, we use a default config */ |
| 7098 | pstrcpy(net_clients[0], sizeof(net_clients[0]), |
| 7099 | "nic"); |
| 7100 | pstrcpy(net_clients[1], sizeof(net_clients[0]), |
| 7101 | "user"); |
| 7102 | nb_net_clients = 2; |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 7103 | } |
| 7104 | |
bellard | 7c9d8e0 | 2005-11-15 22:16:05 +0000 | [diff] [blame] | 7105 | for(i = 0;i < nb_net_clients; i++) { |
| 7106 | if (net_client_init(net_clients[i]) < 0) |
| 7107 | exit(1); |
bellard | 702c651 | 2004-04-02 21:21:32 +0000 | [diff] [blame] | 7108 | } |
bellard | f1510b2 | 2003-06-25 00:07:40 +0000 | [diff] [blame] | 7109 | |
ths | eec85c2 | 2007-01-05 17:41:07 +0000 | [diff] [blame] | 7110 | #ifdef TARGET_I386 |
| 7111 | if (boot_device == 'n') { |
| 7112 | for (i = 0; i < nb_nics; i++) { |
| 7113 | const char *model = nd_table[i].model; |
| 7114 | char buf[1024]; |
| 7115 | if (model == NULL) |
| 7116 | model = "ne2k_pci"; |
| 7117 | snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model); |
| 7118 | if (get_image_size(buf) > 0) { |
| 7119 | option_rom[nb_option_roms] = strdup(buf); |
| 7120 | nb_option_roms++; |
| 7121 | break; |
| 7122 | } |
| 7123 | } |
| 7124 | if (i == nb_nics) { |
| 7125 | fprintf(stderr, "No valid PXE rom found for network device\n"); |
| 7126 | exit(1); |
| 7127 | } |
| 7128 | boot_device = 'c'; /* to prevent confusion by the BIOS */ |
| 7129 | } |
| 7130 | #endif |
| 7131 | |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7132 | /* init the memory */ |
bellard | 0ced658 | 2004-05-23 21:06:12 +0000 | [diff] [blame] | 7133 | phys_ram_size = ram_size + vga_ram_size + bios_size; |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 7134 | |
ths | 9ae0255 | 2007-01-05 17:39:04 +0000 | [diff] [blame] | 7135 | for (i = 0; i < nb_option_roms; i++) { |
| 7136 | int ret = get_image_size(option_rom[i]); |
| 7137 | if (ret == -1) { |
| 7138 | fprintf(stderr, "Could not load option rom '%s'\n", option_rom[i]); |
| 7139 | exit(1); |
| 7140 | } |
| 7141 | phys_ram_size += ret; |
| 7142 | } |
| 7143 | |
bellard | d993e02 | 2005-02-10 22:00:06 +0000 | [diff] [blame] | 7144 | phys_ram_base = qemu_vmalloc(phys_ram_size); |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 7145 | if (!phys_ram_base) { |
| 7146 | fprintf(stderr, "Could not allocate physical memory\n"); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7147 | exit(1); |
| 7148 | } |
| 7149 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame^] | 7150 | /* we always create the cdrom drive, even if no disk is there */ |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7151 | bdrv_init(); |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame^] | 7152 | if (cdrom_index >= 0) { |
| 7153 | bs_table[cdrom_index] = bdrv_new("cdrom"); |
| 7154 | bdrv_set_type_hint(bs_table[cdrom_index], BDRV_TYPE_CDROM); |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7155 | } |
| 7156 | |
ths | 96d30e4 | 2007-01-07 20:42:14 +0000 | [diff] [blame^] | 7157 | /* open the virtual block devices */ |
| 7158 | for(i = 0; i < MAX_DISKS; i++) { |
| 7159 | if (hd_filename[i]) { |
| 7160 | if (!bs_table[i]) { |
| 7161 | char buf[64]; |
| 7162 | snprintf(buf, sizeof(buf), "hd%c", i + 'a'); |
| 7163 | bs_table[i] = bdrv_new(buf); |
| 7164 | } |
| 7165 | if (bdrv_open(bs_table[i], hd_filename[i], snapshot ? BDRV_O_SNAPSHOT : 0) < 0) { |
| 7166 | fprintf(stderr, "qemu: could not open hard disk image '%s'\n", |
| 7167 | hd_filename[i]); |
| 7168 | exit(1); |
| 7169 | } |
| 7170 | if (i == 0 && cyls != 0) { |
| 7171 | bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs); |
| 7172 | bdrv_set_translation_hint(bs_table[i], translation); |
| 7173 | } |
| 7174 | } |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7175 | } |
| 7176 | |
| 7177 | /* we always create at least one floppy disk */ |
| 7178 | fd_table[0] = bdrv_new("fda"); |
| 7179 | bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY); |
| 7180 | |
| 7181 | for(i = 0; i < MAX_FD; i++) { |
| 7182 | if (fd_filename[i]) { |
| 7183 | if (!fd_table[i]) { |
| 7184 | char buf[64]; |
| 7185 | snprintf(buf, sizeof(buf), "fd%c", i + 'a'); |
| 7186 | fd_table[i] = bdrv_new(buf); |
| 7187 | bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY); |
| 7188 | } |
| 7189 | if (fd_filename[i] != '\0') { |
bellard | 83f6409 | 2006-08-01 16:21:11 +0000 | [diff] [blame] | 7190 | if (bdrv_open(fd_table[i], fd_filename[i], |
| 7191 | snapshot ? BDRV_O_SNAPSHOT : 0) < 0) { |
bellard | c20709a | 2004-04-21 23:27:19 +0000 | [diff] [blame] | 7192 | fprintf(stderr, "qemu: could not open floppy disk image '%s'\n", |
bellard | c4b1fcc | 2004-03-14 21:44:30 +0000 | [diff] [blame] | 7193 | fd_filename[i]); |
| 7194 | exit(1); |
| 7195 | } |
| 7196 | } |
bellard | 33e3963 | 2003-07-06 17:15:21 +0000 | [diff] [blame] | 7197 | } |
| 7198 | } |
| 7199 | |
bellard | c88676f | 2006-08-06 13:36:11 +0000 | [diff] [blame] | 7200 | register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
| 7201 | register_savevm("ram", 0, 2, ram_save, ram_load, NULL); |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7202 | |
bellard | 330d041 | 2003-07-26 18:11:40 +0000 | [diff] [blame] | 7203 | init_ioports(); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7204 | |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7205 | /* terminal init */ |
bellard | a20dd50 | 2003-09-30 21:07:02 +0000 | [diff] [blame] | 7206 | if (nographic) { |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7207 | dumb_display_init(ds); |
ths | 73fc974 | 2006-12-22 02:09:07 +0000 | [diff] [blame] | 7208 | } else if (vnc_display != NULL) { |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 7209 | vnc_display_init(ds, vnc_display); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7210 | } else { |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 7211 | #if defined(CONFIG_SDL) |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7212 | sdl_display_init(ds, full_screen); |
bellard | 5b0753e | 2005-03-01 21:37:28 +0000 | [diff] [blame] | 7213 | #elif defined(CONFIG_COCOA) |
| 7214 | cocoa_display_init(ds, full_screen); |
bellard | 313aa56 | 2003-08-10 21:52:11 +0000 | [diff] [blame] | 7215 | #else |
| 7216 | dumb_display_init(ds); |
| 7217 | #endif |
| 7218 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7219 | |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7220 | monitor_hd = qemu_chr_open(monitor_device); |
| 7221 | if (!monitor_hd) { |
| 7222 | fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device); |
| 7223 | exit(1); |
| 7224 | } |
| 7225 | monitor_init(monitor_hd, !nographic); |
| 7226 | |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7227 | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7228 | const char *devname = serial_devices[i]; |
| 7229 | if (devname[0] != '\0' && strcmp(devname, "none")) { |
| 7230 | serial_hds[i] = qemu_chr_open(devname); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7231 | if (!serial_hds[i]) { |
| 7232 | fprintf(stderr, "qemu: could not open serial device '%s'\n", |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7233 | devname); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7234 | exit(1); |
| 7235 | } |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7236 | if (!strcmp(devname, "vc")) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 7237 | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i); |
bellard | 8d11df9 | 2004-08-24 21:13:40 +0000 | [diff] [blame] | 7238 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7239 | } |
bellard | 82c643f | 2004-07-14 17:28:13 +0000 | [diff] [blame] | 7240 | |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7241 | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7242 | const char *devname = parallel_devices[i]; |
| 7243 | if (devname[0] != '\0' && strcmp(devname, "none")) { |
| 7244 | parallel_hds[i] = qemu_chr_open(devname); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7245 | if (!parallel_hds[i]) { |
| 7246 | fprintf(stderr, "qemu: could not open parallel device '%s'\n", |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7247 | devname); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7248 | exit(1); |
| 7249 | } |
bellard | c03b0f0 | 2006-09-03 14:10:53 +0000 | [diff] [blame] | 7250 | if (!strcmp(devname, "vc")) |
bellard | 7ba1260 | 2006-07-14 20:26:42 +0000 | [diff] [blame] | 7251 | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i); |
bellard | 6508fe5 | 2005-01-15 12:02:56 +0000 | [diff] [blame] | 7252 | } |
| 7253 | } |
| 7254 | |
bellard | cc1daa4 | 2005-06-05 14:49:17 +0000 | [diff] [blame] | 7255 | machine->init(ram_size, vga_ram_size, boot_device, |
| 7256 | ds, fd_filename, snapshot, |
| 7257 | kernel_filename, kernel_cmdline, initrd_filename); |
bellard | 73332e5 | 2004-04-04 20:22:28 +0000 | [diff] [blame] | 7258 | |
pbrook | 0d92ed3 | 2006-05-21 16:30:15 +0000 | [diff] [blame] | 7259 | /* init USB devices */ |
| 7260 | if (usb_enabled) { |
| 7261 | for(i = 0; i < usb_devices_index; i++) { |
| 7262 | if (usb_device_add(usb_devices[i]) < 0) { |
| 7263 | fprintf(stderr, "Warning: could not add USB device %s\n", |
| 7264 | usb_devices[i]); |
| 7265 | } |
| 7266 | } |
| 7267 | } |
| 7268 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7269 | gui_timer = qemu_new_timer(rt_clock, gui_update, NULL); |
| 7270 | qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock)); |
bellard | 7f7f987 | 2003-10-30 01:11:23 +0000 | [diff] [blame] | 7271 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7272 | #ifdef CONFIG_GDBSTUB |
bellard | b4608c0 | 2003-06-27 17:34:32 +0000 | [diff] [blame] | 7273 | if (use_gdbstub) { |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7274 | if (gdbserver_start(gdbstub_port) < 0) { |
| 7275 | fprintf(stderr, "Could not open gdbserver socket on port %d\n", |
| 7276 | gdbstub_port); |
| 7277 | exit(1); |
| 7278 | } else { |
| 7279 | printf("Waiting gdb connection on port %d\n", gdbstub_port); |
| 7280 | } |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7281 | } else |
| 7282 | #endif |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7283 | if (loadvm) |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 7284 | do_loadvm(loadvm); |
bellard | d63d307 | 2004-10-03 13:29:03 +0000 | [diff] [blame] | 7285 | |
bellard | 67b915a | 2004-03-31 23:37:16 +0000 | [diff] [blame] | 7286 | { |
bellard | 5905b2e | 2004-08-01 21:53:26 +0000 | [diff] [blame] | 7287 | /* XXX: simplify init */ |
| 7288 | read_passwords(); |
| 7289 | if (start_emulation) { |
| 7290 | vm_start(); |
| 7291 | } |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7292 | } |
ths | ffd843b | 2006-12-21 19:46:43 +0000 | [diff] [blame] | 7293 | |
ths | 71e3ceb | 2006-12-22 02:11:31 +0000 | [diff] [blame] | 7294 | if (daemonize) { |
| 7295 | uint8_t status = 0; |
| 7296 | ssize_t len; |
| 7297 | int fd; |
| 7298 | |
| 7299 | again1: |
| 7300 | len = write(fds[1], &status, 1); |
| 7301 | if (len == -1 && (errno == EINTR)) |
| 7302 | goto again1; |
| 7303 | |
| 7304 | if (len != 1) |
| 7305 | exit(1); |
| 7306 | |
| 7307 | fd = open("/dev/null", O_RDWR); |
| 7308 | if (fd == -1) |
| 7309 | exit(1); |
| 7310 | |
| 7311 | dup2(fd, 0); |
| 7312 | dup2(fd, 1); |
| 7313 | dup2(fd, 2); |
| 7314 | |
| 7315 | close(fd); |
| 7316 | } |
| 7317 | |
bellard | 8a7ddc3 | 2004-03-31 19:00:16 +0000 | [diff] [blame] | 7318 | main_loop(); |
bellard | 40c3bac | 2004-04-04 12:56:28 +0000 | [diff] [blame] | 7319 | quit_timers(); |
bellard | 0824d6f | 2003-06-24 13:42:40 +0000 | [diff] [blame] | 7320 | return 0; |
| 7321 | } |