root / vl.c @ 4f7de373
History | View | Annotate | Download (222.2 kB)
1 | 0824d6fc | bellard | /*
|
---|---|---|---|
2 | 80cabfad | bellard | * QEMU System Emulator
|
3 | 5fafdf24 | ths | *
|
4 | 68d0f70e | bellard | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | 1df912cf | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 1df912cf | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 1df912cf | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 1df912cf | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 1df912cf | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 1df912cf | bellard | * furnished to do so, subject to the following conditions:
|
12 | 1df912cf | bellard | *
|
13 | 1df912cf | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 1df912cf | bellard | * all copies or substantial portions of the Software.
|
15 | 1df912cf | bellard | *
|
16 | 1df912cf | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 1df912cf | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 1df912cf | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 1df912cf | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 1df912cf | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 1df912cf | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 1df912cf | bellard | * THE SOFTWARE.
|
23 | 0824d6fc | bellard | */
|
24 | 87ecb68b | pbrook | #include "hw/hw.h" |
25 | 87ecb68b | pbrook | #include "hw/boards.h" |
26 | 87ecb68b | pbrook | #include "hw/usb.h" |
27 | 87ecb68b | pbrook | #include "hw/pcmcia.h" |
28 | 87ecb68b | pbrook | #include "hw/pc.h" |
29 | 87ecb68b | pbrook | #include "hw/audiodev.h" |
30 | 87ecb68b | pbrook | #include "hw/isa.h" |
31 | 2e4d9fb1 | aurel32 | #include "hw/baum.h" |
32 | 87ecb68b | pbrook | #include "net.h" |
33 | 87ecb68b | pbrook | #include "console.h" |
34 | 87ecb68b | pbrook | #include "sysemu.h" |
35 | 87ecb68b | pbrook | #include "gdbstub.h" |
36 | 87ecb68b | pbrook | #include "qemu-timer.h" |
37 | 87ecb68b | pbrook | #include "qemu-char.h" |
38 | 87ecb68b | pbrook | #include "block.h" |
39 | 87ecb68b | pbrook | #include "audio/audio.h" |
40 | 67b915a5 | bellard | |
41 | 0824d6fc | bellard | #include <unistd.h> |
42 | 0824d6fc | bellard | #include <fcntl.h> |
43 | 0824d6fc | bellard | #include <signal.h> |
44 | 0824d6fc | bellard | #include <time.h> |
45 | 0824d6fc | bellard | #include <errno.h> |
46 | 67b915a5 | bellard | #include <sys/time.h> |
47 | c88676f8 | bellard | #include <zlib.h> |
48 | 67b915a5 | bellard | |
49 | 67b915a5 | bellard | #ifndef _WIN32
|
50 | 67b915a5 | bellard | #include <sys/times.h> |
51 | f1510b2c | bellard | #include <sys/wait.h> |
52 | 67b915a5 | bellard | #include <termios.h> |
53 | 67b915a5 | bellard | #include <sys/poll.h> |
54 | 67b915a5 | bellard | #include <sys/mman.h> |
55 | f1510b2c | bellard | #include <sys/ioctl.h> |
56 | f1510b2c | bellard | #include <sys/socket.h> |
57 | c94c8d64 | bellard | #include <netinet/in.h> |
58 | 9d728e8c | bellard | #include <dirent.h> |
59 | 7c9d8e07 | bellard | #include <netdb.h> |
60 | cb4b976b | ths | #include <sys/select.h> |
61 | cb4b976b | ths | #include <arpa/inet.h> |
62 | 7d3505c5 | bellard | #ifdef _BSD
|
63 | 7d3505c5 | bellard | #include <sys/stat.h> |
64 | 83fb7adf | bellard | #ifndef __APPLE__
|
65 | 7d3505c5 | bellard | #include <libutil.h> |
66 | 83fb7adf | bellard | #endif
|
67 | 5c40d2bd | ths | #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
|
68 | 5c40d2bd | ths | #include <freebsd/stdlib.h> |
69 | 7d3505c5 | bellard | #else
|
70 | ec530c81 | bellard | #ifndef __sun__
|
71 | f1510b2c | bellard | #include <linux/if.h> |
72 | f1510b2c | bellard | #include <linux/if_tun.h> |
73 | 7d3505c5 | bellard | #include <pty.h> |
74 | 7d3505c5 | bellard | #include <malloc.h> |
75 | fd872598 | bellard | #include <linux/rtc.h> |
76 | bd494f4c | ths | |
77 | bd494f4c | ths | /* For the benefit of older linux systems which don't supply it,
|
78 | bd494f4c | ths | we use a local copy of hpet.h. */
|
79 | bd494f4c | ths | /* #include <linux/hpet.h> */
|
80 | bd494f4c | ths | #include "hpet.h" |
81 | bd494f4c | ths | |
82 | e57a8c0e | bellard | #include <linux/ppdev.h> |
83 | 5867c88a | ths | #include <linux/parport.h> |
84 | d5d10bc3 | ths | #else
|
85 | d5d10bc3 | ths | #include <sys/stat.h> |
86 | d5d10bc3 | ths | #include <sys/ethernet.h> |
87 | d5d10bc3 | ths | #include <sys/sockio.h> |
88 | d5d10bc3 | ths | #include <netinet/arp.h> |
89 | d5d10bc3 | ths | #include <netinet/in.h> |
90 | d5d10bc3 | ths | #include <netinet/in_systm.h> |
91 | d5d10bc3 | ths | #include <netinet/ip.h> |
92 | d5d10bc3 | ths | #include <netinet/ip_icmp.h> // must come after ip.h |
93 | d5d10bc3 | ths | #include <netinet/udp.h> |
94 | d5d10bc3 | ths | #include <netinet/tcp.h> |
95 | d5d10bc3 | ths | #include <net/if.h> |
96 | d5d10bc3 | ths | #include <syslog.h> |
97 | d5d10bc3 | ths | #include <stropts.h> |
98 | 67b915a5 | bellard | #endif
|
99 | 7d3505c5 | bellard | #endif
|
100 | cb4b976b | ths | #else
|
101 | cb4b976b | ths | #include <winsock2.h> |
102 | cb4b976b | ths | int inet_aton(const char *cp, struct in_addr *ia); |
103 | ec530c81 | bellard | #endif
|
104 | 67b915a5 | bellard | |
105 | c20709aa | bellard | #if defined(CONFIG_SLIRP)
|
106 | c20709aa | bellard | #include "libslirp.h" |
107 | c20709aa | bellard | #endif
|
108 | c20709aa | bellard | |
109 | 67b915a5 | bellard | #ifdef _WIN32
|
110 | 7d3505c5 | bellard | #include <malloc.h> |
111 | 67b915a5 | bellard | #include <sys/timeb.h> |
112 | 4fddf62a | ths | #include <mmsystem.h> |
113 | 67b915a5 | bellard | #define getopt_long_only getopt_long
|
114 | 67b915a5 | bellard | #define memalign(align, size) malloc(size)
|
115 | 67b915a5 | bellard | #endif
|
116 | 67b915a5 | bellard | |
117 | 6ca957f0 | bellard | #include "qemu_socket.h" |
118 | 6ca957f0 | bellard | |
119 | 73332e5c | bellard | #ifdef CONFIG_SDL
|
120 | 96bcd4f8 | bellard | #ifdef __APPLE__
|
121 | 83fb7adf | bellard | #include <SDL/SDL.h> |
122 | 96bcd4f8 | bellard | #endif
|
123 | 73332e5c | bellard | #endif /* CONFIG_SDL */ |
124 | 0824d6fc | bellard | |
125 | 5b0753e0 | bellard | #ifdef CONFIG_COCOA
|
126 | 5b0753e0 | bellard | #undef main
|
127 | 5b0753e0 | bellard | #define main qemu_main
|
128 | 5b0753e0 | bellard | #endif /* CONFIG_COCOA */ |
129 | 5b0753e0 | bellard | |
130 | 0824d6fc | bellard | #include "disas.h" |
131 | fc01f7e7 | bellard | |
132 | 8a7ddc38 | bellard | #include "exec-all.h" |
133 | 0824d6fc | bellard | |
134 | 5a67135a | bellard | #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
135 | b46a8906 | ths | #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown" |
136 | a14d6c8c | pbrook | #ifdef __sun__
|
137 | a14d6c8c | pbrook | #define SMBD_COMMAND "/usr/sfw/sbin/smbd" |
138 | a14d6c8c | pbrook | #else
|
139 | a14d6c8c | pbrook | #define SMBD_COMMAND "/usr/sbin/smbd" |
140 | a14d6c8c | pbrook | #endif
|
141 | f1510b2c | bellard | |
142 | 0824d6fc | bellard | //#define DEBUG_UNUSED_IOPORT
|
143 | fd872598 | bellard | //#define DEBUG_IOPORT
|
144 | 330d0414 | bellard | |
145 | 77d4bc34 | bellard | #ifdef TARGET_PPC
|
146 | 77d4bc34 | bellard | #define DEFAULT_RAM_SIZE 144 |
147 | 77d4bc34 | bellard | #else
|
148 | 1bfe856e | bellard | #define DEFAULT_RAM_SIZE 128 |
149 | 77d4bc34 | bellard | #endif
|
150 | 8a7ddc38 | bellard | /* in ms */
|
151 | 8a7ddc38 | bellard | #define GUI_REFRESH_INTERVAL 30 |
152 | 313aa567 | bellard | |
153 | 0d92ed30 | pbrook | /* Max number of USB devices that can be specified on the commandline. */
|
154 | 0d92ed30 | pbrook | #define MAX_USB_CMDLINE 8 |
155 | 0d92ed30 | pbrook | |
156 | 7dea1da4 | bellard | /* XXX: use a two level table to limit memory usage */
|
157 | 7dea1da4 | bellard | #define MAX_IOPORTS 65536 |
158 | 0824d6fc | bellard | |
159 | 80cabfad | bellard | const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
160 | 1192dad8 | j_mayer | const char *bios_name = NULL; |
161 | c4b1fcc0 | bellard | void *ioport_opaque[MAX_IOPORTS];
|
162 | fc01f7e7 | bellard | IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
|
163 | fc01f7e7 | bellard | IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
|
164 | e4bcb14c | ths | /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
|
165 | faea38e7 | bellard | to store the VM snapshots */
|
166 | e4bcb14c | ths | DriveInfo drives_table[MAX_DRIVES+1];
|
167 | e4bcb14c | ths | int nb_drives;
|
168 | faea38e7 | bellard | /* point to the block driver where the snapshots are managed */
|
169 | faea38e7 | bellard | BlockDriverState *bs_snapshots; |
170 | 313aa567 | bellard | int vga_ram_size;
|
171 | 313aa567 | bellard | static DisplayState display_state;
|
172 | a20dd508 | bellard | int nographic;
|
173 | 4d3b6f6e | balrog | int curses;
|
174 | 3d11d0eb | bellard | const char* keyboard_layout = NULL; |
175 | 313aa567 | bellard | int64_t ticks_per_sec; |
176 | 00f82b8a | aurel32 | ram_addr_t ram_size; |
177 | 80cabfad | bellard | int pit_min_timer_count = 0; |
178 | c4b1fcc0 | bellard | int nb_nics;
|
179 | 7c9d8e07 | bellard | NICInfo nd_table[MAX_NICS]; |
180 | 8a7ddc38 | bellard | int vm_running;
|
181 | f6503059 | balrog | static int rtc_utc = 1; |
182 | f6503059 | balrog | static int rtc_date_offset = -1; /* -1 means no change */ |
183 | 1bfe856e | bellard | int cirrus_vga_enabled = 1; |
184 | d34cab9f | ths | int vmsvga_enabled = 0; |
185 | d827220b | bellard | #ifdef TARGET_SPARC
|
186 | d827220b | bellard | int graphic_width = 1024; |
187 | d827220b | bellard | int graphic_height = 768; |
188 | eee0b836 | blueswir1 | int graphic_depth = 8; |
189 | d827220b | bellard | #else
|
190 | 1bfe856e | bellard | int graphic_width = 800; |
191 | 1bfe856e | bellard | int graphic_height = 600; |
192 | e9b137c2 | bellard | int graphic_depth = 15; |
193 | eee0b836 | blueswir1 | #endif
|
194 | d63d307f | bellard | int full_screen = 0; |
195 | 43523e93 | ths | int no_frame = 0; |
196 | 667accab | ths | int no_quit = 0; |
197 | 8d11df9e | bellard | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
198 | 6508fe59 | bellard | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
199 | a09db21f | bellard | #ifdef TARGET_I386
|
200 | a09db21f | bellard | int win2k_install_hack = 0; |
201 | a09db21f | bellard | #endif
|
202 | bb36d470 | bellard | int usb_enabled = 0; |
203 | 7c9d8e07 | bellard | static VLANState *first_vlan;
|
204 | 6a00d601 | bellard | int smp_cpus = 1; |
205 | 73fc9742 | ths | const char *vnc_display; |
206 | d3e9db93 | bellard | #if defined(TARGET_SPARC)
|
207 | ba3c64fb | bellard | #define MAX_CPUS 16 |
208 | d3e9db93 | bellard | #elif defined(TARGET_I386)
|
209 | d3e9db93 | bellard | #define MAX_CPUS 255 |
210 | ba3c64fb | bellard | #else
|
211 | d3e9db93 | bellard | #define MAX_CPUS 1 |
212 | ba3c64fb | bellard | #endif
|
213 | 6515b203 | bellard | int acpi_enabled = 1; |
214 | 52ca8d6a | bellard | int fd_bootchk = 1; |
215 | d1beab82 | bellard | int no_reboot = 0; |
216 | b2f76161 | aurel32 | int no_shutdown = 0; |
217 | 9467cd46 | balrog | int cursor_hide = 1; |
218 | a171fe39 | balrog | int graphic_rotate = 0; |
219 | 71e3ceb8 | ths | int daemonize = 0; |
220 | 9ae02555 | ths | const char *option_rom[MAX_OPTION_ROMS]; |
221 | 9ae02555 | ths | int nb_option_roms;
|
222 | 8e71621f | pbrook | int semihosting_enabled = 0; |
223 | 3c07f8e8 | pbrook | int autostart = 1; |
224 | 2b8f2d41 | balrog | #ifdef TARGET_ARM
|
225 | 2b8f2d41 | balrog | int old_param = 0; |
226 | 2b8f2d41 | balrog | #endif
|
227 | c35734b2 | ths | const char *qemu_name; |
228 | 3780e197 | ths | int alt_grab = 0; |
229 | 66508601 | blueswir1 | #ifdef TARGET_SPARC
|
230 | 66508601 | blueswir1 | unsigned int nb_prom_envs = 0; |
231 | 66508601 | blueswir1 | const char *prom_envs[MAX_PROM_ENVS]; |
232 | 66508601 | blueswir1 | #endif
|
233 | e4bcb14c | ths | int nb_drives_opt;
|
234 | 609497ab | balrog | struct drive_opt {
|
235 | 609497ab | balrog | const char *file; |
236 | 609497ab | balrog | char opt[1024]; |
237 | 609497ab | balrog | } drives_opt[MAX_DRIVES]; |
238 | 0824d6fc | bellard | |
239 | ee5605e5 | balrog | static CPUState *cur_cpu;
|
240 | ee5605e5 | balrog | static CPUState *next_cpu;
|
241 | 76ea08f9 | balrog | static int event_pending = 1; |
242 | ee5605e5 | balrog | |
243 | aeb30be6 | balrog | #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR) |
244 | aeb30be6 | balrog | |
245 | 0824d6fc | bellard | /***********************************************************/
|
246 | 26aa7d72 | bellard | /* x86 ISA bus support */
|
247 | 26aa7d72 | bellard | |
248 | 26aa7d72 | bellard | target_phys_addr_t isa_mem_base = 0;
|
249 | 3de388f6 | bellard | PicState2 *isa_pic; |
250 | 0824d6fc | bellard | |
251 | 9596ebb7 | pbrook | static uint32_t default_ioport_readb(void *opaque, uint32_t address) |
252 | 0824d6fc | bellard | { |
253 | 0824d6fc | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
254 | 1196be37 | ths | fprintf(stderr, "unused inb: port=0x%04x\n", address);
|
255 | 0824d6fc | bellard | #endif
|
256 | fc01f7e7 | bellard | return 0xff; |
257 | 0824d6fc | bellard | } |
258 | 0824d6fc | bellard | |
259 | 9596ebb7 | pbrook | static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
260 | 0824d6fc | bellard | { |
261 | 0824d6fc | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
262 | 1196be37 | ths | fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
|
263 | 0824d6fc | bellard | #endif
|
264 | 0824d6fc | bellard | } |
265 | 0824d6fc | bellard | |
266 | 0824d6fc | bellard | /* default is to make two byte accesses */
|
267 | 9596ebb7 | pbrook | static uint32_t default_ioport_readw(void *opaque, uint32_t address) |
268 | 0824d6fc | bellard | { |
269 | 0824d6fc | bellard | uint32_t data; |
270 | db45c29a | bellard | data = ioport_read_table[0][address](ioport_opaque[address], address);
|
271 | db45c29a | bellard | address = (address + 1) & (MAX_IOPORTS - 1); |
272 | db45c29a | bellard | data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8; |
273 | 0824d6fc | bellard | return data;
|
274 | 0824d6fc | bellard | } |
275 | 0824d6fc | bellard | |
276 | 9596ebb7 | pbrook | static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
277 | 0824d6fc | bellard | { |
278 | db45c29a | bellard | ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff); |
279 | db45c29a | bellard | address = (address + 1) & (MAX_IOPORTS - 1); |
280 | db45c29a | bellard | ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff); |
281 | 0824d6fc | bellard | } |
282 | 0824d6fc | bellard | |
283 | 9596ebb7 | pbrook | static uint32_t default_ioport_readl(void *opaque, uint32_t address) |
284 | 0824d6fc | bellard | { |
285 | fc01f7e7 | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
286 | 1196be37 | ths | fprintf(stderr, "unused inl: port=0x%04x\n", address);
|
287 | fc01f7e7 | bellard | #endif
|
288 | fc01f7e7 | bellard | return 0xffffffff; |
289 | 0824d6fc | bellard | } |
290 | 0824d6fc | bellard | |
291 | 9596ebb7 | pbrook | static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
292 | 0824d6fc | bellard | { |
293 | fc01f7e7 | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
294 | 1196be37 | ths | fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
|
295 | fc01f7e7 | bellard | #endif
|
296 | 0824d6fc | bellard | } |
297 | 0824d6fc | bellard | |
298 | 9596ebb7 | pbrook | static void init_ioports(void) |
299 | 0824d6fc | bellard | { |
300 | 0824d6fc | bellard | int i;
|
301 | 0824d6fc | bellard | |
302 | fc01f7e7 | bellard | for(i = 0; i < MAX_IOPORTS; i++) { |
303 | fc01f7e7 | bellard | ioport_read_table[0][i] = default_ioport_readb;
|
304 | fc01f7e7 | bellard | ioport_write_table[0][i] = default_ioport_writeb;
|
305 | fc01f7e7 | bellard | ioport_read_table[1][i] = default_ioport_readw;
|
306 | fc01f7e7 | bellard | ioport_write_table[1][i] = default_ioport_writew;
|
307 | fc01f7e7 | bellard | ioport_read_table[2][i] = default_ioport_readl;
|
308 | fc01f7e7 | bellard | ioport_write_table[2][i] = default_ioport_writel;
|
309 | fc01f7e7 | bellard | } |
310 | 0824d6fc | bellard | } |
311 | 0824d6fc | bellard | |
312 | fc01f7e7 | bellard | /* size is the word size in byte */
|
313 | 5fafdf24 | ths | int register_ioport_read(int start, int length, int size, |
314 | c4b1fcc0 | bellard | IOPortReadFunc *func, void *opaque)
|
315 | f1510b2c | bellard | { |
316 | fc01f7e7 | bellard | int i, bsize;
|
317 | f1510b2c | bellard | |
318 | c4b1fcc0 | bellard | if (size == 1) { |
319 | fc01f7e7 | bellard | bsize = 0;
|
320 | c4b1fcc0 | bellard | } else if (size == 2) { |
321 | fc01f7e7 | bellard | bsize = 1;
|
322 | c4b1fcc0 | bellard | } else if (size == 4) { |
323 | fc01f7e7 | bellard | bsize = 2;
|
324 | c4b1fcc0 | bellard | } else {
|
325 | 88fdf56f | balrog | hw_error("register_ioport_read: invalid size");
|
326 | fc01f7e7 | bellard | return -1; |
327 | c4b1fcc0 | bellard | } |
328 | c4b1fcc0 | bellard | for(i = start; i < start + length; i += size) {
|
329 | fc01f7e7 | bellard | ioport_read_table[bsize][i] = func; |
330 | c4b1fcc0 | bellard | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
331 | 88fdf56f | balrog | hw_error("register_ioport_read: invalid opaque");
|
332 | c4b1fcc0 | bellard | ioport_opaque[i] = opaque; |
333 | c4b1fcc0 | bellard | } |
334 | f1510b2c | bellard | return 0; |
335 | f1510b2c | bellard | } |
336 | f1510b2c | bellard | |
337 | fc01f7e7 | bellard | /* size is the word size in byte */
|
338 | 5fafdf24 | ths | int register_ioport_write(int start, int length, int size, |
339 | c4b1fcc0 | bellard | IOPortWriteFunc *func, void *opaque)
|
340 | f1510b2c | bellard | { |
341 | fc01f7e7 | bellard | int i, bsize;
|
342 | f1510b2c | bellard | |
343 | c4b1fcc0 | bellard | if (size == 1) { |
344 | fc01f7e7 | bellard | bsize = 0;
|
345 | c4b1fcc0 | bellard | } else if (size == 2) { |
346 | fc01f7e7 | bellard | bsize = 1;
|
347 | c4b1fcc0 | bellard | } else if (size == 4) { |
348 | fc01f7e7 | bellard | bsize = 2;
|
349 | c4b1fcc0 | bellard | } else {
|
350 | 88fdf56f | balrog | hw_error("register_ioport_write: invalid size");
|
351 | fc01f7e7 | bellard | return -1; |
352 | c4b1fcc0 | bellard | } |
353 | c4b1fcc0 | bellard | for(i = start; i < start + length; i += size) {
|
354 | fc01f7e7 | bellard | ioport_write_table[bsize][i] = func; |
355 | 88fdf56f | balrog | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
356 | 88fdf56f | balrog | hw_error("register_ioport_write: invalid opaque");
|
357 | c4b1fcc0 | bellard | ioport_opaque[i] = opaque; |
358 | c4b1fcc0 | bellard | } |
359 | f1510b2c | bellard | return 0; |
360 | f1510b2c | bellard | } |
361 | f1510b2c | bellard | |
362 | 69b91039 | bellard | void isa_unassign_ioport(int start, int length) |
363 | 69b91039 | bellard | { |
364 | 69b91039 | bellard | int i;
|
365 | 69b91039 | bellard | |
366 | 69b91039 | bellard | for(i = start; i < start + length; i++) {
|
367 | 69b91039 | bellard | ioport_read_table[0][i] = default_ioport_readb;
|
368 | 69b91039 | bellard | ioport_read_table[1][i] = default_ioport_readw;
|
369 | 69b91039 | bellard | ioport_read_table[2][i] = default_ioport_readl;
|
370 | 69b91039 | bellard | |
371 | 69b91039 | bellard | ioport_write_table[0][i] = default_ioport_writeb;
|
372 | 69b91039 | bellard | ioport_write_table[1][i] = default_ioport_writew;
|
373 | 69b91039 | bellard | ioport_write_table[2][i] = default_ioport_writel;
|
374 | 69b91039 | bellard | } |
375 | 69b91039 | bellard | } |
376 | 69b91039 | bellard | |
377 | 20f32282 | bellard | /***********************************************************/
|
378 | 20f32282 | bellard | |
379 | c45886db | bellard | void cpu_outb(CPUState *env, int addr, int val) |
380 | 0824d6fc | bellard | { |
381 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
382 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
383 | fd872598 | bellard | fprintf(logfile, "outb: %04x %02x\n", addr, val);
|
384 | 3b46e624 | ths | #endif
|
385 | c4b1fcc0 | bellard | ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
|
386 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
387 | 89bfc105 | bellard | if (env)
|
388 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
389 | 89bfc105 | bellard | #endif
|
390 | 0824d6fc | bellard | } |
391 | 0824d6fc | bellard | |
392 | c45886db | bellard | void cpu_outw(CPUState *env, int addr, int val) |
393 | 0824d6fc | bellard | { |
394 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
395 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
396 | fd872598 | bellard | fprintf(logfile, "outw: %04x %04x\n", addr, val);
|
397 | 3b46e624 | ths | #endif
|
398 | c4b1fcc0 | bellard | ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
|
399 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
400 | 89bfc105 | bellard | if (env)
|
401 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
402 | 89bfc105 | bellard | #endif
|
403 | 0824d6fc | bellard | } |
404 | 0824d6fc | bellard | |
405 | c45886db | bellard | void cpu_outl(CPUState *env, int addr, int val) |
406 | 0824d6fc | bellard | { |
407 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
408 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
409 | fd872598 | bellard | fprintf(logfile, "outl: %04x %08x\n", addr, val);
|
410 | fd872598 | bellard | #endif
|
411 | c4b1fcc0 | bellard | ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
|
412 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
413 | 89bfc105 | bellard | if (env)
|
414 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
415 | 89bfc105 | bellard | #endif
|
416 | 0824d6fc | bellard | } |
417 | 0824d6fc | bellard | |
418 | c45886db | bellard | int cpu_inb(CPUState *env, int addr) |
419 | 0824d6fc | bellard | { |
420 | fd872598 | bellard | int val;
|
421 | fd872598 | bellard | val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
|
422 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
423 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
424 | fd872598 | bellard | fprintf(logfile, "inb : %04x %02x\n", addr, val);
|
425 | fd872598 | bellard | #endif
|
426 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
427 | 89bfc105 | bellard | if (env)
|
428 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
429 | 89bfc105 | bellard | #endif
|
430 | fd872598 | bellard | return val;
|
431 | 0824d6fc | bellard | } |
432 | 0824d6fc | bellard | |
433 | c45886db | bellard | int cpu_inw(CPUState *env, int addr) |
434 | 0824d6fc | bellard | { |
435 | fd872598 | bellard | int val;
|
436 | fd872598 | bellard | val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
|
437 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
438 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
439 | fd872598 | bellard | fprintf(logfile, "inw : %04x %04x\n", addr, val);
|
440 | fd872598 | bellard | #endif
|
441 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
442 | 89bfc105 | bellard | if (env)
|
443 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
444 | 89bfc105 | bellard | #endif
|
445 | fd872598 | bellard | return val;
|
446 | 0824d6fc | bellard | } |
447 | 0824d6fc | bellard | |
448 | c45886db | bellard | int cpu_inl(CPUState *env, int addr) |
449 | 0824d6fc | bellard | { |
450 | fd872598 | bellard | int val;
|
451 | fd872598 | bellard | val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
|
452 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
453 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
454 | fd872598 | bellard | fprintf(logfile, "inl : %04x %08x\n", addr, val);
|
455 | fd872598 | bellard | #endif
|
456 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
457 | 89bfc105 | bellard | if (env)
|
458 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
459 | 89bfc105 | bellard | #endif
|
460 | fd872598 | bellard | return val;
|
461 | 0824d6fc | bellard | } |
462 | 0824d6fc | bellard | |
463 | 0824d6fc | bellard | /***********************************************************/
|
464 | 0824d6fc | bellard | void hw_error(const char *fmt, ...) |
465 | 0824d6fc | bellard | { |
466 | 0824d6fc | bellard | va_list ap; |
467 | 6a00d601 | bellard | CPUState *env; |
468 | 0824d6fc | bellard | |
469 | 0824d6fc | bellard | va_start(ap, fmt); |
470 | 0824d6fc | bellard | fprintf(stderr, "qemu: hardware error: ");
|
471 | 0824d6fc | bellard | vfprintf(stderr, fmt, ap); |
472 | 0824d6fc | bellard | fprintf(stderr, "\n");
|
473 | 6a00d601 | bellard | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
474 | 6a00d601 | bellard | fprintf(stderr, "CPU #%d:\n", env->cpu_index);
|
475 | 0824d6fc | bellard | #ifdef TARGET_I386
|
476 | 6a00d601 | bellard | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
477 | c45886db | bellard | #else
|
478 | 6a00d601 | bellard | cpu_dump_state(env, stderr, fprintf, 0);
|
479 | 0824d6fc | bellard | #endif
|
480 | 6a00d601 | bellard | } |
481 | 0824d6fc | bellard | va_end(ap); |
482 | 0824d6fc | bellard | abort(); |
483 | 0824d6fc | bellard | } |
484 | 0824d6fc | bellard | |
485 | 8a7ddc38 | bellard | /***********************************************************/
|
486 | 63066f4f | bellard | /* keyboard/mouse */
|
487 | 63066f4f | bellard | |
488 | 63066f4f | bellard | static QEMUPutKBDEvent *qemu_put_kbd_event;
|
489 | 63066f4f | bellard | static void *qemu_put_kbd_event_opaque; |
490 | 455204eb | ths | static QEMUPutMouseEntry *qemu_put_mouse_event_head;
|
491 | 455204eb | ths | static QEMUPutMouseEntry *qemu_put_mouse_event_current;
|
492 | 63066f4f | bellard | |
493 | 63066f4f | bellard | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
494 | 63066f4f | bellard | { |
495 | 63066f4f | bellard | qemu_put_kbd_event_opaque = opaque; |
496 | 63066f4f | bellard | qemu_put_kbd_event = func; |
497 | 63066f4f | bellard | } |
498 | 63066f4f | bellard | |
499 | 455204eb | ths | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
500 | 455204eb | ths | void *opaque, int absolute, |
501 | 455204eb | ths | const char *name) |
502 | 63066f4f | bellard | { |
503 | 455204eb | ths | QEMUPutMouseEntry *s, *cursor; |
504 | 455204eb | ths | |
505 | 455204eb | ths | s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
|
506 | 455204eb | ths | if (!s)
|
507 | 455204eb | ths | return NULL; |
508 | 455204eb | ths | |
509 | 455204eb | ths | s->qemu_put_mouse_event = func; |
510 | 455204eb | ths | s->qemu_put_mouse_event_opaque = opaque; |
511 | 455204eb | ths | s->qemu_put_mouse_event_absolute = absolute; |
512 | 455204eb | ths | s->qemu_put_mouse_event_name = qemu_strdup(name); |
513 | 455204eb | ths | s->next = NULL;
|
514 | 455204eb | ths | |
515 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
516 | 455204eb | ths | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
517 | 455204eb | ths | return s;
|
518 | 455204eb | ths | } |
519 | 455204eb | ths | |
520 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
521 | 455204eb | ths | while (cursor->next != NULL) |
522 | 455204eb | ths | cursor = cursor->next; |
523 | 455204eb | ths | |
524 | 455204eb | ths | cursor->next = s; |
525 | 455204eb | ths | qemu_put_mouse_event_current = s; |
526 | 455204eb | ths | |
527 | 455204eb | ths | return s;
|
528 | 455204eb | ths | } |
529 | 455204eb | ths | |
530 | 455204eb | ths | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
|
531 | 455204eb | ths | { |
532 | 455204eb | ths | QEMUPutMouseEntry *prev = NULL, *cursor;
|
533 | 455204eb | ths | |
534 | 455204eb | ths | if (!qemu_put_mouse_event_head || entry == NULL) |
535 | 455204eb | ths | return;
|
536 | 455204eb | ths | |
537 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
538 | 455204eb | ths | while (cursor != NULL && cursor != entry) { |
539 | 455204eb | ths | prev = cursor; |
540 | 455204eb | ths | cursor = cursor->next; |
541 | 455204eb | ths | } |
542 | 455204eb | ths | |
543 | 455204eb | ths | if (cursor == NULL) // does not exist or list empty |
544 | 455204eb | ths | return;
|
545 | 455204eb | ths | else if (prev == NULL) { // entry is head |
546 | 455204eb | ths | qemu_put_mouse_event_head = cursor->next; |
547 | 455204eb | ths | if (qemu_put_mouse_event_current == entry)
|
548 | 455204eb | ths | qemu_put_mouse_event_current = cursor->next; |
549 | 455204eb | ths | qemu_free(entry->qemu_put_mouse_event_name); |
550 | 455204eb | ths | qemu_free(entry); |
551 | 455204eb | ths | return;
|
552 | 455204eb | ths | } |
553 | 455204eb | ths | |
554 | 455204eb | ths | prev->next = entry->next; |
555 | 455204eb | ths | |
556 | 455204eb | ths | if (qemu_put_mouse_event_current == entry)
|
557 | 455204eb | ths | qemu_put_mouse_event_current = prev; |
558 | 455204eb | ths | |
559 | 455204eb | ths | qemu_free(entry->qemu_put_mouse_event_name); |
560 | 455204eb | ths | qemu_free(entry); |
561 | 63066f4f | bellard | } |
562 | 63066f4f | bellard | |
563 | 63066f4f | bellard | void kbd_put_keycode(int keycode) |
564 | 63066f4f | bellard | { |
565 | 63066f4f | bellard | if (qemu_put_kbd_event) {
|
566 | 63066f4f | bellard | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
567 | 63066f4f | bellard | } |
568 | 63066f4f | bellard | } |
569 | 63066f4f | bellard | |
570 | 63066f4f | bellard | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
571 | 63066f4f | bellard | { |
572 | 455204eb | ths | QEMUPutMouseEvent *mouse_event; |
573 | 455204eb | ths | void *mouse_event_opaque;
|
574 | a171fe39 | balrog | int width;
|
575 | 455204eb | ths | |
576 | 455204eb | ths | if (!qemu_put_mouse_event_current) {
|
577 | 455204eb | ths | return;
|
578 | 455204eb | ths | } |
579 | 455204eb | ths | |
580 | 455204eb | ths | mouse_event = |
581 | 455204eb | ths | qemu_put_mouse_event_current->qemu_put_mouse_event; |
582 | 455204eb | ths | mouse_event_opaque = |
583 | 455204eb | ths | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
584 | 455204eb | ths | |
585 | 455204eb | ths | if (mouse_event) {
|
586 | a171fe39 | balrog | if (graphic_rotate) {
|
587 | a171fe39 | balrog | if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
|
588 | a171fe39 | balrog | width = 0x7fff;
|
589 | a171fe39 | balrog | else
|
590 | b94ed577 | aurel32 | width = graphic_width - 1;
|
591 | a171fe39 | balrog | mouse_event(mouse_event_opaque, |
592 | a171fe39 | balrog | width - dy, dx, dz, buttons_state); |
593 | a171fe39 | balrog | } else
|
594 | a171fe39 | balrog | mouse_event(mouse_event_opaque, |
595 | a171fe39 | balrog | dx, dy, dz, buttons_state); |
596 | 63066f4f | bellard | } |
597 | 63066f4f | bellard | } |
598 | 63066f4f | bellard | |
599 | 09b26c5e | bellard | int kbd_mouse_is_absolute(void) |
600 | 09b26c5e | bellard | { |
601 | 455204eb | ths | if (!qemu_put_mouse_event_current)
|
602 | 455204eb | ths | return 0; |
603 | 455204eb | ths | |
604 | 455204eb | ths | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
|
605 | 455204eb | ths | } |
606 | 455204eb | ths | |
607 | 455204eb | ths | void do_info_mice(void) |
608 | 455204eb | ths | { |
609 | 455204eb | ths | QEMUPutMouseEntry *cursor; |
610 | 455204eb | ths | int index = 0; |
611 | 455204eb | ths | |
612 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
613 | 455204eb | ths | term_printf("No mouse devices connected\n");
|
614 | 455204eb | ths | return;
|
615 | 455204eb | ths | } |
616 | 455204eb | ths | |
617 | 455204eb | ths | term_printf("Mouse devices available:\n");
|
618 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
619 | 455204eb | ths | while (cursor != NULL) { |
620 | 455204eb | ths | term_printf("%c Mouse #%d: %s\n",
|
621 | 455204eb | ths | (cursor == qemu_put_mouse_event_current ? '*' : ' '), |
622 | 455204eb | ths | index, cursor->qemu_put_mouse_event_name); |
623 | 455204eb | ths | index++; |
624 | 455204eb | ths | cursor = cursor->next; |
625 | 455204eb | ths | } |
626 | 455204eb | ths | } |
627 | 455204eb | ths | |
628 | 455204eb | ths | void do_mouse_set(int index) |
629 | 455204eb | ths | { |
630 | 455204eb | ths | QEMUPutMouseEntry *cursor; |
631 | 455204eb | ths | int i = 0; |
632 | 455204eb | ths | |
633 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
634 | 455204eb | ths | term_printf("No mouse devices connected\n");
|
635 | 455204eb | ths | return;
|
636 | 455204eb | ths | } |
637 | 455204eb | ths | |
638 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
639 | 455204eb | ths | while (cursor != NULL && index != i) { |
640 | 455204eb | ths | i++; |
641 | 455204eb | ths | cursor = cursor->next; |
642 | 455204eb | ths | } |
643 | 455204eb | ths | |
644 | 455204eb | ths | if (cursor != NULL) |
645 | 455204eb | ths | qemu_put_mouse_event_current = cursor; |
646 | 455204eb | ths | else
|
647 | 455204eb | ths | term_printf("Mouse at given index not found\n");
|
648 | 09b26c5e | bellard | } |
649 | 09b26c5e | bellard | |
650 | 1dce7c3c | bellard | /* compute with 96 bit intermediate result: (a*b)/c */
|
651 | 1dce7c3c | bellard | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
652 | 0824d6fc | bellard | { |
653 | 1dce7c3c | bellard | union {
|
654 | 1dce7c3c | bellard | uint64_t ll; |
655 | 1dce7c3c | bellard | struct {
|
656 | 1dce7c3c | bellard | #ifdef WORDS_BIGENDIAN
|
657 | 1dce7c3c | bellard | uint32_t high, low; |
658 | 1dce7c3c | bellard | #else
|
659 | 1dce7c3c | bellard | uint32_t low, high; |
660 | 3b46e624 | ths | #endif
|
661 | 1dce7c3c | bellard | } l; |
662 | 1dce7c3c | bellard | } u, res; |
663 | 1dce7c3c | bellard | uint64_t rl, rh; |
664 | 0824d6fc | bellard | |
665 | 1dce7c3c | bellard | u.ll = a; |
666 | 1dce7c3c | bellard | rl = (uint64_t)u.l.low * (uint64_t)b; |
667 | 1dce7c3c | bellard | rh = (uint64_t)u.l.high * (uint64_t)b; |
668 | 1dce7c3c | bellard | rh += (rl >> 32);
|
669 | 1dce7c3c | bellard | res.l.high = rh / c; |
670 | 1dce7c3c | bellard | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
671 | 1dce7c3c | bellard | return res.ll;
|
672 | 34865134 | bellard | } |
673 | 34865134 | bellard | |
674 | 1dce7c3c | bellard | /***********************************************************/
|
675 | 1dce7c3c | bellard | /* real time host monotonic timer */
|
676 | 34865134 | bellard | |
677 | 1dce7c3c | bellard | #define QEMU_TIMER_BASE 1000000000LL |
678 | 34865134 | bellard | |
679 | 1dce7c3c | bellard | #ifdef WIN32
|
680 | 0824d6fc | bellard | |
681 | 1dce7c3c | bellard | static int64_t clock_freq;
|
682 | 1115dde7 | bellard | |
683 | 1dce7c3c | bellard | static void init_get_clock(void) |
684 | 1115dde7 | bellard | { |
685 | a8e5ac33 | bellard | LARGE_INTEGER freq; |
686 | a8e5ac33 | bellard | int ret;
|
687 | 1dce7c3c | bellard | ret = QueryPerformanceFrequency(&freq); |
688 | 1dce7c3c | bellard | if (ret == 0) { |
689 | 1dce7c3c | bellard | fprintf(stderr, "Could not calibrate ticks\n");
|
690 | 1dce7c3c | bellard | exit(1);
|
691 | 1dce7c3c | bellard | } |
692 | 1dce7c3c | bellard | clock_freq = freq.QuadPart; |
693 | 1115dde7 | bellard | } |
694 | 1115dde7 | bellard | |
695 | 1dce7c3c | bellard | static int64_t get_clock(void) |
696 | b8076a74 | bellard | { |
697 | 1dce7c3c | bellard | LARGE_INTEGER ti; |
698 | 1dce7c3c | bellard | QueryPerformanceCounter(&ti); |
699 | 1dce7c3c | bellard | return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
|
700 | b8076a74 | bellard | } |
701 | b8076a74 | bellard | |
702 | 1dce7c3c | bellard | #else
|
703 | 90cb9493 | bellard | |
704 | 1dce7c3c | bellard | static int use_rt_clock; |
705 | 1dce7c3c | bellard | |
706 | 1dce7c3c | bellard | static void init_get_clock(void) |
707 | 90cb9493 | bellard | { |
708 | 1dce7c3c | bellard | use_rt_clock = 0;
|
709 | 1dce7c3c | bellard | #if defined(__linux__)
|
710 | 1dce7c3c | bellard | { |
711 | 1dce7c3c | bellard | struct timespec ts;
|
712 | 1dce7c3c | bellard | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
713 | 1dce7c3c | bellard | use_rt_clock = 1;
|
714 | 1dce7c3c | bellard | } |
715 | 1dce7c3c | bellard | } |
716 | 1dce7c3c | bellard | #endif
|
717 | 90cb9493 | bellard | } |
718 | 90cb9493 | bellard | |
719 | 1dce7c3c | bellard | static int64_t get_clock(void) |
720 | fdbb4691 | bellard | { |
721 | 1dce7c3c | bellard | #if defined(__linux__)
|
722 | 1dce7c3c | bellard | if (use_rt_clock) {
|
723 | 1dce7c3c | bellard | struct timespec ts;
|
724 | 1dce7c3c | bellard | clock_gettime(CLOCK_MONOTONIC, &ts); |
725 | 1dce7c3c | bellard | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
726 | 5fafdf24 | ths | } else
|
727 | fdbb4691 | bellard | #endif
|
728 | 1dce7c3c | bellard | { |
729 | 1dce7c3c | bellard | /* XXX: using gettimeofday leads to problems if the date
|
730 | 1dce7c3c | bellard | changes, so it should be avoided. */
|
731 | 1dce7c3c | bellard | struct timeval tv;
|
732 | 1dce7c3c | bellard | gettimeofday(&tv, NULL);
|
733 | 1dce7c3c | bellard | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
734 | 1dce7c3c | bellard | } |
735 | fdbb4691 | bellard | } |
736 | fdbb4691 | bellard | |
737 | 34865134 | bellard | #endif
|
738 | 34865134 | bellard | |
739 | 1dce7c3c | bellard | /***********************************************************/
|
740 | 1dce7c3c | bellard | /* guest cycle counter */
|
741 | 1dce7c3c | bellard | |
742 | eade0f19 | bellard | static int64_t cpu_ticks_prev;
|
743 | 34865134 | bellard | static int64_t cpu_ticks_offset;
|
744 | 1dce7c3c | bellard | static int64_t cpu_clock_offset;
|
745 | 8a7ddc38 | bellard | static int cpu_ticks_enabled; |
746 | 34865134 | bellard | |
747 | 1dce7c3c | bellard | /* return the host CPU cycle counter and handle stop/restart */
|
748 | 1dce7c3c | bellard | int64_t cpu_get_ticks(void)
|
749 | 34865134 | bellard | { |
750 | 8a7ddc38 | bellard | if (!cpu_ticks_enabled) {
|
751 | 8a7ddc38 | bellard | return cpu_ticks_offset;
|
752 | 8a7ddc38 | bellard | } else {
|
753 | eade0f19 | bellard | int64_t ticks; |
754 | eade0f19 | bellard | ticks = cpu_get_real_ticks(); |
755 | eade0f19 | bellard | if (cpu_ticks_prev > ticks) {
|
756 | eade0f19 | bellard | /* Note: non increasing ticks may happen if the host uses
|
757 | eade0f19 | bellard | software suspend */
|
758 | eade0f19 | bellard | cpu_ticks_offset += cpu_ticks_prev - ticks; |
759 | eade0f19 | bellard | } |
760 | eade0f19 | bellard | cpu_ticks_prev = ticks; |
761 | eade0f19 | bellard | return ticks + cpu_ticks_offset;
|
762 | 8a7ddc38 | bellard | } |
763 | 34865134 | bellard | } |
764 | 34865134 | bellard | |
765 | 1dce7c3c | bellard | /* return the host CPU monotonic timer and handle stop/restart */
|
766 | 1dce7c3c | bellard | static int64_t cpu_get_clock(void) |
767 | 1dce7c3c | bellard | { |
768 | 1dce7c3c | bellard | int64_t ti; |
769 | 1dce7c3c | bellard | if (!cpu_ticks_enabled) {
|
770 | 1dce7c3c | bellard | return cpu_clock_offset;
|
771 | 1dce7c3c | bellard | } else {
|
772 | 1dce7c3c | bellard | ti = get_clock(); |
773 | 1dce7c3c | bellard | return ti + cpu_clock_offset;
|
774 | 1dce7c3c | bellard | } |
775 | 1dce7c3c | bellard | } |
776 | 1dce7c3c | bellard | |
777 | 34865134 | bellard | /* enable cpu_get_ticks() */
|
778 | 34865134 | bellard | void cpu_enable_ticks(void) |
779 | 34865134 | bellard | { |
780 | 8a7ddc38 | bellard | if (!cpu_ticks_enabled) {
|
781 | 8a7ddc38 | bellard | cpu_ticks_offset -= cpu_get_real_ticks(); |
782 | 1dce7c3c | bellard | cpu_clock_offset -= get_clock(); |
783 | 8a7ddc38 | bellard | cpu_ticks_enabled = 1;
|
784 | 8a7ddc38 | bellard | } |
785 | 34865134 | bellard | } |
786 | 34865134 | bellard | |
787 | 34865134 | bellard | /* disable cpu_get_ticks() : the clock is stopped. You must not call
|
788 | 34865134 | bellard | cpu_get_ticks() after that. */
|
789 | 34865134 | bellard | void cpu_disable_ticks(void) |
790 | 34865134 | bellard | { |
791 | 8a7ddc38 | bellard | if (cpu_ticks_enabled) {
|
792 | 8a7ddc38 | bellard | cpu_ticks_offset = cpu_get_ticks(); |
793 | 1dce7c3c | bellard | cpu_clock_offset = cpu_get_clock(); |
794 | 8a7ddc38 | bellard | cpu_ticks_enabled = 0;
|
795 | 8a7ddc38 | bellard | } |
796 | 34865134 | bellard | } |
797 | 34865134 | bellard | |
798 | 1dce7c3c | bellard | /***********************************************************/
|
799 | 1dce7c3c | bellard | /* timers */
|
800 | 5fafdf24 | ths | |
801 | 8a7ddc38 | bellard | #define QEMU_TIMER_REALTIME 0 |
802 | 8a7ddc38 | bellard | #define QEMU_TIMER_VIRTUAL 1 |
803 | 8a7ddc38 | bellard | |
804 | 8a7ddc38 | bellard | struct QEMUClock {
|
805 | 8a7ddc38 | bellard | int type;
|
806 | 8a7ddc38 | bellard | /* XXX: add frequency */
|
807 | 8a7ddc38 | bellard | }; |
808 | 8a7ddc38 | bellard | |
809 | 8a7ddc38 | bellard | struct QEMUTimer {
|
810 | 8a7ddc38 | bellard | QEMUClock *clock; |
811 | 8a7ddc38 | bellard | int64_t expire_time; |
812 | 8a7ddc38 | bellard | QEMUTimerCB *cb; |
813 | 8a7ddc38 | bellard | void *opaque;
|
814 | 8a7ddc38 | bellard | struct QEMUTimer *next;
|
815 | 8a7ddc38 | bellard | }; |
816 | 8a7ddc38 | bellard | |
817 | c8994013 | ths | struct qemu_alarm_timer {
|
818 | c8994013 | ths | char const *name; |
819 | efe75411 | ths | unsigned int flags; |
820 | c8994013 | ths | |
821 | c8994013 | ths | int (*start)(struct qemu_alarm_timer *t); |
822 | c8994013 | ths | void (*stop)(struct qemu_alarm_timer *t); |
823 | efe75411 | ths | void (*rearm)(struct qemu_alarm_timer *t); |
824 | c8994013 | ths | void *priv;
|
825 | c8994013 | ths | }; |
826 | c8994013 | ths | |
827 | efe75411 | ths | #define ALARM_FLAG_DYNTICKS 0x1 |
828 | d5d08334 | balrog | #define ALARM_FLAG_EXPIRED 0x2 |
829 | efe75411 | ths | |
830 | efe75411 | ths | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
831 | efe75411 | ths | { |
832 | efe75411 | ths | return t->flags & ALARM_FLAG_DYNTICKS;
|
833 | efe75411 | ths | } |
834 | efe75411 | ths | |
835 | efe75411 | ths | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
836 | efe75411 | ths | { |
837 | efe75411 | ths | if (!alarm_has_dynticks(t))
|
838 | efe75411 | ths | return;
|
839 | efe75411 | ths | |
840 | efe75411 | ths | t->rearm(t); |
841 | efe75411 | ths | } |
842 | efe75411 | ths | |
843 | efe75411 | ths | /* TODO: MIN_TIMER_REARM_US should be optimized */
|
844 | efe75411 | ths | #define MIN_TIMER_REARM_US 250 |
845 | efe75411 | ths | |
846 | c8994013 | ths | static struct qemu_alarm_timer *alarm_timer; |
847 | 8a7ddc38 | bellard | |
848 | 40c3bac3 | bellard | #ifdef _WIN32
|
849 | c8994013 | ths | |
850 | c8994013 | ths | struct qemu_alarm_win32 {
|
851 | c8994013 | ths | MMRESULT timerId; |
852 | c8994013 | ths | HANDLE host_alarm; |
853 | c8994013 | ths | unsigned int period; |
854 | c8994013 | ths | } alarm_win32_data = {0, NULL, -1}; |
855 | c8994013 | ths | |
856 | c8994013 | ths | static int win32_start_timer(struct qemu_alarm_timer *t); |
857 | c8994013 | ths | static void win32_stop_timer(struct qemu_alarm_timer *t); |
858 | efe75411 | ths | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
859 | c8994013 | ths | |
860 | 40c3bac3 | bellard | #else
|
861 | c8994013 | ths | |
862 | c8994013 | ths | static int unix_start_timer(struct qemu_alarm_timer *t); |
863 | c8994013 | ths | static void unix_stop_timer(struct qemu_alarm_timer *t); |
864 | c8994013 | ths | |
865 | 231c6586 | ths | #ifdef __linux__
|
866 | 231c6586 | ths | |
867 | efe75411 | ths | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
868 | efe75411 | ths | static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
869 | efe75411 | ths | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
870 | efe75411 | ths | |
871 | c40ec5a9 | ths | static int hpet_start_timer(struct qemu_alarm_timer *t); |
872 | c40ec5a9 | ths | static void hpet_stop_timer(struct qemu_alarm_timer *t); |
873 | c40ec5a9 | ths | |
874 | c8994013 | ths | static int rtc_start_timer(struct qemu_alarm_timer *t); |
875 | c8994013 | ths | static void rtc_stop_timer(struct qemu_alarm_timer *t); |
876 | c8994013 | ths | |
877 | efe75411 | ths | #endif /* __linux__ */ |
878 | 8a7ddc38 | bellard | |
879 | c8994013 | ths | #endif /* _WIN32 */ |
880 | c8994013 | ths | |
881 | c8994013 | ths | static struct qemu_alarm_timer alarm_timers[] = { |
882 | efe75411 | ths | #ifndef _WIN32
|
883 | 231c6586 | ths | #ifdef __linux__
|
884 | efe75411 | ths | {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
|
885 | efe75411 | ths | dynticks_stop_timer, dynticks_rearm_timer, NULL},
|
886 | c40ec5a9 | ths | /* HPET - if available - is preferred */
|
887 | efe75411 | ths | {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
888 | c40ec5a9 | ths | /* ...otherwise try RTC */
|
889 | efe75411 | ths | {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
890 | c8994013 | ths | #endif
|
891 | efe75411 | ths | {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
892 | c8994013 | ths | #else
|
893 | efe75411 | ths | {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
|
894 | efe75411 | ths | win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
895 | efe75411 | ths | {"win32", 0, win32_start_timer, |
896 | efe75411 | ths | win32_stop_timer, NULL, &alarm_win32_data},
|
897 | c8994013 | ths | #endif
|
898 | c8994013 | ths | {NULL, }
|
899 | c8994013 | ths | }; |
900 | c8994013 | ths | |
901 | 3f47aa8c | blueswir1 | static void show_available_alarms(void) |
902 | f3dcfada | ths | { |
903 | f3dcfada | ths | int i;
|
904 | f3dcfada | ths | |
905 | f3dcfada | ths | printf("Available alarm timers, in order of precedence:\n");
|
906 | f3dcfada | ths | for (i = 0; alarm_timers[i].name; i++) |
907 | f3dcfada | ths | printf("%s\n", alarm_timers[i].name);
|
908 | f3dcfada | ths | } |
909 | f3dcfada | ths | |
910 | f3dcfada | ths | static void configure_alarms(char const *opt) |
911 | f3dcfada | ths | { |
912 | f3dcfada | ths | int i;
|
913 | f3dcfada | ths | int cur = 0; |
914 | f3dcfada | ths | int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1; |
915 | f3dcfada | ths | char *arg;
|
916 | f3dcfada | ths | char *name;
|
917 | f3dcfada | ths | |
918 | 3adda04c | aurel32 | if (!strcmp(opt, "?")) { |
919 | f3dcfada | ths | show_available_alarms(); |
920 | f3dcfada | ths | exit(0);
|
921 | f3dcfada | ths | } |
922 | f3dcfada | ths | |
923 | f3dcfada | ths | arg = strdup(opt); |
924 | f3dcfada | ths | |
925 | f3dcfada | ths | /* Reorder the array */
|
926 | f3dcfada | ths | name = strtok(arg, ",");
|
927 | f3dcfada | ths | while (name) {
|
928 | f3dcfada | ths | struct qemu_alarm_timer tmp;
|
929 | f3dcfada | ths | |
930 | e2b577e5 | balrog | for (i = 0; i < count && alarm_timers[i].name; i++) { |
931 | f3dcfada | ths | if (!strcmp(alarm_timers[i].name, name))
|
932 | f3dcfada | ths | break;
|
933 | f3dcfada | ths | } |
934 | f3dcfada | ths | |
935 | f3dcfada | ths | if (i == count) {
|
936 | f3dcfada | ths | fprintf(stderr, "Unknown clock %s\n", name);
|
937 | f3dcfada | ths | goto next;
|
938 | f3dcfada | ths | } |
939 | f3dcfada | ths | |
940 | f3dcfada | ths | if (i < cur)
|
941 | f3dcfada | ths | /* Ignore */
|
942 | f3dcfada | ths | goto next;
|
943 | f3dcfada | ths | |
944 | f3dcfada | ths | /* Swap */
|
945 | f3dcfada | ths | tmp = alarm_timers[i]; |
946 | f3dcfada | ths | alarm_timers[i] = alarm_timers[cur]; |
947 | f3dcfada | ths | alarm_timers[cur] = tmp; |
948 | f3dcfada | ths | |
949 | f3dcfada | ths | cur++; |
950 | f3dcfada | ths | next:
|
951 | f3dcfada | ths | name = strtok(NULL, ","); |
952 | f3dcfada | ths | } |
953 | f3dcfada | ths | |
954 | f3dcfada | ths | free(arg); |
955 | f3dcfada | ths | |
956 | f3dcfada | ths | if (cur) {
|
957 | f3dcfada | ths | /* Disable remaining timers */
|
958 | f3dcfada | ths | for (i = cur; i < count; i++)
|
959 | f3dcfada | ths | alarm_timers[i].name = NULL;
|
960 | 3adda04c | aurel32 | } else {
|
961 | 3adda04c | aurel32 | show_available_alarms(); |
962 | 3adda04c | aurel32 | exit(1);
|
963 | f3dcfada | ths | } |
964 | f3dcfada | ths | } |
965 | f3dcfada | ths | |
966 | c8994013 | ths | QEMUClock *rt_clock; |
967 | c8994013 | ths | QEMUClock *vm_clock; |
968 | c8994013 | ths | |
969 | c8994013 | ths | static QEMUTimer *active_timers[2]; |
970 | c8994013 | ths | |
971 | 9596ebb7 | pbrook | static QEMUClock *qemu_new_clock(int type) |
972 | 8a7ddc38 | bellard | { |
973 | 8a7ddc38 | bellard | QEMUClock *clock; |
974 | 8a7ddc38 | bellard | clock = qemu_mallocz(sizeof(QEMUClock));
|
975 | 8a7ddc38 | bellard | if (!clock)
|
976 | 8a7ddc38 | bellard | return NULL; |
977 | 8a7ddc38 | bellard | clock->type = type; |
978 | 8a7ddc38 | bellard | return clock;
|
979 | 8a7ddc38 | bellard | } |
980 | 8a7ddc38 | bellard | |
981 | 8a7ddc38 | bellard | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
|
982 | 8a7ddc38 | bellard | { |
983 | 8a7ddc38 | bellard | QEMUTimer *ts; |
984 | 8a7ddc38 | bellard | |
985 | 8a7ddc38 | bellard | ts = qemu_mallocz(sizeof(QEMUTimer));
|
986 | 8a7ddc38 | bellard | ts->clock = clock; |
987 | 8a7ddc38 | bellard | ts->cb = cb; |
988 | 8a7ddc38 | bellard | ts->opaque = opaque; |
989 | 8a7ddc38 | bellard | return ts;
|
990 | 8a7ddc38 | bellard | } |
991 | 8a7ddc38 | bellard | |
992 | 8a7ddc38 | bellard | void qemu_free_timer(QEMUTimer *ts)
|
993 | 8a7ddc38 | bellard | { |
994 | 8a7ddc38 | bellard | qemu_free(ts); |
995 | 8a7ddc38 | bellard | } |
996 | 8a7ddc38 | bellard | |
997 | 8a7ddc38 | bellard | /* stop a timer, but do not dealloc it */
|
998 | 8a7ddc38 | bellard | void qemu_del_timer(QEMUTimer *ts)
|
999 | 8a7ddc38 | bellard | { |
1000 | 8a7ddc38 | bellard | QEMUTimer **pt, *t; |
1001 | 8a7ddc38 | bellard | |
1002 | 8a7ddc38 | bellard | /* NOTE: this code must be signal safe because
|
1003 | 8a7ddc38 | bellard | qemu_timer_expired() can be called from a signal. */
|
1004 | 8a7ddc38 | bellard | pt = &active_timers[ts->clock->type]; |
1005 | 8a7ddc38 | bellard | for(;;) {
|
1006 | 8a7ddc38 | bellard | t = *pt; |
1007 | 8a7ddc38 | bellard | if (!t)
|
1008 | 8a7ddc38 | bellard | break;
|
1009 | 8a7ddc38 | bellard | if (t == ts) {
|
1010 | 8a7ddc38 | bellard | *pt = t->next; |
1011 | 8a7ddc38 | bellard | break;
|
1012 | 8a7ddc38 | bellard | } |
1013 | 8a7ddc38 | bellard | pt = &t->next; |
1014 | 8a7ddc38 | bellard | } |
1015 | 8a7ddc38 | bellard | } |
1016 | 8a7ddc38 | bellard | |
1017 | 8a7ddc38 | bellard | /* modify the current timer so that it will be fired when current_time
|
1018 | 8a7ddc38 | bellard | >= expire_time. The corresponding callback will be called. */
|
1019 | 8a7ddc38 | bellard | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
|
1020 | 8a7ddc38 | bellard | { |
1021 | 8a7ddc38 | bellard | QEMUTimer **pt, *t; |
1022 | 8a7ddc38 | bellard | |
1023 | 8a7ddc38 | bellard | qemu_del_timer(ts); |
1024 | 8a7ddc38 | bellard | |
1025 | 8a7ddc38 | bellard | /* add the timer in the sorted list */
|
1026 | 8a7ddc38 | bellard | /* NOTE: this code must be signal safe because
|
1027 | 8a7ddc38 | bellard | qemu_timer_expired() can be called from a signal. */
|
1028 | 8a7ddc38 | bellard | pt = &active_timers[ts->clock->type]; |
1029 | 8a7ddc38 | bellard | for(;;) {
|
1030 | 8a7ddc38 | bellard | t = *pt; |
1031 | 8a7ddc38 | bellard | if (!t)
|
1032 | 8a7ddc38 | bellard | break;
|
1033 | 5fafdf24 | ths | if (t->expire_time > expire_time)
|
1034 | 8a7ddc38 | bellard | break;
|
1035 | 8a7ddc38 | bellard | pt = &t->next; |
1036 | 8a7ddc38 | bellard | } |
1037 | 8a7ddc38 | bellard | ts->expire_time = expire_time; |
1038 | 8a7ddc38 | bellard | ts->next = *pt; |
1039 | 8a7ddc38 | bellard | *pt = ts; |
1040 | d5d08334 | balrog | |
1041 | d5d08334 | balrog | /* Rearm if necessary */
|
1042 | d5d08334 | balrog | if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0 && |
1043 | d5d08334 | balrog | pt == &active_timers[ts->clock->type]) |
1044 | d5d08334 | balrog | qemu_rearm_alarm_timer(alarm_timer); |
1045 | 8a7ddc38 | bellard | } |
1046 | 8a7ddc38 | bellard | |
1047 | 8a7ddc38 | bellard | int qemu_timer_pending(QEMUTimer *ts)
|
1048 | 8a7ddc38 | bellard | { |
1049 | 8a7ddc38 | bellard | QEMUTimer *t; |
1050 | 8a7ddc38 | bellard | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
1051 | 8a7ddc38 | bellard | if (t == ts)
|
1052 | 8a7ddc38 | bellard | return 1; |
1053 | 8a7ddc38 | bellard | } |
1054 | 8a7ddc38 | bellard | return 0; |
1055 | 8a7ddc38 | bellard | } |
1056 | 8a7ddc38 | bellard | |
1057 | 8a7ddc38 | bellard | static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
1058 | 8a7ddc38 | bellard | { |
1059 | 8a7ddc38 | bellard | if (!timer_head)
|
1060 | 8a7ddc38 | bellard | return 0; |
1061 | 8a7ddc38 | bellard | return (timer_head->expire_time <= current_time);
|
1062 | 8a7ddc38 | bellard | } |
1063 | 8a7ddc38 | bellard | |
1064 | 8a7ddc38 | bellard | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
1065 | 8a7ddc38 | bellard | { |
1066 | 8a7ddc38 | bellard | QEMUTimer *ts; |
1067 | 3b46e624 | ths | |
1068 | 8a7ddc38 | bellard | for(;;) {
|
1069 | 8a7ddc38 | bellard | ts = *ptimer_head; |
1070 | e95c8d51 | bellard | if (!ts || ts->expire_time > current_time)
|
1071 | 8a7ddc38 | bellard | break;
|
1072 | 8a7ddc38 | bellard | /* remove timer from the list before calling the callback */
|
1073 | 8a7ddc38 | bellard | *ptimer_head = ts->next; |
1074 | 8a7ddc38 | bellard | ts->next = NULL;
|
1075 | 3b46e624 | ths | |
1076 | 8a7ddc38 | bellard | /* run the callback (the timer list can be modified) */
|
1077 | 8a7ddc38 | bellard | ts->cb(ts->opaque); |
1078 | 8a7ddc38 | bellard | } |
1079 | 8a7ddc38 | bellard | } |
1080 | 8a7ddc38 | bellard | |
1081 | 8a7ddc38 | bellard | int64_t qemu_get_clock(QEMUClock *clock) |
1082 | 8a7ddc38 | bellard | { |
1083 | 8a7ddc38 | bellard | switch(clock->type) {
|
1084 | 8a7ddc38 | bellard | case QEMU_TIMER_REALTIME:
|
1085 | 1dce7c3c | bellard | return get_clock() / 1000000; |
1086 | 8a7ddc38 | bellard | default:
|
1087 | 8a7ddc38 | bellard | case QEMU_TIMER_VIRTUAL:
|
1088 | 1dce7c3c | bellard | return cpu_get_clock();
|
1089 | 8a7ddc38 | bellard | } |
1090 | 8a7ddc38 | bellard | } |
1091 | 8a7ddc38 | bellard | |
1092 | 1dce7c3c | bellard | static void init_timers(void) |
1093 | 1dce7c3c | bellard | { |
1094 | 1dce7c3c | bellard | init_get_clock(); |
1095 | 1dce7c3c | bellard | ticks_per_sec = QEMU_TIMER_BASE; |
1096 | 1dce7c3c | bellard | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
1097 | 1dce7c3c | bellard | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
1098 | 1dce7c3c | bellard | } |
1099 | 1dce7c3c | bellard | |
1100 | 8a7ddc38 | bellard | /* save a timer */
|
1101 | 8a7ddc38 | bellard | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
|
1102 | 8a7ddc38 | bellard | { |
1103 | 8a7ddc38 | bellard | uint64_t expire_time; |
1104 | 8a7ddc38 | bellard | |
1105 | 8a7ddc38 | bellard | if (qemu_timer_pending(ts)) {
|
1106 | 8a7ddc38 | bellard | expire_time = ts->expire_time; |
1107 | 8a7ddc38 | bellard | } else {
|
1108 | 8a7ddc38 | bellard | expire_time = -1;
|
1109 | 8a7ddc38 | bellard | } |
1110 | 8a7ddc38 | bellard | qemu_put_be64(f, expire_time); |
1111 | 8a7ddc38 | bellard | } |
1112 | 8a7ddc38 | bellard | |
1113 | 8a7ddc38 | bellard | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
|
1114 | 8a7ddc38 | bellard | { |
1115 | 8a7ddc38 | bellard | uint64_t expire_time; |
1116 | 8a7ddc38 | bellard | |
1117 | 8a7ddc38 | bellard | expire_time = qemu_get_be64(f); |
1118 | 8a7ddc38 | bellard | if (expire_time != -1) { |
1119 | 8a7ddc38 | bellard | qemu_mod_timer(ts, expire_time); |
1120 | 8a7ddc38 | bellard | } else {
|
1121 | 8a7ddc38 | bellard | qemu_del_timer(ts); |
1122 | 8a7ddc38 | bellard | } |
1123 | 8a7ddc38 | bellard | } |
1124 | 8a7ddc38 | bellard | |
1125 | 8a7ddc38 | bellard | static void timer_save(QEMUFile *f, void *opaque) |
1126 | 8a7ddc38 | bellard | { |
1127 | 8a7ddc38 | bellard | if (cpu_ticks_enabled) {
|
1128 | 8a7ddc38 | bellard | hw_error("cannot save state if virtual timers are running");
|
1129 | 8a7ddc38 | bellard | } |
1130 | bee8d684 | ths | qemu_put_be64(f, cpu_ticks_offset); |
1131 | bee8d684 | ths | qemu_put_be64(f, ticks_per_sec); |
1132 | bee8d684 | ths | qemu_put_be64(f, cpu_clock_offset); |
1133 | 8a7ddc38 | bellard | } |
1134 | 8a7ddc38 | bellard | |
1135 | 8a7ddc38 | bellard | static int timer_load(QEMUFile *f, void *opaque, int version_id) |
1136 | 8a7ddc38 | bellard | { |
1137 | c88676f8 | bellard | if (version_id != 1 && version_id != 2) |
1138 | 8a7ddc38 | bellard | return -EINVAL;
|
1139 | 8a7ddc38 | bellard | if (cpu_ticks_enabled) {
|
1140 | 8a7ddc38 | bellard | return -EINVAL;
|
1141 | 8a7ddc38 | bellard | } |
1142 | bee8d684 | ths | cpu_ticks_offset=qemu_get_be64(f); |
1143 | bee8d684 | ths | ticks_per_sec=qemu_get_be64(f); |
1144 | c88676f8 | bellard | if (version_id == 2) { |
1145 | bee8d684 | ths | cpu_clock_offset=qemu_get_be64(f); |
1146 | c88676f8 | bellard | } |
1147 | 8a7ddc38 | bellard | return 0; |
1148 | 8a7ddc38 | bellard | } |
1149 | 8a7ddc38 | bellard | |
1150 | 67b915a5 | bellard | #ifdef _WIN32
|
1151 | 5fafdf24 | ths | void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
|
1152 | 67b915a5 | bellard | DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
1153 | 67b915a5 | bellard | #else
|
1154 | 8a7ddc38 | bellard | static void host_alarm_handler(int host_signum) |
1155 | 67b915a5 | bellard | #endif
|
1156 | 8a7ddc38 | bellard | { |
1157 | 02ba45c5 | bellard | #if 0
|
1158 | 02ba45c5 | bellard | #define DISP_FREQ 1000
|
1159 | 02ba45c5 | bellard | {
|
1160 | 02ba45c5 | bellard | static int64_t delta_min = INT64_MAX;
|
1161 | 02ba45c5 | bellard | static int64_t delta_max, delta_cum, last_clock, delta, ti;
|
1162 | 02ba45c5 | bellard | static int count;
|
1163 | 02ba45c5 | bellard | ti = qemu_get_clock(vm_clock);
|
1164 | 02ba45c5 | bellard | if (last_clock != 0) {
|
1165 | 02ba45c5 | bellard | delta = ti - last_clock;
|
1166 | 02ba45c5 | bellard | if (delta < delta_min)
|
1167 | 02ba45c5 | bellard | delta_min = delta;
|
1168 | 02ba45c5 | bellard | if (delta > delta_max)
|
1169 | 02ba45c5 | bellard | delta_max = delta;
|
1170 | 02ba45c5 | bellard | delta_cum += delta;
|
1171 | 02ba45c5 | bellard | if (++count == DISP_FREQ) {
|
1172 | 26a76461 | bellard | printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
|
1173 | 02ba45c5 | bellard | muldiv64(delta_min, 1000000, ticks_per_sec),
|
1174 | 02ba45c5 | bellard | muldiv64(delta_max, 1000000, ticks_per_sec),
|
1175 | 02ba45c5 | bellard | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
|
1176 | 02ba45c5 | bellard | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
|
1177 | 02ba45c5 | bellard | count = 0;
|
1178 | 02ba45c5 | bellard | delta_min = INT64_MAX;
|
1179 | 02ba45c5 | bellard | delta_max = 0;
|
1180 | 02ba45c5 | bellard | delta_cum = 0;
|
1181 | 02ba45c5 | bellard | }
|
1182 | 02ba45c5 | bellard | }
|
1183 | 02ba45c5 | bellard | last_clock = ti;
|
1184 | 02ba45c5 | bellard | }
|
1185 | 02ba45c5 | bellard | #endif
|
1186 | efe75411 | ths | if (alarm_has_dynticks(alarm_timer) ||
|
1187 | efe75411 | ths | qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
1188 | 8a7ddc38 | bellard | qemu_get_clock(vm_clock)) || |
1189 | 8a7ddc38 | bellard | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
1190 | 8a7ddc38 | bellard | qemu_get_clock(rt_clock))) { |
1191 | 06d9f2f7 | bellard | #ifdef _WIN32
|
1192 | c8994013 | ths | struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv; |
1193 | c8994013 | ths | SetEvent(data->host_alarm); |
1194 | 06d9f2f7 | bellard | #endif
|
1195 | ee5605e5 | balrog | CPUState *env = next_cpu; |
1196 | ee5605e5 | balrog | |
1197 | d5d08334 | balrog | alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
1198 | d5d08334 | balrog | |
1199 | 4f8eb8da | balrog | if (env) {
|
1200 | 4f8eb8da | balrog | /* stop the currently executing cpu because a timer occured */
|
1201 | 4f8eb8da | balrog | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
1202 | a332e112 | bellard | #ifdef USE_KQEMU
|
1203 | 4f8eb8da | balrog | if (env->kqemu_enabled) {
|
1204 | 4f8eb8da | balrog | kqemu_cpu_interrupt(env); |
1205 | 4f8eb8da | balrog | } |
1206 | ee5605e5 | balrog | #endif
|
1207 | 4f8eb8da | balrog | } |
1208 | ee5605e5 | balrog | event_pending = 1;
|
1209 | 8a7ddc38 | bellard | } |
1210 | 8a7ddc38 | bellard | } |
1211 | 8a7ddc38 | bellard | |
1212 | efe75411 | ths | static uint64_t qemu_next_deadline(void) |
1213 | efe75411 | ths | { |
1214 | 49a9b725 | balrog | int64_t nearest_delta_us = INT64_MAX; |
1215 | efe75411 | ths | int64_t vmdelta_us; |
1216 | efe75411 | ths | |
1217 | efe75411 | ths | if (active_timers[QEMU_TIMER_REALTIME])
|
1218 | efe75411 | ths | nearest_delta_us = (active_timers[QEMU_TIMER_REALTIME]->expire_time - |
1219 | efe75411 | ths | qemu_get_clock(rt_clock))*1000;
|
1220 | efe75411 | ths | |
1221 | efe75411 | ths | if (active_timers[QEMU_TIMER_VIRTUAL]) {
|
1222 | efe75411 | ths | /* round up */
|
1223 | efe75411 | ths | vmdelta_us = (active_timers[QEMU_TIMER_VIRTUAL]->expire_time - |
1224 | efe75411 | ths | qemu_get_clock(vm_clock)+999)/1000; |
1225 | efe75411 | ths | if (vmdelta_us < nearest_delta_us)
|
1226 | efe75411 | ths | nearest_delta_us = vmdelta_us; |
1227 | efe75411 | ths | } |
1228 | efe75411 | ths | |
1229 | efe75411 | ths | /* Avoid arming the timer to negative, zero, or too low values */
|
1230 | efe75411 | ths | if (nearest_delta_us <= MIN_TIMER_REARM_US)
|
1231 | efe75411 | ths | nearest_delta_us = MIN_TIMER_REARM_US; |
1232 | efe75411 | ths | |
1233 | efe75411 | ths | return nearest_delta_us;
|
1234 | efe75411 | ths | } |
1235 | efe75411 | ths | |
1236 | fd872598 | bellard | #ifndef _WIN32
|
1237 | fd872598 | bellard | |
1238 | 829309c7 | bellard | #if defined(__linux__)
|
1239 | 829309c7 | bellard | |
1240 | fd872598 | bellard | #define RTC_FREQ 1024 |
1241 | fd872598 | bellard | |
1242 | c8994013 | ths | static void enable_sigio_timer(int fd) |
1243 | c8994013 | ths | { |
1244 | c8994013 | ths | struct sigaction act;
|
1245 | c8994013 | ths | |
1246 | c8994013 | ths | /* timer signal */
|
1247 | c8994013 | ths | sigfillset(&act.sa_mask); |
1248 | c8994013 | ths | act.sa_flags = 0;
|
1249 | c8994013 | ths | act.sa_handler = host_alarm_handler; |
1250 | c8994013 | ths | |
1251 | c8994013 | ths | sigaction(SIGIO, &act, NULL);
|
1252 | c8994013 | ths | fcntl(fd, F_SETFL, O_ASYNC); |
1253 | c8994013 | ths | fcntl(fd, F_SETOWN, getpid()); |
1254 | c8994013 | ths | } |
1255 | 829309c7 | bellard | |
1256 | c40ec5a9 | ths | static int hpet_start_timer(struct qemu_alarm_timer *t) |
1257 | c40ec5a9 | ths | { |
1258 | c40ec5a9 | ths | struct hpet_info info;
|
1259 | c40ec5a9 | ths | int r, fd;
|
1260 | c40ec5a9 | ths | |
1261 | c40ec5a9 | ths | fd = open("/dev/hpet", O_RDONLY);
|
1262 | c40ec5a9 | ths | if (fd < 0) |
1263 | c40ec5a9 | ths | return -1; |
1264 | c40ec5a9 | ths | |
1265 | c40ec5a9 | ths | /* Set frequency */
|
1266 | c40ec5a9 | ths | r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
1267 | c40ec5a9 | ths | if (r < 0) { |
1268 | c40ec5a9 | ths | fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
|
1269 | c40ec5a9 | ths | "error, but for better emulation accuracy type:\n"
|
1270 | c40ec5a9 | ths | "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
|
1271 | c40ec5a9 | ths | goto fail;
|
1272 | c40ec5a9 | ths | } |
1273 | c40ec5a9 | ths | |
1274 | c40ec5a9 | ths | /* Check capabilities */
|
1275 | c40ec5a9 | ths | r = ioctl(fd, HPET_INFO, &info); |
1276 | c40ec5a9 | ths | if (r < 0) |
1277 | c40ec5a9 | ths | goto fail;
|
1278 | c40ec5a9 | ths | |
1279 | c40ec5a9 | ths | /* Enable periodic mode */
|
1280 | c40ec5a9 | ths | r = ioctl(fd, HPET_EPI, 0);
|
1281 | c40ec5a9 | ths | if (info.hi_flags && (r < 0)) |
1282 | c40ec5a9 | ths | goto fail;
|
1283 | c40ec5a9 | ths | |
1284 | c40ec5a9 | ths | /* Enable interrupt */
|
1285 | c40ec5a9 | ths | r = ioctl(fd, HPET_IE_ON, 0);
|
1286 | c40ec5a9 | ths | if (r < 0) |
1287 | c40ec5a9 | ths | goto fail;
|
1288 | c40ec5a9 | ths | |
1289 | c40ec5a9 | ths | enable_sigio_timer(fd); |
1290 | fcdc2129 | pbrook | t->priv = (void *)(long)fd; |
1291 | c40ec5a9 | ths | |
1292 | c40ec5a9 | ths | return 0; |
1293 | c40ec5a9 | ths | fail:
|
1294 | c40ec5a9 | ths | close(fd); |
1295 | c40ec5a9 | ths | return -1; |
1296 | c40ec5a9 | ths | } |
1297 | c40ec5a9 | ths | |
1298 | c40ec5a9 | ths | static void hpet_stop_timer(struct qemu_alarm_timer *t) |
1299 | c40ec5a9 | ths | { |
1300 | fcdc2129 | pbrook | int fd = (long)t->priv; |
1301 | c40ec5a9 | ths | |
1302 | c40ec5a9 | ths | close(fd); |
1303 | c40ec5a9 | ths | } |
1304 | c40ec5a9 | ths | |
1305 | c8994013 | ths | static int rtc_start_timer(struct qemu_alarm_timer *t) |
1306 | fd872598 | bellard | { |
1307 | c8994013 | ths | int rtc_fd;
|
1308 | b5a23ad4 | balrog | unsigned long current_rtc_freq = 0; |
1309 | c8994013 | ths | |
1310 | aeb30be6 | balrog | TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
|
1311 | fd872598 | bellard | if (rtc_fd < 0) |
1312 | fd872598 | bellard | return -1; |
1313 | b5a23ad4 | balrog | ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
1314 | b5a23ad4 | balrog | if (current_rtc_freq != RTC_FREQ &&
|
1315 | b5a23ad4 | balrog | ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
|
1316 | fd872598 | bellard | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
|
1317 | fd872598 | bellard | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
|
1318 | fd872598 | bellard | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
|
1319 | fd872598 | bellard | goto fail;
|
1320 | fd872598 | bellard | } |
1321 | fd872598 | bellard | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
1322 | fd872598 | bellard | fail:
|
1323 | fd872598 | bellard | close(rtc_fd); |
1324 | fd872598 | bellard | return -1; |
1325 | fd872598 | bellard | } |
1326 | c8994013 | ths | |
1327 | c8994013 | ths | enable_sigio_timer(rtc_fd); |
1328 | c8994013 | ths | |
1329 | fcdc2129 | pbrook | t->priv = (void *)(long)rtc_fd; |
1330 | c8994013 | ths | |
1331 | fd872598 | bellard | return 0; |
1332 | fd872598 | bellard | } |
1333 | fd872598 | bellard | |
1334 | c8994013 | ths | static void rtc_stop_timer(struct qemu_alarm_timer *t) |
1335 | 829309c7 | bellard | { |
1336 | fcdc2129 | pbrook | int rtc_fd = (long)t->priv; |
1337 | c8994013 | ths | |
1338 | c8994013 | ths | close(rtc_fd); |
1339 | 829309c7 | bellard | } |
1340 | 829309c7 | bellard | |
1341 | efe75411 | ths | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
1342 | efe75411 | ths | { |
1343 | efe75411 | ths | struct sigevent ev;
|
1344 | efe75411 | ths | timer_t host_timer; |
1345 | efe75411 | ths | struct sigaction act;
|
1346 | efe75411 | ths | |
1347 | efe75411 | ths | sigfillset(&act.sa_mask); |
1348 | efe75411 | ths | act.sa_flags = 0;
|
1349 | efe75411 | ths | act.sa_handler = host_alarm_handler; |
1350 | efe75411 | ths | |
1351 | efe75411 | ths | sigaction(SIGALRM, &act, NULL);
|
1352 | efe75411 | ths | |
1353 | efe75411 | ths | ev.sigev_value.sival_int = 0;
|
1354 | efe75411 | ths | ev.sigev_notify = SIGEV_SIGNAL; |
1355 | efe75411 | ths | ev.sigev_signo = SIGALRM; |
1356 | efe75411 | ths | |
1357 | efe75411 | ths | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
|
1358 | efe75411 | ths | perror("timer_create");
|
1359 | efe75411 | ths | |
1360 | efe75411 | ths | /* disable dynticks */
|
1361 | efe75411 | ths | fprintf(stderr, "Dynamic Ticks disabled\n");
|
1362 | efe75411 | ths | |
1363 | efe75411 | ths | return -1; |
1364 | efe75411 | ths | } |
1365 | efe75411 | ths | |
1366 | efe75411 | ths | t->priv = (void *)host_timer;
|
1367 | efe75411 | ths | |
1368 | efe75411 | ths | return 0; |
1369 | efe75411 | ths | } |
1370 | efe75411 | ths | |
1371 | efe75411 | ths | static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
1372 | efe75411 | ths | { |
1373 | efe75411 | ths | timer_t host_timer = (timer_t)t->priv; |
1374 | efe75411 | ths | |
1375 | efe75411 | ths | timer_delete(host_timer); |
1376 | efe75411 | ths | } |
1377 | efe75411 | ths | |
1378 | efe75411 | ths | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
1379 | efe75411 | ths | { |
1380 | efe75411 | ths | timer_t host_timer = (timer_t)t->priv; |
1381 | efe75411 | ths | struct itimerspec timeout;
|
1382 | efe75411 | ths | int64_t nearest_delta_us = INT64_MAX; |
1383 | efe75411 | ths | int64_t current_us; |
1384 | efe75411 | ths | |
1385 | efe75411 | ths | if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1386 | efe75411 | ths | !active_timers[QEMU_TIMER_VIRTUAL]) |
1387 | d5d08334 | balrog | return;
|
1388 | efe75411 | ths | |
1389 | efe75411 | ths | nearest_delta_us = qemu_next_deadline(); |
1390 | efe75411 | ths | |
1391 | efe75411 | ths | /* check whether a timer is already running */
|
1392 | efe75411 | ths | if (timer_gettime(host_timer, &timeout)) {
|
1393 | efe75411 | ths | perror("gettime");
|
1394 | efe75411 | ths | fprintf(stderr, "Internal timer error: aborting\n");
|
1395 | efe75411 | ths | exit(1);
|
1396 | efe75411 | ths | } |
1397 | efe75411 | ths | current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
1398 | efe75411 | ths | if (current_us && current_us <= nearest_delta_us)
|
1399 | efe75411 | ths | return;
|
1400 | efe75411 | ths | |
1401 | efe75411 | ths | timeout.it_interval.tv_sec = 0;
|
1402 | efe75411 | ths | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
1403 | efe75411 | ths | timeout.it_value.tv_sec = nearest_delta_us / 1000000;
|
1404 | efe75411 | ths | timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
1405 | efe75411 | ths | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
1406 | efe75411 | ths | perror("settime");
|
1407 | efe75411 | ths | fprintf(stderr, "Internal timer error: aborting\n");
|
1408 | efe75411 | ths | exit(1);
|
1409 | efe75411 | ths | } |
1410 | efe75411 | ths | } |
1411 | efe75411 | ths | |
1412 | 70744b3a | ths | #endif /* defined(__linux__) */ |
1413 | 231c6586 | ths | |
1414 | c8994013 | ths | static int unix_start_timer(struct qemu_alarm_timer *t) |
1415 | c8994013 | ths | { |
1416 | c8994013 | ths | struct sigaction act;
|
1417 | c8994013 | ths | struct itimerval itv;
|
1418 | c8994013 | ths | int err;
|
1419 | c8994013 | ths | |
1420 | c8994013 | ths | /* timer signal */
|
1421 | c8994013 | ths | sigfillset(&act.sa_mask); |
1422 | c8994013 | ths | act.sa_flags = 0;
|
1423 | c8994013 | ths | act.sa_handler = host_alarm_handler; |
1424 | c8994013 | ths | |
1425 | c8994013 | ths | sigaction(SIGALRM, &act, NULL);
|
1426 | c8994013 | ths | |
1427 | c8994013 | ths | itv.it_interval.tv_sec = 0;
|
1428 | c8994013 | ths | /* for i386 kernel 2.6 to get 1 ms */
|
1429 | c8994013 | ths | itv.it_interval.tv_usec = 999;
|
1430 | c8994013 | ths | itv.it_value.tv_sec = 0;
|
1431 | c8994013 | ths | itv.it_value.tv_usec = 10 * 1000; |
1432 | c8994013 | ths | |
1433 | c8994013 | ths | err = setitimer(ITIMER_REAL, &itv, NULL);
|
1434 | c8994013 | ths | if (err)
|
1435 | c8994013 | ths | return -1; |
1436 | c8994013 | ths | |
1437 | c8994013 | ths | return 0; |
1438 | c8994013 | ths | } |
1439 | c8994013 | ths | |
1440 | c8994013 | ths | static void unix_stop_timer(struct qemu_alarm_timer *t) |
1441 | c8994013 | ths | { |
1442 | c8994013 | ths | struct itimerval itv;
|
1443 | c8994013 | ths | |
1444 | c8994013 | ths | memset(&itv, 0, sizeof(itv)); |
1445 | c8994013 | ths | setitimer(ITIMER_REAL, &itv, NULL);
|
1446 | c8994013 | ths | } |
1447 | c8994013 | ths | |
1448 | 829309c7 | bellard | #endif /* !defined(_WIN32) */ |
1449 | fd872598 | bellard | |
1450 | c8994013 | ths | #ifdef _WIN32
|
1451 | c8994013 | ths | |
1452 | c8994013 | ths | static int win32_start_timer(struct qemu_alarm_timer *t) |
1453 | c8994013 | ths | { |
1454 | c8994013 | ths | TIMECAPS tc; |
1455 | c8994013 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1456 | efe75411 | ths | UINT flags; |
1457 | c8994013 | ths | |
1458 | c8994013 | ths | data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL); |
1459 | c8994013 | ths | if (!data->host_alarm) {
|
1460 | c8994013 | ths | perror("Failed CreateEvent");
|
1461 | c396a7f0 | ths | return -1; |
1462 | c8994013 | ths | } |
1463 | c8994013 | ths | |
1464 | c8994013 | ths | memset(&tc, 0, sizeof(tc)); |
1465 | c8994013 | ths | timeGetDevCaps(&tc, sizeof(tc));
|
1466 | c8994013 | ths | |
1467 | c8994013 | ths | if (data->period < tc.wPeriodMin)
|
1468 | c8994013 | ths | data->period = tc.wPeriodMin; |
1469 | c8994013 | ths | |
1470 | c8994013 | ths | timeBeginPeriod(data->period); |
1471 | c8994013 | ths | |
1472 | efe75411 | ths | flags = TIME_CALLBACK_FUNCTION; |
1473 | efe75411 | ths | if (alarm_has_dynticks(t))
|
1474 | efe75411 | ths | flags |= TIME_ONESHOT; |
1475 | efe75411 | ths | else
|
1476 | efe75411 | ths | flags |= TIME_PERIODIC; |
1477 | efe75411 | ths | |
1478 | c8994013 | ths | data->timerId = timeSetEvent(1, // interval (ms) |
1479 | c8994013 | ths | data->period, // resolution
|
1480 | c8994013 | ths | host_alarm_handler, // function
|
1481 | c8994013 | ths | (DWORD)t, // parameter
|
1482 | efe75411 | ths | flags); |
1483 | c8994013 | ths | |
1484 | c8994013 | ths | if (!data->timerId) {
|
1485 | c8994013 | ths | perror("Failed to initialize win32 alarm timer");
|
1486 | c8994013 | ths | |
1487 | c8994013 | ths | timeEndPeriod(data->period); |
1488 | c8994013 | ths | CloseHandle(data->host_alarm); |
1489 | c8994013 | ths | return -1; |
1490 | c8994013 | ths | } |
1491 | c8994013 | ths | |
1492 | c8994013 | ths | qemu_add_wait_object(data->host_alarm, NULL, NULL); |
1493 | c8994013 | ths | |
1494 | c8994013 | ths | return 0; |
1495 | c8994013 | ths | } |
1496 | c8994013 | ths | |
1497 | c8994013 | ths | static void win32_stop_timer(struct qemu_alarm_timer *t) |
1498 | c8994013 | ths | { |
1499 | c8994013 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1500 | c8994013 | ths | |
1501 | c8994013 | ths | timeKillEvent(data->timerId); |
1502 | c8994013 | ths | timeEndPeriod(data->period); |
1503 | c8994013 | ths | |
1504 | c8994013 | ths | CloseHandle(data->host_alarm); |
1505 | c8994013 | ths | } |
1506 | c8994013 | ths | |
1507 | efe75411 | ths | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
1508 | efe75411 | ths | { |
1509 | efe75411 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1510 | efe75411 | ths | uint64_t nearest_delta_us; |
1511 | efe75411 | ths | |
1512 | efe75411 | ths | if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1513 | efe75411 | ths | !active_timers[QEMU_TIMER_VIRTUAL]) |
1514 | d5d08334 | balrog | return;
|
1515 | efe75411 | ths | |
1516 | efe75411 | ths | nearest_delta_us = qemu_next_deadline(); |
1517 | efe75411 | ths | nearest_delta_us /= 1000;
|
1518 | efe75411 | ths | |
1519 | efe75411 | ths | timeKillEvent(data->timerId); |
1520 | efe75411 | ths | |
1521 | efe75411 | ths | data->timerId = timeSetEvent(1,
|
1522 | efe75411 | ths | data->period, |
1523 | efe75411 | ths | host_alarm_handler, |
1524 | efe75411 | ths | (DWORD)t, |
1525 | efe75411 | ths | TIME_ONESHOT | TIME_PERIODIC); |
1526 | efe75411 | ths | |
1527 | efe75411 | ths | if (!data->timerId) {
|
1528 | efe75411 | ths | perror("Failed to re-arm win32 alarm timer");
|
1529 | efe75411 | ths | |
1530 | efe75411 | ths | timeEndPeriod(data->period); |
1531 | efe75411 | ths | CloseHandle(data->host_alarm); |
1532 | efe75411 | ths | exit(1);
|
1533 | efe75411 | ths | } |
1534 | efe75411 | ths | } |
1535 | efe75411 | ths | |
1536 | c8994013 | ths | #endif /* _WIN32 */ |
1537 | c8994013 | ths | |
1538 | 1dce7c3c | bellard | static void init_timer_alarm(void) |
1539 | 8a7ddc38 | bellard | { |
1540 | c8994013 | ths | struct qemu_alarm_timer *t;
|
1541 | c8994013 | ths | int i, err = -1; |
1542 | c8994013 | ths | |
1543 | c8994013 | ths | for (i = 0; alarm_timers[i].name; i++) { |
1544 | c8994013 | ths | t = &alarm_timers[i]; |
1545 | c8994013 | ths | |
1546 | c8994013 | ths | err = t->start(t); |
1547 | c8994013 | ths | if (!err)
|
1548 | c8994013 | ths | break;
|
1549 | 67b915a5 | bellard | } |
1550 | fd872598 | bellard | |
1551 | c8994013 | ths | if (err) {
|
1552 | c8994013 | ths | fprintf(stderr, "Unable to find any suitable alarm timer.\n");
|
1553 | c8994013 | ths | fprintf(stderr, "Terminating\n");
|
1554 | c8994013 | ths | exit(1);
|
1555 | 67b915a5 | bellard | } |
1556 | c8994013 | ths | |
1557 | c8994013 | ths | alarm_timer = t; |
1558 | 8a7ddc38 | bellard | } |
1559 | 8a7ddc38 | bellard | |
1560 | 9596ebb7 | pbrook | static void quit_timers(void) |
1561 | 40c3bac3 | bellard | { |
1562 | c8994013 | ths | alarm_timer->stop(alarm_timer); |
1563 | c8994013 | ths | alarm_timer = NULL;
|
1564 | 40c3bac3 | bellard | } |
1565 | 40c3bac3 | bellard | |
1566 | c4b1fcc0 | bellard | /***********************************************************/
|
1567 | f6503059 | balrog | /* host time/date access */
|
1568 | f6503059 | balrog | void qemu_get_timedate(struct tm *tm, int offset) |
1569 | f6503059 | balrog | { |
1570 | f6503059 | balrog | time_t ti; |
1571 | f6503059 | balrog | struct tm *ret;
|
1572 | f6503059 | balrog | |
1573 | f6503059 | balrog | time(&ti); |
1574 | f6503059 | balrog | ti += offset; |
1575 | f6503059 | balrog | if (rtc_date_offset == -1) { |
1576 | f6503059 | balrog | if (rtc_utc)
|
1577 | f6503059 | balrog | ret = gmtime(&ti); |
1578 | f6503059 | balrog | else
|
1579 | f6503059 | balrog | ret = localtime(&ti); |
1580 | f6503059 | balrog | } else {
|
1581 | f6503059 | balrog | ti -= rtc_date_offset; |
1582 | f6503059 | balrog | ret = gmtime(&ti); |
1583 | f6503059 | balrog | } |
1584 | f6503059 | balrog | |
1585 | f6503059 | balrog | memcpy(tm, ret, sizeof(struct tm)); |
1586 | f6503059 | balrog | } |
1587 | f6503059 | balrog | |
1588 | f6503059 | balrog | int qemu_timedate_diff(struct tm *tm) |
1589 | f6503059 | balrog | { |
1590 | f6503059 | balrog | time_t seconds; |
1591 | f6503059 | balrog | |
1592 | f6503059 | balrog | if (rtc_date_offset == -1) |
1593 | f6503059 | balrog | if (rtc_utc)
|
1594 | f6503059 | balrog | seconds = mktimegm(tm); |
1595 | f6503059 | balrog | else
|
1596 | f6503059 | balrog | seconds = mktime(tm); |
1597 | f6503059 | balrog | else
|
1598 | f6503059 | balrog | seconds = mktimegm(tm) + rtc_date_offset; |
1599 | f6503059 | balrog | |
1600 | f6503059 | balrog | return seconds - time(NULL); |
1601 | f6503059 | balrog | } |
1602 | f6503059 | balrog | |
1603 | f6503059 | balrog | /***********************************************************/
|
1604 | 82c643ff | bellard | /* character device */
|
1605 | 313aa567 | bellard | |
1606 | e5b0bc44 | pbrook | static void qemu_chr_event(CharDriverState *s, int event) |
1607 | e5b0bc44 | pbrook | { |
1608 | e5b0bc44 | pbrook | if (!s->chr_event)
|
1609 | e5b0bc44 | pbrook | return;
|
1610 | e5b0bc44 | pbrook | s->chr_event(s->handler_opaque, event); |
1611 | e5b0bc44 | pbrook | } |
1612 | e5b0bc44 | pbrook | |
1613 | 86e94dea | ths | static void qemu_chr_reset_bh(void *opaque) |
1614 | 86e94dea | ths | { |
1615 | 86e94dea | ths | CharDriverState *s = opaque; |
1616 | e5b0bc44 | pbrook | qemu_chr_event(s, CHR_EVENT_RESET); |
1617 | 86e94dea | ths | qemu_bh_delete(s->bh); |
1618 | 86e94dea | ths | s->bh = NULL;
|
1619 | 86e94dea | ths | } |
1620 | 86e94dea | ths | |
1621 | 86e94dea | ths | void qemu_chr_reset(CharDriverState *s)
|
1622 | 86e94dea | ths | { |
1623 | 86e94dea | ths | if (s->bh == NULL) { |
1624 | 86e94dea | ths | s->bh = qemu_bh_new(qemu_chr_reset_bh, s); |
1625 | 86e94dea | ths | qemu_bh_schedule(s->bh); |
1626 | 86e94dea | ths | } |
1627 | 86e94dea | ths | } |
1628 | 86e94dea | ths | |
1629 | 82c643ff | bellard | int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
1630 | 82c643ff | bellard | { |
1631 | 82c643ff | bellard | return s->chr_write(s, buf, len);
|
1632 | 82c643ff | bellard | } |
1633 | 67b915a5 | bellard | |
1634 | e57a8c0e | bellard | int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg) |
1635 | f8d179e3 | bellard | { |
1636 | e57a8c0e | bellard | if (!s->chr_ioctl)
|
1637 | e57a8c0e | bellard | return -ENOTSUP;
|
1638 | e57a8c0e | bellard | return s->chr_ioctl(s, cmd, arg);
|
1639 | f8d179e3 | bellard | } |
1640 | f8d179e3 | bellard | |
1641 | e5b0bc44 | pbrook | int qemu_chr_can_read(CharDriverState *s)
|
1642 | e5b0bc44 | pbrook | { |
1643 | e5b0bc44 | pbrook | if (!s->chr_can_read)
|
1644 | e5b0bc44 | pbrook | return 0; |
1645 | e5b0bc44 | pbrook | return s->chr_can_read(s->handler_opaque);
|
1646 | e5b0bc44 | pbrook | } |
1647 | e5b0bc44 | pbrook | |
1648 | e5b0bc44 | pbrook | void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len) |
1649 | e5b0bc44 | pbrook | { |
1650 | e5b0bc44 | pbrook | s->chr_read(s->handler_opaque, buf, len); |
1651 | e5b0bc44 | pbrook | } |
1652 | e5b0bc44 | pbrook | |
1653 | bd9bdce6 | balrog | void qemu_chr_accept_input(CharDriverState *s)
|
1654 | bd9bdce6 | balrog | { |
1655 | bd9bdce6 | balrog | if (s->chr_accept_input)
|
1656 | bd9bdce6 | balrog | s->chr_accept_input(s); |
1657 | bd9bdce6 | balrog | } |
1658 | e5b0bc44 | pbrook | |
1659 | 82c643ff | bellard | void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
1660 | 67b915a5 | bellard | { |
1661 | 82c643ff | bellard | char buf[4096]; |
1662 | 82c643ff | bellard | va_list ap; |
1663 | 82c643ff | bellard | va_start(ap, fmt); |
1664 | 82c643ff | bellard | vsnprintf(buf, sizeof(buf), fmt, ap);
|
1665 | ffe8ab83 | ths | qemu_chr_write(s, (uint8_t *)buf, strlen(buf)); |
1666 | 82c643ff | bellard | va_end(ap); |
1667 | 67b915a5 | bellard | } |
1668 | 67b915a5 | bellard | |
1669 | 5905b2e5 | bellard | void qemu_chr_send_event(CharDriverState *s, int event) |
1670 | 5905b2e5 | bellard | { |
1671 | 5905b2e5 | bellard | if (s->chr_send_event)
|
1672 | 5905b2e5 | bellard | s->chr_send_event(s, event); |
1673 | 5905b2e5 | bellard | } |
1674 | 5905b2e5 | bellard | |
1675 | 5fafdf24 | ths | void qemu_chr_add_handlers(CharDriverState *s,
|
1676 | 5fafdf24 | ths | IOCanRWHandler *fd_can_read, |
1677 | e5b0bc44 | pbrook | IOReadHandler *fd_read, |
1678 | e5b0bc44 | pbrook | IOEventHandler *fd_event, |
1679 | e5b0bc44 | pbrook | void *opaque)
|
1680 | 82c643ff | bellard | { |
1681 | e5b0bc44 | pbrook | s->chr_can_read = fd_can_read; |
1682 | e5b0bc44 | pbrook | s->chr_read = fd_read; |
1683 | e5b0bc44 | pbrook | s->chr_event = fd_event; |
1684 | e5b0bc44 | pbrook | s->handler_opaque = opaque; |
1685 | e5b0bc44 | pbrook | if (s->chr_update_read_handler)
|
1686 | e5b0bc44 | pbrook | s->chr_update_read_handler(s); |
1687 | 82c643ff | bellard | } |
1688 | 3b46e624 | ths | |
1689 | 82c643ff | bellard | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1690 | 313aa567 | bellard | { |
1691 | 82c643ff | bellard | return len;
|
1692 | 82c643ff | bellard | } |
1693 | 82c643ff | bellard | |
1694 | 52f61fde | ths | static CharDriverState *qemu_chr_open_null(void) |
1695 | 82c643ff | bellard | { |
1696 | 82c643ff | bellard | CharDriverState *chr; |
1697 | 82c643ff | bellard | |
1698 | 82c643ff | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
1699 | 82c643ff | bellard | if (!chr)
|
1700 | 82c643ff | bellard | return NULL; |
1701 | 82c643ff | bellard | chr->chr_write = null_chr_write; |
1702 | 82c643ff | bellard | return chr;
|
1703 | 82c643ff | bellard | } |
1704 | 82c643ff | bellard | |
1705 | 20d8a3ed | ths | /* MUX driver for serial I/O splitting */
|
1706 | 20d8a3ed | ths | static int term_timestamps; |
1707 | 20d8a3ed | ths | static int64_t term_timestamps_start;
|
1708 | 9c1de612 | ths | #define MAX_MUX 4 |
1709 | bd9bdce6 | balrog | #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ |
1710 | bd9bdce6 | balrog | #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) |
1711 | 20d8a3ed | ths | typedef struct { |
1712 | 20d8a3ed | ths | IOCanRWHandler *chr_can_read[MAX_MUX]; |
1713 | 20d8a3ed | ths | IOReadHandler *chr_read[MAX_MUX]; |
1714 | 20d8a3ed | ths | IOEventHandler *chr_event[MAX_MUX]; |
1715 | 20d8a3ed | ths | void *ext_opaque[MAX_MUX];
|
1716 | 20d8a3ed | ths | CharDriverState *drv; |
1717 | bd9bdce6 | balrog | unsigned char buffer[MUX_BUFFER_SIZE]; |
1718 | bd9bdce6 | balrog | int prod;
|
1719 | bd9bdce6 | balrog | int cons;
|
1720 | 20d8a3ed | ths | int mux_cnt;
|
1721 | 20d8a3ed | ths | int term_got_escape;
|
1722 | 20d8a3ed | ths | int max_size;
|
1723 | 20d8a3ed | ths | } MuxDriver; |
1724 | 20d8a3ed | ths | |
1725 | 20d8a3ed | ths | |
1726 | 20d8a3ed | ths | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1727 | 20d8a3ed | ths | { |
1728 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
1729 | 20d8a3ed | ths | int ret;
|
1730 | 20d8a3ed | ths | if (!term_timestamps) {
|
1731 | 20d8a3ed | ths | ret = d->drv->chr_write(d->drv, buf, len); |
1732 | 20d8a3ed | ths | } else {
|
1733 | 20d8a3ed | ths | int i;
|
1734 | 20d8a3ed | ths | |
1735 | 20d8a3ed | ths | ret = 0;
|
1736 | 20d8a3ed | ths | for(i = 0; i < len; i++) { |
1737 | 20d8a3ed | ths | ret += d->drv->chr_write(d->drv, buf+i, 1);
|
1738 | 20d8a3ed | ths | if (buf[i] == '\n') { |
1739 | 20d8a3ed | ths | char buf1[64]; |
1740 | 20d8a3ed | ths | int64_t ti; |
1741 | 20d8a3ed | ths | int secs;
|
1742 | 20d8a3ed | ths | |
1743 | 20d8a3ed | ths | ti = get_clock(); |
1744 | 20d8a3ed | ths | if (term_timestamps_start == -1) |
1745 | 20d8a3ed | ths | term_timestamps_start = ti; |
1746 | 20d8a3ed | ths | ti -= term_timestamps_start; |
1747 | 20d8a3ed | ths | secs = ti / 1000000000;
|
1748 | 20d8a3ed | ths | snprintf(buf1, sizeof(buf1),
|
1749 | 20d8a3ed | ths | "[%02d:%02d:%02d.%03d] ",
|
1750 | 20d8a3ed | ths | secs / 3600,
|
1751 | 20d8a3ed | ths | (secs / 60) % 60, |
1752 | 20d8a3ed | ths | secs % 60,
|
1753 | 20d8a3ed | ths | (int)((ti / 1000000) % 1000)); |
1754 | ffe8ab83 | ths | d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
1755 | 20d8a3ed | ths | } |
1756 | 20d8a3ed | ths | } |
1757 | 20d8a3ed | ths | } |
1758 | 20d8a3ed | ths | return ret;
|
1759 | 20d8a3ed | ths | } |
1760 | 20d8a3ed | ths | |
1761 | 20d8a3ed | ths | static char *mux_help[] = { |
1762 | 20d8a3ed | ths | "% h print this help\n\r",
|
1763 | 20d8a3ed | ths | "% x exit emulator\n\r",
|
1764 | 20d8a3ed | ths | "% s save disk data back to file (if -snapshot)\n\r",
|
1765 | 20d8a3ed | ths | "% t toggle console timestamps\n\r"
|
1766 | 20d8a3ed | ths | "% b send break (magic sysrq)\n\r",
|
1767 | 20d8a3ed | ths | "% c switch between console and monitor\n\r",
|
1768 | 20d8a3ed | ths | "% % sends %\n\r",
|
1769 | 20d8a3ed | ths | NULL
|
1770 | 20d8a3ed | ths | }; |
1771 | 20d8a3ed | ths | |
1772 | 20d8a3ed | ths | static int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
1773 | 20d8a3ed | ths | static void mux_print_help(CharDriverState *chr) |
1774 | 20d8a3ed | ths | { |
1775 | 20d8a3ed | ths | int i, j;
|
1776 | 20d8a3ed | ths | char ebuf[15] = "Escape-Char"; |
1777 | 20d8a3ed | ths | char cbuf[50] = "\n\r"; |
1778 | 20d8a3ed | ths | |
1779 | 20d8a3ed | ths | if (term_escape_char > 0 && term_escape_char < 26) { |
1780 | 20d8a3ed | ths | sprintf(cbuf,"\n\r");
|
1781 | 20d8a3ed | ths | sprintf(ebuf,"C-%c", term_escape_char - 1 + 'a'); |
1782 | 20d8a3ed | ths | } else {
|
1783 | ffe8ab83 | ths | sprintf(cbuf,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
|
1784 | ffe8ab83 | ths | term_escape_char); |
1785 | 20d8a3ed | ths | } |
1786 | ffe8ab83 | ths | chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf)); |
1787 | 20d8a3ed | ths | for (i = 0; mux_help[i] != NULL; i++) { |
1788 | 20d8a3ed | ths | for (j=0; mux_help[i][j] != '\0'; j++) { |
1789 | 20d8a3ed | ths | if (mux_help[i][j] == '%') |
1790 | ffe8ab83 | ths | chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf)); |
1791 | 20d8a3ed | ths | else
|
1792 | ffe8ab83 | ths | chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
|
1793 | 20d8a3ed | ths | } |
1794 | 20d8a3ed | ths | } |
1795 | 20d8a3ed | ths | } |
1796 | 20d8a3ed | ths | |
1797 | 20d8a3ed | ths | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
1798 | 20d8a3ed | ths | { |
1799 | 20d8a3ed | ths | if (d->term_got_escape) {
|
1800 | 20d8a3ed | ths | d->term_got_escape = 0;
|
1801 | 20d8a3ed | ths | if (ch == term_escape_char)
|
1802 | 20d8a3ed | ths | goto send_char;
|
1803 | 20d8a3ed | ths | switch(ch) {
|
1804 | 20d8a3ed | ths | case '?': |
1805 | 20d8a3ed | ths | case 'h': |
1806 | 20d8a3ed | ths | mux_print_help(chr); |
1807 | 20d8a3ed | ths | break;
|
1808 | 20d8a3ed | ths | case 'x': |
1809 | 20d8a3ed | ths | { |
1810 | 20d8a3ed | ths | char *term = "QEMU: Terminated\n\r"; |
1811 | ffe8ab83 | ths | chr->chr_write(chr,(uint8_t *)term,strlen(term)); |
1812 | 20d8a3ed | ths | exit(0);
|
1813 | 20d8a3ed | ths | break;
|
1814 | 20d8a3ed | ths | } |
1815 | 20d8a3ed | ths | case 's': |
1816 | 20d8a3ed | ths | { |
1817 | 20d8a3ed | ths | int i;
|
1818 | e4bcb14c | ths | for (i = 0; i < nb_drives; i++) { |
1819 | e4bcb14c | ths | bdrv_commit(drives_table[i].bdrv); |
1820 | 20d8a3ed | ths | } |
1821 | 20d8a3ed | ths | } |
1822 | 20d8a3ed | ths | break;
|
1823 | 20d8a3ed | ths | case 'b': |
1824 | 36ddb83b | balrog | qemu_chr_event(chr, CHR_EVENT_BREAK); |
1825 | 20d8a3ed | ths | break;
|
1826 | 20d8a3ed | ths | case 'c': |
1827 | 20d8a3ed | ths | /* Switch to the next registered device */
|
1828 | 20d8a3ed | ths | chr->focus++; |
1829 | 20d8a3ed | ths | if (chr->focus >= d->mux_cnt)
|
1830 | 20d8a3ed | ths | chr->focus = 0;
|
1831 | 20d8a3ed | ths | break;
|
1832 | 20d8a3ed | ths | case 't': |
1833 | 20d8a3ed | ths | term_timestamps = !term_timestamps; |
1834 | 20d8a3ed | ths | term_timestamps_start = -1;
|
1835 | 20d8a3ed | ths | break;
|
1836 | 20d8a3ed | ths | } |
1837 | 20d8a3ed | ths | } else if (ch == term_escape_char) { |
1838 | 20d8a3ed | ths | d->term_got_escape = 1;
|
1839 | 20d8a3ed | ths | } else {
|
1840 | 20d8a3ed | ths | send_char:
|
1841 | 20d8a3ed | ths | return 1; |
1842 | 20d8a3ed | ths | } |
1843 | 20d8a3ed | ths | return 0; |
1844 | 20d8a3ed | ths | } |
1845 | 20d8a3ed | ths | |
1846 | bd9bdce6 | balrog | static void mux_chr_accept_input(CharDriverState *chr) |
1847 | bd9bdce6 | balrog | { |
1848 | bd9bdce6 | balrog | int m = chr->focus;
|
1849 | bd9bdce6 | balrog | MuxDriver *d = chr->opaque; |
1850 | bd9bdce6 | balrog | |
1851 | bd9bdce6 | balrog | while (d->prod != d->cons &&
|
1852 | bd9bdce6 | balrog | d->chr_can_read[m] && |
1853 | bd9bdce6 | balrog | d->chr_can_read[m](d->ext_opaque[m])) { |
1854 | bd9bdce6 | balrog | d->chr_read[m](d->ext_opaque[m], |
1855 | bd9bdce6 | balrog | &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
|
1856 | bd9bdce6 | balrog | } |
1857 | bd9bdce6 | balrog | } |
1858 | bd9bdce6 | balrog | |
1859 | 20d8a3ed | ths | static int mux_chr_can_read(void *opaque) |
1860 | 20d8a3ed | ths | { |
1861 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
1862 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
1863 | bd9bdce6 | balrog | |
1864 | bd9bdce6 | balrog | if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
|
1865 | bd9bdce6 | balrog | return 1; |
1866 | 20d8a3ed | ths | if (d->chr_can_read[chr->focus])
|
1867 | bd9bdce6 | balrog | return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
|
1868 | 20d8a3ed | ths | return 0; |
1869 | 20d8a3ed | ths | } |
1870 | 20d8a3ed | ths | |
1871 | 20d8a3ed | ths | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
1872 | 20d8a3ed | ths | { |
1873 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
1874 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
1875 | bd9bdce6 | balrog | int m = chr->focus;
|
1876 | 20d8a3ed | ths | int i;
|
1877 | bd9bdce6 | balrog | |
1878 | bd9bdce6 | balrog | mux_chr_accept_input (opaque); |
1879 | bd9bdce6 | balrog | |
1880 | 20d8a3ed | ths | for(i = 0; i < size; i++) |
1881 | bd9bdce6 | balrog | if (mux_proc_byte(chr, d, buf[i])) {
|
1882 | bd9bdce6 | balrog | if (d->prod == d->cons &&
|
1883 | bd9bdce6 | balrog | d->chr_can_read[m] && |
1884 | bd9bdce6 | balrog | d->chr_can_read[m](d->ext_opaque[m])) |
1885 | bd9bdce6 | balrog | d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
|
1886 | bd9bdce6 | balrog | else
|
1887 | bd9bdce6 | balrog | d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i]; |
1888 | bd9bdce6 | balrog | } |
1889 | 20d8a3ed | ths | } |
1890 | 20d8a3ed | ths | |
1891 | 20d8a3ed | ths | static void mux_chr_event(void *opaque, int event) |
1892 | 20d8a3ed | ths | { |
1893 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
1894 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
1895 | 20d8a3ed | ths | int i;
|
1896 | 20d8a3ed | ths | |
1897 | 20d8a3ed | ths | /* Send the event to all registered listeners */
|
1898 | 20d8a3ed | ths | for (i = 0; i < d->mux_cnt; i++) |
1899 | 20d8a3ed | ths | if (d->chr_event[i])
|
1900 | 20d8a3ed | ths | d->chr_event[i](d->ext_opaque[i], event); |
1901 | 20d8a3ed | ths | } |
1902 | 20d8a3ed | ths | |
1903 | 20d8a3ed | ths | static void mux_chr_update_read_handler(CharDriverState *chr) |
1904 | 20d8a3ed | ths | { |
1905 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
1906 | 20d8a3ed | ths | |
1907 | 20d8a3ed | ths | if (d->mux_cnt >= MAX_MUX) {
|
1908 | 20d8a3ed | ths | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
|
1909 | 20d8a3ed | ths | return;
|
1910 | 20d8a3ed | ths | } |
1911 | 20d8a3ed | ths | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
1912 | 20d8a3ed | ths | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
1913 | 20d8a3ed | ths | d->chr_read[d->mux_cnt] = chr->chr_read; |
1914 | 20d8a3ed | ths | d->chr_event[d->mux_cnt] = chr->chr_event; |
1915 | 20d8a3ed | ths | /* Fix up the real driver with mux routines */
|
1916 | 20d8a3ed | ths | if (d->mux_cnt == 0) { |
1917 | 20d8a3ed | ths | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
1918 | 20d8a3ed | ths | mux_chr_event, chr); |
1919 | 20d8a3ed | ths | } |
1920 | 20d8a3ed | ths | chr->focus = d->mux_cnt; |
1921 | 20d8a3ed | ths | d->mux_cnt++; |
1922 | 20d8a3ed | ths | } |
1923 | 20d8a3ed | ths | |
1924 | 9596ebb7 | pbrook | static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
|
1925 | 20d8a3ed | ths | { |
1926 | 20d8a3ed | ths | CharDriverState *chr; |
1927 | 20d8a3ed | ths | MuxDriver *d; |
1928 | 20d8a3ed | ths | |
1929 | 20d8a3ed | ths | chr = qemu_mallocz(sizeof(CharDriverState));
|
1930 | 20d8a3ed | ths | if (!chr)
|
1931 | 20d8a3ed | ths | return NULL; |
1932 | 20d8a3ed | ths | d = qemu_mallocz(sizeof(MuxDriver));
|
1933 | 20d8a3ed | ths | if (!d) {
|
1934 | 20d8a3ed | ths | free(chr); |
1935 | 20d8a3ed | ths | return NULL; |
1936 | 20d8a3ed | ths | } |
1937 | 20d8a3ed | ths | |
1938 | 20d8a3ed | ths | chr->opaque = d; |
1939 | 20d8a3ed | ths | d->drv = drv; |
1940 | 20d8a3ed | ths | chr->focus = -1;
|
1941 | 20d8a3ed | ths | chr->chr_write = mux_chr_write; |
1942 | 20d8a3ed | ths | chr->chr_update_read_handler = mux_chr_update_read_handler; |
1943 | bd9bdce6 | balrog | chr->chr_accept_input = mux_chr_accept_input; |
1944 | 20d8a3ed | ths | return chr;
|
1945 | 20d8a3ed | ths | } |
1946 | 20d8a3ed | ths | |
1947 | 20d8a3ed | ths | |
1948 | fd1dff4b | bellard | #ifdef _WIN32
|
1949 | 82c643ff | bellard | |
1950 | fd1dff4b | bellard | static void socket_cleanup(void) |
1951 | fd1dff4b | bellard | { |
1952 | fd1dff4b | bellard | WSACleanup(); |
1953 | fd1dff4b | bellard | } |
1954 | 82c643ff | bellard | |
1955 | fd1dff4b | bellard | static int socket_init(void) |
1956 | fd1dff4b | bellard | { |
1957 | fd1dff4b | bellard | WSADATA Data; |
1958 | fd1dff4b | bellard | int ret, err;
|
1959 | fd1dff4b | bellard | |
1960 | fd1dff4b | bellard | ret = WSAStartup(MAKEWORD(2,2), &Data); |
1961 | fd1dff4b | bellard | if (ret != 0) { |
1962 | fd1dff4b | bellard | err = WSAGetLastError(); |
1963 | fd1dff4b | bellard | fprintf(stderr, "WSAStartup: %d\n", err);
|
1964 | fd1dff4b | bellard | return -1; |
1965 | fd1dff4b | bellard | } |
1966 | fd1dff4b | bellard | atexit(socket_cleanup); |
1967 | fd1dff4b | bellard | return 0; |
1968 | fd1dff4b | bellard | } |
1969 | fd1dff4b | bellard | |
1970 | fd1dff4b | bellard | static int send_all(int fd, const uint8_t *buf, int len1) |
1971 | fd1dff4b | bellard | { |
1972 | fd1dff4b | bellard | int ret, len;
|
1973 | 3b46e624 | ths | |
1974 | fd1dff4b | bellard | len = len1; |
1975 | fd1dff4b | bellard | while (len > 0) { |
1976 | fd1dff4b | bellard | ret = send(fd, buf, len, 0);
|
1977 | fd1dff4b | bellard | if (ret < 0) { |
1978 | fd1dff4b | bellard | int errno;
|
1979 | fd1dff4b | bellard | errno = WSAGetLastError(); |
1980 | fd1dff4b | bellard | if (errno != WSAEWOULDBLOCK) {
|
1981 | fd1dff4b | bellard | return -1; |
1982 | fd1dff4b | bellard | } |
1983 | fd1dff4b | bellard | } else if (ret == 0) { |
1984 | fd1dff4b | bellard | break;
|
1985 | fd1dff4b | bellard | } else {
|
1986 | fd1dff4b | bellard | buf += ret; |
1987 | fd1dff4b | bellard | len -= ret; |
1988 | fd1dff4b | bellard | } |
1989 | fd1dff4b | bellard | } |
1990 | fd1dff4b | bellard | return len1 - len;
|
1991 | fd1dff4b | bellard | } |
1992 | fd1dff4b | bellard | |
1993 | fd1dff4b | bellard | void socket_set_nonblock(int fd) |
1994 | fd1dff4b | bellard | { |
1995 | fd1dff4b | bellard | unsigned long opt = 1; |
1996 | fd1dff4b | bellard | ioctlsocket(fd, FIONBIO, &opt); |
1997 | fd1dff4b | bellard | } |
1998 | fd1dff4b | bellard | |
1999 | fd1dff4b | bellard | #else
|
2000 | fd1dff4b | bellard | |
2001 | 1d96905d | bellard | static int unix_write(int fd, const uint8_t *buf, int len1) |
2002 | 1d96905d | bellard | { |
2003 | 1d96905d | bellard | int ret, len;
|
2004 | 1d96905d | bellard | |
2005 | 1d96905d | bellard | len = len1; |
2006 | 1d96905d | bellard | while (len > 0) { |
2007 | 1d96905d | bellard | ret = write(fd, buf, len); |
2008 | 1d96905d | bellard | if (ret < 0) { |
2009 | 1d96905d | bellard | if (errno != EINTR && errno != EAGAIN)
|
2010 | 1d96905d | bellard | return -1; |
2011 | 1d96905d | bellard | } else if (ret == 0) { |
2012 | 1d96905d | bellard | break;
|
2013 | 1d96905d | bellard | } else {
|
2014 | 1d96905d | bellard | buf += ret; |
2015 | 1d96905d | bellard | len -= ret; |
2016 | 1d96905d | bellard | } |
2017 | 1d96905d | bellard | } |
2018 | 1d96905d | bellard | return len1 - len;
|
2019 | 1d96905d | bellard | } |
2020 | 1d96905d | bellard | |
2021 | fd1dff4b | bellard | static inline int send_all(int fd, const uint8_t *buf, int len1) |
2022 | fd1dff4b | bellard | { |
2023 | fd1dff4b | bellard | return unix_write(fd, buf, len1);
|
2024 | fd1dff4b | bellard | } |
2025 | fd1dff4b | bellard | |
2026 | fd1dff4b | bellard | void socket_set_nonblock(int fd) |
2027 | fd1dff4b | bellard | { |
2028 | fd1dff4b | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
2029 | fd1dff4b | bellard | } |
2030 | fd1dff4b | bellard | #endif /* !_WIN32 */ |
2031 | fd1dff4b | bellard | |
2032 | fd1dff4b | bellard | #ifndef _WIN32
|
2033 | fd1dff4b | bellard | |
2034 | fd1dff4b | bellard | typedef struct { |
2035 | fd1dff4b | bellard | int fd_in, fd_out;
|
2036 | fd1dff4b | bellard | int max_size;
|
2037 | fd1dff4b | bellard | } FDCharDriver; |
2038 | fd1dff4b | bellard | |
2039 | 20d8a3ed | ths | #define STDIO_MAX_CLIENTS 1 |
2040 | 20d8a3ed | ths | static int stdio_nb_clients = 0; |
2041 | fd1dff4b | bellard | |
2042 | 82c643ff | bellard | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
2043 | 82c643ff | bellard | { |
2044 | 82c643ff | bellard | FDCharDriver *s = chr->opaque; |
2045 | 1d96905d | bellard | return unix_write(s->fd_out, buf, len);
|
2046 | 82c643ff | bellard | } |
2047 | 82c643ff | bellard | |
2048 | 7c9d8e07 | bellard | static int fd_chr_read_poll(void *opaque) |
2049 | 7c9d8e07 | bellard | { |
2050 | 7c9d8e07 | bellard | CharDriverState *chr = opaque; |
2051 | 7c9d8e07 | bellard | FDCharDriver *s = chr->opaque; |
2052 | 7c9d8e07 | bellard | |
2053 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
2054 | 7c9d8e07 | bellard | return s->max_size;
|
2055 | 7c9d8e07 | bellard | } |
2056 | 7c9d8e07 | bellard | |
2057 | 7c9d8e07 | bellard | static void fd_chr_read(void *opaque) |
2058 | 7c9d8e07 | bellard | { |
2059 | 7c9d8e07 | bellard | CharDriverState *chr = opaque; |
2060 | 7c9d8e07 | bellard | FDCharDriver *s = chr->opaque; |
2061 | 7c9d8e07 | bellard | int size, len;
|
2062 | 7c9d8e07 | bellard | uint8_t buf[1024];
|
2063 | 3b46e624 | ths | |
2064 | 7c9d8e07 | bellard | len = sizeof(buf);
|
2065 | 7c9d8e07 | bellard | if (len > s->max_size)
|
2066 | 7c9d8e07 | bellard | len = s->max_size; |
2067 | 7c9d8e07 | bellard | if (len == 0) |
2068 | 7c9d8e07 | bellard | return;
|
2069 | 7c9d8e07 | bellard | size = read(s->fd_in, buf, len); |
2070 | 188157fe | pbrook | if (size == 0) { |
2071 | 188157fe | pbrook | /* FD has been closed. Remove it from the active list. */
|
2072 | 188157fe | pbrook | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
2073 | 188157fe | pbrook | return;
|
2074 | 188157fe | pbrook | } |
2075 | 7c9d8e07 | bellard | if (size > 0) { |
2076 | e5b0bc44 | pbrook | qemu_chr_read(chr, buf, size); |
2077 | 7c9d8e07 | bellard | } |
2078 | 7c9d8e07 | bellard | } |
2079 | 7c9d8e07 | bellard | |
2080 | e5b0bc44 | pbrook | static void fd_chr_update_read_handler(CharDriverState *chr) |
2081 | 82c643ff | bellard | { |
2082 | 82c643ff | bellard | FDCharDriver *s = chr->opaque; |
2083 | 82c643ff | bellard | |
2084 | f8d179e3 | bellard | if (s->fd_in >= 0) { |
2085 | f8d179e3 | bellard | if (nographic && s->fd_in == 0) { |
2086 | f8d179e3 | bellard | } else {
|
2087 | 5fafdf24 | ths | qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, |
2088 | 7c9d8e07 | bellard | fd_chr_read, NULL, chr);
|
2089 | f8d179e3 | bellard | } |
2090 | 82c643ff | bellard | } |
2091 | 82c643ff | bellard | } |
2092 | 82c643ff | bellard | |
2093 | a11d070e | balrog | static void fd_chr_close(struct CharDriverState *chr) |
2094 | a11d070e | balrog | { |
2095 | a11d070e | balrog | FDCharDriver *s = chr->opaque; |
2096 | a11d070e | balrog | |
2097 | a11d070e | balrog | if (s->fd_in >= 0) { |
2098 | a11d070e | balrog | if (nographic && s->fd_in == 0) { |
2099 | a11d070e | balrog | } else {
|
2100 | a11d070e | balrog | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
2101 | a11d070e | balrog | } |
2102 | a11d070e | balrog | } |
2103 | a11d070e | balrog | |
2104 | a11d070e | balrog | qemu_free(s); |
2105 | a11d070e | balrog | } |
2106 | a11d070e | balrog | |
2107 | 82c643ff | bellard | /* open a character device to a unix fd */
|
2108 | 52f61fde | ths | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
2109 | 82c643ff | bellard | { |
2110 | 82c643ff | bellard | CharDriverState *chr; |
2111 | 82c643ff | bellard | FDCharDriver *s; |
2112 | 82c643ff | bellard | |
2113 | 82c643ff | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
2114 | 82c643ff | bellard | if (!chr)
|
2115 | 82c643ff | bellard | return NULL; |
2116 | 82c643ff | bellard | s = qemu_mallocz(sizeof(FDCharDriver));
|
2117 | 82c643ff | bellard | if (!s) {
|
2118 | 82c643ff | bellard | free(chr); |
2119 | 82c643ff | bellard | return NULL; |
2120 | 82c643ff | bellard | } |
2121 | 82c643ff | bellard | s->fd_in = fd_in; |
2122 | 82c643ff | bellard | s->fd_out = fd_out; |
2123 | 82c643ff | bellard | chr->opaque = s; |
2124 | 82c643ff | bellard | chr->chr_write = fd_chr_write; |
2125 | e5b0bc44 | pbrook | chr->chr_update_read_handler = fd_chr_update_read_handler; |
2126 | a11d070e | balrog | chr->chr_close = fd_chr_close; |
2127 | 86e94dea | ths | |
2128 | 86e94dea | ths | qemu_chr_reset(chr); |
2129 | 86e94dea | ths | |
2130 | 82c643ff | bellard | return chr;
|
2131 | 82c643ff | bellard | } |
2132 | 82c643ff | bellard | |
2133 | 52f61fde | ths | static CharDriverState *qemu_chr_open_file_out(const char *file_out) |
2134 | f8d179e3 | bellard | { |
2135 | f8d179e3 | bellard | int fd_out;
|
2136 | f8d179e3 | bellard | |
2137 | aeb30be6 | balrog | TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
|
2138 | f8d179e3 | bellard | if (fd_out < 0) |
2139 | f8d179e3 | bellard | return NULL; |
2140 | f8d179e3 | bellard | return qemu_chr_open_fd(-1, fd_out); |
2141 | f8d179e3 | bellard | } |
2142 | f8d179e3 | bellard | |
2143 | 52f61fde | ths | static CharDriverState *qemu_chr_open_pipe(const char *filename) |
2144 | f8d179e3 | bellard | { |
2145 | c26c1c4b | ths | int fd_in, fd_out;
|
2146 | c26c1c4b | ths | char filename_in[256], filename_out[256]; |
2147 | c26c1c4b | ths | |
2148 | c26c1c4b | ths | snprintf(filename_in, 256, "%s.in", filename); |
2149 | c26c1c4b | ths | snprintf(filename_out, 256, "%s.out", filename); |
2150 | aeb30be6 | balrog | TFR(fd_in = open(filename_in, O_RDWR | O_BINARY)); |
2151 | aeb30be6 | balrog | TFR(fd_out = open(filename_out, O_RDWR | O_BINARY)); |
2152 | c26c1c4b | ths | if (fd_in < 0 || fd_out < 0) { |
2153 | c26c1c4b | ths | if (fd_in >= 0) |
2154 | c26c1c4b | ths | close(fd_in); |
2155 | c26c1c4b | ths | if (fd_out >= 0) |
2156 | c26c1c4b | ths | close(fd_out); |
2157 | aeb30be6 | balrog | TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY)); |
2158 | c26c1c4b | ths | if (fd_in < 0) |
2159 | c26c1c4b | ths | return NULL; |
2160 | c26c1c4b | ths | } |
2161 | c26c1c4b | ths | return qemu_chr_open_fd(fd_in, fd_out);
|
2162 | f8d179e3 | bellard | } |
2163 | f8d179e3 | bellard | |
2164 | f8d179e3 | bellard | |
2165 | 82c643ff | bellard | /* for STDIO, we handle the case where several clients use it
|
2166 | 82c643ff | bellard | (nographic mode) */
|
2167 | 82c643ff | bellard | |
2168 | aa0bc6b6 | bellard | #define TERM_FIFO_MAX_SIZE 1 |
2169 | aa0bc6b6 | bellard | |
2170 | aa0bc6b6 | bellard | static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
|
2171 | 1dce7c3c | bellard | static int term_fifo_size; |
2172 | 330d0414 | bellard | |
2173 | 7c9d8e07 | bellard | static int stdio_read_poll(void *opaque) |
2174 | 82c643ff | bellard | { |
2175 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2176 | aa0bc6b6 | bellard | |
2177 | 20d8a3ed | ths | /* try to flush the queue if needed */
|
2178 | 20d8a3ed | ths | if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) { |
2179 | 20d8a3ed | ths | qemu_chr_read(chr, term_fifo, 1);
|
2180 | 20d8a3ed | ths | term_fifo_size = 0;
|
2181 | aa0bc6b6 | bellard | } |
2182 | 20d8a3ed | ths | /* see if we can absorb more chars */
|
2183 | 20d8a3ed | ths | if (term_fifo_size == 0) |
2184 | 20d8a3ed | ths | return 1; |
2185 | 20d8a3ed | ths | else
|
2186 | 20d8a3ed | ths | return 0; |
2187 | 82c643ff | bellard | } |
2188 | 82c643ff | bellard | |
2189 | 7c9d8e07 | bellard | static void stdio_read(void *opaque) |
2190 | 82c643ff | bellard | { |
2191 | 7c9d8e07 | bellard | int size;
|
2192 | 7c9d8e07 | bellard | uint8_t buf[1];
|
2193 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2194 | 20d8a3ed | ths | |
2195 | 7c9d8e07 | bellard | size = read(0, buf, 1); |
2196 | 519945df | pbrook | if (size == 0) { |
2197 | 519945df | pbrook | /* stdin has been closed. Remove it from the active list. */
|
2198 | 519945df | pbrook | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
2199 | 519945df | pbrook | return;
|
2200 | 519945df | pbrook | } |
2201 | 20d8a3ed | ths | if (size > 0) { |
2202 | 20d8a3ed | ths | if (qemu_chr_can_read(chr) > 0) { |
2203 | 20d8a3ed | ths | qemu_chr_read(chr, buf, 1);
|
2204 | 20d8a3ed | ths | } else if (term_fifo_size == 0) { |
2205 | 20d8a3ed | ths | term_fifo[term_fifo_size++] = buf[0];
|
2206 | 1dce7c3c | bellard | } |
2207 | 1dce7c3c | bellard | } |
2208 | 1dce7c3c | bellard | } |
2209 | 1dce7c3c | bellard | |
2210 | 8d11df9e | bellard | /* init terminal so that we can grab keys */
|
2211 | 8d11df9e | bellard | static struct termios oldtty; |
2212 | 8d11df9e | bellard | static int old_fd0_flags; |
2213 | a11d070e | balrog | static int term_atexit_done; |
2214 | 8d11df9e | bellard | |
2215 | 8d11df9e | bellard | static void term_exit(void) |
2216 | 8d11df9e | bellard | { |
2217 | 8d11df9e | bellard | tcsetattr (0, TCSANOW, &oldtty);
|
2218 | 8d11df9e | bellard | fcntl(0, F_SETFL, old_fd0_flags);
|
2219 | 8d11df9e | bellard | } |
2220 | 8d11df9e | bellard | |
2221 | 8d11df9e | bellard | static void term_init(void) |
2222 | 8d11df9e | bellard | { |
2223 | 8d11df9e | bellard | struct termios tty;
|
2224 | 8d11df9e | bellard | |
2225 | 8d11df9e | bellard | tcgetattr (0, &tty);
|
2226 | 8d11df9e | bellard | oldtty = tty; |
2227 | 8d11df9e | bellard | old_fd0_flags = fcntl(0, F_GETFL);
|
2228 | 8d11df9e | bellard | |
2229 | 8d11df9e | bellard | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
2230 | 8d11df9e | bellard | |INLCR|IGNCR|ICRNL|IXON); |
2231 | 8d11df9e | bellard | tty.c_oflag |= OPOST; |
2232 | 8d11df9e | bellard | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
2233 | 8d11df9e | bellard | /* if graphical mode, we allow Ctrl-C handling */
|
2234 | 8d11df9e | bellard | if (nographic)
|
2235 | 8d11df9e | bellard | tty.c_lflag &= ~ISIG; |
2236 | 8d11df9e | bellard | tty.c_cflag &= ~(CSIZE|PARENB); |
2237 | 8d11df9e | bellard | tty.c_cflag |= CS8; |
2238 | 8d11df9e | bellard | tty.c_cc[VMIN] = 1;
|
2239 | 8d11df9e | bellard | tty.c_cc[VTIME] = 0;
|
2240 | 3b46e624 | ths | |
2241 | 8d11df9e | bellard | tcsetattr (0, TCSANOW, &tty);
|
2242 | 8d11df9e | bellard | |
2243 | a11d070e | balrog | if (!term_atexit_done++)
|
2244 | a11d070e | balrog | atexit(term_exit); |
2245 | 8d11df9e | bellard | |
2246 | 8d11df9e | bellard | fcntl(0, F_SETFL, O_NONBLOCK);
|
2247 | 8d11df9e | bellard | } |
2248 | 8d11df9e | bellard | |
2249 | a11d070e | balrog | static void qemu_chr_close_stdio(struct CharDriverState *chr) |
2250 | a11d070e | balrog | { |
2251 | a11d070e | balrog | term_exit(); |
2252 | a11d070e | balrog | stdio_nb_clients--; |
2253 | a11d070e | balrog | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
2254 | a11d070e | balrog | fd_chr_close(chr); |
2255 | a11d070e | balrog | } |
2256 | a11d070e | balrog | |
2257 | 52f61fde | ths | static CharDriverState *qemu_chr_open_stdio(void) |
2258 | 82c643ff | bellard | { |
2259 | 82c643ff | bellard | CharDriverState *chr; |
2260 | 82c643ff | bellard | |
2261 | 20d8a3ed | ths | if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
|
2262 | 20d8a3ed | ths | return NULL; |
2263 | 20d8a3ed | ths | chr = qemu_chr_open_fd(0, 1); |
2264 | a11d070e | balrog | chr->chr_close = qemu_chr_close_stdio; |
2265 | 20d8a3ed | ths | qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr); |
2266 | 20d8a3ed | ths | stdio_nb_clients++; |
2267 | 20d8a3ed | ths | term_init(); |
2268 | 20d8a3ed | ths | |
2269 | 82c643ff | bellard | return chr;
|
2270 | 82c643ff | bellard | } |
2271 | 82c643ff | bellard | |
2272 | 64b7b733 | aurel32 | #ifdef __sun__
|
2273 | 64b7b733 | aurel32 | /* Once Solaris has openpty(), this is going to be removed. */
|
2274 | 64b7b733 | aurel32 | int openpty(int *amaster, int *aslave, char *name, |
2275 | 64b7b733 | aurel32 | struct termios *termp, struct winsize *winp) |
2276 | 64b7b733 | aurel32 | { |
2277 | 64b7b733 | aurel32 | const char *slave; |
2278 | 64b7b733 | aurel32 | int mfd = -1, sfd = -1; |
2279 | 64b7b733 | aurel32 | |
2280 | 64b7b733 | aurel32 | *amaster = *aslave = -1;
|
2281 | 64b7b733 | aurel32 | |
2282 | 64b7b733 | aurel32 | mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
2283 | 64b7b733 | aurel32 | if (mfd < 0) |
2284 | 64b7b733 | aurel32 | goto err;
|
2285 | 64b7b733 | aurel32 | |
2286 | 64b7b733 | aurel32 | if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) |
2287 | 64b7b733 | aurel32 | goto err;
|
2288 | 64b7b733 | aurel32 | |
2289 | 64b7b733 | aurel32 | if ((slave = ptsname(mfd)) == NULL) |
2290 | 64b7b733 | aurel32 | goto err;
|
2291 | 64b7b733 | aurel32 | |
2292 | 64b7b733 | aurel32 | if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) |
2293 | 64b7b733 | aurel32 | goto err;
|
2294 | 64b7b733 | aurel32 | |
2295 | 64b7b733 | aurel32 | if (ioctl(sfd, I_PUSH, "ptem") == -1 || |
2296 | 64b7b733 | aurel32 | (termp != NULL && tcgetattr(sfd, termp) < 0)) |
2297 | 64b7b733 | aurel32 | goto err;
|
2298 | 64b7b733 | aurel32 | |
2299 | 64b7b733 | aurel32 | if (amaster)
|
2300 | 64b7b733 | aurel32 | *amaster = mfd; |
2301 | 64b7b733 | aurel32 | if (aslave)
|
2302 | 64b7b733 | aurel32 | *aslave = sfd; |
2303 | 64b7b733 | aurel32 | if (winp)
|
2304 | 64b7b733 | aurel32 | ioctl(sfd, TIOCSWINSZ, winp); |
2305 | 64b7b733 | aurel32 | |
2306 | 64b7b733 | aurel32 | return 0; |
2307 | 64b7b733 | aurel32 | |
2308 | 64b7b733 | aurel32 | err:
|
2309 | 64b7b733 | aurel32 | if (sfd != -1) |
2310 | 64b7b733 | aurel32 | close(sfd); |
2311 | 64b7b733 | aurel32 | close(mfd); |
2312 | 64b7b733 | aurel32 | return -1; |
2313 | 64b7b733 | aurel32 | } |
2314 | 64b7b733 | aurel32 | |
2315 | 64b7b733 | aurel32 | void cfmakeraw (struct termios *termios_p) |
2316 | 64b7b733 | aurel32 | { |
2317 | 64b7b733 | aurel32 | termios_p->c_iflag &= |
2318 | 64b7b733 | aurel32 | ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); |
2319 | 64b7b733 | aurel32 | termios_p->c_oflag &= ~OPOST; |
2320 | 64b7b733 | aurel32 | termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); |
2321 | 64b7b733 | aurel32 | termios_p->c_cflag &= ~(CSIZE|PARENB); |
2322 | 64b7b733 | aurel32 | termios_p->c_cflag |= CS8; |
2323 | 64b7b733 | aurel32 | |
2324 | 64b7b733 | aurel32 | termios_p->c_cc[VMIN] = 0;
|
2325 | 64b7b733 | aurel32 | termios_p->c_cc[VTIME] = 0;
|
2326 | 64b7b733 | aurel32 | } |
2327 | 64b7b733 | aurel32 | #endif
|
2328 | 64b7b733 | aurel32 | |
2329 | aec62507 | ths | #if defined(__linux__) || defined(__sun__)
|
2330 | 52f61fde | ths | static CharDriverState *qemu_chr_open_pty(void) |
2331 | 82c643ff | bellard | { |
2332 | 91fc2119 | bellard | struct termios tty;
|
2333 | 82c643ff | bellard | int master_fd, slave_fd;
|
2334 | 3b46e624 | ths | |
2335 | 64b7b733 | aurel32 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) < 0) { |
2336 | 82c643ff | bellard | return NULL; |
2337 | 82c643ff | bellard | } |
2338 | 3b46e624 | ths | |
2339 | 64b7b733 | aurel32 | /* Set raw attributes on the pty. */
|
2340 | 64b7b733 | aurel32 | cfmakeraw(&tty); |
2341 | 64b7b733 | aurel32 | tcsetattr(slave_fd, TCSAFLUSH, &tty); |
2342 | 91fc2119 | bellard | |
2343 | 64b7b733 | aurel32 | fprintf(stderr, "char device redirected to %s\n", ptsname(master_fd));
|
2344 | 82c643ff | bellard | return qemu_chr_open_fd(master_fd, master_fd);
|
2345 | 82c643ff | bellard | } |
2346 | f8d179e3 | bellard | |
2347 | 5fafdf24 | ths | static void tty_serial_init(int fd, int speed, |
2348 | f8d179e3 | bellard | int parity, int data_bits, int stop_bits) |
2349 | f8d179e3 | bellard | { |
2350 | f8d179e3 | bellard | struct termios tty;
|
2351 | f8d179e3 | bellard | speed_t spd; |
2352 | f8d179e3 | bellard | |
2353 | e57a8c0e | bellard | #if 0
|
2354 | 5fafdf24 | ths | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
|
2355 | e57a8c0e | bellard | speed, parity, data_bits, stop_bits);
|
2356 | e57a8c0e | bellard | #endif
|
2357 | e57a8c0e | bellard | tcgetattr (fd, &tty); |
2358 | f8d179e3 | bellard | |
2359 | a7954218 | balrog | #define MARGIN 1.1 |
2360 | a7954218 | balrog | if (speed <= 50 * MARGIN) |
2361 | f8d179e3 | bellard | spd = B50; |
2362 | a7954218 | balrog | else if (speed <= 75 * MARGIN) |
2363 | f8d179e3 | bellard | spd = B75; |
2364 | a7954218 | balrog | else if (speed <= 300 * MARGIN) |
2365 | f8d179e3 | bellard | spd = B300; |
2366 | a7954218 | balrog | else if (speed <= 600 * MARGIN) |
2367 | f8d179e3 | bellard | spd = B600; |
2368 | a7954218 | balrog | else if (speed <= 1200 * MARGIN) |
2369 | f8d179e3 | bellard | spd = B1200; |
2370 | a7954218 | balrog | else if (speed <= 2400 * MARGIN) |
2371 | f8d179e3 | bellard | spd = B2400; |
2372 | a7954218 | balrog | else if (speed <= 4800 * MARGIN) |
2373 | f8d179e3 | bellard | spd = B4800; |
2374 | a7954218 | balrog | else if (speed <= 9600 * MARGIN) |
2375 | f8d179e3 | bellard | spd = B9600; |
2376 | a7954218 | balrog | else if (speed <= 19200 * MARGIN) |
2377 | f8d179e3 | bellard | spd = B19200; |
2378 | a7954218 | balrog | else if (speed <= 38400 * MARGIN) |
2379 | f8d179e3 | bellard | spd = B38400; |
2380 | a7954218 | balrog | else if (speed <= 57600 * MARGIN) |
2381 | f8d179e3 | bellard | spd = B57600; |
2382 | a7954218 | balrog | else if (speed <= 115200 * MARGIN) |
2383 | a7954218 | balrog | spd = B115200; |
2384 | a7954218 | balrog | else
|
2385 | f8d179e3 | bellard | spd = B115200; |
2386 | f8d179e3 | bellard | |
2387 | f8d179e3 | bellard | cfsetispeed(&tty, spd); |
2388 | f8d179e3 | bellard | cfsetospeed(&tty, spd); |
2389 | f8d179e3 | bellard | |
2390 | f8d179e3 | bellard | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
2391 | f8d179e3 | bellard | |INLCR|IGNCR|ICRNL|IXON); |
2392 | f8d179e3 | bellard | tty.c_oflag |= OPOST; |
2393 | f8d179e3 | bellard | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
2394 | 094eed6c | bellard | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
2395 | f8d179e3 | bellard | switch(data_bits) {
|
2396 | f8d179e3 | bellard | default:
|
2397 | f8d179e3 | bellard | case 8: |
2398 | f8d179e3 | bellard | tty.c_cflag |= CS8; |
2399 | f8d179e3 | bellard | break;
|
2400 | f8d179e3 | bellard | case 7: |
2401 | f8d179e3 | bellard | tty.c_cflag |= CS7; |
2402 | f8d179e3 | bellard | break;
|
2403 | f8d179e3 | bellard | case 6: |
2404 | f8d179e3 | bellard | tty.c_cflag |= CS6; |
2405 | f8d179e3 | bellard | break;
|
2406 | f8d179e3 | bellard | case 5: |
2407 | f8d179e3 | bellard | tty.c_cflag |= CS5; |
2408 | f8d179e3 | bellard | break;
|
2409 | f8d179e3 | bellard | } |
2410 | f8d179e3 | bellard | switch(parity) {
|
2411 | f8d179e3 | bellard | default:
|
2412 | f8d179e3 | bellard | case 'N': |
2413 | f8d179e3 | bellard | break;
|
2414 | f8d179e3 | bellard | case 'E': |
2415 | f8d179e3 | bellard | tty.c_cflag |= PARENB; |
2416 | f8d179e3 | bellard | break;
|
2417 | f8d179e3 | bellard | case 'O': |
2418 | f8d179e3 | bellard | tty.c_cflag |= PARENB | PARODD; |
2419 | f8d179e3 | bellard | break;
|
2420 | f8d179e3 | bellard | } |
2421 | 094eed6c | bellard | if (stop_bits == 2) |
2422 | 094eed6c | bellard | tty.c_cflag |= CSTOPB; |
2423 | 3b46e624 | ths | |
2424 | f8d179e3 | bellard | tcsetattr (fd, TCSANOW, &tty); |
2425 | f8d179e3 | bellard | } |
2426 | f8d179e3 | bellard | |
2427 | e57a8c0e | bellard | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
2428 | f8d179e3 | bellard | { |
2429 | f8d179e3 | bellard | FDCharDriver *s = chr->opaque; |
2430 | 3b46e624 | ths | |
2431 | e57a8c0e | bellard | switch(cmd) {
|
2432 | e57a8c0e | bellard | case CHR_IOCTL_SERIAL_SET_PARAMS:
|
2433 | e57a8c0e | bellard | { |
2434 | e57a8c0e | bellard | QEMUSerialSetParams *ssp = arg; |
2435 | 5fafdf24 | ths | tty_serial_init(s->fd_in, ssp->speed, ssp->parity, |
2436 | e57a8c0e | bellard | ssp->data_bits, ssp->stop_bits); |
2437 | e57a8c0e | bellard | } |
2438 | e57a8c0e | bellard | break;
|
2439 | e57a8c0e | bellard | case CHR_IOCTL_SERIAL_SET_BREAK:
|
2440 | e57a8c0e | bellard | { |
2441 | e57a8c0e | bellard | int enable = *(int *)arg; |
2442 | e57a8c0e | bellard | if (enable)
|
2443 | e57a8c0e | bellard | tcsendbreak(s->fd_in, 1);
|
2444 | e57a8c0e | bellard | } |
2445 | e57a8c0e | bellard | break;
|
2446 | e57a8c0e | bellard | default:
|
2447 | e57a8c0e | bellard | return -ENOTSUP;
|
2448 | e57a8c0e | bellard | } |
2449 | e57a8c0e | bellard | return 0; |
2450 | f8d179e3 | bellard | } |
2451 | f8d179e3 | bellard | |
2452 | 52f61fde | ths | static CharDriverState *qemu_chr_open_tty(const char *filename) |
2453 | f8d179e3 | bellard | { |
2454 | f8d179e3 | bellard | CharDriverState *chr; |
2455 | f8d179e3 | bellard | int fd;
|
2456 | f8d179e3 | bellard | |
2457 | aeb30be6 | balrog | TFR(fd = open(filename, O_RDWR | O_NONBLOCK)); |
2458 | f8d179e3 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
2459 | f8d179e3 | bellard | tty_serial_init(fd, 115200, 'N', 8, 1); |
2460 | f8d179e3 | bellard | chr = qemu_chr_open_fd(fd, fd); |
2461 | aeb30be6 | balrog | if (!chr) {
|
2462 | aeb30be6 | balrog | close(fd); |
2463 | f8d179e3 | bellard | return NULL; |
2464 | aeb30be6 | balrog | } |
2465 | e57a8c0e | bellard | chr->chr_ioctl = tty_serial_ioctl; |
2466 | 86e94dea | ths | qemu_chr_reset(chr); |
2467 | e57a8c0e | bellard | return chr;
|
2468 | e57a8c0e | bellard | } |
2469 | aec62507 | ths | #else /* ! __linux__ && ! __sun__ */ |
2470 | aec62507 | ths | static CharDriverState *qemu_chr_open_pty(void) |
2471 | aec62507 | ths | { |
2472 | aec62507 | ths | return NULL; |
2473 | aec62507 | ths | } |
2474 | aec62507 | ths | #endif /* __linux__ || __sun__ */ |
2475 | e57a8c0e | bellard | |
2476 | aec62507 | ths | #if defined(__linux__)
|
2477 | 5867c88a | ths | typedef struct { |
2478 | 5867c88a | ths | int fd;
|
2479 | 5867c88a | ths | int mode;
|
2480 | 5867c88a | ths | } ParallelCharDriver; |
2481 | 5867c88a | ths | |
2482 | 5867c88a | ths | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
2483 | 5867c88a | ths | { |
2484 | 5867c88a | ths | if (s->mode != mode) {
|
2485 | 5867c88a | ths | int m = mode;
|
2486 | 5867c88a | ths | if (ioctl(s->fd, PPSETMODE, &m) < 0) |
2487 | 5867c88a | ths | return 0; |
2488 | 5867c88a | ths | s->mode = mode; |
2489 | 5867c88a | ths | } |
2490 | 5867c88a | ths | return 1; |
2491 | 5867c88a | ths | } |
2492 | 5867c88a | ths | |
2493 | e57a8c0e | bellard | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
2494 | e57a8c0e | bellard | { |
2495 | 5867c88a | ths | ParallelCharDriver *drv = chr->opaque; |
2496 | 5867c88a | ths | int fd = drv->fd;
|
2497 | e57a8c0e | bellard | uint8_t b; |
2498 | e57a8c0e | bellard | |
2499 | e57a8c0e | bellard | switch(cmd) {
|
2500 | e57a8c0e | bellard | case CHR_IOCTL_PP_READ_DATA:
|
2501 | e57a8c0e | bellard | if (ioctl(fd, PPRDATA, &b) < 0) |
2502 | e57a8c0e | bellard | return -ENOTSUP;
|
2503 | e57a8c0e | bellard | *(uint8_t *)arg = b; |
2504 | e57a8c0e | bellard | break;
|
2505 | e57a8c0e | bellard | case CHR_IOCTL_PP_WRITE_DATA:
|
2506 | e57a8c0e | bellard | b = *(uint8_t *)arg; |
2507 | e57a8c0e | bellard | if (ioctl(fd, PPWDATA, &b) < 0) |
2508 | e57a8c0e | bellard | return -ENOTSUP;
|
2509 | e57a8c0e | bellard | break;
|
2510 | e57a8c0e | bellard | case CHR_IOCTL_PP_READ_CONTROL:
|
2511 | e57a8c0e | bellard | if (ioctl(fd, PPRCONTROL, &b) < 0) |
2512 | e57a8c0e | bellard | return -ENOTSUP;
|
2513 | 5867c88a | ths | /* Linux gives only the lowest bits, and no way to know data
|
2514 | 5867c88a | ths | direction! For better compatibility set the fixed upper
|
2515 | 5867c88a | ths | bits. */
|
2516 | 5867c88a | ths | *(uint8_t *)arg = b | 0xc0;
|
2517 | e57a8c0e | bellard | break;
|
2518 | e57a8c0e | bellard | case CHR_IOCTL_PP_WRITE_CONTROL:
|
2519 | e57a8c0e | bellard | b = *(uint8_t *)arg; |
2520 | e57a8c0e | bellard | if (ioctl(fd, PPWCONTROL, &b) < 0) |
2521 | e57a8c0e | bellard | return -ENOTSUP;
|
2522 | e57a8c0e | bellard | break;
|
2523 | e57a8c0e | bellard | case CHR_IOCTL_PP_READ_STATUS:
|
2524 | e57a8c0e | bellard | if (ioctl(fd, PPRSTATUS, &b) < 0) |
2525 | e57a8c0e | bellard | return -ENOTSUP;
|
2526 | e57a8c0e | bellard | *(uint8_t *)arg = b; |
2527 | e57a8c0e | bellard | break;
|
2528 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_READ_ADDR:
|
2529 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
2530 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2531 | 5867c88a | ths | int n = read(fd, parg->buffer, parg->count);
|
2532 | 5867c88a | ths | if (n != parg->count) {
|
2533 | 5867c88a | ths | return -EIO;
|
2534 | 5867c88a | ths | } |
2535 | 5867c88a | ths | } |
2536 | 5867c88a | ths | break;
|
2537 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_READ:
|
2538 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
2539 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2540 | 5867c88a | ths | int n = read(fd, parg->buffer, parg->count);
|
2541 | 5867c88a | ths | if (n != parg->count) {
|
2542 | 5867c88a | ths | return -EIO;
|
2543 | 5867c88a | ths | } |
2544 | 5867c88a | ths | } |
2545 | 5867c88a | ths | break;
|
2546 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_WRITE_ADDR:
|
2547 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
2548 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2549 | 5867c88a | ths | int n = write(fd, parg->buffer, parg->count);
|
2550 | 5867c88a | ths | if (n != parg->count) {
|
2551 | 5867c88a | ths | return -EIO;
|
2552 | 5867c88a | ths | } |
2553 | 5867c88a | ths | } |
2554 | 5867c88a | ths | break;
|
2555 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_WRITE:
|
2556 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
2557 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2558 | 5867c88a | ths | int n = write(fd, parg->buffer, parg->count);
|
2559 | 5867c88a | ths | if (n != parg->count) {
|
2560 | 5867c88a | ths | return -EIO;
|
2561 | 5867c88a | ths | } |
2562 | 5867c88a | ths | } |
2563 | 5867c88a | ths | break;
|
2564 | e57a8c0e | bellard | default:
|
2565 | e57a8c0e | bellard | return -ENOTSUP;
|
2566 | e57a8c0e | bellard | } |
2567 | e57a8c0e | bellard | return 0; |
2568 | e57a8c0e | bellard | } |
2569 | e57a8c0e | bellard | |
2570 | 5867c88a | ths | static void pp_close(CharDriverState *chr) |
2571 | 5867c88a | ths | { |
2572 | 5867c88a | ths | ParallelCharDriver *drv = chr->opaque; |
2573 | 5867c88a | ths | int fd = drv->fd;
|
2574 | 5867c88a | ths | |
2575 | 5867c88a | ths | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
2576 | 5867c88a | ths | ioctl(fd, PPRELEASE); |
2577 | 5867c88a | ths | close(fd); |
2578 | 5867c88a | ths | qemu_free(drv); |
2579 | 5867c88a | ths | } |
2580 | 5867c88a | ths | |
2581 | 52f61fde | ths | static CharDriverState *qemu_chr_open_pp(const char *filename) |
2582 | e57a8c0e | bellard | { |
2583 | e57a8c0e | bellard | CharDriverState *chr; |
2584 | 5867c88a | ths | ParallelCharDriver *drv; |
2585 | e57a8c0e | bellard | int fd;
|
2586 | e57a8c0e | bellard | |
2587 | aeb30be6 | balrog | TFR(fd = open(filename, O_RDWR)); |
2588 | e57a8c0e | bellard | if (fd < 0) |
2589 | e57a8c0e | bellard | return NULL; |
2590 | e57a8c0e | bellard | |
2591 | e57a8c0e | bellard | if (ioctl(fd, PPCLAIM) < 0) { |
2592 | e57a8c0e | bellard | close(fd); |
2593 | e57a8c0e | bellard | return NULL; |
2594 | e57a8c0e | bellard | } |
2595 | e57a8c0e | bellard | |
2596 | 5867c88a | ths | drv = qemu_mallocz(sizeof(ParallelCharDriver));
|
2597 | 5867c88a | ths | if (!drv) {
|
2598 | 5867c88a | ths | close(fd); |
2599 | 5867c88a | ths | return NULL; |
2600 | 5867c88a | ths | } |
2601 | 5867c88a | ths | drv->fd = fd; |
2602 | 5867c88a | ths | drv->mode = IEEE1284_MODE_COMPAT; |
2603 | 5867c88a | ths | |
2604 | e57a8c0e | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
2605 | e57a8c0e | bellard | if (!chr) {
|
2606 | 5867c88a | ths | qemu_free(drv); |
2607 | e57a8c0e | bellard | close(fd); |
2608 | e57a8c0e | bellard | return NULL; |
2609 | e57a8c0e | bellard | } |
2610 | e57a8c0e | bellard | chr->chr_write = null_chr_write; |
2611 | e57a8c0e | bellard | chr->chr_ioctl = pp_ioctl; |
2612 | 5867c88a | ths | chr->chr_close = pp_close; |
2613 | 5867c88a | ths | chr->opaque = drv; |
2614 | 86e94dea | ths | |
2615 | 86e94dea | ths | qemu_chr_reset(chr); |
2616 | 86e94dea | ths | |
2617 | f8d179e3 | bellard | return chr;
|
2618 | f8d179e3 | bellard | } |
2619 | aec62507 | ths | #endif /* __linux__ */ |
2620 | f8d179e3 | bellard | |
2621 | aec62507 | ths | #else /* _WIN32 */ |
2622 | 82c643ff | bellard | |
2623 | f331110f | bellard | typedef struct { |
2624 | f331110f | bellard | int max_size;
|
2625 | f331110f | bellard | HANDLE hcom, hrecv, hsend; |
2626 | f331110f | bellard | OVERLAPPED orecv, osend; |
2627 | f331110f | bellard | BOOL fpipe; |
2628 | f331110f | bellard | DWORD len; |
2629 | f331110f | bellard | } WinCharState; |
2630 | f331110f | bellard | |
2631 | f331110f | bellard | #define NSENDBUF 2048 |
2632 | f331110f | bellard | #define NRECVBUF 2048 |
2633 | f331110f | bellard | #define MAXCONNECT 1 |
2634 | f331110f | bellard | #define NTIMEOUT 5000 |
2635 | f331110f | bellard | |
2636 | f331110f | bellard | static int win_chr_poll(void *opaque); |
2637 | f331110f | bellard | static int win_chr_pipe_poll(void *opaque); |
2638 | f331110f | bellard | |
2639 | 087f4ae0 | ths | static void win_chr_close(CharDriverState *chr) |
2640 | f331110f | bellard | { |
2641 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2642 | 087f4ae0 | ths | |
2643 | f331110f | bellard | if (s->hsend) {
|
2644 | f331110f | bellard | CloseHandle(s->hsend); |
2645 | f331110f | bellard | s->hsend = NULL;
|
2646 | f331110f | bellard | } |
2647 | f331110f | bellard | if (s->hrecv) {
|
2648 | f331110f | bellard | CloseHandle(s->hrecv); |
2649 | f331110f | bellard | s->hrecv = NULL;
|
2650 | f331110f | bellard | } |
2651 | f331110f | bellard | if (s->hcom) {
|
2652 | f331110f | bellard | CloseHandle(s->hcom); |
2653 | f331110f | bellard | s->hcom = NULL;
|
2654 | f331110f | bellard | } |
2655 | f331110f | bellard | if (s->fpipe)
|
2656 | 087f4ae0 | ths | qemu_del_polling_cb(win_chr_pipe_poll, chr); |
2657 | f331110f | bellard | else
|
2658 | 087f4ae0 | ths | qemu_del_polling_cb(win_chr_poll, chr); |
2659 | f331110f | bellard | } |
2660 | f331110f | bellard | |
2661 | 087f4ae0 | ths | static int win_chr_init(CharDriverState *chr, const char *filename) |
2662 | f331110f | bellard | { |
2663 | f331110f | bellard | WinCharState *s = chr->opaque; |
2664 | f331110f | bellard | COMMCONFIG comcfg; |
2665 | f331110f | bellard | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
2666 | f331110f | bellard | COMSTAT comstat; |
2667 | f331110f | bellard | DWORD size; |
2668 | f331110f | bellard | DWORD err; |
2669 | 3b46e624 | ths | |
2670 | f331110f | bellard | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
2671 | f331110f | bellard | if (!s->hsend) {
|
2672 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
2673 | f331110f | bellard | goto fail;
|
2674 | f331110f | bellard | } |
2675 | f331110f | bellard | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
2676 | f331110f | bellard | if (!s->hrecv) {
|
2677 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
2678 | f331110f | bellard | goto fail;
|
2679 | f331110f | bellard | } |
2680 | f331110f | bellard | |
2681 | f331110f | bellard | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
2682 | f331110f | bellard | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
|
2683 | f331110f | bellard | if (s->hcom == INVALID_HANDLE_VALUE) {
|
2684 | f331110f | bellard | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
|
2685 | f331110f | bellard | s->hcom = NULL;
|
2686 | f331110f | bellard | goto fail;
|
2687 | f331110f | bellard | } |
2688 | 3b46e624 | ths | |
2689 | f331110f | bellard | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
|
2690 | f331110f | bellard | fprintf(stderr, "Failed SetupComm\n");
|
2691 | f331110f | bellard | goto fail;
|
2692 | f331110f | bellard | } |
2693 | 3b46e624 | ths | |
2694 | f331110f | bellard | ZeroMemory(&comcfg, sizeof(COMMCONFIG));
|
2695 | f331110f | bellard | size = sizeof(COMMCONFIG);
|
2696 | f331110f | bellard | GetDefaultCommConfig(filename, &comcfg, &size); |
2697 | f331110f | bellard | comcfg.dcb.DCBlength = sizeof(DCB);
|
2698 | f331110f | bellard | CommConfigDialog(filename, NULL, &comcfg);
|
2699 | f331110f | bellard | |
2700 | f331110f | bellard | if (!SetCommState(s->hcom, &comcfg.dcb)) {
|
2701 | f331110f | bellard | fprintf(stderr, "Failed SetCommState\n");
|
2702 | f331110f | bellard | goto fail;
|
2703 | f331110f | bellard | } |
2704 | f331110f | bellard | |
2705 | f331110f | bellard | if (!SetCommMask(s->hcom, EV_ERR)) {
|
2706 | f331110f | bellard | fprintf(stderr, "Failed SetCommMask\n");
|
2707 | f331110f | bellard | goto fail;
|
2708 | f331110f | bellard | } |
2709 | f331110f | bellard | |
2710 | f331110f | bellard | cto.ReadIntervalTimeout = MAXDWORD; |
2711 | f331110f | bellard | if (!SetCommTimeouts(s->hcom, &cto)) {
|
2712 | f331110f | bellard | fprintf(stderr, "Failed SetCommTimeouts\n");
|
2713 | f331110f | bellard | goto fail;
|
2714 | f331110f | bellard | } |
2715 | 3b46e624 | ths | |
2716 | f331110f | bellard | if (!ClearCommError(s->hcom, &err, &comstat)) {
|
2717 | f331110f | bellard | fprintf(stderr, "Failed ClearCommError\n");
|
2718 | f331110f | bellard | goto fail;
|
2719 | f331110f | bellard | } |
2720 | 087f4ae0 | ths | qemu_add_polling_cb(win_chr_poll, chr); |
2721 | f331110f | bellard | return 0; |
2722 | f331110f | bellard | |
2723 | f331110f | bellard | fail:
|
2724 | 087f4ae0 | ths | win_chr_close(chr); |
2725 | f331110f | bellard | return -1; |
2726 | f331110f | bellard | } |
2727 | f331110f | bellard | |
2728 | f331110f | bellard | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
2729 | f331110f | bellard | { |
2730 | f331110f | bellard | WinCharState *s = chr->opaque; |
2731 | f331110f | bellard | DWORD len, ret, size, err; |
2732 | f331110f | bellard | |
2733 | f331110f | bellard | len = len1; |
2734 | f331110f | bellard | ZeroMemory(&s->osend, sizeof(s->osend));
|
2735 | f331110f | bellard | s->osend.hEvent = s->hsend; |
2736 | f331110f | bellard | while (len > 0) { |
2737 | f331110f | bellard | if (s->hsend)
|
2738 | f331110f | bellard | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
2739 | f331110f | bellard | else
|
2740 | f331110f | bellard | ret = WriteFile(s->hcom, buf, len, &size, NULL);
|
2741 | f331110f | bellard | if (!ret) {
|
2742 | f331110f | bellard | err = GetLastError(); |
2743 | f331110f | bellard | if (err == ERROR_IO_PENDING) {
|
2744 | f331110f | bellard | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
2745 | f331110f | bellard | if (ret) {
|
2746 | f331110f | bellard | buf += size; |
2747 | f331110f | bellard | len -= size; |
2748 | f331110f | bellard | } else {
|
2749 | f331110f | bellard | break;
|
2750 | f331110f | bellard | } |
2751 | f331110f | bellard | } else {
|
2752 | f331110f | bellard | break;
|
2753 | f331110f | bellard | } |
2754 | f331110f | bellard | } else {
|
2755 | f331110f | bellard | buf += size; |
2756 | f331110f | bellard | len -= size; |
2757 | f331110f | bellard | } |
2758 | f331110f | bellard | } |
2759 | f331110f | bellard | return len1 - len;
|
2760 | f331110f | bellard | } |
2761 | f331110f | bellard | |
2762 | 087f4ae0 | ths | static int win_chr_read_poll(CharDriverState *chr) |
2763 | f331110f | bellard | { |
2764 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2765 | 087f4ae0 | ths | |
2766 | 087f4ae0 | ths | s->max_size = qemu_chr_can_read(chr); |
2767 | f331110f | bellard | return s->max_size;
|
2768 | f331110f | bellard | } |
2769 | e5b0bc44 | pbrook | |
2770 | 087f4ae0 | ths | static void win_chr_readfile(CharDriverState *chr) |
2771 | f331110f | bellard | { |
2772 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2773 | f331110f | bellard | int ret, err;
|
2774 | f331110f | bellard | uint8_t buf[1024];
|
2775 | f331110f | bellard | DWORD size; |
2776 | 3b46e624 | ths | |
2777 | f331110f | bellard | ZeroMemory(&s->orecv, sizeof(s->orecv));
|
2778 | f331110f | bellard | s->orecv.hEvent = s->hrecv; |
2779 | f331110f | bellard | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
2780 | f331110f | bellard | if (!ret) {
|
2781 | f331110f | bellard | err = GetLastError(); |
2782 | f331110f | bellard | if (err == ERROR_IO_PENDING) {
|
2783 | f331110f | bellard | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
2784 | f331110f | bellard | } |
2785 | f331110f | bellard | } |
2786 | f331110f | bellard | |
2787 | f331110f | bellard | if (size > 0) { |
2788 | 087f4ae0 | ths | qemu_chr_read(chr, buf, size); |
2789 | f331110f | bellard | } |
2790 | f331110f | bellard | } |
2791 | f331110f | bellard | |
2792 | 087f4ae0 | ths | static void win_chr_read(CharDriverState *chr) |
2793 | f331110f | bellard | { |
2794 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2795 | 087f4ae0 | ths | |
2796 | f331110f | bellard | if (s->len > s->max_size)
|
2797 | f331110f | bellard | s->len = s->max_size; |
2798 | f331110f | bellard | if (s->len == 0) |
2799 | f331110f | bellard | return;
|
2800 | 3b46e624 | ths | |
2801 | 087f4ae0 | ths | win_chr_readfile(chr); |
2802 | f331110f | bellard | } |
2803 | f331110f | bellard | |
2804 | f331110f | bellard | static int win_chr_poll(void *opaque) |
2805 | f331110f | bellard | { |
2806 | 087f4ae0 | ths | CharDriverState *chr = opaque; |
2807 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2808 | f331110f | bellard | COMSTAT status; |
2809 | f331110f | bellard | DWORD comerr; |
2810 | 3b46e624 | ths | |
2811 | f331110f | bellard | ClearCommError(s->hcom, &comerr, &status); |
2812 | f331110f | bellard | if (status.cbInQue > 0) { |
2813 | f331110f | bellard | s->len = status.cbInQue; |
2814 | 087f4ae0 | ths | win_chr_read_poll(chr); |
2815 | 087f4ae0 | ths | win_chr_read(chr); |
2816 | f331110f | bellard | return 1; |
2817 | f331110f | bellard | } |
2818 | f331110f | bellard | return 0; |
2819 | f331110f | bellard | } |
2820 | f331110f | bellard | |
2821 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win(const char *filename) |
2822 | f331110f | bellard | { |
2823 | f331110f | bellard | CharDriverState *chr; |
2824 | f331110f | bellard | WinCharState *s; |
2825 | 3b46e624 | ths | |
2826 | f331110f | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
2827 | f331110f | bellard | if (!chr)
|
2828 | f331110f | bellard | return NULL; |
2829 | f331110f | bellard | s = qemu_mallocz(sizeof(WinCharState));
|
2830 | f331110f | bellard | if (!s) {
|
2831 | f331110f | bellard | free(chr); |
2832 | f331110f | bellard | return NULL; |
2833 | f331110f | bellard | } |
2834 | f331110f | bellard | chr->opaque = s; |
2835 | f331110f | bellard | chr->chr_write = win_chr_write; |
2836 | f331110f | bellard | chr->chr_close = win_chr_close; |
2837 | f331110f | bellard | |
2838 | 087f4ae0 | ths | if (win_chr_init(chr, filename) < 0) { |
2839 | f331110f | bellard | free(s); |
2840 | f331110f | bellard | free(chr); |
2841 | f331110f | bellard | return NULL; |
2842 | f331110f | bellard | } |
2843 | 86e94dea | ths | qemu_chr_reset(chr); |
2844 | f331110f | bellard | return chr;
|
2845 | f331110f | bellard | } |
2846 | f331110f | bellard | |
2847 | f331110f | bellard | static int win_chr_pipe_poll(void *opaque) |
2848 | f331110f | bellard | { |
2849 | 087f4ae0 | ths | CharDriverState *chr = opaque; |
2850 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2851 | f331110f | bellard | DWORD size; |
2852 | f331110f | bellard | |
2853 | f331110f | bellard | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
2854 | f331110f | bellard | if (size > 0) { |
2855 | f331110f | bellard | s->len = size; |
2856 | 087f4ae0 | ths | win_chr_read_poll(chr); |
2857 | 087f4ae0 | ths | win_chr_read(chr); |
2858 | f331110f | bellard | return 1; |
2859 | f331110f | bellard | } |
2860 | f331110f | bellard | return 0; |
2861 | f331110f | bellard | } |
2862 | f331110f | bellard | |
2863 | 087f4ae0 | ths | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) |
2864 | f331110f | bellard | { |
2865 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2866 | f331110f | bellard | OVERLAPPED ov; |
2867 | f331110f | bellard | int ret;
|
2868 | f331110f | bellard | DWORD size; |
2869 | f331110f | bellard | char openname[256]; |
2870 | 3b46e624 | ths | |
2871 | f331110f | bellard | s->fpipe = TRUE; |
2872 | f331110f | bellard | |
2873 | f331110f | bellard | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
2874 | f331110f | bellard | if (!s->hsend) {
|
2875 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
2876 | f331110f | bellard | goto fail;
|
2877 | f331110f | bellard | } |
2878 | f331110f | bellard | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
2879 | f331110f | bellard | if (!s->hrecv) {
|
2880 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
2881 | f331110f | bellard | goto fail;
|
2882 | f331110f | bellard | } |
2883 | 3b46e624 | ths | |
2884 | f331110f | bellard | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
2885 | f331110f | bellard | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
2886 | f331110f | bellard | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
2887 | f331110f | bellard | PIPE_WAIT, |
2888 | f331110f | bellard | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
|
2889 | f331110f | bellard | if (s->hcom == INVALID_HANDLE_VALUE) {
|
2890 | f331110f | bellard | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
|
2891 | f331110f | bellard | s->hcom = NULL;
|
2892 | f331110f | bellard | goto fail;
|
2893 | f331110f | bellard | } |
2894 | f331110f | bellard | |
2895 | f331110f | bellard | ZeroMemory(&ov, sizeof(ov));
|
2896 | f331110f | bellard | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
2897 | f331110f | bellard | ret = ConnectNamedPipe(s->hcom, &ov); |
2898 | f331110f | bellard | if (ret) {
|
2899 | f331110f | bellard | fprintf(stderr, "Failed ConnectNamedPipe\n");
|
2900 | f331110f | bellard | goto fail;
|
2901 | f331110f | bellard | } |
2902 | f331110f | bellard | |
2903 | f331110f | bellard | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
2904 | f331110f | bellard | if (!ret) {
|
2905 | f331110f | bellard | fprintf(stderr, "Failed GetOverlappedResult\n");
|
2906 | f331110f | bellard | if (ov.hEvent) {
|
2907 | f331110f | bellard | CloseHandle(ov.hEvent); |
2908 | f331110f | bellard | ov.hEvent = NULL;
|
2909 | f331110f | bellard | } |
2910 | f331110f | bellard | goto fail;
|
2911 | f331110f | bellard | } |
2912 | f331110f | bellard | |
2913 | f331110f | bellard | if (ov.hEvent) {
|
2914 | f331110f | bellard | CloseHandle(ov.hEvent); |
2915 | f331110f | bellard | ov.hEvent = NULL;
|
2916 | f331110f | bellard | } |
2917 | 087f4ae0 | ths | qemu_add_polling_cb(win_chr_pipe_poll, chr); |
2918 | f331110f | bellard | return 0; |
2919 | f331110f | bellard | |
2920 | f331110f | bellard | fail:
|
2921 | 087f4ae0 | ths | win_chr_close(chr); |
2922 | f331110f | bellard | return -1; |
2923 | f331110f | bellard | } |
2924 | f331110f | bellard | |
2925 | f331110f | bellard | |
2926 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win_pipe(const char *filename) |
2927 | f331110f | bellard | { |
2928 | f331110f | bellard | CharDriverState *chr; |
2929 | f331110f | bellard | WinCharState *s; |
2930 | f331110f | bellard | |
2931 | f331110f | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
2932 | f331110f | bellard | if (!chr)
|
2933 | f331110f | bellard | return NULL; |
2934 | f331110f | bellard | s = qemu_mallocz(sizeof(WinCharState));
|
2935 | f331110f | bellard | if (!s) {
|
2936 | f331110f | bellard | free(chr); |
2937 | f331110f | bellard | return NULL; |
2938 | f331110f | bellard | } |
2939 | f331110f | bellard | chr->opaque = s; |
2940 | f331110f | bellard | chr->chr_write = win_chr_write; |
2941 | f331110f | bellard | chr->chr_close = win_chr_close; |
2942 | 3b46e624 | ths | |
2943 | 087f4ae0 | ths | if (win_chr_pipe_init(chr, filename) < 0) { |
2944 | f331110f | bellard | free(s); |
2945 | f331110f | bellard | free(chr); |
2946 | f331110f | bellard | return NULL; |
2947 | f331110f | bellard | } |
2948 | 86e94dea | ths | qemu_chr_reset(chr); |
2949 | f331110f | bellard | return chr;
|
2950 | f331110f | bellard | } |
2951 | f331110f | bellard | |
2952 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
|
2953 | f331110f | bellard | { |
2954 | f331110f | bellard | CharDriverState *chr; |
2955 | f331110f | bellard | WinCharState *s; |
2956 | f331110f | bellard | |
2957 | f331110f | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
2958 | f331110f | bellard | if (!chr)
|
2959 | f331110f | bellard | return NULL; |
2960 | f331110f | bellard | s = qemu_mallocz(sizeof(WinCharState));
|
2961 | f331110f | bellard | if (!s) {
|
2962 | f331110f | bellard | free(chr); |
2963 | f331110f | bellard | return NULL; |
2964 | f331110f | bellard | } |
2965 | f331110f | bellard | s->hcom = fd_out; |
2966 | f331110f | bellard | chr->opaque = s; |
2967 | f331110f | bellard | chr->chr_write = win_chr_write; |
2968 | 86e94dea | ths | qemu_chr_reset(chr); |
2969 | f331110f | bellard | return chr;
|
2970 | f331110f | bellard | } |
2971 | 72d46479 | ths | |
2972 | 72d46479 | ths | static CharDriverState *qemu_chr_open_win_con(const char *filename) |
2973 | 72d46479 | ths | { |
2974 | 72d46479 | ths | return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
|
2975 | 72d46479 | ths | } |
2976 | 72d46479 | ths | |
2977 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win_file_out(const char *file_out) |
2978 | f331110f | bellard | { |
2979 | f331110f | bellard | HANDLE fd_out; |
2980 | 3b46e624 | ths | |
2981 | f331110f | bellard | fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
|
2982 | f331110f | bellard | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
2983 | f331110f | bellard | if (fd_out == INVALID_HANDLE_VALUE)
|
2984 | f331110f | bellard | return NULL; |
2985 | f331110f | bellard | |
2986 | f331110f | bellard | return qemu_chr_open_win_file(fd_out);
|
2987 | f331110f | bellard | } |
2988 | aec62507 | ths | #endif /* !_WIN32 */ |
2989 | f331110f | bellard | |
2990 | 0bab00f3 | bellard | /***********************************************************/
|
2991 | 0bab00f3 | bellard | /* UDP Net console */
|
2992 | 0bab00f3 | bellard | |
2993 | 0bab00f3 | bellard | typedef struct { |
2994 | 0bab00f3 | bellard | int fd;
|
2995 | 0bab00f3 | bellard | struct sockaddr_in daddr;
|
2996 | 60fe76f3 | ths | uint8_t buf[1024];
|
2997 | 0bab00f3 | bellard | int bufcnt;
|
2998 | 0bab00f3 | bellard | int bufptr;
|
2999 | 0bab00f3 | bellard | int max_size;
|
3000 | 0bab00f3 | bellard | } NetCharDriver; |
3001 | 0bab00f3 | bellard | |
3002 | 0bab00f3 | bellard | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
3003 | 0bab00f3 | bellard | { |
3004 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3005 | 0bab00f3 | bellard | |
3006 | 0bab00f3 | bellard | return sendto(s->fd, buf, len, 0, |
3007 | 0bab00f3 | bellard | (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in)); |
3008 | 0bab00f3 | bellard | } |
3009 | 0bab00f3 | bellard | |
3010 | 0bab00f3 | bellard | static int udp_chr_read_poll(void *opaque) |
3011 | 0bab00f3 | bellard | { |
3012 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3013 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3014 | 0bab00f3 | bellard | |
3015 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3016 | 0bab00f3 | bellard | |
3017 | 0bab00f3 | bellard | /* If there were any stray characters in the queue process them
|
3018 | 0bab00f3 | bellard | * first
|
3019 | 0bab00f3 | bellard | */
|
3020 | 0bab00f3 | bellard | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
3021 | e5b0bc44 | pbrook | qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
3022 | 0bab00f3 | bellard | s->bufptr++; |
3023 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3024 | 0bab00f3 | bellard | } |
3025 | 0bab00f3 | bellard | return s->max_size;
|
3026 | 0bab00f3 | bellard | } |
3027 | 0bab00f3 | bellard | |
3028 | 0bab00f3 | bellard | static void udp_chr_read(void *opaque) |
3029 | 0bab00f3 | bellard | { |
3030 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3031 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3032 | 0bab00f3 | bellard | |
3033 | 0bab00f3 | bellard | if (s->max_size == 0) |
3034 | 0bab00f3 | bellard | return;
|
3035 | 0bab00f3 | bellard | s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0); |
3036 | 0bab00f3 | bellard | s->bufptr = s->bufcnt; |
3037 | 0bab00f3 | bellard | if (s->bufcnt <= 0) |
3038 | 0bab00f3 | bellard | return;
|
3039 | 0bab00f3 | bellard | |
3040 | 0bab00f3 | bellard | s->bufptr = 0;
|
3041 | 0bab00f3 | bellard | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
3042 | e5b0bc44 | pbrook | qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
3043 | 0bab00f3 | bellard | s->bufptr++; |
3044 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3045 | 0bab00f3 | bellard | } |
3046 | 0bab00f3 | bellard | } |
3047 | 0bab00f3 | bellard | |
3048 | e5b0bc44 | pbrook | static void udp_chr_update_read_handler(CharDriverState *chr) |
3049 | 0bab00f3 | bellard | { |
3050 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3051 | 0bab00f3 | bellard | |
3052 | 0bab00f3 | bellard | if (s->fd >= 0) { |
3053 | 0bab00f3 | bellard | qemu_set_fd_handler2(s->fd, udp_chr_read_poll, |
3054 | 0bab00f3 | bellard | udp_chr_read, NULL, chr);
|
3055 | 0bab00f3 | bellard | } |
3056 | 0bab00f3 | bellard | } |
3057 | 0bab00f3 | bellard | |
3058 | 0bab00f3 | bellard | int parse_host_port(struct sockaddr_in *saddr, const char *str); |
3059 | 52f61fde | ths | #ifndef _WIN32
|
3060 | 52f61fde | ths | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str); |
3061 | 52f61fde | ths | #endif
|
3062 | 951f1351 | bellard | int parse_host_src_port(struct sockaddr_in *haddr, |
3063 | 951f1351 | bellard | struct sockaddr_in *saddr,
|
3064 | 951f1351 | bellard | const char *str); |
3065 | 0bab00f3 | bellard | |
3066 | 52f61fde | ths | static CharDriverState *qemu_chr_open_udp(const char *def) |
3067 | 0bab00f3 | bellard | { |
3068 | 0bab00f3 | bellard | CharDriverState *chr = NULL;
|
3069 | 0bab00f3 | bellard | NetCharDriver *s = NULL;
|
3070 | 0bab00f3 | bellard | int fd = -1; |
3071 | 951f1351 | bellard | struct sockaddr_in saddr;
|
3072 | 0bab00f3 | bellard | |
3073 | 0bab00f3 | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3074 | 0bab00f3 | bellard | if (!chr)
|
3075 | 0bab00f3 | bellard | goto return_err;
|
3076 | 0bab00f3 | bellard | s = qemu_mallocz(sizeof(NetCharDriver));
|
3077 | 0bab00f3 | bellard | if (!s)
|
3078 | 0bab00f3 | bellard | goto return_err;
|
3079 | 0bab00f3 | bellard | |
3080 | 0bab00f3 | bellard | fd = socket(PF_INET, SOCK_DGRAM, 0);
|
3081 | 0bab00f3 | bellard | if (fd < 0) { |
3082 | 0bab00f3 | bellard | perror("socket(PF_INET, SOCK_DGRAM)");
|
3083 | 0bab00f3 | bellard | goto return_err;
|
3084 | 0bab00f3 | bellard | } |
3085 | 0bab00f3 | bellard | |
3086 | 951f1351 | bellard | if (parse_host_src_port(&s->daddr, &saddr, def) < 0) { |
3087 | 951f1351 | bellard | printf("Could not parse: %s\n", def);
|
3088 | 951f1351 | bellard | goto return_err;
|
3089 | 0bab00f3 | bellard | } |
3090 | 0bab00f3 | bellard | |
3091 | 951f1351 | bellard | if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) |
3092 | 0bab00f3 | bellard | { |
3093 | 0bab00f3 | bellard | perror("bind");
|
3094 | 0bab00f3 | bellard | goto return_err;
|
3095 | 0bab00f3 | bellard | } |
3096 | 0bab00f3 | bellard | |
3097 | 0bab00f3 | bellard | s->fd = fd; |
3098 | 0bab00f3 | bellard | s->bufcnt = 0;
|
3099 | 0bab00f3 | bellard | s->bufptr = 0;
|
3100 | 0bab00f3 | bellard | chr->opaque = s; |
3101 | 0bab00f3 | bellard | chr->chr_write = udp_chr_write; |
3102 | e5b0bc44 | pbrook | chr->chr_update_read_handler = udp_chr_update_read_handler; |
3103 | 0bab00f3 | bellard | return chr;
|
3104 | 0bab00f3 | bellard | |
3105 | 0bab00f3 | bellard | return_err:
|
3106 | 0bab00f3 | bellard | if (chr)
|
3107 | 0bab00f3 | bellard | free(chr); |
3108 | 0bab00f3 | bellard | if (s)
|
3109 | 0bab00f3 | bellard | free(s); |
3110 | 0bab00f3 | bellard | if (fd >= 0) |
3111 | 0bab00f3 | bellard | closesocket(fd); |
3112 | 0bab00f3 | bellard | return NULL; |
3113 | 0bab00f3 | bellard | } |
3114 | 0bab00f3 | bellard | |
3115 | 0bab00f3 | bellard | /***********************************************************/
|
3116 | 0bab00f3 | bellard | /* TCP Net console */
|
3117 | 0bab00f3 | bellard | |
3118 | 0bab00f3 | bellard | typedef struct { |
3119 | 0bab00f3 | bellard | int fd, listen_fd;
|
3120 | 0bab00f3 | bellard | int connected;
|
3121 | 0bab00f3 | bellard | int max_size;
|
3122 | 951f1351 | bellard | int do_telnetopt;
|
3123 | e5b0bc44 | pbrook | int do_nodelay;
|
3124 | ffd843bc | ths | int is_unix;
|
3125 | 0bab00f3 | bellard | } TCPCharDriver; |
3126 | 0bab00f3 | bellard | |
3127 | 0bab00f3 | bellard | static void tcp_chr_accept(void *opaque); |
3128 | 0bab00f3 | bellard | |
3129 | 0bab00f3 | bellard | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
3130 | 0bab00f3 | bellard | { |
3131 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3132 | 0bab00f3 | bellard | if (s->connected) {
|
3133 | 0bab00f3 | bellard | return send_all(s->fd, buf, len);
|
3134 | 0bab00f3 | bellard | } else {
|
3135 | 0bab00f3 | bellard | /* XXX: indicate an error ? */
|
3136 | 0bab00f3 | bellard | return len;
|
3137 | 0bab00f3 | bellard | } |
3138 | 0bab00f3 | bellard | } |
3139 | 0bab00f3 | bellard | |
3140 | 0bab00f3 | bellard | static int tcp_chr_read_poll(void *opaque) |
3141 | 0bab00f3 | bellard | { |
3142 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3143 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3144 | 0bab00f3 | bellard | if (!s->connected)
|
3145 | 0bab00f3 | bellard | return 0; |
3146 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3147 | 0bab00f3 | bellard | return s->max_size;
|
3148 | 0bab00f3 | bellard | } |
3149 | 0bab00f3 | bellard | |
3150 | 951f1351 | bellard | #define IAC 255 |
3151 | 951f1351 | bellard | #define IAC_BREAK 243 |
3152 | 951f1351 | bellard | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
3153 | 951f1351 | bellard | TCPCharDriver *s, |
3154 | 60fe76f3 | ths | uint8_t *buf, int *size)
|
3155 | 951f1351 | bellard | { |
3156 | 951f1351 | bellard | /* Handle any telnet client's basic IAC options to satisfy char by
|
3157 | 951f1351 | bellard | * char mode with no echo. All IAC options will be removed from
|
3158 | 951f1351 | bellard | * the buf and the do_telnetopt variable will be used to track the
|
3159 | 951f1351 | bellard | * state of the width of the IAC information.
|
3160 | 951f1351 | bellard | *
|
3161 | 951f1351 | bellard | * IAC commands come in sets of 3 bytes with the exception of the
|
3162 | 951f1351 | bellard | * "IAC BREAK" command and the double IAC.
|
3163 | 951f1351 | bellard | */
|
3164 | 951f1351 | bellard | |
3165 | 951f1351 | bellard | int i;
|
3166 | 951f1351 | bellard | int j = 0; |
3167 | 951f1351 | bellard | |
3168 | 951f1351 | bellard | for (i = 0; i < *size; i++) { |
3169 | 951f1351 | bellard | if (s->do_telnetopt > 1) { |
3170 | 951f1351 | bellard | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
3171 | 951f1351 | bellard | /* Double IAC means send an IAC */
|
3172 | 951f1351 | bellard | if (j != i)
|
3173 | 951f1351 | bellard | buf[j] = buf[i]; |
3174 | 951f1351 | bellard | j++; |
3175 | 951f1351 | bellard | s->do_telnetopt = 1;
|
3176 | 951f1351 | bellard | } else {
|
3177 | 951f1351 | bellard | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
3178 | 951f1351 | bellard | /* Handle IAC break commands by sending a serial break */
|
3179 | e5b0bc44 | pbrook | qemu_chr_event(chr, CHR_EVENT_BREAK); |
3180 | 951f1351 | bellard | s->do_telnetopt++; |
3181 | 951f1351 | bellard | } |
3182 | 951f1351 | bellard | s->do_telnetopt++; |
3183 | 951f1351 | bellard | } |
3184 | 951f1351 | bellard | if (s->do_telnetopt >= 4) { |
3185 | 951f1351 | bellard | s->do_telnetopt = 1;
|
3186 | 951f1351 | bellard | } |
3187 | 951f1351 | bellard | } else {
|
3188 | 951f1351 | bellard | if ((unsigned char)buf[i] == IAC) { |
3189 | 951f1351 | bellard | s->do_telnetopt = 2;
|
3190 | 951f1351 | bellard | } else {
|
3191 | 951f1351 | bellard | if (j != i)
|
3192 | 951f1351 | bellard | buf[j] = buf[i]; |
3193 | 951f1351 | bellard | j++; |
3194 | 951f1351 | bellard | } |
3195 | 951f1351 | bellard | } |
3196 | 951f1351 | bellard | } |
3197 | 951f1351 | bellard | *size = j; |
3198 | 951f1351 | bellard | } |
3199 | 951f1351 | bellard | |
3200 | 0bab00f3 | bellard | static void tcp_chr_read(void *opaque) |
3201 | 0bab00f3 | bellard | { |
3202 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3203 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3204 | 0bab00f3 | bellard | uint8_t buf[1024];
|
3205 | 0bab00f3 | bellard | int len, size;
|
3206 | 0bab00f3 | bellard | |
3207 | 0bab00f3 | bellard | if (!s->connected || s->max_size <= 0) |
3208 | 0bab00f3 | bellard | return;
|
3209 | 0bab00f3 | bellard | len = sizeof(buf);
|
3210 | 0bab00f3 | bellard | if (len > s->max_size)
|
3211 | 0bab00f3 | bellard | len = s->max_size; |
3212 | 0bab00f3 | bellard | size = recv(s->fd, buf, len, 0);
|
3213 | 0bab00f3 | bellard | if (size == 0) { |
3214 | 0bab00f3 | bellard | /* connection closed */
|
3215 | 0bab00f3 | bellard | s->connected = 0;
|
3216 | 0bab00f3 | bellard | if (s->listen_fd >= 0) { |
3217 | 0bab00f3 | bellard | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
3218 | 0bab00f3 | bellard | } |
3219 | 0bab00f3 | bellard | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
3220 | 0bab00f3 | bellard | closesocket(s->fd); |
3221 | 0bab00f3 | bellard | s->fd = -1;
|
3222 | 0bab00f3 | bellard | } else if (size > 0) { |
3223 | 951f1351 | bellard | if (s->do_telnetopt)
|
3224 | 951f1351 | bellard | tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
3225 | 951f1351 | bellard | if (size > 0) |
3226 | e5b0bc44 | pbrook | qemu_chr_read(chr, buf, size); |
3227 | 0bab00f3 | bellard | } |
3228 | 0bab00f3 | bellard | } |
3229 | 0bab00f3 | bellard | |
3230 | 0bab00f3 | bellard | static void tcp_chr_connect(void *opaque) |
3231 | 0bab00f3 | bellard | { |
3232 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3233 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3234 | 0bab00f3 | bellard | |
3235 | 0bab00f3 | bellard | s->connected = 1;
|
3236 | 0bab00f3 | bellard | qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, |
3237 | 0bab00f3 | bellard | tcp_chr_read, NULL, chr);
|
3238 | 86e94dea | ths | qemu_chr_reset(chr); |
3239 | 0bab00f3 | bellard | } |
3240 | 0bab00f3 | bellard | |
3241 | 951f1351 | bellard | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
3242 | 951f1351 | bellard | static void tcp_chr_telnet_init(int fd) |
3243 | 951f1351 | bellard | { |
3244 | 951f1351 | bellard | char buf[3]; |
3245 | 951f1351 | bellard | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
|
3246 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
3247 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3248 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
3249 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3250 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
3251 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3252 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
3253 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3254 | 951f1351 | bellard | } |
3255 | 951f1351 | bellard | |
3256 | f7499989 | pbrook | static void socket_set_nodelay(int fd) |
3257 | f7499989 | pbrook | { |
3258 | f7499989 | pbrook | int val = 1; |
3259 | f7499989 | pbrook | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
3260 | f7499989 | pbrook | } |
3261 | f7499989 | pbrook | |
3262 | 0bab00f3 | bellard | static void tcp_chr_accept(void *opaque) |
3263 | 0bab00f3 | bellard | { |
3264 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3265 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3266 | 0bab00f3 | bellard | struct sockaddr_in saddr;
|
3267 | ffd843bc | ths | #ifndef _WIN32
|
3268 | ffd843bc | ths | struct sockaddr_un uaddr;
|
3269 | ffd843bc | ths | #endif
|
3270 | ffd843bc | ths | struct sockaddr *addr;
|
3271 | 0bab00f3 | bellard | socklen_t len; |
3272 | 0bab00f3 | bellard | int fd;
|
3273 | 0bab00f3 | bellard | |
3274 | 0bab00f3 | bellard | for(;;) {
|
3275 | ffd843bc | ths | #ifndef _WIN32
|
3276 | ffd843bc | ths | if (s->is_unix) {
|
3277 | ffd843bc | ths | len = sizeof(uaddr);
|
3278 | ffd843bc | ths | addr = (struct sockaddr *)&uaddr;
|
3279 | ffd843bc | ths | } else
|
3280 | ffd843bc | ths | #endif
|
3281 | ffd843bc | ths | { |
3282 | ffd843bc | ths | len = sizeof(saddr);
|
3283 | ffd843bc | ths | addr = (struct sockaddr *)&saddr;
|
3284 | ffd843bc | ths | } |
3285 | ffd843bc | ths | fd = accept(s->listen_fd, addr, &len); |
3286 | 0bab00f3 | bellard | if (fd < 0 && errno != EINTR) { |
3287 | 0bab00f3 | bellard | return;
|
3288 | 0bab00f3 | bellard | } else if (fd >= 0) { |
3289 | 951f1351 | bellard | if (s->do_telnetopt)
|
3290 | 951f1351 | bellard | tcp_chr_telnet_init(fd); |
3291 | 0bab00f3 | bellard | break;
|
3292 | 0bab00f3 | bellard | } |
3293 | 0bab00f3 | bellard | } |
3294 | 0bab00f3 | bellard | socket_set_nonblock(fd); |
3295 | f7499989 | pbrook | if (s->do_nodelay)
|
3296 | f7499989 | pbrook | socket_set_nodelay(fd); |
3297 | 0bab00f3 | bellard | s->fd = fd; |
3298 | 0bab00f3 | bellard | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
3299 | 0bab00f3 | bellard | tcp_chr_connect(chr); |
3300 | 0bab00f3 | bellard | } |
3301 | 0bab00f3 | bellard | |
3302 | 0bab00f3 | bellard | static void tcp_chr_close(CharDriverState *chr) |
3303 | 0bab00f3 | bellard | { |
3304 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3305 | 0bab00f3 | bellard | if (s->fd >= 0) |
3306 | 0bab00f3 | bellard | closesocket(s->fd); |
3307 | 0bab00f3 | bellard | if (s->listen_fd >= 0) |
3308 | 0bab00f3 | bellard | closesocket(s->listen_fd); |
3309 | 0bab00f3 | bellard | qemu_free(s); |
3310 | 0bab00f3 | bellard | } |
3311 | 0bab00f3 | bellard | |
3312 | 5fafdf24 | ths | static CharDriverState *qemu_chr_open_tcp(const char *host_str, |
3313 | ffd843bc | ths | int is_telnet,
|
3314 | ffd843bc | ths | int is_unix)
|
3315 | 0bab00f3 | bellard | { |
3316 | 0bab00f3 | bellard | CharDriverState *chr = NULL;
|
3317 | 0bab00f3 | bellard | TCPCharDriver *s = NULL;
|
3318 | 0bab00f3 | bellard | int fd = -1, ret, err, val; |
3319 | 951f1351 | bellard | int is_listen = 0; |
3320 | 951f1351 | bellard | int is_waitconnect = 1; |
3321 | f7499989 | pbrook | int do_nodelay = 0; |
3322 | 951f1351 | bellard | const char *ptr; |
3323 | 0bab00f3 | bellard | struct sockaddr_in saddr;
|
3324 | ffd843bc | ths | #ifndef _WIN32
|
3325 | ffd843bc | ths | struct sockaddr_un uaddr;
|
3326 | ffd843bc | ths | #endif
|
3327 | ffd843bc | ths | struct sockaddr *addr;
|
3328 | ffd843bc | ths | socklen_t addrlen; |
3329 | 0bab00f3 | bellard | |
3330 | ffd843bc | ths | #ifndef _WIN32
|
3331 | ffd843bc | ths | if (is_unix) {
|
3332 | ffd843bc | ths | addr = (struct sockaddr *)&uaddr;
|
3333 | ffd843bc | ths | addrlen = sizeof(uaddr);
|
3334 | ffd843bc | ths | if (parse_unix_path(&uaddr, host_str) < 0) |
3335 | ffd843bc | ths | goto fail;
|
3336 | ffd843bc | ths | } else
|
3337 | ffd843bc | ths | #endif
|
3338 | ffd843bc | ths | { |
3339 | ffd843bc | ths | addr = (struct sockaddr *)&saddr;
|
3340 | ffd843bc | ths | addrlen = sizeof(saddr);
|
3341 | ffd843bc | ths | if (parse_host_port(&saddr, host_str) < 0) |
3342 | ffd843bc | ths | goto fail;
|
3343 | ffd843bc | ths | } |
3344 | 0bab00f3 | bellard | |
3345 | 951f1351 | bellard | ptr = host_str; |
3346 | 951f1351 | bellard | while((ptr = strchr(ptr,','))) { |
3347 | 951f1351 | bellard | ptr++; |
3348 | 951f1351 | bellard | if (!strncmp(ptr,"server",6)) { |
3349 | 951f1351 | bellard | is_listen = 1;
|
3350 | 951f1351 | bellard | } else if (!strncmp(ptr,"nowait",6)) { |
3351 | 951f1351 | bellard | is_waitconnect = 0;
|
3352 | f7499989 | pbrook | } else if (!strncmp(ptr,"nodelay",6)) { |
3353 | f7499989 | pbrook | do_nodelay = 1;
|
3354 | 951f1351 | bellard | } else {
|
3355 | 951f1351 | bellard | printf("Unknown option: %s\n", ptr);
|
3356 | 951f1351 | bellard | goto fail;
|
3357 | 951f1351 | bellard | } |
3358 | 951f1351 | bellard | } |
3359 | 951f1351 | bellard | if (!is_listen)
|
3360 | 951f1351 | bellard | is_waitconnect = 0;
|
3361 | 951f1351 | bellard | |
3362 | 0bab00f3 | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3363 | 0bab00f3 | bellard | if (!chr)
|
3364 | 0bab00f3 | bellard | goto fail;
|
3365 | 0bab00f3 | bellard | s = qemu_mallocz(sizeof(TCPCharDriver));
|
3366 | 0bab00f3 | bellard | if (!s)
|
3367 | 0bab00f3 | bellard | goto fail;
|
3368 | ffd843bc | ths | |
3369 | ffd843bc | ths | #ifndef _WIN32
|
3370 | ffd843bc | ths | if (is_unix)
|
3371 | ffd843bc | ths | fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
3372 | ffd843bc | ths | else
|
3373 | ffd843bc | ths | #endif
|
3374 | ffd843bc | ths | fd = socket(PF_INET, SOCK_STREAM, 0);
|
3375 | 5fafdf24 | ths | |
3376 | 5fafdf24 | ths | if (fd < 0) |
3377 | 0bab00f3 | bellard | goto fail;
|
3378 | 951f1351 | bellard | |
3379 | 951f1351 | bellard | if (!is_waitconnect)
|
3380 | 951f1351 | bellard | socket_set_nonblock(fd); |
3381 | 0bab00f3 | bellard | |
3382 | 0bab00f3 | bellard | s->connected = 0;
|
3383 | 0bab00f3 | bellard | s->fd = -1;
|
3384 | 0bab00f3 | bellard | s->listen_fd = -1;
|
3385 | ffd843bc | ths | s->is_unix = is_unix; |
3386 | f7499989 | pbrook | s->do_nodelay = do_nodelay && !is_unix; |
3387 | ffd843bc | ths | |
3388 | ffd843bc | ths | chr->opaque = s; |
3389 | ffd843bc | ths | chr->chr_write = tcp_chr_write; |
3390 | ffd843bc | ths | chr->chr_close = tcp_chr_close; |
3391 | ffd843bc | ths | |
3392 | 0bab00f3 | bellard | if (is_listen) {
|
3393 | 0bab00f3 | bellard | /* allow fast reuse */
|
3394 | ffd843bc | ths | #ifndef _WIN32
|
3395 | ffd843bc | ths | if (is_unix) {
|
3396 | ffd843bc | ths | char path[109]; |
3397 | ffd843bc | ths | strncpy(path, uaddr.sun_path, 108);
|
3398 | ffd843bc | ths | path[108] = 0; |
3399 | ffd843bc | ths | unlink(path); |
3400 | ffd843bc | ths | } else
|
3401 | ffd843bc | ths | #endif
|
3402 | ffd843bc | ths | { |
3403 | ffd843bc | ths | val = 1;
|
3404 | ffd843bc | ths | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
3405 | ffd843bc | ths | } |
3406 | 3b46e624 | ths | |
3407 | ffd843bc | ths | ret = bind(fd, addr, addrlen); |
3408 | ffd843bc | ths | if (ret < 0) |
3409 | 0bab00f3 | bellard | goto fail;
|
3410 | ffd843bc | ths | |
3411 | 0bab00f3 | bellard | ret = listen(fd, 0);
|
3412 | 0bab00f3 | bellard | if (ret < 0) |
3413 | 0bab00f3 | bellard | goto fail;
|
3414 | ffd843bc | ths | |
3415 | 0bab00f3 | bellard | s->listen_fd = fd; |
3416 | 0bab00f3 | bellard | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
3417 | 951f1351 | bellard | if (is_telnet)
|
3418 | 951f1351 | bellard | s->do_telnetopt = 1;
|
3419 | 0bab00f3 | bellard | } else {
|
3420 | 0bab00f3 | bellard | for(;;) {
|
3421 | ffd843bc | ths | ret = connect(fd, addr, addrlen); |
3422 | 0bab00f3 | bellard | if (ret < 0) { |
3423 | 0bab00f3 | bellard | err = socket_error(); |
3424 | 0bab00f3 | bellard | if (err == EINTR || err == EWOULDBLOCK) {
|
3425 | 0bab00f3 | bellard | } else if (err == EINPROGRESS) { |
3426 | 0bab00f3 | bellard | break;
|
3427 | f5b12268 | ths | #ifdef _WIN32
|
3428 | f5b12268 | ths | } else if (err == WSAEALREADY) { |
3429 | f5b12268 | ths | break;
|
3430 | f5b12268 | ths | #endif
|
3431 | 0bab00f3 | bellard | } else {
|
3432 | 0bab00f3 | bellard | goto fail;
|
3433 | 0bab00f3 | bellard | } |
3434 | 0bab00f3 | bellard | } else {
|
3435 | 0bab00f3 | bellard | s->connected = 1;
|
3436 | 0bab00f3 | bellard | break;
|
3437 | 0bab00f3 | bellard | } |
3438 | 0bab00f3 | bellard | } |
3439 | 0bab00f3 | bellard | s->fd = fd; |
3440 | f7499989 | pbrook | socket_set_nodelay(fd); |
3441 | 0bab00f3 | bellard | if (s->connected)
|
3442 | 0bab00f3 | bellard | tcp_chr_connect(chr); |
3443 | 0bab00f3 | bellard | else
|
3444 | 0bab00f3 | bellard | qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
|
3445 | 0bab00f3 | bellard | } |
3446 | 3b46e624 | ths | |
3447 | 951f1351 | bellard | if (is_listen && is_waitconnect) {
|
3448 | 951f1351 | bellard | printf("QEMU waiting for connection on: %s\n", host_str);
|
3449 | 951f1351 | bellard | tcp_chr_accept(chr); |
3450 | 951f1351 | bellard | socket_set_nonblock(s->listen_fd); |
3451 | 951f1351 | bellard | } |
3452 | 951f1351 | bellard | |
3453 | 0bab00f3 | bellard | return chr;
|
3454 | 0bab00f3 | bellard | fail:
|
3455 | 0bab00f3 | bellard | if (fd >= 0) |
3456 | 0bab00f3 | bellard | closesocket(fd); |
3457 | 0bab00f3 | bellard | qemu_free(s); |
3458 | 0bab00f3 | bellard | qemu_free(chr); |
3459 | 0bab00f3 | bellard | return NULL; |
3460 | 0bab00f3 | bellard | } |
3461 | 0bab00f3 | bellard | |
3462 | 82c643ff | bellard | CharDriverState *qemu_chr_open(const char *filename) |
3463 | 82c643ff | bellard | { |
3464 | f8d179e3 | bellard | const char *p; |
3465 | fd1dff4b | bellard | |
3466 | 82c643ff | bellard | if (!strcmp(filename, "vc")) { |
3467 | af3a9031 | ths | return text_console_init(&display_state, 0); |
3468 | af3a9031 | ths | } else if (strstart(filename, "vc:", &p)) { |
3469 | af3a9031 | ths | return text_console_init(&display_state, p);
|
3470 | 82c643ff | bellard | } else if (!strcmp(filename, "null")) { |
3471 | 82c643ff | bellard | return qemu_chr_open_null();
|
3472 | 5fafdf24 | ths | } else
|
3473 | 0bab00f3 | bellard | if (strstart(filename, "tcp:", &p)) { |
3474 | ffd843bc | ths | return qemu_chr_open_tcp(p, 0, 0); |
3475 | 0bab00f3 | bellard | } else
|
3476 | 951f1351 | bellard | if (strstart(filename, "telnet:", &p)) { |
3477 | ffd843bc | ths | return qemu_chr_open_tcp(p, 1, 0); |
3478 | 0bab00f3 | bellard | } else
|
3479 | 0bab00f3 | bellard | if (strstart(filename, "udp:", &p)) { |
3480 | 0bab00f3 | bellard | return qemu_chr_open_udp(p);
|
3481 | 0bab00f3 | bellard | } else
|
3482 | 20d8a3ed | ths | if (strstart(filename, "mon:", &p)) { |
3483 | 20d8a3ed | ths | CharDriverState *drv = qemu_chr_open(p); |
3484 | 20d8a3ed | ths | if (drv) {
|
3485 | 20d8a3ed | ths | drv = qemu_chr_open_mux(drv); |
3486 | 20d8a3ed | ths | monitor_init(drv, !nographic); |
3487 | 20d8a3ed | ths | return drv;
|
3488 | 20d8a3ed | ths | } |
3489 | 20d8a3ed | ths | printf("Unable to open driver: %s\n", p);
|
3490 | 20d8a3ed | ths | return 0; |
3491 | 20d8a3ed | ths | } else
|
3492 | 7664728b | bellard | #ifndef _WIN32
|
3493 | ffd843bc | ths | if (strstart(filename, "unix:", &p)) { |
3494 | ffd843bc | ths | return qemu_chr_open_tcp(p, 0, 1); |
3495 | ffd843bc | ths | } else if (strstart(filename, "file:", &p)) { |
3496 | f8d179e3 | bellard | return qemu_chr_open_file_out(p);
|
3497 | f8d179e3 | bellard | } else if (strstart(filename, "pipe:", &p)) { |
3498 | f8d179e3 | bellard | return qemu_chr_open_pipe(p);
|
3499 | 7664728b | bellard | } else if (!strcmp(filename, "pty")) { |
3500 | 82c643ff | bellard | return qemu_chr_open_pty();
|
3501 | 82c643ff | bellard | } else if (!strcmp(filename, "stdio")) { |
3502 | 82c643ff | bellard | return qemu_chr_open_stdio();
|
3503 | 5fafdf24 | ths | } else
|
3504 | f8d179e3 | bellard | #if defined(__linux__)
|
3505 | e57a8c0e | bellard | if (strstart(filename, "/dev/parport", NULL)) { |
3506 | e57a8c0e | bellard | return qemu_chr_open_pp(filename);
|
3507 | 5fafdf24 | ths | } else
|
3508 | aec62507 | ths | #endif
|
3509 | 3fda388a | ths | #if defined(__linux__) || defined(__sun__)
|
3510 | f8d179e3 | bellard | if (strstart(filename, "/dev/", NULL)) { |
3511 | f8d179e3 | bellard | return qemu_chr_open_tty(filename);
|
3512 | 3fda388a | ths | } else
|
3513 | 3fda388a | ths | #endif
|
3514 | aec62507 | ths | #else /* !_WIN32 */ |
3515 | f331110f | bellard | if (strstart(filename, "COM", NULL)) { |
3516 | f331110f | bellard | return qemu_chr_open_win(filename);
|
3517 | f331110f | bellard | } else
|
3518 | f331110f | bellard | if (strstart(filename, "pipe:", &p)) { |
3519 | f331110f | bellard | return qemu_chr_open_win_pipe(p);
|
3520 | f331110f | bellard | } else
|
3521 | 72d46479 | ths | if (strstart(filename, "con:", NULL)) { |
3522 | 72d46479 | ths | return qemu_chr_open_win_con(filename);
|
3523 | 72d46479 | ths | } else
|
3524 | f331110f | bellard | if (strstart(filename, "file:", &p)) { |
3525 | f331110f | bellard | return qemu_chr_open_win_file_out(p);
|
3526 | 2e4d9fb1 | aurel32 | } else
|
3527 | 2e4d9fb1 | aurel32 | #endif
|
3528 | 2e4d9fb1 | aurel32 | #ifdef CONFIG_BRLAPI
|
3529 | 2e4d9fb1 | aurel32 | if (!strcmp(filename, "braille")) { |
3530 | 2e4d9fb1 | aurel32 | return chr_baum_init();
|
3531 | 2e4d9fb1 | aurel32 | } else
|
3532 | f331110f | bellard | #endif
|
3533 | 82c643ff | bellard | { |
3534 | 82c643ff | bellard | return NULL; |
3535 | 82c643ff | bellard | } |
3536 | 82c643ff | bellard | } |
3537 | 82c643ff | bellard | |
3538 | f331110f | bellard | void qemu_chr_close(CharDriverState *chr)
|
3539 | f331110f | bellard | { |
3540 | f331110f | bellard | if (chr->chr_close)
|
3541 | f331110f | bellard | chr->chr_close(chr); |
3542 | a11d070e | balrog | qemu_free(chr); |
3543 | f331110f | bellard | } |
3544 | f331110f | bellard | |
3545 | 80cabfad | bellard | /***********************************************************/
|
3546 | 7c9d8e07 | bellard | /* network device redirectors */
|
3547 | 330d0414 | bellard | |
3548 | 3f4afa14 | j_mayer | __attribute__ (( unused )) |
3549 | 9596ebb7 | pbrook | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
3550 | c20709aa | bellard | { |
3551 | c20709aa | bellard | int len, i, j, c;
|
3552 | c20709aa | bellard | |
3553 | c20709aa | bellard | for(i=0;i<size;i+=16) { |
3554 | c20709aa | bellard | len = size - i; |
3555 | c20709aa | bellard | if (len > 16) |
3556 | c20709aa | bellard | len = 16;
|
3557 | c20709aa | bellard | fprintf(f, "%08x ", i);
|
3558 | c20709aa | bellard | for(j=0;j<16;j++) { |
3559 | c20709aa | bellard | if (j < len)
|
3560 | c20709aa | bellard | fprintf(f, " %02x", buf[i+j]);
|
3561 | c20709aa | bellard | else
|
3562 | c20709aa | bellard | fprintf(f, " ");
|
3563 | c20709aa | bellard | } |
3564 | c20709aa | bellard | fprintf(f, " ");
|
3565 | c20709aa | bellard | for(j=0;j<len;j++) { |
3566 | c20709aa | bellard | c = buf[i+j]; |
3567 | c20709aa | bellard | if (c < ' ' || c > '~') |
3568 | c20709aa | bellard | c = '.';
|
3569 | c20709aa | bellard | fprintf(f, "%c", c);
|
3570 | c20709aa | bellard | } |
3571 | c20709aa | bellard | fprintf(f, "\n");
|
3572 | c20709aa | bellard | } |
3573 | c20709aa | bellard | } |
3574 | c20709aa | bellard | |
3575 | 7c9d8e07 | bellard | static int parse_macaddr(uint8_t *macaddr, const char *p) |
3576 | c20709aa | bellard | { |
3577 | 7c9d8e07 | bellard | int i;
|
3578 | 76ea08f9 | balrog | char *last_char;
|
3579 | 76ea08f9 | balrog | long int offset; |
3580 | 76ea08f9 | balrog | |
3581 | 76ea08f9 | balrog | errno = 0;
|
3582 | 76ea08f9 | balrog | offset = strtol(p, &last_char, 0);
|
3583 | 76ea08f9 | balrog | if (0 == errno && '\0' == *last_char && |
3584 | 76ea08f9 | balrog | offset >= 0 && offset <= 0xFFFFFF) { |
3585 | 76ea08f9 | balrog | macaddr[3] = (offset & 0xFF0000) >> 16; |
3586 | 76ea08f9 | balrog | macaddr[4] = (offset & 0xFF00) >> 8; |
3587 | 76ea08f9 | balrog | macaddr[5] = offset & 0xFF; |
3588 | 76ea08f9 | balrog | return 0; |
3589 | 76ea08f9 | balrog | } else {
|
3590 | 76ea08f9 | balrog | for(i = 0; i < 6; i++) { |
3591 | 76ea08f9 | balrog | macaddr[i] = strtol(p, (char **)&p, 16); |
3592 | 76ea08f9 | balrog | if (i == 5) { |
3593 | 76ea08f9 | balrog | if (*p != '\0') |
3594 | 76ea08f9 | balrog | return -1; |
3595 | 76ea08f9 | balrog | } else {
|
3596 | 76ea08f9 | balrog | if (*p != ':' && *p != '-') |
3597 | 76ea08f9 | balrog | return -1; |
3598 | 76ea08f9 | balrog | p++; |
3599 | 76ea08f9 | balrog | } |
3600 | 7c9d8e07 | bellard | } |
3601 | 76ea08f9 | balrog | return 0; |
3602 | 7c9d8e07 | bellard | } |
3603 | 76ea08f9 | balrog | |
3604 | 76ea08f9 | balrog | return -1; |
3605 | c20709aa | bellard | } |
3606 | 67b915a5 | bellard | |
3607 | 7c9d8e07 | bellard | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
3608 | 67b915a5 | bellard | { |
3609 | 7c9d8e07 | bellard | const char *p, *p1; |
3610 | 7c9d8e07 | bellard | int len;
|
3611 | 7c9d8e07 | bellard | p = *pp; |
3612 | 7c9d8e07 | bellard | p1 = strchr(p, sep); |
3613 | 7c9d8e07 | bellard | if (!p1)
|
3614 | 7c9d8e07 | bellard | return -1; |
3615 | 7c9d8e07 | bellard | len = p1 - p; |
3616 | 7c9d8e07 | bellard | p1++; |
3617 | 7c9d8e07 | bellard | if (buf_size > 0) { |
3618 | 7c9d8e07 | bellard | if (len > buf_size - 1) |
3619 | 7c9d8e07 | bellard | len = buf_size - 1;
|
3620 | 7c9d8e07 | bellard | memcpy(buf, p, len); |
3621 | 7c9d8e07 | bellard | buf[len] = '\0';
|
3622 | 7c9d8e07 | bellard | } |
3623 | 7c9d8e07 | bellard | *pp = p1; |
3624 | 7c9d8e07 | bellard | return 0; |
3625 | c20709aa | bellard | } |
3626 | c20709aa | bellard | |
3627 | 951f1351 | bellard | int parse_host_src_port(struct sockaddr_in *haddr, |
3628 | 951f1351 | bellard | struct sockaddr_in *saddr,
|
3629 | 951f1351 | bellard | const char *input_str) |
3630 | 951f1351 | bellard | { |
3631 | 951f1351 | bellard | char *str = strdup(input_str);
|
3632 | 951f1351 | bellard | char *host_str = str;
|
3633 | 951f1351 | bellard | char *src_str;
|
3634 | 951f1351 | bellard | char *ptr;
|
3635 | 951f1351 | bellard | |
3636 | 951f1351 | bellard | /*
|
3637 | 951f1351 | bellard | * Chop off any extra arguments at the end of the string which
|
3638 | 951f1351 | bellard | * would start with a comma, then fill in the src port information
|
3639 | 951f1351 | bellard | * if it was provided else use the "any address" and "any port".
|
3640 | 951f1351 | bellard | */
|
3641 | 951f1351 | bellard | if ((ptr = strchr(str,','))) |
3642 | 951f1351 | bellard | *ptr = '\0';
|
3643 | 951f1351 | bellard | |
3644 | 951f1351 | bellard | if ((src_str = strchr(input_str,'@'))) { |
3645 | 951f1351 | bellard | *src_str = '\0';
|
3646 | 951f1351 | bellard | src_str++; |
3647 | 951f1351 | bellard | } |
3648 | 951f1351 | bellard | |
3649 | 951f1351 | bellard | if (parse_host_port(haddr, host_str) < 0) |
3650 | 951f1351 | bellard | goto fail;
|
3651 | 951f1351 | bellard | |
3652 | 951f1351 | bellard | if (!src_str || *src_str == '\0') |
3653 | 951f1351 | bellard | src_str = ":0";
|
3654 | 951f1351 | bellard | |
3655 | 951f1351 | bellard | if (parse_host_port(saddr, src_str) < 0) |
3656 | 951f1351 | bellard | goto fail;
|
3657 | 951f1351 | bellard | |
3658 | 951f1351 | bellard | free(str); |
3659 | 951f1351 | bellard | return(0); |
3660 | 951f1351 | bellard | |
3661 | 951f1351 | bellard | fail:
|
3662 | 951f1351 | bellard | free(str); |
3663 | 951f1351 | bellard | return -1; |
3664 | 951f1351 | bellard | } |
3665 | 951f1351 | bellard | |
3666 | 7c9d8e07 | bellard | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
3667 | 7c9d8e07 | bellard | { |
3668 | 7c9d8e07 | bellard | char buf[512]; |
3669 | 7c9d8e07 | bellard | struct hostent *he;
|
3670 | 7c9d8e07 | bellard | const char *p, *r; |
3671 | 7c9d8e07 | bellard | int port;
|
3672 | 7c9d8e07 | bellard | |
3673 | 7c9d8e07 | bellard | p = str; |
3674 | 7c9d8e07 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
3675 | 7c9d8e07 | bellard | return -1; |
3676 | 7c9d8e07 | bellard | saddr->sin_family = AF_INET; |
3677 | 7c9d8e07 | bellard | if (buf[0] == '\0') { |
3678 | 7c9d8e07 | bellard | saddr->sin_addr.s_addr = 0;
|
3679 | 7c9d8e07 | bellard | } else {
|
3680 | 7c9d8e07 | bellard | if (isdigit(buf[0])) { |
3681 | 7c9d8e07 | bellard | if (!inet_aton(buf, &saddr->sin_addr))
|
3682 | 7c9d8e07 | bellard | return -1; |
3683 | 7c9d8e07 | bellard | } else {
|
3684 | 7c9d8e07 | bellard | if ((he = gethostbyname(buf)) == NULL) |
3685 | 7c9d8e07 | bellard | return - 1; |
3686 | 7c9d8e07 | bellard | saddr->sin_addr = *(struct in_addr *)he->h_addr;
|
3687 | 7c9d8e07 | bellard | } |
3688 | 7c9d8e07 | bellard | } |
3689 | 7c9d8e07 | bellard | port = strtol(p, (char **)&r, 0); |
3690 | 7c9d8e07 | bellard | if (r == p)
|
3691 | 7c9d8e07 | bellard | return -1; |
3692 | 7c9d8e07 | bellard | saddr->sin_port = htons(port); |
3693 | 7c9d8e07 | bellard | return 0; |
3694 | 7c9d8e07 | bellard | } |
3695 | c20709aa | bellard | |
3696 | 52f61fde | ths | #ifndef _WIN32
|
3697 | 52f61fde | ths | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) |
3698 | ffd843bc | ths | { |
3699 | ffd843bc | ths | const char *p; |
3700 | ffd843bc | ths | int len;
|
3701 | ffd843bc | ths | |
3702 | ffd843bc | ths | len = MIN(108, strlen(str));
|
3703 | ffd843bc | ths | p = strchr(str, ',');
|
3704 | ffd843bc | ths | if (p)
|
3705 | ffd843bc | ths | len = MIN(len, p - str); |
3706 | ffd843bc | ths | |
3707 | ffd843bc | ths | memset(uaddr, 0, sizeof(*uaddr)); |
3708 | ffd843bc | ths | |
3709 | ffd843bc | ths | uaddr->sun_family = AF_UNIX; |
3710 | ffd843bc | ths | memcpy(uaddr->sun_path, str, len); |
3711 | ffd843bc | ths | |
3712 | ffd843bc | ths | return 0; |
3713 | ffd843bc | ths | } |
3714 | 52f61fde | ths | #endif
|
3715 | ffd843bc | ths | |
3716 | 7c9d8e07 | bellard | /* find or alloc a new VLAN */
|
3717 | 7c9d8e07 | bellard | VLANState *qemu_find_vlan(int id)
|
3718 | c20709aa | bellard | { |
3719 | 7c9d8e07 | bellard | VLANState **pvlan, *vlan; |
3720 | 7c9d8e07 | bellard | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
3721 | 7c9d8e07 | bellard | if (vlan->id == id)
|
3722 | 7c9d8e07 | bellard | return vlan;
|
3723 | 7c9d8e07 | bellard | } |
3724 | 7c9d8e07 | bellard | vlan = qemu_mallocz(sizeof(VLANState));
|
3725 | 7c9d8e07 | bellard | if (!vlan)
|
3726 | 7c9d8e07 | bellard | return NULL; |
3727 | 7c9d8e07 | bellard | vlan->id = id; |
3728 | 7c9d8e07 | bellard | vlan->next = NULL;
|
3729 | 7c9d8e07 | bellard | pvlan = &first_vlan; |
3730 | 7c9d8e07 | bellard | while (*pvlan != NULL) |
3731 | 7c9d8e07 | bellard | pvlan = &(*pvlan)->next; |
3732 | 7c9d8e07 | bellard | *pvlan = vlan; |
3733 | 7c9d8e07 | bellard | return vlan;
|
3734 | c20709aa | bellard | } |
3735 | c20709aa | bellard | |
3736 | 7c9d8e07 | bellard | VLANClientState *qemu_new_vlan_client(VLANState *vlan, |
3737 | d861b05e | pbrook | IOReadHandler *fd_read, |
3738 | d861b05e | pbrook | IOCanRWHandler *fd_can_read, |
3739 | d861b05e | pbrook | void *opaque)
|
3740 | c20709aa | bellard | { |
3741 | 7c9d8e07 | bellard | VLANClientState *vc, **pvc; |
3742 | 7c9d8e07 | bellard | vc = qemu_mallocz(sizeof(VLANClientState));
|
3743 | 7c9d8e07 | bellard | if (!vc)
|
3744 | 7c9d8e07 | bellard | return NULL; |
3745 | 7c9d8e07 | bellard | vc->fd_read = fd_read; |
3746 | d861b05e | pbrook | vc->fd_can_read = fd_can_read; |
3747 | 7c9d8e07 | bellard | vc->opaque = opaque; |
3748 | 7c9d8e07 | bellard | vc->vlan = vlan; |
3749 | 7c9d8e07 | bellard | |
3750 | 7c9d8e07 | bellard | vc->next = NULL;
|
3751 | 7c9d8e07 | bellard | pvc = &vlan->first_client; |
3752 | 7c9d8e07 | bellard | while (*pvc != NULL) |
3753 | 7c9d8e07 | bellard | pvc = &(*pvc)->next; |
3754 | 7c9d8e07 | bellard | *pvc = vc; |
3755 | 7c9d8e07 | bellard | return vc;
|
3756 | c20709aa | bellard | } |
3757 | c20709aa | bellard | |
3758 | d861b05e | pbrook | int qemu_can_send_packet(VLANClientState *vc1)
|
3759 | d861b05e | pbrook | { |
3760 | d861b05e | pbrook | VLANState *vlan = vc1->vlan; |
3761 | d861b05e | pbrook | VLANClientState *vc; |
3762 | d861b05e | pbrook | |
3763 | d861b05e | pbrook | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
3764 | d861b05e | pbrook | if (vc != vc1) {
|
3765 | fbd1711d | balrog | if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
|
3766 | fbd1711d | balrog | return 1; |
3767 | d861b05e | pbrook | } |
3768 | d861b05e | pbrook | } |
3769 | fbd1711d | balrog | return 0; |
3770 | d861b05e | pbrook | } |
3771 | d861b05e | pbrook | |
3772 | 7c9d8e07 | bellard | void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size) |
3773 | c20709aa | bellard | { |
3774 | 7c9d8e07 | bellard | VLANState *vlan = vc1->vlan; |
3775 | 7c9d8e07 | bellard | VLANClientState *vc; |
3776 | 7c9d8e07 | bellard | |
3777 | 7c9d8e07 | bellard | #if 0
|
3778 | 7c9d8e07 | bellard | printf("vlan %d send:\n", vlan->id);
|
3779 | 7c9d8e07 | bellard | hex_dump(stdout, buf, size);
|
3780 | 7c9d8e07 | bellard | #endif
|
3781 | 7c9d8e07 | bellard | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
3782 | 7c9d8e07 | bellard | if (vc != vc1) {
|
3783 | 7c9d8e07 | bellard | vc->fd_read(vc->opaque, buf, size); |
3784 | 7c9d8e07 | bellard | } |
3785 | 7c9d8e07 | bellard | } |
3786 | 67b915a5 | bellard | } |
3787 | 67b915a5 | bellard | |
3788 | c20709aa | bellard | #if defined(CONFIG_SLIRP)
|
3789 | c20709aa | bellard | |
3790 | c20709aa | bellard | /* slirp network adapter */
|
3791 | c20709aa | bellard | |
3792 | c20709aa | bellard | static int slirp_inited; |
3793 | 7c9d8e07 | bellard | static VLANClientState *slirp_vc;
|
3794 | c20709aa | bellard | |
3795 | c20709aa | bellard | int slirp_can_output(void) |
3796 | c20709aa | bellard | { |
3797 | 3b7f5d47 | pbrook | return !slirp_vc || qemu_can_send_packet(slirp_vc);
|
3798 | c20709aa | bellard | } |
3799 | c20709aa | bellard | |
3800 | c20709aa | bellard | void slirp_output(const uint8_t *pkt, int pkt_len) |
3801 | 67b915a5 | bellard | { |
3802 | c20709aa | bellard | #if 0
|
3803 | 7c9d8e07 | bellard | printf("slirp output:\n");
|
3804 | c20709aa | bellard | hex_dump(stdout, pkt, pkt_len);
|
3805 | c20709aa | bellard | #endif
|
3806 | 3b7f5d47 | pbrook | if (!slirp_vc)
|
3807 | 3b7f5d47 | pbrook | return;
|
3808 | 7c9d8e07 | bellard | qemu_send_packet(slirp_vc, pkt, pkt_len); |
3809 | 67b915a5 | bellard | } |
3810 | 67b915a5 | bellard | |
3811 | 7c9d8e07 | bellard | static void slirp_receive(void *opaque, const uint8_t *buf, int size) |
3812 | c20709aa | bellard | { |
3813 | c20709aa | bellard | #if 0
|
3814 | 7c9d8e07 | bellard | printf("slirp input:\n");
|
3815 | c20709aa | bellard | hex_dump(stdout, buf, size);
|
3816 | c20709aa | bellard | #endif
|
3817 | c20709aa | bellard | slirp_input(buf, size); |
3818 | c20709aa | bellard | } |
3819 | c20709aa | bellard | |
3820 | 7c9d8e07 | bellard | static int net_slirp_init(VLANState *vlan) |
3821 | c20709aa | bellard | { |
3822 | c20709aa | bellard | if (!slirp_inited) {
|
3823 | c20709aa | bellard | slirp_inited = 1;
|
3824 | c20709aa | bellard | slirp_init(); |
3825 | c20709aa | bellard | } |
3826 | 5fafdf24 | ths | slirp_vc = qemu_new_vlan_client(vlan, |
3827 | d861b05e | pbrook | slirp_receive, NULL, NULL); |
3828 | 7c9d8e07 | bellard | snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector"); |
3829 | 9bf05444 | bellard | return 0; |
3830 | 9bf05444 | bellard | } |
3831 | 9bf05444 | bellard | |
3832 | 9bf05444 | bellard | static void net_slirp_redir(const char *redir_str) |
3833 | 9bf05444 | bellard | { |
3834 | 9bf05444 | bellard | int is_udp;
|
3835 | 9bf05444 | bellard | char buf[256], *r; |
3836 | 9bf05444 | bellard | const char *p; |
3837 | 9bf05444 | bellard | struct in_addr guest_addr;
|
3838 | 9bf05444 | bellard | int host_port, guest_port;
|
3839 | 3b46e624 | ths | |
3840 | 9bf05444 | bellard | if (!slirp_inited) {
|
3841 | 9bf05444 | bellard | slirp_inited = 1;
|
3842 | 9bf05444 | bellard | slirp_init(); |
3843 | 9bf05444 | bellard | } |
3844 | 9bf05444 | bellard | |
3845 | 9bf05444 | bellard | p = redir_str; |
3846 | 9bf05444 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
3847 | 9bf05444 | bellard | goto fail;
|
3848 | 9bf05444 | bellard | if (!strcmp(buf, "tcp")) { |
3849 | 9bf05444 | bellard | is_udp = 0;
|
3850 | 9bf05444 | bellard | } else if (!strcmp(buf, "udp")) { |
3851 | 9bf05444 | bellard | is_udp = 1;
|
3852 | 9bf05444 | bellard | } else {
|
3853 | 9bf05444 | bellard | goto fail;
|
3854 | 9bf05444 | bellard | } |
3855 | 9bf05444 | bellard | |
3856 | 9bf05444 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
3857 | 9bf05444 | bellard | goto fail;
|
3858 | 9bf05444 | bellard | host_port = strtol(buf, &r, 0);
|
3859 | 9bf05444 | bellard | if (r == buf)
|
3860 | 9bf05444 | bellard | goto fail;
|
3861 | 9bf05444 | bellard | |
3862 | 9bf05444 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
3863 | 9bf05444 | bellard | goto fail;
|
3864 | 9bf05444 | bellard | if (buf[0] == '\0') { |
3865 | 9bf05444 | bellard | pstrcpy(buf, sizeof(buf), "10.0.2.15"); |
3866 | 9bf05444 | bellard | } |
3867 | 9bf05444 | bellard | if (!inet_aton(buf, &guest_addr))
|
3868 | 9bf05444 | bellard | goto fail;
|
3869 | 3b46e624 | ths | |
3870 | 9bf05444 | bellard | guest_port = strtol(p, &r, 0);
|
3871 | 9bf05444 | bellard | if (r == p)
|
3872 | 9bf05444 | bellard | goto fail;
|
3873 | 3b46e624 | ths | |
3874 | 9bf05444 | bellard | if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { |
3875 | 9bf05444 | bellard | fprintf(stderr, "qemu: could not set up redirection\n");
|
3876 | 9bf05444 | bellard | exit(1);
|
3877 | 9bf05444 | bellard | } |
3878 | 9bf05444 | bellard | return;
|
3879 | 9bf05444 | bellard | fail:
|
3880 | 9bf05444 | bellard | fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
|
3881 | 9bf05444 | bellard | exit(1);
|
3882 | 9bf05444 | bellard | } |
3883 | 3b46e624 | ths | |
3884 | c94c8d64 | bellard | #ifndef _WIN32
|
3885 | c94c8d64 | bellard | |
3886 | 9d728e8c | bellard | char smb_dir[1024]; |
3887 | 9d728e8c | bellard | |
3888 | 044fae83 | balrog | static void erase_dir(char *dir_name) |
3889 | 9d728e8c | bellard | { |
3890 | 9d728e8c | bellard | DIR *d; |
3891 | 9d728e8c | bellard | struct dirent *de;
|
3892 | 9d728e8c | bellard | char filename[1024]; |
3893 | 9d728e8c | bellard | |
3894 | 9d728e8c | bellard | /* erase all the files in the directory */
|
3895 | 044fae83 | balrog | if ((d = opendir(dir_name)) != 0) { |
3896 | 044fae83 | balrog | for(;;) {
|
3897 | 044fae83 | balrog | de = readdir(d); |
3898 | 044fae83 | balrog | if (!de)
|
3899 | 044fae83 | balrog | break;
|
3900 | 044fae83 | balrog | if (strcmp(de->d_name, ".") != 0 && |
3901 | 044fae83 | balrog | strcmp(de->d_name, "..") != 0) { |
3902 | 044fae83 | balrog | snprintf(filename, sizeof(filename), "%s/%s", |
3903 | 044fae83 | balrog | smb_dir, de->d_name); |
3904 | 044fae83 | balrog | if (unlink(filename) != 0) /* is it a directory? */ |
3905 | 044fae83 | balrog | erase_dir(filename); |
3906 | 044fae83 | balrog | } |
3907 | 9d728e8c | bellard | } |
3908 | 044fae83 | balrog | closedir(d); |
3909 | 044fae83 | balrog | rmdir(dir_name); |
3910 | 9d728e8c | bellard | } |
3911 | 044fae83 | balrog | } |
3912 | 044fae83 | balrog | |
3913 | 044fae83 | balrog | /* automatic user mode samba server configuration */
|
3914 | 044fae83 | balrog | static void smb_exit(void) |
3915 | 044fae83 | balrog | { |
3916 | 044fae83 | balrog | erase_dir(smb_dir); |
3917 | 9d728e8c | bellard | } |
3918 | 9d728e8c | bellard | |
3919 | 9d728e8c | bellard | /* automatic user mode samba server configuration */
|
3920 | 9596ebb7 | pbrook | static void net_slirp_smb(const char *exported_dir) |
3921 | 9d728e8c | bellard | { |
3922 | 9d728e8c | bellard | char smb_conf[1024]; |
3923 | 9d728e8c | bellard | char smb_cmdline[1024]; |
3924 | 9d728e8c | bellard | FILE *f; |
3925 | 9d728e8c | bellard | |
3926 | 9d728e8c | bellard | if (!slirp_inited) {
|
3927 | 9d728e8c | bellard | slirp_inited = 1;
|
3928 | 9d728e8c | bellard | slirp_init(); |
3929 | 9d728e8c | bellard | } |
3930 | 9d728e8c | bellard | |
3931 | 9d728e8c | bellard | /* XXX: better tmp dir construction */
|
3932 | 9d728e8c | bellard | snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); |
3933 | 9d728e8c | bellard | if (mkdir(smb_dir, 0700) < 0) { |
3934 | 9d728e8c | bellard | fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
|
3935 | 9d728e8c | bellard | exit(1);
|
3936 | 9d728e8c | bellard | } |
3937 | 9d728e8c | bellard | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); |
3938 | 3b46e624 | ths | |
3939 | 9d728e8c | bellard | f = fopen(smb_conf, "w");
|
3940 | 9d728e8c | bellard | if (!f) {
|
3941 | 9d728e8c | bellard | fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
|
3942 | 9d728e8c | bellard | exit(1);
|
3943 | 9d728e8c | bellard | } |
3944 | 5fafdf24 | ths | fprintf(f, |
3945 | 9d728e8c | bellard | "[global]\n"
|
3946 | 157777ef | bellard | "private dir=%s\n"
|
3947 | 157777ef | bellard | "smb ports=0\n"
|
3948 | 157777ef | bellard | "socket address=127.0.0.1\n"
|
3949 | 9d728e8c | bellard | "pid directory=%s\n"
|
3950 | 9d728e8c | bellard | "lock directory=%s\n"
|
3951 | 9d728e8c | bellard | "log file=%s/log.smbd\n"
|
3952 | 9d728e8c | bellard | "smb passwd file=%s/smbpasswd\n"
|
3953 | 03ffbb69 | bellard | "security = share\n"
|
3954 | 9d728e8c | bellard | "[qemu]\n"
|
3955 | 9d728e8c | bellard | "path=%s\n"
|
3956 | 9d728e8c | bellard | "read only=no\n"
|
3957 | 9d728e8c | bellard | "guest ok=yes\n",
|
3958 | 9d728e8c | bellard | smb_dir, |
3959 | 157777ef | bellard | smb_dir, |
3960 | 9d728e8c | bellard | smb_dir, |
3961 | 9d728e8c | bellard | smb_dir, |
3962 | 9d728e8c | bellard | smb_dir, |
3963 | 9d728e8c | bellard | exported_dir |
3964 | 9d728e8c | bellard | ); |
3965 | 9d728e8c | bellard | fclose(f); |
3966 | 9d728e8c | bellard | atexit(smb_exit); |
3967 | 9d728e8c | bellard | |
3968 | a14d6c8c | pbrook | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
3969 | a14d6c8c | pbrook | SMBD_COMMAND, smb_conf); |
3970 | 3b46e624 | ths | |
3971 | 9d728e8c | bellard | slirp_add_exec(0, smb_cmdline, 4, 139); |
3972 | 9d728e8c | bellard | } |
3973 | 9bf05444 | bellard | |
3974 | c94c8d64 | bellard | #endif /* !defined(_WIN32) */ |
3975 | 31a60e22 | blueswir1 | void do_info_slirp(void) |
3976 | 31a60e22 | blueswir1 | { |
3977 | 31a60e22 | blueswir1 | slirp_stats(); |
3978 | 31a60e22 | blueswir1 | } |
3979 | c94c8d64 | bellard | |
3980 | c20709aa | bellard | #endif /* CONFIG_SLIRP */ |
3981 | c20709aa | bellard | |
3982 | c20709aa | bellard | #if !defined(_WIN32)
|
3983 | 7c9d8e07 | bellard | |
3984 | 7c9d8e07 | bellard | typedef struct TAPState { |
3985 | 7c9d8e07 | bellard | VLANClientState *vc; |
3986 | 7c9d8e07 | bellard | int fd;
|
3987 | b46a8906 | ths | char down_script[1024]; |
3988 | 7c9d8e07 | bellard | } TAPState; |
3989 | 7c9d8e07 | bellard | |
3990 | 7c9d8e07 | bellard | static void tap_receive(void *opaque, const uint8_t *buf, int size) |
3991 | 7c9d8e07 | bellard | { |
3992 | 7c9d8e07 | bellard | TAPState *s = opaque; |
3993 | 7c9d8e07 | bellard | int ret;
|
3994 | 7c9d8e07 | bellard | for(;;) {
|
3995 | 7c9d8e07 | bellard | ret = write(s->fd, buf, size); |
3996 | 7c9d8e07 | bellard | if (ret < 0 && (errno == EINTR || errno == EAGAIN)) { |
3997 | 7c9d8e07 | bellard | } else {
|
3998 | 7c9d8e07 | bellard | break;
|
3999 | 7c9d8e07 | bellard | } |
4000 | 7c9d8e07 | bellard | } |
4001 | 7c9d8e07 | bellard | } |
4002 | 7c9d8e07 | bellard | |
4003 | 7c9d8e07 | bellard | static void tap_send(void *opaque) |
4004 | 7c9d8e07 | bellard | { |
4005 | 7c9d8e07 | bellard | TAPState *s = opaque; |
4006 | 7c9d8e07 | bellard | uint8_t buf[4096];
|
4007 | 7c9d8e07 | bellard | int size;
|
4008 | 7c9d8e07 | bellard | |
4009 | d5d10bc3 | ths | #ifdef __sun__
|
4010 | d5d10bc3 | ths | struct strbuf sbuf;
|
4011 | d5d10bc3 | ths | int f = 0; |
4012 | d5d10bc3 | ths | sbuf.maxlen = sizeof(buf);
|
4013 | d5d10bc3 | ths | sbuf.buf = buf; |
4014 | d5d10bc3 | ths | size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1; |
4015 | d5d10bc3 | ths | #else
|
4016 | 7c9d8e07 | bellard | size = read(s->fd, buf, sizeof(buf));
|
4017 | d5d10bc3 | ths | #endif
|
4018 | 7c9d8e07 | bellard | if (size > 0) { |
4019 | 7c9d8e07 | bellard | qemu_send_packet(s->vc, buf, size); |
4020 | 7c9d8e07 | bellard | } |
4021 | 7c9d8e07 | bellard | } |
4022 | 7c9d8e07 | bellard | |
4023 | 7c9d8e07 | bellard | /* fd support */
|
4024 | 7c9d8e07 | bellard | |
4025 | 7c9d8e07 | bellard | static TAPState *net_tap_fd_init(VLANState *vlan, int fd) |
4026 | 7c9d8e07 | bellard | { |
4027 | 7c9d8e07 | bellard | TAPState *s; |
4028 | 7c9d8e07 | bellard | |
4029 | 7c9d8e07 | bellard | s = qemu_mallocz(sizeof(TAPState));
|
4030 | 7c9d8e07 | bellard | if (!s)
|
4031 | 7c9d8e07 | bellard | return NULL; |
4032 | 7c9d8e07 | bellard | s->fd = fd; |
4033 | d861b05e | pbrook | s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
|
4034 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, tap_send, NULL, s);
|
4035 | 7c9d8e07 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd); |
4036 | 7c9d8e07 | bellard | return s;
|
4037 | 7c9d8e07 | bellard | } |
4038 | 7c9d8e07 | bellard | |
4039 | 5c40d2bd | ths | #if defined (_BSD) || defined (__FreeBSD_kernel__)
|
4040 | 7c9d8e07 | bellard | static int tap_open(char *ifname, int ifname_size) |
4041 | 7d3505c5 | bellard | { |
4042 | 7d3505c5 | bellard | int fd;
|
4043 | 7d3505c5 | bellard | char *dev;
|
4044 | 7d3505c5 | bellard | struct stat s;
|
4045 | 67b915a5 | bellard | |
4046 | aeb30be6 | balrog | TFR(fd = open("/dev/tap", O_RDWR));
|
4047 | 7d3505c5 | bellard | if (fd < 0) { |
4048 | 7d3505c5 | bellard | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
|
4049 | 7d3505c5 | bellard | return -1; |
4050 | 7d3505c5 | bellard | } |
4051 | 7d3505c5 | bellard | |
4052 | 7d3505c5 | bellard | fstat(fd, &s); |
4053 | 7d3505c5 | bellard | dev = devname(s.st_rdev, S_IFCHR); |
4054 | 7d3505c5 | bellard | pstrcpy(ifname, ifname_size, dev); |
4055 | 7d3505c5 | bellard | |
4056 | 7d3505c5 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
4057 | 7d3505c5 | bellard | return fd;
|
4058 | 7d3505c5 | bellard | } |
4059 | ec530c81 | bellard | #elif defined(__sun__)
|
4060 | d5d10bc3 | ths | #define TUNNEWPPA (('T'<<16) | 0x0001) |
4061 | 5fafdf24 | ths | /*
|
4062 | 5fafdf24 | ths | * Allocate TAP device, returns opened fd.
|
4063 | d5d10bc3 | ths | * Stores dev name in the first arg(must be large enough).
|
4064 | 3b46e624 | ths | */
|
4065 | d5d10bc3 | ths | int tap_alloc(char *dev) |
4066 | d5d10bc3 | ths | { |
4067 | d5d10bc3 | ths | int tap_fd, if_fd, ppa = -1; |
4068 | d5d10bc3 | ths | static int ip_fd = 0; |
4069 | d5d10bc3 | ths | char *ptr;
|
4070 | d5d10bc3 | ths | |
4071 | d5d10bc3 | ths | static int arp_fd = 0; |
4072 | d5d10bc3 | ths | int ip_muxid, arp_muxid;
|
4073 | d5d10bc3 | ths | struct strioctl strioc_if, strioc_ppa;
|
4074 | d5d10bc3 | ths | int link_type = I_PLINK;;
|
4075 | d5d10bc3 | ths | struct lifreq ifr;
|
4076 | d5d10bc3 | ths | char actual_name[32] = ""; |
4077 | d5d10bc3 | ths | |
4078 | d5d10bc3 | ths | memset(&ifr, 0x0, sizeof(ifr)); |
4079 | d5d10bc3 | ths | |
4080 | d5d10bc3 | ths | if( *dev ){
|
4081 | 5fafdf24 | ths | ptr = dev; |
4082 | 5fafdf24 | ths | while( *ptr && !isdigit((int)*ptr) ) ptr++; |
4083 | d5d10bc3 | ths | ppa = atoi(ptr); |
4084 | d5d10bc3 | ths | } |
4085 | d5d10bc3 | ths | |
4086 | d5d10bc3 | ths | /* Check if IP device was opened */
|
4087 | d5d10bc3 | ths | if( ip_fd )
|
4088 | d5d10bc3 | ths | close(ip_fd); |
4089 | d5d10bc3 | ths | |
4090 | aeb30be6 | balrog | TFR(ip_fd = open("/dev/udp", O_RDWR, 0)); |
4091 | aeb30be6 | balrog | if (ip_fd < 0) { |
4092 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
|
4093 | d5d10bc3 | ths | return -1; |
4094 | d5d10bc3 | ths | } |
4095 | d5d10bc3 | ths | |
4096 | aeb30be6 | balrog | TFR(tap_fd = open("/dev/tap", O_RDWR, 0)); |
4097 | aeb30be6 | balrog | if (tap_fd < 0) { |
4098 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't open /dev/tap");
|
4099 | d5d10bc3 | ths | return -1; |
4100 | d5d10bc3 | ths | } |
4101 | d5d10bc3 | ths | |
4102 | d5d10bc3 | ths | /* Assign a new PPA and get its unit number. */
|
4103 | d5d10bc3 | ths | strioc_ppa.ic_cmd = TUNNEWPPA; |
4104 | d5d10bc3 | ths | strioc_ppa.ic_timout = 0;
|
4105 | d5d10bc3 | ths | strioc_ppa.ic_len = sizeof(ppa);
|
4106 | d5d10bc3 | ths | strioc_ppa.ic_dp = (char *)&ppa;
|
4107 | d5d10bc3 | ths | if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) |
4108 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't assign new interface");
|
4109 | d5d10bc3 | ths | |
4110 | aeb30be6 | balrog | TFR(if_fd = open("/dev/tap", O_RDWR, 0)); |
4111 | aeb30be6 | balrog | if (if_fd < 0) { |
4112 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't open /dev/tap (2)");
|
4113 | d5d10bc3 | ths | return -1; |
4114 | d5d10bc3 | ths | } |
4115 | d5d10bc3 | ths | if(ioctl(if_fd, I_PUSH, "ip") < 0){ |
4116 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't push IP module");
|
4117 | d5d10bc3 | ths | return -1; |
4118 | d5d10bc3 | ths | } |
4119 | d5d10bc3 | ths | |
4120 | d5d10bc3 | ths | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) |
4121 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't get flags\n");
|
4122 | d5d10bc3 | ths | |
4123 | d5d10bc3 | ths | snprintf (actual_name, 32, "tap%d", ppa); |
4124 | d5d10bc3 | ths | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
|
4125 | d5d10bc3 | ths | |
4126 | d5d10bc3 | ths | ifr.lifr_ppa = ppa; |
4127 | d5d10bc3 | ths | /* Assign ppa according to the unit number returned by tun device */
|
4128 | d5d10bc3 | ths | |
4129 | d5d10bc3 | ths | if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) |
4130 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't set PPA %d", ppa);
|
4131 | d5d10bc3 | ths | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) |
4132 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't get flags\n");
|
4133 | d5d10bc3 | ths | /* Push arp module to if_fd */
|
4134 | d5d10bc3 | ths | if (ioctl (if_fd, I_PUSH, "arp") < 0) |
4135 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't push ARP module (2)");
|
4136 | d5d10bc3 | ths | |
4137 | d5d10bc3 | ths | /* Push arp module to ip_fd */
|
4138 | d5d10bc3 | ths | if (ioctl (ip_fd, I_POP, NULL) < 0) |
4139 | d5d10bc3 | ths | syslog (LOG_ERR, "I_POP failed\n");
|
4140 | d5d10bc3 | ths | if (ioctl (ip_fd, I_PUSH, "arp") < 0) |
4141 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't push ARP module (3)\n");
|
4142 | d5d10bc3 | ths | /* Open arp_fd */
|
4143 | aeb30be6 | balrog | TFR(arp_fd = open ("/dev/tap", O_RDWR, 0)); |
4144 | aeb30be6 | balrog | if (arp_fd < 0) |
4145 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't open %s\n", "/dev/tap"); |
4146 | d5d10bc3 | ths | |
4147 | d5d10bc3 | ths | /* Set ifname to arp */
|
4148 | d5d10bc3 | ths | strioc_if.ic_cmd = SIOCSLIFNAME; |
4149 | d5d10bc3 | ths | strioc_if.ic_timout = 0;
|
4150 | d5d10bc3 | ths | strioc_if.ic_len = sizeof(ifr);
|
4151 | d5d10bc3 | ths | strioc_if.ic_dp = (char *)𝔦
|
4152 | d5d10bc3 | ths | if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ |
4153 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't set ifname to arp\n");
|
4154 | d5d10bc3 | ths | } |
4155 | d5d10bc3 | ths | |
4156 | d5d10bc3 | ths | if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ |
4157 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't link TAP device to IP");
|
4158 | d5d10bc3 | ths | return -1; |
4159 | d5d10bc3 | ths | } |
4160 | d5d10bc3 | ths | |
4161 | d5d10bc3 | ths | if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) |
4162 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't link TAP device to ARP");
|
4163 | d5d10bc3 | ths | |
4164 | d5d10bc3 | ths | close (if_fd); |
4165 | d5d10bc3 | ths | |
4166 | d5d10bc3 | ths | memset(&ifr, 0x0, sizeof(ifr)); |
4167 | d5d10bc3 | ths | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
|
4168 | d5d10bc3 | ths | ifr.lifr_ip_muxid = ip_muxid; |
4169 | d5d10bc3 | ths | ifr.lifr_arp_muxid = arp_muxid; |
4170 | d5d10bc3 | ths | |
4171 | d5d10bc3 | ths | if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) |
4172 | d5d10bc3 | ths | { |
4173 | d5d10bc3 | ths | ioctl (ip_fd, I_PUNLINK , arp_muxid); |
4174 | d5d10bc3 | ths | ioctl (ip_fd, I_PUNLINK, ip_muxid); |
4175 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't set multiplexor id");
|
4176 | d5d10bc3 | ths | } |
4177 | d5d10bc3 | ths | |
4178 | d5d10bc3 | ths | sprintf(dev, "tap%d", ppa);
|
4179 | d5d10bc3 | ths | return tap_fd;
|
4180 | d5d10bc3 | ths | } |
4181 | d5d10bc3 | ths | |
4182 | ec530c81 | bellard | static int tap_open(char *ifname, int ifname_size) |
4183 | ec530c81 | bellard | { |
4184 | d5d10bc3 | ths | char dev[10]=""; |
4185 | d5d10bc3 | ths | int fd;
|
4186 | d5d10bc3 | ths | if( (fd = tap_alloc(dev)) < 0 ){ |
4187 | d5d10bc3 | ths | fprintf(stderr, "Cannot allocate TAP device\n");
|
4188 | d5d10bc3 | ths | return -1; |
4189 | d5d10bc3 | ths | } |
4190 | d5d10bc3 | ths | pstrcpy(ifname, ifname_size, dev); |
4191 | d5d10bc3 | ths | fcntl(fd, F_SETFL, O_NONBLOCK); |
4192 | d5d10bc3 | ths | return fd;
|
4193 | ec530c81 | bellard | } |
4194 | 7d3505c5 | bellard | #else
|
4195 | 7c9d8e07 | bellard | static int tap_open(char *ifname, int ifname_size) |
4196 | 330d0414 | bellard | { |
4197 | 80cabfad | bellard | struct ifreq ifr;
|
4198 | c4b1fcc0 | bellard | int fd, ret;
|
4199 | 3b46e624 | ths | |
4200 | aeb30be6 | balrog | TFR(fd = open("/dev/net/tun", O_RDWR));
|
4201 | 80cabfad | bellard | if (fd < 0) { |
4202 | 80cabfad | bellard | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
|
4203 | 80cabfad | bellard | return -1; |
4204 | 330d0414 | bellard | } |
4205 | 80cabfad | bellard | memset(&ifr, 0, sizeof(ifr)); |
4206 | 80cabfad | bellard | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
4207 | 7c9d8e07 | bellard | if (ifname[0] != '\0') |
4208 | 7c9d8e07 | bellard | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
4209 | 7c9d8e07 | bellard | else
|
4210 | 7c9d8e07 | bellard | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
|
4211 | 80cabfad | bellard | ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
|
4212 | 80cabfad | bellard | if (ret != 0) { |
4213 | 80cabfad | bellard | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
|
4214 | 80cabfad | bellard | close(fd); |
4215 | 80cabfad | bellard | return -1; |
4216 | 80cabfad | bellard | } |
4217 | c4b1fcc0 | bellard | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
4218 | 80cabfad | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
4219 | c4b1fcc0 | bellard | return fd;
|
4220 | c4b1fcc0 | bellard | } |
4221 | 7d3505c5 | bellard | #endif
|
4222 | 330d0414 | bellard | |
4223 | b46a8906 | ths | static int launch_script(const char *setup_script, const char *ifname, int fd) |
4224 | 7c9d8e07 | bellard | { |
4225 | b46a8906 | ths | int pid, status;
|
4226 | 7c9d8e07 | bellard | char *args[3]; |
4227 | 7c9d8e07 | bellard | char **parg;
|
4228 | 7c9d8e07 | bellard | |
4229 | b46a8906 | ths | /* try to launch network script */
|
4230 | 7c9d8e07 | bellard | pid = fork(); |
4231 | 7c9d8e07 | bellard | if (pid >= 0) { |
4232 | 7c9d8e07 | bellard | if (pid == 0) { |
4233 | 50d3eeae | ths | int open_max = sysconf (_SC_OPEN_MAX), i;
|
4234 | 50d3eeae | ths | for (i = 0; i < open_max; i++) |
4235 | 50d3eeae | ths | if (i != STDIN_FILENO &&
|
4236 | 50d3eeae | ths | i != STDOUT_FILENO && |
4237 | 50d3eeae | ths | i != STDERR_FILENO && |
4238 | 50d3eeae | ths | i != fd) |
4239 | 50d3eeae | ths | close(i); |
4240 | 50d3eeae | ths | |
4241 | 7c9d8e07 | bellard | parg = args; |
4242 | 7c9d8e07 | bellard | *parg++ = (char *)setup_script;
|
4243 | b46a8906 | ths | *parg++ = (char *)ifname;
|
4244 | 7c9d8e07 | bellard | *parg++ = NULL;
|
4245 | 7c9d8e07 | bellard | execv(setup_script, args); |
4246 | 4a38940d | bellard | _exit(1);
|
4247 | 7c9d8e07 | bellard | } |
4248 | 7c9d8e07 | bellard | while (waitpid(pid, &status, 0) != pid); |
4249 | 7c9d8e07 | bellard | if (!WIFEXITED(status) ||
|
4250 | 7c9d8e07 | bellard | WEXITSTATUS(status) != 0) {
|
4251 | 7c9d8e07 | bellard | fprintf(stderr, "%s: could not launch network script\n",
|
4252 | 7c9d8e07 | bellard | setup_script); |
4253 | 7c9d8e07 | bellard | return -1; |
4254 | 7c9d8e07 | bellard | } |
4255 | 7c9d8e07 | bellard | } |
4256 | b46a8906 | ths | return 0; |
4257 | b46a8906 | ths | } |
4258 | b46a8906 | ths | |
4259 | b46a8906 | ths | static int net_tap_init(VLANState *vlan, const char *ifname1, |
4260 | b46a8906 | ths | const char *setup_script, const char *down_script) |
4261 | b46a8906 | ths | { |
4262 | b46a8906 | ths | TAPState *s; |
4263 | b46a8906 | ths | int fd;
|
4264 | b46a8906 | ths | char ifname[128]; |
4265 | b46a8906 | ths | |
4266 | b46a8906 | ths | if (ifname1 != NULL) |
4267 | b46a8906 | ths | pstrcpy(ifname, sizeof(ifname), ifname1);
|
4268 | b46a8906 | ths | else
|
4269 | b46a8906 | ths | ifname[0] = '\0'; |
4270 | b46a8906 | ths | TFR(fd = tap_open(ifname, sizeof(ifname)));
|
4271 | b46a8906 | ths | if (fd < 0) |
4272 | b46a8906 | ths | return -1; |
4273 | b46a8906 | ths | |
4274 | b46a8906 | ths | if (!setup_script || !strcmp(setup_script, "no")) |
4275 | b46a8906 | ths | setup_script = "";
|
4276 | b46a8906 | ths | if (setup_script[0] != '\0') { |
4277 | b46a8906 | ths | if (launch_script(setup_script, ifname, fd))
|
4278 | b46a8906 | ths | return -1; |
4279 | 7c9d8e07 | bellard | } |
4280 | 7c9d8e07 | bellard | s = net_tap_fd_init(vlan, fd); |
4281 | 7c9d8e07 | bellard | if (!s)
|
4282 | 7c9d8e07 | bellard | return -1; |
4283 | 5fafdf24 | ths | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4284 | 7c9d8e07 | bellard | "tap: ifname=%s setup_script=%s", ifname, setup_script);
|
4285 | b46a8906 | ths | if (down_script && strcmp(down_script, "no")) |
4286 | b46a8906 | ths | snprintf(s->down_script, sizeof(s->down_script), "%s", down_script); |
4287 | 7c9d8e07 | bellard | return 0; |
4288 | 7c9d8e07 | bellard | } |
4289 | 7c9d8e07 | bellard | |
4290 | fd1dff4b | bellard | #endif /* !_WIN32 */ |
4291 | fd1dff4b | bellard | |
4292 | 7c9d8e07 | bellard | /* network connection */
|
4293 | 7c9d8e07 | bellard | typedef struct NetSocketState { |
4294 | 7c9d8e07 | bellard | VLANClientState *vc; |
4295 | 7c9d8e07 | bellard | int fd;
|
4296 | 7c9d8e07 | bellard | int state; /* 0 = getting length, 1 = getting data */ |
4297 | 7c9d8e07 | bellard | int index;
|
4298 | 7c9d8e07 | bellard | int packet_len;
|
4299 | 7c9d8e07 | bellard | uint8_t buf[4096];
|
4300 | 3d830459 | bellard | struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
4301 | 7c9d8e07 | bellard | } NetSocketState; |
4302 | 7c9d8e07 | bellard | |
4303 | 7c9d8e07 | bellard | typedef struct NetSocketListenState { |
4304 | 7c9d8e07 | bellard | VLANState *vlan; |
4305 | 7c9d8e07 | bellard | int fd;
|
4306 | 7c9d8e07 | bellard | } NetSocketListenState; |
4307 | 7c9d8e07 | bellard | |
4308 | 7c9d8e07 | bellard | /* XXX: we consider we can send the whole packet without blocking */
|
4309 | 7c9d8e07 | bellard | static void net_socket_receive(void *opaque, const uint8_t *buf, int size) |
4310 | c20709aa | bellard | { |
4311 | 7c9d8e07 | bellard | NetSocketState *s = opaque; |
4312 | 7c9d8e07 | bellard | uint32_t len; |
4313 | 7c9d8e07 | bellard | len = htonl(size); |
4314 | 7c9d8e07 | bellard | |
4315 | fd1dff4b | bellard | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
4316 | fd1dff4b | bellard | send_all(s->fd, buf, size); |
4317 | c20709aa | bellard | } |
4318 | c20709aa | bellard | |
4319 | 3d830459 | bellard | static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size) |
4320 | 3d830459 | bellard | { |
4321 | 3d830459 | bellard | NetSocketState *s = opaque; |
4322 | 5fafdf24 | ths | sendto(s->fd, buf, size, 0,
|
4323 | 3d830459 | bellard | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
4324 | 3d830459 | bellard | } |
4325 | 3d830459 | bellard | |
4326 | 7c9d8e07 | bellard | static void net_socket_send(void *opaque) |
4327 | c4b1fcc0 | bellard | { |
4328 | 7c9d8e07 | bellard | NetSocketState *s = opaque; |
4329 | fd1dff4b | bellard | int l, size, err;
|
4330 | 7c9d8e07 | bellard | uint8_t buf1[4096];
|
4331 | 7c9d8e07 | bellard | const uint8_t *buf;
|
4332 | 7c9d8e07 | bellard | |
4333 | fd1dff4b | bellard | size = recv(s->fd, buf1, sizeof(buf1), 0); |
4334 | fd1dff4b | bellard | if (size < 0) { |
4335 | fd1dff4b | bellard | err = socket_error(); |
4336 | 5fafdf24 | ths | if (err != EWOULDBLOCK)
|
4337 | fd1dff4b | bellard | goto eoc;
|
4338 | fd1dff4b | bellard | } else if (size == 0) { |
4339 | 7c9d8e07 | bellard | /* end of connection */
|
4340 | fd1dff4b | bellard | eoc:
|
4341 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
4342 | fd1dff4b | bellard | closesocket(s->fd); |
4343 | 7c9d8e07 | bellard | return;
|
4344 | 7c9d8e07 | bellard | } |
4345 | 7c9d8e07 | bellard | buf = buf1; |
4346 | 7c9d8e07 | bellard | while (size > 0) { |
4347 | 7c9d8e07 | bellard | /* reassemble a packet from the network */
|
4348 | 7c9d8e07 | bellard | switch(s->state) {
|
4349 | 7c9d8e07 | bellard | case 0: |
4350 | 7c9d8e07 | bellard | l = 4 - s->index;
|
4351 | 7c9d8e07 | bellard | if (l > size)
|
4352 | 7c9d8e07 | bellard | l = size; |
4353 | 7c9d8e07 | bellard | memcpy(s->buf + s->index, buf, l); |
4354 | 7c9d8e07 | bellard | buf += l; |
4355 | 7c9d8e07 | bellard | size -= l; |
4356 | 7c9d8e07 | bellard | s->index += l; |
4357 | 7c9d8e07 | bellard | if (s->index == 4) { |
4358 | 7c9d8e07 | bellard | /* got length */
|
4359 | 7c9d8e07 | bellard | s->packet_len = ntohl(*(uint32_t *)s->buf); |
4360 | 7c9d8e07 | bellard | s->index = 0;
|
4361 | 7c9d8e07 | bellard | s->state = 1;
|
4362 | 7c9d8e07 | bellard | } |
4363 | 7c9d8e07 | bellard | break;
|
4364 | 7c9d8e07 | bellard | case 1: |
4365 | 7c9d8e07 | bellard | l = s->packet_len - s->index; |
4366 | 7c9d8e07 | bellard | if (l > size)
|
4367 | 7c9d8e07 | bellard | l = size; |
4368 | 7c9d8e07 | bellard | memcpy(s->buf + s->index, buf, l); |
4369 | 7c9d8e07 | bellard | s->index += l; |
4370 | 7c9d8e07 | bellard | buf += l; |
4371 | 7c9d8e07 | bellard | size -= l; |
4372 | 7c9d8e07 | bellard | if (s->index >= s->packet_len) {
|
4373 | 7c9d8e07 | bellard | qemu_send_packet(s->vc, s->buf, s->packet_len); |
4374 | 7c9d8e07 | bellard | s->index = 0;
|
4375 | 7c9d8e07 | bellard | s->state = 0;
|
4376 | 7c9d8e07 | bellard | } |
4377 | 7c9d8e07 | bellard | break;
|
4378 | 7c9d8e07 | bellard | } |
4379 | 7c9d8e07 | bellard | } |
4380 | c20709aa | bellard | } |
4381 | c20709aa | bellard | |
4382 | 3d830459 | bellard | static void net_socket_send_dgram(void *opaque) |
4383 | 3d830459 | bellard | { |
4384 | 3d830459 | bellard | NetSocketState *s = opaque; |
4385 | 3d830459 | bellard | int size;
|
4386 | 3d830459 | bellard | |
4387 | 3d830459 | bellard | size = recv(s->fd, s->buf, sizeof(s->buf), 0); |
4388 | 5fafdf24 | ths | if (size < 0) |
4389 | 3d830459 | bellard | return;
|
4390 | 3d830459 | bellard | if (size == 0) { |
4391 | 3d830459 | bellard | /* end of connection */
|
4392 | 3d830459 | bellard | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
4393 | 3d830459 | bellard | return;
|
4394 | 3d830459 | bellard | } |
4395 | 3d830459 | bellard | qemu_send_packet(s->vc, s->buf, size); |
4396 | 3d830459 | bellard | } |
4397 | 3d830459 | bellard | |
4398 | 3d830459 | bellard | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
4399 | 3d830459 | bellard | { |
4400 | 3d830459 | bellard | struct ip_mreq imr;
|
4401 | 3d830459 | bellard | int fd;
|
4402 | 3d830459 | bellard | int val, ret;
|
4403 | 3d830459 | bellard | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
|
4404 | 3d830459 | bellard | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
|
4405 | 5fafdf24 | ths | inet_ntoa(mcastaddr->sin_addr), |
4406 | fd1dff4b | bellard | (int)ntohl(mcastaddr->sin_addr.s_addr));
|
4407 | 3d830459 | bellard | return -1; |
4408 | 3d830459 | bellard | |
4409 | 3d830459 | bellard | } |
4410 | 3d830459 | bellard | fd = socket(PF_INET, SOCK_DGRAM, 0);
|
4411 | 3d830459 | bellard | if (fd < 0) { |
4412 | 3d830459 | bellard | perror("socket(PF_INET, SOCK_DGRAM)");
|
4413 | 3d830459 | bellard | return -1; |
4414 | 3d830459 | bellard | } |
4415 | 3d830459 | bellard | |
4416 | fd1dff4b | bellard | val = 1;
|
4417 | 5fafdf24 | ths | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
4418 | fd1dff4b | bellard | (const char *)&val, sizeof(val)); |
4419 | fd1dff4b | bellard | if (ret < 0) { |
4420 | fd1dff4b | bellard | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
|
4421 | fd1dff4b | bellard | goto fail;
|
4422 | fd1dff4b | bellard | } |
4423 | fd1dff4b | bellard | |
4424 | fd1dff4b | bellard | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
4425 | fd1dff4b | bellard | if (ret < 0) { |
4426 | fd1dff4b | bellard | perror("bind");
|
4427 | fd1dff4b | bellard | goto fail;
|
4428 | fd1dff4b | bellard | } |
4429 | 3b46e624 | ths | |
4430 | 3d830459 | bellard | /* Add host to multicast group */
|
4431 | 3d830459 | bellard | imr.imr_multiaddr = mcastaddr->sin_addr; |
4432 | 3d830459 | bellard | imr.imr_interface.s_addr = htonl(INADDR_ANY); |
4433 | 3d830459 | bellard | |
4434 | 5fafdf24 | ths | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
4435 | fd1dff4b | bellard | (const char *)&imr, sizeof(struct ip_mreq)); |
4436 | 3d830459 | bellard | if (ret < 0) { |
4437 | 3d830459 | bellard | perror("setsockopt(IP_ADD_MEMBERSHIP)");
|
4438 | 3d830459 | bellard | goto fail;
|
4439 | 3d830459 | bellard | } |
4440 | 3d830459 | bellard | |
4441 | 3d830459 | bellard | /* Force mcast msgs to loopback (eg. several QEMUs in same host */
|
4442 | 3d830459 | bellard | val = 1;
|
4443 | 5fafdf24 | ths | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
4444 | fd1dff4b | bellard | (const char *)&val, sizeof(val)); |
4445 | 3d830459 | bellard | if (ret < 0) { |
4446 | 3d830459 | bellard | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
|
4447 | 3d830459 | bellard | goto fail;
|
4448 | 3d830459 | bellard | } |
4449 | 3d830459 | bellard | |
4450 | fd1dff4b | bellard | socket_set_nonblock(fd); |
4451 | 3d830459 | bellard | return fd;
|
4452 | 3d830459 | bellard | fail:
|
4453 | 5fafdf24 | ths | if (fd >= 0) |
4454 | 0bab00f3 | bellard | closesocket(fd); |
4455 | 3d830459 | bellard | return -1; |
4456 | 3d830459 | bellard | } |
4457 | 3d830459 | bellard | |
4458 | 5fafdf24 | ths | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, |
4459 | 3d830459 | bellard | int is_connected)
|
4460 | 3d830459 | bellard | { |
4461 | 3d830459 | bellard | struct sockaddr_in saddr;
|
4462 | 3d830459 | bellard | int newfd;
|
4463 | 3d830459 | bellard | socklen_t saddr_len; |
4464 | 3d830459 | bellard | NetSocketState *s; |
4465 | 3d830459 | bellard | |
4466 | 3d830459 | bellard | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
|
4467 | 5fafdf24 | ths | * Because this may be "shared" socket from a "master" process, datagrams would be recv()
|
4468 | 3d830459 | bellard | * by ONLY ONE process: we must "clone" this dgram socket --jjo
|
4469 | 3d830459 | bellard | */
|
4470 | 3d830459 | bellard | |
4471 | 3d830459 | bellard | if (is_connected) {
|
4472 | 3d830459 | bellard | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
4473 | 3d830459 | bellard | /* must be bound */
|
4474 | 3d830459 | bellard | if (saddr.sin_addr.s_addr==0) { |
4475 | 3d830459 | bellard | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
|
4476 | 3d830459 | bellard | fd); |
4477 | 3d830459 | bellard | return NULL; |
4478 | 3d830459 | bellard | } |
4479 | 3d830459 | bellard | /* clone dgram socket */
|
4480 | 3d830459 | bellard | newfd = net_socket_mcast_create(&saddr); |
4481 | 3d830459 | bellard | if (newfd < 0) { |
4482 | 3d830459 | bellard | /* error already reported by net_socket_mcast_create() */
|
4483 | 3d830459 | bellard | close(fd); |
4484 | 3d830459 | bellard | return NULL; |
4485 | 3d830459 | bellard | } |
4486 | 3d830459 | bellard | /* clone newfd to fd, close newfd */
|
4487 | 3d830459 | bellard | dup2(newfd, fd); |
4488 | 3d830459 | bellard | close(newfd); |
4489 | 5fafdf24 | ths | |
4490 | 3d830459 | bellard | } else {
|
4491 | 3d830459 | bellard | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
|
4492 | 3d830459 | bellard | fd, strerror(errno)); |
4493 | 3d830459 | bellard | return NULL; |
4494 | 3d830459 | bellard | } |
4495 | 3d830459 | bellard | } |
4496 | 3d830459 | bellard | |
4497 | 3d830459 | bellard | s = qemu_mallocz(sizeof(NetSocketState));
|
4498 | 3d830459 | bellard | if (!s)
|
4499 | 3d830459 | bellard | return NULL; |
4500 | 3d830459 | bellard | s->fd = fd; |
4501 | 3d830459 | bellard | |
4502 | d861b05e | pbrook | s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
|
4503 | 3d830459 | bellard | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
|
4504 | 3d830459 | bellard | |
4505 | 3d830459 | bellard | /* mcast: save bound address as dst */
|
4506 | 3d830459 | bellard | if (is_connected) s->dgram_dst=saddr;
|
4507 | 3d830459 | bellard | |
4508 | 3d830459 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4509 | 5fafdf24 | ths | "socket: fd=%d (%s mcast=%s:%d)",
|
4510 | 3d830459 | bellard | fd, is_connected? "cloned" : "", |
4511 | 3d830459 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4512 | 3d830459 | bellard | return s;
|
4513 | 3d830459 | bellard | } |
4514 | 3d830459 | bellard | |
4515 | 7c9d8e07 | bellard | static void net_socket_connect(void *opaque) |
4516 | c20709aa | bellard | { |
4517 | 7c9d8e07 | bellard | NetSocketState *s = opaque; |
4518 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
|
4519 | 7c9d8e07 | bellard | } |
4520 | c4b1fcc0 | bellard | |
4521 | 5fafdf24 | ths | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, |
4522 | 7c9d8e07 | bellard | int is_connected)
|
4523 | 7c9d8e07 | bellard | { |
4524 | 7c9d8e07 | bellard | NetSocketState *s; |
4525 | 7c9d8e07 | bellard | s = qemu_mallocz(sizeof(NetSocketState));
|
4526 | 7c9d8e07 | bellard | if (!s)
|
4527 | 7c9d8e07 | bellard | return NULL; |
4528 | 7c9d8e07 | bellard | s->fd = fd; |
4529 | 5fafdf24 | ths | s->vc = qemu_new_vlan_client(vlan, |
4530 | d861b05e | pbrook | net_socket_receive, NULL, s);
|
4531 | 7c9d8e07 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4532 | 7c9d8e07 | bellard | "socket: fd=%d", fd);
|
4533 | 7c9d8e07 | bellard | if (is_connected) {
|
4534 | 7c9d8e07 | bellard | net_socket_connect(s); |
4535 | 7c9d8e07 | bellard | } else {
|
4536 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
|
4537 | 7c9d8e07 | bellard | } |
4538 | 7c9d8e07 | bellard | return s;
|
4539 | 7c9d8e07 | bellard | } |
4540 | c4b1fcc0 | bellard | |
4541 | 5fafdf24 | ths | static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, |
4542 | 3d830459 | bellard | int is_connected)
|
4543 | 3d830459 | bellard | { |
4544 | 3d830459 | bellard | int so_type=-1, optlen=sizeof(so_type); |
4545 | 3d830459 | bellard | |
4546 | 69b34976 | ths | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, |
4547 | 69b34976 | ths | (socklen_t *)&optlen)< 0) {
|
4548 | 931f03ee | ths | fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
|
4549 | 3d830459 | bellard | return NULL; |
4550 | 3d830459 | bellard | } |
4551 | 3d830459 | bellard | switch(so_type) {
|
4552 | 3d830459 | bellard | case SOCK_DGRAM:
|
4553 | 3d830459 | bellard | return net_socket_fd_init_dgram(vlan, fd, is_connected);
|
4554 | 3d830459 | bellard | case SOCK_STREAM:
|
4555 | 3d830459 | bellard | return net_socket_fd_init_stream(vlan, fd, is_connected);
|
4556 | 3d830459 | bellard | default:
|
4557 | 3d830459 | bellard | /* who knows ... this could be a eg. a pty, do warn and continue as stream */
|
4558 | 3d830459 | bellard | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
|
4559 | 3d830459 | bellard | return net_socket_fd_init_stream(vlan, fd, is_connected);
|
4560 | 3d830459 | bellard | } |
4561 | 3d830459 | bellard | return NULL; |
4562 | 3d830459 | bellard | } |
4563 | 3d830459 | bellard | |
4564 | 7c9d8e07 | bellard | static void net_socket_accept(void *opaque) |
4565 | 7c9d8e07 | bellard | { |
4566 | 3b46e624 | ths | NetSocketListenState *s = opaque; |
4567 | 7c9d8e07 | bellard | NetSocketState *s1; |
4568 | 7c9d8e07 | bellard | struct sockaddr_in saddr;
|
4569 | 7c9d8e07 | bellard | socklen_t len; |
4570 | 7c9d8e07 | bellard | int fd;
|
4571 | 7c9d8e07 | bellard | |
4572 | 7c9d8e07 | bellard | for(;;) {
|
4573 | 7c9d8e07 | bellard | len = sizeof(saddr);
|
4574 | 7c9d8e07 | bellard | fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
|
4575 | 7c9d8e07 | bellard | if (fd < 0 && errno != EINTR) { |
4576 | 7c9d8e07 | bellard | return;
|
4577 | 7c9d8e07 | bellard | } else if (fd >= 0) { |
4578 | 7c9d8e07 | bellard | break;
|
4579 | 80cabfad | bellard | } |
4580 | 330d0414 | bellard | } |
4581 | 5fafdf24 | ths | s1 = net_socket_fd_init(s->vlan, fd, 1);
|
4582 | 7c9d8e07 | bellard | if (!s1) {
|
4583 | 0bab00f3 | bellard | closesocket(fd); |
4584 | 7c9d8e07 | bellard | } else {
|
4585 | 7c9d8e07 | bellard | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
|
4586 | 5fafdf24 | ths | "socket: connection from %s:%d",
|
4587 | 7c9d8e07 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4588 | 7c9d8e07 | bellard | } |
4589 | 7c9d8e07 | bellard | } |
4590 | 7c9d8e07 | bellard | |
4591 | 7c9d8e07 | bellard | static int net_socket_listen_init(VLANState *vlan, const char *host_str) |
4592 | 7c9d8e07 | bellard | { |
4593 | 7c9d8e07 | bellard | NetSocketListenState *s; |
4594 | 7c9d8e07 | bellard | int fd, val, ret;
|
4595 | 7c9d8e07 | bellard | struct sockaddr_in saddr;
|
4596 | 7c9d8e07 | bellard | |
4597 | 7c9d8e07 | bellard | if (parse_host_port(&saddr, host_str) < 0) |
4598 | 7c9d8e07 | bellard | return -1; |
4599 | 3b46e624 | ths | |
4600 | 7c9d8e07 | bellard | s = qemu_mallocz(sizeof(NetSocketListenState));
|
4601 | 7c9d8e07 | bellard | if (!s)
|
4602 | 7c9d8e07 | bellard | return -1; |
4603 | 7c9d8e07 | bellard | |
4604 | 7c9d8e07 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
4605 | 7c9d8e07 | bellard | if (fd < 0) { |
4606 | 7c9d8e07 | bellard | perror("socket");
|
4607 | 7c9d8e07 | bellard | return -1; |
4608 | 7c9d8e07 | bellard | } |
4609 | fd1dff4b | bellard | socket_set_nonblock(fd); |
4610 | 7c9d8e07 | bellard | |
4611 | 7c9d8e07 | bellard | /* allow fast reuse */
|
4612 | 7c9d8e07 | bellard | val = 1;
|
4613 | fd1dff4b | bellard | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
4614 | 3b46e624 | ths | |
4615 | 7c9d8e07 | bellard | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
4616 | 7c9d8e07 | bellard | if (ret < 0) { |
4617 | 7c9d8e07 | bellard | perror("bind");
|
4618 | 7c9d8e07 | bellard | return -1; |
4619 | 7c9d8e07 | bellard | } |
4620 | 7c9d8e07 | bellard | ret = listen(fd, 0);
|
4621 | 7c9d8e07 | bellard | if (ret < 0) { |
4622 | 7c9d8e07 | bellard | perror("listen");
|
4623 | 7c9d8e07 | bellard | return -1; |
4624 | 7c9d8e07 | bellard | } |
4625 | 7c9d8e07 | bellard | s->vlan = vlan; |
4626 | 7c9d8e07 | bellard | s->fd = fd; |
4627 | 7c9d8e07 | bellard | qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
|
4628 | 80cabfad | bellard | return 0; |
4629 | 330d0414 | bellard | } |
4630 | 330d0414 | bellard | |
4631 | 7c9d8e07 | bellard | static int net_socket_connect_init(VLANState *vlan, const char *host_str) |
4632 | 330d0414 | bellard | { |
4633 | 7c9d8e07 | bellard | NetSocketState *s; |
4634 | fd1dff4b | bellard | int fd, connected, ret, err;
|
4635 | 7c9d8e07 | bellard | struct sockaddr_in saddr;
|
4636 | 7c9d8e07 | bellard | |
4637 | 7c9d8e07 | bellard | if (parse_host_port(&saddr, host_str) < 0) |
4638 | 7c9d8e07 | bellard | return -1; |
4639 | 7c9d8e07 | bellard | |
4640 | 7c9d8e07 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
4641 | 7c9d8e07 | bellard | if (fd < 0) { |
4642 | 7c9d8e07 | bellard | perror("socket");
|
4643 | 7c9d8e07 | bellard | return -1; |
4644 | 7c9d8e07 | bellard | } |
4645 | fd1dff4b | bellard | socket_set_nonblock(fd); |
4646 | 7c9d8e07 | bellard | |
4647 | 7c9d8e07 | bellard | connected = 0;
|
4648 | 7c9d8e07 | bellard | for(;;) {
|
4649 | 7c9d8e07 | bellard | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
4650 | 7c9d8e07 | bellard | if (ret < 0) { |
4651 | fd1dff4b | bellard | err = socket_error(); |
4652 | fd1dff4b | bellard | if (err == EINTR || err == EWOULDBLOCK) {
|
4653 | fd1dff4b | bellard | } else if (err == EINPROGRESS) { |
4654 | 7c9d8e07 | bellard | break;
|
4655 | f5b12268 | ths | #ifdef _WIN32
|
4656 | f5b12268 | ths | } else if (err == WSAEALREADY) { |
4657 | f5b12268 | ths | break;
|
4658 | f5b12268 | ths | #endif
|
4659 | 7c9d8e07 | bellard | } else {
|
4660 | 7c9d8e07 | bellard | perror("connect");
|
4661 | fd1dff4b | bellard | closesocket(fd); |
4662 | 7c9d8e07 | bellard | return -1; |
4663 | 7c9d8e07 | bellard | } |
4664 | 7c9d8e07 | bellard | } else {
|
4665 | 7c9d8e07 | bellard | connected = 1;
|
4666 | 7c9d8e07 | bellard | break;
|
4667 | 7c9d8e07 | bellard | } |
4668 | 7c9d8e07 | bellard | } |
4669 | 7c9d8e07 | bellard | s = net_socket_fd_init(vlan, fd, connected); |
4670 | 7c9d8e07 | bellard | if (!s)
|
4671 | 7c9d8e07 | bellard | return -1; |
4672 | 7c9d8e07 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4673 | 5fafdf24 | ths | "socket: connect to %s:%d",
|
4674 | 7c9d8e07 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4675 | c20709aa | bellard | return 0; |
4676 | 80cabfad | bellard | } |
4677 | 330d0414 | bellard | |
4678 | 3d830459 | bellard | static int net_socket_mcast_init(VLANState *vlan, const char *host_str) |
4679 | 3d830459 | bellard | { |
4680 | 3d830459 | bellard | NetSocketState *s; |
4681 | 3d830459 | bellard | int fd;
|
4682 | 3d830459 | bellard | struct sockaddr_in saddr;
|
4683 | 3d830459 | bellard | |
4684 | 3d830459 | bellard | if (parse_host_port(&saddr, host_str) < 0) |
4685 | 3d830459 | bellard | return -1; |
4686 | 3d830459 | bellard | |
4687 | 3d830459 | bellard | |
4688 | 3d830459 | bellard | fd = net_socket_mcast_create(&saddr); |
4689 | 3d830459 | bellard | if (fd < 0) |
4690 | 3d830459 | bellard | return -1; |
4691 | 3d830459 | bellard | |
4692 | 3d830459 | bellard | s = net_socket_fd_init(vlan, fd, 0);
|
4693 | 3d830459 | bellard | if (!s)
|
4694 | 3d830459 | bellard | return -1; |
4695 | 3d830459 | bellard | |
4696 | 3d830459 | bellard | s->dgram_dst = saddr; |
4697 | 3b46e624 | ths | |
4698 | 3d830459 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4699 | 5fafdf24 | ths | "socket: mcast=%s:%d",
|
4700 | 3d830459 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4701 | 3d830459 | bellard | return 0; |
4702 | 3d830459 | bellard | |
4703 | 3d830459 | bellard | } |
4704 | 3d830459 | bellard | |
4705 | 609497ab | balrog | static const char *get_opt_name(char *buf, int buf_size, const char *p) |
4706 | 609497ab | balrog | { |
4707 | 609497ab | balrog | char *q;
|
4708 | 609497ab | balrog | |
4709 | 609497ab | balrog | q = buf; |
4710 | 609497ab | balrog | while (*p != '\0' && *p != '=') { |
4711 | 609497ab | balrog | if (q && (q - buf) < buf_size - 1) |
4712 | 609497ab | balrog | *q++ = *p; |
4713 | 609497ab | balrog | p++; |
4714 | 609497ab | balrog | } |
4715 | 609497ab | balrog | if (q)
|
4716 | 609497ab | balrog | *q = '\0';
|
4717 | 609497ab | balrog | |
4718 | 609497ab | balrog | return p;
|
4719 | 609497ab | balrog | } |
4720 | 609497ab | balrog | |
4721 | 609497ab | balrog | static const char *get_opt_value(char *buf, int buf_size, const char *p) |
4722 | e4bcb14c | ths | { |
4723 | e4bcb14c | ths | char *q;
|
4724 | e4bcb14c | ths | |
4725 | e4bcb14c | ths | q = buf; |
4726 | e4bcb14c | ths | while (*p != '\0') { |
4727 | 609497ab | balrog | if (*p == ',') { |
4728 | 609497ab | balrog | if (*(p + 1) != ',') |
4729 | e4bcb14c | ths | break;
|
4730 | e4bcb14c | ths | p++; |
4731 | 609497ab | balrog | } |
4732 | e4bcb14c | ths | if (q && (q - buf) < buf_size - 1) |
4733 | e4bcb14c | ths | *q++ = *p; |
4734 | e4bcb14c | ths | p++; |
4735 | e4bcb14c | ths | } |
4736 | e4bcb14c | ths | if (q)
|
4737 | e4bcb14c | ths | *q = '\0';
|
4738 | e4bcb14c | ths | |
4739 | e4bcb14c | ths | return p;
|
4740 | e4bcb14c | ths | } |
4741 | e4bcb14c | ths | |
4742 | 7c9d8e07 | bellard | static int get_param_value(char *buf, int buf_size, |
4743 | 7c9d8e07 | bellard | const char *tag, const char *str) |
4744 | 7c9d8e07 | bellard | { |
4745 | 7c9d8e07 | bellard | const char *p; |
4746 | 7c9d8e07 | bellard | char option[128]; |
4747 | 7c9d8e07 | bellard | |
4748 | 7c9d8e07 | bellard | p = str; |
4749 | 7c9d8e07 | bellard | for(;;) {
|
4750 | 609497ab | balrog | p = get_opt_name(option, sizeof(option), p);
|
4751 | 7c9d8e07 | bellard | if (*p != '=') |
4752 | 7c9d8e07 | bellard | break;
|
4753 | 7c9d8e07 | bellard | p++; |
4754 | 7c9d8e07 | bellard | if (!strcmp(tag, option)) {
|
4755 | 609497ab | balrog | (void)get_opt_value(buf, buf_size, p);
|
4756 | e4bcb14c | ths | return strlen(buf);
|
4757 | 7c9d8e07 | bellard | } else {
|
4758 | 609497ab | balrog | p = get_opt_value(NULL, 0, p); |
4759 | 7c9d8e07 | bellard | } |
4760 | 7c9d8e07 | bellard | if (*p != ',') |
4761 | 7c9d8e07 | bellard | break;
|
4762 | 7c9d8e07 | bellard | p++; |
4763 | 7c9d8e07 | bellard | } |
4764 | 7c9d8e07 | bellard | return 0; |
4765 | 7c9d8e07 | bellard | } |
4766 | 7c9d8e07 | bellard | |
4767 | e4bcb14c | ths | static int check_params(char *buf, int buf_size, |
4768 | e4bcb14c | ths | char **params, const char *str) |
4769 | e4bcb14c | ths | { |
4770 | e4bcb14c | ths | const char *p; |
4771 | e4bcb14c | ths | int i;
|
4772 | e4bcb14c | ths | |
4773 | e4bcb14c | ths | p = str; |
4774 | e4bcb14c | ths | for(;;) {
|
4775 | 609497ab | balrog | p = get_opt_name(buf, buf_size, p); |
4776 | e4bcb14c | ths | if (*p != '=') |
4777 | e4bcb14c | ths | return -1; |
4778 | e4bcb14c | ths | p++; |
4779 | e4bcb14c | ths | for(i = 0; params[i] != NULL; i++) |
4780 | e4bcb14c | ths | if (!strcmp(params[i], buf))
|
4781 | e4bcb14c | ths | break;
|
4782 | e4bcb14c | ths | if (params[i] == NULL) |
4783 | e4bcb14c | ths | return -1; |
4784 | 609497ab | balrog | p = get_opt_value(NULL, 0, p); |
4785 | e4bcb14c | ths | if (*p != ',') |
4786 | e4bcb14c | ths | break;
|
4787 | e4bcb14c | ths | p++; |
4788 | e4bcb14c | ths | } |
4789 | e4bcb14c | ths | return 0; |
4790 | e4bcb14c | ths | } |
4791 | e4bcb14c | ths | |
4792 | e4bcb14c | ths | |
4793 | 52f61fde | ths | static int net_client_init(const char *str) |
4794 | 7c9d8e07 | bellard | { |
4795 | 7c9d8e07 | bellard | const char *p; |
4796 | 7c9d8e07 | bellard | char *q;
|
4797 | 7c9d8e07 | bellard | char device[64]; |
4798 | 7c9d8e07 | bellard | char buf[1024]; |
4799 | 7c9d8e07 | bellard | int vlan_id, ret;
|
4800 | 7c9d8e07 | bellard | VLANState *vlan; |
4801 | 7c9d8e07 | bellard | |
4802 | 7c9d8e07 | bellard | p = str; |
4803 | 7c9d8e07 | bellard | q = device; |
4804 | 7c9d8e07 | bellard | while (*p != '\0' && *p != ',') { |
4805 | 7c9d8e07 | bellard | if ((q - device) < sizeof(device) - 1) |
4806 | 7c9d8e07 | bellard | *q++ = *p; |
4807 | 7c9d8e07 | bellard | p++; |
4808 | 7c9d8e07 | bellard | } |
4809 | 7c9d8e07 | bellard | *q = '\0';
|
4810 | 7c9d8e07 | bellard | if (*p == ',') |
4811 | 7c9d8e07 | bellard | p++; |
4812 | 7c9d8e07 | bellard | vlan_id = 0;
|
4813 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "vlan", p)) { |
4814 | 7c9d8e07 | bellard | vlan_id = strtol(buf, NULL, 0); |
4815 | 7c9d8e07 | bellard | } |
4816 | 7c9d8e07 | bellard | vlan = qemu_find_vlan(vlan_id); |
4817 | 7c9d8e07 | bellard | if (!vlan) {
|
4818 | 7c9d8e07 | bellard | fprintf(stderr, "Could not create vlan %d\n", vlan_id);
|
4819 | 7c9d8e07 | bellard | return -1; |
4820 | 7c9d8e07 | bellard | } |
4821 | 7c9d8e07 | bellard | if (!strcmp(device, "nic")) { |
4822 | 7c9d8e07 | bellard | NICInfo *nd; |
4823 | 7c9d8e07 | bellard | uint8_t *macaddr; |
4824 | 7c9d8e07 | bellard | |
4825 | 7c9d8e07 | bellard | if (nb_nics >= MAX_NICS) {
|
4826 | 7c9d8e07 | bellard | fprintf(stderr, "Too Many NICs\n");
|
4827 | 7c9d8e07 | bellard | return -1; |
4828 | 7c9d8e07 | bellard | } |
4829 | 7c9d8e07 | bellard | nd = &nd_table[nb_nics]; |
4830 | 7c9d8e07 | bellard | macaddr = nd->macaddr; |
4831 | 7c9d8e07 | bellard | macaddr[0] = 0x52; |
4832 | 7c9d8e07 | bellard | macaddr[1] = 0x54; |
4833 | 7c9d8e07 | bellard | macaddr[2] = 0x00; |
4834 | 7c9d8e07 | bellard | macaddr[3] = 0x12; |
4835 | 7c9d8e07 | bellard | macaddr[4] = 0x34; |
4836 | 7c9d8e07 | bellard | macaddr[5] = 0x56 + nb_nics; |
4837 | 7c9d8e07 | bellard | |
4838 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "macaddr", p)) { |
4839 | 7c9d8e07 | bellard | if (parse_macaddr(macaddr, buf) < 0) { |
4840 | 7c9d8e07 | bellard | fprintf(stderr, "invalid syntax for ethernet address\n");
|
4841 | 7c9d8e07 | bellard | return -1; |
4842 | 7c9d8e07 | bellard | } |
4843 | 7c9d8e07 | bellard | } |
4844 | a41b2ff2 | pbrook | if (get_param_value(buf, sizeof(buf), "model", p)) { |
4845 | a41b2ff2 | pbrook | nd->model = strdup(buf); |
4846 | a41b2ff2 | pbrook | } |
4847 | 7c9d8e07 | bellard | nd->vlan = vlan; |
4848 | 7c9d8e07 | bellard | nb_nics++; |
4849 | 833c7174 | blueswir1 | vlan->nb_guest_devs++; |
4850 | 7c9d8e07 | bellard | ret = 0;
|
4851 | 7c9d8e07 | bellard | } else
|
4852 | 7c9d8e07 | bellard | if (!strcmp(device, "none")) { |
4853 | 7c9d8e07 | bellard | /* does nothing. It is needed to signal that no network cards
|
4854 | 7c9d8e07 | bellard | are wanted */
|
4855 | 7c9d8e07 | bellard | ret = 0;
|
4856 | 7c9d8e07 | bellard | } else
|
4857 | 7c9d8e07 | bellard | #ifdef CONFIG_SLIRP
|
4858 | 7c9d8e07 | bellard | if (!strcmp(device, "user")) { |
4859 | 115defd1 | pbrook | if (get_param_value(buf, sizeof(buf), "hostname", p)) { |
4860 | 3f423c9c | bellard | pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
|
4861 | 115defd1 | pbrook | } |
4862 | 833c7174 | blueswir1 | vlan->nb_host_devs++; |
4863 | 7c9d8e07 | bellard | ret = net_slirp_init(vlan); |
4864 | 7c9d8e07 | bellard | } else
|
4865 | 7c9d8e07 | bellard | #endif
|
4866 | 7fb843f8 | bellard | #ifdef _WIN32
|
4867 | 7fb843f8 | bellard | if (!strcmp(device, "tap")) { |
4868 | 7fb843f8 | bellard | char ifname[64]; |
4869 | 7fb843f8 | bellard | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
4870 | 7fb843f8 | bellard | fprintf(stderr, "tap: no interface name\n");
|
4871 | 7fb843f8 | bellard | return -1; |
4872 | 7fb843f8 | bellard | } |
4873 | 833c7174 | blueswir1 | vlan->nb_host_devs++; |
4874 | 7fb843f8 | bellard | ret = tap_win32_init(vlan, ifname); |
4875 | 7fb843f8 | bellard | } else
|
4876 | 7fb843f8 | bellard | #else
|
4877 | 7c9d8e07 | bellard | if (!strcmp(device, "tap")) { |
4878 | 7c9d8e07 | bellard | char ifname[64]; |
4879 | b46a8906 | ths | char setup_script[1024], down_script[1024]; |
4880 | 7c9d8e07 | bellard | int fd;
|
4881 | 4f01035f | pbrook | vlan->nb_host_devs++; |
4882 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
4883 | 7c9d8e07 | bellard | fd = strtol(buf, NULL, 0); |
4884 | 64538cdf | pbrook | fcntl(fd, F_SETFL, O_NONBLOCK); |
4885 | 7c9d8e07 | bellard | ret = -1;
|
4886 | 7c9d8e07 | bellard | if (net_tap_fd_init(vlan, fd))
|
4887 | 7c9d8e07 | bellard | ret = 0;
|
4888 | 7c9d8e07 | bellard | } else {
|
4889 | bf8c5342 | bellard | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
4890 | bf8c5342 | bellard | ifname[0] = '\0'; |
4891 | bf8c5342 | bellard | } |
4892 | 7c9d8e07 | bellard | if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) { |
4893 | 7c9d8e07 | bellard | pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
|
4894 | 7c9d8e07 | bellard | } |
4895 | b46a8906 | ths | if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) { |
4896 | b46a8906 | ths | pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
|
4897 | b46a8906 | ths | } |
4898 | b46a8906 | ths | ret = net_tap_init(vlan, ifname, setup_script, down_script); |
4899 | 7c9d8e07 | bellard | } |
4900 | 7c9d8e07 | bellard | } else
|
4901 | fd1dff4b | bellard | #endif
|
4902 | 7c9d8e07 | bellard | if (!strcmp(device, "socket")) { |
4903 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
4904 | 7c9d8e07 | bellard | int fd;
|
4905 | 7c9d8e07 | bellard | fd = strtol(buf, NULL, 0); |
4906 | 7c9d8e07 | bellard | ret = -1;
|
4907 | 7c9d8e07 | bellard | if (net_socket_fd_init(vlan, fd, 1)) |
4908 | 7c9d8e07 | bellard | ret = 0;
|
4909 | 7c9d8e07 | bellard | } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) { |
4910 | 7c9d8e07 | bellard | ret = net_socket_listen_init(vlan, buf); |
4911 | 7c9d8e07 | bellard | } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { |
4912 | 7c9d8e07 | bellard | ret = net_socket_connect_init(vlan, buf); |
4913 | 3d830459 | bellard | } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { |
4914 | 3d830459 | bellard | ret = net_socket_mcast_init(vlan, buf); |
4915 | 7c9d8e07 | bellard | } else {
|
4916 | 7c9d8e07 | bellard | fprintf(stderr, "Unknown socket options: %s\n", p);
|
4917 | 7c9d8e07 | bellard | return -1; |
4918 | 7c9d8e07 | bellard | } |
4919 | 833c7174 | blueswir1 | vlan->nb_host_devs++; |
4920 | 7c9d8e07 | bellard | } else
|
4921 | 7c9d8e07 | bellard | { |
4922 | 7c9d8e07 | bellard | fprintf(stderr, "Unknown network device: %s\n", device);
|
4923 | 7c9d8e07 | bellard | return -1; |
4924 | 7c9d8e07 | bellard | } |
4925 | 7c9d8e07 | bellard | if (ret < 0) { |
4926 | 7c9d8e07 | bellard | fprintf(stderr, "Could not initialize device '%s'\n", device);
|
4927 | 7c9d8e07 | bellard | } |
4928 | 3b46e624 | ths | |
4929 | 7c9d8e07 | bellard | return ret;
|
4930 | 7c9d8e07 | bellard | } |
4931 | 7c9d8e07 | bellard | |
4932 | 7c9d8e07 | bellard | void do_info_network(void) |
4933 | 7c9d8e07 | bellard | { |
4934 | 7c9d8e07 | bellard | VLANState *vlan; |
4935 | 7c9d8e07 | bellard | VLANClientState *vc; |
4936 | 7c9d8e07 | bellard | |
4937 | 7c9d8e07 | bellard | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
4938 | 7c9d8e07 | bellard | term_printf("VLAN %d devices:\n", vlan->id);
|
4939 | 7c9d8e07 | bellard | for(vc = vlan->first_client; vc != NULL; vc = vc->next) |
4940 | 7c9d8e07 | bellard | term_printf(" %s\n", vc->info_str);
|
4941 | 7c9d8e07 | bellard | } |
4942 | 7c9d8e07 | bellard | } |
4943 | 42550fde | ths | |
4944 | 609497ab | balrog | #define HD_ALIAS "index=%d,media=disk" |
4945 | e4bcb14c | ths | #ifdef TARGET_PPC
|
4946 | e4bcb14c | ths | #define CDROM_ALIAS "index=1,media=cdrom" |
4947 | e4bcb14c | ths | #else
|
4948 | e4bcb14c | ths | #define CDROM_ALIAS "index=2,media=cdrom" |
4949 | e4bcb14c | ths | #endif
|
4950 | e4bcb14c | ths | #define FD_ALIAS "index=%d,if=floppy" |
4951 | 609497ab | balrog | #define PFLASH_ALIAS "if=pflash" |
4952 | 609497ab | balrog | #define MTD_ALIAS "if=mtd" |
4953 | 9d413d1d | balrog | #define SD_ALIAS "index=0,if=sd" |
4954 | e4bcb14c | ths | |
4955 | 609497ab | balrog | static int drive_add(const char *file, const char *fmt, ...) |
4956 | e4bcb14c | ths | { |
4957 | e4bcb14c | ths | va_list ap; |
4958 | e4bcb14c | ths | |
4959 | e4bcb14c | ths | if (nb_drives_opt >= MAX_DRIVES) {
|
4960 | e4bcb14c | ths | fprintf(stderr, "qemu: too many drives\n");
|
4961 | e4bcb14c | ths | exit(1);
|
4962 | e4bcb14c | ths | } |
4963 | e4bcb14c | ths | |
4964 | 609497ab | balrog | drives_opt[nb_drives_opt].file = file; |
4965 | e4bcb14c | ths | va_start(ap, fmt); |
4966 | 609497ab | balrog | vsnprintf(drives_opt[nb_drives_opt].opt, |
4967 | 609497ab | balrog | sizeof(drives_opt[0].opt), fmt, ap); |
4968 | e4bcb14c | ths | va_end(ap); |
4969 | e4bcb14c | ths | |
4970 | e4bcb14c | ths | return nb_drives_opt++;
|
4971 | e4bcb14c | ths | } |
4972 | e4bcb14c | ths | |
4973 | f60d39bc | ths | int drive_get_index(BlockInterfaceType type, int bus, int unit) |
4974 | e4bcb14c | ths | { |
4975 | e4bcb14c | ths | int index;
|
4976 | e4bcb14c | ths | |
4977 | e4bcb14c | ths | /* seek interface, bus and unit */
|
4978 | e4bcb14c | ths | |
4979 | e4bcb14c | ths | for (index = 0; index < nb_drives; index++) |
4980 | f60d39bc | ths | if (drives_table[index].type == type &&
|
4981 | e4bcb14c | ths | drives_table[index].bus == bus && |
4982 | e4bcb14c | ths | drives_table[index].unit == unit) |
4983 | e4bcb14c | ths | return index;
|
4984 | e4bcb14c | ths | |
4985 | e4bcb14c | ths | return -1; |
4986 | e4bcb14c | ths | } |
4987 | e4bcb14c | ths | |
4988 | f60d39bc | ths | int drive_get_max_bus(BlockInterfaceType type)
|
4989 | e4bcb14c | ths | { |
4990 | e4bcb14c | ths | int max_bus;
|
4991 | e4bcb14c | ths | int index;
|
4992 | e4bcb14c | ths | |
4993 | e4bcb14c | ths | max_bus = -1;
|
4994 | e4bcb14c | ths | for (index = 0; index < nb_drives; index++) { |
4995 | f60d39bc | ths | if(drives_table[index].type == type &&
|
4996 | e4bcb14c | ths | drives_table[index].bus > max_bus) |
4997 | e4bcb14c | ths | max_bus = drives_table[index].bus; |
4998 | e4bcb14c | ths | } |
4999 | e4bcb14c | ths | return max_bus;
|
5000 | e4bcb14c | ths | } |
5001 | e4bcb14c | ths | |
5002 | a1620fac | aurel32 | static void bdrv_format_print(void *opaque, const char *name) |
5003 | a1620fac | aurel32 | { |
5004 | a1620fac | aurel32 | fprintf(stderr, " %s", name);
|
5005 | a1620fac | aurel32 | } |
5006 | a1620fac | aurel32 | |
5007 | 609497ab | balrog | static int drive_init(struct drive_opt *arg, int snapshot, |
5008 | 609497ab | balrog | QEMUMachine *machine) |
5009 | e4bcb14c | ths | { |
5010 | e4bcb14c | ths | char buf[128]; |
5011 | e4bcb14c | ths | char file[1024]; |
5012 | c8522bdf | balrog | char devname[128]; |
5013 | c8522bdf | balrog | const char *mediastr = ""; |
5014 | f60d39bc | ths | BlockInterfaceType type; |
5015 | e4bcb14c | ths | enum { MEDIA_DISK, MEDIA_CDROM } media;
|
5016 | e4bcb14c | ths | int bus_id, unit_id;
|
5017 | e4bcb14c | ths | int cyls, heads, secs, translation;
|
5018 | e4bcb14c | ths | BlockDriverState *bdrv; |
5019 | 1e72d3b7 | aurel32 | BlockDriver *drv = NULL;
|
5020 | e4bcb14c | ths | int max_devs;
|
5021 | e4bcb14c | ths | int index;
|
5022 | 33f00271 | balrog | int cache;
|
5023 | 33f00271 | balrog | int bdrv_flags;
|
5024 | 609497ab | balrog | char *str = arg->opt;
|
5025 | e4bcb14c | ths | char *params[] = { "bus", "unit", "if", "index", "cyls", "heads", |
5026 | 33f00271 | balrog | "secs", "trans", "media", "snapshot", "file", |
5027 | 1e72d3b7 | aurel32 | "cache", "format", NULL }; |
5028 | e4bcb14c | ths | |
5029 | e4bcb14c | ths | if (check_params(buf, sizeof(buf), params, str) < 0) { |
5030 | ff993638 | balrog | fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
|
5031 | e4bcb14c | ths | buf, str); |
5032 | e4bcb14c | ths | return -1; |
5033 | e4bcb14c | ths | } |
5034 | e4bcb14c | ths | |
5035 | e4bcb14c | ths | file[0] = 0; |
5036 | e4bcb14c | ths | cyls = heads = secs = 0;
|
5037 | e4bcb14c | ths | bus_id = 0;
|
5038 | e4bcb14c | ths | unit_id = -1;
|
5039 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_AUTO; |
5040 | e4bcb14c | ths | index = -1;
|
5041 | 33f00271 | balrog | cache = 1;
|
5042 | e4bcb14c | ths | |
5043 | e4bcb14c | ths | if (!strcmp(machine->name, "realview") || |
5044 | e4bcb14c | ths | !strcmp(machine->name, "SS-5") ||
|
5045 | e4bcb14c | ths | !strcmp(machine->name, "SS-10") ||
|
5046 | e4bcb14c | ths | !strcmp(machine->name, "SS-600MP") ||
|
5047 | e4bcb14c | ths | !strcmp(machine->name, "versatilepb") ||
|
5048 | e4bcb14c | ths | !strcmp(machine->name, "versatileab")) {
|
5049 | f60d39bc | ths | type = IF_SCSI; |
5050 | e4bcb14c | ths | max_devs = MAX_SCSI_DEVS; |
5051 | c8522bdf | balrog | strcpy(devname, "scsi");
|
5052 | e4bcb14c | ths | } else {
|
5053 | f60d39bc | ths | type = IF_IDE; |
5054 | e4bcb14c | ths | max_devs = MAX_IDE_DEVS; |
5055 | c8522bdf | balrog | strcpy(devname, "ide");
|
5056 | e4bcb14c | ths | } |
5057 | e4bcb14c | ths | media = MEDIA_DISK; |
5058 | e4bcb14c | ths | |
5059 | e4bcb14c | ths | /* extract parameters */
|
5060 | e4bcb14c | ths | |
5061 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "bus", str)) { |
5062 | e4bcb14c | ths | bus_id = strtol(buf, NULL, 0); |
5063 | e4bcb14c | ths | if (bus_id < 0) { |
5064 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
|
5065 | e4bcb14c | ths | return -1; |
5066 | e4bcb14c | ths | } |
5067 | e4bcb14c | ths | } |
5068 | e4bcb14c | ths | |
5069 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "unit", str)) { |
5070 | e4bcb14c | ths | unit_id = strtol(buf, NULL, 0); |
5071 | e4bcb14c | ths | if (unit_id < 0) { |
5072 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
|
5073 | e4bcb14c | ths | return -1; |
5074 | e4bcb14c | ths | } |
5075 | e4bcb14c | ths | } |
5076 | e4bcb14c | ths | |
5077 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "if", str)) { |
5078 | c8522bdf | balrog | strncpy(devname, buf, sizeof(devname));
|
5079 | e4bcb14c | ths | if (!strcmp(buf, "ide")) { |
5080 | f60d39bc | ths | type = IF_IDE; |
5081 | e4bcb14c | ths | max_devs = MAX_IDE_DEVS; |
5082 | e4bcb14c | ths | } else if (!strcmp(buf, "scsi")) { |
5083 | f60d39bc | ths | type = IF_SCSI; |
5084 | e4bcb14c | ths | max_devs = MAX_SCSI_DEVS; |
5085 | e4bcb14c | ths | } else if (!strcmp(buf, "floppy")) { |
5086 | f60d39bc | ths | type = IF_FLOPPY; |
5087 | e4bcb14c | ths | max_devs = 0;
|
5088 | e4bcb14c | ths | } else if (!strcmp(buf, "pflash")) { |
5089 | f60d39bc | ths | type = IF_PFLASH; |
5090 | e4bcb14c | ths | max_devs = 0;
|
5091 | e4bcb14c | ths | } else if (!strcmp(buf, "mtd")) { |
5092 | f60d39bc | ths | type = IF_MTD; |
5093 | e4bcb14c | ths | max_devs = 0;
|
5094 | e4bcb14c | ths | } else if (!strcmp(buf, "sd")) { |
5095 | f60d39bc | ths | type = IF_SD; |
5096 | e4bcb14c | ths | max_devs = 0;
|
5097 | e4bcb14c | ths | } else {
|
5098 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
|
5099 | e4bcb14c | ths | return -1; |
5100 | e4bcb14c | ths | } |
5101 | e4bcb14c | ths | } |
5102 | e4bcb14c | ths | |
5103 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "index", str)) { |
5104 | e4bcb14c | ths | index = strtol(buf, NULL, 0); |
5105 | e4bcb14c | ths | if (index < 0) { |
5106 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid index\n", str);
|
5107 | e4bcb14c | ths | return -1; |
5108 | e4bcb14c | ths | } |
5109 | e4bcb14c | ths | } |
5110 | e4bcb14c | ths | |
5111 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "cyls", str)) { |
5112 | e4bcb14c | ths | cyls = strtol(buf, NULL, 0); |
5113 | e4bcb14c | ths | } |
5114 | e4bcb14c | ths | |
5115 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "heads", str)) { |
5116 | e4bcb14c | ths | heads = strtol(buf, NULL, 0); |
5117 | e4bcb14c | ths | } |
5118 | e4bcb14c | ths | |
5119 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "secs", str)) { |
5120 | e4bcb14c | ths | secs = strtol(buf, NULL, 0); |
5121 | e4bcb14c | ths | } |
5122 | e4bcb14c | ths | |
5123 | e4bcb14c | ths | if (cyls || heads || secs) {
|
5124 | e4bcb14c | ths | if (cyls < 1 || cyls > 16383) { |
5125 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
|
5126 | e4bcb14c | ths | return -1; |
5127 | e4bcb14c | ths | } |
5128 | e4bcb14c | ths | if (heads < 1 || heads > 16) { |
5129 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
|
5130 | e4bcb14c | ths | return -1; |
5131 | e4bcb14c | ths | } |
5132 | e4bcb14c | ths | if (secs < 1 || secs > 63) { |
5133 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
|
5134 | e4bcb14c | ths | return -1; |
5135 | e4bcb14c | ths | } |
5136 | e4bcb14c | ths | } |
5137 | e4bcb14c | ths | |
5138 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "trans", str)) { |
5139 | e4bcb14c | ths | if (!cyls) {
|
5140 | e4bcb14c | ths | fprintf(stderr, |
5141 | e4bcb14c | ths | "qemu: '%s' trans must be used with cyls,heads and secs\n",
|
5142 | e4bcb14c | ths | str); |
5143 | e4bcb14c | ths | return -1; |
5144 | e4bcb14c | ths | } |
5145 | e4bcb14c | ths | if (!strcmp(buf, "none")) |
5146 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_NONE; |
5147 | e4bcb14c | ths | else if (!strcmp(buf, "lba")) |
5148 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_LBA; |
5149 | e4bcb14c | ths | else if (!strcmp(buf, "auto")) |
5150 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_AUTO; |
5151 | e4bcb14c | ths | else {
|
5152 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
|
5153 | e4bcb14c | ths | return -1; |
5154 | e4bcb14c | ths | } |
5155 | e4bcb14c | ths | } |
5156 | e4bcb14c | ths | |
5157 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "media", str)) { |
5158 | e4bcb14c | ths | if (!strcmp(buf, "disk")) { |
5159 | e4bcb14c | ths | media = MEDIA_DISK; |
5160 | e4bcb14c | ths | } else if (!strcmp(buf, "cdrom")) { |
5161 | e4bcb14c | ths | if (cyls || secs || heads) {
|
5162 | e4bcb14c | ths | fprintf(stderr, |
5163 | e4bcb14c | ths | "qemu: '%s' invalid physical CHS format\n", str);
|
5164 | e4bcb14c | ths | return -1; |
5165 | e4bcb14c | ths | } |
5166 | e4bcb14c | ths | media = MEDIA_CDROM; |
5167 | e4bcb14c | ths | } else {
|
5168 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid media\n", str);
|
5169 | e4bcb14c | ths | return -1; |
5170 | e4bcb14c | ths | } |
5171 | e4bcb14c | ths | } |
5172 | e4bcb14c | ths | |
5173 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "snapshot", str)) { |
5174 | e4bcb14c | ths | if (!strcmp(buf, "on")) |
5175 | e4bcb14c | ths | snapshot = 1;
|
5176 | e4bcb14c | ths | else if (!strcmp(buf, "off")) |
5177 | e4bcb14c | ths | snapshot = 0;
|
5178 | e4bcb14c | ths | else {
|
5179 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
|
5180 | e4bcb14c | ths | return -1; |
5181 | e4bcb14c | ths | } |
5182 | e4bcb14c | ths | } |
5183 | e4bcb14c | ths | |
5184 | 33f00271 | balrog | if (get_param_value(buf, sizeof(buf), "cache", str)) { |
5185 | 33f00271 | balrog | if (!strcmp(buf, "off")) |
5186 | 33f00271 | balrog | cache = 0;
|
5187 | 33f00271 | balrog | else if (!strcmp(buf, "on")) |
5188 | 33f00271 | balrog | cache = 1;
|
5189 | 33f00271 | balrog | else {
|
5190 | 33f00271 | balrog | fprintf(stderr, "qemu: invalid cache option\n");
|
5191 | 33f00271 | balrog | return -1; |
5192 | 33f00271 | balrog | } |
5193 | 33f00271 | balrog | } |
5194 | 33f00271 | balrog | |
5195 | 1e72d3b7 | aurel32 | if (get_param_value(buf, sizeof(buf), "format", str)) { |
5196 | a1620fac | aurel32 | if (strcmp(buf, "?") == 0) { |
5197 | a1620fac | aurel32 | fprintf(stderr, "qemu: Supported formats:");
|
5198 | a1620fac | aurel32 | bdrv_iterate_format(bdrv_format_print, NULL);
|
5199 | a1620fac | aurel32 | fprintf(stderr, "\n");
|
5200 | a1620fac | aurel32 | return -1; |
5201 | a1620fac | aurel32 | } |
5202 | 1e72d3b7 | aurel32 | drv = bdrv_find_format(buf); |
5203 | 1e72d3b7 | aurel32 | if (!drv) {
|
5204 | 1e72d3b7 | aurel32 | fprintf(stderr, "qemu: '%s' invalid format\n", buf);
|
5205 | 1e72d3b7 | aurel32 | return -1; |
5206 | 1e72d3b7 | aurel32 | } |
5207 | 1e72d3b7 | aurel32 | } |
5208 | 1e72d3b7 | aurel32 | |
5209 | 609497ab | balrog | if (arg->file == NULL) |
5210 | 609497ab | balrog | get_param_value(file, sizeof(file), "file", str); |
5211 | 609497ab | balrog | else
|
5212 | 609497ab | balrog | pstrcpy(file, sizeof(file), arg->file);
|
5213 | e4bcb14c | ths | |
5214 | e4bcb14c | ths | /* compute bus and unit according index */
|
5215 | e4bcb14c | ths | |
5216 | e4bcb14c | ths | if (index != -1) { |
5217 | e4bcb14c | ths | if (bus_id != 0 || unit_id != -1) { |
5218 | e4bcb14c | ths | fprintf(stderr, |
5219 | e4bcb14c | ths | "qemu: '%s' index cannot be used with bus and unit\n", str);
|
5220 | e4bcb14c | ths | return -1; |
5221 | e4bcb14c | ths | } |
5222 | e4bcb14c | ths | if (max_devs == 0) |
5223 | e4bcb14c | ths | { |
5224 | e4bcb14c | ths | unit_id = index; |
5225 | e4bcb14c | ths | bus_id = 0;
|
5226 | e4bcb14c | ths | } else {
|
5227 | e4bcb14c | ths | unit_id = index % max_devs; |
5228 | e4bcb14c | ths | bus_id = index / max_devs; |
5229 | e4bcb14c | ths | } |
5230 | e4bcb14c | ths | } |
5231 | e4bcb14c | ths | |
5232 | e4bcb14c | ths | /* if user doesn't specify a unit_id,
|
5233 | e4bcb14c | ths | * try to find the first free
|
5234 | e4bcb14c | ths | */
|
5235 | e4bcb14c | ths | |
5236 | e4bcb14c | ths | if (unit_id == -1) { |
5237 | e4bcb14c | ths | unit_id = 0;
|
5238 | f60d39bc | ths | while (drive_get_index(type, bus_id, unit_id) != -1) { |
5239 | e4bcb14c | ths | unit_id++; |
5240 | e4bcb14c | ths | if (max_devs && unit_id >= max_devs) {
|
5241 | e4bcb14c | ths | unit_id -= max_devs; |
5242 | e4bcb14c | ths | bus_id++; |
5243 | e4bcb14c | ths | } |
5244 | e4bcb14c | ths | } |
5245 | e4bcb14c | ths | } |
5246 | e4bcb14c | ths | |
5247 | e4bcb14c | ths | /* check unit id */
|
5248 | e4bcb14c | ths | |
5249 | e4bcb14c | ths | if (max_devs && unit_id >= max_devs) {
|
5250 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
|
5251 | e4bcb14c | ths | str, unit_id, max_devs - 1);
|
5252 | e4bcb14c | ths | return -1; |
5253 | e4bcb14c | ths | } |
5254 | e4bcb14c | ths | |
5255 | e4bcb14c | ths | /*
|
5256 | e4bcb14c | ths | * ignore multiple definitions
|
5257 | e4bcb14c | ths | */
|
5258 | e4bcb14c | ths | |
5259 | f60d39bc | ths | if (drive_get_index(type, bus_id, unit_id) != -1) |
5260 | e4bcb14c | ths | return 0; |
5261 | e4bcb14c | ths | |
5262 | e4bcb14c | ths | /* init */
|
5263 | e4bcb14c | ths | |
5264 | f60d39bc | ths | if (type == IF_IDE || type == IF_SCSI)
|
5265 | c8522bdf | balrog | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
5266 | e6198a70 | balrog | if (max_devs)
|
5267 | e6198a70 | balrog | snprintf(buf, sizeof(buf), "%s%i%s%i", |
5268 | e6198a70 | balrog | devname, bus_id, mediastr, unit_id); |
5269 | e6198a70 | balrog | else
|
5270 | e6198a70 | balrog | snprintf(buf, sizeof(buf), "%s%s%i", |
5271 | e6198a70 | balrog | devname, mediastr, unit_id); |
5272 | e4bcb14c | ths | bdrv = bdrv_new(buf); |
5273 | e4bcb14c | ths | drives_table[nb_drives].bdrv = bdrv; |
5274 | f60d39bc | ths | drives_table[nb_drives].type = type; |
5275 | e4bcb14c | ths | drives_table[nb_drives].bus = bus_id; |
5276 | e4bcb14c | ths | drives_table[nb_drives].unit = unit_id; |
5277 | e4bcb14c | ths | nb_drives++; |
5278 | e4bcb14c | ths | |
5279 | f60d39bc | ths | switch(type) {
|
5280 | e4bcb14c | ths | case IF_IDE:
|
5281 | e4bcb14c | ths | case IF_SCSI:
|
5282 | e4bcb14c | ths | switch(media) {
|
5283 | e4bcb14c | ths | case MEDIA_DISK:
|
5284 | e4bcb14c | ths | if (cyls != 0) { |
5285 | e4bcb14c | ths | bdrv_set_geometry_hint(bdrv, cyls, heads, secs); |
5286 | e4bcb14c | ths | bdrv_set_translation_hint(bdrv, translation); |
5287 | e4bcb14c | ths | } |
5288 | e4bcb14c | ths | break;
|
5289 | e4bcb14c | ths | case MEDIA_CDROM:
|
5290 | e4bcb14c | ths | bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM); |
5291 | e4bcb14c | ths | break;
|
5292 | e4bcb14c | ths | } |
5293 | e4bcb14c | ths | break;
|
5294 | e4bcb14c | ths | case IF_SD:
|
5295 | e4bcb14c | ths | /* FIXME: This isn't really a floppy, but it's a reasonable
|
5296 | e4bcb14c | ths | approximation. */
|
5297 | e4bcb14c | ths | case IF_FLOPPY:
|
5298 | e4bcb14c | ths | bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY); |
5299 | e4bcb14c | ths | break;
|
5300 | e4bcb14c | ths | case IF_PFLASH:
|
5301 | e4bcb14c | ths | case IF_MTD:
|
5302 | e4bcb14c | ths | break;
|
5303 | e4bcb14c | ths | } |
5304 | e4bcb14c | ths | if (!file[0]) |
5305 | e4bcb14c | ths | return 0; |
5306 | 33f00271 | balrog | bdrv_flags = 0;
|
5307 | 33f00271 | balrog | if (snapshot)
|
5308 | 33f00271 | balrog | bdrv_flags |= BDRV_O_SNAPSHOT; |
5309 | 33f00271 | balrog | if (!cache)
|
5310 | 33f00271 | balrog | bdrv_flags |= BDRV_O_DIRECT; |
5311 | 1e72d3b7 | aurel32 | if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) { |
5312 | e4bcb14c | ths | fprintf(stderr, "qemu: could not open disk image %s\n",
|
5313 | e4bcb14c | ths | file); |
5314 | e4bcb14c | ths | return -1; |
5315 | e4bcb14c | ths | } |
5316 | e4bcb14c | ths | return 0; |
5317 | e4bcb14c | ths | } |
5318 | e4bcb14c | ths | |
5319 | 330d0414 | bellard | /***********************************************************/
|
5320 | a594cfbf | bellard | /* USB devices */
|
5321 | a594cfbf | bellard | |
5322 | 0d92ed30 | pbrook | static USBPort *used_usb_ports;
|
5323 | 0d92ed30 | pbrook | static USBPort *free_usb_ports;
|
5324 | 0d92ed30 | pbrook | |
5325 | 0d92ed30 | pbrook | /* ??? Maybe change this to register a hub to keep track of the topology. */
|
5326 | 0d92ed30 | pbrook | void qemu_register_usb_port(USBPort *port, void *opaque, int index, |
5327 | 0d92ed30 | pbrook | usb_attachfn attach) |
5328 | 0d92ed30 | pbrook | { |
5329 | 0d92ed30 | pbrook | port->opaque = opaque; |
5330 | 0d92ed30 | pbrook | port->index = index; |
5331 | 0d92ed30 | pbrook | port->attach = attach; |
5332 | 0d92ed30 | pbrook | port->next = free_usb_ports; |
5333 | 0d92ed30 | pbrook | free_usb_ports = port; |
5334 | 0d92ed30 | pbrook | } |
5335 | 0d92ed30 | pbrook | |
5336 | a594cfbf | bellard | static int usb_device_add(const char *devname) |
5337 | a594cfbf | bellard | { |
5338 | a594cfbf | bellard | const char *p; |
5339 | a594cfbf | bellard | USBDevice *dev; |
5340 | 0d92ed30 | pbrook | USBPort *port; |
5341 | a594cfbf | bellard | |
5342 | 0d92ed30 | pbrook | if (!free_usb_ports)
|
5343 | a594cfbf | bellard | return -1; |
5344 | a594cfbf | bellard | |
5345 | a594cfbf | bellard | if (strstart(devname, "host:", &p)) { |
5346 | a594cfbf | bellard | dev = usb_host_device_open(p); |
5347 | a594cfbf | bellard | } else if (!strcmp(devname, "mouse")) { |
5348 | a594cfbf | bellard | dev = usb_mouse_init(); |
5349 | 09b26c5e | bellard | } else if (!strcmp(devname, "tablet")) { |
5350 | 47b2d338 | balrog | dev = usb_tablet_init(); |
5351 | 47b2d338 | balrog | } else if (!strcmp(devname, "keyboard")) { |
5352 | 47b2d338 | balrog | dev = usb_keyboard_init(); |
5353 | 2e5d83bb | pbrook | } else if (strstart(devname, "disk:", &p)) { |
5354 | 2e5d83bb | pbrook | dev = usb_msd_init(p); |
5355 | f6d2a316 | balrog | } else if (!strcmp(devname, "wacom-tablet")) { |
5356 | f6d2a316 | balrog | dev = usb_wacom_init(); |
5357 | a7954218 | balrog | } else if (strstart(devname, "serial:", &p)) { |
5358 | a7954218 | balrog | dev = usb_serial_init(p); |
5359 | 2e4d9fb1 | aurel32 | #ifdef CONFIG_BRLAPI
|
5360 | 2e4d9fb1 | aurel32 | } else if (!strcmp(devname, "braille")) { |
5361 | 2e4d9fb1 | aurel32 | dev = usb_baum_init(); |
5362 | 2e4d9fb1 | aurel32 | #endif
|
5363 | a594cfbf | bellard | } else {
|
5364 | a594cfbf | bellard | return -1; |
5365 | a594cfbf | bellard | } |
5366 | 0d92ed30 | pbrook | if (!dev)
|
5367 | 0d92ed30 | pbrook | return -1; |
5368 | 0d92ed30 | pbrook | |
5369 | 0d92ed30 | pbrook | /* Find a USB port to add the device to. */
|
5370 | 0d92ed30 | pbrook | port = free_usb_ports; |
5371 | 0d92ed30 | pbrook | if (!port->next) {
|
5372 | 0d92ed30 | pbrook | USBDevice *hub; |
5373 | 0d92ed30 | pbrook | |
5374 | 0d92ed30 | pbrook | /* Create a new hub and chain it on. */
|
5375 | 0d92ed30 | pbrook | free_usb_ports = NULL;
|
5376 | 0d92ed30 | pbrook | port->next = used_usb_ports; |
5377 | 0d92ed30 | pbrook | used_usb_ports = port; |
5378 | 0d92ed30 | pbrook | |
5379 | 0d92ed30 | pbrook | hub = usb_hub_init(VM_USB_HUB_SIZE); |
5380 | 0d92ed30 | pbrook | usb_attach(port, hub); |
5381 | 0d92ed30 | pbrook | port = free_usb_ports; |
5382 | 0d92ed30 | pbrook | } |
5383 | 0d92ed30 | pbrook | |
5384 | 0d92ed30 | pbrook | free_usb_ports = port->next; |
5385 | 0d92ed30 | pbrook | port->next = used_usb_ports; |
5386 | 0d92ed30 | pbrook | used_usb_ports = port; |
5387 | 0d92ed30 | pbrook | usb_attach(port, dev); |
5388 | a594cfbf | bellard | return 0; |
5389 | a594cfbf | bellard | } |
5390 | a594cfbf | bellard | |
5391 | a594cfbf | bellard | static int usb_device_del(const char *devname) |
5392 | a594cfbf | bellard | { |
5393 | 0d92ed30 | pbrook | USBPort *port; |
5394 | 0d92ed30 | pbrook | USBPort **lastp; |
5395 | 059809e4 | bellard | USBDevice *dev; |
5396 | 0d92ed30 | pbrook | int bus_num, addr;
|
5397 | a594cfbf | bellard | const char *p; |
5398 | a594cfbf | bellard | |
5399 | 0d92ed30 | pbrook | if (!used_usb_ports)
|
5400 | a594cfbf | bellard | return -1; |
5401 | a594cfbf | bellard | |
5402 | a594cfbf | bellard | p = strchr(devname, '.');
|
5403 | 5fafdf24 | ths | if (!p)
|
5404 | a594cfbf | bellard | return -1; |
5405 | a594cfbf | bellard | bus_num = strtoul(devname, NULL, 0); |
5406 | a594cfbf | bellard | addr = strtoul(p + 1, NULL, 0); |
5407 | a594cfbf | bellard | if (bus_num != 0) |
5408 | a594cfbf | bellard | return -1; |
5409 | 0d92ed30 | pbrook | |
5410 | 0d92ed30 | pbrook | lastp = &used_usb_ports; |
5411 | 0d92ed30 | pbrook | port = used_usb_ports; |
5412 | 0d92ed30 | pbrook | while (port && port->dev->addr != addr) {
|
5413 | 0d92ed30 | pbrook | lastp = &port->next; |
5414 | 0d92ed30 | pbrook | port = port->next; |
5415 | a594cfbf | bellard | } |
5416 | 0d92ed30 | pbrook | |
5417 | 0d92ed30 | pbrook | if (!port)
|
5418 | a594cfbf | bellard | return -1; |
5419 | 0d92ed30 | pbrook | |
5420 | 059809e4 | bellard | dev = port->dev; |
5421 | 0d92ed30 | pbrook | *lastp = port->next; |
5422 | 0d92ed30 | pbrook | usb_attach(port, NULL);
|
5423 | 059809e4 | bellard | dev->handle_destroy(dev); |
5424 | 0d92ed30 | pbrook | port->next = free_usb_ports; |
5425 | 0d92ed30 | pbrook | free_usb_ports = port; |
5426 | a594cfbf | bellard | return 0; |
5427 | a594cfbf | bellard | } |
5428 | a594cfbf | bellard | |
5429 | a594cfbf | bellard | void do_usb_add(const char *devname) |
5430 | a594cfbf | bellard | { |
5431 | a594cfbf | bellard | int ret;
|
5432 | a594cfbf | bellard | ret = usb_device_add(devname); |
5433 | 5fafdf24 | ths | if (ret < 0) |
5434 | a594cfbf | bellard | term_printf("Could not add USB device '%s'\n", devname);
|
5435 | a594cfbf | bellard | } |
5436 | a594cfbf | bellard | |
5437 | a594cfbf | bellard | void do_usb_del(const char *devname) |
5438 | a594cfbf | bellard | { |
5439 | a594cfbf | bellard | int ret;
|
5440 | a594cfbf | bellard | ret = usb_device_del(devname); |
5441 | 5fafdf24 | ths | if (ret < 0) |
5442 | a594cfbf | bellard | term_printf("Could not remove USB device '%s'\n", devname);
|
5443 | a594cfbf | bellard | } |
5444 | a594cfbf | bellard | |
5445 | a594cfbf | bellard | void usb_info(void) |
5446 | a594cfbf | bellard | { |
5447 | a594cfbf | bellard | USBDevice *dev; |
5448 | 0d92ed30 | pbrook | USBPort *port; |
5449 | a594cfbf | bellard | const char *speed_str; |
5450 | a594cfbf | bellard | |
5451 | 0d92ed30 | pbrook | if (!usb_enabled) {
|
5452 | a594cfbf | bellard | term_printf("USB support not enabled\n");
|
5453 | a594cfbf | bellard | return;
|
5454 | a594cfbf | bellard | } |
5455 | a594cfbf | bellard | |
5456 | 0d92ed30 | pbrook | for (port = used_usb_ports; port; port = port->next) {
|
5457 | 0d92ed30 | pbrook | dev = port->dev; |
5458 | 0d92ed30 | pbrook | if (!dev)
|
5459 | 0d92ed30 | pbrook | continue;
|
5460 | 0d92ed30 | pbrook | switch(dev->speed) {
|
5461 | 5fafdf24 | ths | case USB_SPEED_LOW:
|
5462 | 5fafdf24 | ths | speed_str = "1.5";
|
5463 | 0d92ed30 | pbrook | break;
|
5464 | 5fafdf24 | ths | case USB_SPEED_FULL:
|
5465 | 5fafdf24 | ths | speed_str = "12";
|
5466 | 0d92ed30 | pbrook | break;
|
5467 | 5fafdf24 | ths | case USB_SPEED_HIGH:
|
5468 | 5fafdf24 | ths | speed_str = "480";
|
5469 | 0d92ed30 | pbrook | break;
|
5470 | 0d92ed30 | pbrook | default:
|
5471 | 5fafdf24 | ths | speed_str = "?";
|
5472 | 0d92ed30 | pbrook | break;
|
5473 | a594cfbf | bellard | } |
5474 | 5fafdf24 | ths | term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
|
5475 | 1f6e24e7 | bellard | 0, dev->addr, speed_str, dev->devname);
|
5476 | a594cfbf | bellard | } |
5477 | a594cfbf | bellard | } |
5478 | a594cfbf | bellard | |
5479 | f7cce898 | bellard | /***********************************************************/
|
5480 | 201a51fc | balrog | /* PCMCIA/Cardbus */
|
5481 | 201a51fc | balrog | |
5482 | 201a51fc | balrog | static struct pcmcia_socket_entry_s { |
5483 | 201a51fc | balrog | struct pcmcia_socket_s *socket;
|
5484 | 201a51fc | balrog | struct pcmcia_socket_entry_s *next;
|
5485 | 201a51fc | balrog | } *pcmcia_sockets = 0;
|
5486 | 201a51fc | balrog | |
5487 | 201a51fc | balrog | void pcmcia_socket_register(struct pcmcia_socket_s *socket) |
5488 | 201a51fc | balrog | { |
5489 | 201a51fc | balrog | struct pcmcia_socket_entry_s *entry;
|
5490 | 201a51fc | balrog | |
5491 | 201a51fc | balrog | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); |
5492 | 201a51fc | balrog | entry->socket = socket; |
5493 | 201a51fc | balrog | entry->next = pcmcia_sockets; |
5494 | 201a51fc | balrog | pcmcia_sockets = entry; |
5495 | 201a51fc | balrog | } |
5496 | 201a51fc | balrog | |
5497 | 201a51fc | balrog | void pcmcia_socket_unregister(struct pcmcia_socket_s *socket) |
5498 | 201a51fc | balrog | { |
5499 | 201a51fc | balrog | struct pcmcia_socket_entry_s *entry, **ptr;
|
5500 | 201a51fc | balrog | |
5501 | 201a51fc | balrog | ptr = &pcmcia_sockets; |
5502 | 201a51fc | balrog | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
|
5503 | 201a51fc | balrog | if (entry->socket == socket) {
|
5504 | 201a51fc | balrog | *ptr = entry->next; |
5505 | 201a51fc | balrog | qemu_free(entry); |
5506 | 201a51fc | balrog | } |
5507 | 201a51fc | balrog | } |
5508 | 201a51fc | balrog | |
5509 | 201a51fc | balrog | void pcmcia_info(void) |
5510 | 201a51fc | balrog | { |
5511 | 201a51fc | balrog | struct pcmcia_socket_entry_s *iter;
|
5512 | 201a51fc | balrog | if (!pcmcia_sockets)
|
5513 | 201a51fc | balrog | term_printf("No PCMCIA sockets\n");
|
5514 | 201a51fc | balrog | |
5515 | 201a51fc | balrog | for (iter = pcmcia_sockets; iter; iter = iter->next)
|
5516 | 201a51fc | balrog | term_printf("%s: %s\n", iter->socket->slot_string,
|
5517 | 201a51fc | balrog | iter->socket->attached ? iter->socket->card_string : |
5518 | 201a51fc | balrog | "Empty");
|
5519 | 201a51fc | balrog | } |
5520 | 201a51fc | balrog | |
5521 | 201a51fc | balrog | /***********************************************************/
|
5522 | 2ff89790 | ths | /* dumb display */
|
5523 | 2ff89790 | ths | |
5524 | 2ff89790 | ths | static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
5525 | 2ff89790 | ths | { |
5526 | 2ff89790 | ths | } |
5527 | 2ff89790 | ths | |
5528 | 2ff89790 | ths | static void dumb_resize(DisplayState *ds, int w, int h) |
5529 | 2ff89790 | ths | { |
5530 | 2ff89790 | ths | } |
5531 | 2ff89790 | ths | |
5532 | 2ff89790 | ths | static void dumb_refresh(DisplayState *ds) |
5533 | 2ff89790 | ths | { |
5534 | 2ff89790 | ths | #if defined(CONFIG_SDL)
|
5535 | 2ff89790 | ths | vga_hw_update(); |
5536 | 2ff89790 | ths | #endif
|
5537 | 2ff89790 | ths | } |
5538 | 2ff89790 | ths | |
5539 | 2ff89790 | ths | static void dumb_display_init(DisplayState *ds) |
5540 | 2ff89790 | ths | { |
5541 | 2ff89790 | ths | ds->data = NULL;
|
5542 | 2ff89790 | ths | ds->linesize = 0;
|
5543 | 2ff89790 | ths | ds->depth = 0;
|
5544 | 2ff89790 | ths | ds->dpy_update = dumb_update; |
5545 | 2ff89790 | ths | ds->dpy_resize = dumb_resize; |
5546 | 2ff89790 | ths | ds->dpy_refresh = dumb_refresh; |
5547 | 2ff89790 | ths | } |
5548 | 2ff89790 | ths | |
5549 | 2ff89790 | ths | /***********************************************************/
|
5550 | 8a7ddc38 | bellard | /* I/O handling */
|
5551 | 0824d6fc | bellard | |
5552 | c4b1fcc0 | bellard | #define MAX_IO_HANDLERS 64 |
5553 | c4b1fcc0 | bellard | |
5554 | c4b1fcc0 | bellard | typedef struct IOHandlerRecord { |
5555 | c4b1fcc0 | bellard | int fd;
|
5556 | 7c9d8e07 | bellard | IOCanRWHandler *fd_read_poll; |
5557 | 7c9d8e07 | bellard | IOHandler *fd_read; |
5558 | 7c9d8e07 | bellard | IOHandler *fd_write; |
5559 | cafffd40 | ths | int deleted;
|
5560 | c4b1fcc0 | bellard | void *opaque;
|
5561 | c4b1fcc0 | bellard | /* temporary data */
|
5562 | c4b1fcc0 | bellard | struct pollfd *ufd;
|
5563 | 8a7ddc38 | bellard | struct IOHandlerRecord *next;
|
5564 | c4b1fcc0 | bellard | } IOHandlerRecord; |
5565 | c4b1fcc0 | bellard | |
5566 | 8a7ddc38 | bellard | static IOHandlerRecord *first_io_handler;
|
5567 | c4b1fcc0 | bellard | |
5568 | 7c9d8e07 | bellard | /* XXX: fd_read_poll should be suppressed, but an API change is
|
5569 | 7c9d8e07 | bellard | necessary in the character devices to suppress fd_can_read(). */
|
5570 | 5fafdf24 | ths | int qemu_set_fd_handler2(int fd, |
5571 | 5fafdf24 | ths | IOCanRWHandler *fd_read_poll, |
5572 | 5fafdf24 | ths | IOHandler *fd_read, |
5573 | 5fafdf24 | ths | IOHandler *fd_write, |
5574 | 7c9d8e07 | bellard | void *opaque)
|
5575 | c4b1fcc0 | bellard | { |
5576 | 7c9d8e07 | bellard | IOHandlerRecord **pioh, *ioh; |
5577 | c4b1fcc0 | bellard | |
5578 | 7c9d8e07 | bellard | if (!fd_read && !fd_write) {
|
5579 | 7c9d8e07 | bellard | pioh = &first_io_handler; |
5580 | 7c9d8e07 | bellard | for(;;) {
|
5581 | 7c9d8e07 | bellard | ioh = *pioh; |
5582 | 7c9d8e07 | bellard | if (ioh == NULL) |
5583 | 7c9d8e07 | bellard | break;
|
5584 | 7c9d8e07 | bellard | if (ioh->fd == fd) {
|
5585 | cafffd40 | ths | ioh->deleted = 1;
|
5586 | 7c9d8e07 | bellard | break;
|
5587 | 7c9d8e07 | bellard | } |
5588 | 7c9d8e07 | bellard | pioh = &ioh->next; |
5589 | 7c9d8e07 | bellard | } |
5590 | 7c9d8e07 | bellard | } else {
|
5591 | 7c9d8e07 | bellard | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
5592 | 7c9d8e07 | bellard | if (ioh->fd == fd)
|
5593 | 7c9d8e07 | bellard | goto found;
|
5594 | 7c9d8e07 | bellard | } |
5595 | 7c9d8e07 | bellard | ioh = qemu_mallocz(sizeof(IOHandlerRecord));
|
5596 | 7c9d8e07 | bellard | if (!ioh)
|
5597 | 7c9d8e07 | bellard | return -1; |
5598 | 7c9d8e07 | bellard | ioh->next = first_io_handler; |
5599 | 7c9d8e07 | bellard | first_io_handler = ioh; |
5600 | 7c9d8e07 | bellard | found:
|
5601 | 7c9d8e07 | bellard | ioh->fd = fd; |
5602 | 7c9d8e07 | bellard | ioh->fd_read_poll = fd_read_poll; |
5603 | 7c9d8e07 | bellard | ioh->fd_read = fd_read; |
5604 | 7c9d8e07 | bellard | ioh->fd_write = fd_write; |
5605 | 7c9d8e07 | bellard | ioh->opaque = opaque; |
5606 | cafffd40 | ths | ioh->deleted = 0;
|
5607 | 7c9d8e07 | bellard | } |
5608 | c4b1fcc0 | bellard | return 0; |
5609 | c4b1fcc0 | bellard | } |
5610 | c4b1fcc0 | bellard | |
5611 | 5fafdf24 | ths | int qemu_set_fd_handler(int fd, |
5612 | 5fafdf24 | ths | IOHandler *fd_read, |
5613 | 5fafdf24 | ths | IOHandler *fd_write, |
5614 | 7c9d8e07 | bellard | void *opaque)
|
5615 | 8a7ddc38 | bellard | { |
5616 | 7c9d8e07 | bellard | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
5617 | 8a7ddc38 | bellard | } |
5618 | 8a7ddc38 | bellard | |
5619 | 8a7ddc38 | bellard | /***********************************************************/
|
5620 | f331110f | bellard | /* Polling handling */
|
5621 | f331110f | bellard | |
5622 | f331110f | bellard | typedef struct PollingEntry { |
5623 | f331110f | bellard | PollingFunc *func; |
5624 | f331110f | bellard | void *opaque;
|
5625 | f331110f | bellard | struct PollingEntry *next;
|
5626 | f331110f | bellard | } PollingEntry; |
5627 | f331110f | bellard | |
5628 | f331110f | bellard | static PollingEntry *first_polling_entry;
|
5629 | f331110f | bellard | |
5630 | f331110f | bellard | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
5631 | f331110f | bellard | { |
5632 | f331110f | bellard | PollingEntry **ppe, *pe; |
5633 | f331110f | bellard | pe = qemu_mallocz(sizeof(PollingEntry));
|
5634 | f331110f | bellard | if (!pe)
|
5635 | f331110f | bellard | return -1; |
5636 | f331110f | bellard | pe->func = func; |
5637 | f331110f | bellard | pe->opaque = opaque; |
5638 | f331110f | bellard | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
5639 | f331110f | bellard | *ppe = pe; |
5640 | f331110f | bellard | return 0; |
5641 | f331110f | bellard | } |
5642 | f331110f | bellard | |
5643 | f331110f | bellard | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
5644 | f331110f | bellard | { |
5645 | f331110f | bellard | PollingEntry **ppe, *pe; |
5646 | f331110f | bellard | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
5647 | f331110f | bellard | pe = *ppe; |
5648 | f331110f | bellard | if (pe->func == func && pe->opaque == opaque) {
|
5649 | f331110f | bellard | *ppe = pe->next; |
5650 | f331110f | bellard | qemu_free(pe); |
5651 | f331110f | bellard | break;
|
5652 | f331110f | bellard | } |
5653 | f331110f | bellard | } |
5654 | f331110f | bellard | } |
5655 | f331110f | bellard | |
5656 | a18e524a | bellard | #ifdef _WIN32
|
5657 | a18e524a | bellard | /***********************************************************/
|
5658 | a18e524a | bellard | /* Wait objects support */
|
5659 | a18e524a | bellard | typedef struct WaitObjects { |
5660 | a18e524a | bellard | int num;
|
5661 | a18e524a | bellard | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
|
5662 | a18e524a | bellard | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
|
5663 | a18e524a | bellard | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
5664 | a18e524a | bellard | } WaitObjects; |
5665 | a18e524a | bellard | |
5666 | a18e524a | bellard | static WaitObjects wait_objects = {0}; |
5667 | 3b46e624 | ths | |
5668 | a18e524a | bellard | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
5669 | a18e524a | bellard | { |
5670 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
5671 | a18e524a | bellard | |
5672 | a18e524a | bellard | if (w->num >= MAXIMUM_WAIT_OBJECTS)
|
5673 | a18e524a | bellard | return -1; |
5674 | a18e524a | bellard | w->events[w->num] = handle; |
5675 | a18e524a | bellard | w->func[w->num] = func; |
5676 | a18e524a | bellard | w->opaque[w->num] = opaque; |
5677 | a18e524a | bellard | w->num++; |
5678 | a18e524a | bellard | return 0; |
5679 | a18e524a | bellard | } |
5680 | a18e524a | bellard | |
5681 | a18e524a | bellard | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
5682 | a18e524a | bellard | { |
5683 | a18e524a | bellard | int i, found;
|
5684 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
5685 | a18e524a | bellard | |
5686 | a18e524a | bellard | found = 0;
|
5687 | a18e524a | bellard | for (i = 0; i < w->num; i++) { |
5688 | a18e524a | bellard | if (w->events[i] == handle)
|
5689 | a18e524a | bellard | found = 1;
|
5690 | a18e524a | bellard | if (found) {
|
5691 | a18e524a | bellard | w->events[i] = w->events[i + 1];
|
5692 | a18e524a | bellard | w->func[i] = w->func[i + 1];
|
5693 | a18e524a | bellard | w->opaque[i] = w->opaque[i + 1];
|
5694 | 3b46e624 | ths | } |
5695 | a18e524a | bellard | } |
5696 | a18e524a | bellard | if (found)
|
5697 | a18e524a | bellard | w->num--; |
5698 | a18e524a | bellard | } |
5699 | a18e524a | bellard | #endif
|
5700 | a18e524a | bellard | |
5701 | f331110f | bellard | /***********************************************************/
|
5702 | 8a7ddc38 | bellard | /* savevm/loadvm support */
|
5703 | 8a7ddc38 | bellard | |
5704 | faea38e7 | bellard | #define IO_BUF_SIZE 32768 |
5705 | faea38e7 | bellard | |
5706 | faea38e7 | bellard | struct QEMUFile {
|
5707 | faea38e7 | bellard | FILE *outfile; |
5708 | faea38e7 | bellard | BlockDriverState *bs; |
5709 | faea38e7 | bellard | int is_file;
|
5710 | faea38e7 | bellard | int is_writable;
|
5711 | faea38e7 | bellard | int64_t base_offset; |
5712 | faea38e7 | bellard | int64_t buf_offset; /* start of buffer when writing, end of buffer
|
5713 | faea38e7 | bellard | when reading */
|
5714 | faea38e7 | bellard | int buf_index;
|
5715 | faea38e7 | bellard | int buf_size; /* 0 when writing */ |
5716 | faea38e7 | bellard | uint8_t buf[IO_BUF_SIZE]; |
5717 | faea38e7 | bellard | }; |
5718 | faea38e7 | bellard | |
5719 | faea38e7 | bellard | QEMUFile *qemu_fopen(const char *filename, const char *mode) |
5720 | faea38e7 | bellard | { |
5721 | faea38e7 | bellard | QEMUFile *f; |
5722 | faea38e7 | bellard | |
5723 | faea38e7 | bellard | f = qemu_mallocz(sizeof(QEMUFile));
|
5724 | faea38e7 | bellard | if (!f)
|
5725 | faea38e7 | bellard | return NULL; |
5726 | faea38e7 | bellard | if (!strcmp(mode, "wb")) { |
5727 | faea38e7 | bellard | f->is_writable = 1;
|
5728 | faea38e7 | bellard | } else if (!strcmp(mode, "rb")) { |
5729 | faea38e7 | bellard | f->is_writable = 0;
|
5730 | faea38e7 | bellard | } else {
|
5731 | faea38e7 | bellard | goto fail;
|
5732 | faea38e7 | bellard | } |
5733 | faea38e7 | bellard | f->outfile = fopen(filename, mode); |
5734 | faea38e7 | bellard | if (!f->outfile)
|
5735 | faea38e7 | bellard | goto fail;
|
5736 | faea38e7 | bellard | f->is_file = 1;
|
5737 | faea38e7 | bellard | return f;
|
5738 | faea38e7 | bellard | fail:
|
5739 | faea38e7 | bellard | if (f->outfile)
|
5740 | faea38e7 | bellard | fclose(f->outfile); |
5741 | faea38e7 | bellard | qemu_free(f); |
5742 | faea38e7 | bellard | return NULL; |
5743 | faea38e7 | bellard | } |
5744 | faea38e7 | bellard | |
5745 | 9596ebb7 | pbrook | static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable) |
5746 | faea38e7 | bellard | { |
5747 | faea38e7 | bellard | QEMUFile *f; |
5748 | faea38e7 | bellard | |
5749 | faea38e7 | bellard | f = qemu_mallocz(sizeof(QEMUFile));
|
5750 | faea38e7 | bellard | if (!f)
|
5751 | faea38e7 | bellard | return NULL; |
5752 | faea38e7 | bellard | f->is_file = 0;
|
5753 | faea38e7 | bellard | f->bs = bs; |
5754 | faea38e7 | bellard | f->is_writable = is_writable; |
5755 | faea38e7 | bellard | f->base_offset = offset; |
5756 | faea38e7 | bellard | return f;
|
5757 | faea38e7 | bellard | } |
5758 | faea38e7 | bellard | |
5759 | faea38e7 | bellard | void qemu_fflush(QEMUFile *f)
|
5760 | faea38e7 | bellard | { |
5761 | faea38e7 | bellard | if (!f->is_writable)
|
5762 | faea38e7 | bellard | return;
|
5763 | faea38e7 | bellard | if (f->buf_index > 0) { |
5764 | faea38e7 | bellard | if (f->is_file) {
|
5765 | faea38e7 | bellard | fseek(f->outfile, f->buf_offset, SEEK_SET); |
5766 | faea38e7 | bellard | fwrite(f->buf, 1, f->buf_index, f->outfile);
|
5767 | faea38e7 | bellard | } else {
|
5768 | 5fafdf24 | ths | bdrv_pwrite(f->bs, f->base_offset + f->buf_offset, |
5769 | faea38e7 | bellard | f->buf, f->buf_index); |
5770 | faea38e7 | bellard | } |
5771 | faea38e7 | bellard | f->buf_offset += f->buf_index; |
5772 | faea38e7 | bellard | f->buf_index = 0;
|
5773 | faea38e7 | bellard | } |
5774 | faea38e7 | bellard | } |
5775 | faea38e7 | bellard | |
5776 | faea38e7 | bellard | static void qemu_fill_buffer(QEMUFile *f) |
5777 | faea38e7 | bellard | { |
5778 | faea38e7 | bellard | int len;
|
5779 | faea38e7 | bellard | |
5780 | faea38e7 | bellard | if (f->is_writable)
|
5781 | faea38e7 | bellard | return;
|
5782 | faea38e7 | bellard | if (f->is_file) {
|
5783 | faea38e7 | bellard | fseek(f->outfile, f->buf_offset, SEEK_SET); |
5784 | faea38e7 | bellard | len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile);
|
5785 | faea38e7 | bellard | if (len < 0) |
5786 | faea38e7 | bellard | len = 0;
|
5787 | faea38e7 | bellard | } else {
|
5788 | 5fafdf24 | ths | len = bdrv_pread(f->bs, f->base_offset + f->buf_offset, |
5789 | faea38e7 | bellard | f->buf, IO_BUF_SIZE); |
5790 | faea38e7 | bellard | if (len < 0) |
5791 | faea38e7 | bellard | len = 0;
|
5792 | faea38e7 | bellard | } |
5793 | faea38e7 | bellard | f->buf_index = 0;
|
5794 | faea38e7 | bellard | f->buf_size = len; |
5795 | faea38e7 | bellard | f->buf_offset += len; |
5796 | faea38e7 | bellard | } |
5797 | faea38e7 | bellard | |
5798 | faea38e7 | bellard | void qemu_fclose(QEMUFile *f)
|
5799 | faea38e7 | bellard | { |
5800 | faea38e7 | bellard | if (f->is_writable)
|
5801 | faea38e7 | bellard | qemu_fflush(f); |
5802 | faea38e7 | bellard | if (f->is_file) {
|
5803 | faea38e7 | bellard | fclose(f->outfile); |
5804 | faea38e7 | bellard | } |
5805 | faea38e7 | bellard | qemu_free(f); |
5806 | faea38e7 | bellard | } |
5807 | faea38e7 | bellard | |
5808 | 8a7ddc38 | bellard | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
5809 | b4608c04 | bellard | { |
5810 | faea38e7 | bellard | int l;
|
5811 | faea38e7 | bellard | while (size > 0) { |
5812 | faea38e7 | bellard | l = IO_BUF_SIZE - f->buf_index; |
5813 | faea38e7 | bellard | if (l > size)
|
5814 | faea38e7 | bellard | l = size; |
5815 | faea38e7 | bellard | memcpy(f->buf + f->buf_index, buf, l); |
5816 | faea38e7 | bellard | f->buf_index += l; |
5817 | faea38e7 | bellard | buf += l; |
5818 | faea38e7 | bellard | size -= l; |
5819 | faea38e7 | bellard | if (f->buf_index >= IO_BUF_SIZE)
|
5820 | faea38e7 | bellard | qemu_fflush(f); |
5821 | faea38e7 | bellard | } |
5822 | b4608c04 | bellard | } |
5823 | b4608c04 | bellard | |
5824 | 8a7ddc38 | bellard | void qemu_put_byte(QEMUFile *f, int v) |
5825 | b4608c04 | bellard | { |
5826 | faea38e7 | bellard | f->buf[f->buf_index++] = v; |
5827 | faea38e7 | bellard | if (f->buf_index >= IO_BUF_SIZE)
|
5828 | faea38e7 | bellard | qemu_fflush(f); |
5829 | faea38e7 | bellard | } |
5830 | faea38e7 | bellard | |
5831 | faea38e7 | bellard | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1) |
5832 | faea38e7 | bellard | { |
5833 | faea38e7 | bellard | int size, l;
|
5834 | faea38e7 | bellard | |
5835 | faea38e7 | bellard | size = size1; |
5836 | faea38e7 | bellard | while (size > 0) { |
5837 | faea38e7 | bellard | l = f->buf_size - f->buf_index; |
5838 | faea38e7 | bellard | if (l == 0) { |
5839 | faea38e7 | bellard | qemu_fill_buffer(f); |
5840 | faea38e7 | bellard | l = f->buf_size - f->buf_index; |
5841 | faea38e7 | bellard | if (l == 0) |
5842 | faea38e7 | bellard | break;
|
5843 | faea38e7 | bellard | } |
5844 | faea38e7 | bellard | if (l > size)
|
5845 | faea38e7 | bellard | l = size; |
5846 | faea38e7 | bellard | memcpy(buf, f->buf + f->buf_index, l); |
5847 | faea38e7 | bellard | f->buf_index += l; |
5848 | faea38e7 | bellard | buf += l; |
5849 | faea38e7 | bellard | size -= l; |
5850 | faea38e7 | bellard | } |
5851 | faea38e7 | bellard | return size1 - size;
|
5852 | faea38e7 | bellard | } |
5853 | faea38e7 | bellard | |
5854 | faea38e7 | bellard | int qemu_get_byte(QEMUFile *f)
|
5855 | faea38e7 | bellard | { |
5856 | faea38e7 | bellard | if (f->buf_index >= f->buf_size) {
|
5857 | faea38e7 | bellard | qemu_fill_buffer(f); |
5858 | faea38e7 | bellard | if (f->buf_index >= f->buf_size)
|
5859 | faea38e7 | bellard | return 0; |
5860 | faea38e7 | bellard | } |
5861 | faea38e7 | bellard | return f->buf[f->buf_index++];
|
5862 | faea38e7 | bellard | } |
5863 | faea38e7 | bellard | |
5864 | faea38e7 | bellard | int64_t qemu_ftell(QEMUFile *f) |
5865 | faea38e7 | bellard | { |
5866 | faea38e7 | bellard | return f->buf_offset - f->buf_size + f->buf_index;
|
5867 | faea38e7 | bellard | } |
5868 | faea38e7 | bellard | |
5869 | faea38e7 | bellard | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
|
5870 | faea38e7 | bellard | { |
5871 | faea38e7 | bellard | if (whence == SEEK_SET) {
|
5872 | faea38e7 | bellard | /* nothing to do */
|
5873 | faea38e7 | bellard | } else if (whence == SEEK_CUR) { |
5874 | faea38e7 | bellard | pos += qemu_ftell(f); |
5875 | faea38e7 | bellard | } else {
|
5876 | faea38e7 | bellard | /* SEEK_END not supported */
|
5877 | faea38e7 | bellard | return -1; |
5878 | faea38e7 | bellard | } |
5879 | faea38e7 | bellard | if (f->is_writable) {
|
5880 | faea38e7 | bellard | qemu_fflush(f); |
5881 | faea38e7 | bellard | f->buf_offset = pos; |
5882 | faea38e7 | bellard | } else {
|
5883 | faea38e7 | bellard | f->buf_offset = pos; |
5884 | faea38e7 | bellard | f->buf_index = 0;
|
5885 | faea38e7 | bellard | f->buf_size = 0;
|
5886 | faea38e7 | bellard | } |
5887 | faea38e7 | bellard | return pos;
|
5888 | 8a7ddc38 | bellard | } |
5889 | 8a7ddc38 | bellard | |
5890 | 8a7ddc38 | bellard | void qemu_put_be16(QEMUFile *f, unsigned int v) |
5891 | 8a7ddc38 | bellard | { |
5892 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 8);
|
5893 | 8a7ddc38 | bellard | qemu_put_byte(f, v); |
5894 | 8a7ddc38 | bellard | } |
5895 | 8a7ddc38 | bellard | |
5896 | 8a7ddc38 | bellard | void qemu_put_be32(QEMUFile *f, unsigned int v) |
5897 | 8a7ddc38 | bellard | { |
5898 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 24);
|
5899 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 16);
|
5900 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 8);
|
5901 | 8a7ddc38 | bellard | qemu_put_byte(f, v); |
5902 | 8a7ddc38 | bellard | } |
5903 | 8a7ddc38 | bellard | |
5904 | 8a7ddc38 | bellard | void qemu_put_be64(QEMUFile *f, uint64_t v)
|
5905 | 8a7ddc38 | bellard | { |
5906 | 8a7ddc38 | bellard | qemu_put_be32(f, v >> 32);
|
5907 | 8a7ddc38 | bellard | qemu_put_be32(f, v); |
5908 | 8a7ddc38 | bellard | } |
5909 | 8a7ddc38 | bellard | |
5910 | 8a7ddc38 | bellard | unsigned int qemu_get_be16(QEMUFile *f) |
5911 | 8a7ddc38 | bellard | { |
5912 | 8a7ddc38 | bellard | unsigned int v; |
5913 | 8a7ddc38 | bellard | v = qemu_get_byte(f) << 8;
|
5914 | 8a7ddc38 | bellard | v |= qemu_get_byte(f); |
5915 | 8a7ddc38 | bellard | return v;
|
5916 | 8a7ddc38 | bellard | } |
5917 | 8a7ddc38 | bellard | |
5918 | 8a7ddc38 | bellard | unsigned int qemu_get_be32(QEMUFile *f) |
5919 | 8a7ddc38 | bellard | { |
5920 | 8a7ddc38 | bellard | unsigned int v; |
5921 | 8a7ddc38 | bellard | v = qemu_get_byte(f) << 24;
|
5922 | 8a7ddc38 | bellard | v |= qemu_get_byte(f) << 16;
|
5923 | 8a7ddc38 | bellard | v |= qemu_get_byte(f) << 8;
|
5924 | 8a7ddc38 | bellard | v |= qemu_get_byte(f); |
5925 | 8a7ddc38 | bellard | return v;
|
5926 | 8a7ddc38 | bellard | } |
5927 | 8a7ddc38 | bellard | |
5928 | 8a7ddc38 | bellard | uint64_t qemu_get_be64(QEMUFile *f) |
5929 | 8a7ddc38 | bellard | { |
5930 | 8a7ddc38 | bellard | uint64_t v; |
5931 | 8a7ddc38 | bellard | v = (uint64_t)qemu_get_be32(f) << 32;
|
5932 | 8a7ddc38 | bellard | v |= qemu_get_be32(f); |
5933 | 8a7ddc38 | bellard | return v;
|
5934 | 8a7ddc38 | bellard | } |
5935 | 8a7ddc38 | bellard | |
5936 | 8a7ddc38 | bellard | typedef struct SaveStateEntry { |
5937 | 8a7ddc38 | bellard | char idstr[256]; |
5938 | 8a7ddc38 | bellard | int instance_id;
|
5939 | 8a7ddc38 | bellard | int version_id;
|
5940 | 8a7ddc38 | bellard | SaveStateHandler *save_state; |
5941 | 8a7ddc38 | bellard | LoadStateHandler *load_state; |
5942 | 8a7ddc38 | bellard | void *opaque;
|
5943 | 8a7ddc38 | bellard | struct SaveStateEntry *next;
|
5944 | 8a7ddc38 | bellard | } SaveStateEntry; |
5945 | b4608c04 | bellard | |
5946 | 8a7ddc38 | bellard | static SaveStateEntry *first_se;
|
5947 | 8a7ddc38 | bellard | |
5948 | 5fafdf24 | ths | int register_savevm(const char *idstr, |
5949 | 5fafdf24 | ths | int instance_id,
|
5950 | 8a7ddc38 | bellard | int version_id,
|
5951 | 8a7ddc38 | bellard | SaveStateHandler *save_state, |
5952 | 8a7ddc38 | bellard | LoadStateHandler *load_state, |
5953 | 8a7ddc38 | bellard | void *opaque)
|
5954 | 8a7ddc38 | bellard | { |
5955 | 8a7ddc38 | bellard | SaveStateEntry *se, **pse; |
5956 | 8a7ddc38 | bellard | |
5957 | 8a7ddc38 | bellard | se = qemu_malloc(sizeof(SaveStateEntry));
|
5958 | 8a7ddc38 | bellard | if (!se)
|
5959 | 8a7ddc38 | bellard | return -1; |
5960 | 8a7ddc38 | bellard | pstrcpy(se->idstr, sizeof(se->idstr), idstr);
|
5961 | 8a7ddc38 | bellard | se->instance_id = instance_id; |
5962 | 8a7ddc38 | bellard | se->version_id = version_id; |
5963 | 8a7ddc38 | bellard | se->save_state = save_state; |
5964 | 8a7ddc38 | bellard | se->load_state = load_state; |
5965 | 8a7ddc38 | bellard | se->opaque = opaque; |
5966 | 8a7ddc38 | bellard | se->next = NULL;
|
5967 | 8a7ddc38 | bellard | |
5968 | 8a7ddc38 | bellard | /* add at the end of list */
|
5969 | 8a7ddc38 | bellard | pse = &first_se; |
5970 | 8a7ddc38 | bellard | while (*pse != NULL) |
5971 | 8a7ddc38 | bellard | pse = &(*pse)->next; |
5972 | 8a7ddc38 | bellard | *pse = se; |
5973 | 8a7ddc38 | bellard | return 0; |
5974 | 8a7ddc38 | bellard | } |
5975 | 8a7ddc38 | bellard | |
5976 | 8a7ddc38 | bellard | #define QEMU_VM_FILE_MAGIC 0x5145564d |
5977 | faea38e7 | bellard | #define QEMU_VM_FILE_VERSION 0x00000002 |
5978 | 8a7ddc38 | bellard | |
5979 | 9596ebb7 | pbrook | static int qemu_savevm_state(QEMUFile *f) |
5980 | 8a7ddc38 | bellard | { |
5981 | 8a7ddc38 | bellard | SaveStateEntry *se; |
5982 | faea38e7 | bellard | int len, ret;
|
5983 | faea38e7 | bellard | int64_t cur_pos, len_pos, total_len_pos; |
5984 | 313aa567 | bellard | |
5985 | 8a7ddc38 | bellard | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
5986 | 8a7ddc38 | bellard | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
5987 | faea38e7 | bellard | total_len_pos = qemu_ftell(f); |
5988 | faea38e7 | bellard | qemu_put_be64(f, 0); /* total size */ |
5989 | 8a7ddc38 | bellard | |
5990 | 8a7ddc38 | bellard | for(se = first_se; se != NULL; se = se->next) { |
5991 | 8a7ddc38 | bellard | /* ID string */
|
5992 | 8a7ddc38 | bellard | len = strlen(se->idstr); |
5993 | 8a7ddc38 | bellard | qemu_put_byte(f, len); |
5994 | ffe8ab83 | ths | qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
5995 | 8a7ddc38 | bellard | |
5996 | 8a7ddc38 | bellard | qemu_put_be32(f, se->instance_id); |
5997 | 8a7ddc38 | bellard | qemu_put_be32(f, se->version_id); |
5998 | 8a7ddc38 | bellard | |
5999 | 8a7ddc38 | bellard | /* record size: filled later */
|
6000 | faea38e7 | bellard | len_pos = qemu_ftell(f); |
6001 | 8a7ddc38 | bellard | qemu_put_be32(f, 0);
|
6002 | 8a7ddc38 | bellard | se->save_state(f, se->opaque); |
6003 | 8a7ddc38 | bellard | |
6004 | 8a7ddc38 | bellard | /* fill record size */
|
6005 | faea38e7 | bellard | cur_pos = qemu_ftell(f); |
6006 | faea38e7 | bellard | len = cur_pos - len_pos - 4;
|
6007 | faea38e7 | bellard | qemu_fseek(f, len_pos, SEEK_SET); |
6008 | 8a7ddc38 | bellard | qemu_put_be32(f, len); |
6009 | faea38e7 | bellard | qemu_fseek(f, cur_pos, SEEK_SET); |
6010 | 8a7ddc38 | bellard | } |
6011 | faea38e7 | bellard | cur_pos = qemu_ftell(f); |
6012 | faea38e7 | bellard | qemu_fseek(f, total_len_pos, SEEK_SET); |
6013 | faea38e7 | bellard | qemu_put_be64(f, cur_pos - total_len_pos - 8);
|
6014 | faea38e7 | bellard | qemu_fseek(f, cur_pos, SEEK_SET); |
6015 | 8a7ddc38 | bellard | |
6016 | 8a7ddc38 | bellard | ret = 0;
|
6017 | 8a7ddc38 | bellard | return ret;
|
6018 | 8a7ddc38 | bellard | } |
6019 | 8a7ddc38 | bellard | |
6020 | 8a7ddc38 | bellard | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
6021 | 8a7ddc38 | bellard | { |
6022 | 8a7ddc38 | bellard | SaveStateEntry *se; |
6023 | 8a7ddc38 | bellard | |
6024 | 8a7ddc38 | bellard | for(se = first_se; se != NULL; se = se->next) { |
6025 | 5fafdf24 | ths | if (!strcmp(se->idstr, idstr) &&
|
6026 | 8a7ddc38 | bellard | instance_id == se->instance_id) |
6027 | 8a7ddc38 | bellard | return se;
|
6028 | 8a7ddc38 | bellard | } |
6029 | 8a7ddc38 | bellard | return NULL; |
6030 | 8a7ddc38 | bellard | } |
6031 | 8a7ddc38 | bellard | |
6032 | 9596ebb7 | pbrook | static int qemu_loadvm_state(QEMUFile *f) |
6033 | 8a7ddc38 | bellard | { |
6034 | 8a7ddc38 | bellard | SaveStateEntry *se; |
6035 | faea38e7 | bellard | int len, ret, instance_id, record_len, version_id;
|
6036 | faea38e7 | bellard | int64_t total_len, end_pos, cur_pos; |
6037 | 8a7ddc38 | bellard | unsigned int v; |
6038 | 8a7ddc38 | bellard | char idstr[256]; |
6039 | 3b46e624 | ths | |
6040 | 8a7ddc38 | bellard | v = qemu_get_be32(f); |
6041 | 8a7ddc38 | bellard | if (v != QEMU_VM_FILE_MAGIC)
|
6042 | 8a7ddc38 | bellard | goto fail;
|
6043 | 8a7ddc38 | bellard | v = qemu_get_be32(f); |
6044 | 8a7ddc38 | bellard | if (v != QEMU_VM_FILE_VERSION) {
|
6045 | 8a7ddc38 | bellard | fail:
|
6046 | 8a7ddc38 | bellard | ret = -1;
|
6047 | 8a7ddc38 | bellard | goto the_end;
|
6048 | 8a7ddc38 | bellard | } |
6049 | faea38e7 | bellard | total_len = qemu_get_be64(f); |
6050 | faea38e7 | bellard | end_pos = total_len + qemu_ftell(f); |
6051 | b4608c04 | bellard | for(;;) {
|
6052 | faea38e7 | bellard | if (qemu_ftell(f) >= end_pos)
|
6053 | cd4c3e88 | bellard | break;
|
6054 | faea38e7 | bellard | len = qemu_get_byte(f); |
6055 | ffe8ab83 | ths | qemu_get_buffer(f, (uint8_t *)idstr, len); |
6056 | 8a7ddc38 | bellard | idstr[len] = '\0';
|
6057 | 8a7ddc38 | bellard | instance_id = qemu_get_be32(f); |
6058 | 8a7ddc38 | bellard | version_id = qemu_get_be32(f); |
6059 | 8a7ddc38 | bellard | record_len = qemu_get_be32(f); |
6060 | 8a7ddc38 | bellard | #if 0
|
6061 | 5fafdf24 | ths | printf("idstr=%s instance=0x%x version=%d len=%d\n",
|
6062 | 8a7ddc38 | bellard | idstr, instance_id, version_id, record_len);
|
6063 | 8a7ddc38 | bellard | #endif
|
6064 | faea38e7 | bellard | cur_pos = qemu_ftell(f); |
6065 | 8a7ddc38 | bellard | se = find_se(idstr, instance_id); |
6066 | 8a7ddc38 | bellard | if (!se) {
|
6067 | 5fafdf24 | ths | fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
|
6068 | 8a7ddc38 | bellard | instance_id, idstr); |
6069 | 8a7ddc38 | bellard | } else {
|
6070 | 8a7ddc38 | bellard | ret = se->load_state(f, se->opaque, version_id); |
6071 | 8a7ddc38 | bellard | if (ret < 0) { |
6072 | 5fafdf24 | ths | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
|
6073 | 8a7ddc38 | bellard | instance_id, idstr); |
6074 | 8a7ddc38 | bellard | } |
6075 | 34865134 | bellard | } |
6076 | 8a7ddc38 | bellard | /* always seek to exact end of record */
|
6077 | 8a7ddc38 | bellard | qemu_fseek(f, cur_pos + record_len, SEEK_SET); |
6078 | 8a7ddc38 | bellard | } |
6079 | 8a7ddc38 | bellard | ret = 0;
|
6080 | 8a7ddc38 | bellard | the_end:
|
6081 | faea38e7 | bellard | return ret;
|
6082 | faea38e7 | bellard | } |
6083 | faea38e7 | bellard | |
6084 | faea38e7 | bellard | /* device can contain snapshots */
|
6085 | faea38e7 | bellard | static int bdrv_can_snapshot(BlockDriverState *bs) |
6086 | faea38e7 | bellard | { |
6087 | faea38e7 | bellard | return (bs &&
|
6088 | faea38e7 | bellard | !bdrv_is_removable(bs) && |
6089 | faea38e7 | bellard | !bdrv_is_read_only(bs)); |
6090 | faea38e7 | bellard | } |
6091 | faea38e7 | bellard | |
6092 | faea38e7 | bellard | /* device must be snapshots in order to have a reliable snapshot */
|
6093 | faea38e7 | bellard | static int bdrv_has_snapshot(BlockDriverState *bs) |
6094 | faea38e7 | bellard | { |
6095 | faea38e7 | bellard | return (bs &&
|
6096 | faea38e7 | bellard | !bdrv_is_removable(bs) && |
6097 | faea38e7 | bellard | !bdrv_is_read_only(bs)); |
6098 | faea38e7 | bellard | } |
6099 | faea38e7 | bellard | |
6100 | faea38e7 | bellard | static BlockDriverState *get_bs_snapshots(void) |
6101 | faea38e7 | bellard | { |
6102 | faea38e7 | bellard | BlockDriverState *bs; |
6103 | faea38e7 | bellard | int i;
|
6104 | faea38e7 | bellard | |
6105 | faea38e7 | bellard | if (bs_snapshots)
|
6106 | faea38e7 | bellard | return bs_snapshots;
|
6107 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6108 | e4bcb14c | ths | bs = drives_table[i].bdrv; |
6109 | faea38e7 | bellard | if (bdrv_can_snapshot(bs))
|
6110 | faea38e7 | bellard | goto ok;
|
6111 | faea38e7 | bellard | } |
6112 | faea38e7 | bellard | return NULL; |
6113 | faea38e7 | bellard | ok:
|
6114 | faea38e7 | bellard | bs_snapshots = bs; |
6115 | faea38e7 | bellard | return bs;
|
6116 | faea38e7 | bellard | } |
6117 | faea38e7 | bellard | |
6118 | faea38e7 | bellard | static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
6119 | faea38e7 | bellard | const char *name) |
6120 | faea38e7 | bellard | { |
6121 | faea38e7 | bellard | QEMUSnapshotInfo *sn_tab, *sn; |
6122 | faea38e7 | bellard | int nb_sns, i, ret;
|
6123 | 3b46e624 | ths | |
6124 | faea38e7 | bellard | ret = -ENOENT; |
6125 | faea38e7 | bellard | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
6126 | faea38e7 | bellard | if (nb_sns < 0) |
6127 | faea38e7 | bellard | return ret;
|
6128 | faea38e7 | bellard | for(i = 0; i < nb_sns; i++) { |
6129 | faea38e7 | bellard | sn = &sn_tab[i]; |
6130 | faea38e7 | bellard | if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
|
6131 | faea38e7 | bellard | *sn_info = *sn; |
6132 | faea38e7 | bellard | ret = 0;
|
6133 | faea38e7 | bellard | break;
|
6134 | faea38e7 | bellard | } |
6135 | faea38e7 | bellard | } |
6136 | faea38e7 | bellard | qemu_free(sn_tab); |
6137 | faea38e7 | bellard | return ret;
|
6138 | faea38e7 | bellard | } |
6139 | faea38e7 | bellard | |
6140 | faea38e7 | bellard | void do_savevm(const char *name) |
6141 | faea38e7 | bellard | { |
6142 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6143 | faea38e7 | bellard | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; |
6144 | faea38e7 | bellard | int must_delete, ret, i;
|
6145 | faea38e7 | bellard | BlockDriverInfo bdi1, *bdi = &bdi1; |
6146 | faea38e7 | bellard | QEMUFile *f; |
6147 | faea38e7 | bellard | int saved_vm_running;
|
6148 | 4c279bdf | bellard | #ifdef _WIN32
|
6149 | 4c279bdf | bellard | struct _timeb tb;
|
6150 | 4c279bdf | bellard | #else
|
6151 | faea38e7 | bellard | struct timeval tv;
|
6152 | 4c279bdf | bellard | #endif
|
6153 | faea38e7 | bellard | |
6154 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6155 | faea38e7 | bellard | if (!bs) {
|
6156 | faea38e7 | bellard | term_printf("No block device can accept snapshots\n");
|
6157 | faea38e7 | bellard | return;
|
6158 | faea38e7 | bellard | } |
6159 | faea38e7 | bellard | |
6160 | 6192bc37 | pbrook | /* ??? Should this occur after vm_stop? */
|
6161 | 6192bc37 | pbrook | qemu_aio_flush(); |
6162 | 6192bc37 | pbrook | |
6163 | faea38e7 | bellard | saved_vm_running = vm_running; |
6164 | faea38e7 | bellard | vm_stop(0);
|
6165 | 3b46e624 | ths | |
6166 | faea38e7 | bellard | must_delete = 0;
|
6167 | faea38e7 | bellard | if (name) {
|
6168 | faea38e7 | bellard | ret = bdrv_snapshot_find(bs, old_sn, name); |
6169 | faea38e7 | bellard | if (ret >= 0) { |
6170 | faea38e7 | bellard | must_delete = 1;
|
6171 | faea38e7 | bellard | } |
6172 | faea38e7 | bellard | } |
6173 | faea38e7 | bellard | memset(sn, 0, sizeof(*sn)); |
6174 | faea38e7 | bellard | if (must_delete) {
|
6175 | faea38e7 | bellard | pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
|
6176 | faea38e7 | bellard | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
|
6177 | faea38e7 | bellard | } else {
|
6178 | faea38e7 | bellard | if (name)
|
6179 | faea38e7 | bellard | pstrcpy(sn->name, sizeof(sn->name), name);
|
6180 | faea38e7 | bellard | } |
6181 | faea38e7 | bellard | |
6182 | faea38e7 | bellard | /* fill auxiliary fields */
|
6183 | 4c279bdf | bellard | #ifdef _WIN32
|
6184 | 4c279bdf | bellard | _ftime(&tb); |
6185 | 4c279bdf | bellard | sn->date_sec = tb.time; |
6186 | 4c279bdf | bellard | sn->date_nsec = tb.millitm * 1000000;
|
6187 | 4c279bdf | bellard | #else
|
6188 | faea38e7 | bellard | gettimeofday(&tv, NULL);
|
6189 | faea38e7 | bellard | sn->date_sec = tv.tv_sec; |
6190 | faea38e7 | bellard | sn->date_nsec = tv.tv_usec * 1000;
|
6191 | 4c279bdf | bellard | #endif
|
6192 | faea38e7 | bellard | sn->vm_clock_nsec = qemu_get_clock(vm_clock); |
6193 | 3b46e624 | ths | |
6194 | faea38e7 | bellard | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
6195 | faea38e7 | bellard | term_printf("Device %s does not support VM state snapshots\n",
|
6196 | faea38e7 | bellard | bdrv_get_device_name(bs)); |
6197 | faea38e7 | bellard | goto the_end;
|
6198 | faea38e7 | bellard | } |
6199 | 3b46e624 | ths | |
6200 | faea38e7 | bellard | /* save the VM state */
|
6201 | faea38e7 | bellard | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
|
6202 | faea38e7 | bellard | if (!f) {
|
6203 | faea38e7 | bellard | term_printf("Could not open VM state file\n");
|
6204 | faea38e7 | bellard | goto the_end;
|
6205 | faea38e7 | bellard | } |
6206 | faea38e7 | bellard | ret = qemu_savevm_state(f); |
6207 | faea38e7 | bellard | sn->vm_state_size = qemu_ftell(f); |
6208 | faea38e7 | bellard | qemu_fclose(f); |
6209 | faea38e7 | bellard | if (ret < 0) { |
6210 | faea38e7 | bellard | term_printf("Error %d while writing VM\n", ret);
|
6211 | faea38e7 | bellard | goto the_end;
|
6212 | faea38e7 | bellard | } |
6213 | 3b46e624 | ths | |
6214 | faea38e7 | bellard | /* create the snapshots */
|
6215 | faea38e7 | bellard | |
6216 | e4bcb14c | ths | for(i = 0; i < nb_drives; i++) { |
6217 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6218 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6219 | faea38e7 | bellard | if (must_delete) {
|
6220 | faea38e7 | bellard | ret = bdrv_snapshot_delete(bs1, old_sn->id_str); |
6221 | faea38e7 | bellard | if (ret < 0) { |
6222 | faea38e7 | bellard | term_printf("Error while deleting snapshot on '%s'\n",
|
6223 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6224 | faea38e7 | bellard | } |
6225 | faea38e7 | bellard | } |
6226 | faea38e7 | bellard | ret = bdrv_snapshot_create(bs1, sn); |
6227 | faea38e7 | bellard | if (ret < 0) { |
6228 | faea38e7 | bellard | term_printf("Error while creating snapshot on '%s'\n",
|
6229 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6230 | faea38e7 | bellard | } |
6231 | faea38e7 | bellard | } |
6232 | faea38e7 | bellard | } |
6233 | faea38e7 | bellard | |
6234 | faea38e7 | bellard | the_end:
|
6235 | 8a7ddc38 | bellard | if (saved_vm_running)
|
6236 | 8a7ddc38 | bellard | vm_start(); |
6237 | faea38e7 | bellard | } |
6238 | faea38e7 | bellard | |
6239 | faea38e7 | bellard | void do_loadvm(const char *name) |
6240 | faea38e7 | bellard | { |
6241 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6242 | faea38e7 | bellard | BlockDriverInfo bdi1, *bdi = &bdi1; |
6243 | faea38e7 | bellard | QEMUFile *f; |
6244 | faea38e7 | bellard | int i, ret;
|
6245 | faea38e7 | bellard | int saved_vm_running;
|
6246 | faea38e7 | bellard | |
6247 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6248 | faea38e7 | bellard | if (!bs) {
|
6249 | faea38e7 | bellard | term_printf("No block device supports snapshots\n");
|
6250 | faea38e7 | bellard | return;
|
6251 | faea38e7 | bellard | } |
6252 | 3b46e624 | ths | |
6253 | 6192bc37 | pbrook | /* Flush all IO requests so they don't interfere with the new state. */
|
6254 | 6192bc37 | pbrook | qemu_aio_flush(); |
6255 | 6192bc37 | pbrook | |
6256 | faea38e7 | bellard | saved_vm_running = vm_running; |
6257 | faea38e7 | bellard | vm_stop(0);
|
6258 | faea38e7 | bellard | |
6259 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6260 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6261 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6262 | faea38e7 | bellard | ret = bdrv_snapshot_goto(bs1, name); |
6263 | faea38e7 | bellard | if (ret < 0) { |
6264 | faea38e7 | bellard | if (bs != bs1)
|
6265 | faea38e7 | bellard | term_printf("Warning: ");
|
6266 | faea38e7 | bellard | switch(ret) {
|
6267 | faea38e7 | bellard | case -ENOTSUP:
|
6268 | faea38e7 | bellard | term_printf("Snapshots not supported on device '%s'\n",
|
6269 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6270 | faea38e7 | bellard | break;
|
6271 | faea38e7 | bellard | case -ENOENT:
|
6272 | faea38e7 | bellard | term_printf("Could not find snapshot '%s' on device '%s'\n",
|
6273 | faea38e7 | bellard | name, bdrv_get_device_name(bs1)); |
6274 | faea38e7 | bellard | break;
|
6275 | faea38e7 | bellard | default:
|
6276 | faea38e7 | bellard | term_printf("Error %d while activating snapshot on '%s'\n",
|
6277 | faea38e7 | bellard | ret, bdrv_get_device_name(bs1)); |
6278 | faea38e7 | bellard | break;
|
6279 | faea38e7 | bellard | } |
6280 | faea38e7 | bellard | /* fatal on snapshot block device */
|
6281 | faea38e7 | bellard | if (bs == bs1)
|
6282 | faea38e7 | bellard | goto the_end;
|
6283 | faea38e7 | bellard | } |
6284 | faea38e7 | bellard | } |
6285 | faea38e7 | bellard | } |
6286 | faea38e7 | bellard | |
6287 | faea38e7 | bellard | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
6288 | faea38e7 | bellard | term_printf("Device %s does not support VM state snapshots\n",
|
6289 | faea38e7 | bellard | bdrv_get_device_name(bs)); |
6290 | faea38e7 | bellard | return;
|
6291 | faea38e7 | bellard | } |
6292 | 3b46e624 | ths | |
6293 | faea38e7 | bellard | /* restore the VM state */
|
6294 | faea38e7 | bellard | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
|
6295 | faea38e7 | bellard | if (!f) {
|
6296 | faea38e7 | bellard | term_printf("Could not open VM state file\n");
|
6297 | faea38e7 | bellard | goto the_end;
|
6298 | faea38e7 | bellard | } |
6299 | faea38e7 | bellard | ret = qemu_loadvm_state(f); |
6300 | faea38e7 | bellard | qemu_fclose(f); |
6301 | faea38e7 | bellard | if (ret < 0) { |
6302 | faea38e7 | bellard | term_printf("Error %d while loading VM state\n", ret);
|
6303 | faea38e7 | bellard | } |
6304 | faea38e7 | bellard | the_end:
|
6305 | faea38e7 | bellard | if (saved_vm_running)
|
6306 | faea38e7 | bellard | vm_start(); |
6307 | faea38e7 | bellard | } |
6308 | faea38e7 | bellard | |
6309 | faea38e7 | bellard | void do_delvm(const char *name) |
6310 | faea38e7 | bellard | { |
6311 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6312 | faea38e7 | bellard | int i, ret;
|
6313 | faea38e7 | bellard | |
6314 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6315 | faea38e7 | bellard | if (!bs) {
|
6316 | faea38e7 | bellard | term_printf("No block device supports snapshots\n");
|
6317 | faea38e7 | bellard | return;
|
6318 | faea38e7 | bellard | } |
6319 | 3b46e624 | ths | |
6320 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6321 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6322 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6323 | faea38e7 | bellard | ret = bdrv_snapshot_delete(bs1, name); |
6324 | faea38e7 | bellard | if (ret < 0) { |
6325 | faea38e7 | bellard | if (ret == -ENOTSUP)
|
6326 | faea38e7 | bellard | term_printf("Snapshots not supported on device '%s'\n",
|
6327 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6328 | faea38e7 | bellard | else
|
6329 | faea38e7 | bellard | term_printf("Error %d while deleting snapshot on '%s'\n",
|
6330 | faea38e7 | bellard | ret, bdrv_get_device_name(bs1)); |
6331 | faea38e7 | bellard | } |
6332 | faea38e7 | bellard | } |
6333 | faea38e7 | bellard | } |
6334 | faea38e7 | bellard | } |
6335 | faea38e7 | bellard | |
6336 | faea38e7 | bellard | void do_info_snapshots(void) |
6337 | faea38e7 | bellard | { |
6338 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6339 | faea38e7 | bellard | QEMUSnapshotInfo *sn_tab, *sn; |
6340 | faea38e7 | bellard | int nb_sns, i;
|
6341 | faea38e7 | bellard | char buf[256]; |
6342 | faea38e7 | bellard | |
6343 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6344 | faea38e7 | bellard | if (!bs) {
|
6345 | faea38e7 | bellard | term_printf("No available block device supports snapshots\n");
|
6346 | faea38e7 | bellard | return;
|
6347 | faea38e7 | bellard | } |
6348 | faea38e7 | bellard | term_printf("Snapshot devices:");
|
6349 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6350 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6351 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6352 | faea38e7 | bellard | if (bs == bs1)
|
6353 | faea38e7 | bellard | term_printf(" %s", bdrv_get_device_name(bs1));
|
6354 | faea38e7 | bellard | } |
6355 | faea38e7 | bellard | } |
6356 | faea38e7 | bellard | term_printf("\n");
|
6357 | faea38e7 | bellard | |
6358 | faea38e7 | bellard | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
6359 | faea38e7 | bellard | if (nb_sns < 0) { |
6360 | faea38e7 | bellard | term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
|
6361 | faea38e7 | bellard | return;
|
6362 | faea38e7 | bellard | } |
6363 | faea38e7 | bellard | term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
|
6364 | faea38e7 | bellard | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
6365 | faea38e7 | bellard | for(i = 0; i < nb_sns; i++) { |
6366 | faea38e7 | bellard | sn = &sn_tab[i]; |
6367 | faea38e7 | bellard | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
6368 | faea38e7 | bellard | } |
6369 | faea38e7 | bellard | qemu_free(sn_tab); |
6370 | 8a7ddc38 | bellard | } |
6371 | 8a7ddc38 | bellard | |
6372 | 8a7ddc38 | bellard | /***********************************************************/
|
6373 | 8a7ddc38 | bellard | /* ram save/restore */
|
6374 | 8a7ddc38 | bellard | |
6375 | 8a7ddc38 | bellard | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
6376 | 8a7ddc38 | bellard | { |
6377 | 8a7ddc38 | bellard | int v;
|
6378 | 8a7ddc38 | bellard | |
6379 | 8a7ddc38 | bellard | v = qemu_get_byte(f); |
6380 | 8a7ddc38 | bellard | switch(v) {
|
6381 | 8a7ddc38 | bellard | case 0: |
6382 | 8a7ddc38 | bellard | if (qemu_get_buffer(f, buf, len) != len)
|
6383 | 8a7ddc38 | bellard | return -EIO;
|
6384 | 8a7ddc38 | bellard | break;
|
6385 | 8a7ddc38 | bellard | case 1: |
6386 | 8a7ddc38 | bellard | v = qemu_get_byte(f); |
6387 | 8a7ddc38 | bellard | memset(buf, v, len); |
6388 | 8a7ddc38 | bellard | break;
|
6389 | 8a7ddc38 | bellard | default:
|
6390 | 8a7ddc38 | bellard | return -EINVAL;
|
6391 | 8a7ddc38 | bellard | } |
6392 | 8a7ddc38 | bellard | return 0; |
6393 | 8a7ddc38 | bellard | } |
6394 | 8a7ddc38 | bellard | |
6395 | c88676f8 | bellard | static int ram_load_v1(QEMUFile *f, void *opaque) |
6396 | c88676f8 | bellard | { |
6397 | 00f82b8a | aurel32 | int ret;
|
6398 | 00f82b8a | aurel32 | ram_addr_t i; |
6399 | c88676f8 | bellard | |
6400 | c88676f8 | bellard | if (qemu_get_be32(f) != phys_ram_size)
|
6401 | c88676f8 | bellard | return -EINVAL;
|
6402 | c88676f8 | bellard | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
6403 | c88676f8 | bellard | ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
6404 | c88676f8 | bellard | if (ret)
|
6405 | c88676f8 | bellard | return ret;
|
6406 | c88676f8 | bellard | } |
6407 | c88676f8 | bellard | return 0; |
6408 | c88676f8 | bellard | } |
6409 | c88676f8 | bellard | |
6410 | c88676f8 | bellard | #define BDRV_HASH_BLOCK_SIZE 1024 |
6411 | c88676f8 | bellard | #define IOBUF_SIZE 4096 |
6412 | c88676f8 | bellard | #define RAM_CBLOCK_MAGIC 0xfabe |
6413 | c88676f8 | bellard | |
6414 | c88676f8 | bellard | typedef struct RamCompressState { |
6415 | c88676f8 | bellard | z_stream zstream; |
6416 | c88676f8 | bellard | QEMUFile *f; |
6417 | c88676f8 | bellard | uint8_t buf[IOBUF_SIZE]; |
6418 | c88676f8 | bellard | } RamCompressState; |
6419 | c88676f8 | bellard | |
6420 | c88676f8 | bellard | static int ram_compress_open(RamCompressState *s, QEMUFile *f) |
6421 | c88676f8 | bellard | { |
6422 | c88676f8 | bellard | int ret;
|
6423 | c88676f8 | bellard | memset(s, 0, sizeof(*s)); |
6424 | c88676f8 | bellard | s->f = f; |
6425 | c88676f8 | bellard | ret = deflateInit2(&s->zstream, 1,
|
6426 | 5fafdf24 | ths | Z_DEFLATED, 15,
|
6427 | c88676f8 | bellard | 9, Z_DEFAULT_STRATEGY);
|
6428 | c88676f8 | bellard | if (ret != Z_OK)
|
6429 | c88676f8 | bellard | return -1; |
6430 | c88676f8 | bellard | s->zstream.avail_out = IOBUF_SIZE; |
6431 | c88676f8 | bellard | s->zstream.next_out = s->buf; |
6432 | c88676f8 | bellard | return 0; |
6433 | c88676f8 | bellard | } |
6434 | c88676f8 | bellard | |
6435 | c88676f8 | bellard | static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len) |
6436 | c88676f8 | bellard | { |
6437 | c88676f8 | bellard | qemu_put_be16(s->f, RAM_CBLOCK_MAGIC); |
6438 | c88676f8 | bellard | qemu_put_be16(s->f, len); |
6439 | c88676f8 | bellard | qemu_put_buffer(s->f, buf, len); |
6440 | c88676f8 | bellard | } |
6441 | c88676f8 | bellard | |
6442 | c88676f8 | bellard | static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len) |
6443 | c88676f8 | bellard | { |
6444 | c88676f8 | bellard | int ret;
|
6445 | c88676f8 | bellard | |
6446 | c88676f8 | bellard | s->zstream.avail_in = len; |
6447 | c88676f8 | bellard | s->zstream.next_in = (uint8_t *)buf; |
6448 | c88676f8 | bellard | while (s->zstream.avail_in > 0) { |
6449 | c88676f8 | bellard | ret = deflate(&s->zstream, Z_NO_FLUSH); |
6450 | c88676f8 | bellard | if (ret != Z_OK)
|
6451 | c88676f8 | bellard | return -1; |
6452 | c88676f8 | bellard | if (s->zstream.avail_out == 0) { |
6453 | c88676f8 | bellard | ram_put_cblock(s, s->buf, IOBUF_SIZE); |
6454 | c88676f8 | bellard | s->zstream.avail_out = IOBUF_SIZE; |
6455 | c88676f8 | bellard | s->zstream.next_out = s->buf; |
6456 | c88676f8 | bellard | } |
6457 | c88676f8 | bellard | } |
6458 | c88676f8 | bellard | return 0; |
6459 | c88676f8 | bellard | } |
6460 | c88676f8 | bellard | |
6461 | c88676f8 | bellard | static void ram_compress_close(RamCompressState *s) |
6462 | c88676f8 | bellard | { |
6463 | c88676f8 | bellard | int len, ret;
|
6464 | c88676f8 | bellard | |
6465 | c88676f8 | bellard | /* compress last bytes */
|
6466 | c88676f8 | bellard | for(;;) {
|
6467 | c88676f8 | bellard | ret = deflate(&s->zstream, Z_FINISH); |
6468 | c88676f8 | bellard | if (ret == Z_OK || ret == Z_STREAM_END) {
|
6469 | c88676f8 | bellard | len = IOBUF_SIZE - s->zstream.avail_out; |
6470 | c88676f8 | bellard | if (len > 0) { |
6471 | c88676f8 | bellard | ram_put_cblock(s, s->buf, len); |
6472 | c88676f8 | bellard | } |
6473 | c88676f8 | bellard | s->zstream.avail_out = IOBUF_SIZE; |
6474 | c88676f8 | bellard | s->zstream.next_out = s->buf; |
6475 | c88676f8 | bellard | if (ret == Z_STREAM_END)
|
6476 | c88676f8 | bellard | break;
|
6477 | c88676f8 | bellard | } else {
|
6478 | c88676f8 | bellard | goto fail;
|
6479 | c88676f8 | bellard | } |
6480 | c88676f8 | bellard | } |
6481 | c88676f8 | bellard | fail:
|
6482 | c88676f8 | bellard | deflateEnd(&s->zstream); |
6483 | c88676f8 | bellard | } |
6484 | c88676f8 | bellard | |
6485 | c88676f8 | bellard | typedef struct RamDecompressState { |
6486 | c88676f8 | bellard | z_stream zstream; |
6487 | c88676f8 | bellard | QEMUFile *f; |
6488 | c88676f8 | bellard | uint8_t buf[IOBUF_SIZE]; |
6489 | c88676f8 | bellard | } RamDecompressState; |
6490 | c88676f8 | bellard | |
6491 | c88676f8 | bellard | static int ram_decompress_open(RamDecompressState *s, QEMUFile *f) |
6492 | c88676f8 | bellard | { |
6493 | c88676f8 | bellard | int ret;
|
6494 | c88676f8 | bellard | memset(s, 0, sizeof(*s)); |
6495 | c88676f8 | bellard | s->f = f; |
6496 | c88676f8 | bellard | ret = inflateInit(&s->zstream); |
6497 | c88676f8 | bellard | if (ret != Z_OK)
|
6498 | c88676f8 | bellard | return -1; |
6499 | c88676f8 | bellard | return 0; |
6500 | c88676f8 | bellard | } |
6501 | c88676f8 | bellard | |
6502 | c88676f8 | bellard | static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len) |
6503 | c88676f8 | bellard | { |
6504 | c88676f8 | bellard | int ret, clen;
|
6505 | c88676f8 | bellard | |
6506 | c88676f8 | bellard | s->zstream.avail_out = len; |
6507 | c88676f8 | bellard | s->zstream.next_out = buf; |
6508 | c88676f8 | bellard | while (s->zstream.avail_out > 0) { |
6509 | c88676f8 | bellard | if (s->zstream.avail_in == 0) { |
6510 | c88676f8 | bellard | if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
|
6511 | c88676f8 | bellard | return -1; |
6512 | c88676f8 | bellard | clen = qemu_get_be16(s->f); |
6513 | c88676f8 | bellard | if (clen > IOBUF_SIZE)
|
6514 | c88676f8 | bellard | return -1; |
6515 | c88676f8 | bellard | qemu_get_buffer(s->f, s->buf, clen); |
6516 | c88676f8 | bellard | s->zstream.avail_in = clen; |
6517 | c88676f8 | bellard | s->zstream.next_in = s->buf; |
6518 | c88676f8 | bellard | } |
6519 | c88676f8 | bellard | ret = inflate(&s->zstream, Z_PARTIAL_FLUSH); |
6520 | c88676f8 | bellard | if (ret != Z_OK && ret != Z_STREAM_END) {
|
6521 | c88676f8 | bellard | return -1; |
6522 | c88676f8 | bellard | } |
6523 | c88676f8 | bellard | } |
6524 | c88676f8 | bellard | return 0; |
6525 | c88676f8 | bellard | } |
6526 | c88676f8 | bellard | |
6527 | c88676f8 | bellard | static void ram_decompress_close(RamDecompressState *s) |
6528 | c88676f8 | bellard | { |
6529 | c88676f8 | bellard | inflateEnd(&s->zstream); |
6530 | c88676f8 | bellard | } |
6531 | c88676f8 | bellard | |
6532 | 8a7ddc38 | bellard | static void ram_save(QEMUFile *f, void *opaque) |
6533 | 8a7ddc38 | bellard | { |
6534 | 00f82b8a | aurel32 | ram_addr_t i; |
6535 | c88676f8 | bellard | RamCompressState s1, *s = &s1; |
6536 | c88676f8 | bellard | uint8_t buf[10];
|
6537 | 3b46e624 | ths | |
6538 | 8a7ddc38 | bellard | qemu_put_be32(f, phys_ram_size); |
6539 | c88676f8 | bellard | if (ram_compress_open(s, f) < 0) |
6540 | c88676f8 | bellard | return;
|
6541 | c88676f8 | bellard | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
6542 | c88676f8 | bellard | #if 0
|
6543 | c88676f8 | bellard | if (tight_savevm_enabled) {
|
6544 | c88676f8 | bellard | int64_t sector_num;
|
6545 | c88676f8 | bellard | int j;
|
6546 | c88676f8 | bellard | |
6547 | c88676f8 | bellard | /* find if the memory block is available on a virtual
|
6548 | c88676f8 | bellard | block device */
|
6549 | c88676f8 | bellard | sector_num = -1;
|
6550 | e4bcb14c | ths | for(j = 0; j < nb_drives; j++) {
|
6551 | e4bcb14c | ths | sector_num = bdrv_hash_find(drives_table[j].bdrv,
|
6552 | e4bcb14c | ths | phys_ram_base + i,
|
6553 | e4bcb14c | ths | BDRV_HASH_BLOCK_SIZE);
|
6554 | e4bcb14c | ths | if (sector_num >= 0)
|
6555 | e4bcb14c | ths | break;
|
6556 | c88676f8 | bellard | }
|
6557 | e4bcb14c | ths | if (j == nb_drives)
|
6558 | c88676f8 | bellard | goto normal_compress;
|
6559 | c88676f8 | bellard | buf[0] = 1;
|
6560 | c88676f8 | bellard | buf[1] = j;
|
6561 | c88676f8 | bellard | cpu_to_be64wu((uint64_t *)(buf + 2), sector_num);
|
6562 | c88676f8 | bellard | ram_compress_buf(s, buf, 10);
|
6563 | 5fafdf24 | ths | } else
|
6564 | c88676f8 | bellard | #endif
|
6565 | c88676f8 | bellard | { |
6566 | c88676f8 | bellard | // normal_compress:
|
6567 | c88676f8 | bellard | buf[0] = 0; |
6568 | c88676f8 | bellard | ram_compress_buf(s, buf, 1);
|
6569 | c88676f8 | bellard | ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); |
6570 | c88676f8 | bellard | } |
6571 | 8a7ddc38 | bellard | } |
6572 | c88676f8 | bellard | ram_compress_close(s); |
6573 | 8a7ddc38 | bellard | } |
6574 | 8a7ddc38 | bellard | |
6575 | 8a7ddc38 | bellard | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
6576 | 8a7ddc38 | bellard | { |
6577 | c88676f8 | bellard | RamDecompressState s1, *s = &s1; |
6578 | c88676f8 | bellard | uint8_t buf[10];
|
6579 | 00f82b8a | aurel32 | ram_addr_t i; |
6580 | 8a7ddc38 | bellard | |
6581 | c88676f8 | bellard | if (version_id == 1) |
6582 | c88676f8 | bellard | return ram_load_v1(f, opaque);
|
6583 | c88676f8 | bellard | if (version_id != 2) |
6584 | 8a7ddc38 | bellard | return -EINVAL;
|
6585 | 8a7ddc38 | bellard | if (qemu_get_be32(f) != phys_ram_size)
|
6586 | 8a7ddc38 | bellard | return -EINVAL;
|
6587 | c88676f8 | bellard | if (ram_decompress_open(s, f) < 0) |
6588 | c88676f8 | bellard | return -EINVAL;
|
6589 | c88676f8 | bellard | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
6590 | c88676f8 | bellard | if (ram_decompress_buf(s, buf, 1) < 0) { |
6591 | c88676f8 | bellard | fprintf(stderr, "Error while reading ram block header\n");
|
6592 | c88676f8 | bellard | goto error;
|
6593 | c88676f8 | bellard | } |
6594 | c88676f8 | bellard | if (buf[0] == 0) { |
6595 | c88676f8 | bellard | if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) { |
6596 | 00f82b8a | aurel32 | fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
|
6597 | c88676f8 | bellard | goto error;
|
6598 | c88676f8 | bellard | } |
6599 | 5fafdf24 | ths | } else
|
6600 | c88676f8 | bellard | #if 0
|
6601 | c88676f8 | bellard | if (buf[0] == 1) {
|
6602 | c88676f8 | bellard | int bs_index;
|
6603 | c88676f8 | bellard | int64_t sector_num;
|
6604 | c88676f8 | bellard | |
6605 | c88676f8 | bellard | ram_decompress_buf(s, buf + 1, 9);
|
6606 | c88676f8 | bellard | bs_index = buf[1];
|
6607 | c88676f8 | bellard | sector_num = be64_to_cpupu((const uint64_t *)(buf + 2));
|
6608 | e4bcb14c | ths | if (bs_index >= nb_drives) {
|
6609 | c88676f8 | bellard | fprintf(stderr, "Invalid block device index %d\n", bs_index);
|
6610 | c88676f8 | bellard | goto error;
|
6611 | c88676f8 | bellard | }
|
6612 | e4bcb14c | ths | if (bdrv_read(drives_table[bs_index].bdrv, sector_num,
|
6613 | e4bcb14c | ths | phys_ram_base + i,
|
6614 | c88676f8 | bellard | BDRV_HASH_BLOCK_SIZE / 512) < 0) {
|
6615 | 5fafdf24 | ths | fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n",
|
6616 | c88676f8 | bellard | bs_index, sector_num);
|
6617 | c88676f8 | bellard | goto error;
|
6618 | c88676f8 | bellard | }
|
6619 | 5fafdf24 | ths | } else
|
6620 | c88676f8 | bellard | #endif
|
6621 | c88676f8 | bellard | { |
6622 | c88676f8 | bellard | error:
|
6623 | c88676f8 | bellard | printf("Error block header\n");
|
6624 | c88676f8 | bellard | return -EINVAL;
|
6625 | c88676f8 | bellard | } |
6626 | 8a7ddc38 | bellard | } |
6627 | c88676f8 | bellard | ram_decompress_close(s); |
6628 | 8a7ddc38 | bellard | return 0; |
6629 | 8a7ddc38 | bellard | } |
6630 | 8a7ddc38 | bellard | |
6631 | 8a7ddc38 | bellard | /***********************************************************/
|
6632 | 83f64091 | bellard | /* bottom halves (can be seen as timers which expire ASAP) */
|
6633 | 83f64091 | bellard | |
6634 | 83f64091 | bellard | struct QEMUBH {
|
6635 | 83f64091 | bellard | QEMUBHFunc *cb; |
6636 | 83f64091 | bellard | void *opaque;
|
6637 | 83f64091 | bellard | int scheduled;
|
6638 | 83f64091 | bellard | QEMUBH *next; |
6639 | 83f64091 | bellard | }; |
6640 | 83f64091 | bellard | |
6641 | 83f64091 | bellard | static QEMUBH *first_bh = NULL; |
6642 | 83f64091 | bellard | |
6643 | 83f64091 | bellard | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
|
6644 | 83f64091 | bellard | { |
6645 | 83f64091 | bellard | QEMUBH *bh; |
6646 | 83f64091 | bellard | bh = qemu_mallocz(sizeof(QEMUBH));
|
6647 | 83f64091 | bellard | if (!bh)
|
6648 | 83f64091 | bellard | return NULL; |
6649 | 83f64091 | bellard | bh->cb = cb; |
6650 | 83f64091 | bellard | bh->opaque = opaque; |
6651 | 83f64091 | bellard | return bh;
|
6652 | 83f64091 | bellard | } |
6653 | 83f64091 | bellard | |
6654 | 6eb5733a | bellard | int qemu_bh_poll(void) |
6655 | 83f64091 | bellard | { |
6656 | 83f64091 | bellard | QEMUBH *bh, **pbh; |
6657 | 6eb5733a | bellard | int ret;
|
6658 | 83f64091 | bellard | |
6659 | 6eb5733a | bellard | ret = 0;
|
6660 | 83f64091 | bellard | for(;;) {
|
6661 | 83f64091 | bellard | pbh = &first_bh; |
6662 | 83f64091 | bellard | bh = *pbh; |
6663 | 83f64091 | bellard | if (!bh)
|
6664 | 83f64091 | bellard | break;
|
6665 | 6eb5733a | bellard | ret = 1;
|
6666 | 83f64091 | bellard | *pbh = bh->next; |
6667 | 83f64091 | bellard | bh->scheduled = 0;
|
6668 | 83f64091 | bellard | bh->cb(bh->opaque); |
6669 | 83f64091 | bellard | } |
6670 | 6eb5733a | bellard | return ret;
|
6671 | 83f64091 | bellard | } |
6672 | 83f64091 | bellard | |
6673 | 83f64091 | bellard | void qemu_bh_schedule(QEMUBH *bh)
|
6674 | 83f64091 | bellard | { |
6675 | 83f64091 | bellard | CPUState *env = cpu_single_env; |
6676 | 83f64091 | bellard | if (bh->scheduled)
|
6677 | 83f64091 | bellard | return;
|
6678 | 83f64091 | bellard | bh->scheduled = 1;
|
6679 | 83f64091 | bellard | bh->next = first_bh; |
6680 | 83f64091 | bellard | first_bh = bh; |
6681 | 83f64091 | bellard | |
6682 | 83f64091 | bellard | /* stop the currently executing CPU to execute the BH ASAP */
|
6683 | 83f64091 | bellard | if (env) {
|
6684 | 83f64091 | bellard | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
6685 | 83f64091 | bellard | } |
6686 | 83f64091 | bellard | } |
6687 | 83f64091 | bellard | |
6688 | 83f64091 | bellard | void qemu_bh_cancel(QEMUBH *bh)
|
6689 | 83f64091 | bellard | { |
6690 | 83f64091 | bellard | QEMUBH **pbh; |
6691 | 83f64091 | bellard | if (bh->scheduled) {
|
6692 | 83f64091 | bellard | pbh = &first_bh; |
6693 | 83f64091 | bellard | while (*pbh != bh)
|
6694 | 83f64091 | bellard | pbh = &(*pbh)->next; |
6695 | 83f64091 | bellard | *pbh = bh->next; |
6696 | 83f64091 | bellard | bh->scheduled = 0;
|
6697 | 83f64091 | bellard | } |
6698 | 83f64091 | bellard | } |
6699 | 83f64091 | bellard | |
6700 | 83f64091 | bellard | void qemu_bh_delete(QEMUBH *bh)
|
6701 | 83f64091 | bellard | { |
6702 | 83f64091 | bellard | qemu_bh_cancel(bh); |
6703 | 83f64091 | bellard | qemu_free(bh); |
6704 | 83f64091 | bellard | } |
6705 | 83f64091 | bellard | |
6706 | 83f64091 | bellard | /***********************************************************/
|
6707 | cc1daa40 | bellard | /* machine registration */
|
6708 | cc1daa40 | bellard | |
6709 | cc1daa40 | bellard | QEMUMachine *first_machine = NULL;
|
6710 | cc1daa40 | bellard | |
6711 | cc1daa40 | bellard | int qemu_register_machine(QEMUMachine *m)
|
6712 | cc1daa40 | bellard | { |
6713 | cc1daa40 | bellard | QEMUMachine **pm; |
6714 | cc1daa40 | bellard | pm = &first_machine; |
6715 | cc1daa40 | bellard | while (*pm != NULL) |
6716 | cc1daa40 | bellard | pm = &(*pm)->next; |
6717 | cc1daa40 | bellard | m->next = NULL;
|
6718 | cc1daa40 | bellard | *pm = m; |
6719 | cc1daa40 | bellard | return 0; |
6720 | cc1daa40 | bellard | } |
6721 | cc1daa40 | bellard | |
6722 | 9596ebb7 | pbrook | static QEMUMachine *find_machine(const char *name) |
6723 | cc1daa40 | bellard | { |
6724 | cc1daa40 | bellard | QEMUMachine *m; |
6725 | cc1daa40 | bellard | |
6726 | cc1daa40 | bellard | for(m = first_machine; m != NULL; m = m->next) { |
6727 | cc1daa40 | bellard | if (!strcmp(m->name, name))
|
6728 | cc1daa40 | bellard | return m;
|
6729 | cc1daa40 | bellard | } |
6730 | cc1daa40 | bellard | return NULL; |
6731 | cc1daa40 | bellard | } |
6732 | cc1daa40 | bellard | |
6733 | cc1daa40 | bellard | /***********************************************************/
|
6734 | 8a7ddc38 | bellard | /* main execution loop */
|
6735 | 8a7ddc38 | bellard | |
6736 | 9596ebb7 | pbrook | static void gui_update(void *opaque) |
6737 | 8a7ddc38 | bellard | { |
6738 | 740733bb | ths | DisplayState *ds = opaque; |
6739 | 740733bb | ths | ds->dpy_refresh(ds); |
6740 | f442e08b | aurel32 | qemu_mod_timer(ds->gui_timer, |
6741 | f442e08b | aurel32 | (ds->gui_timer_interval ? |
6742 | f442e08b | aurel32 | ds->gui_timer_interval : |
6743 | f442e08b | aurel32 | GUI_REFRESH_INTERVAL) |
6744 | f442e08b | aurel32 | + qemu_get_clock(rt_clock)); |
6745 | 8a7ddc38 | bellard | } |
6746 | 8a7ddc38 | bellard | |
6747 | 0bd48850 | bellard | struct vm_change_state_entry {
|
6748 | 0bd48850 | bellard | VMChangeStateHandler *cb; |
6749 | 0bd48850 | bellard | void *opaque;
|
6750 | 0bd48850 | bellard | LIST_ENTRY (vm_change_state_entry) entries; |
6751 | 0bd48850 | bellard | }; |
6752 | 0bd48850 | bellard | |
6753 | 0bd48850 | bellard | static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
|
6754 | 0bd48850 | bellard | |
6755 | 0bd48850 | bellard | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
6756 | 0bd48850 | bellard | void *opaque)
|
6757 | 0bd48850 | bellard | { |
6758 | 0bd48850 | bellard | VMChangeStateEntry *e; |
6759 | 0bd48850 | bellard | |
6760 | 0bd48850 | bellard | e = qemu_mallocz(sizeof (*e));
|
6761 | 0bd48850 | bellard | if (!e)
|
6762 | 0bd48850 | bellard | return NULL; |
6763 | 0bd48850 | bellard | |
6764 | 0bd48850 | bellard | e->cb = cb; |
6765 | 0bd48850 | bellard | e->opaque = opaque; |
6766 | 0bd48850 | bellard | LIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
6767 | 0bd48850 | bellard | return e;
|
6768 | 0bd48850 | bellard | } |
6769 | 0bd48850 | bellard | |
6770 | 0bd48850 | bellard | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
|
6771 | 0bd48850 | bellard | { |
6772 | 0bd48850 | bellard | LIST_REMOVE (e, entries); |
6773 | 0bd48850 | bellard | qemu_free (e); |
6774 | 0bd48850 | bellard | } |
6775 | 0bd48850 | bellard | |
6776 | 0bd48850 | bellard | static void vm_state_notify(int running) |
6777 | 0bd48850 | bellard | { |
6778 | 0bd48850 | bellard | VMChangeStateEntry *e; |
6779 | 0bd48850 | bellard | |
6780 | 0bd48850 | bellard | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
|
6781 | 0bd48850 | bellard | e->cb(e->opaque, running); |
6782 | 0bd48850 | bellard | } |
6783 | 0bd48850 | bellard | } |
6784 | 0bd48850 | bellard | |
6785 | 8a7ddc38 | bellard | /* XXX: support several handlers */
|
6786 | 0bd48850 | bellard | static VMStopHandler *vm_stop_cb;
|
6787 | 0bd48850 | bellard | static void *vm_stop_opaque; |
6788 | 8a7ddc38 | bellard | |
6789 | 8a7ddc38 | bellard | int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) |
6790 | 8a7ddc38 | bellard | { |
6791 | 8a7ddc38 | bellard | vm_stop_cb = cb; |
6792 | 8a7ddc38 | bellard | vm_stop_opaque = opaque; |
6793 | 8a7ddc38 | bellard | return 0; |
6794 | 8a7ddc38 | bellard | } |
6795 | 8a7ddc38 | bellard | |
6796 | 8a7ddc38 | bellard | void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) |
6797 | 8a7ddc38 | bellard | { |
6798 | 8a7ddc38 | bellard | vm_stop_cb = NULL;
|
6799 | 8a7ddc38 | bellard | } |
6800 | 8a7ddc38 | bellard | |
6801 | 8a7ddc38 | bellard | void vm_start(void) |
6802 | 8a7ddc38 | bellard | { |
6803 | 8a7ddc38 | bellard | if (!vm_running) {
|
6804 | 8a7ddc38 | bellard | cpu_enable_ticks(); |
6805 | 8a7ddc38 | bellard | vm_running = 1;
|
6806 | 0bd48850 | bellard | vm_state_notify(1);
|
6807 | efe75411 | ths | qemu_rearm_alarm_timer(alarm_timer); |
6808 | 8a7ddc38 | bellard | } |
6809 | 8a7ddc38 | bellard | } |
6810 | 8a7ddc38 | bellard | |
6811 | 5fafdf24 | ths | void vm_stop(int reason) |
6812 | 8a7ddc38 | bellard | { |
6813 | 8a7ddc38 | bellard | if (vm_running) {
|
6814 | 8a7ddc38 | bellard | cpu_disable_ticks(); |
6815 | 8a7ddc38 | bellard | vm_running = 0;
|
6816 | 8a7ddc38 | bellard | if (reason != 0) { |
6817 | 8a7ddc38 | bellard | if (vm_stop_cb) {
|
6818 | 8a7ddc38 | bellard | vm_stop_cb(vm_stop_opaque, reason); |
6819 | 8a7ddc38 | bellard | } |
6820 | 34865134 | bellard | } |
6821 | 0bd48850 | bellard | vm_state_notify(0);
|
6822 | 8a7ddc38 | bellard | } |
6823 | 8a7ddc38 | bellard | } |
6824 | 8a7ddc38 | bellard | |
6825 | bb0c6722 | bellard | /* reset/shutdown handler */
|
6826 | bb0c6722 | bellard | |
6827 | bb0c6722 | bellard | typedef struct QEMUResetEntry { |
6828 | bb0c6722 | bellard | QEMUResetHandler *func; |
6829 | bb0c6722 | bellard | void *opaque;
|
6830 | bb0c6722 | bellard | struct QEMUResetEntry *next;
|
6831 | bb0c6722 | bellard | } QEMUResetEntry; |
6832 | bb0c6722 | bellard | |
6833 | bb0c6722 | bellard | static QEMUResetEntry *first_reset_entry;
|
6834 | bb0c6722 | bellard | static int reset_requested; |
6835 | bb0c6722 | bellard | static int shutdown_requested; |
6836 | 3475187d | bellard | static int powerdown_requested; |
6837 | bb0c6722 | bellard | |
6838 | cf7a2fe2 | aurel32 | int qemu_shutdown_requested(void) |
6839 | cf7a2fe2 | aurel32 | { |
6840 | cf7a2fe2 | aurel32 | int r = shutdown_requested;
|
6841 | cf7a2fe2 | aurel32 | shutdown_requested = 0;
|
6842 | cf7a2fe2 | aurel32 | return r;
|
6843 | cf7a2fe2 | aurel32 | } |
6844 | cf7a2fe2 | aurel32 | |
6845 | cf7a2fe2 | aurel32 | int qemu_reset_requested(void) |
6846 | cf7a2fe2 | aurel32 | { |
6847 | cf7a2fe2 | aurel32 | int r = reset_requested;
|
6848 | cf7a2fe2 | aurel32 | reset_requested = 0;
|
6849 | cf7a2fe2 | aurel32 | return r;
|
6850 | cf7a2fe2 | aurel32 | } |
6851 | cf7a2fe2 | aurel32 | |
6852 | cf7a2fe2 | aurel32 | int qemu_powerdown_requested(void) |
6853 | cf7a2fe2 | aurel32 | { |
6854 | cf7a2fe2 | aurel32 | int r = powerdown_requested;
|
6855 | cf7a2fe2 | aurel32 | powerdown_requested = 0;
|
6856 | cf7a2fe2 | aurel32 | return r;
|
6857 | cf7a2fe2 | aurel32 | } |
6858 | cf7a2fe2 | aurel32 | |
6859 | bb0c6722 | bellard | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
6860 | bb0c6722 | bellard | { |
6861 | bb0c6722 | bellard | QEMUResetEntry **pre, *re; |
6862 | bb0c6722 | bellard | |
6863 | bb0c6722 | bellard | pre = &first_reset_entry; |
6864 | bb0c6722 | bellard | while (*pre != NULL) |
6865 | bb0c6722 | bellard | pre = &(*pre)->next; |
6866 | bb0c6722 | bellard | re = qemu_mallocz(sizeof(QEMUResetEntry));
|
6867 | bb0c6722 | bellard | re->func = func; |
6868 | bb0c6722 | bellard | re->opaque = opaque; |
6869 | bb0c6722 | bellard | re->next = NULL;
|
6870 | bb0c6722 | bellard | *pre = re; |
6871 | bb0c6722 | bellard | } |
6872 | bb0c6722 | bellard | |
6873 | cf7a2fe2 | aurel32 | void qemu_system_reset(void) |
6874 | bb0c6722 | bellard | { |
6875 | bb0c6722 | bellard | QEMUResetEntry *re; |
6876 | bb0c6722 | bellard | |
6877 | bb0c6722 | bellard | /* reset all devices */
|
6878 | bb0c6722 | bellard | for(re = first_reset_entry; re != NULL; re = re->next) { |
6879 | bb0c6722 | bellard | re->func(re->opaque); |
6880 | bb0c6722 | bellard | } |
6881 | bb0c6722 | bellard | } |
6882 | bb0c6722 | bellard | |
6883 | bb0c6722 | bellard | void qemu_system_reset_request(void) |
6884 | bb0c6722 | bellard | { |
6885 | d1beab82 | bellard | if (no_reboot) {
|
6886 | d1beab82 | bellard | shutdown_requested = 1;
|
6887 | d1beab82 | bellard | } else {
|
6888 | d1beab82 | bellard | reset_requested = 1;
|
6889 | d1beab82 | bellard | } |
6890 | 6a00d601 | bellard | if (cpu_single_env)
|
6891 | 6a00d601 | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
6892 | bb0c6722 | bellard | } |
6893 | bb0c6722 | bellard | |
6894 | bb0c6722 | bellard | void qemu_system_shutdown_request(void) |
6895 | bb0c6722 | bellard | { |
6896 | bb0c6722 | bellard | shutdown_requested = 1;
|
6897 | 6a00d601 | bellard | if (cpu_single_env)
|
6898 | 6a00d601 | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
6899 | bb0c6722 | bellard | } |
6900 | bb0c6722 | bellard | |
6901 | 3475187d | bellard | void qemu_system_powerdown_request(void) |
6902 | 3475187d | bellard | { |
6903 | 3475187d | bellard | powerdown_requested = 1;
|
6904 | 6a00d601 | bellard | if (cpu_single_env)
|
6905 | 6a00d601 | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
6906 | bb0c6722 | bellard | } |
6907 | bb0c6722 | bellard | |
6908 | 0ecdffbb | aurel32 | /* boot_set handler */
|
6909 | 0ecdffbb | aurel32 | QEMUBootSetHandler *qemu_boot_set_handler = NULL;
|
6910 | 0ecdffbb | aurel32 | |
6911 | 0ecdffbb | aurel32 | void qemu_register_boot_set(QEMUBootSetHandler *func)
|
6912 | 0ecdffbb | aurel32 | { |
6913 | 0ecdffbb | aurel32 | qemu_boot_set_handler = func; |
6914 | 0ecdffbb | aurel32 | } |
6915 | 0ecdffbb | aurel32 | |
6916 | 5905b2e5 | bellard | void main_loop_wait(int timeout) |
6917 | 8a7ddc38 | bellard | { |
6918 | cafffd40 | ths | IOHandlerRecord *ioh; |
6919 | e035649e | bellard | fd_set rfds, wfds, xfds; |
6920 | 877cf882 | ths | int ret, nfds;
|
6921 | 877cf882 | ths | #ifdef _WIN32
|
6922 | 877cf882 | ths | int ret2, i;
|
6923 | 877cf882 | ths | #endif
|
6924 | fd1dff4b | bellard | struct timeval tv;
|
6925 | f331110f | bellard | PollingEntry *pe; |
6926 | f331110f | bellard | |
6927 | c4b1fcc0 | bellard | |
6928 | f331110f | bellard | /* XXX: need to suppress polling by better using win32 events */
|
6929 | f331110f | bellard | ret = 0;
|
6930 | f331110f | bellard | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
6931 | f331110f | bellard | ret |= pe->func(pe->opaque); |
6932 | f331110f | bellard | } |
6933 | 38e205a2 | bellard | #ifdef _WIN32
|
6934 | e6b1e558 | ths | if (ret == 0) { |
6935 | a18e524a | bellard | int err;
|
6936 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
6937 | 3b46e624 | ths | |
6938 | a18e524a | bellard | ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout); |
6939 | a18e524a | bellard | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
6940 | a18e524a | bellard | if (w->func[ret - WAIT_OBJECT_0])
|
6941 | a18e524a | bellard | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
6942 | 3b46e624 | ths | |
6943 | 5fafdf24 | ths | /* Check for additional signaled events */
|
6944 | e6b1e558 | ths | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
6945 | 3b46e624 | ths | |
6946 | e6b1e558 | ths | /* Check if event is signaled */
|
6947 | e6b1e558 | ths | ret2 = WaitForSingleObject(w->events[i], 0);
|
6948 | e6b1e558 | ths | if(ret2 == WAIT_OBJECT_0) {
|
6949 | e6b1e558 | ths | if (w->func[i])
|
6950 | e6b1e558 | ths | w->func[i](w->opaque[i]); |
6951 | e6b1e558 | ths | } else if (ret2 == WAIT_TIMEOUT) { |
6952 | e6b1e558 | ths | } else {
|
6953 | e6b1e558 | ths | err = GetLastError(); |
6954 | e6b1e558 | ths | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
|
6955 | 3b46e624 | ths | } |
6956 | 3b46e624 | ths | } |
6957 | a18e524a | bellard | } else if (ret == WAIT_TIMEOUT) { |
6958 | a18e524a | bellard | } else {
|
6959 | a18e524a | bellard | err = GetLastError(); |
6960 | e6b1e558 | ths | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
|
6961 | a18e524a | bellard | } |
6962 | f331110f | bellard | } |
6963 | fd1dff4b | bellard | #endif
|
6964 | fd1dff4b | bellard | /* poll any events */
|
6965 | fd1dff4b | bellard | /* XXX: separate device handlers from system ones */
|
6966 | fd1dff4b | bellard | nfds = -1;
|
6967 | fd1dff4b | bellard | FD_ZERO(&rfds); |
6968 | fd1dff4b | bellard | FD_ZERO(&wfds); |
6969 | e035649e | bellard | FD_ZERO(&xfds); |
6970 | fd1dff4b | bellard | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
6971 | cafffd40 | ths | if (ioh->deleted)
|
6972 | cafffd40 | ths | continue;
|
6973 | fd1dff4b | bellard | if (ioh->fd_read &&
|
6974 | fd1dff4b | bellard | (!ioh->fd_read_poll || |
6975 | fd1dff4b | bellard | ioh->fd_read_poll(ioh->opaque) != 0)) {
|
6976 | fd1dff4b | bellard | FD_SET(ioh->fd, &rfds); |
6977 | fd1dff4b | bellard | if (ioh->fd > nfds)
|
6978 | fd1dff4b | bellard | nfds = ioh->fd; |
6979 | fd1dff4b | bellard | } |
6980 | fd1dff4b | bellard | if (ioh->fd_write) {
|
6981 | fd1dff4b | bellard | FD_SET(ioh->fd, &wfds); |
6982 | fd1dff4b | bellard | if (ioh->fd > nfds)
|
6983 | fd1dff4b | bellard | nfds = ioh->fd; |
6984 | fd1dff4b | bellard | } |
6985 | fd1dff4b | bellard | } |
6986 | 3b46e624 | ths | |
6987 | fd1dff4b | bellard | tv.tv_sec = 0;
|
6988 | fd1dff4b | bellard | #ifdef _WIN32
|
6989 | fd1dff4b | bellard | tv.tv_usec = 0;
|
6990 | 38e205a2 | bellard | #else
|
6991 | fd1dff4b | bellard | tv.tv_usec = timeout * 1000;
|
6992 | fd1dff4b | bellard | #endif
|
6993 | e035649e | bellard | #if defined(CONFIG_SLIRP)
|
6994 | e035649e | bellard | if (slirp_inited) {
|
6995 | e035649e | bellard | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
6996 | e035649e | bellard | } |
6997 | e035649e | bellard | #endif
|
6998 | e035649e | bellard | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
|
6999 | fd1dff4b | bellard | if (ret > 0) { |
7000 | cafffd40 | ths | IOHandlerRecord **pioh; |
7001 | cafffd40 | ths | |
7002 | cafffd40 | ths | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
7003 | 6ab43fdc | ths | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
|
7004 | fd1dff4b | bellard | ioh->fd_read(ioh->opaque); |
7005 | 7c9d8e07 | bellard | } |
7006 | 6ab43fdc | ths | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
|
7007 | fd1dff4b | bellard | ioh->fd_write(ioh->opaque); |
7008 | c4b1fcc0 | bellard | } |
7009 | b4608c04 | bellard | } |
7010 | cafffd40 | ths | |
7011 | cafffd40 | ths | /* remove deleted IO handlers */
|
7012 | cafffd40 | ths | pioh = &first_io_handler; |
7013 | cafffd40 | ths | while (*pioh) {
|
7014 | cafffd40 | ths | ioh = *pioh; |
7015 | cafffd40 | ths | if (ioh->deleted) {
|
7016 | cafffd40 | ths | *pioh = ioh->next; |
7017 | cafffd40 | ths | qemu_free(ioh); |
7018 | 5fafdf24 | ths | } else
|
7019 | cafffd40 | ths | pioh = &ioh->next; |
7020 | cafffd40 | ths | } |
7021 | fd1dff4b | bellard | } |
7022 | c20709aa | bellard | #if defined(CONFIG_SLIRP)
|
7023 | fd1dff4b | bellard | if (slirp_inited) {
|
7024 | e035649e | bellard | if (ret < 0) { |
7025 | e035649e | bellard | FD_ZERO(&rfds); |
7026 | e035649e | bellard | FD_ZERO(&wfds); |
7027 | e035649e | bellard | FD_ZERO(&xfds); |
7028 | c20709aa | bellard | } |
7029 | e035649e | bellard | slirp_select_poll(&rfds, &wfds, &xfds); |
7030 | fd1dff4b | bellard | } |
7031 | c20709aa | bellard | #endif
|
7032 | 83f64091 | bellard | qemu_aio_poll(); |
7033 | c20709aa | bellard | |
7034 | fd1dff4b | bellard | if (vm_running) {
|
7035 | 21b20814 | edgar_igl | if (likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
|
7036 | 5fafdf24 | ths | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
7037 | fd1dff4b | bellard | qemu_get_clock(vm_clock)); |
7038 | fd1dff4b | bellard | /* run dma transfers, if any */
|
7039 | fd1dff4b | bellard | DMA_run(); |
7040 | fd1dff4b | bellard | } |
7041 | 423f0742 | pbrook | |
7042 | fd1dff4b | bellard | /* real time timers */
|
7043 | 5fafdf24 | ths | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
7044 | fd1dff4b | bellard | qemu_get_clock(rt_clock)); |
7045 | 423f0742 | pbrook | |
7046 | d5d08334 | balrog | if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
|
7047 | d5d08334 | balrog | alarm_timer->flags &= ~(ALARM_FLAG_EXPIRED); |
7048 | d5d08334 | balrog | qemu_rearm_alarm_timer(alarm_timer); |
7049 | d5d08334 | balrog | } |
7050 | b99dc0d1 | balrog | |
7051 | 423f0742 | pbrook | /* Check bottom-halves last in case any of the earlier events triggered
|
7052 | 423f0742 | pbrook | them. */
|
7053 | 423f0742 | pbrook | qemu_bh_poll(); |
7054 | 3b46e624 | ths | |
7055 | 5905b2e5 | bellard | } |
7056 | 5905b2e5 | bellard | |
7057 | 9596ebb7 | pbrook | static int main_loop(void) |
7058 | 5905b2e5 | bellard | { |
7059 | 5905b2e5 | bellard | int ret, timeout;
|
7060 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7061 | 89bfc105 | bellard | int64_t ti; |
7062 | 89bfc105 | bellard | #endif
|
7063 | 6a00d601 | bellard | CPUState *env; |
7064 | 5905b2e5 | bellard | |
7065 | 6a00d601 | bellard | cur_cpu = first_cpu; |
7066 | ee5605e5 | balrog | next_cpu = cur_cpu->next_cpu ?: first_cpu; |
7067 | 5905b2e5 | bellard | for(;;) {
|
7068 | 5905b2e5 | bellard | if (vm_running) {
|
7069 | 15a76449 | bellard | |
7070 | 15a76449 | bellard | for(;;) {
|
7071 | 15a76449 | bellard | /* get next cpu */
|
7072 | ee5605e5 | balrog | env = next_cpu; |
7073 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7074 | 89bfc105 | bellard | ti = profile_getclock(); |
7075 | 89bfc105 | bellard | #endif
|
7076 | 6a00d601 | bellard | ret = cpu_exec(env); |
7077 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7078 | 89bfc105 | bellard | qemu_time += profile_getclock() - ti; |
7079 | 89bfc105 | bellard | #endif
|
7080 | ee5605e5 | balrog | next_cpu = env->next_cpu ?: first_cpu; |
7081 | 95b01009 | aurel32 | if (event_pending && likely(ret != EXCP_DEBUG)) {
|
7082 | ee5605e5 | balrog | ret = EXCP_INTERRUPT; |
7083 | ee5605e5 | balrog | event_pending = 0;
|
7084 | ee5605e5 | balrog | break;
|
7085 | ee5605e5 | balrog | } |
7086 | bd967e05 | pbrook | if (ret == EXCP_HLT) {
|
7087 | bd967e05 | pbrook | /* Give the next CPU a chance to run. */
|
7088 | bd967e05 | pbrook | cur_cpu = env; |
7089 | bd967e05 | pbrook | continue;
|
7090 | bd967e05 | pbrook | } |
7091 | 15a76449 | bellard | if (ret != EXCP_HALTED)
|
7092 | 15a76449 | bellard | break;
|
7093 | 15a76449 | bellard | /* all CPUs are halted ? */
|
7094 | bd967e05 | pbrook | if (env == cur_cpu)
|
7095 | 15a76449 | bellard | break;
|
7096 | 15a76449 | bellard | } |
7097 | 15a76449 | bellard | cur_cpu = env; |
7098 | 15a76449 | bellard | |
7099 | 5905b2e5 | bellard | if (shutdown_requested) {
|
7100 | 3475187d | bellard | ret = EXCP_INTERRUPT; |
7101 | b2f76161 | aurel32 | if (no_shutdown) {
|
7102 | b2f76161 | aurel32 | vm_stop(0);
|
7103 | b2f76161 | aurel32 | no_shutdown = 0;
|
7104 | b2f76161 | aurel32 | } |
7105 | b2f76161 | aurel32 | else
|
7106 | b2f76161 | aurel32 | break;
|
7107 | 5905b2e5 | bellard | } |
7108 | 5905b2e5 | bellard | if (reset_requested) {
|
7109 | 5905b2e5 | bellard | reset_requested = 0;
|
7110 | 5905b2e5 | bellard | qemu_system_reset(); |
7111 | 3475187d | bellard | ret = EXCP_INTERRUPT; |
7112 | 3475187d | bellard | } |
7113 | 3475187d | bellard | if (powerdown_requested) {
|
7114 | 3475187d | bellard | powerdown_requested = 0;
|
7115 | 3475187d | bellard | qemu_system_powerdown(); |
7116 | 3475187d | bellard | ret = EXCP_INTERRUPT; |
7117 | 5905b2e5 | bellard | } |
7118 | 95b01009 | aurel32 | if (unlikely(ret == EXCP_DEBUG)) {
|
7119 | 5905b2e5 | bellard | vm_stop(EXCP_DEBUG); |
7120 | 5905b2e5 | bellard | } |
7121 | bd967e05 | pbrook | /* If all cpus are halted then wait until the next IRQ */
|
7122 | 5905b2e5 | bellard | /* XXX: use timeout computed from timers */
|
7123 | bd967e05 | pbrook | if (ret == EXCP_HALTED)
|
7124 | 5905b2e5 | bellard | timeout = 10;
|
7125 | 5905b2e5 | bellard | else
|
7126 | 5905b2e5 | bellard | timeout = 0;
|
7127 | 5905b2e5 | bellard | } else {
|
7128 | 5905b2e5 | bellard | timeout = 10;
|
7129 | 5905b2e5 | bellard | } |
7130 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7131 | 89bfc105 | bellard | ti = profile_getclock(); |
7132 | 89bfc105 | bellard | #endif
|
7133 | 5905b2e5 | bellard | main_loop_wait(timeout); |
7134 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7135 | 89bfc105 | bellard | dev_time += profile_getclock() - ti; |
7136 | 89bfc105 | bellard | #endif
|
7137 | b4608c04 | bellard | } |
7138 | 34865134 | bellard | cpu_disable_ticks(); |
7139 | 34865134 | bellard | return ret;
|
7140 | b4608c04 | bellard | } |
7141 | b4608c04 | bellard | |
7142 | 15f82208 | ths | static void help(int exitcode) |
7143 | 0824d6fc | bellard | { |
7144 | 68d0f70e | bellard | printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n" |
7145 | 0db63474 | bellard | "usage: %s [options] [disk_image]\n"
|
7146 | 0824d6fc | bellard | "\n"
|
7147 | a20dd508 | bellard | "'disk_image' is a raw hard image image for IDE hard disk 0\n"
|
7148 | fc01f7e7 | bellard | "\n"
|
7149 | a20dd508 | bellard | "Standard options:\n"
|
7150 | cc1daa40 | bellard | "-M machine select emulated machine (-M ? for list)\n"
|
7151 | 5adb4839 | pbrook | "-cpu cpu select CPU (-cpu ? for list)\n"
|
7152 | c45886db | bellard | "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
|
7153 | 36b486bb | bellard | "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
|
7154 | 36b486bb | bellard | "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
|
7155 | c4b1fcc0 | bellard | "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
|
7156 | a1620fac | aurel32 | "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
|
7157 | a1620fac | aurel32 | " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
|
7158 | a1620fac | aurel32 | " [,cache=on|off][,format=f]\n"
|
7159 | e4bcb14c | ths | " use 'file' as a drive image\n"
|
7160 | 3e3d5815 | balrog | "-mtdblock file use 'file' as on-board Flash memory image\n"
|
7161 | a1bb27b1 | pbrook | "-sd file use 'file' as SecureDigital card image\n"
|
7162 | 86f55663 | j_mayer | "-pflash file use 'file' as a parallel flash image\n"
|
7163 | eec85c2a | ths | "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
|
7164 | 667accab | ths | "-snapshot write to temporary files instead of disk image files\n"
|
7165 | 667accab | ths | #ifdef CONFIG_SDL
|
7166 | 43523e93 | ths | "-no-frame open SDL window without a frame and window decorations\n"
|
7167 | 3780e197 | ths | "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
|
7168 | 667accab | ths | "-no-quit disable SDL window close capability\n"
|
7169 | 667accab | ths | #endif
|
7170 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7171 | 52ca8d6a | bellard | "-no-fd-bootchk disable boot signature checking for floppy disks\n"
|
7172 | 52ca8d6a | bellard | #endif
|
7173 | a00bad7e | bellard | "-m megs set virtual RAM size to megs MB [default=%d]\n"
|
7174 | 91fc2119 | bellard | "-smp n set the number of CPUs to 'n' [default=1]\n"
|
7175 | c4b1fcc0 | bellard | "-nographic disable graphical output and redirect serial I/Os to console\n"
|
7176 | a171fe39 | balrog | "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
|
7177 | 4ca0074c | bellard | #ifndef _WIN32
|
7178 | 667accab | ths | "-k language use keyboard layout (for example \"fr\" for French)\n"
|
7179 | 4ca0074c | bellard | #endif
|
7180 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7181 | 1d14ffa9 | bellard | "-audio-help print list of audio drivers and their options\n"
|
7182 | c0fe3827 | bellard | "-soundhw c1,... enable audio support\n"
|
7183 | c0fe3827 | bellard | " and only specified sound cards (comma separated list)\n"
|
7184 | c0fe3827 | bellard | " use -soundhw ? to get the list of supported cards\n"
|
7185 | 6a36d84e | bellard | " use -soundhw all to enable all of them\n"
|
7186 | 1d14ffa9 | bellard | #endif
|
7187 | 89980284 | bellard | "-localtime set the real time clock to local time [default=utc]\n"
|
7188 | d63d307f | bellard | "-full-screen start in full screen\n"
|
7189 | a09db21f | bellard | #ifdef TARGET_I386
|
7190 | a09db21f | bellard | "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
|
7191 | a09db21f | bellard | #endif
|
7192 | b389dbfb | bellard | "-usb enable the USB driver (will be the default soon)\n"
|
7193 | b389dbfb | bellard | "-usbdevice name add the host or guest USB device 'name'\n"
|
7194 | 6f7e9aec | bellard | #if defined(TARGET_PPC) || defined(TARGET_SPARC)
|
7195 | 6f7e9aec | bellard | "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
|
7196 | bb0c6722 | bellard | #endif
|
7197 | c35734b2 | ths | "-name string set the name of the guest\n"
|
7198 | c4b1fcc0 | bellard | "\n"
|
7199 | c4b1fcc0 | bellard | "Network options:\n"
|
7200 | a41b2ff2 | pbrook | "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
|
7201 | 7c9d8e07 | bellard | " create a new Network Interface Card and connect it to VLAN 'n'\n"
|
7202 | c20709aa | bellard | #ifdef CONFIG_SLIRP
|
7203 | 115defd1 | pbrook | "-net user[,vlan=n][,hostname=host]\n"
|
7204 | 115defd1 | pbrook | " connect the user mode network stack to VLAN 'n' and send\n"
|
7205 | 115defd1 | pbrook | " hostname 'host' to DHCP clients\n"
|
7206 | 7c9d8e07 | bellard | #endif
|
7207 | 7fb843f8 | bellard | #ifdef _WIN32
|
7208 | 7fb843f8 | bellard | "-net tap[,vlan=n],ifname=name\n"
|
7209 | 7fb843f8 | bellard | " connect the host TAP network interface to VLAN 'n'\n"
|
7210 | 7fb843f8 | bellard | #else
|
7211 | b46a8906 | ths | "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
|
7212 | b46a8906 | ths | " connect the host TAP network interface to VLAN 'n' and use the\n"
|
7213 | b46a8906 | ths | " network scripts 'file' (default=%s)\n"
|
7214 | b46a8906 | ths | " and 'dfile' (default=%s);\n"
|
7215 | b46a8906 | ths | " use '[down]script=no' to disable script execution;\n"
|
7216 | 7c9d8e07 | bellard | " use 'fd=h' to connect to an already opened TAP interface\n"
|
7217 | 7fb843f8 | bellard | #endif
|
7218 | 6a00d601 | bellard | "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
|
7219 | 7c9d8e07 | bellard | " connect the vlan 'n' to another VLAN using a socket connection\n"
|
7220 | 3d830459 | bellard | "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
|
7221 | 3d830459 | bellard | " connect the vlan 'n' to multicast maddr and port\n"
|
7222 | 7c9d8e07 | bellard | "-net none use it alone to have zero network devices; if no -net option\n"
|
7223 | 7c9d8e07 | bellard | " is provided, the default is '-net nic -net user'\n"
|
7224 | 7c9d8e07 | bellard | "\n"
|
7225 | 7c9d8e07 | bellard | #ifdef CONFIG_SLIRP
|
7226 | 0db1137d | ths | "-tftp dir allow tftp access to files in dir [-net user]\n"
|
7227 | 47d5d01a | ths | "-bootp file advertise file in BOOTP replies\n"
|
7228 | 7c9d8e07 | bellard | #ifndef _WIN32
|
7229 | 7c9d8e07 | bellard | "-smb dir allow SMB access to files in 'dir' [-net user]\n"
|
7230 | c94c8d64 | bellard | #endif
|
7231 | 9bf05444 | bellard | "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
|
7232 | 7c9d8e07 | bellard | " redirect TCP or UDP connections from host to guest [-net user]\n"
|
7233 | c20709aa | bellard | #endif
|
7234 | a20dd508 | bellard | "\n"
|
7235 | c4b1fcc0 | bellard | "Linux boot specific:\n"
|
7236 | a20dd508 | bellard | "-kernel bzImage use 'bzImage' as kernel image\n"
|
7237 | a20dd508 | bellard | "-append cmdline use 'cmdline' as kernel command line\n"
|
7238 | a20dd508 | bellard | "-initrd file use 'file' as initial ram disk\n"
|
7239 | fc01f7e7 | bellard | "\n"
|
7240 | 330d0414 | bellard | "Debug/Expert options:\n"
|
7241 | 82c643ff | bellard | "-monitor dev redirect the monitor to char device 'dev'\n"
|
7242 | 82c643ff | bellard | "-serial dev redirect the serial port to char device 'dev'\n"
|
7243 | 6508fe59 | bellard | "-parallel dev redirect the parallel port to char device 'dev'\n"
|
7244 | f7cce898 | bellard | "-pidfile file Write PID to 'file'\n"
|
7245 | cd6f1169 | bellard | "-S freeze CPU at startup (use 'c' to start execution)\n"
|
7246 | cfc3475a | pbrook | "-s wait gdb connection to port\n"
|
7247 | cfc3475a | pbrook | "-p port set gdb connection port [default=%s]\n"
|
7248 | f193c797 | bellard | "-d item1,... output log to %s (use -d ? for a list of log items)\n"
|
7249 | 46d4767d | bellard | "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
|
7250 | 46d4767d | bellard | " translation (t=none or lba) (usually qemu can guess them)\n"
|
7251 | 87b47350 | bellard | "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
|
7252 | d993e026 | bellard | #ifdef USE_KQEMU
|
7253 | 6515b203 | bellard | "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
|
7254 | d993e026 | bellard | "-no-kqemu disable KQEMU kernel module usage\n"
|
7255 | d993e026 | bellard | #endif
|
7256 | bb0c6722 | bellard | #ifdef TARGET_I386
|
7257 | 1bfe856e | bellard | "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
|
7258 | 1bfe856e | bellard | " (default is CL-GD5446 PCI VGA)\n"
|
7259 | 6515b203 | bellard | "-no-acpi disable ACPI\n"
|
7260 | bb0c6722 | bellard | #endif
|
7261 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
7262 | 4d3b6f6e | balrog | "-curses use a curses/ncurses interface instead of SDL\n"
|
7263 | 4d3b6f6e | balrog | #endif
|
7264 | d1beab82 | bellard | "-no-reboot exit instead of rebooting\n"
|
7265 | b2f76161 | aurel32 | "-no-shutdown stop before shutdown\n"
|
7266 | a8080006 | aurel32 | "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
|
7267 | 24236869 | bellard | "-vnc display start a VNC server on display\n"
|
7268 | 71e3ceb8 | ths | #ifndef _WIN32
|
7269 | 71e3ceb8 | ths | "-daemonize daemonize QEMU after initializing\n"
|
7270 | 71e3ceb8 | ths | #endif
|
7271 | 9ae02555 | ths | "-option-rom rom load a file, rom, into the option ROM space\n"
|
7272 | 66508601 | blueswir1 | #ifdef TARGET_SPARC
|
7273 | 66508601 | blueswir1 | "-prom-env variable=value set OpenBIOS nvram variables\n"
|
7274 | 66508601 | blueswir1 | #endif
|
7275 | f3dcfada | ths | "-clock force the use of the given methods for timer alarm.\n"
|
7276 | 3adda04c | aurel32 | " To see what timers are available use -clock ?\n"
|
7277 | bce61846 | bellard | "-startdate select initial date of the clock\n"
|
7278 | 0824d6fc | bellard | "\n"
|
7279 | 82c643ff | bellard | "During emulation, the following keys are useful:\n"
|
7280 | 032a8c9e | bellard | "ctrl-alt-f toggle full screen\n"
|
7281 | 032a8c9e | bellard | "ctrl-alt-n switch to virtual console 'n'\n"
|
7282 | 032a8c9e | bellard | "ctrl-alt toggle mouse and keyboard grab\n"
|
7283 | 82c643ff | bellard | "\n"
|
7284 | 82c643ff | bellard | "When using -nographic, press 'ctrl-a h' to get some help.\n"
|
7285 | 82c643ff | bellard | , |
7286 | 0db63474 | bellard | "qemu",
|
7287 | a00bad7e | bellard | DEFAULT_RAM_SIZE, |
7288 | 7c9d8e07 | bellard | #ifndef _WIN32
|
7289 | a00bad7e | bellard | DEFAULT_NETWORK_SCRIPT, |
7290 | b46a8906 | ths | DEFAULT_NETWORK_DOWN_SCRIPT, |
7291 | 7c9d8e07 | bellard | #endif
|
7292 | 6e44ba7f | bellard | DEFAULT_GDBSTUB_PORT, |
7293 | bce61846 | bellard | "/tmp/qemu.log");
|
7294 | 15f82208 | ths | exit(exitcode); |
7295 | 0824d6fc | bellard | } |
7296 | 0824d6fc | bellard | |
7297 | cd6f1169 | bellard | #define HAS_ARG 0x0001 |
7298 | cd6f1169 | bellard | |
7299 | cd6f1169 | bellard | enum {
|
7300 | cd6f1169 | bellard | QEMU_OPTION_h, |
7301 | cd6f1169 | bellard | |
7302 | cc1daa40 | bellard | QEMU_OPTION_M, |
7303 | 94fc95cd | j_mayer | QEMU_OPTION_cpu, |
7304 | cd6f1169 | bellard | QEMU_OPTION_fda, |
7305 | cd6f1169 | bellard | QEMU_OPTION_fdb, |
7306 | cd6f1169 | bellard | QEMU_OPTION_hda, |
7307 | cd6f1169 | bellard | QEMU_OPTION_hdb, |
7308 | cd6f1169 | bellard | QEMU_OPTION_hdc, |
7309 | cd6f1169 | bellard | QEMU_OPTION_hdd, |
7310 | e4bcb14c | ths | QEMU_OPTION_drive, |
7311 | cd6f1169 | bellard | QEMU_OPTION_cdrom, |
7312 | 3e3d5815 | balrog | QEMU_OPTION_mtdblock, |
7313 | a1bb27b1 | pbrook | QEMU_OPTION_sd, |
7314 | 86f55663 | j_mayer | QEMU_OPTION_pflash, |
7315 | cd6f1169 | bellard | QEMU_OPTION_boot, |
7316 | cd6f1169 | bellard | QEMU_OPTION_snapshot, |
7317 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7318 | 52ca8d6a | bellard | QEMU_OPTION_no_fd_bootchk, |
7319 | 52ca8d6a | bellard | #endif
|
7320 | cd6f1169 | bellard | QEMU_OPTION_m, |
7321 | cd6f1169 | bellard | QEMU_OPTION_nographic, |
7322 | a171fe39 | balrog | QEMU_OPTION_portrait, |
7323 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7324 | 1d14ffa9 | bellard | QEMU_OPTION_audio_help, |
7325 | 1d14ffa9 | bellard | QEMU_OPTION_soundhw, |
7326 | 1d14ffa9 | bellard | #endif
|
7327 | cd6f1169 | bellard | |
7328 | 7c9d8e07 | bellard | QEMU_OPTION_net, |
7329 | c7f74643 | bellard | QEMU_OPTION_tftp, |
7330 | 47d5d01a | ths | QEMU_OPTION_bootp, |
7331 | 9d728e8c | bellard | QEMU_OPTION_smb, |
7332 | 9bf05444 | bellard | QEMU_OPTION_redir, |
7333 | cd6f1169 | bellard | |
7334 | cd6f1169 | bellard | QEMU_OPTION_kernel, |
7335 | cd6f1169 | bellard | QEMU_OPTION_append, |
7336 | cd6f1169 | bellard | QEMU_OPTION_initrd, |
7337 | cd6f1169 | bellard | |
7338 | cd6f1169 | bellard | QEMU_OPTION_S, |
7339 | cd6f1169 | bellard | QEMU_OPTION_s, |
7340 | cd6f1169 | bellard | QEMU_OPTION_p, |
7341 | cd6f1169 | bellard | QEMU_OPTION_d, |
7342 | cd6f1169 | bellard | QEMU_OPTION_hdachs, |
7343 | cd6f1169 | bellard | QEMU_OPTION_L, |
7344 | 1192dad8 | j_mayer | QEMU_OPTION_bios, |
7345 | 3d11d0eb | bellard | QEMU_OPTION_k, |
7346 | ee22c2f7 | bellard | QEMU_OPTION_localtime, |
7347 | 1f04275e | bellard | QEMU_OPTION_cirrusvga, |
7348 | d34cab9f | ths | QEMU_OPTION_vmsvga, |
7349 | e9b137c2 | bellard | QEMU_OPTION_g, |
7350 | 1bfe856e | bellard | QEMU_OPTION_std_vga, |
7351 | 20d8a3ed | ths | QEMU_OPTION_echr, |
7352 | 82c643ff | bellard | QEMU_OPTION_monitor, |
7353 | 82c643ff | bellard | QEMU_OPTION_serial, |
7354 | 6508fe59 | bellard | QEMU_OPTION_parallel, |
7355 | d63d307f | bellard | QEMU_OPTION_loadvm, |
7356 | d63d307f | bellard | QEMU_OPTION_full_screen, |
7357 | 43523e93 | ths | QEMU_OPTION_no_frame, |
7358 | 3780e197 | ths | QEMU_OPTION_alt_grab, |
7359 | 667accab | ths | QEMU_OPTION_no_quit, |
7360 | f7cce898 | bellard | QEMU_OPTION_pidfile, |
7361 | d993e026 | bellard | QEMU_OPTION_no_kqemu, |
7362 | 89bfc105 | bellard | QEMU_OPTION_kernel_kqemu, |
7363 | a09db21f | bellard | QEMU_OPTION_win2k_hack, |
7364 | bb36d470 | bellard | QEMU_OPTION_usb, |
7365 | a594cfbf | bellard | QEMU_OPTION_usbdevice, |
7366 | 6a00d601 | bellard | QEMU_OPTION_smp, |
7367 | 24236869 | bellard | QEMU_OPTION_vnc, |
7368 | 6515b203 | bellard | QEMU_OPTION_no_acpi, |
7369 | 4d3b6f6e | balrog | QEMU_OPTION_curses, |
7370 | d1beab82 | bellard | QEMU_OPTION_no_reboot, |
7371 | b2f76161 | aurel32 | QEMU_OPTION_no_shutdown, |
7372 | 9467cd46 | balrog | QEMU_OPTION_show_cursor, |
7373 | 71e3ceb8 | ths | QEMU_OPTION_daemonize, |
7374 | 9ae02555 | ths | QEMU_OPTION_option_rom, |
7375 | c35734b2 | ths | QEMU_OPTION_semihosting, |
7376 | c35734b2 | ths | QEMU_OPTION_name, |
7377 | 66508601 | blueswir1 | QEMU_OPTION_prom_env, |
7378 | 2b8f2d41 | balrog | QEMU_OPTION_old_param, |
7379 | f3dcfada | ths | QEMU_OPTION_clock, |
7380 | 7e0af5d0 | bellard | QEMU_OPTION_startdate, |
7381 | cd6f1169 | bellard | }; |
7382 | cd6f1169 | bellard | |
7383 | cd6f1169 | bellard | typedef struct QEMUOption { |
7384 | cd6f1169 | bellard | const char *name; |
7385 | cd6f1169 | bellard | int flags;
|
7386 | cd6f1169 | bellard | int index;
|
7387 | cd6f1169 | bellard | } QEMUOption; |
7388 | cd6f1169 | bellard | |
7389 | cd6f1169 | bellard | const QEMUOption qemu_options[] = {
|
7390 | cd6f1169 | bellard | { "h", 0, QEMU_OPTION_h }, |
7391 | 64423fb2 | pbrook | { "help", 0, QEMU_OPTION_h }, |
7392 | cd6f1169 | bellard | |
7393 | cc1daa40 | bellard | { "M", HAS_ARG, QEMU_OPTION_M },
|
7394 | 94fc95cd | j_mayer | { "cpu", HAS_ARG, QEMU_OPTION_cpu },
|
7395 | cd6f1169 | bellard | { "fda", HAS_ARG, QEMU_OPTION_fda },
|
7396 | cd6f1169 | bellard | { "fdb", HAS_ARG, QEMU_OPTION_fdb },
|
7397 | cd6f1169 | bellard | { "hda", HAS_ARG, QEMU_OPTION_hda },
|
7398 | cd6f1169 | bellard | { "hdb", HAS_ARG, QEMU_OPTION_hdb },
|
7399 | cd6f1169 | bellard | { "hdc", HAS_ARG, QEMU_OPTION_hdc },
|
7400 | cd6f1169 | bellard | { "hdd", HAS_ARG, QEMU_OPTION_hdd },
|
7401 | e4bcb14c | ths | { "drive", HAS_ARG, QEMU_OPTION_drive },
|
7402 | cd6f1169 | bellard | { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
|
7403 | 3e3d5815 | balrog | { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
|
7404 | a1bb27b1 | pbrook | { "sd", HAS_ARG, QEMU_OPTION_sd },
|
7405 | 86f55663 | j_mayer | { "pflash", HAS_ARG, QEMU_OPTION_pflash },
|
7406 | cd6f1169 | bellard | { "boot", HAS_ARG, QEMU_OPTION_boot },
|
7407 | cd6f1169 | bellard | { "snapshot", 0, QEMU_OPTION_snapshot }, |
7408 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7409 | 52ca8d6a | bellard | { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk }, |
7410 | 52ca8d6a | bellard | #endif
|
7411 | cd6f1169 | bellard | { "m", HAS_ARG, QEMU_OPTION_m },
|
7412 | cd6f1169 | bellard | { "nographic", 0, QEMU_OPTION_nographic }, |
7413 | a171fe39 | balrog | { "portrait", 0, QEMU_OPTION_portrait }, |
7414 | 3d11d0eb | bellard | { "k", HAS_ARG, QEMU_OPTION_k },
|
7415 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7416 | 1d14ffa9 | bellard | { "audio-help", 0, QEMU_OPTION_audio_help }, |
7417 | 1d14ffa9 | bellard | { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
|
7418 | 1d14ffa9 | bellard | #endif
|
7419 | cd6f1169 | bellard | |
7420 | 7c9d8e07 | bellard | { "net", HAS_ARG, QEMU_OPTION_net},
|
7421 | 158156d1 | bellard | #ifdef CONFIG_SLIRP
|
7422 | c7f74643 | bellard | { "tftp", HAS_ARG, QEMU_OPTION_tftp },
|
7423 | 47d5d01a | ths | { "bootp", HAS_ARG, QEMU_OPTION_bootp },
|
7424 | c94c8d64 | bellard | #ifndef _WIN32
|
7425 | 9d728e8c | bellard | { "smb", HAS_ARG, QEMU_OPTION_smb },
|
7426 | c94c8d64 | bellard | #endif
|
7427 | 9bf05444 | bellard | { "redir", HAS_ARG, QEMU_OPTION_redir },
|
7428 | 158156d1 | bellard | #endif
|
7429 | cd6f1169 | bellard | |
7430 | cd6f1169 | bellard | { "kernel", HAS_ARG, QEMU_OPTION_kernel },
|
7431 | cd6f1169 | bellard | { "append", HAS_ARG, QEMU_OPTION_append },
|
7432 | cd6f1169 | bellard | { "initrd", HAS_ARG, QEMU_OPTION_initrd },
|
7433 | cd6f1169 | bellard | |
7434 | cd6f1169 | bellard | { "S", 0, QEMU_OPTION_S }, |
7435 | cd6f1169 | bellard | { "s", 0, QEMU_OPTION_s }, |
7436 | cd6f1169 | bellard | { "p", HAS_ARG, QEMU_OPTION_p },
|
7437 | cd6f1169 | bellard | { "d", HAS_ARG, QEMU_OPTION_d },
|
7438 | cd6f1169 | bellard | { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
|
7439 | cd6f1169 | bellard | { "L", HAS_ARG, QEMU_OPTION_L },
|
7440 | 1192dad8 | j_mayer | { "bios", HAS_ARG, QEMU_OPTION_bios },
|
7441 | d993e026 | bellard | #ifdef USE_KQEMU
|
7442 | d993e026 | bellard | { "no-kqemu", 0, QEMU_OPTION_no_kqemu }, |
7443 | 89bfc105 | bellard | { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu }, |
7444 | d993e026 | bellard | #endif
|
7445 | 6f7e9aec | bellard | #if defined(TARGET_PPC) || defined(TARGET_SPARC)
|
7446 | e9b137c2 | bellard | { "g", 1, QEMU_OPTION_g }, |
7447 | 77d4bc34 | bellard | #endif
|
7448 | ee22c2f7 | bellard | { "localtime", 0, QEMU_OPTION_localtime }, |
7449 | 1bfe856e | bellard | { "std-vga", 0, QEMU_OPTION_std_vga }, |
7450 | 8b6e0729 | balrog | { "echr", HAS_ARG, QEMU_OPTION_echr },
|
7451 | 8b6e0729 | balrog | { "monitor", HAS_ARG, QEMU_OPTION_monitor },
|
7452 | 8b6e0729 | balrog | { "serial", HAS_ARG, QEMU_OPTION_serial },
|
7453 | 8b6e0729 | balrog | { "parallel", HAS_ARG, QEMU_OPTION_parallel },
|
7454 | d63d307f | bellard | { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
|
7455 | d63d307f | bellard | { "full-screen", 0, QEMU_OPTION_full_screen }, |
7456 | 667accab | ths | #ifdef CONFIG_SDL
|
7457 | 43523e93 | ths | { "no-frame", 0, QEMU_OPTION_no_frame }, |
7458 | 3780e197 | ths | { "alt-grab", 0, QEMU_OPTION_alt_grab }, |
7459 | 667accab | ths | { "no-quit", 0, QEMU_OPTION_no_quit }, |
7460 | 667accab | ths | #endif
|
7461 | f7cce898 | bellard | { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
|
7462 | a09db21f | bellard | { "win2k-hack", 0, QEMU_OPTION_win2k_hack }, |
7463 | a594cfbf | bellard | { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
|
7464 | 6a00d601 | bellard | { "smp", HAS_ARG, QEMU_OPTION_smp },
|
7465 | 24236869 | bellard | { "vnc", HAS_ARG, QEMU_OPTION_vnc },
|
7466 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
7467 | 4d3b6f6e | balrog | { "curses", 0, QEMU_OPTION_curses }, |
7468 | 4d3b6f6e | balrog | #endif
|
7469 | 96d30e48 | ths | |
7470 | 1f04275e | bellard | /* temporary options */
|
7471 | a594cfbf | bellard | { "usb", 0, QEMU_OPTION_usb }, |
7472 | 1f04275e | bellard | { "cirrusvga", 0, QEMU_OPTION_cirrusvga }, |
7473 | d34cab9f | ths | { "vmwarevga", 0, QEMU_OPTION_vmsvga }, |
7474 | 6515b203 | bellard | { "no-acpi", 0, QEMU_OPTION_no_acpi }, |
7475 | d1beab82 | bellard | { "no-reboot", 0, QEMU_OPTION_no_reboot }, |
7476 | b2f76161 | aurel32 | { "no-shutdown", 0, QEMU_OPTION_no_shutdown }, |
7477 | 9467cd46 | balrog | { "show-cursor", 0, QEMU_OPTION_show_cursor }, |
7478 | 71e3ceb8 | ths | { "daemonize", 0, QEMU_OPTION_daemonize }, |
7479 | 9ae02555 | ths | { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
|
7480 | a87295e8 | pbrook | #if defined(TARGET_ARM) || defined(TARGET_M68K)
|
7481 | 8e71621f | pbrook | { "semihosting", 0, QEMU_OPTION_semihosting }, |
7482 | 8e71621f | pbrook | #endif
|
7483 | c35734b2 | ths | { "name", HAS_ARG, QEMU_OPTION_name },
|
7484 | 66508601 | blueswir1 | #if defined(TARGET_SPARC)
|
7485 | 66508601 | blueswir1 | { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
|
7486 | 66508601 | blueswir1 | #endif
|
7487 | 2b8f2d41 | balrog | #if defined(TARGET_ARM)
|
7488 | 2b8f2d41 | balrog | { "old-param", 0, QEMU_OPTION_old_param }, |
7489 | 2b8f2d41 | balrog | #endif
|
7490 | f3dcfada | ths | { "clock", HAS_ARG, QEMU_OPTION_clock },
|
7491 | 7e0af5d0 | bellard | { "startdate", HAS_ARG, QEMU_OPTION_startdate },
|
7492 | cd6f1169 | bellard | { NULL },
|
7493 | fc01f7e7 | bellard | }; |
7494 | fc01f7e7 | bellard | |
7495 | 5905b2e5 | bellard | /* password input */
|
7496 | 5905b2e5 | bellard | |
7497 | 2bac6019 | balrog | int qemu_key_check(BlockDriverState *bs, const char *name) |
7498 | 2bac6019 | balrog | { |
7499 | 2bac6019 | balrog | char password[256]; |
7500 | 2bac6019 | balrog | int i;
|
7501 | 2bac6019 | balrog | |
7502 | 2bac6019 | balrog | if (!bdrv_is_encrypted(bs))
|
7503 | 2bac6019 | balrog | return 0; |
7504 | 2bac6019 | balrog | |
7505 | 2bac6019 | balrog | term_printf("%s is encrypted.\n", name);
|
7506 | 2bac6019 | balrog | for(i = 0; i < 3; i++) { |
7507 | 2bac6019 | balrog | monitor_readline("Password: ", 1, password, sizeof(password)); |
7508 | 2bac6019 | balrog | if (bdrv_set_key(bs, password) == 0) |
7509 | 2bac6019 | balrog | return 0; |
7510 | 2bac6019 | balrog | term_printf("invalid password\n");
|
7511 | 2bac6019 | balrog | } |
7512 | 2bac6019 | balrog | return -EPERM;
|
7513 | 2bac6019 | balrog | } |
7514 | 2bac6019 | balrog | |
7515 | 5905b2e5 | bellard | static BlockDriverState *get_bdrv(int index) |
7516 | 5905b2e5 | bellard | { |
7517 | e4bcb14c | ths | if (index > nb_drives)
|
7518 | e4bcb14c | ths | return NULL; |
7519 | e4bcb14c | ths | return drives_table[index].bdrv;
|
7520 | 5905b2e5 | bellard | } |
7521 | 5905b2e5 | bellard | |
7522 | 5905b2e5 | bellard | static void read_passwords(void) |
7523 | 5905b2e5 | bellard | { |
7524 | 5905b2e5 | bellard | BlockDriverState *bs; |
7525 | 2bac6019 | balrog | int i;
|
7526 | 5905b2e5 | bellard | |
7527 | 5905b2e5 | bellard | for(i = 0; i < 6; i++) { |
7528 | 5905b2e5 | bellard | bs = get_bdrv(i); |
7529 | 2bac6019 | balrog | if (bs)
|
7530 | 2bac6019 | balrog | qemu_key_check(bs, bdrv_get_device_name(bs)); |
7531 | 5905b2e5 | bellard | } |
7532 | 5905b2e5 | bellard | } |
7533 | 5905b2e5 | bellard | |
7534 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7535 | 6a36d84e | bellard | struct soundhw soundhw[] = {
|
7536 | b00052e4 | balrog | #ifdef HAS_AUDIO_CHOICE
|
7537 | 4ce7ff6e | aurel32 | #if defined(TARGET_I386) || defined(TARGET_MIPS)
|
7538 | fd06c375 | bellard | { |
7539 | fd06c375 | bellard | "pcspk",
|
7540 | fd06c375 | bellard | "PC speaker",
|
7541 | fd06c375 | bellard | 0,
|
7542 | fd06c375 | bellard | 1,
|
7543 | fd06c375 | bellard | { .init_isa = pcspk_audio_init } |
7544 | fd06c375 | bellard | }, |
7545 | fd06c375 | bellard | #endif
|
7546 | 6a36d84e | bellard | { |
7547 | 6a36d84e | bellard | "sb16",
|
7548 | 6a36d84e | bellard | "Creative Sound Blaster 16",
|
7549 | 6a36d84e | bellard | 0,
|
7550 | 6a36d84e | bellard | 1,
|
7551 | 6a36d84e | bellard | { .init_isa = SB16_init } |
7552 | 6a36d84e | bellard | }, |
7553 | 6a36d84e | bellard | |
7554 | 1d14ffa9 | bellard | #ifdef CONFIG_ADLIB
|
7555 | 6a36d84e | bellard | { |
7556 | 6a36d84e | bellard | "adlib",
|
7557 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
7558 | 6a36d84e | bellard | "Yamaha YMF262 (OPL3)",
|
7559 | 1d14ffa9 | bellard | #else
|
7560 | 6a36d84e | bellard | "Yamaha YM3812 (OPL2)",
|
7561 | 1d14ffa9 | bellard | #endif
|
7562 | 6a36d84e | bellard | 0,
|
7563 | 6a36d84e | bellard | 1,
|
7564 | 6a36d84e | bellard | { .init_isa = Adlib_init } |
7565 | 6a36d84e | bellard | }, |
7566 | 1d14ffa9 | bellard | #endif
|
7567 | 6a36d84e | bellard | |
7568 | 1d14ffa9 | bellard | #ifdef CONFIG_GUS
|
7569 | 6a36d84e | bellard | { |
7570 | 6a36d84e | bellard | "gus",
|
7571 | 6a36d84e | bellard | "Gravis Ultrasound GF1",
|
7572 | 6a36d84e | bellard | 0,
|
7573 | 6a36d84e | bellard | 1,
|
7574 | 6a36d84e | bellard | { .init_isa = GUS_init } |
7575 | 6a36d84e | bellard | }, |
7576 | 1d14ffa9 | bellard | #endif
|
7577 | 6a36d84e | bellard | |
7578 | e5c9a13e | balrog | #ifdef CONFIG_AC97
|
7579 | e5c9a13e | balrog | { |
7580 | e5c9a13e | balrog | "ac97",
|
7581 | e5c9a13e | balrog | "Intel 82801AA AC97 Audio",
|
7582 | e5c9a13e | balrog | 0,
|
7583 | e5c9a13e | balrog | 0,
|
7584 | e5c9a13e | balrog | { .init_pci = ac97_init } |
7585 | e5c9a13e | balrog | }, |
7586 | e5c9a13e | balrog | #endif
|
7587 | e5c9a13e | balrog | |
7588 | 6a36d84e | bellard | { |
7589 | 6a36d84e | bellard | "es1370",
|
7590 | 6a36d84e | bellard | "ENSONIQ AudioPCI ES1370",
|
7591 | 6a36d84e | bellard | 0,
|
7592 | 6a36d84e | bellard | 0,
|
7593 | 6a36d84e | bellard | { .init_pci = es1370_init } |
7594 | 6a36d84e | bellard | }, |
7595 | b00052e4 | balrog | #endif
|
7596 | 6a36d84e | bellard | |
7597 | 6a36d84e | bellard | { NULL, NULL, 0, 0, { NULL } } |
7598 | 6a36d84e | bellard | }; |
7599 | 6a36d84e | bellard | |
7600 | 6a36d84e | bellard | static void select_soundhw (const char *optarg) |
7601 | 6a36d84e | bellard | { |
7602 | 6a36d84e | bellard | struct soundhw *c;
|
7603 | 6a36d84e | bellard | |
7604 | 6a36d84e | bellard | if (*optarg == '?') { |
7605 | 6a36d84e | bellard | show_valid_cards:
|
7606 | 6a36d84e | bellard | |
7607 | 6a36d84e | bellard | printf ("Valid sound card names (comma separated):\n");
|
7608 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
7609 | 6a36d84e | bellard | printf ("%-11s %s\n", c->name, c->descr);
|
7610 | 6a36d84e | bellard | } |
7611 | 6a36d84e | bellard | printf ("\n-soundhw all will enable all of the above\n");
|
7612 | 1d14ffa9 | bellard | exit (*optarg != '?');
|
7613 | 1d14ffa9 | bellard | } |
7614 | 1d14ffa9 | bellard | else {
|
7615 | 6a36d84e | bellard | size_t l; |
7616 | 1d14ffa9 | bellard | const char *p; |
7617 | 1d14ffa9 | bellard | char *e;
|
7618 | 1d14ffa9 | bellard | int bad_card = 0; |
7619 | 1d14ffa9 | bellard | |
7620 | 6a36d84e | bellard | if (!strcmp (optarg, "all")) { |
7621 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
7622 | 6a36d84e | bellard | c->enabled = 1;
|
7623 | 6a36d84e | bellard | } |
7624 | 6a36d84e | bellard | return;
|
7625 | 6a36d84e | bellard | } |
7626 | 1d14ffa9 | bellard | |
7627 | 6a36d84e | bellard | p = optarg; |
7628 | 1d14ffa9 | bellard | while (*p) {
|
7629 | 1d14ffa9 | bellard | e = strchr (p, ',');
|
7630 | 1d14ffa9 | bellard | l = !e ? strlen (p) : (size_t) (e - p); |
7631 | 6a36d84e | bellard | |
7632 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
7633 | 6a36d84e | bellard | if (!strncmp (c->name, p, l)) {
|
7634 | 6a36d84e | bellard | c->enabled = 1;
|
7635 | 1d14ffa9 | bellard | break;
|
7636 | 1d14ffa9 | bellard | } |
7637 | 1d14ffa9 | bellard | } |
7638 | 6a36d84e | bellard | |
7639 | 6a36d84e | bellard | if (!c->name) {
|
7640 | 1d14ffa9 | bellard | if (l > 80) { |
7641 | 1d14ffa9 | bellard | fprintf (stderr, |
7642 | 1d14ffa9 | bellard | "Unknown sound card name (too big to show)\n");
|
7643 | 1d14ffa9 | bellard | } |
7644 | 1d14ffa9 | bellard | else {
|
7645 | 1d14ffa9 | bellard | fprintf (stderr, "Unknown sound card name `%.*s'\n",
|
7646 | 1d14ffa9 | bellard | (int) l, p);
|
7647 | 1d14ffa9 | bellard | } |
7648 | 1d14ffa9 | bellard | bad_card = 1;
|
7649 | 1d14ffa9 | bellard | } |
7650 | 1d14ffa9 | bellard | p += l + (e != NULL);
|
7651 | 1d14ffa9 | bellard | } |
7652 | 1d14ffa9 | bellard | |
7653 | 1d14ffa9 | bellard | if (bad_card)
|
7654 | 1d14ffa9 | bellard | goto show_valid_cards;
|
7655 | 1d14ffa9 | bellard | } |
7656 | 1d14ffa9 | bellard | } |
7657 | 1d14ffa9 | bellard | #endif
|
7658 | 1d14ffa9 | bellard | |
7659 | 3587d7e6 | bellard | #ifdef _WIN32
|
7660 | 3587d7e6 | bellard | static BOOL WINAPI qemu_ctrl_handler(DWORD type)
|
7661 | 3587d7e6 | bellard | { |
7662 | 3587d7e6 | bellard | exit(STATUS_CONTROL_C_EXIT); |
7663 | 3587d7e6 | bellard | return TRUE;
|
7664 | 3587d7e6 | bellard | } |
7665 | 3587d7e6 | bellard | #endif
|
7666 | 3587d7e6 | bellard | |
7667 | 7c9d8e07 | bellard | #define MAX_NET_CLIENTS 32 |
7668 | c20709aa | bellard | |
7669 | 0824d6fc | bellard | int main(int argc, char **argv) |
7670 | 0824d6fc | bellard | { |
7671 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
7672 | cfc3475a | pbrook | int use_gdbstub;
|
7673 | cfc3475a | pbrook | const char *gdbstub_port; |
7674 | 67b915a5 | bellard | #endif
|
7675 | 28c5af54 | j_mayer | uint32_t boot_devices_bitmap = 0;
|
7676 | e4bcb14c | ths | int i;
|
7677 | 28c5af54 | j_mayer | int snapshot, linux_boot, net_boot;
|
7678 | 7f7f9873 | bellard | const char *initrd_filename; |
7679 | a20dd508 | bellard | const char *kernel_filename, *kernel_cmdline; |
7680 | 28c5af54 | j_mayer | const char *boot_devices = ""; |
7681 | 313aa567 | bellard | DisplayState *ds = &display_state; |
7682 | 46d4767d | bellard | int cyls, heads, secs, translation;
|
7683 | fd5f393a | pbrook | const char *net_clients[MAX_NET_CLIENTS]; |
7684 | 7c9d8e07 | bellard | int nb_net_clients;
|
7685 | e4bcb14c | ths | int hda_index;
|
7686 | cd6f1169 | bellard | int optind;
|
7687 | cd6f1169 | bellard | const char *r, *optarg; |
7688 | 82c643ff | bellard | CharDriverState *monitor_hd; |
7689 | fd5f393a | pbrook | const char *monitor_device; |
7690 | fd5f393a | pbrook | const char *serial_devices[MAX_SERIAL_PORTS]; |
7691 | 8d11df9e | bellard | int serial_device_index;
|
7692 | fd5f393a | pbrook | const char *parallel_devices[MAX_PARALLEL_PORTS]; |
7693 | 6508fe59 | bellard | int parallel_device_index;
|
7694 | d63d307f | bellard | const char *loadvm = NULL; |
7695 | cc1daa40 | bellard | QEMUMachine *machine; |
7696 | 94fc95cd | j_mayer | const char *cpu_model; |
7697 | fd5f393a | pbrook | const char *usb_devices[MAX_USB_CMDLINE]; |
7698 | a594cfbf | bellard | int usb_devices_index;
|
7699 | 71e3ceb8 | ths | int fds[2]; |
7700 | 93815bc2 | ths | const char *pid_file = NULL; |
7701 | 833c7174 | blueswir1 | VLANState *vlan; |
7702 | 0bd48850 | bellard | |
7703 | 0bd48850 | bellard | LIST_INIT (&vm_change_state_head); |
7704 | be995c27 | bellard | #ifndef _WIN32
|
7705 | be995c27 | bellard | { |
7706 | be995c27 | bellard | struct sigaction act;
|
7707 | be995c27 | bellard | sigfillset(&act.sa_mask); |
7708 | be995c27 | bellard | act.sa_flags = 0;
|
7709 | be995c27 | bellard | act.sa_handler = SIG_IGN; |
7710 | be995c27 | bellard | sigaction(SIGPIPE, &act, NULL);
|
7711 | be995c27 | bellard | } |
7712 | 3587d7e6 | bellard | #else
|
7713 | 3587d7e6 | bellard | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
7714 | a8e5ac33 | bellard | /* Note: cpu_interrupt() is currently not SMP safe, so we force
|
7715 | a8e5ac33 | bellard | QEMU to run on a single CPU */
|
7716 | a8e5ac33 | bellard | { |
7717 | a8e5ac33 | bellard | HANDLE h; |
7718 | a8e5ac33 | bellard | DWORD mask, smask; |
7719 | a8e5ac33 | bellard | int i;
|
7720 | a8e5ac33 | bellard | h = GetCurrentProcess(); |
7721 | a8e5ac33 | bellard | if (GetProcessAffinityMask(h, &mask, &smask)) {
|
7722 | a8e5ac33 | bellard | for(i = 0; i < 32; i++) { |
7723 | a8e5ac33 | bellard | if (mask & (1 << i)) |
7724 | a8e5ac33 | bellard | break;
|
7725 | a8e5ac33 | bellard | } |
7726 | a8e5ac33 | bellard | if (i != 32) { |
7727 | a8e5ac33 | bellard | mask = 1 << i;
|
7728 | a8e5ac33 | bellard | SetProcessAffinityMask(h, mask); |
7729 | a8e5ac33 | bellard | } |
7730 | a8e5ac33 | bellard | } |
7731 | a8e5ac33 | bellard | } |
7732 | 67b915a5 | bellard | #endif
|
7733 | be995c27 | bellard | |
7734 | cc1daa40 | bellard | register_machines(); |
7735 | cc1daa40 | bellard | machine = first_machine; |
7736 | 94fc95cd | j_mayer | cpu_model = NULL;
|
7737 | fc01f7e7 | bellard | initrd_filename = NULL;
|
7738 | 4fc5d071 | aurel32 | ram_size = 0;
|
7739 | 313aa567 | bellard | vga_ram_size = VGA_RAM_SIZE; |
7740 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
7741 | b4608c04 | bellard | use_gdbstub = 0;
|
7742 | c636bb66 | bellard | gdbstub_port = DEFAULT_GDBSTUB_PORT; |
7743 | 67b915a5 | bellard | #endif
|
7744 | 33e3963e | bellard | snapshot = 0;
|
7745 | a20dd508 | bellard | nographic = 0;
|
7746 | 4d3b6f6e | balrog | curses = 0;
|
7747 | a20dd508 | bellard | kernel_filename = NULL;
|
7748 | a20dd508 | bellard | kernel_cmdline = "";
|
7749 | c4b1fcc0 | bellard | cyls = heads = secs = 0;
|
7750 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_AUTO; |
7751 | c75a823c | aurel32 | monitor_device = "vc:800x600";
|
7752 | c4b1fcc0 | bellard | |
7753 | c75a823c | aurel32 | serial_devices[0] = "vc:80Cx24C"; |
7754 | 8d11df9e | bellard | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
7755 | fd5f393a | pbrook | serial_devices[i] = NULL;
|
7756 | 8d11df9e | bellard | serial_device_index = 0;
|
7757 | 3b46e624 | ths | |
7758 | c75a823c | aurel32 | parallel_devices[0] = "vc:640x480"; |
7759 | 6508fe59 | bellard | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
7760 | fd5f393a | pbrook | parallel_devices[i] = NULL;
|
7761 | 6508fe59 | bellard | parallel_device_index = 0;
|
7762 | 3b46e624 | ths | |
7763 | a594cfbf | bellard | usb_devices_index = 0;
|
7764 | 3b46e624 | ths | |
7765 | 7c9d8e07 | bellard | nb_net_clients = 0;
|
7766 | e4bcb14c | ths | nb_drives = 0;
|
7767 | e4bcb14c | ths | nb_drives_opt = 0;
|
7768 | e4bcb14c | ths | hda_index = -1;
|
7769 | 7c9d8e07 | bellard | |
7770 | 7c9d8e07 | bellard | nb_nics = 0;
|
7771 | 702c651c | bellard | /* default mac address of the first network interface */
|
7772 | 3b46e624 | ths | |
7773 | cd6f1169 | bellard | optind = 1;
|
7774 | 0824d6fc | bellard | for(;;) {
|
7775 | cd6f1169 | bellard | if (optind >= argc)
|
7776 | 0824d6fc | bellard | break;
|
7777 | cd6f1169 | bellard | r = argv[optind]; |
7778 | cd6f1169 | bellard | if (r[0] != '-') { |
7779 | 609497ab | balrog | hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
|
7780 | cd6f1169 | bellard | } else {
|
7781 | cd6f1169 | bellard | const QEMUOption *popt;
|
7782 | cd6f1169 | bellard | |
7783 | cd6f1169 | bellard | optind++; |
7784 | dff5efc8 | pbrook | /* Treat --foo the same as -foo. */
|
7785 | dff5efc8 | pbrook | if (r[1] == '-') |
7786 | dff5efc8 | pbrook | r++; |
7787 | cd6f1169 | bellard | popt = qemu_options; |
7788 | cd6f1169 | bellard | for(;;) {
|
7789 | cd6f1169 | bellard | if (!popt->name) {
|
7790 | 5fafdf24 | ths | fprintf(stderr, "%s: invalid option -- '%s'\n",
|
7791 | cd6f1169 | bellard | argv[0], r);
|
7792 | cd6f1169 | bellard | exit(1);
|
7793 | cd6f1169 | bellard | } |
7794 | cd6f1169 | bellard | if (!strcmp(popt->name, r + 1)) |
7795 | cd6f1169 | bellard | break;
|
7796 | cd6f1169 | bellard | popt++; |
7797 | cd6f1169 | bellard | } |
7798 | cd6f1169 | bellard | if (popt->flags & HAS_ARG) {
|
7799 | cd6f1169 | bellard | if (optind >= argc) {
|
7800 | cd6f1169 | bellard | fprintf(stderr, "%s: option '%s' requires an argument\n",
|
7801 | cd6f1169 | bellard | argv[0], r);
|
7802 | cd6f1169 | bellard | exit(1);
|
7803 | cd6f1169 | bellard | } |
7804 | cd6f1169 | bellard | optarg = argv[optind++]; |
7805 | cd6f1169 | bellard | } else {
|
7806 | cd6f1169 | bellard | optarg = NULL;
|
7807 | cd6f1169 | bellard | } |
7808 | cd6f1169 | bellard | |
7809 | cd6f1169 | bellard | switch(popt->index) {
|
7810 | cc1daa40 | bellard | case QEMU_OPTION_M:
|
7811 | cc1daa40 | bellard | machine = find_machine(optarg); |
7812 | cc1daa40 | bellard | if (!machine) {
|
7813 | cc1daa40 | bellard | QEMUMachine *m; |
7814 | cc1daa40 | bellard | printf("Supported machines are:\n");
|
7815 | cc1daa40 | bellard | for(m = first_machine; m != NULL; m = m->next) { |
7816 | cc1daa40 | bellard | printf("%-10s %s%s\n",
|
7817 | 5fafdf24 | ths | m->name, m->desc, |
7818 | cc1daa40 | bellard | m == first_machine ? " (default)" : ""); |
7819 | cc1daa40 | bellard | } |
7820 | 15f82208 | ths | exit(*optarg != '?');
|
7821 | cc1daa40 | bellard | } |
7822 | cc1daa40 | bellard | break;
|
7823 | 94fc95cd | j_mayer | case QEMU_OPTION_cpu:
|
7824 | 94fc95cd | j_mayer | /* hw initialization will check this */
|
7825 | 15f82208 | ths | if (*optarg == '?') { |
7826 | c732abe2 | j_mayer | /* XXX: implement xxx_cpu_list for targets that still miss it */
|
7827 | c732abe2 | j_mayer | #if defined(cpu_list)
|
7828 | c732abe2 | j_mayer | cpu_list(stdout, &fprintf); |
7829 | 94fc95cd | j_mayer | #endif
|
7830 | 15f82208 | ths | exit(0);
|
7831 | 94fc95cd | j_mayer | } else {
|
7832 | 94fc95cd | j_mayer | cpu_model = optarg; |
7833 | 94fc95cd | j_mayer | } |
7834 | 94fc95cd | j_mayer | break;
|
7835 | cd6f1169 | bellard | case QEMU_OPTION_initrd:
|
7836 | fc01f7e7 | bellard | initrd_filename = optarg; |
7837 | fc01f7e7 | bellard | break;
|
7838 | cd6f1169 | bellard | case QEMU_OPTION_hda:
|
7839 | e4bcb14c | ths | if (cyls == 0) |
7840 | 609497ab | balrog | hda_index = drive_add(optarg, HD_ALIAS, 0);
|
7841 | e4bcb14c | ths | else
|
7842 | 609497ab | balrog | hda_index = drive_add(optarg, HD_ALIAS |
7843 | e4bcb14c | ths | ",cyls=%d,heads=%d,secs=%d%s",
|
7844 | 609497ab | balrog | 0, cyls, heads, secs,
|
7845 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_LBA ? |
7846 | e4bcb14c | ths | ",trans=lba" :
|
7847 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_NONE ? |
7848 | e4bcb14c | ths | ",trans=none" : ""); |
7849 | e4bcb14c | ths | break;
|
7850 | cd6f1169 | bellard | case QEMU_OPTION_hdb:
|
7851 | cc1daa40 | bellard | case QEMU_OPTION_hdc:
|
7852 | cc1daa40 | bellard | case QEMU_OPTION_hdd:
|
7853 | 609497ab | balrog | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
7854 | fc01f7e7 | bellard | break;
|
7855 | e4bcb14c | ths | case QEMU_OPTION_drive:
|
7856 | 609497ab | balrog | drive_add(NULL, "%s", optarg); |
7857 | e4bcb14c | ths | break;
|
7858 | 3e3d5815 | balrog | case QEMU_OPTION_mtdblock:
|
7859 | 609497ab | balrog | drive_add(optarg, MTD_ALIAS); |
7860 | 3e3d5815 | balrog | break;
|
7861 | a1bb27b1 | pbrook | case QEMU_OPTION_sd:
|
7862 | 609497ab | balrog | drive_add(optarg, SD_ALIAS); |
7863 | a1bb27b1 | pbrook | break;
|
7864 | 86f55663 | j_mayer | case QEMU_OPTION_pflash:
|
7865 | 609497ab | balrog | drive_add(optarg, PFLASH_ALIAS); |
7866 | 86f55663 | j_mayer | break;
|
7867 | cd6f1169 | bellard | case QEMU_OPTION_snapshot:
|
7868 | 33e3963e | bellard | snapshot = 1;
|
7869 | 33e3963e | bellard | break;
|
7870 | cd6f1169 | bellard | case QEMU_OPTION_hdachs:
|
7871 | 330d0414 | bellard | { |
7872 | 330d0414 | bellard | const char *p; |
7873 | 330d0414 | bellard | p = optarg; |
7874 | 330d0414 | bellard | cyls = strtol(p, (char **)&p, 0); |
7875 | 46d4767d | bellard | if (cyls < 1 || cyls > 16383) |
7876 | 46d4767d | bellard | goto chs_fail;
|
7877 | 330d0414 | bellard | if (*p != ',') |
7878 | 330d0414 | bellard | goto chs_fail;
|
7879 | 330d0414 | bellard | p++; |
7880 | 330d0414 | bellard | heads = strtol(p, (char **)&p, 0); |
7881 | 46d4767d | bellard | if (heads < 1 || heads > 16) |
7882 | 46d4767d | bellard | goto chs_fail;
|
7883 | 330d0414 | bellard | if (*p != ',') |
7884 | 330d0414 | bellard | goto chs_fail;
|
7885 | 330d0414 | bellard | p++; |
7886 | 330d0414 | bellard | secs = strtol(p, (char **)&p, 0); |
7887 | 46d4767d | bellard | if (secs < 1 || secs > 63) |
7888 | 46d4767d | bellard | goto chs_fail;
|
7889 | 46d4767d | bellard | if (*p == ',') { |
7890 | 46d4767d | bellard | p++; |
7891 | 46d4767d | bellard | if (!strcmp(p, "none")) |
7892 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_NONE; |
7893 | 46d4767d | bellard | else if (!strcmp(p, "lba")) |
7894 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_LBA; |
7895 | 46d4767d | bellard | else if (!strcmp(p, "auto")) |
7896 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_AUTO; |
7897 | 46d4767d | bellard | else
|
7898 | 46d4767d | bellard | goto chs_fail;
|
7899 | 46d4767d | bellard | } else if (*p != '\0') { |
7900 | c4b1fcc0 | bellard | chs_fail:
|
7901 | 46d4767d | bellard | fprintf(stderr, "qemu: invalid physical CHS format\n");
|
7902 | 46d4767d | bellard | exit(1);
|
7903 | c4b1fcc0 | bellard | } |
7904 | e4bcb14c | ths | if (hda_index != -1) |
7905 | 609497ab | balrog | snprintf(drives_opt[hda_index].opt, |
7906 | 609497ab | balrog | sizeof(drives_opt[hda_index].opt),
|
7907 | 609497ab | balrog | HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
|
7908 | 609497ab | balrog | 0, cyls, heads, secs,
|
7909 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_LBA ? |
7910 | e4bcb14c | ths | ",trans=lba" :
|
7911 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_NONE ? |
7912 | e4bcb14c | ths | ",trans=none" : ""); |
7913 | 330d0414 | bellard | } |
7914 | 330d0414 | bellard | break;
|
7915 | cd6f1169 | bellard | case QEMU_OPTION_nographic:
|
7916 | fd5f393a | pbrook | serial_devices[0] = "stdio"; |
7917 | fd5f393a | pbrook | parallel_devices[0] = "null"; |
7918 | fd5f393a | pbrook | monitor_device = "stdio";
|
7919 | a20dd508 | bellard | nographic = 1;
|
7920 | a20dd508 | bellard | break;
|
7921 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
7922 | 4d3b6f6e | balrog | case QEMU_OPTION_curses:
|
7923 | 4d3b6f6e | balrog | curses = 1;
|
7924 | 4d3b6f6e | balrog | break;
|
7925 | 4d3b6f6e | balrog | #endif
|
7926 | a171fe39 | balrog | case QEMU_OPTION_portrait:
|
7927 | a171fe39 | balrog | graphic_rotate = 1;
|
7928 | a171fe39 | balrog | break;
|
7929 | cd6f1169 | bellard | case QEMU_OPTION_kernel:
|
7930 | a20dd508 | bellard | kernel_filename = optarg; |
7931 | a20dd508 | bellard | break;
|
7932 | cd6f1169 | bellard | case QEMU_OPTION_append:
|
7933 | a20dd508 | bellard | kernel_cmdline = optarg; |
7934 | 313aa567 | bellard | break;
|
7935 | cd6f1169 | bellard | case QEMU_OPTION_cdrom:
|
7936 | 609497ab | balrog | drive_add(optarg, CDROM_ALIAS); |
7937 | 36b486bb | bellard | break;
|
7938 | cd6f1169 | bellard | case QEMU_OPTION_boot:
|
7939 | 28c5af54 | j_mayer | boot_devices = optarg; |
7940 | 28c5af54 | j_mayer | /* We just do some generic consistency checks */
|
7941 | 28c5af54 | j_mayer | { |
7942 | 28c5af54 | j_mayer | /* Could easily be extended to 64 devices if needed */
|
7943 | 60fe76f3 | ths | const char *p; |
7944 | 28c5af54 | j_mayer | |
7945 | 28c5af54 | j_mayer | boot_devices_bitmap = 0;
|
7946 | 28c5af54 | j_mayer | for (p = boot_devices; *p != '\0'; p++) { |
7947 | 28c5af54 | j_mayer | /* Allowed boot devices are:
|
7948 | 28c5af54 | j_mayer | * a b : floppy disk drives
|
7949 | 28c5af54 | j_mayer | * c ... f : IDE disk drives
|
7950 | 28c5af54 | j_mayer | * g ... m : machine implementation dependant drives
|
7951 | 28c5af54 | j_mayer | * n ... p : network devices
|
7952 | 28c5af54 | j_mayer | * It's up to each machine implementation to check
|
7953 | 28c5af54 | j_mayer | * if the given boot devices match the actual hardware
|
7954 | 28c5af54 | j_mayer | * implementation and firmware features.
|
7955 | 28c5af54 | j_mayer | */
|
7956 | 28c5af54 | j_mayer | if (*p < 'a' || *p > 'q') { |
7957 | 28c5af54 | j_mayer | fprintf(stderr, "Invalid boot device '%c'\n", *p);
|
7958 | 28c5af54 | j_mayer | exit(1);
|
7959 | 28c5af54 | j_mayer | } |
7960 | 28c5af54 | j_mayer | if (boot_devices_bitmap & (1 << (*p - 'a'))) { |
7961 | 28c5af54 | j_mayer | fprintf(stderr, |
7962 | 28c5af54 | j_mayer | "Boot device '%c' was given twice\n",*p);
|
7963 | 28c5af54 | j_mayer | exit(1);
|
7964 | 28c5af54 | j_mayer | } |
7965 | 28c5af54 | j_mayer | boot_devices_bitmap |= 1 << (*p - 'a'); |
7966 | 28c5af54 | j_mayer | } |
7967 | 36b486bb | bellard | } |
7968 | 36b486bb | bellard | break;
|
7969 | cd6f1169 | bellard | case QEMU_OPTION_fda:
|
7970 | cd6f1169 | bellard | case QEMU_OPTION_fdb:
|
7971 | 609497ab | balrog | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
7972 | c45886db | bellard | break;
|
7973 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7974 | 52ca8d6a | bellard | case QEMU_OPTION_no_fd_bootchk:
|
7975 | 52ca8d6a | bellard | fd_bootchk = 0;
|
7976 | 52ca8d6a | bellard | break;
|
7977 | 52ca8d6a | bellard | #endif
|
7978 | 7c9d8e07 | bellard | case QEMU_OPTION_net:
|
7979 | 7c9d8e07 | bellard | if (nb_net_clients >= MAX_NET_CLIENTS) {
|
7980 | 7c9d8e07 | bellard | fprintf(stderr, "qemu: too many network clients\n");
|
7981 | c4b1fcc0 | bellard | exit(1);
|
7982 | c4b1fcc0 | bellard | } |
7983 | fd5f393a | pbrook | net_clients[nb_net_clients] = optarg; |
7984 | 7c9d8e07 | bellard | nb_net_clients++; |
7985 | 702c651c | bellard | break;
|
7986 | c7f74643 | bellard | #ifdef CONFIG_SLIRP
|
7987 | c7f74643 | bellard | case QEMU_OPTION_tftp:
|
7988 | c7f74643 | bellard | tftp_prefix = optarg; |
7989 | 9bf05444 | bellard | break;
|
7990 | 47d5d01a | ths | case QEMU_OPTION_bootp:
|
7991 | 47d5d01a | ths | bootp_filename = optarg; |
7992 | 47d5d01a | ths | break;
|
7993 | c94c8d64 | bellard | #ifndef _WIN32
|
7994 | 9d728e8c | bellard | case QEMU_OPTION_smb:
|
7995 | 9d728e8c | bellard | net_slirp_smb(optarg); |
7996 | 9d728e8c | bellard | break;
|
7997 | c94c8d64 | bellard | #endif
|
7998 | 9bf05444 | bellard | case QEMU_OPTION_redir:
|
7999 | 3b46e624 | ths | net_slirp_redir(optarg); |
8000 | 9bf05444 | bellard | break;
|
8001 | c7f74643 | bellard | #endif
|
8002 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
8003 | 1d14ffa9 | bellard | case QEMU_OPTION_audio_help:
|
8004 | 1d14ffa9 | bellard | AUD_help (); |
8005 | 1d14ffa9 | bellard | exit (0);
|
8006 | 1d14ffa9 | bellard | break;
|
8007 | 1d14ffa9 | bellard | case QEMU_OPTION_soundhw:
|
8008 | 1d14ffa9 | bellard | select_soundhw (optarg); |
8009 | 1d14ffa9 | bellard | break;
|
8010 | 1d14ffa9 | bellard | #endif
|
8011 | cd6f1169 | bellard | case QEMU_OPTION_h:
|
8012 | 15f82208 | ths | help(0);
|
8013 | cd6f1169 | bellard | break;
|
8014 | 00f82b8a | aurel32 | case QEMU_OPTION_m: {
|
8015 | 00f82b8a | aurel32 | uint64_t value; |
8016 | 00f82b8a | aurel32 | char *ptr;
|
8017 | 00f82b8a | aurel32 | |
8018 | 00f82b8a | aurel32 | value = strtoul(optarg, &ptr, 10);
|
8019 | 00f82b8a | aurel32 | switch (*ptr) {
|
8020 | 00f82b8a | aurel32 | case 0: case 'M': case 'm': |
8021 | 00f82b8a | aurel32 | value <<= 20;
|
8022 | 00f82b8a | aurel32 | break;
|
8023 | 00f82b8a | aurel32 | case 'G': case 'g': |
8024 | 00f82b8a | aurel32 | value <<= 30;
|
8025 | 00f82b8a | aurel32 | break;
|
8026 | 00f82b8a | aurel32 | default:
|
8027 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
|
8028 | cd6f1169 | bellard | exit(1);
|
8029 | cd6f1169 | bellard | } |
8030 | 00f82b8a | aurel32 | |
8031 | 00f82b8a | aurel32 | /* On 32-bit hosts, QEMU is limited by virtual address space */
|
8032 | 00f82b8a | aurel32 | if (value > (2047 << 20) |
8033 | 00f82b8a | aurel32 | #ifndef USE_KQEMU
|
8034 | 00f82b8a | aurel32 | && HOST_LONG_BITS == 32
|
8035 | 00f82b8a | aurel32 | #endif
|
8036 | 00f82b8a | aurel32 | ) { |
8037 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
|
8038 | 00f82b8a | aurel32 | exit(1);
|
8039 | 00f82b8a | aurel32 | } |
8040 | 00f82b8a | aurel32 | if (value != (uint64_t)(ram_addr_t)value) {
|
8041 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: ram size too large\n");
|
8042 | 00f82b8a | aurel32 | exit(1);
|
8043 | 00f82b8a | aurel32 | } |
8044 | 00f82b8a | aurel32 | ram_size = value; |
8045 | cd6f1169 | bellard | break;
|
8046 | 00f82b8a | aurel32 | } |
8047 | cd6f1169 | bellard | case QEMU_OPTION_d:
|
8048 | cd6f1169 | bellard | { |
8049 | cd6f1169 | bellard | int mask;
|
8050 | cd6f1169 | bellard | CPULogItem *item; |
8051 | 3b46e624 | ths | |
8052 | cd6f1169 | bellard | mask = cpu_str_to_log_mask(optarg); |
8053 | cd6f1169 | bellard | if (!mask) {
|
8054 | cd6f1169 | bellard | printf("Log items (comma separated):\n");
|
8055 | f193c797 | bellard | for(item = cpu_log_items; item->mask != 0; item++) { |
8056 | f193c797 | bellard | printf("%-10s %s\n", item->name, item->help);
|
8057 | f193c797 | bellard | } |
8058 | f193c797 | bellard | exit(1);
|
8059 | cd6f1169 | bellard | } |
8060 | cd6f1169 | bellard | cpu_set_log(mask); |
8061 | f193c797 | bellard | } |
8062 | cd6f1169 | bellard | break;
|
8063 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
8064 | cd6f1169 | bellard | case QEMU_OPTION_s:
|
8065 | cd6f1169 | bellard | use_gdbstub = 1;
|
8066 | cd6f1169 | bellard | break;
|
8067 | cd6f1169 | bellard | case QEMU_OPTION_p:
|
8068 | cfc3475a | pbrook | gdbstub_port = optarg; |
8069 | cd6f1169 | bellard | break;
|
8070 | 67b915a5 | bellard | #endif
|
8071 | cd6f1169 | bellard | case QEMU_OPTION_L:
|
8072 | cd6f1169 | bellard | bios_dir = optarg; |
8073 | cd6f1169 | bellard | break;
|
8074 | 1192dad8 | j_mayer | case QEMU_OPTION_bios:
|
8075 | 1192dad8 | j_mayer | bios_name = optarg; |
8076 | 1192dad8 | j_mayer | break;
|
8077 | cd6f1169 | bellard | case QEMU_OPTION_S:
|
8078 | 3c07f8e8 | pbrook | autostart = 0;
|
8079 | cd6f1169 | bellard | break;
|
8080 | 3d11d0eb | bellard | case QEMU_OPTION_k:
|
8081 | 3d11d0eb | bellard | keyboard_layout = optarg; |
8082 | 3d11d0eb | bellard | break;
|
8083 | ee22c2f7 | bellard | case QEMU_OPTION_localtime:
|
8084 | ee22c2f7 | bellard | rtc_utc = 0;
|
8085 | ee22c2f7 | bellard | break;
|
8086 | 1f04275e | bellard | case QEMU_OPTION_cirrusvga:
|
8087 | 1f04275e | bellard | cirrus_vga_enabled = 1;
|
8088 | d34cab9f | ths | vmsvga_enabled = 0;
|
8089 | d34cab9f | ths | break;
|
8090 | d34cab9f | ths | case QEMU_OPTION_vmsvga:
|
8091 | d34cab9f | ths | cirrus_vga_enabled = 0;
|
8092 | d34cab9f | ths | vmsvga_enabled = 1;
|
8093 | 1f04275e | bellard | break;
|
8094 | 1bfe856e | bellard | case QEMU_OPTION_std_vga:
|
8095 | 1bfe856e | bellard | cirrus_vga_enabled = 0;
|
8096 | d34cab9f | ths | vmsvga_enabled = 0;
|
8097 | 1bfe856e | bellard | break;
|
8098 | e9b137c2 | bellard | case QEMU_OPTION_g:
|
8099 | e9b137c2 | bellard | { |
8100 | e9b137c2 | bellard | const char *p; |
8101 | e9b137c2 | bellard | int w, h, depth;
|
8102 | e9b137c2 | bellard | p = optarg; |
8103 | e9b137c2 | bellard | w = strtol(p, (char **)&p, 10); |
8104 | e9b137c2 | bellard | if (w <= 0) { |
8105 | e9b137c2 | bellard | graphic_error:
|
8106 | e9b137c2 | bellard | fprintf(stderr, "qemu: invalid resolution or depth\n");
|
8107 | e9b137c2 | bellard | exit(1);
|
8108 | e9b137c2 | bellard | } |
8109 | e9b137c2 | bellard | if (*p != 'x') |
8110 | e9b137c2 | bellard | goto graphic_error;
|
8111 | e9b137c2 | bellard | p++; |
8112 | e9b137c2 | bellard | h = strtol(p, (char **)&p, 10); |
8113 | e9b137c2 | bellard | if (h <= 0) |
8114 | e9b137c2 | bellard | goto graphic_error;
|
8115 | e9b137c2 | bellard | if (*p == 'x') { |
8116 | e9b137c2 | bellard | p++; |
8117 | e9b137c2 | bellard | depth = strtol(p, (char **)&p, 10); |
8118 | 5fafdf24 | ths | if (depth != 8 && depth != 15 && depth != 16 && |
8119 | e9b137c2 | bellard | depth != 24 && depth != 32) |
8120 | e9b137c2 | bellard | goto graphic_error;
|
8121 | e9b137c2 | bellard | } else if (*p == '\0') { |
8122 | e9b137c2 | bellard | depth = graphic_depth; |
8123 | e9b137c2 | bellard | } else {
|
8124 | e9b137c2 | bellard | goto graphic_error;
|
8125 | e9b137c2 | bellard | } |
8126 | 3b46e624 | ths | |
8127 | e9b137c2 | bellard | graphic_width = w; |
8128 | e9b137c2 | bellard | graphic_height = h; |
8129 | e9b137c2 | bellard | graphic_depth = depth; |
8130 | e9b137c2 | bellard | } |
8131 | e9b137c2 | bellard | break;
|
8132 | 20d8a3ed | ths | case QEMU_OPTION_echr:
|
8133 | 20d8a3ed | ths | { |
8134 | 20d8a3ed | ths | char *r;
|
8135 | 20d8a3ed | ths | term_escape_char = strtol(optarg, &r, 0);
|
8136 | 20d8a3ed | ths | if (r == optarg)
|
8137 | 20d8a3ed | ths | printf("Bad argument to echr\n");
|
8138 | 20d8a3ed | ths | break;
|
8139 | 20d8a3ed | ths | } |
8140 | 82c643ff | bellard | case QEMU_OPTION_monitor:
|
8141 | fd5f393a | pbrook | monitor_device = optarg; |
8142 | 82c643ff | bellard | break;
|
8143 | 82c643ff | bellard | case QEMU_OPTION_serial:
|
8144 | 8d11df9e | bellard | if (serial_device_index >= MAX_SERIAL_PORTS) {
|
8145 | 8d11df9e | bellard | fprintf(stderr, "qemu: too many serial ports\n");
|
8146 | 8d11df9e | bellard | exit(1);
|
8147 | 8d11df9e | bellard | } |
8148 | fd5f393a | pbrook | serial_devices[serial_device_index] = optarg; |
8149 | 8d11df9e | bellard | serial_device_index++; |
8150 | 82c643ff | bellard | break;
|
8151 | 6508fe59 | bellard | case QEMU_OPTION_parallel:
|
8152 | 6508fe59 | bellard | if (parallel_device_index >= MAX_PARALLEL_PORTS) {
|
8153 | 6508fe59 | bellard | fprintf(stderr, "qemu: too many parallel ports\n");
|
8154 | 6508fe59 | bellard | exit(1);
|
8155 | 6508fe59 | bellard | } |
8156 | fd5f393a | pbrook | parallel_devices[parallel_device_index] = optarg; |
8157 | 6508fe59 | bellard | parallel_device_index++; |
8158 | 6508fe59 | bellard | break;
|
8159 | d63d307f | bellard | case QEMU_OPTION_loadvm:
|
8160 | d63d307f | bellard | loadvm = optarg; |
8161 | d63d307f | bellard | break;
|
8162 | d63d307f | bellard | case QEMU_OPTION_full_screen:
|
8163 | d63d307f | bellard | full_screen = 1;
|
8164 | d63d307f | bellard | break;
|
8165 | 667accab | ths | #ifdef CONFIG_SDL
|
8166 | 43523e93 | ths | case QEMU_OPTION_no_frame:
|
8167 | 43523e93 | ths | no_frame = 1;
|
8168 | 43523e93 | ths | break;
|
8169 | 3780e197 | ths | case QEMU_OPTION_alt_grab:
|
8170 | 3780e197 | ths | alt_grab = 1;
|
8171 | 3780e197 | ths | break;
|
8172 | 667accab | ths | case QEMU_OPTION_no_quit:
|
8173 | 667accab | ths | no_quit = 1;
|
8174 | 667accab | ths | break;
|
8175 | 667accab | ths | #endif
|
8176 | f7cce898 | bellard | case QEMU_OPTION_pidfile:
|
8177 | 93815bc2 | ths | pid_file = optarg; |
8178 | f7cce898 | bellard | break;
|
8179 | a09db21f | bellard | #ifdef TARGET_I386
|
8180 | a09db21f | bellard | case QEMU_OPTION_win2k_hack:
|
8181 | a09db21f | bellard | win2k_install_hack = 1;
|
8182 | a09db21f | bellard | break;
|
8183 | a09db21f | bellard | #endif
|
8184 | d993e026 | bellard | #ifdef USE_KQEMU
|
8185 | d993e026 | bellard | case QEMU_OPTION_no_kqemu:
|
8186 | d993e026 | bellard | kqemu_allowed = 0;
|
8187 | d993e026 | bellard | break;
|
8188 | 89bfc105 | bellard | case QEMU_OPTION_kernel_kqemu:
|
8189 | 89bfc105 | bellard | kqemu_allowed = 2;
|
8190 | 89bfc105 | bellard | break;
|
8191 | d993e026 | bellard | #endif
|
8192 | bb36d470 | bellard | case QEMU_OPTION_usb:
|
8193 | bb36d470 | bellard | usb_enabled = 1;
|
8194 | bb36d470 | bellard | break;
|
8195 | a594cfbf | bellard | case QEMU_OPTION_usbdevice:
|
8196 | a594cfbf | bellard | usb_enabled = 1;
|
8197 | 0d92ed30 | pbrook | if (usb_devices_index >= MAX_USB_CMDLINE) {
|
8198 | a594cfbf | bellard | fprintf(stderr, "Too many USB devices\n");
|
8199 | a594cfbf | bellard | exit(1);
|
8200 | a594cfbf | bellard | } |
8201 | fd5f393a | pbrook | usb_devices[usb_devices_index] = optarg; |
8202 | a594cfbf | bellard | usb_devices_index++; |
8203 | a594cfbf | bellard | break;
|
8204 | 6a00d601 | bellard | case QEMU_OPTION_smp:
|
8205 | 6a00d601 | bellard | smp_cpus = atoi(optarg); |
8206 | ba3c64fb | bellard | if (smp_cpus < 1 || smp_cpus > MAX_CPUS) { |
8207 | 6a00d601 | bellard | fprintf(stderr, "Invalid number of CPUs\n");
|
8208 | 6a00d601 | bellard | exit(1);
|
8209 | 6a00d601 | bellard | } |
8210 | 6a00d601 | bellard | break;
|
8211 | 24236869 | bellard | case QEMU_OPTION_vnc:
|
8212 | 73fc9742 | ths | vnc_display = optarg; |
8213 | 24236869 | bellard | break;
|
8214 | 6515b203 | bellard | case QEMU_OPTION_no_acpi:
|
8215 | 6515b203 | bellard | acpi_enabled = 0;
|
8216 | 6515b203 | bellard | break;
|
8217 | d1beab82 | bellard | case QEMU_OPTION_no_reboot:
|
8218 | d1beab82 | bellard | no_reboot = 1;
|
8219 | d1beab82 | bellard | break;
|
8220 | b2f76161 | aurel32 | case QEMU_OPTION_no_shutdown:
|
8221 | b2f76161 | aurel32 | no_shutdown = 1;
|
8222 | b2f76161 | aurel32 | break;
|
8223 | 9467cd46 | balrog | case QEMU_OPTION_show_cursor:
|
8224 | 9467cd46 | balrog | cursor_hide = 0;
|
8225 | 9467cd46 | balrog | break;
|
8226 | 71e3ceb8 | ths | case QEMU_OPTION_daemonize:
|
8227 | 71e3ceb8 | ths | daemonize = 1;
|
8228 | 71e3ceb8 | ths | break;
|
8229 | 9ae02555 | ths | case QEMU_OPTION_option_rom:
|
8230 | 9ae02555 | ths | if (nb_option_roms >= MAX_OPTION_ROMS) {
|
8231 | 9ae02555 | ths | fprintf(stderr, "Too many option ROMs\n");
|
8232 | 9ae02555 | ths | exit(1);
|
8233 | 9ae02555 | ths | } |
8234 | 9ae02555 | ths | option_rom[nb_option_roms] = optarg; |
8235 | 9ae02555 | ths | nb_option_roms++; |
8236 | 9ae02555 | ths | break;
|
8237 | 8e71621f | pbrook | case QEMU_OPTION_semihosting:
|
8238 | 8e71621f | pbrook | semihosting_enabled = 1;
|
8239 | 8e71621f | pbrook | break;
|
8240 | c35734b2 | ths | case QEMU_OPTION_name:
|
8241 | c35734b2 | ths | qemu_name = optarg; |
8242 | c35734b2 | ths | break;
|
8243 | 66508601 | blueswir1 | #ifdef TARGET_SPARC
|
8244 | 66508601 | blueswir1 | case QEMU_OPTION_prom_env:
|
8245 | 66508601 | blueswir1 | if (nb_prom_envs >= MAX_PROM_ENVS) {
|
8246 | 66508601 | blueswir1 | fprintf(stderr, "Too many prom variables\n");
|
8247 | 66508601 | blueswir1 | exit(1);
|
8248 | 66508601 | blueswir1 | } |
8249 | 66508601 | blueswir1 | prom_envs[nb_prom_envs] = optarg; |
8250 | 66508601 | blueswir1 | nb_prom_envs++; |
8251 | 66508601 | blueswir1 | break;
|
8252 | 66508601 | blueswir1 | #endif
|
8253 | 2b8f2d41 | balrog | #ifdef TARGET_ARM
|
8254 | 2b8f2d41 | balrog | case QEMU_OPTION_old_param:
|
8255 | 2b8f2d41 | balrog | old_param = 1;
|
8256 | 05ebd537 | ths | break;
|
8257 | 2b8f2d41 | balrog | #endif
|
8258 | f3dcfada | ths | case QEMU_OPTION_clock:
|
8259 | f3dcfada | ths | configure_alarms(optarg); |
8260 | f3dcfada | ths | break;
|
8261 | 7e0af5d0 | bellard | case QEMU_OPTION_startdate:
|
8262 | 7e0af5d0 | bellard | { |
8263 | 7e0af5d0 | bellard | struct tm tm;
|
8264 | f6503059 | balrog | time_t rtc_start_date; |
8265 | 7e0af5d0 | bellard | if (!strcmp(optarg, "now")) { |
8266 | f6503059 | balrog | rtc_date_offset = -1;
|
8267 | 7e0af5d0 | bellard | } else {
|
8268 | 7e0af5d0 | bellard | if (sscanf(optarg, "%d-%d-%dT%d:%d:%d", |
8269 | 7e0af5d0 | bellard | &tm.tm_year, |
8270 | 7e0af5d0 | bellard | &tm.tm_mon, |
8271 | 7e0af5d0 | bellard | &tm.tm_mday, |
8272 | 7e0af5d0 | bellard | &tm.tm_hour, |
8273 | 7e0af5d0 | bellard | &tm.tm_min, |
8274 | 7e0af5d0 | bellard | &tm.tm_sec) == 6) {
|
8275 | 7e0af5d0 | bellard | /* OK */
|
8276 | 7e0af5d0 | bellard | } else if (sscanf(optarg, "%d-%d-%d", |
8277 | 7e0af5d0 | bellard | &tm.tm_year, |
8278 | 7e0af5d0 | bellard | &tm.tm_mon, |
8279 | 7e0af5d0 | bellard | &tm.tm_mday) == 3) {
|
8280 | 7e0af5d0 | bellard | tm.tm_hour = 0;
|
8281 | 7e0af5d0 | bellard | tm.tm_min = 0;
|
8282 | 7e0af5d0 | bellard | tm.tm_sec = 0;
|
8283 | 7e0af5d0 | bellard | } else {
|
8284 | 7e0af5d0 | bellard | goto date_fail;
|
8285 | 7e0af5d0 | bellard | } |
8286 | 7e0af5d0 | bellard | tm.tm_year -= 1900;
|
8287 | 7e0af5d0 | bellard | tm.tm_mon--; |
8288 | 3c6b2088 | bellard | rtc_start_date = mktimegm(&tm); |
8289 | 7e0af5d0 | bellard | if (rtc_start_date == -1) { |
8290 | 7e0af5d0 | bellard | date_fail:
|
8291 | 7e0af5d0 | bellard | fprintf(stderr, "Invalid date format. Valid format are:\n"
|
8292 | 7e0af5d0 | bellard | "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
|
8293 | 7e0af5d0 | bellard | exit(1);
|
8294 | 7e0af5d0 | bellard | } |
8295 | f6503059 | balrog | rtc_date_offset = time(NULL) - rtc_start_date;
|
8296 | 7e0af5d0 | bellard | } |
8297 | 7e0af5d0 | bellard | } |
8298 | 7e0af5d0 | bellard | break;
|
8299 | cd6f1169 | bellard | } |
8300 | 0824d6fc | bellard | } |
8301 | 0824d6fc | bellard | } |
8302 | 330d0414 | bellard | |
8303 | 71e3ceb8 | ths | #ifndef _WIN32
|
8304 | 71e3ceb8 | ths | if (daemonize && !nographic && vnc_display == NULL) { |
8305 | 71e3ceb8 | ths | fprintf(stderr, "Can only daemonize if using -nographic or -vnc\n");
|
8306 | 71e3ceb8 | ths | daemonize = 0;
|
8307 | 71e3ceb8 | ths | } |
8308 | 71e3ceb8 | ths | |
8309 | 71e3ceb8 | ths | if (daemonize) {
|
8310 | 71e3ceb8 | ths | pid_t pid; |
8311 | 71e3ceb8 | ths | |
8312 | 71e3ceb8 | ths | if (pipe(fds) == -1) |
8313 | 71e3ceb8 | ths | exit(1);
|
8314 | 71e3ceb8 | ths | |
8315 | 71e3ceb8 | ths | pid = fork(); |
8316 | 71e3ceb8 | ths | if (pid > 0) { |
8317 | 71e3ceb8 | ths | uint8_t status; |
8318 | 71e3ceb8 | ths | ssize_t len; |
8319 | 71e3ceb8 | ths | |
8320 | 71e3ceb8 | ths | close(fds[1]);
|
8321 | 71e3ceb8 | ths | |
8322 | 71e3ceb8 | ths | again:
|
8323 | 93815bc2 | ths | len = read(fds[0], &status, 1); |
8324 | 93815bc2 | ths | if (len == -1 && (errno == EINTR)) |
8325 | 93815bc2 | ths | goto again;
|
8326 | 93815bc2 | ths | |
8327 | 93815bc2 | ths | if (len != 1) |
8328 | 93815bc2 | ths | exit(1);
|
8329 | 93815bc2 | ths | else if (status == 1) { |
8330 | 93815bc2 | ths | fprintf(stderr, "Could not acquire pidfile\n");
|
8331 | 93815bc2 | ths | exit(1);
|
8332 | 93815bc2 | ths | } else
|
8333 | 93815bc2 | ths | exit(0);
|
8334 | 71e3ceb8 | ths | } else if (pid < 0) |
8335 | 93815bc2 | ths | exit(1);
|
8336 | 71e3ceb8 | ths | |
8337 | 71e3ceb8 | ths | setsid(); |
8338 | 71e3ceb8 | ths | |
8339 | 71e3ceb8 | ths | pid = fork(); |
8340 | 71e3ceb8 | ths | if (pid > 0) |
8341 | 71e3ceb8 | ths | exit(0);
|
8342 | 71e3ceb8 | ths | else if (pid < 0) |
8343 | 71e3ceb8 | ths | exit(1);
|
8344 | 71e3ceb8 | ths | |
8345 | 71e3ceb8 | ths | umask(027);
|
8346 | 71e3ceb8 | ths | chdir("/");
|
8347 | 71e3ceb8 | ths | |
8348 | 71e3ceb8 | ths | signal(SIGTSTP, SIG_IGN); |
8349 | 71e3ceb8 | ths | signal(SIGTTOU, SIG_IGN); |
8350 | 71e3ceb8 | ths | signal(SIGTTIN, SIG_IGN); |
8351 | 71e3ceb8 | ths | } |
8352 | 71e3ceb8 | ths | #endif
|
8353 | 71e3ceb8 | ths | |
8354 | aa26bb2d | ths | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
8355 | 93815bc2 | ths | if (daemonize) {
|
8356 | 93815bc2 | ths | uint8_t status = 1;
|
8357 | 93815bc2 | ths | write(fds[1], &status, 1); |
8358 | 93815bc2 | ths | } else
|
8359 | 93815bc2 | ths | fprintf(stderr, "Could not acquire pid file\n");
|
8360 | 93815bc2 | ths | exit(1);
|
8361 | 93815bc2 | ths | } |
8362 | 93815bc2 | ths | |
8363 | ff3fbb30 | bellard | #ifdef USE_KQEMU
|
8364 | ff3fbb30 | bellard | if (smp_cpus > 1) |
8365 | ff3fbb30 | bellard | kqemu_allowed = 0;
|
8366 | ff3fbb30 | bellard | #endif
|
8367 | a20dd508 | bellard | linux_boot = (kernel_filename != NULL);
|
8368 | 7317b8ca | balrog | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
8369 | 6c41b272 | balrog | |
8370 | 28c5af54 | j_mayer | /* XXX: this should not be: some embedded targets just have flash */
|
8371 | 28c5af54 | j_mayer | if (!linux_boot && net_boot == 0 && |
8372 | e4bcb14c | ths | nb_drives_opt == 0)
|
8373 | 15f82208 | ths | help(1);
|
8374 | 0824d6fc | bellard | |
8375 | 96d30e48 | ths | /* boot to floppy or the default cd if no hard disk defined yet */
|
8376 | 28c5af54 | j_mayer | if (!boot_devices[0]) { |
8377 | e4bcb14c | ths | boot_devices = "cad";
|
8378 | 96d30e48 | ths | } |
8379 | b118d61e | bellard | setvbuf(stdout, NULL, _IOLBF, 0); |
8380 | 3b46e624 | ths | |
8381 | 634fce96 | pbrook | init_timers(); |
8382 | 634fce96 | pbrook | init_timer_alarm(); |
8383 | 83f64091 | bellard | qemu_aio_init(); |
8384 | 634fce96 | pbrook | |
8385 | fd1dff4b | bellard | #ifdef _WIN32
|
8386 | fd1dff4b | bellard | socket_init(); |
8387 | fd1dff4b | bellard | #endif
|
8388 | fd1dff4b | bellard | |
8389 | 7c9d8e07 | bellard | /* init network clients */
|
8390 | 7c9d8e07 | bellard | if (nb_net_clients == 0) { |
8391 | 7c9d8e07 | bellard | /* if no clients, we use a default config */
|
8392 | fd5f393a | pbrook | net_clients[0] = "nic"; |
8393 | fd5f393a | pbrook | net_clients[1] = "user"; |
8394 | 7c9d8e07 | bellard | nb_net_clients = 2;
|
8395 | c20709aa | bellard | } |
8396 | c20709aa | bellard | |
8397 | 7c9d8e07 | bellard | for(i = 0;i < nb_net_clients; i++) { |
8398 | 7c9d8e07 | bellard | if (net_client_init(net_clients[i]) < 0) |
8399 | 7c9d8e07 | bellard | exit(1);
|
8400 | 702c651c | bellard | } |
8401 | 833c7174 | blueswir1 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
8402 | 833c7174 | blueswir1 | if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) |
8403 | 833c7174 | blueswir1 | continue;
|
8404 | 833c7174 | blueswir1 | if (vlan->nb_guest_devs == 0) { |
8405 | 833c7174 | blueswir1 | fprintf(stderr, "Invalid vlan (%d) with no nics\n", vlan->id);
|
8406 | 833c7174 | blueswir1 | exit(1);
|
8407 | 833c7174 | blueswir1 | } |
8408 | 833c7174 | blueswir1 | if (vlan->nb_host_devs == 0) |
8409 | 833c7174 | blueswir1 | fprintf(stderr, |
8410 | 833c7174 | blueswir1 | "Warning: vlan %d is not connected to host network\n",
|
8411 | 833c7174 | blueswir1 | vlan->id); |
8412 | 833c7174 | blueswir1 | } |
8413 | f1510b2c | bellard | |
8414 | eec85c2a | ths | #ifdef TARGET_I386
|
8415 | ed494d87 | balrog | /* XXX: this should be moved in the PC machine instantiation code */
|
8416 | 28c5af54 | j_mayer | if (net_boot != 0) { |
8417 | 28c5af54 | j_mayer | int netroms = 0; |
8418 | 28c5af54 | j_mayer | for (i = 0; i < nb_nics && i < 4; i++) { |
8419 | eec85c2a | ths | const char *model = nd_table[i].model; |
8420 | eec85c2a | ths | char buf[1024]; |
8421 | 28c5af54 | j_mayer | if (net_boot & (1 << i)) { |
8422 | 28c5af54 | j_mayer | if (model == NULL) |
8423 | 28c5af54 | j_mayer | model = "ne2k_pci";
|
8424 | 28c5af54 | j_mayer | snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model); |
8425 | 28c5af54 | j_mayer | if (get_image_size(buf) > 0) { |
8426 | 28c5af54 | j_mayer | if (nb_option_roms >= MAX_OPTION_ROMS) {
|
8427 | 28c5af54 | j_mayer | fprintf(stderr, "Too many option ROMs\n");
|
8428 | 28c5af54 | j_mayer | exit(1);
|
8429 | 28c5af54 | j_mayer | } |
8430 | 28c5af54 | j_mayer | option_rom[nb_option_roms] = strdup(buf); |
8431 | 28c5af54 | j_mayer | nb_option_roms++; |
8432 | 28c5af54 | j_mayer | netroms++; |
8433 | 28c5af54 | j_mayer | } |
8434 | 28c5af54 | j_mayer | } |
8435 | eec85c2a | ths | } |
8436 | 28c5af54 | j_mayer | if (netroms == 0) { |
8437 | eec85c2a | ths | fprintf(stderr, "No valid PXE rom found for network device\n");
|
8438 | eec85c2a | ths | exit(1);
|
8439 | eec85c2a | ths | } |
8440 | eec85c2a | ths | } |
8441 | eec85c2a | ths | #endif
|
8442 | eec85c2a | ths | |
8443 | 0824d6fc | bellard | /* init the memory */
|
8444 | 7fb4fdcf | balrog | phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED; |
8445 | 7fb4fdcf | balrog | |
8446 | 7fb4fdcf | balrog | if (machine->ram_require & RAMSIZE_FIXED) {
|
8447 | 7fb4fdcf | balrog | if (ram_size > 0) { |
8448 | 7fb4fdcf | balrog | if (ram_size < phys_ram_size) {
|
8449 | cd940061 | aurel32 | fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
|
8450 | cd940061 | aurel32 | machine->name, (unsigned long long) phys_ram_size); |
8451 | 7fb4fdcf | balrog | exit(-1);
|
8452 | 7fb4fdcf | balrog | } |
8453 | 7fb4fdcf | balrog | |
8454 | 7fb4fdcf | balrog | phys_ram_size = ram_size; |
8455 | 7fb4fdcf | balrog | } else
|
8456 | 7fb4fdcf | balrog | ram_size = phys_ram_size; |
8457 | 7fb4fdcf | balrog | } else {
|
8458 | 4fc5d071 | aurel32 | if (ram_size == 0) |
8459 | 7fb4fdcf | balrog | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
8460 | 7fb4fdcf | balrog | |
8461 | 7fb4fdcf | balrog | phys_ram_size += ram_size; |
8462 | 7fb4fdcf | balrog | } |
8463 | 9ae02555 | ths | |
8464 | d993e026 | bellard | phys_ram_base = qemu_vmalloc(phys_ram_size); |
8465 | 7f7f9873 | bellard | if (!phys_ram_base) {
|
8466 | 7f7f9873 | bellard | fprintf(stderr, "Could not allocate physical memory\n");
|
8467 | 0824d6fc | bellard | exit(1);
|
8468 | 0824d6fc | bellard | } |
8469 | 0824d6fc | bellard | |
8470 | 5905b2e5 | bellard | bdrv_init(); |
8471 | c4b1fcc0 | bellard | |
8472 | e4bcb14c | ths | /* we always create the cdrom drive, even if no disk is there */
|
8473 | c4b1fcc0 | bellard | |
8474 | e4bcb14c | ths | if (nb_drives_opt < MAX_DRIVES)
|
8475 | 609497ab | balrog | drive_add(NULL, CDROM_ALIAS);
|
8476 | c4b1fcc0 | bellard | |
8477 | 9d413d1d | balrog | /* we always create at least one floppy */
|
8478 | 33e3963e | bellard | |
8479 | e4bcb14c | ths | if (nb_drives_opt < MAX_DRIVES)
|
8480 | 609497ab | balrog | drive_add(NULL, FD_ALIAS, 0); |
8481 | 86f55663 | j_mayer | |
8482 | 9d413d1d | balrog | /* we always create one sd slot, even if no card is in it */
|
8483 | 9d413d1d | balrog | |
8484 | 9d413d1d | balrog | if (nb_drives_opt < MAX_DRIVES)
|
8485 | 609497ab | balrog | drive_add(NULL, SD_ALIAS);
|
8486 | 9d413d1d | balrog | |
8487 | e4bcb14c | ths | /* open the virtual block devices */
|
8488 | e4bcb14c | ths | |
8489 | e4bcb14c | ths | for(i = 0; i < nb_drives_opt; i++) |
8490 | 609497ab | balrog | if (drive_init(&drives_opt[i], snapshot, machine) == -1) |
8491 | e4bcb14c | ths | exit(1);
|
8492 | 3e3d5815 | balrog | |
8493 | c88676f8 | bellard | register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
8494 | c88676f8 | bellard | register_savevm("ram", 0, 2, ram_save, ram_load, NULL); |
8495 | 8a7ddc38 | bellard | |
8496 | 330d0414 | bellard | init_ioports(); |
8497 | 0824d6fc | bellard | |
8498 | 313aa567 | bellard | /* terminal init */
|
8499 | 740733bb | ths | memset(&display_state, 0, sizeof(display_state)); |
8500 | a20dd508 | bellard | if (nographic) {
|
8501 | 4d3b6f6e | balrog | if (curses) {
|
8502 | 4d3b6f6e | balrog | fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
|
8503 | 4d3b6f6e | balrog | exit(1);
|
8504 | 4d3b6f6e | balrog | } |
8505 | 2ff89790 | ths | /* nearly nothing to do */
|
8506 | 2ff89790 | ths | dumb_display_init(ds); |
8507 | 73fc9742 | ths | } else if (vnc_display != NULL) { |
8508 | 71cab5ca | ths | vnc_display_init(ds); |
8509 | 71cab5ca | ths | if (vnc_display_open(ds, vnc_display) < 0) |
8510 | 71cab5ca | ths | exit(1);
|
8511 | 4d3b6f6e | balrog | } else
|
8512 | 4d3b6f6e | balrog | #if defined(CONFIG_CURSES)
|
8513 | 4d3b6f6e | balrog | if (curses) {
|
8514 | 4d3b6f6e | balrog | curses_display_init(ds, full_screen); |
8515 | 4d3b6f6e | balrog | } else
|
8516 | 4d3b6f6e | balrog | #endif
|
8517 | 4d3b6f6e | balrog | { |
8518 | 5b0753e0 | bellard | #if defined(CONFIG_SDL)
|
8519 | 43523e93 | ths | sdl_display_init(ds, full_screen, no_frame); |
8520 | 5b0753e0 | bellard | #elif defined(CONFIG_COCOA)
|
8521 | 5b0753e0 | bellard | cocoa_display_init(ds, full_screen); |
8522 | 67276f53 | pbrook | #else
|
8523 | 67276f53 | pbrook | dumb_display_init(ds); |
8524 | 313aa567 | bellard | #endif
|
8525 | 313aa567 | bellard | } |
8526 | 0824d6fc | bellard | |
8527 | 20d8a3ed | ths | /* Maintain compatibility with multiple stdio monitors */
|
8528 | 20d8a3ed | ths | if (!strcmp(monitor_device,"stdio")) { |
8529 | 20d8a3ed | ths | for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
8530 | fd5f393a | pbrook | const char *devname = serial_devices[i]; |
8531 | fd5f393a | pbrook | if (devname && !strcmp(devname,"mon:stdio")) { |
8532 | fd5f393a | pbrook | monitor_device = NULL;
|
8533 | 20d8a3ed | ths | break;
|
8534 | fd5f393a | pbrook | } else if (devname && !strcmp(devname,"stdio")) { |
8535 | fd5f393a | pbrook | monitor_device = NULL;
|
8536 | fd5f393a | pbrook | serial_devices[i] = "mon:stdio";
|
8537 | 20d8a3ed | ths | break;
|
8538 | 20d8a3ed | ths | } |
8539 | 20d8a3ed | ths | } |
8540 | 20d8a3ed | ths | } |
8541 | fd5f393a | pbrook | if (monitor_device) {
|
8542 | 20d8a3ed | ths | monitor_hd = qemu_chr_open(monitor_device); |
8543 | 20d8a3ed | ths | if (!monitor_hd) {
|
8544 | 20d8a3ed | ths | fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
|
8545 | 20d8a3ed | ths | exit(1);
|
8546 | 20d8a3ed | ths | } |
8547 | 20d8a3ed | ths | monitor_init(monitor_hd, !nographic); |
8548 | 82c643ff | bellard | } |
8549 | 82c643ff | bellard | |
8550 | 8d11df9e | bellard | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
8551 | c03b0f0f | bellard | const char *devname = serial_devices[i]; |
8552 | fd5f393a | pbrook | if (devname && strcmp(devname, "none")) { |
8553 | c03b0f0f | bellard | serial_hds[i] = qemu_chr_open(devname); |
8554 | 8d11df9e | bellard | if (!serial_hds[i]) {
|
8555 | 5fafdf24 | ths | fprintf(stderr, "qemu: could not open serial device '%s'\n",
|
8556 | c03b0f0f | bellard | devname); |
8557 | 8d11df9e | bellard | exit(1);
|
8558 | 8d11df9e | bellard | } |
8559 | af3a9031 | ths | if (strstart(devname, "vc", 0)) |
8560 | 7ba1260a | bellard | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
|
8561 | 8d11df9e | bellard | } |
8562 | 82c643ff | bellard | } |
8563 | 82c643ff | bellard | |
8564 | 6508fe59 | bellard | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
8565 | c03b0f0f | bellard | const char *devname = parallel_devices[i]; |
8566 | fd5f393a | pbrook | if (devname && strcmp(devname, "none")) { |
8567 | c03b0f0f | bellard | parallel_hds[i] = qemu_chr_open(devname); |
8568 | 6508fe59 | bellard | if (!parallel_hds[i]) {
|
8569 | 5fafdf24 | ths | fprintf(stderr, "qemu: could not open parallel device '%s'\n",
|
8570 | c03b0f0f | bellard | devname); |
8571 | 6508fe59 | bellard | exit(1);
|
8572 | 6508fe59 | bellard | } |
8573 | af3a9031 | ths | if (strstart(devname, "vc", 0)) |
8574 | 7ba1260a | bellard | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
|
8575 | 6508fe59 | bellard | } |
8576 | 6508fe59 | bellard | } |
8577 | 6508fe59 | bellard | |
8578 | b881c2c6 | blueswir1 | machine->init(ram_size, vga_ram_size, boot_devices, ds, |
8579 | 94fc95cd | j_mayer | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
8580 | 73332e5c | bellard | |
8581 | 0d92ed30 | pbrook | /* init USB devices */
|
8582 | 0d92ed30 | pbrook | if (usb_enabled) {
|
8583 | 0d92ed30 | pbrook | for(i = 0; i < usb_devices_index; i++) { |
8584 | 0d92ed30 | pbrook | if (usb_device_add(usb_devices[i]) < 0) { |
8585 | 0d92ed30 | pbrook | fprintf(stderr, "Warning: could not add USB device %s\n",
|
8586 | 0d92ed30 | pbrook | usb_devices[i]); |
8587 | 0d92ed30 | pbrook | } |
8588 | 0d92ed30 | pbrook | } |
8589 | 0d92ed30 | pbrook | } |
8590 | 0d92ed30 | pbrook | |
8591 | 740733bb | ths | if (display_state.dpy_refresh) {
|
8592 | 740733bb | ths | display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state); |
8593 | 740733bb | ths | qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock)); |
8594 | 740733bb | ths | } |
8595 | 7f7f9873 | bellard | |
8596 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
8597 | b4608c04 | bellard | if (use_gdbstub) {
|
8598 | c636bb66 | bellard | /* XXX: use standard host:port notation and modify options
|
8599 | c636bb66 | bellard | accordingly. */
|
8600 | cfc3475a | pbrook | if (gdbserver_start(gdbstub_port) < 0) { |
8601 | cfc3475a | pbrook | fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
|
8602 | c636bb66 | bellard | gdbstub_port); |
8603 | 8a7ddc38 | bellard | exit(1);
|
8604 | 8a7ddc38 | bellard | } |
8605 | 45669e00 | balrog | } |
8606 | 67b915a5 | bellard | #endif
|
8607 | 45669e00 | balrog | |
8608 | d63d307f | bellard | if (loadvm)
|
8609 | faea38e7 | bellard | do_loadvm(loadvm); |
8610 | d63d307f | bellard | |
8611 | 67b915a5 | bellard | { |
8612 | 5905b2e5 | bellard | /* XXX: simplify init */
|
8613 | 5905b2e5 | bellard | read_passwords(); |
8614 | 3c07f8e8 | pbrook | if (autostart) {
|
8615 | 5905b2e5 | bellard | vm_start(); |
8616 | 5905b2e5 | bellard | } |
8617 | 0824d6fc | bellard | } |
8618 | ffd843bc | ths | |
8619 | 71e3ceb8 | ths | if (daemonize) {
|
8620 | 71e3ceb8 | ths | uint8_t status = 0;
|
8621 | 71e3ceb8 | ths | ssize_t len; |
8622 | 71e3ceb8 | ths | int fd;
|
8623 | 71e3ceb8 | ths | |
8624 | 71e3ceb8 | ths | again1:
|
8625 | 71e3ceb8 | ths | len = write(fds[1], &status, 1); |
8626 | 71e3ceb8 | ths | if (len == -1 && (errno == EINTR)) |
8627 | 71e3ceb8 | ths | goto again1;
|
8628 | 71e3ceb8 | ths | |
8629 | 71e3ceb8 | ths | if (len != 1) |
8630 | 71e3ceb8 | ths | exit(1);
|
8631 | 71e3ceb8 | ths | |
8632 | aeb30be6 | balrog | TFR(fd = open("/dev/null", O_RDWR));
|
8633 | 71e3ceb8 | ths | if (fd == -1) |
8634 | 71e3ceb8 | ths | exit(1);
|
8635 | 71e3ceb8 | ths | |
8636 | 71e3ceb8 | ths | dup2(fd, 0);
|
8637 | 71e3ceb8 | ths | dup2(fd, 1);
|
8638 | 71e3ceb8 | ths | dup2(fd, 2);
|
8639 | 71e3ceb8 | ths | |
8640 | 71e3ceb8 | ths | close(fd); |
8641 | 71e3ceb8 | ths | } |
8642 | 71e3ceb8 | ths | |
8643 | 8a7ddc38 | bellard | main_loop(); |
8644 | 40c3bac3 | bellard | quit_timers(); |
8645 | b46a8906 | ths | |
8646 | 7d294b61 | ths | #if !defined(_WIN32)
|
8647 | b46a8906 | ths | /* close network clients */
|
8648 | b46a8906 | ths | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
8649 | b46a8906 | ths | VLANClientState *vc; |
8650 | b46a8906 | ths | |
8651 | 7d294b61 | ths | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
8652 | b46a8906 | ths | if (vc->fd_read == tap_receive) {
|
8653 | b46a8906 | ths | char ifname[64]; |
8654 | b46a8906 | ths | TAPState *s = vc->opaque; |
8655 | b46a8906 | ths | |
8656 | b46a8906 | ths | if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 && |
8657 | b46a8906 | ths | s->down_script[0])
|
8658 | b46a8906 | ths | launch_script(s->down_script, ifname, s->fd); |
8659 | b46a8906 | ths | } |
8660 | 4fddf62a | ths | } |
8661 | 7d294b61 | ths | } |
8662 | 7d294b61 | ths | #endif
|
8663 | 0824d6fc | bellard | return 0; |
8664 | 0824d6fc | bellard | } |