root / vl.c @ 8c5e95d8
History | View | Annotate | Download (230.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 | bf20dc07 | ths | /* Conversion factor from emulated instructions to virtual clock ticks. */
|
243 | 2e70f6ef | pbrook | static int icount_time_shift; |
244 | bf20dc07 | ths | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
|
245 | 2e70f6ef | pbrook | #define MAX_ICOUNT_SHIFT 10 |
246 | 2e70f6ef | pbrook | /* Compensate for varying guest execution speed. */
|
247 | 2e70f6ef | pbrook | static int64_t qemu_icount_bias;
|
248 | 2e70f6ef | pbrook | QEMUTimer *icount_rt_timer; |
249 | 2e70f6ef | pbrook | QEMUTimer *icount_vm_timer; |
250 | ee5605e5 | balrog | |
251 | aeb30be6 | balrog | #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR) |
252 | aeb30be6 | balrog | |
253 | 0824d6fc | bellard | /***********************************************************/
|
254 | 26aa7d72 | bellard | /* x86 ISA bus support */
|
255 | 26aa7d72 | bellard | |
256 | 26aa7d72 | bellard | target_phys_addr_t isa_mem_base = 0;
|
257 | 3de388f6 | bellard | PicState2 *isa_pic; |
258 | 0824d6fc | bellard | |
259 | 9596ebb7 | pbrook | static uint32_t default_ioport_readb(void *opaque, uint32_t address) |
260 | 0824d6fc | bellard | { |
261 | 0824d6fc | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
262 | 1196be37 | ths | fprintf(stderr, "unused inb: port=0x%04x\n", address);
|
263 | 0824d6fc | bellard | #endif
|
264 | fc01f7e7 | bellard | return 0xff; |
265 | 0824d6fc | bellard | } |
266 | 0824d6fc | bellard | |
267 | 9596ebb7 | pbrook | static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
268 | 0824d6fc | bellard | { |
269 | 0824d6fc | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
270 | 1196be37 | ths | fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
|
271 | 0824d6fc | bellard | #endif
|
272 | 0824d6fc | bellard | } |
273 | 0824d6fc | bellard | |
274 | 0824d6fc | bellard | /* default is to make two byte accesses */
|
275 | 9596ebb7 | pbrook | static uint32_t default_ioport_readw(void *opaque, uint32_t address) |
276 | 0824d6fc | bellard | { |
277 | 0824d6fc | bellard | uint32_t data; |
278 | db45c29a | bellard | data = ioport_read_table[0][address](ioport_opaque[address], address);
|
279 | db45c29a | bellard | address = (address + 1) & (MAX_IOPORTS - 1); |
280 | db45c29a | bellard | data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8; |
281 | 0824d6fc | bellard | return data;
|
282 | 0824d6fc | bellard | } |
283 | 0824d6fc | bellard | |
284 | 9596ebb7 | pbrook | static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
285 | 0824d6fc | bellard | { |
286 | db45c29a | bellard | ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff); |
287 | db45c29a | bellard | address = (address + 1) & (MAX_IOPORTS - 1); |
288 | db45c29a | bellard | ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff); |
289 | 0824d6fc | bellard | } |
290 | 0824d6fc | bellard | |
291 | 9596ebb7 | pbrook | static uint32_t default_ioport_readl(void *opaque, uint32_t address) |
292 | 0824d6fc | bellard | { |
293 | fc01f7e7 | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
294 | 1196be37 | ths | fprintf(stderr, "unused inl: port=0x%04x\n", address);
|
295 | fc01f7e7 | bellard | #endif
|
296 | fc01f7e7 | bellard | return 0xffffffff; |
297 | 0824d6fc | bellard | } |
298 | 0824d6fc | bellard | |
299 | 9596ebb7 | pbrook | static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
300 | 0824d6fc | bellard | { |
301 | fc01f7e7 | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
302 | 1196be37 | ths | fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
|
303 | fc01f7e7 | bellard | #endif
|
304 | 0824d6fc | bellard | } |
305 | 0824d6fc | bellard | |
306 | 9596ebb7 | pbrook | static void init_ioports(void) |
307 | 0824d6fc | bellard | { |
308 | 0824d6fc | bellard | int i;
|
309 | 0824d6fc | bellard | |
310 | fc01f7e7 | bellard | for(i = 0; i < MAX_IOPORTS; i++) { |
311 | fc01f7e7 | bellard | ioport_read_table[0][i] = default_ioport_readb;
|
312 | fc01f7e7 | bellard | ioport_write_table[0][i] = default_ioport_writeb;
|
313 | fc01f7e7 | bellard | ioport_read_table[1][i] = default_ioport_readw;
|
314 | fc01f7e7 | bellard | ioport_write_table[1][i] = default_ioport_writew;
|
315 | fc01f7e7 | bellard | ioport_read_table[2][i] = default_ioport_readl;
|
316 | fc01f7e7 | bellard | ioport_write_table[2][i] = default_ioport_writel;
|
317 | fc01f7e7 | bellard | } |
318 | 0824d6fc | bellard | } |
319 | 0824d6fc | bellard | |
320 | fc01f7e7 | bellard | /* size is the word size in byte */
|
321 | 5fafdf24 | ths | int register_ioport_read(int start, int length, int size, |
322 | c4b1fcc0 | bellard | IOPortReadFunc *func, void *opaque)
|
323 | f1510b2c | bellard | { |
324 | fc01f7e7 | bellard | int i, bsize;
|
325 | f1510b2c | bellard | |
326 | c4b1fcc0 | bellard | if (size == 1) { |
327 | fc01f7e7 | bellard | bsize = 0;
|
328 | c4b1fcc0 | bellard | } else if (size == 2) { |
329 | fc01f7e7 | bellard | bsize = 1;
|
330 | c4b1fcc0 | bellard | } else if (size == 4) { |
331 | fc01f7e7 | bellard | bsize = 2;
|
332 | c4b1fcc0 | bellard | } else {
|
333 | 88fdf56f | balrog | hw_error("register_ioport_read: invalid size");
|
334 | fc01f7e7 | bellard | return -1; |
335 | c4b1fcc0 | bellard | } |
336 | c4b1fcc0 | bellard | for(i = start; i < start + length; i += size) {
|
337 | fc01f7e7 | bellard | ioport_read_table[bsize][i] = func; |
338 | c4b1fcc0 | bellard | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
339 | 88fdf56f | balrog | hw_error("register_ioport_read: invalid opaque");
|
340 | c4b1fcc0 | bellard | ioport_opaque[i] = opaque; |
341 | c4b1fcc0 | bellard | } |
342 | f1510b2c | bellard | return 0; |
343 | f1510b2c | bellard | } |
344 | f1510b2c | bellard | |
345 | fc01f7e7 | bellard | /* size is the word size in byte */
|
346 | 5fafdf24 | ths | int register_ioport_write(int start, int length, int size, |
347 | c4b1fcc0 | bellard | IOPortWriteFunc *func, void *opaque)
|
348 | f1510b2c | bellard | { |
349 | fc01f7e7 | bellard | int i, bsize;
|
350 | f1510b2c | bellard | |
351 | c4b1fcc0 | bellard | if (size == 1) { |
352 | fc01f7e7 | bellard | bsize = 0;
|
353 | c4b1fcc0 | bellard | } else if (size == 2) { |
354 | fc01f7e7 | bellard | bsize = 1;
|
355 | c4b1fcc0 | bellard | } else if (size == 4) { |
356 | fc01f7e7 | bellard | bsize = 2;
|
357 | c4b1fcc0 | bellard | } else {
|
358 | 88fdf56f | balrog | hw_error("register_ioport_write: invalid size");
|
359 | fc01f7e7 | bellard | return -1; |
360 | c4b1fcc0 | bellard | } |
361 | c4b1fcc0 | bellard | for(i = start; i < start + length; i += size) {
|
362 | fc01f7e7 | bellard | ioport_write_table[bsize][i] = func; |
363 | 88fdf56f | balrog | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
364 | 88fdf56f | balrog | hw_error("register_ioport_write: invalid opaque");
|
365 | c4b1fcc0 | bellard | ioport_opaque[i] = opaque; |
366 | c4b1fcc0 | bellard | } |
367 | f1510b2c | bellard | return 0; |
368 | f1510b2c | bellard | } |
369 | f1510b2c | bellard | |
370 | 69b91039 | bellard | void isa_unassign_ioport(int start, int length) |
371 | 69b91039 | bellard | { |
372 | 69b91039 | bellard | int i;
|
373 | 69b91039 | bellard | |
374 | 69b91039 | bellard | for(i = start; i < start + length; i++) {
|
375 | 69b91039 | bellard | ioport_read_table[0][i] = default_ioport_readb;
|
376 | 69b91039 | bellard | ioport_read_table[1][i] = default_ioport_readw;
|
377 | 69b91039 | bellard | ioport_read_table[2][i] = default_ioport_readl;
|
378 | 69b91039 | bellard | |
379 | 69b91039 | bellard | ioport_write_table[0][i] = default_ioport_writeb;
|
380 | 69b91039 | bellard | ioport_write_table[1][i] = default_ioport_writew;
|
381 | 69b91039 | bellard | ioport_write_table[2][i] = default_ioport_writel;
|
382 | 69b91039 | bellard | } |
383 | 69b91039 | bellard | } |
384 | 69b91039 | bellard | |
385 | 20f32282 | bellard | /***********************************************************/
|
386 | 20f32282 | bellard | |
387 | c45886db | bellard | void cpu_outb(CPUState *env, int addr, int val) |
388 | 0824d6fc | bellard | { |
389 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
390 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
391 | fd872598 | bellard | fprintf(logfile, "outb: %04x %02x\n", addr, val);
|
392 | 3b46e624 | ths | #endif
|
393 | c4b1fcc0 | bellard | ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
|
394 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
395 | 89bfc105 | bellard | if (env)
|
396 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
397 | 89bfc105 | bellard | #endif
|
398 | 0824d6fc | bellard | } |
399 | 0824d6fc | bellard | |
400 | c45886db | bellard | void cpu_outw(CPUState *env, int addr, int val) |
401 | 0824d6fc | bellard | { |
402 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
403 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
404 | fd872598 | bellard | fprintf(logfile, "outw: %04x %04x\n", addr, val);
|
405 | 3b46e624 | ths | #endif
|
406 | c4b1fcc0 | bellard | ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
|
407 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
408 | 89bfc105 | bellard | if (env)
|
409 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
410 | 89bfc105 | bellard | #endif
|
411 | 0824d6fc | bellard | } |
412 | 0824d6fc | bellard | |
413 | c45886db | bellard | void cpu_outl(CPUState *env, int addr, int val) |
414 | 0824d6fc | bellard | { |
415 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
416 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
417 | fd872598 | bellard | fprintf(logfile, "outl: %04x %08x\n", addr, val);
|
418 | fd872598 | bellard | #endif
|
419 | c4b1fcc0 | bellard | ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
|
420 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
421 | 89bfc105 | bellard | if (env)
|
422 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
423 | 89bfc105 | bellard | #endif
|
424 | 0824d6fc | bellard | } |
425 | 0824d6fc | bellard | |
426 | c45886db | bellard | int cpu_inb(CPUState *env, int addr) |
427 | 0824d6fc | bellard | { |
428 | fd872598 | bellard | int val;
|
429 | fd872598 | bellard | val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
|
430 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
431 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
432 | fd872598 | bellard | fprintf(logfile, "inb : %04x %02x\n", addr, val);
|
433 | fd872598 | bellard | #endif
|
434 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
435 | 89bfc105 | bellard | if (env)
|
436 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
437 | 89bfc105 | bellard | #endif
|
438 | fd872598 | bellard | return val;
|
439 | 0824d6fc | bellard | } |
440 | 0824d6fc | bellard | |
441 | c45886db | bellard | int cpu_inw(CPUState *env, int addr) |
442 | 0824d6fc | bellard | { |
443 | fd872598 | bellard | int val;
|
444 | fd872598 | bellard | val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
|
445 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
446 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
447 | fd872598 | bellard | fprintf(logfile, "inw : %04x %04x\n", addr, val);
|
448 | fd872598 | bellard | #endif
|
449 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
450 | 89bfc105 | bellard | if (env)
|
451 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
452 | 89bfc105 | bellard | #endif
|
453 | fd872598 | bellard | return val;
|
454 | 0824d6fc | bellard | } |
455 | 0824d6fc | bellard | |
456 | c45886db | bellard | int cpu_inl(CPUState *env, int addr) |
457 | 0824d6fc | bellard | { |
458 | fd872598 | bellard | int val;
|
459 | fd872598 | bellard | val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
|
460 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
461 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
462 | fd872598 | bellard | fprintf(logfile, "inl : %04x %08x\n", addr, val);
|
463 | fd872598 | bellard | #endif
|
464 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
465 | 89bfc105 | bellard | if (env)
|
466 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
467 | 89bfc105 | bellard | #endif
|
468 | fd872598 | bellard | return val;
|
469 | 0824d6fc | bellard | } |
470 | 0824d6fc | bellard | |
471 | 0824d6fc | bellard | /***********************************************************/
|
472 | 0824d6fc | bellard | void hw_error(const char *fmt, ...) |
473 | 0824d6fc | bellard | { |
474 | 0824d6fc | bellard | va_list ap; |
475 | 6a00d601 | bellard | CPUState *env; |
476 | 0824d6fc | bellard | |
477 | 0824d6fc | bellard | va_start(ap, fmt); |
478 | 0824d6fc | bellard | fprintf(stderr, "qemu: hardware error: ");
|
479 | 0824d6fc | bellard | vfprintf(stderr, fmt, ap); |
480 | 0824d6fc | bellard | fprintf(stderr, "\n");
|
481 | 6a00d601 | bellard | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
482 | 6a00d601 | bellard | fprintf(stderr, "CPU #%d:\n", env->cpu_index);
|
483 | 0824d6fc | bellard | #ifdef TARGET_I386
|
484 | 6a00d601 | bellard | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
485 | c45886db | bellard | #else
|
486 | 6a00d601 | bellard | cpu_dump_state(env, stderr, fprintf, 0);
|
487 | 0824d6fc | bellard | #endif
|
488 | 6a00d601 | bellard | } |
489 | 0824d6fc | bellard | va_end(ap); |
490 | 0824d6fc | bellard | abort(); |
491 | 0824d6fc | bellard | } |
492 | 0824d6fc | bellard | |
493 | 8a7ddc38 | bellard | /***********************************************************/
|
494 | 63066f4f | bellard | /* keyboard/mouse */
|
495 | 63066f4f | bellard | |
496 | 63066f4f | bellard | static QEMUPutKBDEvent *qemu_put_kbd_event;
|
497 | 63066f4f | bellard | static void *qemu_put_kbd_event_opaque; |
498 | 455204eb | ths | static QEMUPutMouseEntry *qemu_put_mouse_event_head;
|
499 | 455204eb | ths | static QEMUPutMouseEntry *qemu_put_mouse_event_current;
|
500 | 63066f4f | bellard | |
501 | 63066f4f | bellard | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
502 | 63066f4f | bellard | { |
503 | 63066f4f | bellard | qemu_put_kbd_event_opaque = opaque; |
504 | 63066f4f | bellard | qemu_put_kbd_event = func; |
505 | 63066f4f | bellard | } |
506 | 63066f4f | bellard | |
507 | 455204eb | ths | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
508 | 455204eb | ths | void *opaque, int absolute, |
509 | 455204eb | ths | const char *name) |
510 | 63066f4f | bellard | { |
511 | 455204eb | ths | QEMUPutMouseEntry *s, *cursor; |
512 | 455204eb | ths | |
513 | 455204eb | ths | s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
|
514 | 455204eb | ths | if (!s)
|
515 | 455204eb | ths | return NULL; |
516 | 455204eb | ths | |
517 | 455204eb | ths | s->qemu_put_mouse_event = func; |
518 | 455204eb | ths | s->qemu_put_mouse_event_opaque = opaque; |
519 | 455204eb | ths | s->qemu_put_mouse_event_absolute = absolute; |
520 | 455204eb | ths | s->qemu_put_mouse_event_name = qemu_strdup(name); |
521 | 455204eb | ths | s->next = NULL;
|
522 | 455204eb | ths | |
523 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
524 | 455204eb | ths | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
525 | 455204eb | ths | return s;
|
526 | 455204eb | ths | } |
527 | 455204eb | ths | |
528 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
529 | 455204eb | ths | while (cursor->next != NULL) |
530 | 455204eb | ths | cursor = cursor->next; |
531 | 455204eb | ths | |
532 | 455204eb | ths | cursor->next = s; |
533 | 455204eb | ths | qemu_put_mouse_event_current = s; |
534 | 455204eb | ths | |
535 | 455204eb | ths | return s;
|
536 | 455204eb | ths | } |
537 | 455204eb | ths | |
538 | 455204eb | ths | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
|
539 | 455204eb | ths | { |
540 | 455204eb | ths | QEMUPutMouseEntry *prev = NULL, *cursor;
|
541 | 455204eb | ths | |
542 | 455204eb | ths | if (!qemu_put_mouse_event_head || entry == NULL) |
543 | 455204eb | ths | return;
|
544 | 455204eb | ths | |
545 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
546 | 455204eb | ths | while (cursor != NULL && cursor != entry) { |
547 | 455204eb | ths | prev = cursor; |
548 | 455204eb | ths | cursor = cursor->next; |
549 | 455204eb | ths | } |
550 | 455204eb | ths | |
551 | 455204eb | ths | if (cursor == NULL) // does not exist or list empty |
552 | 455204eb | ths | return;
|
553 | 455204eb | ths | else if (prev == NULL) { // entry is head |
554 | 455204eb | ths | qemu_put_mouse_event_head = cursor->next; |
555 | 455204eb | ths | if (qemu_put_mouse_event_current == entry)
|
556 | 455204eb | ths | qemu_put_mouse_event_current = cursor->next; |
557 | 455204eb | ths | qemu_free(entry->qemu_put_mouse_event_name); |
558 | 455204eb | ths | qemu_free(entry); |
559 | 455204eb | ths | return;
|
560 | 455204eb | ths | } |
561 | 455204eb | ths | |
562 | 455204eb | ths | prev->next = entry->next; |
563 | 455204eb | ths | |
564 | 455204eb | ths | if (qemu_put_mouse_event_current == entry)
|
565 | 455204eb | ths | qemu_put_mouse_event_current = prev; |
566 | 455204eb | ths | |
567 | 455204eb | ths | qemu_free(entry->qemu_put_mouse_event_name); |
568 | 455204eb | ths | qemu_free(entry); |
569 | 63066f4f | bellard | } |
570 | 63066f4f | bellard | |
571 | 63066f4f | bellard | void kbd_put_keycode(int keycode) |
572 | 63066f4f | bellard | { |
573 | 63066f4f | bellard | if (qemu_put_kbd_event) {
|
574 | 63066f4f | bellard | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
575 | 63066f4f | bellard | } |
576 | 63066f4f | bellard | } |
577 | 63066f4f | bellard | |
578 | 63066f4f | bellard | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
579 | 63066f4f | bellard | { |
580 | 455204eb | ths | QEMUPutMouseEvent *mouse_event; |
581 | 455204eb | ths | void *mouse_event_opaque;
|
582 | a171fe39 | balrog | int width;
|
583 | 455204eb | ths | |
584 | 455204eb | ths | if (!qemu_put_mouse_event_current) {
|
585 | 455204eb | ths | return;
|
586 | 455204eb | ths | } |
587 | 455204eb | ths | |
588 | 455204eb | ths | mouse_event = |
589 | 455204eb | ths | qemu_put_mouse_event_current->qemu_put_mouse_event; |
590 | 455204eb | ths | mouse_event_opaque = |
591 | 455204eb | ths | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
592 | 455204eb | ths | |
593 | 455204eb | ths | if (mouse_event) {
|
594 | a171fe39 | balrog | if (graphic_rotate) {
|
595 | a171fe39 | balrog | if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
|
596 | a171fe39 | balrog | width = 0x7fff;
|
597 | a171fe39 | balrog | else
|
598 | b94ed577 | aurel32 | width = graphic_width - 1;
|
599 | a171fe39 | balrog | mouse_event(mouse_event_opaque, |
600 | a171fe39 | balrog | width - dy, dx, dz, buttons_state); |
601 | a171fe39 | balrog | } else
|
602 | a171fe39 | balrog | mouse_event(mouse_event_opaque, |
603 | a171fe39 | balrog | dx, dy, dz, buttons_state); |
604 | 63066f4f | bellard | } |
605 | 63066f4f | bellard | } |
606 | 63066f4f | bellard | |
607 | 09b26c5e | bellard | int kbd_mouse_is_absolute(void) |
608 | 09b26c5e | bellard | { |
609 | 455204eb | ths | if (!qemu_put_mouse_event_current)
|
610 | 455204eb | ths | return 0; |
611 | 455204eb | ths | |
612 | 455204eb | ths | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
|
613 | 455204eb | ths | } |
614 | 455204eb | ths | |
615 | 455204eb | ths | void do_info_mice(void) |
616 | 455204eb | ths | { |
617 | 455204eb | ths | QEMUPutMouseEntry *cursor; |
618 | 455204eb | ths | int index = 0; |
619 | 455204eb | ths | |
620 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
621 | 455204eb | ths | term_printf("No mouse devices connected\n");
|
622 | 455204eb | ths | return;
|
623 | 455204eb | ths | } |
624 | 455204eb | ths | |
625 | 455204eb | ths | term_printf("Mouse devices available:\n");
|
626 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
627 | 455204eb | ths | while (cursor != NULL) { |
628 | 455204eb | ths | term_printf("%c Mouse #%d: %s\n",
|
629 | 455204eb | ths | (cursor == qemu_put_mouse_event_current ? '*' : ' '), |
630 | 455204eb | ths | index, cursor->qemu_put_mouse_event_name); |
631 | 455204eb | ths | index++; |
632 | 455204eb | ths | cursor = cursor->next; |
633 | 455204eb | ths | } |
634 | 455204eb | ths | } |
635 | 455204eb | ths | |
636 | 455204eb | ths | void do_mouse_set(int index) |
637 | 455204eb | ths | { |
638 | 455204eb | ths | QEMUPutMouseEntry *cursor; |
639 | 455204eb | ths | int i = 0; |
640 | 455204eb | ths | |
641 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
642 | 455204eb | ths | term_printf("No mouse devices connected\n");
|
643 | 455204eb | ths | return;
|
644 | 455204eb | ths | } |
645 | 455204eb | ths | |
646 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
647 | 455204eb | ths | while (cursor != NULL && index != i) { |
648 | 455204eb | ths | i++; |
649 | 455204eb | ths | cursor = cursor->next; |
650 | 455204eb | ths | } |
651 | 455204eb | ths | |
652 | 455204eb | ths | if (cursor != NULL) |
653 | 455204eb | ths | qemu_put_mouse_event_current = cursor; |
654 | 455204eb | ths | else
|
655 | 455204eb | ths | term_printf("Mouse at given index not found\n");
|
656 | 09b26c5e | bellard | } |
657 | 09b26c5e | bellard | |
658 | 1dce7c3c | bellard | /* compute with 96 bit intermediate result: (a*b)/c */
|
659 | 1dce7c3c | bellard | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
660 | 0824d6fc | bellard | { |
661 | 1dce7c3c | bellard | union {
|
662 | 1dce7c3c | bellard | uint64_t ll; |
663 | 1dce7c3c | bellard | struct {
|
664 | 1dce7c3c | bellard | #ifdef WORDS_BIGENDIAN
|
665 | 1dce7c3c | bellard | uint32_t high, low; |
666 | 1dce7c3c | bellard | #else
|
667 | 1dce7c3c | bellard | uint32_t low, high; |
668 | 3b46e624 | ths | #endif
|
669 | 1dce7c3c | bellard | } l; |
670 | 1dce7c3c | bellard | } u, res; |
671 | 1dce7c3c | bellard | uint64_t rl, rh; |
672 | 0824d6fc | bellard | |
673 | 1dce7c3c | bellard | u.ll = a; |
674 | 1dce7c3c | bellard | rl = (uint64_t)u.l.low * (uint64_t)b; |
675 | 1dce7c3c | bellard | rh = (uint64_t)u.l.high * (uint64_t)b; |
676 | 1dce7c3c | bellard | rh += (rl >> 32);
|
677 | 1dce7c3c | bellard | res.l.high = rh / c; |
678 | 1dce7c3c | bellard | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
679 | 1dce7c3c | bellard | return res.ll;
|
680 | 34865134 | bellard | } |
681 | 34865134 | bellard | |
682 | 1dce7c3c | bellard | /***********************************************************/
|
683 | 1dce7c3c | bellard | /* real time host monotonic timer */
|
684 | 34865134 | bellard | |
685 | 1dce7c3c | bellard | #define QEMU_TIMER_BASE 1000000000LL |
686 | 34865134 | bellard | |
687 | 1dce7c3c | bellard | #ifdef WIN32
|
688 | 0824d6fc | bellard | |
689 | 1dce7c3c | bellard | static int64_t clock_freq;
|
690 | 1115dde7 | bellard | |
691 | 1dce7c3c | bellard | static void init_get_clock(void) |
692 | 1115dde7 | bellard | { |
693 | a8e5ac33 | bellard | LARGE_INTEGER freq; |
694 | a8e5ac33 | bellard | int ret;
|
695 | 1dce7c3c | bellard | ret = QueryPerformanceFrequency(&freq); |
696 | 1dce7c3c | bellard | if (ret == 0) { |
697 | 1dce7c3c | bellard | fprintf(stderr, "Could not calibrate ticks\n");
|
698 | 1dce7c3c | bellard | exit(1);
|
699 | 1dce7c3c | bellard | } |
700 | 1dce7c3c | bellard | clock_freq = freq.QuadPart; |
701 | 1115dde7 | bellard | } |
702 | 1115dde7 | bellard | |
703 | 1dce7c3c | bellard | static int64_t get_clock(void) |
704 | b8076a74 | bellard | { |
705 | 1dce7c3c | bellard | LARGE_INTEGER ti; |
706 | 1dce7c3c | bellard | QueryPerformanceCounter(&ti); |
707 | 1dce7c3c | bellard | return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
|
708 | b8076a74 | bellard | } |
709 | b8076a74 | bellard | |
710 | 1dce7c3c | bellard | #else
|
711 | 90cb9493 | bellard | |
712 | 1dce7c3c | bellard | static int use_rt_clock; |
713 | 1dce7c3c | bellard | |
714 | 1dce7c3c | bellard | static void init_get_clock(void) |
715 | 90cb9493 | bellard | { |
716 | 1dce7c3c | bellard | use_rt_clock = 0;
|
717 | 1dce7c3c | bellard | #if defined(__linux__)
|
718 | 1dce7c3c | bellard | { |
719 | 1dce7c3c | bellard | struct timespec ts;
|
720 | 1dce7c3c | bellard | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
721 | 1dce7c3c | bellard | use_rt_clock = 1;
|
722 | 1dce7c3c | bellard | } |
723 | 1dce7c3c | bellard | } |
724 | 1dce7c3c | bellard | #endif
|
725 | 90cb9493 | bellard | } |
726 | 90cb9493 | bellard | |
727 | 1dce7c3c | bellard | static int64_t get_clock(void) |
728 | fdbb4691 | bellard | { |
729 | 1dce7c3c | bellard | #if defined(__linux__)
|
730 | 1dce7c3c | bellard | if (use_rt_clock) {
|
731 | 1dce7c3c | bellard | struct timespec ts;
|
732 | 1dce7c3c | bellard | clock_gettime(CLOCK_MONOTONIC, &ts); |
733 | 1dce7c3c | bellard | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
734 | 5fafdf24 | ths | } else
|
735 | fdbb4691 | bellard | #endif
|
736 | 1dce7c3c | bellard | { |
737 | 1dce7c3c | bellard | /* XXX: using gettimeofday leads to problems if the date
|
738 | 1dce7c3c | bellard | changes, so it should be avoided. */
|
739 | 1dce7c3c | bellard | struct timeval tv;
|
740 | 1dce7c3c | bellard | gettimeofday(&tv, NULL);
|
741 | 1dce7c3c | bellard | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
742 | 1dce7c3c | bellard | } |
743 | fdbb4691 | bellard | } |
744 | 34865134 | bellard | #endif
|
745 | 34865134 | bellard | |
746 | 2e70f6ef | pbrook | /* Return the virtual CPU time, based on the instruction counter. */
|
747 | 2e70f6ef | pbrook | static int64_t cpu_get_icount(void) |
748 | 2e70f6ef | pbrook | { |
749 | 2e70f6ef | pbrook | int64_t icount; |
750 | 2e70f6ef | pbrook | CPUState *env = cpu_single_env;; |
751 | 2e70f6ef | pbrook | icount = qemu_icount; |
752 | 2e70f6ef | pbrook | if (env) {
|
753 | 2e70f6ef | pbrook | if (!can_do_io(env))
|
754 | 2e70f6ef | pbrook | fprintf(stderr, "Bad clock read\n");
|
755 | 2e70f6ef | pbrook | icount -= (env->icount_decr.u16.low + env->icount_extra); |
756 | 2e70f6ef | pbrook | } |
757 | 2e70f6ef | pbrook | return qemu_icount_bias + (icount << icount_time_shift);
|
758 | 2e70f6ef | pbrook | } |
759 | 2e70f6ef | pbrook | |
760 | 1dce7c3c | bellard | /***********************************************************/
|
761 | 1dce7c3c | bellard | /* guest cycle counter */
|
762 | 1dce7c3c | bellard | |
763 | eade0f19 | bellard | static int64_t cpu_ticks_prev;
|
764 | 34865134 | bellard | static int64_t cpu_ticks_offset;
|
765 | 1dce7c3c | bellard | static int64_t cpu_clock_offset;
|
766 | 8a7ddc38 | bellard | static int cpu_ticks_enabled; |
767 | 34865134 | bellard | |
768 | 1dce7c3c | bellard | /* return the host CPU cycle counter and handle stop/restart */
|
769 | 1dce7c3c | bellard | int64_t cpu_get_ticks(void)
|
770 | 34865134 | bellard | { |
771 | 2e70f6ef | pbrook | if (use_icount) {
|
772 | 2e70f6ef | pbrook | return cpu_get_icount();
|
773 | 2e70f6ef | pbrook | } |
774 | 8a7ddc38 | bellard | if (!cpu_ticks_enabled) {
|
775 | 8a7ddc38 | bellard | return cpu_ticks_offset;
|
776 | 8a7ddc38 | bellard | } else {
|
777 | eade0f19 | bellard | int64_t ticks; |
778 | eade0f19 | bellard | ticks = cpu_get_real_ticks(); |
779 | eade0f19 | bellard | if (cpu_ticks_prev > ticks) {
|
780 | eade0f19 | bellard | /* Note: non increasing ticks may happen if the host uses
|
781 | eade0f19 | bellard | software suspend */
|
782 | eade0f19 | bellard | cpu_ticks_offset += cpu_ticks_prev - ticks; |
783 | eade0f19 | bellard | } |
784 | eade0f19 | bellard | cpu_ticks_prev = ticks; |
785 | eade0f19 | bellard | return ticks + cpu_ticks_offset;
|
786 | 8a7ddc38 | bellard | } |
787 | 34865134 | bellard | } |
788 | 34865134 | bellard | |
789 | 1dce7c3c | bellard | /* return the host CPU monotonic timer and handle stop/restart */
|
790 | 1dce7c3c | bellard | static int64_t cpu_get_clock(void) |
791 | 1dce7c3c | bellard | { |
792 | 1dce7c3c | bellard | int64_t ti; |
793 | 1dce7c3c | bellard | if (!cpu_ticks_enabled) {
|
794 | 1dce7c3c | bellard | return cpu_clock_offset;
|
795 | 1dce7c3c | bellard | } else {
|
796 | 1dce7c3c | bellard | ti = get_clock(); |
797 | 1dce7c3c | bellard | return ti + cpu_clock_offset;
|
798 | 1dce7c3c | bellard | } |
799 | 1dce7c3c | bellard | } |
800 | 1dce7c3c | bellard | |
801 | 34865134 | bellard | /* enable cpu_get_ticks() */
|
802 | 34865134 | bellard | void cpu_enable_ticks(void) |
803 | 34865134 | bellard | { |
804 | 8a7ddc38 | bellard | if (!cpu_ticks_enabled) {
|
805 | 8a7ddc38 | bellard | cpu_ticks_offset -= cpu_get_real_ticks(); |
806 | 1dce7c3c | bellard | cpu_clock_offset -= get_clock(); |
807 | 8a7ddc38 | bellard | cpu_ticks_enabled = 1;
|
808 | 8a7ddc38 | bellard | } |
809 | 34865134 | bellard | } |
810 | 34865134 | bellard | |
811 | 34865134 | bellard | /* disable cpu_get_ticks() : the clock is stopped. You must not call
|
812 | 34865134 | bellard | cpu_get_ticks() after that. */
|
813 | 34865134 | bellard | void cpu_disable_ticks(void) |
814 | 34865134 | bellard | { |
815 | 8a7ddc38 | bellard | if (cpu_ticks_enabled) {
|
816 | 8a7ddc38 | bellard | cpu_ticks_offset = cpu_get_ticks(); |
817 | 1dce7c3c | bellard | cpu_clock_offset = cpu_get_clock(); |
818 | 8a7ddc38 | bellard | cpu_ticks_enabled = 0;
|
819 | 8a7ddc38 | bellard | } |
820 | 34865134 | bellard | } |
821 | 34865134 | bellard | |
822 | 1dce7c3c | bellard | /***********************************************************/
|
823 | 1dce7c3c | bellard | /* timers */
|
824 | 5fafdf24 | ths | |
825 | 8a7ddc38 | bellard | #define QEMU_TIMER_REALTIME 0 |
826 | 8a7ddc38 | bellard | #define QEMU_TIMER_VIRTUAL 1 |
827 | 8a7ddc38 | bellard | |
828 | 8a7ddc38 | bellard | struct QEMUClock {
|
829 | 8a7ddc38 | bellard | int type;
|
830 | 8a7ddc38 | bellard | /* XXX: add frequency */
|
831 | 8a7ddc38 | bellard | }; |
832 | 8a7ddc38 | bellard | |
833 | 8a7ddc38 | bellard | struct QEMUTimer {
|
834 | 8a7ddc38 | bellard | QEMUClock *clock; |
835 | 8a7ddc38 | bellard | int64_t expire_time; |
836 | 8a7ddc38 | bellard | QEMUTimerCB *cb; |
837 | 8a7ddc38 | bellard | void *opaque;
|
838 | 8a7ddc38 | bellard | struct QEMUTimer *next;
|
839 | 8a7ddc38 | bellard | }; |
840 | 8a7ddc38 | bellard | |
841 | c8994013 | ths | struct qemu_alarm_timer {
|
842 | c8994013 | ths | char const *name; |
843 | efe75411 | ths | unsigned int flags; |
844 | c8994013 | ths | |
845 | c8994013 | ths | int (*start)(struct qemu_alarm_timer *t); |
846 | c8994013 | ths | void (*stop)(struct qemu_alarm_timer *t); |
847 | efe75411 | ths | void (*rearm)(struct qemu_alarm_timer *t); |
848 | c8994013 | ths | void *priv;
|
849 | c8994013 | ths | }; |
850 | c8994013 | ths | |
851 | efe75411 | ths | #define ALARM_FLAG_DYNTICKS 0x1 |
852 | d5d08334 | balrog | #define ALARM_FLAG_EXPIRED 0x2 |
853 | efe75411 | ths | |
854 | efe75411 | ths | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
855 | efe75411 | ths | { |
856 | efe75411 | ths | return t->flags & ALARM_FLAG_DYNTICKS;
|
857 | efe75411 | ths | } |
858 | efe75411 | ths | |
859 | efe75411 | ths | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
860 | efe75411 | ths | { |
861 | efe75411 | ths | if (!alarm_has_dynticks(t))
|
862 | efe75411 | ths | return;
|
863 | efe75411 | ths | |
864 | efe75411 | ths | t->rearm(t); |
865 | efe75411 | ths | } |
866 | efe75411 | ths | |
867 | efe75411 | ths | /* TODO: MIN_TIMER_REARM_US should be optimized */
|
868 | efe75411 | ths | #define MIN_TIMER_REARM_US 250 |
869 | efe75411 | ths | |
870 | c8994013 | ths | static struct qemu_alarm_timer *alarm_timer; |
871 | 8a7ddc38 | bellard | |
872 | 40c3bac3 | bellard | #ifdef _WIN32
|
873 | c8994013 | ths | |
874 | c8994013 | ths | struct qemu_alarm_win32 {
|
875 | c8994013 | ths | MMRESULT timerId; |
876 | c8994013 | ths | HANDLE host_alarm; |
877 | c8994013 | ths | unsigned int period; |
878 | c8994013 | ths | } alarm_win32_data = {0, NULL, -1}; |
879 | c8994013 | ths | |
880 | c8994013 | ths | static int win32_start_timer(struct qemu_alarm_timer *t); |
881 | c8994013 | ths | static void win32_stop_timer(struct qemu_alarm_timer *t); |
882 | efe75411 | ths | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
883 | c8994013 | ths | |
884 | 40c3bac3 | bellard | #else
|
885 | c8994013 | ths | |
886 | c8994013 | ths | static int unix_start_timer(struct qemu_alarm_timer *t); |
887 | c8994013 | ths | static void unix_stop_timer(struct qemu_alarm_timer *t); |
888 | c8994013 | ths | |
889 | 231c6586 | ths | #ifdef __linux__
|
890 | 231c6586 | ths | |
891 | efe75411 | ths | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
892 | efe75411 | ths | static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
893 | efe75411 | ths | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
894 | efe75411 | ths | |
895 | c40ec5a9 | ths | static int hpet_start_timer(struct qemu_alarm_timer *t); |
896 | c40ec5a9 | ths | static void hpet_stop_timer(struct qemu_alarm_timer *t); |
897 | c40ec5a9 | ths | |
898 | c8994013 | ths | static int rtc_start_timer(struct qemu_alarm_timer *t); |
899 | c8994013 | ths | static void rtc_stop_timer(struct qemu_alarm_timer *t); |
900 | c8994013 | ths | |
901 | efe75411 | ths | #endif /* __linux__ */ |
902 | 8a7ddc38 | bellard | |
903 | c8994013 | ths | #endif /* _WIN32 */ |
904 | c8994013 | ths | |
905 | 2e70f6ef | pbrook | /* Correlation between real and virtual time is always going to be
|
906 | bf20dc07 | ths | fairly approximate, so ignore small variation.
|
907 | 2e70f6ef | pbrook | When the guest is idle real and virtual time will be aligned in
|
908 | 2e70f6ef | pbrook | the IO wait loop. */
|
909 | 2e70f6ef | pbrook | #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10) |
910 | 2e70f6ef | pbrook | |
911 | 2e70f6ef | pbrook | static void icount_adjust(void) |
912 | 2e70f6ef | pbrook | { |
913 | 2e70f6ef | pbrook | int64_t cur_time; |
914 | 2e70f6ef | pbrook | int64_t cur_icount; |
915 | 2e70f6ef | pbrook | int64_t delta; |
916 | 2e70f6ef | pbrook | static int64_t last_delta;
|
917 | 2e70f6ef | pbrook | /* If the VM is not running, then do nothing. */
|
918 | 2e70f6ef | pbrook | if (!vm_running)
|
919 | 2e70f6ef | pbrook | return;
|
920 | 2e70f6ef | pbrook | |
921 | 2e70f6ef | pbrook | cur_time = cpu_get_clock(); |
922 | 2e70f6ef | pbrook | cur_icount = qemu_get_clock(vm_clock); |
923 | 2e70f6ef | pbrook | delta = cur_icount - cur_time; |
924 | 2e70f6ef | pbrook | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
|
925 | 2e70f6ef | pbrook | if (delta > 0 |
926 | 2e70f6ef | pbrook | && last_delta + ICOUNT_WOBBLE < delta * 2
|
927 | 2e70f6ef | pbrook | && icount_time_shift > 0) {
|
928 | 2e70f6ef | pbrook | /* The guest is getting too far ahead. Slow time down. */
|
929 | 2e70f6ef | pbrook | icount_time_shift--; |
930 | 2e70f6ef | pbrook | } |
931 | 2e70f6ef | pbrook | if (delta < 0 |
932 | 2e70f6ef | pbrook | && last_delta - ICOUNT_WOBBLE > delta * 2
|
933 | 2e70f6ef | pbrook | && icount_time_shift < MAX_ICOUNT_SHIFT) { |
934 | 2e70f6ef | pbrook | /* The guest is getting too far behind. Speed time up. */
|
935 | 2e70f6ef | pbrook | icount_time_shift++; |
936 | 2e70f6ef | pbrook | } |
937 | 2e70f6ef | pbrook | last_delta = delta; |
938 | 2e70f6ef | pbrook | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
939 | 2e70f6ef | pbrook | } |
940 | 2e70f6ef | pbrook | |
941 | 2e70f6ef | pbrook | static void icount_adjust_rt(void * opaque) |
942 | 2e70f6ef | pbrook | { |
943 | 2e70f6ef | pbrook | qemu_mod_timer(icount_rt_timer, |
944 | 2e70f6ef | pbrook | qemu_get_clock(rt_clock) + 1000);
|
945 | 2e70f6ef | pbrook | icount_adjust(); |
946 | 2e70f6ef | pbrook | } |
947 | 2e70f6ef | pbrook | |
948 | 2e70f6ef | pbrook | static void icount_adjust_vm(void * opaque) |
949 | 2e70f6ef | pbrook | { |
950 | 2e70f6ef | pbrook | qemu_mod_timer(icount_vm_timer, |
951 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
|
952 | 2e70f6ef | pbrook | icount_adjust(); |
953 | 2e70f6ef | pbrook | } |
954 | 2e70f6ef | pbrook | |
955 | 2e70f6ef | pbrook | static void init_icount_adjust(void) |
956 | 2e70f6ef | pbrook | { |
957 | 2e70f6ef | pbrook | /* Have both realtime and virtual time triggers for speed adjustment.
|
958 | 2e70f6ef | pbrook | The realtime trigger catches emulated time passing too slowly,
|
959 | 2e70f6ef | pbrook | the virtual time trigger catches emulated time passing too fast.
|
960 | 2e70f6ef | pbrook | Realtime triggers occur even when idle, so use them less frequently
|
961 | 2e70f6ef | pbrook | than VM triggers. */
|
962 | 2e70f6ef | pbrook | icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
|
963 | 2e70f6ef | pbrook | qemu_mod_timer(icount_rt_timer, |
964 | 2e70f6ef | pbrook | qemu_get_clock(rt_clock) + 1000);
|
965 | 2e70f6ef | pbrook | icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
|
966 | 2e70f6ef | pbrook | qemu_mod_timer(icount_vm_timer, |
967 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
|
968 | 2e70f6ef | pbrook | } |
969 | 2e70f6ef | pbrook | |
970 | c8994013 | ths | static struct qemu_alarm_timer alarm_timers[] = { |
971 | efe75411 | ths | #ifndef _WIN32
|
972 | 231c6586 | ths | #ifdef __linux__
|
973 | efe75411 | ths | {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
|
974 | efe75411 | ths | dynticks_stop_timer, dynticks_rearm_timer, NULL},
|
975 | c40ec5a9 | ths | /* HPET - if available - is preferred */
|
976 | efe75411 | ths | {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
977 | c40ec5a9 | ths | /* ...otherwise try RTC */
|
978 | efe75411 | ths | {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
979 | c8994013 | ths | #endif
|
980 | efe75411 | ths | {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
981 | c8994013 | ths | #else
|
982 | efe75411 | ths | {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
|
983 | efe75411 | ths | win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
984 | efe75411 | ths | {"win32", 0, win32_start_timer, |
985 | efe75411 | ths | win32_stop_timer, NULL, &alarm_win32_data},
|
986 | c8994013 | ths | #endif
|
987 | c8994013 | ths | {NULL, }
|
988 | c8994013 | ths | }; |
989 | c8994013 | ths | |
990 | 3f47aa8c | blueswir1 | static void show_available_alarms(void) |
991 | f3dcfada | ths | { |
992 | f3dcfada | ths | int i;
|
993 | f3dcfada | ths | |
994 | f3dcfada | ths | printf("Available alarm timers, in order of precedence:\n");
|
995 | f3dcfada | ths | for (i = 0; alarm_timers[i].name; i++) |
996 | f3dcfada | ths | printf("%s\n", alarm_timers[i].name);
|
997 | f3dcfada | ths | } |
998 | f3dcfada | ths | |
999 | f3dcfada | ths | static void configure_alarms(char const *opt) |
1000 | f3dcfada | ths | { |
1001 | f3dcfada | ths | int i;
|
1002 | f3dcfada | ths | int cur = 0; |
1003 | f3dcfada | ths | int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1; |
1004 | f3dcfada | ths | char *arg;
|
1005 | f3dcfada | ths | char *name;
|
1006 | 2e70f6ef | pbrook | struct qemu_alarm_timer tmp;
|
1007 | f3dcfada | ths | |
1008 | 3adda04c | aurel32 | if (!strcmp(opt, "?")) { |
1009 | f3dcfada | ths | show_available_alarms(); |
1010 | f3dcfada | ths | exit(0);
|
1011 | f3dcfada | ths | } |
1012 | f3dcfada | ths | |
1013 | f3dcfada | ths | arg = strdup(opt); |
1014 | f3dcfada | ths | |
1015 | f3dcfada | ths | /* Reorder the array */
|
1016 | f3dcfada | ths | name = strtok(arg, ",");
|
1017 | f3dcfada | ths | while (name) {
|
1018 | e2b577e5 | balrog | for (i = 0; i < count && alarm_timers[i].name; i++) { |
1019 | f3dcfada | ths | if (!strcmp(alarm_timers[i].name, name))
|
1020 | f3dcfada | ths | break;
|
1021 | f3dcfada | ths | } |
1022 | f3dcfada | ths | |
1023 | f3dcfada | ths | if (i == count) {
|
1024 | f3dcfada | ths | fprintf(stderr, "Unknown clock %s\n", name);
|
1025 | f3dcfada | ths | goto next;
|
1026 | f3dcfada | ths | } |
1027 | f3dcfada | ths | |
1028 | f3dcfada | ths | if (i < cur)
|
1029 | f3dcfada | ths | /* Ignore */
|
1030 | f3dcfada | ths | goto next;
|
1031 | f3dcfada | ths | |
1032 | f3dcfada | ths | /* Swap */
|
1033 | f3dcfada | ths | tmp = alarm_timers[i]; |
1034 | f3dcfada | ths | alarm_timers[i] = alarm_timers[cur]; |
1035 | f3dcfada | ths | alarm_timers[cur] = tmp; |
1036 | f3dcfada | ths | |
1037 | f3dcfada | ths | cur++; |
1038 | f3dcfada | ths | next:
|
1039 | f3dcfada | ths | name = strtok(NULL, ","); |
1040 | f3dcfada | ths | } |
1041 | f3dcfada | ths | |
1042 | f3dcfada | ths | free(arg); |
1043 | f3dcfada | ths | |
1044 | f3dcfada | ths | if (cur) {
|
1045 | 2e70f6ef | pbrook | /* Disable remaining timers */
|
1046 | f3dcfada | ths | for (i = cur; i < count; i++)
|
1047 | f3dcfada | ths | alarm_timers[i].name = NULL;
|
1048 | 3adda04c | aurel32 | } else {
|
1049 | 3adda04c | aurel32 | show_available_alarms(); |
1050 | 3adda04c | aurel32 | exit(1);
|
1051 | f3dcfada | ths | } |
1052 | f3dcfada | ths | } |
1053 | f3dcfada | ths | |
1054 | c8994013 | ths | QEMUClock *rt_clock; |
1055 | c8994013 | ths | QEMUClock *vm_clock; |
1056 | c8994013 | ths | |
1057 | c8994013 | ths | static QEMUTimer *active_timers[2]; |
1058 | c8994013 | ths | |
1059 | 9596ebb7 | pbrook | static QEMUClock *qemu_new_clock(int type) |
1060 | 8a7ddc38 | bellard | { |
1061 | 8a7ddc38 | bellard | QEMUClock *clock; |
1062 | 8a7ddc38 | bellard | clock = qemu_mallocz(sizeof(QEMUClock));
|
1063 | 8a7ddc38 | bellard | if (!clock)
|
1064 | 8a7ddc38 | bellard | return NULL; |
1065 | 8a7ddc38 | bellard | clock->type = type; |
1066 | 8a7ddc38 | bellard | return clock;
|
1067 | 8a7ddc38 | bellard | } |
1068 | 8a7ddc38 | bellard | |
1069 | 8a7ddc38 | bellard | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
|
1070 | 8a7ddc38 | bellard | { |
1071 | 8a7ddc38 | bellard | QEMUTimer *ts; |
1072 | 8a7ddc38 | bellard | |
1073 | 8a7ddc38 | bellard | ts = qemu_mallocz(sizeof(QEMUTimer));
|
1074 | 8a7ddc38 | bellard | ts->clock = clock; |
1075 | 8a7ddc38 | bellard | ts->cb = cb; |
1076 | 8a7ddc38 | bellard | ts->opaque = opaque; |
1077 | 8a7ddc38 | bellard | return ts;
|
1078 | 8a7ddc38 | bellard | } |
1079 | 8a7ddc38 | bellard | |
1080 | 8a7ddc38 | bellard | void qemu_free_timer(QEMUTimer *ts)
|
1081 | 8a7ddc38 | bellard | { |
1082 | 8a7ddc38 | bellard | qemu_free(ts); |
1083 | 8a7ddc38 | bellard | } |
1084 | 8a7ddc38 | bellard | |
1085 | 8a7ddc38 | bellard | /* stop a timer, but do not dealloc it */
|
1086 | 8a7ddc38 | bellard | void qemu_del_timer(QEMUTimer *ts)
|
1087 | 8a7ddc38 | bellard | { |
1088 | 8a7ddc38 | bellard | QEMUTimer **pt, *t; |
1089 | 8a7ddc38 | bellard | |
1090 | 8a7ddc38 | bellard | /* NOTE: this code must be signal safe because
|
1091 | 8a7ddc38 | bellard | qemu_timer_expired() can be called from a signal. */
|
1092 | 8a7ddc38 | bellard | pt = &active_timers[ts->clock->type]; |
1093 | 8a7ddc38 | bellard | for(;;) {
|
1094 | 8a7ddc38 | bellard | t = *pt; |
1095 | 8a7ddc38 | bellard | if (!t)
|
1096 | 8a7ddc38 | bellard | break;
|
1097 | 8a7ddc38 | bellard | if (t == ts) {
|
1098 | 8a7ddc38 | bellard | *pt = t->next; |
1099 | 8a7ddc38 | bellard | break;
|
1100 | 8a7ddc38 | bellard | } |
1101 | 8a7ddc38 | bellard | pt = &t->next; |
1102 | 8a7ddc38 | bellard | } |
1103 | 8a7ddc38 | bellard | } |
1104 | 8a7ddc38 | bellard | |
1105 | 8a7ddc38 | bellard | /* modify the current timer so that it will be fired when current_time
|
1106 | 8a7ddc38 | bellard | >= expire_time. The corresponding callback will be called. */
|
1107 | 8a7ddc38 | bellard | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
|
1108 | 8a7ddc38 | bellard | { |
1109 | 8a7ddc38 | bellard | QEMUTimer **pt, *t; |
1110 | 8a7ddc38 | bellard | |
1111 | 8a7ddc38 | bellard | qemu_del_timer(ts); |
1112 | 8a7ddc38 | bellard | |
1113 | 8a7ddc38 | bellard | /* add the timer in the sorted list */
|
1114 | 8a7ddc38 | bellard | /* NOTE: this code must be signal safe because
|
1115 | 8a7ddc38 | bellard | qemu_timer_expired() can be called from a signal. */
|
1116 | 8a7ddc38 | bellard | pt = &active_timers[ts->clock->type]; |
1117 | 8a7ddc38 | bellard | for(;;) {
|
1118 | 8a7ddc38 | bellard | t = *pt; |
1119 | 8a7ddc38 | bellard | if (!t)
|
1120 | 8a7ddc38 | bellard | break;
|
1121 | 5fafdf24 | ths | if (t->expire_time > expire_time)
|
1122 | 8a7ddc38 | bellard | break;
|
1123 | 8a7ddc38 | bellard | pt = &t->next; |
1124 | 8a7ddc38 | bellard | } |
1125 | 8a7ddc38 | bellard | ts->expire_time = expire_time; |
1126 | 8a7ddc38 | bellard | ts->next = *pt; |
1127 | 8a7ddc38 | bellard | *pt = ts; |
1128 | d5d08334 | balrog | |
1129 | d5d08334 | balrog | /* Rearm if necessary */
|
1130 | 2e70f6ef | pbrook | if (pt == &active_timers[ts->clock->type]) {
|
1131 | 2e70f6ef | pbrook | if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { |
1132 | 2e70f6ef | pbrook | qemu_rearm_alarm_timer(alarm_timer); |
1133 | 2e70f6ef | pbrook | } |
1134 | 2e70f6ef | pbrook | /* Interrupt execution to force deadline recalculation. */
|
1135 | 2e70f6ef | pbrook | if (use_icount && cpu_single_env) {
|
1136 | 2e70f6ef | pbrook | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
1137 | 2e70f6ef | pbrook | } |
1138 | 2e70f6ef | pbrook | } |
1139 | 8a7ddc38 | bellard | } |
1140 | 8a7ddc38 | bellard | |
1141 | 8a7ddc38 | bellard | int qemu_timer_pending(QEMUTimer *ts)
|
1142 | 8a7ddc38 | bellard | { |
1143 | 8a7ddc38 | bellard | QEMUTimer *t; |
1144 | 8a7ddc38 | bellard | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
1145 | 8a7ddc38 | bellard | if (t == ts)
|
1146 | 8a7ddc38 | bellard | return 1; |
1147 | 8a7ddc38 | bellard | } |
1148 | 8a7ddc38 | bellard | return 0; |
1149 | 8a7ddc38 | bellard | } |
1150 | 8a7ddc38 | bellard | |
1151 | 8a7ddc38 | bellard | static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
1152 | 8a7ddc38 | bellard | { |
1153 | 8a7ddc38 | bellard | if (!timer_head)
|
1154 | 8a7ddc38 | bellard | return 0; |
1155 | 8a7ddc38 | bellard | return (timer_head->expire_time <= current_time);
|
1156 | 8a7ddc38 | bellard | } |
1157 | 8a7ddc38 | bellard | |
1158 | 8a7ddc38 | bellard | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
1159 | 8a7ddc38 | bellard | { |
1160 | 8a7ddc38 | bellard | QEMUTimer *ts; |
1161 | 3b46e624 | ths | |
1162 | 8a7ddc38 | bellard | for(;;) {
|
1163 | 8a7ddc38 | bellard | ts = *ptimer_head; |
1164 | e95c8d51 | bellard | if (!ts || ts->expire_time > current_time)
|
1165 | 8a7ddc38 | bellard | break;
|
1166 | 8a7ddc38 | bellard | /* remove timer from the list before calling the callback */
|
1167 | 8a7ddc38 | bellard | *ptimer_head = ts->next; |
1168 | 8a7ddc38 | bellard | ts->next = NULL;
|
1169 | 3b46e624 | ths | |
1170 | 8a7ddc38 | bellard | /* run the callback (the timer list can be modified) */
|
1171 | 8a7ddc38 | bellard | ts->cb(ts->opaque); |
1172 | 8a7ddc38 | bellard | } |
1173 | 8a7ddc38 | bellard | } |
1174 | 8a7ddc38 | bellard | |
1175 | 8a7ddc38 | bellard | int64_t qemu_get_clock(QEMUClock *clock) |
1176 | 8a7ddc38 | bellard | { |
1177 | 8a7ddc38 | bellard | switch(clock->type) {
|
1178 | 8a7ddc38 | bellard | case QEMU_TIMER_REALTIME:
|
1179 | 1dce7c3c | bellard | return get_clock() / 1000000; |
1180 | 8a7ddc38 | bellard | default:
|
1181 | 8a7ddc38 | bellard | case QEMU_TIMER_VIRTUAL:
|
1182 | 2e70f6ef | pbrook | if (use_icount) {
|
1183 | 2e70f6ef | pbrook | return cpu_get_icount();
|
1184 | 2e70f6ef | pbrook | } else {
|
1185 | 2e70f6ef | pbrook | return cpu_get_clock();
|
1186 | 2e70f6ef | pbrook | } |
1187 | 8a7ddc38 | bellard | } |
1188 | 8a7ddc38 | bellard | } |
1189 | 8a7ddc38 | bellard | |
1190 | 1dce7c3c | bellard | static void init_timers(void) |
1191 | 1dce7c3c | bellard | { |
1192 | 1dce7c3c | bellard | init_get_clock(); |
1193 | 1dce7c3c | bellard | ticks_per_sec = QEMU_TIMER_BASE; |
1194 | 1dce7c3c | bellard | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
1195 | 1dce7c3c | bellard | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
1196 | 1dce7c3c | bellard | } |
1197 | 1dce7c3c | bellard | |
1198 | 8a7ddc38 | bellard | /* save a timer */
|
1199 | 8a7ddc38 | bellard | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
|
1200 | 8a7ddc38 | bellard | { |
1201 | 8a7ddc38 | bellard | uint64_t expire_time; |
1202 | 8a7ddc38 | bellard | |
1203 | 8a7ddc38 | bellard | if (qemu_timer_pending(ts)) {
|
1204 | 8a7ddc38 | bellard | expire_time = ts->expire_time; |
1205 | 8a7ddc38 | bellard | } else {
|
1206 | 8a7ddc38 | bellard | expire_time = -1;
|
1207 | 8a7ddc38 | bellard | } |
1208 | 8a7ddc38 | bellard | qemu_put_be64(f, expire_time); |
1209 | 8a7ddc38 | bellard | } |
1210 | 8a7ddc38 | bellard | |
1211 | 8a7ddc38 | bellard | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
|
1212 | 8a7ddc38 | bellard | { |
1213 | 8a7ddc38 | bellard | uint64_t expire_time; |
1214 | 8a7ddc38 | bellard | |
1215 | 8a7ddc38 | bellard | expire_time = qemu_get_be64(f); |
1216 | 8a7ddc38 | bellard | if (expire_time != -1) { |
1217 | 8a7ddc38 | bellard | qemu_mod_timer(ts, expire_time); |
1218 | 8a7ddc38 | bellard | } else {
|
1219 | 8a7ddc38 | bellard | qemu_del_timer(ts); |
1220 | 8a7ddc38 | bellard | } |
1221 | 8a7ddc38 | bellard | } |
1222 | 8a7ddc38 | bellard | |
1223 | 8a7ddc38 | bellard | static void timer_save(QEMUFile *f, void *opaque) |
1224 | 8a7ddc38 | bellard | { |
1225 | 8a7ddc38 | bellard | if (cpu_ticks_enabled) {
|
1226 | 8a7ddc38 | bellard | hw_error("cannot save state if virtual timers are running");
|
1227 | 8a7ddc38 | bellard | } |
1228 | bee8d684 | ths | qemu_put_be64(f, cpu_ticks_offset); |
1229 | bee8d684 | ths | qemu_put_be64(f, ticks_per_sec); |
1230 | bee8d684 | ths | qemu_put_be64(f, cpu_clock_offset); |
1231 | 8a7ddc38 | bellard | } |
1232 | 8a7ddc38 | bellard | |
1233 | 8a7ddc38 | bellard | static int timer_load(QEMUFile *f, void *opaque, int version_id) |
1234 | 8a7ddc38 | bellard | { |
1235 | c88676f8 | bellard | if (version_id != 1 && version_id != 2) |
1236 | 8a7ddc38 | bellard | return -EINVAL;
|
1237 | 8a7ddc38 | bellard | if (cpu_ticks_enabled) {
|
1238 | 8a7ddc38 | bellard | return -EINVAL;
|
1239 | 8a7ddc38 | bellard | } |
1240 | bee8d684 | ths | cpu_ticks_offset=qemu_get_be64(f); |
1241 | bee8d684 | ths | ticks_per_sec=qemu_get_be64(f); |
1242 | c88676f8 | bellard | if (version_id == 2) { |
1243 | bee8d684 | ths | cpu_clock_offset=qemu_get_be64(f); |
1244 | c88676f8 | bellard | } |
1245 | 8a7ddc38 | bellard | return 0; |
1246 | 8a7ddc38 | bellard | } |
1247 | 8a7ddc38 | bellard | |
1248 | 67b915a5 | bellard | #ifdef _WIN32
|
1249 | 5fafdf24 | ths | void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
|
1250 | 67b915a5 | bellard | DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
1251 | 67b915a5 | bellard | #else
|
1252 | 8a7ddc38 | bellard | static void host_alarm_handler(int host_signum) |
1253 | 67b915a5 | bellard | #endif
|
1254 | 8a7ddc38 | bellard | { |
1255 | 02ba45c5 | bellard | #if 0
|
1256 | 02ba45c5 | bellard | #define DISP_FREQ 1000
|
1257 | 02ba45c5 | bellard | {
|
1258 | 02ba45c5 | bellard | static int64_t delta_min = INT64_MAX;
|
1259 | 02ba45c5 | bellard | static int64_t delta_max, delta_cum, last_clock, delta, ti;
|
1260 | 02ba45c5 | bellard | static int count;
|
1261 | 02ba45c5 | bellard | ti = qemu_get_clock(vm_clock);
|
1262 | 02ba45c5 | bellard | if (last_clock != 0) {
|
1263 | 02ba45c5 | bellard | delta = ti - last_clock;
|
1264 | 02ba45c5 | bellard | if (delta < delta_min)
|
1265 | 02ba45c5 | bellard | delta_min = delta;
|
1266 | 02ba45c5 | bellard | if (delta > delta_max)
|
1267 | 02ba45c5 | bellard | delta_max = delta;
|
1268 | 02ba45c5 | bellard | delta_cum += delta;
|
1269 | 02ba45c5 | bellard | if (++count == DISP_FREQ) {
|
1270 | 26a76461 | bellard | printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
|
1271 | 02ba45c5 | bellard | muldiv64(delta_min, 1000000, ticks_per_sec),
|
1272 | 02ba45c5 | bellard | muldiv64(delta_max, 1000000, ticks_per_sec),
|
1273 | 02ba45c5 | bellard | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
|
1274 | 02ba45c5 | bellard | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
|
1275 | 02ba45c5 | bellard | count = 0;
|
1276 | 02ba45c5 | bellard | delta_min = INT64_MAX;
|
1277 | 02ba45c5 | bellard | delta_max = 0;
|
1278 | 02ba45c5 | bellard | delta_cum = 0;
|
1279 | 02ba45c5 | bellard | }
|
1280 | 02ba45c5 | bellard | }
|
1281 | 02ba45c5 | bellard | last_clock = ti;
|
1282 | 02ba45c5 | bellard | }
|
1283 | 02ba45c5 | bellard | #endif
|
1284 | efe75411 | ths | if (alarm_has_dynticks(alarm_timer) ||
|
1285 | 2e70f6ef | pbrook | (!use_icount && |
1286 | 2e70f6ef | pbrook | qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
1287 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock))) || |
1288 | 8a7ddc38 | bellard | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
1289 | 8a7ddc38 | bellard | qemu_get_clock(rt_clock))) { |
1290 | 06d9f2f7 | bellard | #ifdef _WIN32
|
1291 | c8994013 | ths | struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv; |
1292 | c8994013 | ths | SetEvent(data->host_alarm); |
1293 | 06d9f2f7 | bellard | #endif
|
1294 | ee5605e5 | balrog | CPUState *env = next_cpu; |
1295 | ee5605e5 | balrog | |
1296 | d5d08334 | balrog | alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
1297 | d5d08334 | balrog | |
1298 | 4f8eb8da | balrog | if (env) {
|
1299 | 4f8eb8da | balrog | /* stop the currently executing cpu because a timer occured */
|
1300 | 4f8eb8da | balrog | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
1301 | a332e112 | bellard | #ifdef USE_KQEMU
|
1302 | 4f8eb8da | balrog | if (env->kqemu_enabled) {
|
1303 | 4f8eb8da | balrog | kqemu_cpu_interrupt(env); |
1304 | 4f8eb8da | balrog | } |
1305 | ee5605e5 | balrog | #endif
|
1306 | 4f8eb8da | balrog | } |
1307 | ee5605e5 | balrog | event_pending = 1;
|
1308 | 8a7ddc38 | bellard | } |
1309 | 8a7ddc38 | bellard | } |
1310 | 8a7ddc38 | bellard | |
1311 | 2e70f6ef | pbrook | static int64_t qemu_next_deadline(void) |
1312 | efe75411 | ths | { |
1313 | 2e70f6ef | pbrook | int64_t delta; |
1314 | efe75411 | ths | |
1315 | efe75411 | ths | if (active_timers[QEMU_TIMER_VIRTUAL]) {
|
1316 | 2e70f6ef | pbrook | delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time - |
1317 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock); |
1318 | 2e70f6ef | pbrook | } else {
|
1319 | 2e70f6ef | pbrook | /* To avoid problems with overflow limit this to 2^32. */
|
1320 | 2e70f6ef | pbrook | delta = INT32_MAX; |
1321 | efe75411 | ths | } |
1322 | efe75411 | ths | |
1323 | 2e70f6ef | pbrook | if (delta < 0) |
1324 | 2e70f6ef | pbrook | delta = 0;
|
1325 | efe75411 | ths | |
1326 | 2e70f6ef | pbrook | return delta;
|
1327 | 2e70f6ef | pbrook | } |
1328 | 2e70f6ef | pbrook | |
1329 | 2e70f6ef | pbrook | static uint64_t qemu_next_deadline_dyntick(void) |
1330 | 2e70f6ef | pbrook | { |
1331 | 2e70f6ef | pbrook | int64_t delta; |
1332 | 2e70f6ef | pbrook | int64_t rtdelta; |
1333 | 2e70f6ef | pbrook | |
1334 | 2e70f6ef | pbrook | if (use_icount)
|
1335 | 2e70f6ef | pbrook | delta = INT32_MAX; |
1336 | 2e70f6ef | pbrook | else
|
1337 | 2e70f6ef | pbrook | delta = (qemu_next_deadline() + 999) / 1000; |
1338 | 2e70f6ef | pbrook | |
1339 | 2e70f6ef | pbrook | if (active_timers[QEMU_TIMER_REALTIME]) {
|
1340 | 2e70f6ef | pbrook | rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time - |
1341 | 2e70f6ef | pbrook | qemu_get_clock(rt_clock))*1000;
|
1342 | 2e70f6ef | pbrook | if (rtdelta < delta)
|
1343 | 2e70f6ef | pbrook | delta = rtdelta; |
1344 | 2e70f6ef | pbrook | } |
1345 | 2e70f6ef | pbrook | |
1346 | 2e70f6ef | pbrook | if (delta < MIN_TIMER_REARM_US)
|
1347 | 2e70f6ef | pbrook | delta = MIN_TIMER_REARM_US; |
1348 | 2e70f6ef | pbrook | |
1349 | 2e70f6ef | pbrook | return delta;
|
1350 | efe75411 | ths | } |
1351 | efe75411 | ths | |
1352 | fd872598 | bellard | #ifndef _WIN32
|
1353 | fd872598 | bellard | |
1354 | 829309c7 | bellard | #if defined(__linux__)
|
1355 | 829309c7 | bellard | |
1356 | fd872598 | bellard | #define RTC_FREQ 1024 |
1357 | fd872598 | bellard | |
1358 | c8994013 | ths | static void enable_sigio_timer(int fd) |
1359 | c8994013 | ths | { |
1360 | c8994013 | ths | struct sigaction act;
|
1361 | c8994013 | ths | |
1362 | c8994013 | ths | /* timer signal */
|
1363 | c8994013 | ths | sigfillset(&act.sa_mask); |
1364 | c8994013 | ths | act.sa_flags = 0;
|
1365 | c8994013 | ths | act.sa_handler = host_alarm_handler; |
1366 | c8994013 | ths | |
1367 | c8994013 | ths | sigaction(SIGIO, &act, NULL);
|
1368 | c8994013 | ths | fcntl(fd, F_SETFL, O_ASYNC); |
1369 | c8994013 | ths | fcntl(fd, F_SETOWN, getpid()); |
1370 | c8994013 | ths | } |
1371 | 829309c7 | bellard | |
1372 | c40ec5a9 | ths | static int hpet_start_timer(struct qemu_alarm_timer *t) |
1373 | c40ec5a9 | ths | { |
1374 | c40ec5a9 | ths | struct hpet_info info;
|
1375 | c40ec5a9 | ths | int r, fd;
|
1376 | c40ec5a9 | ths | |
1377 | c40ec5a9 | ths | fd = open("/dev/hpet", O_RDONLY);
|
1378 | c40ec5a9 | ths | if (fd < 0) |
1379 | c40ec5a9 | ths | return -1; |
1380 | c40ec5a9 | ths | |
1381 | c40ec5a9 | ths | /* Set frequency */
|
1382 | c40ec5a9 | ths | r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
1383 | c40ec5a9 | ths | if (r < 0) { |
1384 | c40ec5a9 | ths | fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
|
1385 | c40ec5a9 | ths | "error, but for better emulation accuracy type:\n"
|
1386 | c40ec5a9 | ths | "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
|
1387 | c40ec5a9 | ths | goto fail;
|
1388 | c40ec5a9 | ths | } |
1389 | c40ec5a9 | ths | |
1390 | c40ec5a9 | ths | /* Check capabilities */
|
1391 | c40ec5a9 | ths | r = ioctl(fd, HPET_INFO, &info); |
1392 | c40ec5a9 | ths | if (r < 0) |
1393 | c40ec5a9 | ths | goto fail;
|
1394 | c40ec5a9 | ths | |
1395 | c40ec5a9 | ths | /* Enable periodic mode */
|
1396 | c40ec5a9 | ths | r = ioctl(fd, HPET_EPI, 0);
|
1397 | c40ec5a9 | ths | if (info.hi_flags && (r < 0)) |
1398 | c40ec5a9 | ths | goto fail;
|
1399 | c40ec5a9 | ths | |
1400 | c40ec5a9 | ths | /* Enable interrupt */
|
1401 | c40ec5a9 | ths | r = ioctl(fd, HPET_IE_ON, 0);
|
1402 | c40ec5a9 | ths | if (r < 0) |
1403 | c40ec5a9 | ths | goto fail;
|
1404 | c40ec5a9 | ths | |
1405 | c40ec5a9 | ths | enable_sigio_timer(fd); |
1406 | fcdc2129 | pbrook | t->priv = (void *)(long)fd; |
1407 | c40ec5a9 | ths | |
1408 | c40ec5a9 | ths | return 0; |
1409 | c40ec5a9 | ths | fail:
|
1410 | c40ec5a9 | ths | close(fd); |
1411 | c40ec5a9 | ths | return -1; |
1412 | c40ec5a9 | ths | } |
1413 | c40ec5a9 | ths | |
1414 | c40ec5a9 | ths | static void hpet_stop_timer(struct qemu_alarm_timer *t) |
1415 | c40ec5a9 | ths | { |
1416 | fcdc2129 | pbrook | int fd = (long)t->priv; |
1417 | c40ec5a9 | ths | |
1418 | c40ec5a9 | ths | close(fd); |
1419 | c40ec5a9 | ths | } |
1420 | c40ec5a9 | ths | |
1421 | c8994013 | ths | static int rtc_start_timer(struct qemu_alarm_timer *t) |
1422 | fd872598 | bellard | { |
1423 | c8994013 | ths | int rtc_fd;
|
1424 | b5a23ad4 | balrog | unsigned long current_rtc_freq = 0; |
1425 | c8994013 | ths | |
1426 | aeb30be6 | balrog | TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
|
1427 | fd872598 | bellard | if (rtc_fd < 0) |
1428 | fd872598 | bellard | return -1; |
1429 | b5a23ad4 | balrog | ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
1430 | b5a23ad4 | balrog | if (current_rtc_freq != RTC_FREQ &&
|
1431 | b5a23ad4 | balrog | ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
|
1432 | fd872598 | bellard | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
|
1433 | fd872598 | bellard | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
|
1434 | fd872598 | bellard | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
|
1435 | fd872598 | bellard | goto fail;
|
1436 | fd872598 | bellard | } |
1437 | fd872598 | bellard | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
1438 | fd872598 | bellard | fail:
|
1439 | fd872598 | bellard | close(rtc_fd); |
1440 | fd872598 | bellard | return -1; |
1441 | fd872598 | bellard | } |
1442 | c8994013 | ths | |
1443 | c8994013 | ths | enable_sigio_timer(rtc_fd); |
1444 | c8994013 | ths | |
1445 | fcdc2129 | pbrook | t->priv = (void *)(long)rtc_fd; |
1446 | c8994013 | ths | |
1447 | fd872598 | bellard | return 0; |
1448 | fd872598 | bellard | } |
1449 | fd872598 | bellard | |
1450 | c8994013 | ths | static void rtc_stop_timer(struct qemu_alarm_timer *t) |
1451 | 829309c7 | bellard | { |
1452 | fcdc2129 | pbrook | int rtc_fd = (long)t->priv; |
1453 | c8994013 | ths | |
1454 | c8994013 | ths | close(rtc_fd); |
1455 | 829309c7 | bellard | } |
1456 | 829309c7 | bellard | |
1457 | efe75411 | ths | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
1458 | efe75411 | ths | { |
1459 | efe75411 | ths | struct sigevent ev;
|
1460 | efe75411 | ths | timer_t host_timer; |
1461 | efe75411 | ths | struct sigaction act;
|
1462 | efe75411 | ths | |
1463 | efe75411 | ths | sigfillset(&act.sa_mask); |
1464 | efe75411 | ths | act.sa_flags = 0;
|
1465 | efe75411 | ths | act.sa_handler = host_alarm_handler; |
1466 | efe75411 | ths | |
1467 | efe75411 | ths | sigaction(SIGALRM, &act, NULL);
|
1468 | efe75411 | ths | |
1469 | efe75411 | ths | ev.sigev_value.sival_int = 0;
|
1470 | efe75411 | ths | ev.sigev_notify = SIGEV_SIGNAL; |
1471 | efe75411 | ths | ev.sigev_signo = SIGALRM; |
1472 | efe75411 | ths | |
1473 | efe75411 | ths | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
|
1474 | efe75411 | ths | perror("timer_create");
|
1475 | efe75411 | ths | |
1476 | efe75411 | ths | /* disable dynticks */
|
1477 | efe75411 | ths | fprintf(stderr, "Dynamic Ticks disabled\n");
|
1478 | efe75411 | ths | |
1479 | efe75411 | ths | return -1; |
1480 | efe75411 | ths | } |
1481 | efe75411 | ths | |
1482 | efe75411 | ths | t->priv = (void *)host_timer;
|
1483 | efe75411 | ths | |
1484 | efe75411 | ths | return 0; |
1485 | efe75411 | ths | } |
1486 | efe75411 | ths | |
1487 | efe75411 | ths | static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
1488 | efe75411 | ths | { |
1489 | efe75411 | ths | timer_t host_timer = (timer_t)t->priv; |
1490 | efe75411 | ths | |
1491 | efe75411 | ths | timer_delete(host_timer); |
1492 | efe75411 | ths | } |
1493 | efe75411 | ths | |
1494 | efe75411 | ths | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
1495 | efe75411 | ths | { |
1496 | efe75411 | ths | timer_t host_timer = (timer_t)t->priv; |
1497 | efe75411 | ths | struct itimerspec timeout;
|
1498 | efe75411 | ths | int64_t nearest_delta_us = INT64_MAX; |
1499 | efe75411 | ths | int64_t current_us; |
1500 | efe75411 | ths | |
1501 | efe75411 | ths | if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1502 | efe75411 | ths | !active_timers[QEMU_TIMER_VIRTUAL]) |
1503 | d5d08334 | balrog | return;
|
1504 | efe75411 | ths | |
1505 | 2e70f6ef | pbrook | nearest_delta_us = qemu_next_deadline_dyntick(); |
1506 | efe75411 | ths | |
1507 | efe75411 | ths | /* check whether a timer is already running */
|
1508 | efe75411 | ths | if (timer_gettime(host_timer, &timeout)) {
|
1509 | efe75411 | ths | perror("gettime");
|
1510 | efe75411 | ths | fprintf(stderr, "Internal timer error: aborting\n");
|
1511 | efe75411 | ths | exit(1);
|
1512 | efe75411 | ths | } |
1513 | efe75411 | ths | current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
1514 | efe75411 | ths | if (current_us && current_us <= nearest_delta_us)
|
1515 | efe75411 | ths | return;
|
1516 | efe75411 | ths | |
1517 | efe75411 | ths | timeout.it_interval.tv_sec = 0;
|
1518 | efe75411 | ths | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
1519 | efe75411 | ths | timeout.it_value.tv_sec = nearest_delta_us / 1000000;
|
1520 | efe75411 | ths | timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
1521 | efe75411 | ths | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
1522 | efe75411 | ths | perror("settime");
|
1523 | efe75411 | ths | fprintf(stderr, "Internal timer error: aborting\n");
|
1524 | efe75411 | ths | exit(1);
|
1525 | efe75411 | ths | } |
1526 | efe75411 | ths | } |
1527 | efe75411 | ths | |
1528 | 70744b3a | ths | #endif /* defined(__linux__) */ |
1529 | 231c6586 | ths | |
1530 | c8994013 | ths | static int unix_start_timer(struct qemu_alarm_timer *t) |
1531 | c8994013 | ths | { |
1532 | c8994013 | ths | struct sigaction act;
|
1533 | c8994013 | ths | struct itimerval itv;
|
1534 | c8994013 | ths | int err;
|
1535 | c8994013 | ths | |
1536 | c8994013 | ths | /* timer signal */
|
1537 | c8994013 | ths | sigfillset(&act.sa_mask); |
1538 | c8994013 | ths | act.sa_flags = 0;
|
1539 | c8994013 | ths | act.sa_handler = host_alarm_handler; |
1540 | c8994013 | ths | |
1541 | c8994013 | ths | sigaction(SIGALRM, &act, NULL);
|
1542 | c8994013 | ths | |
1543 | c8994013 | ths | itv.it_interval.tv_sec = 0;
|
1544 | c8994013 | ths | /* for i386 kernel 2.6 to get 1 ms */
|
1545 | c8994013 | ths | itv.it_interval.tv_usec = 999;
|
1546 | c8994013 | ths | itv.it_value.tv_sec = 0;
|
1547 | c8994013 | ths | itv.it_value.tv_usec = 10 * 1000; |
1548 | c8994013 | ths | |
1549 | c8994013 | ths | err = setitimer(ITIMER_REAL, &itv, NULL);
|
1550 | c8994013 | ths | if (err)
|
1551 | c8994013 | ths | return -1; |
1552 | c8994013 | ths | |
1553 | c8994013 | ths | return 0; |
1554 | c8994013 | ths | } |
1555 | c8994013 | ths | |
1556 | c8994013 | ths | static void unix_stop_timer(struct qemu_alarm_timer *t) |
1557 | c8994013 | ths | { |
1558 | c8994013 | ths | struct itimerval itv;
|
1559 | c8994013 | ths | |
1560 | c8994013 | ths | memset(&itv, 0, sizeof(itv)); |
1561 | c8994013 | ths | setitimer(ITIMER_REAL, &itv, NULL);
|
1562 | c8994013 | ths | } |
1563 | c8994013 | ths | |
1564 | 829309c7 | bellard | #endif /* !defined(_WIN32) */ |
1565 | fd872598 | bellard | |
1566 | c8994013 | ths | #ifdef _WIN32
|
1567 | c8994013 | ths | |
1568 | c8994013 | ths | static int win32_start_timer(struct qemu_alarm_timer *t) |
1569 | c8994013 | ths | { |
1570 | c8994013 | ths | TIMECAPS tc; |
1571 | c8994013 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1572 | efe75411 | ths | UINT flags; |
1573 | c8994013 | ths | |
1574 | c8994013 | ths | data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL); |
1575 | c8994013 | ths | if (!data->host_alarm) {
|
1576 | c8994013 | ths | perror("Failed CreateEvent");
|
1577 | c396a7f0 | ths | return -1; |
1578 | c8994013 | ths | } |
1579 | c8994013 | ths | |
1580 | c8994013 | ths | memset(&tc, 0, sizeof(tc)); |
1581 | c8994013 | ths | timeGetDevCaps(&tc, sizeof(tc));
|
1582 | c8994013 | ths | |
1583 | c8994013 | ths | if (data->period < tc.wPeriodMin)
|
1584 | c8994013 | ths | data->period = tc.wPeriodMin; |
1585 | c8994013 | ths | |
1586 | c8994013 | ths | timeBeginPeriod(data->period); |
1587 | c8994013 | ths | |
1588 | efe75411 | ths | flags = TIME_CALLBACK_FUNCTION; |
1589 | efe75411 | ths | if (alarm_has_dynticks(t))
|
1590 | efe75411 | ths | flags |= TIME_ONESHOT; |
1591 | efe75411 | ths | else
|
1592 | efe75411 | ths | flags |= TIME_PERIODIC; |
1593 | efe75411 | ths | |
1594 | c8994013 | ths | data->timerId = timeSetEvent(1, // interval (ms) |
1595 | c8994013 | ths | data->period, // resolution
|
1596 | c8994013 | ths | host_alarm_handler, // function
|
1597 | c8994013 | ths | (DWORD)t, // parameter
|
1598 | efe75411 | ths | flags); |
1599 | c8994013 | ths | |
1600 | c8994013 | ths | if (!data->timerId) {
|
1601 | c8994013 | ths | perror("Failed to initialize win32 alarm timer");
|
1602 | c8994013 | ths | |
1603 | c8994013 | ths | timeEndPeriod(data->period); |
1604 | c8994013 | ths | CloseHandle(data->host_alarm); |
1605 | c8994013 | ths | return -1; |
1606 | c8994013 | ths | } |
1607 | c8994013 | ths | |
1608 | c8994013 | ths | qemu_add_wait_object(data->host_alarm, NULL, NULL); |
1609 | c8994013 | ths | |
1610 | c8994013 | ths | return 0; |
1611 | c8994013 | ths | } |
1612 | c8994013 | ths | |
1613 | c8994013 | ths | static void win32_stop_timer(struct qemu_alarm_timer *t) |
1614 | c8994013 | ths | { |
1615 | c8994013 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1616 | c8994013 | ths | |
1617 | c8994013 | ths | timeKillEvent(data->timerId); |
1618 | c8994013 | ths | timeEndPeriod(data->period); |
1619 | c8994013 | ths | |
1620 | c8994013 | ths | CloseHandle(data->host_alarm); |
1621 | c8994013 | ths | } |
1622 | c8994013 | ths | |
1623 | efe75411 | ths | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
1624 | efe75411 | ths | { |
1625 | efe75411 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1626 | efe75411 | ths | uint64_t nearest_delta_us; |
1627 | efe75411 | ths | |
1628 | efe75411 | ths | if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1629 | efe75411 | ths | !active_timers[QEMU_TIMER_VIRTUAL]) |
1630 | d5d08334 | balrog | return;
|
1631 | efe75411 | ths | |
1632 | 2e70f6ef | pbrook | nearest_delta_us = qemu_next_deadline_dyntick(); |
1633 | efe75411 | ths | nearest_delta_us /= 1000;
|
1634 | efe75411 | ths | |
1635 | efe75411 | ths | timeKillEvent(data->timerId); |
1636 | efe75411 | ths | |
1637 | efe75411 | ths | data->timerId = timeSetEvent(1,
|
1638 | efe75411 | ths | data->period, |
1639 | efe75411 | ths | host_alarm_handler, |
1640 | efe75411 | ths | (DWORD)t, |
1641 | efe75411 | ths | TIME_ONESHOT | TIME_PERIODIC); |
1642 | efe75411 | ths | |
1643 | efe75411 | ths | if (!data->timerId) {
|
1644 | efe75411 | ths | perror("Failed to re-arm win32 alarm timer");
|
1645 | efe75411 | ths | |
1646 | efe75411 | ths | timeEndPeriod(data->period); |
1647 | efe75411 | ths | CloseHandle(data->host_alarm); |
1648 | efe75411 | ths | exit(1);
|
1649 | efe75411 | ths | } |
1650 | efe75411 | ths | } |
1651 | efe75411 | ths | |
1652 | c8994013 | ths | #endif /* _WIN32 */ |
1653 | c8994013 | ths | |
1654 | 1dce7c3c | bellard | static void init_timer_alarm(void) |
1655 | 8a7ddc38 | bellard | { |
1656 | c8994013 | ths | struct qemu_alarm_timer *t;
|
1657 | c8994013 | ths | int i, err = -1; |
1658 | c8994013 | ths | |
1659 | c8994013 | ths | for (i = 0; alarm_timers[i].name; i++) { |
1660 | c8994013 | ths | t = &alarm_timers[i]; |
1661 | c8994013 | ths | |
1662 | c8994013 | ths | err = t->start(t); |
1663 | c8994013 | ths | if (!err)
|
1664 | c8994013 | ths | break;
|
1665 | 67b915a5 | bellard | } |
1666 | fd872598 | bellard | |
1667 | c8994013 | ths | if (err) {
|
1668 | c8994013 | ths | fprintf(stderr, "Unable to find any suitable alarm timer.\n");
|
1669 | c8994013 | ths | fprintf(stderr, "Terminating\n");
|
1670 | c8994013 | ths | exit(1);
|
1671 | 67b915a5 | bellard | } |
1672 | c8994013 | ths | |
1673 | c8994013 | ths | alarm_timer = t; |
1674 | 8a7ddc38 | bellard | } |
1675 | 8a7ddc38 | bellard | |
1676 | 9596ebb7 | pbrook | static void quit_timers(void) |
1677 | 40c3bac3 | bellard | { |
1678 | c8994013 | ths | alarm_timer->stop(alarm_timer); |
1679 | c8994013 | ths | alarm_timer = NULL;
|
1680 | 40c3bac3 | bellard | } |
1681 | 40c3bac3 | bellard | |
1682 | c4b1fcc0 | bellard | /***********************************************************/
|
1683 | f6503059 | balrog | /* host time/date access */
|
1684 | f6503059 | balrog | void qemu_get_timedate(struct tm *tm, int offset) |
1685 | f6503059 | balrog | { |
1686 | f6503059 | balrog | time_t ti; |
1687 | f6503059 | balrog | struct tm *ret;
|
1688 | f6503059 | balrog | |
1689 | f6503059 | balrog | time(&ti); |
1690 | f6503059 | balrog | ti += offset; |
1691 | f6503059 | balrog | if (rtc_date_offset == -1) { |
1692 | f6503059 | balrog | if (rtc_utc)
|
1693 | f6503059 | balrog | ret = gmtime(&ti); |
1694 | f6503059 | balrog | else
|
1695 | f6503059 | balrog | ret = localtime(&ti); |
1696 | f6503059 | balrog | } else {
|
1697 | f6503059 | balrog | ti -= rtc_date_offset; |
1698 | f6503059 | balrog | ret = gmtime(&ti); |
1699 | f6503059 | balrog | } |
1700 | f6503059 | balrog | |
1701 | f6503059 | balrog | memcpy(tm, ret, sizeof(struct tm)); |
1702 | f6503059 | balrog | } |
1703 | f6503059 | balrog | |
1704 | f6503059 | balrog | int qemu_timedate_diff(struct tm *tm) |
1705 | f6503059 | balrog | { |
1706 | f6503059 | balrog | time_t seconds; |
1707 | f6503059 | balrog | |
1708 | f6503059 | balrog | if (rtc_date_offset == -1) |
1709 | f6503059 | balrog | if (rtc_utc)
|
1710 | f6503059 | balrog | seconds = mktimegm(tm); |
1711 | f6503059 | balrog | else
|
1712 | f6503059 | balrog | seconds = mktime(tm); |
1713 | f6503059 | balrog | else
|
1714 | f6503059 | balrog | seconds = mktimegm(tm) + rtc_date_offset; |
1715 | f6503059 | balrog | |
1716 | f6503059 | balrog | return seconds - time(NULL); |
1717 | f6503059 | balrog | } |
1718 | f6503059 | balrog | |
1719 | f6503059 | balrog | /***********************************************************/
|
1720 | 82c643ff | bellard | /* character device */
|
1721 | 313aa567 | bellard | |
1722 | e5b0bc44 | pbrook | static void qemu_chr_event(CharDriverState *s, int event) |
1723 | e5b0bc44 | pbrook | { |
1724 | e5b0bc44 | pbrook | if (!s->chr_event)
|
1725 | e5b0bc44 | pbrook | return;
|
1726 | e5b0bc44 | pbrook | s->chr_event(s->handler_opaque, event); |
1727 | e5b0bc44 | pbrook | } |
1728 | e5b0bc44 | pbrook | |
1729 | 86e94dea | ths | static void qemu_chr_reset_bh(void *opaque) |
1730 | 86e94dea | ths | { |
1731 | 86e94dea | ths | CharDriverState *s = opaque; |
1732 | e5b0bc44 | pbrook | qemu_chr_event(s, CHR_EVENT_RESET); |
1733 | 86e94dea | ths | qemu_bh_delete(s->bh); |
1734 | 86e94dea | ths | s->bh = NULL;
|
1735 | 86e94dea | ths | } |
1736 | 86e94dea | ths | |
1737 | 86e94dea | ths | void qemu_chr_reset(CharDriverState *s)
|
1738 | 86e94dea | ths | { |
1739 | 86e94dea | ths | if (s->bh == NULL) { |
1740 | 86e94dea | ths | s->bh = qemu_bh_new(qemu_chr_reset_bh, s); |
1741 | 86e94dea | ths | qemu_bh_schedule(s->bh); |
1742 | 86e94dea | ths | } |
1743 | 86e94dea | ths | } |
1744 | 86e94dea | ths | |
1745 | 82c643ff | bellard | int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
1746 | 82c643ff | bellard | { |
1747 | 82c643ff | bellard | return s->chr_write(s, buf, len);
|
1748 | 82c643ff | bellard | } |
1749 | 67b915a5 | bellard | |
1750 | e57a8c0e | bellard | int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg) |
1751 | f8d179e3 | bellard | { |
1752 | e57a8c0e | bellard | if (!s->chr_ioctl)
|
1753 | e57a8c0e | bellard | return -ENOTSUP;
|
1754 | e57a8c0e | bellard | return s->chr_ioctl(s, cmd, arg);
|
1755 | f8d179e3 | bellard | } |
1756 | f8d179e3 | bellard | |
1757 | e5b0bc44 | pbrook | int qemu_chr_can_read(CharDriverState *s)
|
1758 | e5b0bc44 | pbrook | { |
1759 | e5b0bc44 | pbrook | if (!s->chr_can_read)
|
1760 | e5b0bc44 | pbrook | return 0; |
1761 | e5b0bc44 | pbrook | return s->chr_can_read(s->handler_opaque);
|
1762 | e5b0bc44 | pbrook | } |
1763 | e5b0bc44 | pbrook | |
1764 | e5b0bc44 | pbrook | void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len) |
1765 | e5b0bc44 | pbrook | { |
1766 | e5b0bc44 | pbrook | s->chr_read(s->handler_opaque, buf, len); |
1767 | e5b0bc44 | pbrook | } |
1768 | e5b0bc44 | pbrook | |
1769 | bd9bdce6 | balrog | void qemu_chr_accept_input(CharDriverState *s)
|
1770 | bd9bdce6 | balrog | { |
1771 | bd9bdce6 | balrog | if (s->chr_accept_input)
|
1772 | bd9bdce6 | balrog | s->chr_accept_input(s); |
1773 | bd9bdce6 | balrog | } |
1774 | e5b0bc44 | pbrook | |
1775 | 82c643ff | bellard | void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
1776 | 67b915a5 | bellard | { |
1777 | 82c643ff | bellard | char buf[4096]; |
1778 | 82c643ff | bellard | va_list ap; |
1779 | 82c643ff | bellard | va_start(ap, fmt); |
1780 | 82c643ff | bellard | vsnprintf(buf, sizeof(buf), fmt, ap);
|
1781 | ffe8ab83 | ths | qemu_chr_write(s, (uint8_t *)buf, strlen(buf)); |
1782 | 82c643ff | bellard | va_end(ap); |
1783 | 67b915a5 | bellard | } |
1784 | 67b915a5 | bellard | |
1785 | 5905b2e5 | bellard | void qemu_chr_send_event(CharDriverState *s, int event) |
1786 | 5905b2e5 | bellard | { |
1787 | 5905b2e5 | bellard | if (s->chr_send_event)
|
1788 | 5905b2e5 | bellard | s->chr_send_event(s, event); |
1789 | 5905b2e5 | bellard | } |
1790 | 5905b2e5 | bellard | |
1791 | 5fafdf24 | ths | void qemu_chr_add_handlers(CharDriverState *s,
|
1792 | 5fafdf24 | ths | IOCanRWHandler *fd_can_read, |
1793 | e5b0bc44 | pbrook | IOReadHandler *fd_read, |
1794 | e5b0bc44 | pbrook | IOEventHandler *fd_event, |
1795 | e5b0bc44 | pbrook | void *opaque)
|
1796 | 82c643ff | bellard | { |
1797 | e5b0bc44 | pbrook | s->chr_can_read = fd_can_read; |
1798 | e5b0bc44 | pbrook | s->chr_read = fd_read; |
1799 | e5b0bc44 | pbrook | s->chr_event = fd_event; |
1800 | e5b0bc44 | pbrook | s->handler_opaque = opaque; |
1801 | e5b0bc44 | pbrook | if (s->chr_update_read_handler)
|
1802 | e5b0bc44 | pbrook | s->chr_update_read_handler(s); |
1803 | 82c643ff | bellard | } |
1804 | 3b46e624 | ths | |
1805 | 82c643ff | bellard | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1806 | 313aa567 | bellard | { |
1807 | 82c643ff | bellard | return len;
|
1808 | 82c643ff | bellard | } |
1809 | 82c643ff | bellard | |
1810 | 52f61fde | ths | static CharDriverState *qemu_chr_open_null(void) |
1811 | 82c643ff | bellard | { |
1812 | 82c643ff | bellard | CharDriverState *chr; |
1813 | 82c643ff | bellard | |
1814 | 82c643ff | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
1815 | 82c643ff | bellard | if (!chr)
|
1816 | 82c643ff | bellard | return NULL; |
1817 | 82c643ff | bellard | chr->chr_write = null_chr_write; |
1818 | 82c643ff | bellard | return chr;
|
1819 | 82c643ff | bellard | } |
1820 | 82c643ff | bellard | |
1821 | 20d8a3ed | ths | /* MUX driver for serial I/O splitting */
|
1822 | 20d8a3ed | ths | static int term_timestamps; |
1823 | 20d8a3ed | ths | static int64_t term_timestamps_start;
|
1824 | 9c1de612 | ths | #define MAX_MUX 4 |
1825 | bd9bdce6 | balrog | #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ |
1826 | bd9bdce6 | balrog | #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) |
1827 | 20d8a3ed | ths | typedef struct { |
1828 | 20d8a3ed | ths | IOCanRWHandler *chr_can_read[MAX_MUX]; |
1829 | 20d8a3ed | ths | IOReadHandler *chr_read[MAX_MUX]; |
1830 | 20d8a3ed | ths | IOEventHandler *chr_event[MAX_MUX]; |
1831 | 20d8a3ed | ths | void *ext_opaque[MAX_MUX];
|
1832 | 20d8a3ed | ths | CharDriverState *drv; |
1833 | bd9bdce6 | balrog | unsigned char buffer[MUX_BUFFER_SIZE]; |
1834 | bd9bdce6 | balrog | int prod;
|
1835 | bd9bdce6 | balrog | int cons;
|
1836 | 20d8a3ed | ths | int mux_cnt;
|
1837 | 20d8a3ed | ths | int term_got_escape;
|
1838 | 20d8a3ed | ths | int max_size;
|
1839 | 20d8a3ed | ths | } MuxDriver; |
1840 | 20d8a3ed | ths | |
1841 | 20d8a3ed | ths | |
1842 | 20d8a3ed | ths | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1843 | 20d8a3ed | ths | { |
1844 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
1845 | 20d8a3ed | ths | int ret;
|
1846 | 20d8a3ed | ths | if (!term_timestamps) {
|
1847 | 20d8a3ed | ths | ret = d->drv->chr_write(d->drv, buf, len); |
1848 | 20d8a3ed | ths | } else {
|
1849 | 20d8a3ed | ths | int i;
|
1850 | 20d8a3ed | ths | |
1851 | 20d8a3ed | ths | ret = 0;
|
1852 | 20d8a3ed | ths | for(i = 0; i < len; i++) { |
1853 | 20d8a3ed | ths | ret += d->drv->chr_write(d->drv, buf+i, 1);
|
1854 | 20d8a3ed | ths | if (buf[i] == '\n') { |
1855 | 20d8a3ed | ths | char buf1[64]; |
1856 | 20d8a3ed | ths | int64_t ti; |
1857 | 20d8a3ed | ths | int secs;
|
1858 | 20d8a3ed | ths | |
1859 | 20d8a3ed | ths | ti = get_clock(); |
1860 | 20d8a3ed | ths | if (term_timestamps_start == -1) |
1861 | 20d8a3ed | ths | term_timestamps_start = ti; |
1862 | 20d8a3ed | ths | ti -= term_timestamps_start; |
1863 | 20d8a3ed | ths | secs = ti / 1000000000;
|
1864 | 20d8a3ed | ths | snprintf(buf1, sizeof(buf1),
|
1865 | 20d8a3ed | ths | "[%02d:%02d:%02d.%03d] ",
|
1866 | 20d8a3ed | ths | secs / 3600,
|
1867 | 20d8a3ed | ths | (secs / 60) % 60, |
1868 | 20d8a3ed | ths | secs % 60,
|
1869 | 20d8a3ed | ths | (int)((ti / 1000000) % 1000)); |
1870 | ffe8ab83 | ths | d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
1871 | 20d8a3ed | ths | } |
1872 | 20d8a3ed | ths | } |
1873 | 20d8a3ed | ths | } |
1874 | 20d8a3ed | ths | return ret;
|
1875 | 20d8a3ed | ths | } |
1876 | 20d8a3ed | ths | |
1877 | 20d8a3ed | ths | static char *mux_help[] = { |
1878 | 20d8a3ed | ths | "% h print this help\n\r",
|
1879 | 20d8a3ed | ths | "% x exit emulator\n\r",
|
1880 | 20d8a3ed | ths | "% s save disk data back to file (if -snapshot)\n\r",
|
1881 | 20d8a3ed | ths | "% t toggle console timestamps\n\r"
|
1882 | 20d8a3ed | ths | "% b send break (magic sysrq)\n\r",
|
1883 | 20d8a3ed | ths | "% c switch between console and monitor\n\r",
|
1884 | 20d8a3ed | ths | "% % sends %\n\r",
|
1885 | 20d8a3ed | ths | NULL
|
1886 | 20d8a3ed | ths | }; |
1887 | 20d8a3ed | ths | |
1888 | 20d8a3ed | ths | static int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
1889 | 20d8a3ed | ths | static void mux_print_help(CharDriverState *chr) |
1890 | 20d8a3ed | ths | { |
1891 | 20d8a3ed | ths | int i, j;
|
1892 | 20d8a3ed | ths | char ebuf[15] = "Escape-Char"; |
1893 | 20d8a3ed | ths | char cbuf[50] = "\n\r"; |
1894 | 20d8a3ed | ths | |
1895 | 20d8a3ed | ths | if (term_escape_char > 0 && term_escape_char < 26) { |
1896 | 20d8a3ed | ths | sprintf(cbuf,"\n\r");
|
1897 | 20d8a3ed | ths | sprintf(ebuf,"C-%c", term_escape_char - 1 + 'a'); |
1898 | 20d8a3ed | ths | } else {
|
1899 | ffe8ab83 | ths | sprintf(cbuf,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
|
1900 | ffe8ab83 | ths | term_escape_char); |
1901 | 20d8a3ed | ths | } |
1902 | ffe8ab83 | ths | chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf)); |
1903 | 20d8a3ed | ths | for (i = 0; mux_help[i] != NULL; i++) { |
1904 | 20d8a3ed | ths | for (j=0; mux_help[i][j] != '\0'; j++) { |
1905 | 20d8a3ed | ths | if (mux_help[i][j] == '%') |
1906 | ffe8ab83 | ths | chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf)); |
1907 | 20d8a3ed | ths | else
|
1908 | ffe8ab83 | ths | chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
|
1909 | 20d8a3ed | ths | } |
1910 | 20d8a3ed | ths | } |
1911 | 20d8a3ed | ths | } |
1912 | 20d8a3ed | ths | |
1913 | 20d8a3ed | ths | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
1914 | 20d8a3ed | ths | { |
1915 | 20d8a3ed | ths | if (d->term_got_escape) {
|
1916 | 20d8a3ed | ths | d->term_got_escape = 0;
|
1917 | 20d8a3ed | ths | if (ch == term_escape_char)
|
1918 | 20d8a3ed | ths | goto send_char;
|
1919 | 20d8a3ed | ths | switch(ch) {
|
1920 | 20d8a3ed | ths | case '?': |
1921 | 20d8a3ed | ths | case 'h': |
1922 | 20d8a3ed | ths | mux_print_help(chr); |
1923 | 20d8a3ed | ths | break;
|
1924 | 20d8a3ed | ths | case 'x': |
1925 | 20d8a3ed | ths | { |
1926 | 20d8a3ed | ths | char *term = "QEMU: Terminated\n\r"; |
1927 | ffe8ab83 | ths | chr->chr_write(chr,(uint8_t *)term,strlen(term)); |
1928 | 20d8a3ed | ths | exit(0);
|
1929 | 20d8a3ed | ths | break;
|
1930 | 20d8a3ed | ths | } |
1931 | 20d8a3ed | ths | case 's': |
1932 | 20d8a3ed | ths | { |
1933 | 20d8a3ed | ths | int i;
|
1934 | e4bcb14c | ths | for (i = 0; i < nb_drives; i++) { |
1935 | e4bcb14c | ths | bdrv_commit(drives_table[i].bdrv); |
1936 | 20d8a3ed | ths | } |
1937 | 20d8a3ed | ths | } |
1938 | 20d8a3ed | ths | break;
|
1939 | 20d8a3ed | ths | case 'b': |
1940 | 36ddb83b | balrog | qemu_chr_event(chr, CHR_EVENT_BREAK); |
1941 | 20d8a3ed | ths | break;
|
1942 | 20d8a3ed | ths | case 'c': |
1943 | 20d8a3ed | ths | /* Switch to the next registered device */
|
1944 | 20d8a3ed | ths | chr->focus++; |
1945 | 20d8a3ed | ths | if (chr->focus >= d->mux_cnt)
|
1946 | 20d8a3ed | ths | chr->focus = 0;
|
1947 | 20d8a3ed | ths | break;
|
1948 | 20d8a3ed | ths | case 't': |
1949 | 20d8a3ed | ths | term_timestamps = !term_timestamps; |
1950 | 20d8a3ed | ths | term_timestamps_start = -1;
|
1951 | 20d8a3ed | ths | break;
|
1952 | 20d8a3ed | ths | } |
1953 | 20d8a3ed | ths | } else if (ch == term_escape_char) { |
1954 | 20d8a3ed | ths | d->term_got_escape = 1;
|
1955 | 20d8a3ed | ths | } else {
|
1956 | 20d8a3ed | ths | send_char:
|
1957 | 20d8a3ed | ths | return 1; |
1958 | 20d8a3ed | ths | } |
1959 | 20d8a3ed | ths | return 0; |
1960 | 20d8a3ed | ths | } |
1961 | 20d8a3ed | ths | |
1962 | bd9bdce6 | balrog | static void mux_chr_accept_input(CharDriverState *chr) |
1963 | bd9bdce6 | balrog | { |
1964 | bd9bdce6 | balrog | int m = chr->focus;
|
1965 | bd9bdce6 | balrog | MuxDriver *d = chr->opaque; |
1966 | bd9bdce6 | balrog | |
1967 | bd9bdce6 | balrog | while (d->prod != d->cons &&
|
1968 | bd9bdce6 | balrog | d->chr_can_read[m] && |
1969 | bd9bdce6 | balrog | d->chr_can_read[m](d->ext_opaque[m])) { |
1970 | bd9bdce6 | balrog | d->chr_read[m](d->ext_opaque[m], |
1971 | bd9bdce6 | balrog | &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
|
1972 | bd9bdce6 | balrog | } |
1973 | bd9bdce6 | balrog | } |
1974 | bd9bdce6 | balrog | |
1975 | 20d8a3ed | ths | static int mux_chr_can_read(void *opaque) |
1976 | 20d8a3ed | ths | { |
1977 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
1978 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
1979 | bd9bdce6 | balrog | |
1980 | bd9bdce6 | balrog | if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
|
1981 | bd9bdce6 | balrog | return 1; |
1982 | 20d8a3ed | ths | if (d->chr_can_read[chr->focus])
|
1983 | bd9bdce6 | balrog | return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
|
1984 | 20d8a3ed | ths | return 0; |
1985 | 20d8a3ed | ths | } |
1986 | 20d8a3ed | ths | |
1987 | 20d8a3ed | ths | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
1988 | 20d8a3ed | ths | { |
1989 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
1990 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
1991 | bd9bdce6 | balrog | int m = chr->focus;
|
1992 | 20d8a3ed | ths | int i;
|
1993 | bd9bdce6 | balrog | |
1994 | bd9bdce6 | balrog | mux_chr_accept_input (opaque); |
1995 | bd9bdce6 | balrog | |
1996 | 20d8a3ed | ths | for(i = 0; i < size; i++) |
1997 | bd9bdce6 | balrog | if (mux_proc_byte(chr, d, buf[i])) {
|
1998 | bd9bdce6 | balrog | if (d->prod == d->cons &&
|
1999 | bd9bdce6 | balrog | d->chr_can_read[m] && |
2000 | bd9bdce6 | balrog | d->chr_can_read[m](d->ext_opaque[m])) |
2001 | bd9bdce6 | balrog | d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
|
2002 | bd9bdce6 | balrog | else
|
2003 | bd9bdce6 | balrog | d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i]; |
2004 | bd9bdce6 | balrog | } |
2005 | 20d8a3ed | ths | } |
2006 | 20d8a3ed | ths | |
2007 | 20d8a3ed | ths | static void mux_chr_event(void *opaque, int event) |
2008 | 20d8a3ed | ths | { |
2009 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2010 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
2011 | 20d8a3ed | ths | int i;
|
2012 | 20d8a3ed | ths | |
2013 | 20d8a3ed | ths | /* Send the event to all registered listeners */
|
2014 | 20d8a3ed | ths | for (i = 0; i < d->mux_cnt; i++) |
2015 | 20d8a3ed | ths | if (d->chr_event[i])
|
2016 | 20d8a3ed | ths | d->chr_event[i](d->ext_opaque[i], event); |
2017 | 20d8a3ed | ths | } |
2018 | 20d8a3ed | ths | |
2019 | 20d8a3ed | ths | static void mux_chr_update_read_handler(CharDriverState *chr) |
2020 | 20d8a3ed | ths | { |
2021 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
2022 | 20d8a3ed | ths | |
2023 | 20d8a3ed | ths | if (d->mux_cnt >= MAX_MUX) {
|
2024 | 20d8a3ed | ths | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
|
2025 | 20d8a3ed | ths | return;
|
2026 | 20d8a3ed | ths | } |
2027 | 20d8a3ed | ths | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
2028 | 20d8a3ed | ths | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
2029 | 20d8a3ed | ths | d->chr_read[d->mux_cnt] = chr->chr_read; |
2030 | 20d8a3ed | ths | d->chr_event[d->mux_cnt] = chr->chr_event; |
2031 | 20d8a3ed | ths | /* Fix up the real driver with mux routines */
|
2032 | 20d8a3ed | ths | if (d->mux_cnt == 0) { |
2033 | 20d8a3ed | ths | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
2034 | 20d8a3ed | ths | mux_chr_event, chr); |
2035 | 20d8a3ed | ths | } |
2036 | 20d8a3ed | ths | chr->focus = d->mux_cnt; |
2037 | 20d8a3ed | ths | d->mux_cnt++; |
2038 | 20d8a3ed | ths | } |
2039 | 20d8a3ed | ths | |
2040 | 9596ebb7 | pbrook | static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
|
2041 | 20d8a3ed | ths | { |
2042 | 20d8a3ed | ths | CharDriverState *chr; |
2043 | 20d8a3ed | ths | MuxDriver *d; |
2044 | 20d8a3ed | ths | |
2045 | 20d8a3ed | ths | chr = qemu_mallocz(sizeof(CharDriverState));
|
2046 | 20d8a3ed | ths | if (!chr)
|
2047 | 20d8a3ed | ths | return NULL; |
2048 | 20d8a3ed | ths | d = qemu_mallocz(sizeof(MuxDriver));
|
2049 | 20d8a3ed | ths | if (!d) {
|
2050 | 20d8a3ed | ths | free(chr); |
2051 | 20d8a3ed | ths | return NULL; |
2052 | 20d8a3ed | ths | } |
2053 | 20d8a3ed | ths | |
2054 | 20d8a3ed | ths | chr->opaque = d; |
2055 | 20d8a3ed | ths | d->drv = drv; |
2056 | 20d8a3ed | ths | chr->focus = -1;
|
2057 | 20d8a3ed | ths | chr->chr_write = mux_chr_write; |
2058 | 20d8a3ed | ths | chr->chr_update_read_handler = mux_chr_update_read_handler; |
2059 | bd9bdce6 | balrog | chr->chr_accept_input = mux_chr_accept_input; |
2060 | 20d8a3ed | ths | return chr;
|
2061 | 20d8a3ed | ths | } |
2062 | 20d8a3ed | ths | |
2063 | 20d8a3ed | ths | |
2064 | fd1dff4b | bellard | #ifdef _WIN32
|
2065 | 82c643ff | bellard | |
2066 | fd1dff4b | bellard | static void socket_cleanup(void) |
2067 | fd1dff4b | bellard | { |
2068 | fd1dff4b | bellard | WSACleanup(); |
2069 | fd1dff4b | bellard | } |
2070 | 82c643ff | bellard | |
2071 | fd1dff4b | bellard | static int socket_init(void) |
2072 | fd1dff4b | bellard | { |
2073 | fd1dff4b | bellard | WSADATA Data; |
2074 | fd1dff4b | bellard | int ret, err;
|
2075 | fd1dff4b | bellard | |
2076 | fd1dff4b | bellard | ret = WSAStartup(MAKEWORD(2,2), &Data); |
2077 | fd1dff4b | bellard | if (ret != 0) { |
2078 | fd1dff4b | bellard | err = WSAGetLastError(); |
2079 | fd1dff4b | bellard | fprintf(stderr, "WSAStartup: %d\n", err);
|
2080 | fd1dff4b | bellard | return -1; |
2081 | fd1dff4b | bellard | } |
2082 | fd1dff4b | bellard | atexit(socket_cleanup); |
2083 | fd1dff4b | bellard | return 0; |
2084 | fd1dff4b | bellard | } |
2085 | fd1dff4b | bellard | |
2086 | fd1dff4b | bellard | static int send_all(int fd, const uint8_t *buf, int len1) |
2087 | fd1dff4b | bellard | { |
2088 | fd1dff4b | bellard | int ret, len;
|
2089 | 3b46e624 | ths | |
2090 | fd1dff4b | bellard | len = len1; |
2091 | fd1dff4b | bellard | while (len > 0) { |
2092 | fd1dff4b | bellard | ret = send(fd, buf, len, 0);
|
2093 | fd1dff4b | bellard | if (ret < 0) { |
2094 | fd1dff4b | bellard | int errno;
|
2095 | fd1dff4b | bellard | errno = WSAGetLastError(); |
2096 | fd1dff4b | bellard | if (errno != WSAEWOULDBLOCK) {
|
2097 | fd1dff4b | bellard | return -1; |
2098 | fd1dff4b | bellard | } |
2099 | fd1dff4b | bellard | } else if (ret == 0) { |
2100 | fd1dff4b | bellard | break;
|
2101 | fd1dff4b | bellard | } else {
|
2102 | fd1dff4b | bellard | buf += ret; |
2103 | fd1dff4b | bellard | len -= ret; |
2104 | fd1dff4b | bellard | } |
2105 | fd1dff4b | bellard | } |
2106 | fd1dff4b | bellard | return len1 - len;
|
2107 | fd1dff4b | bellard | } |
2108 | fd1dff4b | bellard | |
2109 | fd1dff4b | bellard | void socket_set_nonblock(int fd) |
2110 | fd1dff4b | bellard | { |
2111 | fd1dff4b | bellard | unsigned long opt = 1; |
2112 | fd1dff4b | bellard | ioctlsocket(fd, FIONBIO, &opt); |
2113 | fd1dff4b | bellard | } |
2114 | fd1dff4b | bellard | |
2115 | fd1dff4b | bellard | #else
|
2116 | fd1dff4b | bellard | |
2117 | 1d96905d | bellard | static int unix_write(int fd, const uint8_t *buf, int len1) |
2118 | 1d96905d | bellard | { |
2119 | 1d96905d | bellard | int ret, len;
|
2120 | 1d96905d | bellard | |
2121 | 1d96905d | bellard | len = len1; |
2122 | 1d96905d | bellard | while (len > 0) { |
2123 | 1d96905d | bellard | ret = write(fd, buf, len); |
2124 | 1d96905d | bellard | if (ret < 0) { |
2125 | 1d96905d | bellard | if (errno != EINTR && errno != EAGAIN)
|
2126 | 1d96905d | bellard | return -1; |
2127 | 1d96905d | bellard | } else if (ret == 0) { |
2128 | 1d96905d | bellard | break;
|
2129 | 1d96905d | bellard | } else {
|
2130 | 1d96905d | bellard | buf += ret; |
2131 | 1d96905d | bellard | len -= ret; |
2132 | 1d96905d | bellard | } |
2133 | 1d96905d | bellard | } |
2134 | 1d96905d | bellard | return len1 - len;
|
2135 | 1d96905d | bellard | } |
2136 | 1d96905d | bellard | |
2137 | fd1dff4b | bellard | static inline int send_all(int fd, const uint8_t *buf, int len1) |
2138 | fd1dff4b | bellard | { |
2139 | fd1dff4b | bellard | return unix_write(fd, buf, len1);
|
2140 | fd1dff4b | bellard | } |
2141 | fd1dff4b | bellard | |
2142 | fd1dff4b | bellard | void socket_set_nonblock(int fd) |
2143 | fd1dff4b | bellard | { |
2144 | fd1dff4b | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
2145 | fd1dff4b | bellard | } |
2146 | fd1dff4b | bellard | #endif /* !_WIN32 */ |
2147 | fd1dff4b | bellard | |
2148 | fd1dff4b | bellard | #ifndef _WIN32
|
2149 | fd1dff4b | bellard | |
2150 | fd1dff4b | bellard | typedef struct { |
2151 | fd1dff4b | bellard | int fd_in, fd_out;
|
2152 | fd1dff4b | bellard | int max_size;
|
2153 | fd1dff4b | bellard | } FDCharDriver; |
2154 | fd1dff4b | bellard | |
2155 | 20d8a3ed | ths | #define STDIO_MAX_CLIENTS 1 |
2156 | 20d8a3ed | ths | static int stdio_nb_clients = 0; |
2157 | fd1dff4b | bellard | |
2158 | 82c643ff | bellard | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
2159 | 82c643ff | bellard | { |
2160 | 82c643ff | bellard | FDCharDriver *s = chr->opaque; |
2161 | 1d96905d | bellard | return unix_write(s->fd_out, buf, len);
|
2162 | 82c643ff | bellard | } |
2163 | 82c643ff | bellard | |
2164 | 7c9d8e07 | bellard | static int fd_chr_read_poll(void *opaque) |
2165 | 7c9d8e07 | bellard | { |
2166 | 7c9d8e07 | bellard | CharDriverState *chr = opaque; |
2167 | 7c9d8e07 | bellard | FDCharDriver *s = chr->opaque; |
2168 | 7c9d8e07 | bellard | |
2169 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
2170 | 7c9d8e07 | bellard | return s->max_size;
|
2171 | 7c9d8e07 | bellard | } |
2172 | 7c9d8e07 | bellard | |
2173 | 7c9d8e07 | bellard | static void fd_chr_read(void *opaque) |
2174 | 7c9d8e07 | bellard | { |
2175 | 7c9d8e07 | bellard | CharDriverState *chr = opaque; |
2176 | 7c9d8e07 | bellard | FDCharDriver *s = chr->opaque; |
2177 | 7c9d8e07 | bellard | int size, len;
|
2178 | 7c9d8e07 | bellard | uint8_t buf[1024];
|
2179 | 3b46e624 | ths | |
2180 | 7c9d8e07 | bellard | len = sizeof(buf);
|
2181 | 7c9d8e07 | bellard | if (len > s->max_size)
|
2182 | 7c9d8e07 | bellard | len = s->max_size; |
2183 | 7c9d8e07 | bellard | if (len == 0) |
2184 | 7c9d8e07 | bellard | return;
|
2185 | 7c9d8e07 | bellard | size = read(s->fd_in, buf, len); |
2186 | 188157fe | pbrook | if (size == 0) { |
2187 | 188157fe | pbrook | /* FD has been closed. Remove it from the active list. */
|
2188 | 188157fe | pbrook | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
2189 | 188157fe | pbrook | return;
|
2190 | 188157fe | pbrook | } |
2191 | 7c9d8e07 | bellard | if (size > 0) { |
2192 | e5b0bc44 | pbrook | qemu_chr_read(chr, buf, size); |
2193 | 7c9d8e07 | bellard | } |
2194 | 7c9d8e07 | bellard | } |
2195 | 7c9d8e07 | bellard | |
2196 | e5b0bc44 | pbrook | static void fd_chr_update_read_handler(CharDriverState *chr) |
2197 | 82c643ff | bellard | { |
2198 | 82c643ff | bellard | FDCharDriver *s = chr->opaque; |
2199 | 82c643ff | bellard | |
2200 | f8d179e3 | bellard | if (s->fd_in >= 0) { |
2201 | f8d179e3 | bellard | if (nographic && s->fd_in == 0) { |
2202 | f8d179e3 | bellard | } else {
|
2203 | 5fafdf24 | ths | qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, |
2204 | 7c9d8e07 | bellard | fd_chr_read, NULL, chr);
|
2205 | f8d179e3 | bellard | } |
2206 | 82c643ff | bellard | } |
2207 | 82c643ff | bellard | } |
2208 | 82c643ff | bellard | |
2209 | a11d070e | balrog | static void fd_chr_close(struct CharDriverState *chr) |
2210 | a11d070e | balrog | { |
2211 | a11d070e | balrog | FDCharDriver *s = chr->opaque; |
2212 | a11d070e | balrog | |
2213 | a11d070e | balrog | if (s->fd_in >= 0) { |
2214 | a11d070e | balrog | if (nographic && s->fd_in == 0) { |
2215 | a11d070e | balrog | } else {
|
2216 | a11d070e | balrog | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
2217 | a11d070e | balrog | } |
2218 | a11d070e | balrog | } |
2219 | a11d070e | balrog | |
2220 | a11d070e | balrog | qemu_free(s); |
2221 | a11d070e | balrog | } |
2222 | a11d070e | balrog | |
2223 | 82c643ff | bellard | /* open a character device to a unix fd */
|
2224 | 52f61fde | ths | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
2225 | 82c643ff | bellard | { |
2226 | 82c643ff | bellard | CharDriverState *chr; |
2227 | 82c643ff | bellard | FDCharDriver *s; |
2228 | 82c643ff | bellard | |
2229 | 82c643ff | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
2230 | 82c643ff | bellard | if (!chr)
|
2231 | 82c643ff | bellard | return NULL; |
2232 | 82c643ff | bellard | s = qemu_mallocz(sizeof(FDCharDriver));
|
2233 | 82c643ff | bellard | if (!s) {
|
2234 | 82c643ff | bellard | free(chr); |
2235 | 82c643ff | bellard | return NULL; |
2236 | 82c643ff | bellard | } |
2237 | 82c643ff | bellard | s->fd_in = fd_in; |
2238 | 82c643ff | bellard | s->fd_out = fd_out; |
2239 | 82c643ff | bellard | chr->opaque = s; |
2240 | 82c643ff | bellard | chr->chr_write = fd_chr_write; |
2241 | e5b0bc44 | pbrook | chr->chr_update_read_handler = fd_chr_update_read_handler; |
2242 | a11d070e | balrog | chr->chr_close = fd_chr_close; |
2243 | 86e94dea | ths | |
2244 | 86e94dea | ths | qemu_chr_reset(chr); |
2245 | 86e94dea | ths | |
2246 | 82c643ff | bellard | return chr;
|
2247 | 82c643ff | bellard | } |
2248 | 82c643ff | bellard | |
2249 | 52f61fde | ths | static CharDriverState *qemu_chr_open_file_out(const char *file_out) |
2250 | f8d179e3 | bellard | { |
2251 | f8d179e3 | bellard | int fd_out;
|
2252 | f8d179e3 | bellard | |
2253 | aeb30be6 | balrog | TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
|
2254 | f8d179e3 | bellard | if (fd_out < 0) |
2255 | f8d179e3 | bellard | return NULL; |
2256 | f8d179e3 | bellard | return qemu_chr_open_fd(-1, fd_out); |
2257 | f8d179e3 | bellard | } |
2258 | f8d179e3 | bellard | |
2259 | 52f61fde | ths | static CharDriverState *qemu_chr_open_pipe(const char *filename) |
2260 | f8d179e3 | bellard | { |
2261 | c26c1c4b | ths | int fd_in, fd_out;
|
2262 | c26c1c4b | ths | char filename_in[256], filename_out[256]; |
2263 | c26c1c4b | ths | |
2264 | c26c1c4b | ths | snprintf(filename_in, 256, "%s.in", filename); |
2265 | c26c1c4b | ths | snprintf(filename_out, 256, "%s.out", filename); |
2266 | aeb30be6 | balrog | TFR(fd_in = open(filename_in, O_RDWR | O_BINARY)); |
2267 | aeb30be6 | balrog | TFR(fd_out = open(filename_out, O_RDWR | O_BINARY)); |
2268 | c26c1c4b | ths | if (fd_in < 0 || fd_out < 0) { |
2269 | c26c1c4b | ths | if (fd_in >= 0) |
2270 | c26c1c4b | ths | close(fd_in); |
2271 | c26c1c4b | ths | if (fd_out >= 0) |
2272 | c26c1c4b | ths | close(fd_out); |
2273 | aeb30be6 | balrog | TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY)); |
2274 | c26c1c4b | ths | if (fd_in < 0) |
2275 | c26c1c4b | ths | return NULL; |
2276 | c26c1c4b | ths | } |
2277 | c26c1c4b | ths | return qemu_chr_open_fd(fd_in, fd_out);
|
2278 | f8d179e3 | bellard | } |
2279 | f8d179e3 | bellard | |
2280 | f8d179e3 | bellard | |
2281 | 82c643ff | bellard | /* for STDIO, we handle the case where several clients use it
|
2282 | 82c643ff | bellard | (nographic mode) */
|
2283 | 82c643ff | bellard | |
2284 | aa0bc6b6 | bellard | #define TERM_FIFO_MAX_SIZE 1 |
2285 | aa0bc6b6 | bellard | |
2286 | aa0bc6b6 | bellard | static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
|
2287 | 1dce7c3c | bellard | static int term_fifo_size; |
2288 | 330d0414 | bellard | |
2289 | 7c9d8e07 | bellard | static int stdio_read_poll(void *opaque) |
2290 | 82c643ff | bellard | { |
2291 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2292 | aa0bc6b6 | bellard | |
2293 | 20d8a3ed | ths | /* try to flush the queue if needed */
|
2294 | 20d8a3ed | ths | if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) { |
2295 | 20d8a3ed | ths | qemu_chr_read(chr, term_fifo, 1);
|
2296 | 20d8a3ed | ths | term_fifo_size = 0;
|
2297 | aa0bc6b6 | bellard | } |
2298 | 20d8a3ed | ths | /* see if we can absorb more chars */
|
2299 | 20d8a3ed | ths | if (term_fifo_size == 0) |
2300 | 20d8a3ed | ths | return 1; |
2301 | 20d8a3ed | ths | else
|
2302 | 20d8a3ed | ths | return 0; |
2303 | 82c643ff | bellard | } |
2304 | 82c643ff | bellard | |
2305 | 7c9d8e07 | bellard | static void stdio_read(void *opaque) |
2306 | 82c643ff | bellard | { |
2307 | 7c9d8e07 | bellard | int size;
|
2308 | 7c9d8e07 | bellard | uint8_t buf[1];
|
2309 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2310 | 20d8a3ed | ths | |
2311 | 7c9d8e07 | bellard | size = read(0, buf, 1); |
2312 | 519945df | pbrook | if (size == 0) { |
2313 | 519945df | pbrook | /* stdin has been closed. Remove it from the active list. */
|
2314 | 519945df | pbrook | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
2315 | 519945df | pbrook | return;
|
2316 | 519945df | pbrook | } |
2317 | 20d8a3ed | ths | if (size > 0) { |
2318 | 20d8a3ed | ths | if (qemu_chr_can_read(chr) > 0) { |
2319 | 20d8a3ed | ths | qemu_chr_read(chr, buf, 1);
|
2320 | 20d8a3ed | ths | } else if (term_fifo_size == 0) { |
2321 | 20d8a3ed | ths | term_fifo[term_fifo_size++] = buf[0];
|
2322 | 1dce7c3c | bellard | } |
2323 | 1dce7c3c | bellard | } |
2324 | 1dce7c3c | bellard | } |
2325 | 1dce7c3c | bellard | |
2326 | 8d11df9e | bellard | /* init terminal so that we can grab keys */
|
2327 | 8d11df9e | bellard | static struct termios oldtty; |
2328 | 8d11df9e | bellard | static int old_fd0_flags; |
2329 | a11d070e | balrog | static int term_atexit_done; |
2330 | 8d11df9e | bellard | |
2331 | 8d11df9e | bellard | static void term_exit(void) |
2332 | 8d11df9e | bellard | { |
2333 | 8d11df9e | bellard | tcsetattr (0, TCSANOW, &oldtty);
|
2334 | 8d11df9e | bellard | fcntl(0, F_SETFL, old_fd0_flags);
|
2335 | 8d11df9e | bellard | } |
2336 | 8d11df9e | bellard | |
2337 | 8d11df9e | bellard | static void term_init(void) |
2338 | 8d11df9e | bellard | { |
2339 | 8d11df9e | bellard | struct termios tty;
|
2340 | 8d11df9e | bellard | |
2341 | 8d11df9e | bellard | tcgetattr (0, &tty);
|
2342 | 8d11df9e | bellard | oldtty = tty; |
2343 | 8d11df9e | bellard | old_fd0_flags = fcntl(0, F_GETFL);
|
2344 | 8d11df9e | bellard | |
2345 | 8d11df9e | bellard | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
2346 | 8d11df9e | bellard | |INLCR|IGNCR|ICRNL|IXON); |
2347 | 8d11df9e | bellard | tty.c_oflag |= OPOST; |
2348 | 8d11df9e | bellard | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
2349 | 8d11df9e | bellard | /* if graphical mode, we allow Ctrl-C handling */
|
2350 | 8d11df9e | bellard | if (nographic)
|
2351 | 8d11df9e | bellard | tty.c_lflag &= ~ISIG; |
2352 | 8d11df9e | bellard | tty.c_cflag &= ~(CSIZE|PARENB); |
2353 | 8d11df9e | bellard | tty.c_cflag |= CS8; |
2354 | 8d11df9e | bellard | tty.c_cc[VMIN] = 1;
|
2355 | 8d11df9e | bellard | tty.c_cc[VTIME] = 0;
|
2356 | 3b46e624 | ths | |
2357 | 8d11df9e | bellard | tcsetattr (0, TCSANOW, &tty);
|
2358 | 8d11df9e | bellard | |
2359 | a11d070e | balrog | if (!term_atexit_done++)
|
2360 | a11d070e | balrog | atexit(term_exit); |
2361 | 8d11df9e | bellard | |
2362 | 8d11df9e | bellard | fcntl(0, F_SETFL, O_NONBLOCK);
|
2363 | 8d11df9e | bellard | } |
2364 | 8d11df9e | bellard | |
2365 | a11d070e | balrog | static void qemu_chr_close_stdio(struct CharDriverState *chr) |
2366 | a11d070e | balrog | { |
2367 | a11d070e | balrog | term_exit(); |
2368 | a11d070e | balrog | stdio_nb_clients--; |
2369 | a11d070e | balrog | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
2370 | a11d070e | balrog | fd_chr_close(chr); |
2371 | a11d070e | balrog | } |
2372 | a11d070e | balrog | |
2373 | 52f61fde | ths | static CharDriverState *qemu_chr_open_stdio(void) |
2374 | 82c643ff | bellard | { |
2375 | 82c643ff | bellard | CharDriverState *chr; |
2376 | 82c643ff | bellard | |
2377 | 20d8a3ed | ths | if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
|
2378 | 20d8a3ed | ths | return NULL; |
2379 | 20d8a3ed | ths | chr = qemu_chr_open_fd(0, 1); |
2380 | a11d070e | balrog | chr->chr_close = qemu_chr_close_stdio; |
2381 | 20d8a3ed | ths | qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr); |
2382 | 20d8a3ed | ths | stdio_nb_clients++; |
2383 | 20d8a3ed | ths | term_init(); |
2384 | 20d8a3ed | ths | |
2385 | 82c643ff | bellard | return chr;
|
2386 | 82c643ff | bellard | } |
2387 | 82c643ff | bellard | |
2388 | 64b7b733 | aurel32 | #ifdef __sun__
|
2389 | 64b7b733 | aurel32 | /* Once Solaris has openpty(), this is going to be removed. */
|
2390 | 64b7b733 | aurel32 | int openpty(int *amaster, int *aslave, char *name, |
2391 | 64b7b733 | aurel32 | struct termios *termp, struct winsize *winp) |
2392 | 64b7b733 | aurel32 | { |
2393 | 64b7b733 | aurel32 | const char *slave; |
2394 | 64b7b733 | aurel32 | int mfd = -1, sfd = -1; |
2395 | 64b7b733 | aurel32 | |
2396 | 64b7b733 | aurel32 | *amaster = *aslave = -1;
|
2397 | 64b7b733 | aurel32 | |
2398 | 64b7b733 | aurel32 | mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
2399 | 64b7b733 | aurel32 | if (mfd < 0) |
2400 | 64b7b733 | aurel32 | goto err;
|
2401 | 64b7b733 | aurel32 | |
2402 | 64b7b733 | aurel32 | if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) |
2403 | 64b7b733 | aurel32 | goto err;
|
2404 | 64b7b733 | aurel32 | |
2405 | 64b7b733 | aurel32 | if ((slave = ptsname(mfd)) == NULL) |
2406 | 64b7b733 | aurel32 | goto err;
|
2407 | 64b7b733 | aurel32 | |
2408 | 64b7b733 | aurel32 | if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) |
2409 | 64b7b733 | aurel32 | goto err;
|
2410 | 64b7b733 | aurel32 | |
2411 | 64b7b733 | aurel32 | if (ioctl(sfd, I_PUSH, "ptem") == -1 || |
2412 | 64b7b733 | aurel32 | (termp != NULL && tcgetattr(sfd, termp) < 0)) |
2413 | 64b7b733 | aurel32 | goto err;
|
2414 | 64b7b733 | aurel32 | |
2415 | 64b7b733 | aurel32 | if (amaster)
|
2416 | 64b7b733 | aurel32 | *amaster = mfd; |
2417 | 64b7b733 | aurel32 | if (aslave)
|
2418 | 64b7b733 | aurel32 | *aslave = sfd; |
2419 | 64b7b733 | aurel32 | if (winp)
|
2420 | 64b7b733 | aurel32 | ioctl(sfd, TIOCSWINSZ, winp); |
2421 | 64b7b733 | aurel32 | |
2422 | 64b7b733 | aurel32 | return 0; |
2423 | 64b7b733 | aurel32 | |
2424 | 64b7b733 | aurel32 | err:
|
2425 | 64b7b733 | aurel32 | if (sfd != -1) |
2426 | 64b7b733 | aurel32 | close(sfd); |
2427 | 64b7b733 | aurel32 | close(mfd); |
2428 | 64b7b733 | aurel32 | return -1; |
2429 | 64b7b733 | aurel32 | } |
2430 | 64b7b733 | aurel32 | |
2431 | 64b7b733 | aurel32 | void cfmakeraw (struct termios *termios_p) |
2432 | 64b7b733 | aurel32 | { |
2433 | 64b7b733 | aurel32 | termios_p->c_iflag &= |
2434 | 64b7b733 | aurel32 | ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); |
2435 | 64b7b733 | aurel32 | termios_p->c_oflag &= ~OPOST; |
2436 | 64b7b733 | aurel32 | termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); |
2437 | 64b7b733 | aurel32 | termios_p->c_cflag &= ~(CSIZE|PARENB); |
2438 | 64b7b733 | aurel32 | termios_p->c_cflag |= CS8; |
2439 | 64b7b733 | aurel32 | |
2440 | 64b7b733 | aurel32 | termios_p->c_cc[VMIN] = 0;
|
2441 | 64b7b733 | aurel32 | termios_p->c_cc[VTIME] = 0;
|
2442 | 64b7b733 | aurel32 | } |
2443 | 64b7b733 | aurel32 | #endif
|
2444 | 64b7b733 | aurel32 | |
2445 | aec62507 | ths | #if defined(__linux__) || defined(__sun__)
|
2446 | 52f61fde | ths | static CharDriverState *qemu_chr_open_pty(void) |
2447 | 82c643ff | bellard | { |
2448 | 91fc2119 | bellard | struct termios tty;
|
2449 | 82c643ff | bellard | int master_fd, slave_fd;
|
2450 | 3b46e624 | ths | |
2451 | 64b7b733 | aurel32 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) < 0) { |
2452 | 82c643ff | bellard | return NULL; |
2453 | 82c643ff | bellard | } |
2454 | 3b46e624 | ths | |
2455 | 64b7b733 | aurel32 | /* Set raw attributes on the pty. */
|
2456 | 64b7b733 | aurel32 | cfmakeraw(&tty); |
2457 | 64b7b733 | aurel32 | tcsetattr(slave_fd, TCSAFLUSH, &tty); |
2458 | 91fc2119 | bellard | |
2459 | 64b7b733 | aurel32 | fprintf(stderr, "char device redirected to %s\n", ptsname(master_fd));
|
2460 | 82c643ff | bellard | return qemu_chr_open_fd(master_fd, master_fd);
|
2461 | 82c643ff | bellard | } |
2462 | f8d179e3 | bellard | |
2463 | 5fafdf24 | ths | static void tty_serial_init(int fd, int speed, |
2464 | f8d179e3 | bellard | int parity, int data_bits, int stop_bits) |
2465 | f8d179e3 | bellard | { |
2466 | f8d179e3 | bellard | struct termios tty;
|
2467 | f8d179e3 | bellard | speed_t spd; |
2468 | f8d179e3 | bellard | |
2469 | e57a8c0e | bellard | #if 0
|
2470 | 5fafdf24 | ths | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
|
2471 | e57a8c0e | bellard | speed, parity, data_bits, stop_bits);
|
2472 | e57a8c0e | bellard | #endif
|
2473 | e57a8c0e | bellard | tcgetattr (fd, &tty); |
2474 | f8d179e3 | bellard | |
2475 | a7954218 | balrog | #define MARGIN 1.1 |
2476 | a7954218 | balrog | if (speed <= 50 * MARGIN) |
2477 | f8d179e3 | bellard | spd = B50; |
2478 | a7954218 | balrog | else if (speed <= 75 * MARGIN) |
2479 | f8d179e3 | bellard | spd = B75; |
2480 | a7954218 | balrog | else if (speed <= 300 * MARGIN) |
2481 | f8d179e3 | bellard | spd = B300; |
2482 | a7954218 | balrog | else if (speed <= 600 * MARGIN) |
2483 | f8d179e3 | bellard | spd = B600; |
2484 | a7954218 | balrog | else if (speed <= 1200 * MARGIN) |
2485 | f8d179e3 | bellard | spd = B1200; |
2486 | a7954218 | balrog | else if (speed <= 2400 * MARGIN) |
2487 | f8d179e3 | bellard | spd = B2400; |
2488 | a7954218 | balrog | else if (speed <= 4800 * MARGIN) |
2489 | f8d179e3 | bellard | spd = B4800; |
2490 | a7954218 | balrog | else if (speed <= 9600 * MARGIN) |
2491 | f8d179e3 | bellard | spd = B9600; |
2492 | a7954218 | balrog | else if (speed <= 19200 * MARGIN) |
2493 | f8d179e3 | bellard | spd = B19200; |
2494 | a7954218 | balrog | else if (speed <= 38400 * MARGIN) |
2495 | f8d179e3 | bellard | spd = B38400; |
2496 | a7954218 | balrog | else if (speed <= 57600 * MARGIN) |
2497 | f8d179e3 | bellard | spd = B57600; |
2498 | a7954218 | balrog | else if (speed <= 115200 * MARGIN) |
2499 | a7954218 | balrog | spd = B115200; |
2500 | a7954218 | balrog | else
|
2501 | f8d179e3 | bellard | spd = B115200; |
2502 | f8d179e3 | bellard | |
2503 | f8d179e3 | bellard | cfsetispeed(&tty, spd); |
2504 | f8d179e3 | bellard | cfsetospeed(&tty, spd); |
2505 | f8d179e3 | bellard | |
2506 | f8d179e3 | bellard | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
2507 | f8d179e3 | bellard | |INLCR|IGNCR|ICRNL|IXON); |
2508 | f8d179e3 | bellard | tty.c_oflag |= OPOST; |
2509 | f8d179e3 | bellard | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
2510 | 094eed6c | bellard | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
2511 | f8d179e3 | bellard | switch(data_bits) {
|
2512 | f8d179e3 | bellard | default:
|
2513 | f8d179e3 | bellard | case 8: |
2514 | f8d179e3 | bellard | tty.c_cflag |= CS8; |
2515 | f8d179e3 | bellard | break;
|
2516 | f8d179e3 | bellard | case 7: |
2517 | f8d179e3 | bellard | tty.c_cflag |= CS7; |
2518 | f8d179e3 | bellard | break;
|
2519 | f8d179e3 | bellard | case 6: |
2520 | f8d179e3 | bellard | tty.c_cflag |= CS6; |
2521 | f8d179e3 | bellard | break;
|
2522 | f8d179e3 | bellard | case 5: |
2523 | f8d179e3 | bellard | tty.c_cflag |= CS5; |
2524 | f8d179e3 | bellard | break;
|
2525 | f8d179e3 | bellard | } |
2526 | f8d179e3 | bellard | switch(parity) {
|
2527 | f8d179e3 | bellard | default:
|
2528 | f8d179e3 | bellard | case 'N': |
2529 | f8d179e3 | bellard | break;
|
2530 | f8d179e3 | bellard | case 'E': |
2531 | f8d179e3 | bellard | tty.c_cflag |= PARENB; |
2532 | f8d179e3 | bellard | break;
|
2533 | f8d179e3 | bellard | case 'O': |
2534 | f8d179e3 | bellard | tty.c_cflag |= PARENB | PARODD; |
2535 | f8d179e3 | bellard | break;
|
2536 | f8d179e3 | bellard | } |
2537 | 094eed6c | bellard | if (stop_bits == 2) |
2538 | 094eed6c | bellard | tty.c_cflag |= CSTOPB; |
2539 | 3b46e624 | ths | |
2540 | f8d179e3 | bellard | tcsetattr (fd, TCSANOW, &tty); |
2541 | f8d179e3 | bellard | } |
2542 | f8d179e3 | bellard | |
2543 | e57a8c0e | bellard | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
2544 | f8d179e3 | bellard | { |
2545 | f8d179e3 | bellard | FDCharDriver *s = chr->opaque; |
2546 | 3b46e624 | ths | |
2547 | e57a8c0e | bellard | switch(cmd) {
|
2548 | e57a8c0e | bellard | case CHR_IOCTL_SERIAL_SET_PARAMS:
|
2549 | e57a8c0e | bellard | { |
2550 | e57a8c0e | bellard | QEMUSerialSetParams *ssp = arg; |
2551 | 5fafdf24 | ths | tty_serial_init(s->fd_in, ssp->speed, ssp->parity, |
2552 | e57a8c0e | bellard | ssp->data_bits, ssp->stop_bits); |
2553 | e57a8c0e | bellard | } |
2554 | e57a8c0e | bellard | break;
|
2555 | e57a8c0e | bellard | case CHR_IOCTL_SERIAL_SET_BREAK:
|
2556 | e57a8c0e | bellard | { |
2557 | e57a8c0e | bellard | int enable = *(int *)arg; |
2558 | e57a8c0e | bellard | if (enable)
|
2559 | e57a8c0e | bellard | tcsendbreak(s->fd_in, 1);
|
2560 | e57a8c0e | bellard | } |
2561 | e57a8c0e | bellard | break;
|
2562 | e57a8c0e | bellard | default:
|
2563 | e57a8c0e | bellard | return -ENOTSUP;
|
2564 | e57a8c0e | bellard | } |
2565 | e57a8c0e | bellard | return 0; |
2566 | f8d179e3 | bellard | } |
2567 | f8d179e3 | bellard | |
2568 | 52f61fde | ths | static CharDriverState *qemu_chr_open_tty(const char *filename) |
2569 | f8d179e3 | bellard | { |
2570 | f8d179e3 | bellard | CharDriverState *chr; |
2571 | f8d179e3 | bellard | int fd;
|
2572 | f8d179e3 | bellard | |
2573 | aeb30be6 | balrog | TFR(fd = open(filename, O_RDWR | O_NONBLOCK)); |
2574 | f8d179e3 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
2575 | f8d179e3 | bellard | tty_serial_init(fd, 115200, 'N', 8, 1); |
2576 | f8d179e3 | bellard | chr = qemu_chr_open_fd(fd, fd); |
2577 | aeb30be6 | balrog | if (!chr) {
|
2578 | aeb30be6 | balrog | close(fd); |
2579 | f8d179e3 | bellard | return NULL; |
2580 | aeb30be6 | balrog | } |
2581 | e57a8c0e | bellard | chr->chr_ioctl = tty_serial_ioctl; |
2582 | 86e94dea | ths | qemu_chr_reset(chr); |
2583 | e57a8c0e | bellard | return chr;
|
2584 | e57a8c0e | bellard | } |
2585 | aec62507 | ths | #else /* ! __linux__ && ! __sun__ */ |
2586 | aec62507 | ths | static CharDriverState *qemu_chr_open_pty(void) |
2587 | aec62507 | ths | { |
2588 | aec62507 | ths | return NULL; |
2589 | aec62507 | ths | } |
2590 | aec62507 | ths | #endif /* __linux__ || __sun__ */ |
2591 | e57a8c0e | bellard | |
2592 | aec62507 | ths | #if defined(__linux__)
|
2593 | 5867c88a | ths | typedef struct { |
2594 | 5867c88a | ths | int fd;
|
2595 | 5867c88a | ths | int mode;
|
2596 | 5867c88a | ths | } ParallelCharDriver; |
2597 | 5867c88a | ths | |
2598 | 5867c88a | ths | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
2599 | 5867c88a | ths | { |
2600 | 5867c88a | ths | if (s->mode != mode) {
|
2601 | 5867c88a | ths | int m = mode;
|
2602 | 5867c88a | ths | if (ioctl(s->fd, PPSETMODE, &m) < 0) |
2603 | 5867c88a | ths | return 0; |
2604 | 5867c88a | ths | s->mode = mode; |
2605 | 5867c88a | ths | } |
2606 | 5867c88a | ths | return 1; |
2607 | 5867c88a | ths | } |
2608 | 5867c88a | ths | |
2609 | e57a8c0e | bellard | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
2610 | e57a8c0e | bellard | { |
2611 | 5867c88a | ths | ParallelCharDriver *drv = chr->opaque; |
2612 | 5867c88a | ths | int fd = drv->fd;
|
2613 | e57a8c0e | bellard | uint8_t b; |
2614 | e57a8c0e | bellard | |
2615 | e57a8c0e | bellard | switch(cmd) {
|
2616 | e57a8c0e | bellard | case CHR_IOCTL_PP_READ_DATA:
|
2617 | e57a8c0e | bellard | if (ioctl(fd, PPRDATA, &b) < 0) |
2618 | e57a8c0e | bellard | return -ENOTSUP;
|
2619 | e57a8c0e | bellard | *(uint8_t *)arg = b; |
2620 | e57a8c0e | bellard | break;
|
2621 | e57a8c0e | bellard | case CHR_IOCTL_PP_WRITE_DATA:
|
2622 | e57a8c0e | bellard | b = *(uint8_t *)arg; |
2623 | e57a8c0e | bellard | if (ioctl(fd, PPWDATA, &b) < 0) |
2624 | e57a8c0e | bellard | return -ENOTSUP;
|
2625 | e57a8c0e | bellard | break;
|
2626 | e57a8c0e | bellard | case CHR_IOCTL_PP_READ_CONTROL:
|
2627 | e57a8c0e | bellard | if (ioctl(fd, PPRCONTROL, &b) < 0) |
2628 | e57a8c0e | bellard | return -ENOTSUP;
|
2629 | 5867c88a | ths | /* Linux gives only the lowest bits, and no way to know data
|
2630 | 5867c88a | ths | direction! For better compatibility set the fixed upper
|
2631 | 5867c88a | ths | bits. */
|
2632 | 5867c88a | ths | *(uint8_t *)arg = b | 0xc0;
|
2633 | e57a8c0e | bellard | break;
|
2634 | e57a8c0e | bellard | case CHR_IOCTL_PP_WRITE_CONTROL:
|
2635 | e57a8c0e | bellard | b = *(uint8_t *)arg; |
2636 | e57a8c0e | bellard | if (ioctl(fd, PPWCONTROL, &b) < 0) |
2637 | e57a8c0e | bellard | return -ENOTSUP;
|
2638 | e57a8c0e | bellard | break;
|
2639 | e57a8c0e | bellard | case CHR_IOCTL_PP_READ_STATUS:
|
2640 | e57a8c0e | bellard | if (ioctl(fd, PPRSTATUS, &b) < 0) |
2641 | e57a8c0e | bellard | return -ENOTSUP;
|
2642 | e57a8c0e | bellard | *(uint8_t *)arg = b; |
2643 | e57a8c0e | bellard | break;
|
2644 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_READ_ADDR:
|
2645 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
2646 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2647 | 5867c88a | ths | int n = read(fd, parg->buffer, parg->count);
|
2648 | 5867c88a | ths | if (n != parg->count) {
|
2649 | 5867c88a | ths | return -EIO;
|
2650 | 5867c88a | ths | } |
2651 | 5867c88a | ths | } |
2652 | 5867c88a | ths | break;
|
2653 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_READ:
|
2654 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
2655 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2656 | 5867c88a | ths | int n = read(fd, parg->buffer, parg->count);
|
2657 | 5867c88a | ths | if (n != parg->count) {
|
2658 | 5867c88a | ths | return -EIO;
|
2659 | 5867c88a | ths | } |
2660 | 5867c88a | ths | } |
2661 | 5867c88a | ths | break;
|
2662 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_WRITE_ADDR:
|
2663 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
2664 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2665 | 5867c88a | ths | int n = write(fd, parg->buffer, parg->count);
|
2666 | 5867c88a | ths | if (n != parg->count) {
|
2667 | 5867c88a | ths | return -EIO;
|
2668 | 5867c88a | ths | } |
2669 | 5867c88a | ths | } |
2670 | 5867c88a | ths | break;
|
2671 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_WRITE:
|
2672 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
2673 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2674 | 5867c88a | ths | int n = write(fd, parg->buffer, parg->count);
|
2675 | 5867c88a | ths | if (n != parg->count) {
|
2676 | 5867c88a | ths | return -EIO;
|
2677 | 5867c88a | ths | } |
2678 | 5867c88a | ths | } |
2679 | 5867c88a | ths | break;
|
2680 | e57a8c0e | bellard | default:
|
2681 | e57a8c0e | bellard | return -ENOTSUP;
|
2682 | e57a8c0e | bellard | } |
2683 | e57a8c0e | bellard | return 0; |
2684 | e57a8c0e | bellard | } |
2685 | e57a8c0e | bellard | |
2686 | 5867c88a | ths | static void pp_close(CharDriverState *chr) |
2687 | 5867c88a | ths | { |
2688 | 5867c88a | ths | ParallelCharDriver *drv = chr->opaque; |
2689 | 5867c88a | ths | int fd = drv->fd;
|
2690 | 5867c88a | ths | |
2691 | 5867c88a | ths | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
2692 | 5867c88a | ths | ioctl(fd, PPRELEASE); |
2693 | 5867c88a | ths | close(fd); |
2694 | 5867c88a | ths | qemu_free(drv); |
2695 | 5867c88a | ths | } |
2696 | 5867c88a | ths | |
2697 | 52f61fde | ths | static CharDriverState *qemu_chr_open_pp(const char *filename) |
2698 | e57a8c0e | bellard | { |
2699 | e57a8c0e | bellard | CharDriverState *chr; |
2700 | 5867c88a | ths | ParallelCharDriver *drv; |
2701 | e57a8c0e | bellard | int fd;
|
2702 | e57a8c0e | bellard | |
2703 | aeb30be6 | balrog | TFR(fd = open(filename, O_RDWR)); |
2704 | e57a8c0e | bellard | if (fd < 0) |
2705 | e57a8c0e | bellard | return NULL; |
2706 | e57a8c0e | bellard | |
2707 | e57a8c0e | bellard | if (ioctl(fd, PPCLAIM) < 0) { |
2708 | e57a8c0e | bellard | close(fd); |
2709 | e57a8c0e | bellard | return NULL; |
2710 | e57a8c0e | bellard | } |
2711 | e57a8c0e | bellard | |
2712 | 5867c88a | ths | drv = qemu_mallocz(sizeof(ParallelCharDriver));
|
2713 | 5867c88a | ths | if (!drv) {
|
2714 | 5867c88a | ths | close(fd); |
2715 | 5867c88a | ths | return NULL; |
2716 | 5867c88a | ths | } |
2717 | 5867c88a | ths | drv->fd = fd; |
2718 | 5867c88a | ths | drv->mode = IEEE1284_MODE_COMPAT; |
2719 | 5867c88a | ths | |
2720 | e57a8c0e | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
2721 | e57a8c0e | bellard | if (!chr) {
|
2722 | 5867c88a | ths | qemu_free(drv); |
2723 | e57a8c0e | bellard | close(fd); |
2724 | e57a8c0e | bellard | return NULL; |
2725 | e57a8c0e | bellard | } |
2726 | e57a8c0e | bellard | chr->chr_write = null_chr_write; |
2727 | e57a8c0e | bellard | chr->chr_ioctl = pp_ioctl; |
2728 | 5867c88a | ths | chr->chr_close = pp_close; |
2729 | 5867c88a | ths | chr->opaque = drv; |
2730 | 86e94dea | ths | |
2731 | 86e94dea | ths | qemu_chr_reset(chr); |
2732 | 86e94dea | ths | |
2733 | f8d179e3 | bellard | return chr;
|
2734 | f8d179e3 | bellard | } |
2735 | aec62507 | ths | #endif /* __linux__ */ |
2736 | f8d179e3 | bellard | |
2737 | aec62507 | ths | #else /* _WIN32 */ |
2738 | 82c643ff | bellard | |
2739 | f331110f | bellard | typedef struct { |
2740 | f331110f | bellard | int max_size;
|
2741 | f331110f | bellard | HANDLE hcom, hrecv, hsend; |
2742 | f331110f | bellard | OVERLAPPED orecv, osend; |
2743 | f331110f | bellard | BOOL fpipe; |
2744 | f331110f | bellard | DWORD len; |
2745 | f331110f | bellard | } WinCharState; |
2746 | f331110f | bellard | |
2747 | f331110f | bellard | #define NSENDBUF 2048 |
2748 | f331110f | bellard | #define NRECVBUF 2048 |
2749 | f331110f | bellard | #define MAXCONNECT 1 |
2750 | f331110f | bellard | #define NTIMEOUT 5000 |
2751 | f331110f | bellard | |
2752 | f331110f | bellard | static int win_chr_poll(void *opaque); |
2753 | f331110f | bellard | static int win_chr_pipe_poll(void *opaque); |
2754 | f331110f | bellard | |
2755 | 087f4ae0 | ths | static void win_chr_close(CharDriverState *chr) |
2756 | f331110f | bellard | { |
2757 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2758 | 087f4ae0 | ths | |
2759 | f331110f | bellard | if (s->hsend) {
|
2760 | f331110f | bellard | CloseHandle(s->hsend); |
2761 | f331110f | bellard | s->hsend = NULL;
|
2762 | f331110f | bellard | } |
2763 | f331110f | bellard | if (s->hrecv) {
|
2764 | f331110f | bellard | CloseHandle(s->hrecv); |
2765 | f331110f | bellard | s->hrecv = NULL;
|
2766 | f331110f | bellard | } |
2767 | f331110f | bellard | if (s->hcom) {
|
2768 | f331110f | bellard | CloseHandle(s->hcom); |
2769 | f331110f | bellard | s->hcom = NULL;
|
2770 | f331110f | bellard | } |
2771 | f331110f | bellard | if (s->fpipe)
|
2772 | 087f4ae0 | ths | qemu_del_polling_cb(win_chr_pipe_poll, chr); |
2773 | f331110f | bellard | else
|
2774 | 087f4ae0 | ths | qemu_del_polling_cb(win_chr_poll, chr); |
2775 | f331110f | bellard | } |
2776 | f331110f | bellard | |
2777 | 087f4ae0 | ths | static int win_chr_init(CharDriverState *chr, const char *filename) |
2778 | f331110f | bellard | { |
2779 | f331110f | bellard | WinCharState *s = chr->opaque; |
2780 | f331110f | bellard | COMMCONFIG comcfg; |
2781 | f331110f | bellard | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
2782 | f331110f | bellard | COMSTAT comstat; |
2783 | f331110f | bellard | DWORD size; |
2784 | f331110f | bellard | DWORD err; |
2785 | 3b46e624 | ths | |
2786 | f331110f | bellard | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
2787 | f331110f | bellard | if (!s->hsend) {
|
2788 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
2789 | f331110f | bellard | goto fail;
|
2790 | f331110f | bellard | } |
2791 | f331110f | bellard | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
2792 | f331110f | bellard | if (!s->hrecv) {
|
2793 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
2794 | f331110f | bellard | goto fail;
|
2795 | f331110f | bellard | } |
2796 | f331110f | bellard | |
2797 | f331110f | bellard | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
2798 | f331110f | bellard | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
|
2799 | f331110f | bellard | if (s->hcom == INVALID_HANDLE_VALUE) {
|
2800 | f331110f | bellard | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
|
2801 | f331110f | bellard | s->hcom = NULL;
|
2802 | f331110f | bellard | goto fail;
|
2803 | f331110f | bellard | } |
2804 | 3b46e624 | ths | |
2805 | f331110f | bellard | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
|
2806 | f331110f | bellard | fprintf(stderr, "Failed SetupComm\n");
|
2807 | f331110f | bellard | goto fail;
|
2808 | f331110f | bellard | } |
2809 | 3b46e624 | ths | |
2810 | f331110f | bellard | ZeroMemory(&comcfg, sizeof(COMMCONFIG));
|
2811 | f331110f | bellard | size = sizeof(COMMCONFIG);
|
2812 | f331110f | bellard | GetDefaultCommConfig(filename, &comcfg, &size); |
2813 | f331110f | bellard | comcfg.dcb.DCBlength = sizeof(DCB);
|
2814 | f331110f | bellard | CommConfigDialog(filename, NULL, &comcfg);
|
2815 | f331110f | bellard | |
2816 | f331110f | bellard | if (!SetCommState(s->hcom, &comcfg.dcb)) {
|
2817 | f331110f | bellard | fprintf(stderr, "Failed SetCommState\n");
|
2818 | f331110f | bellard | goto fail;
|
2819 | f331110f | bellard | } |
2820 | f331110f | bellard | |
2821 | f331110f | bellard | if (!SetCommMask(s->hcom, EV_ERR)) {
|
2822 | f331110f | bellard | fprintf(stderr, "Failed SetCommMask\n");
|
2823 | f331110f | bellard | goto fail;
|
2824 | f331110f | bellard | } |
2825 | f331110f | bellard | |
2826 | f331110f | bellard | cto.ReadIntervalTimeout = MAXDWORD; |
2827 | f331110f | bellard | if (!SetCommTimeouts(s->hcom, &cto)) {
|
2828 | f331110f | bellard | fprintf(stderr, "Failed SetCommTimeouts\n");
|
2829 | f331110f | bellard | goto fail;
|
2830 | f331110f | bellard | } |
2831 | 3b46e624 | ths | |
2832 | f331110f | bellard | if (!ClearCommError(s->hcom, &err, &comstat)) {
|
2833 | f331110f | bellard | fprintf(stderr, "Failed ClearCommError\n");
|
2834 | f331110f | bellard | goto fail;
|
2835 | f331110f | bellard | } |
2836 | 087f4ae0 | ths | qemu_add_polling_cb(win_chr_poll, chr); |
2837 | f331110f | bellard | return 0; |
2838 | f331110f | bellard | |
2839 | f331110f | bellard | fail:
|
2840 | 087f4ae0 | ths | win_chr_close(chr); |
2841 | f331110f | bellard | return -1; |
2842 | f331110f | bellard | } |
2843 | f331110f | bellard | |
2844 | f331110f | bellard | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
2845 | f331110f | bellard | { |
2846 | f331110f | bellard | WinCharState *s = chr->opaque; |
2847 | f331110f | bellard | DWORD len, ret, size, err; |
2848 | f331110f | bellard | |
2849 | f331110f | bellard | len = len1; |
2850 | f331110f | bellard | ZeroMemory(&s->osend, sizeof(s->osend));
|
2851 | f331110f | bellard | s->osend.hEvent = s->hsend; |
2852 | f331110f | bellard | while (len > 0) { |
2853 | f331110f | bellard | if (s->hsend)
|
2854 | f331110f | bellard | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
2855 | f331110f | bellard | else
|
2856 | f331110f | bellard | ret = WriteFile(s->hcom, buf, len, &size, NULL);
|
2857 | f331110f | bellard | if (!ret) {
|
2858 | f331110f | bellard | err = GetLastError(); |
2859 | f331110f | bellard | if (err == ERROR_IO_PENDING) {
|
2860 | f331110f | bellard | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
2861 | f331110f | bellard | if (ret) {
|
2862 | f331110f | bellard | buf += size; |
2863 | f331110f | bellard | len -= size; |
2864 | f331110f | bellard | } else {
|
2865 | f331110f | bellard | break;
|
2866 | f331110f | bellard | } |
2867 | f331110f | bellard | } else {
|
2868 | f331110f | bellard | break;
|
2869 | f331110f | bellard | } |
2870 | f331110f | bellard | } else {
|
2871 | f331110f | bellard | buf += size; |
2872 | f331110f | bellard | len -= size; |
2873 | f331110f | bellard | } |
2874 | f331110f | bellard | } |
2875 | f331110f | bellard | return len1 - len;
|
2876 | f331110f | bellard | } |
2877 | f331110f | bellard | |
2878 | 087f4ae0 | ths | static int win_chr_read_poll(CharDriverState *chr) |
2879 | f331110f | bellard | { |
2880 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2881 | 087f4ae0 | ths | |
2882 | 087f4ae0 | ths | s->max_size = qemu_chr_can_read(chr); |
2883 | f331110f | bellard | return s->max_size;
|
2884 | f331110f | bellard | } |
2885 | e5b0bc44 | pbrook | |
2886 | 087f4ae0 | ths | static void win_chr_readfile(CharDriverState *chr) |
2887 | f331110f | bellard | { |
2888 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2889 | f331110f | bellard | int ret, err;
|
2890 | f331110f | bellard | uint8_t buf[1024];
|
2891 | f331110f | bellard | DWORD size; |
2892 | 3b46e624 | ths | |
2893 | f331110f | bellard | ZeroMemory(&s->orecv, sizeof(s->orecv));
|
2894 | f331110f | bellard | s->orecv.hEvent = s->hrecv; |
2895 | f331110f | bellard | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
2896 | f331110f | bellard | if (!ret) {
|
2897 | f331110f | bellard | err = GetLastError(); |
2898 | f331110f | bellard | if (err == ERROR_IO_PENDING) {
|
2899 | f331110f | bellard | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
2900 | f331110f | bellard | } |
2901 | f331110f | bellard | } |
2902 | f331110f | bellard | |
2903 | f331110f | bellard | if (size > 0) { |
2904 | 087f4ae0 | ths | qemu_chr_read(chr, buf, size); |
2905 | f331110f | bellard | } |
2906 | f331110f | bellard | } |
2907 | f331110f | bellard | |
2908 | 087f4ae0 | ths | static void win_chr_read(CharDriverState *chr) |
2909 | f331110f | bellard | { |
2910 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2911 | 087f4ae0 | ths | |
2912 | f331110f | bellard | if (s->len > s->max_size)
|
2913 | f331110f | bellard | s->len = s->max_size; |
2914 | f331110f | bellard | if (s->len == 0) |
2915 | f331110f | bellard | return;
|
2916 | 3b46e624 | ths | |
2917 | 087f4ae0 | ths | win_chr_readfile(chr); |
2918 | f331110f | bellard | } |
2919 | f331110f | bellard | |
2920 | f331110f | bellard | static int win_chr_poll(void *opaque) |
2921 | f331110f | bellard | { |
2922 | 087f4ae0 | ths | CharDriverState *chr = opaque; |
2923 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2924 | f331110f | bellard | COMSTAT status; |
2925 | f331110f | bellard | DWORD comerr; |
2926 | 3b46e624 | ths | |
2927 | f331110f | bellard | ClearCommError(s->hcom, &comerr, &status); |
2928 | f331110f | bellard | if (status.cbInQue > 0) { |
2929 | f331110f | bellard | s->len = status.cbInQue; |
2930 | 087f4ae0 | ths | win_chr_read_poll(chr); |
2931 | 087f4ae0 | ths | win_chr_read(chr); |
2932 | f331110f | bellard | return 1; |
2933 | f331110f | bellard | } |
2934 | f331110f | bellard | return 0; |
2935 | f331110f | bellard | } |
2936 | f331110f | bellard | |
2937 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win(const char *filename) |
2938 | f331110f | bellard | { |
2939 | f331110f | bellard | CharDriverState *chr; |
2940 | f331110f | bellard | WinCharState *s; |
2941 | 3b46e624 | ths | |
2942 | f331110f | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
2943 | f331110f | bellard | if (!chr)
|
2944 | f331110f | bellard | return NULL; |
2945 | f331110f | bellard | s = qemu_mallocz(sizeof(WinCharState));
|
2946 | f331110f | bellard | if (!s) {
|
2947 | f331110f | bellard | free(chr); |
2948 | f331110f | bellard | return NULL; |
2949 | f331110f | bellard | } |
2950 | f331110f | bellard | chr->opaque = s; |
2951 | f331110f | bellard | chr->chr_write = win_chr_write; |
2952 | f331110f | bellard | chr->chr_close = win_chr_close; |
2953 | f331110f | bellard | |
2954 | 087f4ae0 | ths | if (win_chr_init(chr, filename) < 0) { |
2955 | f331110f | bellard | free(s); |
2956 | f331110f | bellard | free(chr); |
2957 | f331110f | bellard | return NULL; |
2958 | f331110f | bellard | } |
2959 | 86e94dea | ths | qemu_chr_reset(chr); |
2960 | f331110f | bellard | return chr;
|
2961 | f331110f | bellard | } |
2962 | f331110f | bellard | |
2963 | f331110f | bellard | static int win_chr_pipe_poll(void *opaque) |
2964 | f331110f | bellard | { |
2965 | 087f4ae0 | ths | CharDriverState *chr = opaque; |
2966 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2967 | f331110f | bellard | DWORD size; |
2968 | f331110f | bellard | |
2969 | f331110f | bellard | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
2970 | f331110f | bellard | if (size > 0) { |
2971 | f331110f | bellard | s->len = size; |
2972 | 087f4ae0 | ths | win_chr_read_poll(chr); |
2973 | 087f4ae0 | ths | win_chr_read(chr); |
2974 | f331110f | bellard | return 1; |
2975 | f331110f | bellard | } |
2976 | f331110f | bellard | return 0; |
2977 | f331110f | bellard | } |
2978 | f331110f | bellard | |
2979 | 087f4ae0 | ths | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) |
2980 | f331110f | bellard | { |
2981 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2982 | f331110f | bellard | OVERLAPPED ov; |
2983 | f331110f | bellard | int ret;
|
2984 | f331110f | bellard | DWORD size; |
2985 | f331110f | bellard | char openname[256]; |
2986 | 3b46e624 | ths | |
2987 | f331110f | bellard | s->fpipe = TRUE; |
2988 | f331110f | bellard | |
2989 | f331110f | bellard | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
2990 | f331110f | bellard | if (!s->hsend) {
|
2991 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
2992 | f331110f | bellard | goto fail;
|
2993 | f331110f | bellard | } |
2994 | f331110f | bellard | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
2995 | f331110f | bellard | if (!s->hrecv) {
|
2996 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
2997 | f331110f | bellard | goto fail;
|
2998 | f331110f | bellard | } |
2999 | 3b46e624 | ths | |
3000 | f331110f | bellard | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
3001 | f331110f | bellard | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
3002 | f331110f | bellard | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
3003 | f331110f | bellard | PIPE_WAIT, |
3004 | f331110f | bellard | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
|
3005 | f331110f | bellard | if (s->hcom == INVALID_HANDLE_VALUE) {
|
3006 | f331110f | bellard | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
|
3007 | f331110f | bellard | s->hcom = NULL;
|
3008 | f331110f | bellard | goto fail;
|
3009 | f331110f | bellard | } |
3010 | f331110f | bellard | |
3011 | f331110f | bellard | ZeroMemory(&ov, sizeof(ov));
|
3012 | f331110f | bellard | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
3013 | f331110f | bellard | ret = ConnectNamedPipe(s->hcom, &ov); |
3014 | f331110f | bellard | if (ret) {
|
3015 | f331110f | bellard | fprintf(stderr, "Failed ConnectNamedPipe\n");
|
3016 | f331110f | bellard | goto fail;
|
3017 | f331110f | bellard | } |
3018 | f331110f | bellard | |
3019 | f331110f | bellard | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
3020 | f331110f | bellard | if (!ret) {
|
3021 | f331110f | bellard | fprintf(stderr, "Failed GetOverlappedResult\n");
|
3022 | f331110f | bellard | if (ov.hEvent) {
|
3023 | f331110f | bellard | CloseHandle(ov.hEvent); |
3024 | f331110f | bellard | ov.hEvent = NULL;
|
3025 | f331110f | bellard | } |
3026 | f331110f | bellard | goto fail;
|
3027 | f331110f | bellard | } |
3028 | f331110f | bellard | |
3029 | f331110f | bellard | if (ov.hEvent) {
|
3030 | f331110f | bellard | CloseHandle(ov.hEvent); |
3031 | f331110f | bellard | ov.hEvent = NULL;
|
3032 | f331110f | bellard | } |
3033 | 087f4ae0 | ths | qemu_add_polling_cb(win_chr_pipe_poll, chr); |
3034 | f331110f | bellard | return 0; |
3035 | f331110f | bellard | |
3036 | f331110f | bellard | fail:
|
3037 | 087f4ae0 | ths | win_chr_close(chr); |
3038 | f331110f | bellard | return -1; |
3039 | f331110f | bellard | } |
3040 | f331110f | bellard | |
3041 | f331110f | bellard | |
3042 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win_pipe(const char *filename) |
3043 | f331110f | bellard | { |
3044 | f331110f | bellard | CharDriverState *chr; |
3045 | f331110f | bellard | WinCharState *s; |
3046 | f331110f | bellard | |
3047 | f331110f | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3048 | f331110f | bellard | if (!chr)
|
3049 | f331110f | bellard | return NULL; |
3050 | f331110f | bellard | s = qemu_mallocz(sizeof(WinCharState));
|
3051 | f331110f | bellard | if (!s) {
|
3052 | f331110f | bellard | free(chr); |
3053 | f331110f | bellard | return NULL; |
3054 | f331110f | bellard | } |
3055 | f331110f | bellard | chr->opaque = s; |
3056 | f331110f | bellard | chr->chr_write = win_chr_write; |
3057 | f331110f | bellard | chr->chr_close = win_chr_close; |
3058 | 3b46e624 | ths | |
3059 | 087f4ae0 | ths | if (win_chr_pipe_init(chr, filename) < 0) { |
3060 | f331110f | bellard | free(s); |
3061 | f331110f | bellard | free(chr); |
3062 | f331110f | bellard | return NULL; |
3063 | f331110f | bellard | } |
3064 | 86e94dea | ths | qemu_chr_reset(chr); |
3065 | f331110f | bellard | return chr;
|
3066 | f331110f | bellard | } |
3067 | f331110f | bellard | |
3068 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
|
3069 | f331110f | bellard | { |
3070 | f331110f | bellard | CharDriverState *chr; |
3071 | f331110f | bellard | WinCharState *s; |
3072 | f331110f | bellard | |
3073 | f331110f | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3074 | f331110f | bellard | if (!chr)
|
3075 | f331110f | bellard | return NULL; |
3076 | f331110f | bellard | s = qemu_mallocz(sizeof(WinCharState));
|
3077 | f331110f | bellard | if (!s) {
|
3078 | f331110f | bellard | free(chr); |
3079 | f331110f | bellard | return NULL; |
3080 | f331110f | bellard | } |
3081 | f331110f | bellard | s->hcom = fd_out; |
3082 | f331110f | bellard | chr->opaque = s; |
3083 | f331110f | bellard | chr->chr_write = win_chr_write; |
3084 | 86e94dea | ths | qemu_chr_reset(chr); |
3085 | f331110f | bellard | return chr;
|
3086 | f331110f | bellard | } |
3087 | 72d46479 | ths | |
3088 | 72d46479 | ths | static CharDriverState *qemu_chr_open_win_con(const char *filename) |
3089 | 72d46479 | ths | { |
3090 | 72d46479 | ths | return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
|
3091 | 72d46479 | ths | } |
3092 | 72d46479 | ths | |
3093 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win_file_out(const char *file_out) |
3094 | f331110f | bellard | { |
3095 | f331110f | bellard | HANDLE fd_out; |
3096 | 3b46e624 | ths | |
3097 | f331110f | bellard | fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
|
3098 | f331110f | bellard | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
3099 | f331110f | bellard | if (fd_out == INVALID_HANDLE_VALUE)
|
3100 | f331110f | bellard | return NULL; |
3101 | f331110f | bellard | |
3102 | f331110f | bellard | return qemu_chr_open_win_file(fd_out);
|
3103 | f331110f | bellard | } |
3104 | aec62507 | ths | #endif /* !_WIN32 */ |
3105 | f331110f | bellard | |
3106 | 0bab00f3 | bellard | /***********************************************************/
|
3107 | 0bab00f3 | bellard | /* UDP Net console */
|
3108 | 0bab00f3 | bellard | |
3109 | 0bab00f3 | bellard | typedef struct { |
3110 | 0bab00f3 | bellard | int fd;
|
3111 | 0bab00f3 | bellard | struct sockaddr_in daddr;
|
3112 | 60fe76f3 | ths | uint8_t buf[1024];
|
3113 | 0bab00f3 | bellard | int bufcnt;
|
3114 | 0bab00f3 | bellard | int bufptr;
|
3115 | 0bab00f3 | bellard | int max_size;
|
3116 | 0bab00f3 | bellard | } NetCharDriver; |
3117 | 0bab00f3 | bellard | |
3118 | 0bab00f3 | bellard | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
3119 | 0bab00f3 | bellard | { |
3120 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3121 | 0bab00f3 | bellard | |
3122 | 0bab00f3 | bellard | return sendto(s->fd, buf, len, 0, |
3123 | 0bab00f3 | bellard | (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in)); |
3124 | 0bab00f3 | bellard | } |
3125 | 0bab00f3 | bellard | |
3126 | 0bab00f3 | bellard | static int udp_chr_read_poll(void *opaque) |
3127 | 0bab00f3 | bellard | { |
3128 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3129 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3130 | 0bab00f3 | bellard | |
3131 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3132 | 0bab00f3 | bellard | |
3133 | 0bab00f3 | bellard | /* If there were any stray characters in the queue process them
|
3134 | 0bab00f3 | bellard | * first
|
3135 | 0bab00f3 | bellard | */
|
3136 | 0bab00f3 | bellard | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
3137 | e5b0bc44 | pbrook | qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
3138 | 0bab00f3 | bellard | s->bufptr++; |
3139 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3140 | 0bab00f3 | bellard | } |
3141 | 0bab00f3 | bellard | return s->max_size;
|
3142 | 0bab00f3 | bellard | } |
3143 | 0bab00f3 | bellard | |
3144 | 0bab00f3 | bellard | static void udp_chr_read(void *opaque) |
3145 | 0bab00f3 | bellard | { |
3146 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3147 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3148 | 0bab00f3 | bellard | |
3149 | 0bab00f3 | bellard | if (s->max_size == 0) |
3150 | 0bab00f3 | bellard | return;
|
3151 | 0bab00f3 | bellard | s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0); |
3152 | 0bab00f3 | bellard | s->bufptr = s->bufcnt; |
3153 | 0bab00f3 | bellard | if (s->bufcnt <= 0) |
3154 | 0bab00f3 | bellard | return;
|
3155 | 0bab00f3 | bellard | |
3156 | 0bab00f3 | bellard | s->bufptr = 0;
|
3157 | 0bab00f3 | bellard | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
3158 | e5b0bc44 | pbrook | qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
3159 | 0bab00f3 | bellard | s->bufptr++; |
3160 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3161 | 0bab00f3 | bellard | } |
3162 | 0bab00f3 | bellard | } |
3163 | 0bab00f3 | bellard | |
3164 | e5b0bc44 | pbrook | static void udp_chr_update_read_handler(CharDriverState *chr) |
3165 | 0bab00f3 | bellard | { |
3166 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3167 | 0bab00f3 | bellard | |
3168 | 0bab00f3 | bellard | if (s->fd >= 0) { |
3169 | 0bab00f3 | bellard | qemu_set_fd_handler2(s->fd, udp_chr_read_poll, |
3170 | 0bab00f3 | bellard | udp_chr_read, NULL, chr);
|
3171 | 0bab00f3 | bellard | } |
3172 | 0bab00f3 | bellard | } |
3173 | 0bab00f3 | bellard | |
3174 | 0bab00f3 | bellard | int parse_host_port(struct sockaddr_in *saddr, const char *str); |
3175 | 52f61fde | ths | #ifndef _WIN32
|
3176 | 52f61fde | ths | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str); |
3177 | 52f61fde | ths | #endif
|
3178 | 951f1351 | bellard | int parse_host_src_port(struct sockaddr_in *haddr, |
3179 | 951f1351 | bellard | struct sockaddr_in *saddr,
|
3180 | 951f1351 | bellard | const char *str); |
3181 | 0bab00f3 | bellard | |
3182 | 52f61fde | ths | static CharDriverState *qemu_chr_open_udp(const char *def) |
3183 | 0bab00f3 | bellard | { |
3184 | 0bab00f3 | bellard | CharDriverState *chr = NULL;
|
3185 | 0bab00f3 | bellard | NetCharDriver *s = NULL;
|
3186 | 0bab00f3 | bellard | int fd = -1; |
3187 | 951f1351 | bellard | struct sockaddr_in saddr;
|
3188 | 0bab00f3 | bellard | |
3189 | 0bab00f3 | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3190 | 0bab00f3 | bellard | if (!chr)
|
3191 | 0bab00f3 | bellard | goto return_err;
|
3192 | 0bab00f3 | bellard | s = qemu_mallocz(sizeof(NetCharDriver));
|
3193 | 0bab00f3 | bellard | if (!s)
|
3194 | 0bab00f3 | bellard | goto return_err;
|
3195 | 0bab00f3 | bellard | |
3196 | 0bab00f3 | bellard | fd = socket(PF_INET, SOCK_DGRAM, 0);
|
3197 | 0bab00f3 | bellard | if (fd < 0) { |
3198 | 0bab00f3 | bellard | perror("socket(PF_INET, SOCK_DGRAM)");
|
3199 | 0bab00f3 | bellard | goto return_err;
|
3200 | 0bab00f3 | bellard | } |
3201 | 0bab00f3 | bellard | |
3202 | 951f1351 | bellard | if (parse_host_src_port(&s->daddr, &saddr, def) < 0) { |
3203 | 951f1351 | bellard | printf("Could not parse: %s\n", def);
|
3204 | 951f1351 | bellard | goto return_err;
|
3205 | 0bab00f3 | bellard | } |
3206 | 0bab00f3 | bellard | |
3207 | 951f1351 | bellard | if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) |
3208 | 0bab00f3 | bellard | { |
3209 | 0bab00f3 | bellard | perror("bind");
|
3210 | 0bab00f3 | bellard | goto return_err;
|
3211 | 0bab00f3 | bellard | } |
3212 | 0bab00f3 | bellard | |
3213 | 0bab00f3 | bellard | s->fd = fd; |
3214 | 0bab00f3 | bellard | s->bufcnt = 0;
|
3215 | 0bab00f3 | bellard | s->bufptr = 0;
|
3216 | 0bab00f3 | bellard | chr->opaque = s; |
3217 | 0bab00f3 | bellard | chr->chr_write = udp_chr_write; |
3218 | e5b0bc44 | pbrook | chr->chr_update_read_handler = udp_chr_update_read_handler; |
3219 | 0bab00f3 | bellard | return chr;
|
3220 | 0bab00f3 | bellard | |
3221 | 0bab00f3 | bellard | return_err:
|
3222 | 0bab00f3 | bellard | if (chr)
|
3223 | 0bab00f3 | bellard | free(chr); |
3224 | 0bab00f3 | bellard | if (s)
|
3225 | 0bab00f3 | bellard | free(s); |
3226 | 0bab00f3 | bellard | if (fd >= 0) |
3227 | 0bab00f3 | bellard | closesocket(fd); |
3228 | 0bab00f3 | bellard | return NULL; |
3229 | 0bab00f3 | bellard | } |
3230 | 0bab00f3 | bellard | |
3231 | 0bab00f3 | bellard | /***********************************************************/
|
3232 | 0bab00f3 | bellard | /* TCP Net console */
|
3233 | 0bab00f3 | bellard | |
3234 | 0bab00f3 | bellard | typedef struct { |
3235 | 0bab00f3 | bellard | int fd, listen_fd;
|
3236 | 0bab00f3 | bellard | int connected;
|
3237 | 0bab00f3 | bellard | int max_size;
|
3238 | 951f1351 | bellard | int do_telnetopt;
|
3239 | e5b0bc44 | pbrook | int do_nodelay;
|
3240 | ffd843bc | ths | int is_unix;
|
3241 | 0bab00f3 | bellard | } TCPCharDriver; |
3242 | 0bab00f3 | bellard | |
3243 | 0bab00f3 | bellard | static void tcp_chr_accept(void *opaque); |
3244 | 0bab00f3 | bellard | |
3245 | 0bab00f3 | bellard | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
3246 | 0bab00f3 | bellard | { |
3247 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3248 | 0bab00f3 | bellard | if (s->connected) {
|
3249 | 0bab00f3 | bellard | return send_all(s->fd, buf, len);
|
3250 | 0bab00f3 | bellard | } else {
|
3251 | 0bab00f3 | bellard | /* XXX: indicate an error ? */
|
3252 | 0bab00f3 | bellard | return len;
|
3253 | 0bab00f3 | bellard | } |
3254 | 0bab00f3 | bellard | } |
3255 | 0bab00f3 | bellard | |
3256 | 0bab00f3 | bellard | static int tcp_chr_read_poll(void *opaque) |
3257 | 0bab00f3 | bellard | { |
3258 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3259 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3260 | 0bab00f3 | bellard | if (!s->connected)
|
3261 | 0bab00f3 | bellard | return 0; |
3262 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3263 | 0bab00f3 | bellard | return s->max_size;
|
3264 | 0bab00f3 | bellard | } |
3265 | 0bab00f3 | bellard | |
3266 | 951f1351 | bellard | #define IAC 255 |
3267 | 951f1351 | bellard | #define IAC_BREAK 243 |
3268 | 951f1351 | bellard | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
3269 | 951f1351 | bellard | TCPCharDriver *s, |
3270 | 60fe76f3 | ths | uint8_t *buf, int *size)
|
3271 | 951f1351 | bellard | { |
3272 | 951f1351 | bellard | /* Handle any telnet client's basic IAC options to satisfy char by
|
3273 | 951f1351 | bellard | * char mode with no echo. All IAC options will be removed from
|
3274 | 951f1351 | bellard | * the buf and the do_telnetopt variable will be used to track the
|
3275 | 951f1351 | bellard | * state of the width of the IAC information.
|
3276 | 951f1351 | bellard | *
|
3277 | 951f1351 | bellard | * IAC commands come in sets of 3 bytes with the exception of the
|
3278 | 951f1351 | bellard | * "IAC BREAK" command and the double IAC.
|
3279 | 951f1351 | bellard | */
|
3280 | 951f1351 | bellard | |
3281 | 951f1351 | bellard | int i;
|
3282 | 951f1351 | bellard | int j = 0; |
3283 | 951f1351 | bellard | |
3284 | 951f1351 | bellard | for (i = 0; i < *size; i++) { |
3285 | 951f1351 | bellard | if (s->do_telnetopt > 1) { |
3286 | 951f1351 | bellard | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
3287 | 951f1351 | bellard | /* Double IAC means send an IAC */
|
3288 | 951f1351 | bellard | if (j != i)
|
3289 | 951f1351 | bellard | buf[j] = buf[i]; |
3290 | 951f1351 | bellard | j++; |
3291 | 951f1351 | bellard | s->do_telnetopt = 1;
|
3292 | 951f1351 | bellard | } else {
|
3293 | 951f1351 | bellard | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
3294 | 951f1351 | bellard | /* Handle IAC break commands by sending a serial break */
|
3295 | e5b0bc44 | pbrook | qemu_chr_event(chr, CHR_EVENT_BREAK); |
3296 | 951f1351 | bellard | s->do_telnetopt++; |
3297 | 951f1351 | bellard | } |
3298 | 951f1351 | bellard | s->do_telnetopt++; |
3299 | 951f1351 | bellard | } |
3300 | 951f1351 | bellard | if (s->do_telnetopt >= 4) { |
3301 | 951f1351 | bellard | s->do_telnetopt = 1;
|
3302 | 951f1351 | bellard | } |
3303 | 951f1351 | bellard | } else {
|
3304 | 951f1351 | bellard | if ((unsigned char)buf[i] == IAC) { |
3305 | 951f1351 | bellard | s->do_telnetopt = 2;
|
3306 | 951f1351 | bellard | } else {
|
3307 | 951f1351 | bellard | if (j != i)
|
3308 | 951f1351 | bellard | buf[j] = buf[i]; |
3309 | 951f1351 | bellard | j++; |
3310 | 951f1351 | bellard | } |
3311 | 951f1351 | bellard | } |
3312 | 951f1351 | bellard | } |
3313 | 951f1351 | bellard | *size = j; |
3314 | 951f1351 | bellard | } |
3315 | 951f1351 | bellard | |
3316 | 0bab00f3 | bellard | static void tcp_chr_read(void *opaque) |
3317 | 0bab00f3 | bellard | { |
3318 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3319 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3320 | 0bab00f3 | bellard | uint8_t buf[1024];
|
3321 | 0bab00f3 | bellard | int len, size;
|
3322 | 0bab00f3 | bellard | |
3323 | 0bab00f3 | bellard | if (!s->connected || s->max_size <= 0) |
3324 | 0bab00f3 | bellard | return;
|
3325 | 0bab00f3 | bellard | len = sizeof(buf);
|
3326 | 0bab00f3 | bellard | if (len > s->max_size)
|
3327 | 0bab00f3 | bellard | len = s->max_size; |
3328 | 0bab00f3 | bellard | size = recv(s->fd, buf, len, 0);
|
3329 | 0bab00f3 | bellard | if (size == 0) { |
3330 | 0bab00f3 | bellard | /* connection closed */
|
3331 | 0bab00f3 | bellard | s->connected = 0;
|
3332 | 0bab00f3 | bellard | if (s->listen_fd >= 0) { |
3333 | 0bab00f3 | bellard | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
3334 | 0bab00f3 | bellard | } |
3335 | 0bab00f3 | bellard | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
3336 | 0bab00f3 | bellard | closesocket(s->fd); |
3337 | 0bab00f3 | bellard | s->fd = -1;
|
3338 | 0bab00f3 | bellard | } else if (size > 0) { |
3339 | 951f1351 | bellard | if (s->do_telnetopt)
|
3340 | 951f1351 | bellard | tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
3341 | 951f1351 | bellard | if (size > 0) |
3342 | e5b0bc44 | pbrook | qemu_chr_read(chr, buf, size); |
3343 | 0bab00f3 | bellard | } |
3344 | 0bab00f3 | bellard | } |
3345 | 0bab00f3 | bellard | |
3346 | 0bab00f3 | bellard | static void tcp_chr_connect(void *opaque) |
3347 | 0bab00f3 | bellard | { |
3348 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3349 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3350 | 0bab00f3 | bellard | |
3351 | 0bab00f3 | bellard | s->connected = 1;
|
3352 | 0bab00f3 | bellard | qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, |
3353 | 0bab00f3 | bellard | tcp_chr_read, NULL, chr);
|
3354 | 86e94dea | ths | qemu_chr_reset(chr); |
3355 | 0bab00f3 | bellard | } |
3356 | 0bab00f3 | bellard | |
3357 | 951f1351 | bellard | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
3358 | 951f1351 | bellard | static void tcp_chr_telnet_init(int fd) |
3359 | 951f1351 | bellard | { |
3360 | 951f1351 | bellard | char buf[3]; |
3361 | 951f1351 | bellard | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
|
3362 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
3363 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3364 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
3365 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3366 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
3367 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3368 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
3369 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3370 | 951f1351 | bellard | } |
3371 | 951f1351 | bellard | |
3372 | f7499989 | pbrook | static void socket_set_nodelay(int fd) |
3373 | f7499989 | pbrook | { |
3374 | f7499989 | pbrook | int val = 1; |
3375 | f7499989 | pbrook | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
3376 | f7499989 | pbrook | } |
3377 | f7499989 | pbrook | |
3378 | 0bab00f3 | bellard | static void tcp_chr_accept(void *opaque) |
3379 | 0bab00f3 | bellard | { |
3380 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3381 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3382 | 0bab00f3 | bellard | struct sockaddr_in saddr;
|
3383 | ffd843bc | ths | #ifndef _WIN32
|
3384 | ffd843bc | ths | struct sockaddr_un uaddr;
|
3385 | ffd843bc | ths | #endif
|
3386 | ffd843bc | ths | struct sockaddr *addr;
|
3387 | 0bab00f3 | bellard | socklen_t len; |
3388 | 0bab00f3 | bellard | int fd;
|
3389 | 0bab00f3 | bellard | |
3390 | 0bab00f3 | bellard | for(;;) {
|
3391 | ffd843bc | ths | #ifndef _WIN32
|
3392 | ffd843bc | ths | if (s->is_unix) {
|
3393 | ffd843bc | ths | len = sizeof(uaddr);
|
3394 | ffd843bc | ths | addr = (struct sockaddr *)&uaddr;
|
3395 | ffd843bc | ths | } else
|
3396 | ffd843bc | ths | #endif
|
3397 | ffd843bc | ths | { |
3398 | ffd843bc | ths | len = sizeof(saddr);
|
3399 | ffd843bc | ths | addr = (struct sockaddr *)&saddr;
|
3400 | ffd843bc | ths | } |
3401 | ffd843bc | ths | fd = accept(s->listen_fd, addr, &len); |
3402 | 0bab00f3 | bellard | if (fd < 0 && errno != EINTR) { |
3403 | 0bab00f3 | bellard | return;
|
3404 | 0bab00f3 | bellard | } else if (fd >= 0) { |
3405 | 951f1351 | bellard | if (s->do_telnetopt)
|
3406 | 951f1351 | bellard | tcp_chr_telnet_init(fd); |
3407 | 0bab00f3 | bellard | break;
|
3408 | 0bab00f3 | bellard | } |
3409 | 0bab00f3 | bellard | } |
3410 | 0bab00f3 | bellard | socket_set_nonblock(fd); |
3411 | f7499989 | pbrook | if (s->do_nodelay)
|
3412 | f7499989 | pbrook | socket_set_nodelay(fd); |
3413 | 0bab00f3 | bellard | s->fd = fd; |
3414 | 0bab00f3 | bellard | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
3415 | 0bab00f3 | bellard | tcp_chr_connect(chr); |
3416 | 0bab00f3 | bellard | } |
3417 | 0bab00f3 | bellard | |
3418 | 0bab00f3 | bellard | static void tcp_chr_close(CharDriverState *chr) |
3419 | 0bab00f3 | bellard | { |
3420 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3421 | 0bab00f3 | bellard | if (s->fd >= 0) |
3422 | 0bab00f3 | bellard | closesocket(s->fd); |
3423 | 0bab00f3 | bellard | if (s->listen_fd >= 0) |
3424 | 0bab00f3 | bellard | closesocket(s->listen_fd); |
3425 | 0bab00f3 | bellard | qemu_free(s); |
3426 | 0bab00f3 | bellard | } |
3427 | 0bab00f3 | bellard | |
3428 | 5fafdf24 | ths | static CharDriverState *qemu_chr_open_tcp(const char *host_str, |
3429 | ffd843bc | ths | int is_telnet,
|
3430 | ffd843bc | ths | int is_unix)
|
3431 | 0bab00f3 | bellard | { |
3432 | 0bab00f3 | bellard | CharDriverState *chr = NULL;
|
3433 | 0bab00f3 | bellard | TCPCharDriver *s = NULL;
|
3434 | 0bab00f3 | bellard | int fd = -1, ret, err, val; |
3435 | 951f1351 | bellard | int is_listen = 0; |
3436 | 951f1351 | bellard | int is_waitconnect = 1; |
3437 | f7499989 | pbrook | int do_nodelay = 0; |
3438 | 951f1351 | bellard | const char *ptr; |
3439 | 0bab00f3 | bellard | struct sockaddr_in saddr;
|
3440 | ffd843bc | ths | #ifndef _WIN32
|
3441 | ffd843bc | ths | struct sockaddr_un uaddr;
|
3442 | ffd843bc | ths | #endif
|
3443 | ffd843bc | ths | struct sockaddr *addr;
|
3444 | ffd843bc | ths | socklen_t addrlen; |
3445 | 0bab00f3 | bellard | |
3446 | ffd843bc | ths | #ifndef _WIN32
|
3447 | ffd843bc | ths | if (is_unix) {
|
3448 | ffd843bc | ths | addr = (struct sockaddr *)&uaddr;
|
3449 | ffd843bc | ths | addrlen = sizeof(uaddr);
|
3450 | ffd843bc | ths | if (parse_unix_path(&uaddr, host_str) < 0) |
3451 | ffd843bc | ths | goto fail;
|
3452 | ffd843bc | ths | } else
|
3453 | ffd843bc | ths | #endif
|
3454 | ffd843bc | ths | { |
3455 | ffd843bc | ths | addr = (struct sockaddr *)&saddr;
|
3456 | ffd843bc | ths | addrlen = sizeof(saddr);
|
3457 | ffd843bc | ths | if (parse_host_port(&saddr, host_str) < 0) |
3458 | ffd843bc | ths | goto fail;
|
3459 | ffd843bc | ths | } |
3460 | 0bab00f3 | bellard | |
3461 | 951f1351 | bellard | ptr = host_str; |
3462 | 951f1351 | bellard | while((ptr = strchr(ptr,','))) { |
3463 | 951f1351 | bellard | ptr++; |
3464 | 951f1351 | bellard | if (!strncmp(ptr,"server",6)) { |
3465 | 951f1351 | bellard | is_listen = 1;
|
3466 | 951f1351 | bellard | } else if (!strncmp(ptr,"nowait",6)) { |
3467 | 951f1351 | bellard | is_waitconnect = 0;
|
3468 | f7499989 | pbrook | } else if (!strncmp(ptr,"nodelay",6)) { |
3469 | f7499989 | pbrook | do_nodelay = 1;
|
3470 | 951f1351 | bellard | } else {
|
3471 | 951f1351 | bellard | printf("Unknown option: %s\n", ptr);
|
3472 | 951f1351 | bellard | goto fail;
|
3473 | 951f1351 | bellard | } |
3474 | 951f1351 | bellard | } |
3475 | 951f1351 | bellard | if (!is_listen)
|
3476 | 951f1351 | bellard | is_waitconnect = 0;
|
3477 | 951f1351 | bellard | |
3478 | 0bab00f3 | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3479 | 0bab00f3 | bellard | if (!chr)
|
3480 | 0bab00f3 | bellard | goto fail;
|
3481 | 0bab00f3 | bellard | s = qemu_mallocz(sizeof(TCPCharDriver));
|
3482 | 0bab00f3 | bellard | if (!s)
|
3483 | 0bab00f3 | bellard | goto fail;
|
3484 | ffd843bc | ths | |
3485 | ffd843bc | ths | #ifndef _WIN32
|
3486 | ffd843bc | ths | if (is_unix)
|
3487 | ffd843bc | ths | fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
3488 | ffd843bc | ths | else
|
3489 | ffd843bc | ths | #endif
|
3490 | ffd843bc | ths | fd = socket(PF_INET, SOCK_STREAM, 0);
|
3491 | 5fafdf24 | ths | |
3492 | 5fafdf24 | ths | if (fd < 0) |
3493 | 0bab00f3 | bellard | goto fail;
|
3494 | 951f1351 | bellard | |
3495 | 951f1351 | bellard | if (!is_waitconnect)
|
3496 | 951f1351 | bellard | socket_set_nonblock(fd); |
3497 | 0bab00f3 | bellard | |
3498 | 0bab00f3 | bellard | s->connected = 0;
|
3499 | 0bab00f3 | bellard | s->fd = -1;
|
3500 | 0bab00f3 | bellard | s->listen_fd = -1;
|
3501 | ffd843bc | ths | s->is_unix = is_unix; |
3502 | f7499989 | pbrook | s->do_nodelay = do_nodelay && !is_unix; |
3503 | ffd843bc | ths | |
3504 | ffd843bc | ths | chr->opaque = s; |
3505 | ffd843bc | ths | chr->chr_write = tcp_chr_write; |
3506 | ffd843bc | ths | chr->chr_close = tcp_chr_close; |
3507 | ffd843bc | ths | |
3508 | 0bab00f3 | bellard | if (is_listen) {
|
3509 | 0bab00f3 | bellard | /* allow fast reuse */
|
3510 | ffd843bc | ths | #ifndef _WIN32
|
3511 | ffd843bc | ths | if (is_unix) {
|
3512 | ffd843bc | ths | char path[109]; |
3513 | ae45d369 | bellard | pstrcpy(path, sizeof(path), uaddr.sun_path);
|
3514 | ffd843bc | ths | unlink(path); |
3515 | ffd843bc | ths | } else
|
3516 | ffd843bc | ths | #endif
|
3517 | ffd843bc | ths | { |
3518 | ffd843bc | ths | val = 1;
|
3519 | ffd843bc | ths | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
3520 | ffd843bc | ths | } |
3521 | 3b46e624 | ths | |
3522 | ffd843bc | ths | ret = bind(fd, addr, addrlen); |
3523 | ffd843bc | ths | if (ret < 0) |
3524 | 0bab00f3 | bellard | goto fail;
|
3525 | ffd843bc | ths | |
3526 | 0bab00f3 | bellard | ret = listen(fd, 0);
|
3527 | 0bab00f3 | bellard | if (ret < 0) |
3528 | 0bab00f3 | bellard | goto fail;
|
3529 | ffd843bc | ths | |
3530 | 0bab00f3 | bellard | s->listen_fd = fd; |
3531 | 0bab00f3 | bellard | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
3532 | 951f1351 | bellard | if (is_telnet)
|
3533 | 951f1351 | bellard | s->do_telnetopt = 1;
|
3534 | 0bab00f3 | bellard | } else {
|
3535 | 0bab00f3 | bellard | for(;;) {
|
3536 | ffd843bc | ths | ret = connect(fd, addr, addrlen); |
3537 | 0bab00f3 | bellard | if (ret < 0) { |
3538 | 0bab00f3 | bellard | err = socket_error(); |
3539 | 0bab00f3 | bellard | if (err == EINTR || err == EWOULDBLOCK) {
|
3540 | 0bab00f3 | bellard | } else if (err == EINPROGRESS) { |
3541 | 0bab00f3 | bellard | break;
|
3542 | f5b12268 | ths | #ifdef _WIN32
|
3543 | f5b12268 | ths | } else if (err == WSAEALREADY) { |
3544 | f5b12268 | ths | break;
|
3545 | f5b12268 | ths | #endif
|
3546 | 0bab00f3 | bellard | } else {
|
3547 | 0bab00f3 | bellard | goto fail;
|
3548 | 0bab00f3 | bellard | } |
3549 | 0bab00f3 | bellard | } else {
|
3550 | 0bab00f3 | bellard | s->connected = 1;
|
3551 | 0bab00f3 | bellard | break;
|
3552 | 0bab00f3 | bellard | } |
3553 | 0bab00f3 | bellard | } |
3554 | 0bab00f3 | bellard | s->fd = fd; |
3555 | f7499989 | pbrook | socket_set_nodelay(fd); |
3556 | 0bab00f3 | bellard | if (s->connected)
|
3557 | 0bab00f3 | bellard | tcp_chr_connect(chr); |
3558 | 0bab00f3 | bellard | else
|
3559 | 0bab00f3 | bellard | qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
|
3560 | 0bab00f3 | bellard | } |
3561 | 3b46e624 | ths | |
3562 | 951f1351 | bellard | if (is_listen && is_waitconnect) {
|
3563 | 951f1351 | bellard | printf("QEMU waiting for connection on: %s\n", host_str);
|
3564 | 951f1351 | bellard | tcp_chr_accept(chr); |
3565 | 951f1351 | bellard | socket_set_nonblock(s->listen_fd); |
3566 | 951f1351 | bellard | } |
3567 | 951f1351 | bellard | |
3568 | 0bab00f3 | bellard | return chr;
|
3569 | 0bab00f3 | bellard | fail:
|
3570 | 0bab00f3 | bellard | if (fd >= 0) |
3571 | 0bab00f3 | bellard | closesocket(fd); |
3572 | 0bab00f3 | bellard | qemu_free(s); |
3573 | 0bab00f3 | bellard | qemu_free(chr); |
3574 | 0bab00f3 | bellard | return NULL; |
3575 | 0bab00f3 | bellard | } |
3576 | 0bab00f3 | bellard | |
3577 | 82c643ff | bellard | CharDriverState *qemu_chr_open(const char *filename) |
3578 | 82c643ff | bellard | { |
3579 | f8d179e3 | bellard | const char *p; |
3580 | fd1dff4b | bellard | |
3581 | 82c643ff | bellard | if (!strcmp(filename, "vc")) { |
3582 | af3a9031 | ths | return text_console_init(&display_state, 0); |
3583 | af3a9031 | ths | } else if (strstart(filename, "vc:", &p)) { |
3584 | af3a9031 | ths | return text_console_init(&display_state, p);
|
3585 | 82c643ff | bellard | } else if (!strcmp(filename, "null")) { |
3586 | 82c643ff | bellard | return qemu_chr_open_null();
|
3587 | 5fafdf24 | ths | } else
|
3588 | 0bab00f3 | bellard | if (strstart(filename, "tcp:", &p)) { |
3589 | ffd843bc | ths | return qemu_chr_open_tcp(p, 0, 0); |
3590 | 0bab00f3 | bellard | } else
|
3591 | 951f1351 | bellard | if (strstart(filename, "telnet:", &p)) { |
3592 | ffd843bc | ths | return qemu_chr_open_tcp(p, 1, 0); |
3593 | 0bab00f3 | bellard | } else
|
3594 | 0bab00f3 | bellard | if (strstart(filename, "udp:", &p)) { |
3595 | 0bab00f3 | bellard | return qemu_chr_open_udp(p);
|
3596 | 0bab00f3 | bellard | } else
|
3597 | 20d8a3ed | ths | if (strstart(filename, "mon:", &p)) { |
3598 | 20d8a3ed | ths | CharDriverState *drv = qemu_chr_open(p); |
3599 | 20d8a3ed | ths | if (drv) {
|
3600 | 20d8a3ed | ths | drv = qemu_chr_open_mux(drv); |
3601 | 20d8a3ed | ths | monitor_init(drv, !nographic); |
3602 | 20d8a3ed | ths | return drv;
|
3603 | 20d8a3ed | ths | } |
3604 | 20d8a3ed | ths | printf("Unable to open driver: %s\n", p);
|
3605 | 20d8a3ed | ths | return 0; |
3606 | 20d8a3ed | ths | } else
|
3607 | 7664728b | bellard | #ifndef _WIN32
|
3608 | ffd843bc | ths | if (strstart(filename, "unix:", &p)) { |
3609 | ffd843bc | ths | return qemu_chr_open_tcp(p, 0, 1); |
3610 | ffd843bc | ths | } else if (strstart(filename, "file:", &p)) { |
3611 | f8d179e3 | bellard | return qemu_chr_open_file_out(p);
|
3612 | f8d179e3 | bellard | } else if (strstart(filename, "pipe:", &p)) { |
3613 | f8d179e3 | bellard | return qemu_chr_open_pipe(p);
|
3614 | 7664728b | bellard | } else if (!strcmp(filename, "pty")) { |
3615 | 82c643ff | bellard | return qemu_chr_open_pty();
|
3616 | 82c643ff | bellard | } else if (!strcmp(filename, "stdio")) { |
3617 | 82c643ff | bellard | return qemu_chr_open_stdio();
|
3618 | 5fafdf24 | ths | } else
|
3619 | f8d179e3 | bellard | #if defined(__linux__)
|
3620 | e57a8c0e | bellard | if (strstart(filename, "/dev/parport", NULL)) { |
3621 | e57a8c0e | bellard | return qemu_chr_open_pp(filename);
|
3622 | 5fafdf24 | ths | } else
|
3623 | aec62507 | ths | #endif
|
3624 | 3fda388a | ths | #if defined(__linux__) || defined(__sun__)
|
3625 | f8d179e3 | bellard | if (strstart(filename, "/dev/", NULL)) { |
3626 | f8d179e3 | bellard | return qemu_chr_open_tty(filename);
|
3627 | 3fda388a | ths | } else
|
3628 | 3fda388a | ths | #endif
|
3629 | aec62507 | ths | #else /* !_WIN32 */ |
3630 | f331110f | bellard | if (strstart(filename, "COM", NULL)) { |
3631 | f331110f | bellard | return qemu_chr_open_win(filename);
|
3632 | f331110f | bellard | } else
|
3633 | f331110f | bellard | if (strstart(filename, "pipe:", &p)) { |
3634 | f331110f | bellard | return qemu_chr_open_win_pipe(p);
|
3635 | f331110f | bellard | } else
|
3636 | 72d46479 | ths | if (strstart(filename, "con:", NULL)) { |
3637 | 72d46479 | ths | return qemu_chr_open_win_con(filename);
|
3638 | 72d46479 | ths | } else
|
3639 | f331110f | bellard | if (strstart(filename, "file:", &p)) { |
3640 | f331110f | bellard | return qemu_chr_open_win_file_out(p);
|
3641 | 2e4d9fb1 | aurel32 | } else
|
3642 | 2e4d9fb1 | aurel32 | #endif
|
3643 | 2e4d9fb1 | aurel32 | #ifdef CONFIG_BRLAPI
|
3644 | 2e4d9fb1 | aurel32 | if (!strcmp(filename, "braille")) { |
3645 | 2e4d9fb1 | aurel32 | return chr_baum_init();
|
3646 | 2e4d9fb1 | aurel32 | } else
|
3647 | f331110f | bellard | #endif
|
3648 | 82c643ff | bellard | { |
3649 | 82c643ff | bellard | return NULL; |
3650 | 82c643ff | bellard | } |
3651 | 82c643ff | bellard | } |
3652 | 82c643ff | bellard | |
3653 | f331110f | bellard | void qemu_chr_close(CharDriverState *chr)
|
3654 | f331110f | bellard | { |
3655 | f331110f | bellard | if (chr->chr_close)
|
3656 | f331110f | bellard | chr->chr_close(chr); |
3657 | a11d070e | balrog | qemu_free(chr); |
3658 | f331110f | bellard | } |
3659 | f331110f | bellard | |
3660 | 80cabfad | bellard | /***********************************************************/
|
3661 | 7c9d8e07 | bellard | /* network device redirectors */
|
3662 | 330d0414 | bellard | |
3663 | 3f4afa14 | j_mayer | __attribute__ (( unused )) |
3664 | 9596ebb7 | pbrook | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
3665 | c20709aa | bellard | { |
3666 | c20709aa | bellard | int len, i, j, c;
|
3667 | c20709aa | bellard | |
3668 | c20709aa | bellard | for(i=0;i<size;i+=16) { |
3669 | c20709aa | bellard | len = size - i; |
3670 | c20709aa | bellard | if (len > 16) |
3671 | c20709aa | bellard | len = 16;
|
3672 | c20709aa | bellard | fprintf(f, "%08x ", i);
|
3673 | c20709aa | bellard | for(j=0;j<16;j++) { |
3674 | c20709aa | bellard | if (j < len)
|
3675 | c20709aa | bellard | fprintf(f, " %02x", buf[i+j]);
|
3676 | c20709aa | bellard | else
|
3677 | c20709aa | bellard | fprintf(f, " ");
|
3678 | c20709aa | bellard | } |
3679 | c20709aa | bellard | fprintf(f, " ");
|
3680 | c20709aa | bellard | for(j=0;j<len;j++) { |
3681 | c20709aa | bellard | c = buf[i+j]; |
3682 | c20709aa | bellard | if (c < ' ' || c > '~') |
3683 | c20709aa | bellard | c = '.';
|
3684 | c20709aa | bellard | fprintf(f, "%c", c);
|
3685 | c20709aa | bellard | } |
3686 | c20709aa | bellard | fprintf(f, "\n");
|
3687 | c20709aa | bellard | } |
3688 | c20709aa | bellard | } |
3689 | c20709aa | bellard | |
3690 | 7c9d8e07 | bellard | static int parse_macaddr(uint8_t *macaddr, const char *p) |
3691 | c20709aa | bellard | { |
3692 | 7c9d8e07 | bellard | int i;
|
3693 | 76ea08f9 | balrog | char *last_char;
|
3694 | 76ea08f9 | balrog | long int offset; |
3695 | 76ea08f9 | balrog | |
3696 | 76ea08f9 | balrog | errno = 0;
|
3697 | 76ea08f9 | balrog | offset = strtol(p, &last_char, 0);
|
3698 | 76ea08f9 | balrog | if (0 == errno && '\0' == *last_char && |
3699 | 76ea08f9 | balrog | offset >= 0 && offset <= 0xFFFFFF) { |
3700 | 76ea08f9 | balrog | macaddr[3] = (offset & 0xFF0000) >> 16; |
3701 | 76ea08f9 | balrog | macaddr[4] = (offset & 0xFF00) >> 8; |
3702 | 76ea08f9 | balrog | macaddr[5] = offset & 0xFF; |
3703 | 76ea08f9 | balrog | return 0; |
3704 | 76ea08f9 | balrog | } else {
|
3705 | 76ea08f9 | balrog | for(i = 0; i < 6; i++) { |
3706 | 76ea08f9 | balrog | macaddr[i] = strtol(p, (char **)&p, 16); |
3707 | 76ea08f9 | balrog | if (i == 5) { |
3708 | 76ea08f9 | balrog | if (*p != '\0') |
3709 | 76ea08f9 | balrog | return -1; |
3710 | 76ea08f9 | balrog | } else {
|
3711 | 76ea08f9 | balrog | if (*p != ':' && *p != '-') |
3712 | 76ea08f9 | balrog | return -1; |
3713 | 76ea08f9 | balrog | p++; |
3714 | 76ea08f9 | balrog | } |
3715 | 7c9d8e07 | bellard | } |
3716 | 76ea08f9 | balrog | return 0; |
3717 | 7c9d8e07 | bellard | } |
3718 | 76ea08f9 | balrog | |
3719 | 76ea08f9 | balrog | return -1; |
3720 | c20709aa | bellard | } |
3721 | 67b915a5 | bellard | |
3722 | 7c9d8e07 | bellard | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
3723 | 67b915a5 | bellard | { |
3724 | 7c9d8e07 | bellard | const char *p, *p1; |
3725 | 7c9d8e07 | bellard | int len;
|
3726 | 7c9d8e07 | bellard | p = *pp; |
3727 | 7c9d8e07 | bellard | p1 = strchr(p, sep); |
3728 | 7c9d8e07 | bellard | if (!p1)
|
3729 | 7c9d8e07 | bellard | return -1; |
3730 | 7c9d8e07 | bellard | len = p1 - p; |
3731 | 7c9d8e07 | bellard | p1++; |
3732 | 7c9d8e07 | bellard | if (buf_size > 0) { |
3733 | 7c9d8e07 | bellard | if (len > buf_size - 1) |
3734 | 7c9d8e07 | bellard | len = buf_size - 1;
|
3735 | 7c9d8e07 | bellard | memcpy(buf, p, len); |
3736 | 7c9d8e07 | bellard | buf[len] = '\0';
|
3737 | 7c9d8e07 | bellard | } |
3738 | 7c9d8e07 | bellard | *pp = p1; |
3739 | 7c9d8e07 | bellard | return 0; |
3740 | c20709aa | bellard | } |
3741 | c20709aa | bellard | |
3742 | 951f1351 | bellard | int parse_host_src_port(struct sockaddr_in *haddr, |
3743 | 951f1351 | bellard | struct sockaddr_in *saddr,
|
3744 | 951f1351 | bellard | const char *input_str) |
3745 | 951f1351 | bellard | { |
3746 | 951f1351 | bellard | char *str = strdup(input_str);
|
3747 | 951f1351 | bellard | char *host_str = str;
|
3748 | 951f1351 | bellard | char *src_str;
|
3749 | 951f1351 | bellard | char *ptr;
|
3750 | 951f1351 | bellard | |
3751 | 951f1351 | bellard | /*
|
3752 | 951f1351 | bellard | * Chop off any extra arguments at the end of the string which
|
3753 | 951f1351 | bellard | * would start with a comma, then fill in the src port information
|
3754 | 951f1351 | bellard | * if it was provided else use the "any address" and "any port".
|
3755 | 951f1351 | bellard | */
|
3756 | 951f1351 | bellard | if ((ptr = strchr(str,','))) |
3757 | 951f1351 | bellard | *ptr = '\0';
|
3758 | 951f1351 | bellard | |
3759 | 951f1351 | bellard | if ((src_str = strchr(input_str,'@'))) { |
3760 | 951f1351 | bellard | *src_str = '\0';
|
3761 | 951f1351 | bellard | src_str++; |
3762 | 951f1351 | bellard | } |
3763 | 951f1351 | bellard | |
3764 | 951f1351 | bellard | if (parse_host_port(haddr, host_str) < 0) |
3765 | 951f1351 | bellard | goto fail;
|
3766 | 951f1351 | bellard | |
3767 | 951f1351 | bellard | if (!src_str || *src_str == '\0') |
3768 | 951f1351 | bellard | src_str = ":0";
|
3769 | 951f1351 | bellard | |
3770 | 951f1351 | bellard | if (parse_host_port(saddr, src_str) < 0) |
3771 | 951f1351 | bellard | goto fail;
|
3772 | 951f1351 | bellard | |
3773 | 951f1351 | bellard | free(str); |
3774 | 951f1351 | bellard | return(0); |
3775 | 951f1351 | bellard | |
3776 | 951f1351 | bellard | fail:
|
3777 | 951f1351 | bellard | free(str); |
3778 | 951f1351 | bellard | return -1; |
3779 | 951f1351 | bellard | } |
3780 | 951f1351 | bellard | |
3781 | 7c9d8e07 | bellard | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
3782 | 7c9d8e07 | bellard | { |
3783 | 7c9d8e07 | bellard | char buf[512]; |
3784 | 7c9d8e07 | bellard | struct hostent *he;
|
3785 | 7c9d8e07 | bellard | const char *p, *r; |
3786 | 7c9d8e07 | bellard | int port;
|
3787 | 7c9d8e07 | bellard | |
3788 | 7c9d8e07 | bellard | p = str; |
3789 | 7c9d8e07 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
3790 | 7c9d8e07 | bellard | return -1; |
3791 | 7c9d8e07 | bellard | saddr->sin_family = AF_INET; |
3792 | 7c9d8e07 | bellard | if (buf[0] == '\0') { |
3793 | 7c9d8e07 | bellard | saddr->sin_addr.s_addr = 0;
|
3794 | 7c9d8e07 | bellard | } else {
|
3795 | 7c9d8e07 | bellard | if (isdigit(buf[0])) { |
3796 | 7c9d8e07 | bellard | if (!inet_aton(buf, &saddr->sin_addr))
|
3797 | 7c9d8e07 | bellard | return -1; |
3798 | 7c9d8e07 | bellard | } else {
|
3799 | 7c9d8e07 | bellard | if ((he = gethostbyname(buf)) == NULL) |
3800 | 7c9d8e07 | bellard | return - 1; |
3801 | 7c9d8e07 | bellard | saddr->sin_addr = *(struct in_addr *)he->h_addr;
|
3802 | 7c9d8e07 | bellard | } |
3803 | 7c9d8e07 | bellard | } |
3804 | 7c9d8e07 | bellard | port = strtol(p, (char **)&r, 0); |
3805 | 7c9d8e07 | bellard | if (r == p)
|
3806 | 7c9d8e07 | bellard | return -1; |
3807 | 7c9d8e07 | bellard | saddr->sin_port = htons(port); |
3808 | 7c9d8e07 | bellard | return 0; |
3809 | 7c9d8e07 | bellard | } |
3810 | c20709aa | bellard | |
3811 | 52f61fde | ths | #ifndef _WIN32
|
3812 | 52f61fde | ths | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) |
3813 | ffd843bc | ths | { |
3814 | ffd843bc | ths | const char *p; |
3815 | ffd843bc | ths | int len;
|
3816 | ffd843bc | ths | |
3817 | ffd843bc | ths | len = MIN(108, strlen(str));
|
3818 | ffd843bc | ths | p = strchr(str, ',');
|
3819 | ffd843bc | ths | if (p)
|
3820 | ffd843bc | ths | len = MIN(len, p - str); |
3821 | ffd843bc | ths | |
3822 | ffd843bc | ths | memset(uaddr, 0, sizeof(*uaddr)); |
3823 | ffd843bc | ths | |
3824 | ffd843bc | ths | uaddr->sun_family = AF_UNIX; |
3825 | ffd843bc | ths | memcpy(uaddr->sun_path, str, len); |
3826 | ffd843bc | ths | |
3827 | ffd843bc | ths | return 0; |
3828 | ffd843bc | ths | } |
3829 | 52f61fde | ths | #endif
|
3830 | ffd843bc | ths | |
3831 | 7c9d8e07 | bellard | /* find or alloc a new VLAN */
|
3832 | 7c9d8e07 | bellard | VLANState *qemu_find_vlan(int id)
|
3833 | c20709aa | bellard | { |
3834 | 7c9d8e07 | bellard | VLANState **pvlan, *vlan; |
3835 | 7c9d8e07 | bellard | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
3836 | 7c9d8e07 | bellard | if (vlan->id == id)
|
3837 | 7c9d8e07 | bellard | return vlan;
|
3838 | 7c9d8e07 | bellard | } |
3839 | 7c9d8e07 | bellard | vlan = qemu_mallocz(sizeof(VLANState));
|
3840 | 7c9d8e07 | bellard | if (!vlan)
|
3841 | 7c9d8e07 | bellard | return NULL; |
3842 | 7c9d8e07 | bellard | vlan->id = id; |
3843 | 7c9d8e07 | bellard | vlan->next = NULL;
|
3844 | 7c9d8e07 | bellard | pvlan = &first_vlan; |
3845 | 7c9d8e07 | bellard | while (*pvlan != NULL) |
3846 | 7c9d8e07 | bellard | pvlan = &(*pvlan)->next; |
3847 | 7c9d8e07 | bellard | *pvlan = vlan; |
3848 | 7c9d8e07 | bellard | return vlan;
|
3849 | c20709aa | bellard | } |
3850 | c20709aa | bellard | |
3851 | 7c9d8e07 | bellard | VLANClientState *qemu_new_vlan_client(VLANState *vlan, |
3852 | d861b05e | pbrook | IOReadHandler *fd_read, |
3853 | d861b05e | pbrook | IOCanRWHandler *fd_can_read, |
3854 | d861b05e | pbrook | void *opaque)
|
3855 | c20709aa | bellard | { |
3856 | 7c9d8e07 | bellard | VLANClientState *vc, **pvc; |
3857 | 7c9d8e07 | bellard | vc = qemu_mallocz(sizeof(VLANClientState));
|
3858 | 7c9d8e07 | bellard | if (!vc)
|
3859 | 7c9d8e07 | bellard | return NULL; |
3860 | 7c9d8e07 | bellard | vc->fd_read = fd_read; |
3861 | d861b05e | pbrook | vc->fd_can_read = fd_can_read; |
3862 | 7c9d8e07 | bellard | vc->opaque = opaque; |
3863 | 7c9d8e07 | bellard | vc->vlan = vlan; |
3864 | 7c9d8e07 | bellard | |
3865 | 7c9d8e07 | bellard | vc->next = NULL;
|
3866 | 7c9d8e07 | bellard | pvc = &vlan->first_client; |
3867 | 7c9d8e07 | bellard | while (*pvc != NULL) |
3868 | 7c9d8e07 | bellard | pvc = &(*pvc)->next; |
3869 | 7c9d8e07 | bellard | *pvc = vc; |
3870 | 7c9d8e07 | bellard | return vc;
|
3871 | c20709aa | bellard | } |
3872 | c20709aa | bellard | |
3873 | d861b05e | pbrook | int qemu_can_send_packet(VLANClientState *vc1)
|
3874 | d861b05e | pbrook | { |
3875 | d861b05e | pbrook | VLANState *vlan = vc1->vlan; |
3876 | d861b05e | pbrook | VLANClientState *vc; |
3877 | d861b05e | pbrook | |
3878 | d861b05e | pbrook | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
3879 | d861b05e | pbrook | if (vc != vc1) {
|
3880 | fbd1711d | balrog | if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
|
3881 | fbd1711d | balrog | return 1; |
3882 | d861b05e | pbrook | } |
3883 | d861b05e | pbrook | } |
3884 | fbd1711d | balrog | return 0; |
3885 | d861b05e | pbrook | } |
3886 | d861b05e | pbrook | |
3887 | 7c9d8e07 | bellard | void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size) |
3888 | c20709aa | bellard | { |
3889 | 7c9d8e07 | bellard | VLANState *vlan = vc1->vlan; |
3890 | 7c9d8e07 | bellard | VLANClientState *vc; |
3891 | 7c9d8e07 | bellard | |
3892 | 7c9d8e07 | bellard | #if 0
|
3893 | 7c9d8e07 | bellard | printf("vlan %d send:\n", vlan->id);
|
3894 | 7c9d8e07 | bellard | hex_dump(stdout, buf, size);
|
3895 | 7c9d8e07 | bellard | #endif
|
3896 | 7c9d8e07 | bellard | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
3897 | 7c9d8e07 | bellard | if (vc != vc1) {
|
3898 | 7c9d8e07 | bellard | vc->fd_read(vc->opaque, buf, size); |
3899 | 7c9d8e07 | bellard | } |
3900 | 7c9d8e07 | bellard | } |
3901 | 67b915a5 | bellard | } |
3902 | 67b915a5 | bellard | |
3903 | c20709aa | bellard | #if defined(CONFIG_SLIRP)
|
3904 | c20709aa | bellard | |
3905 | c20709aa | bellard | /* slirp network adapter */
|
3906 | c20709aa | bellard | |
3907 | c20709aa | bellard | static int slirp_inited; |
3908 | 7c9d8e07 | bellard | static VLANClientState *slirp_vc;
|
3909 | c20709aa | bellard | |
3910 | c20709aa | bellard | int slirp_can_output(void) |
3911 | c20709aa | bellard | { |
3912 | 3b7f5d47 | pbrook | return !slirp_vc || qemu_can_send_packet(slirp_vc);
|
3913 | c20709aa | bellard | } |
3914 | c20709aa | bellard | |
3915 | c20709aa | bellard | void slirp_output(const uint8_t *pkt, int pkt_len) |
3916 | 67b915a5 | bellard | { |
3917 | c20709aa | bellard | #if 0
|
3918 | 7c9d8e07 | bellard | printf("slirp output:\n");
|
3919 | c20709aa | bellard | hex_dump(stdout, pkt, pkt_len);
|
3920 | c20709aa | bellard | #endif
|
3921 | 3b7f5d47 | pbrook | if (!slirp_vc)
|
3922 | 3b7f5d47 | pbrook | return;
|
3923 | 7c9d8e07 | bellard | qemu_send_packet(slirp_vc, pkt, pkt_len); |
3924 | 67b915a5 | bellard | } |
3925 | 67b915a5 | bellard | |
3926 | 7c9d8e07 | bellard | static void slirp_receive(void *opaque, const uint8_t *buf, int size) |
3927 | c20709aa | bellard | { |
3928 | c20709aa | bellard | #if 0
|
3929 | 7c9d8e07 | bellard | printf("slirp input:\n");
|
3930 | c20709aa | bellard | hex_dump(stdout, buf, size);
|
3931 | c20709aa | bellard | #endif
|
3932 | c20709aa | bellard | slirp_input(buf, size); |
3933 | c20709aa | bellard | } |
3934 | c20709aa | bellard | |
3935 | 7c9d8e07 | bellard | static int net_slirp_init(VLANState *vlan) |
3936 | c20709aa | bellard | { |
3937 | c20709aa | bellard | if (!slirp_inited) {
|
3938 | c20709aa | bellard | slirp_inited = 1;
|
3939 | c20709aa | bellard | slirp_init(); |
3940 | c20709aa | bellard | } |
3941 | 5fafdf24 | ths | slirp_vc = qemu_new_vlan_client(vlan, |
3942 | d861b05e | pbrook | slirp_receive, NULL, NULL); |
3943 | 7c9d8e07 | bellard | snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector"); |
3944 | 9bf05444 | bellard | return 0; |
3945 | 9bf05444 | bellard | } |
3946 | 9bf05444 | bellard | |
3947 | 9bf05444 | bellard | static void net_slirp_redir(const char *redir_str) |
3948 | 9bf05444 | bellard | { |
3949 | 9bf05444 | bellard | int is_udp;
|
3950 | 9bf05444 | bellard | char buf[256], *r; |
3951 | 9bf05444 | bellard | const char *p; |
3952 | 9bf05444 | bellard | struct in_addr guest_addr;
|
3953 | 9bf05444 | bellard | int host_port, guest_port;
|
3954 | 3b46e624 | ths | |
3955 | 9bf05444 | bellard | if (!slirp_inited) {
|
3956 | 9bf05444 | bellard | slirp_inited = 1;
|
3957 | 9bf05444 | bellard | slirp_init(); |
3958 | 9bf05444 | bellard | } |
3959 | 9bf05444 | bellard | |
3960 | 9bf05444 | bellard | p = redir_str; |
3961 | 9bf05444 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
3962 | 9bf05444 | bellard | goto fail;
|
3963 | 9bf05444 | bellard | if (!strcmp(buf, "tcp")) { |
3964 | 9bf05444 | bellard | is_udp = 0;
|
3965 | 9bf05444 | bellard | } else if (!strcmp(buf, "udp")) { |
3966 | 9bf05444 | bellard | is_udp = 1;
|
3967 | 9bf05444 | bellard | } else {
|
3968 | 9bf05444 | bellard | goto fail;
|
3969 | 9bf05444 | bellard | } |
3970 | 9bf05444 | bellard | |
3971 | 9bf05444 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
3972 | 9bf05444 | bellard | goto fail;
|
3973 | 9bf05444 | bellard | host_port = strtol(buf, &r, 0);
|
3974 | 9bf05444 | bellard | if (r == buf)
|
3975 | 9bf05444 | bellard | goto fail;
|
3976 | 9bf05444 | bellard | |
3977 | 9bf05444 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
3978 | 9bf05444 | bellard | goto fail;
|
3979 | 9bf05444 | bellard | if (buf[0] == '\0') { |
3980 | 9bf05444 | bellard | pstrcpy(buf, sizeof(buf), "10.0.2.15"); |
3981 | 9bf05444 | bellard | } |
3982 | 9bf05444 | bellard | if (!inet_aton(buf, &guest_addr))
|
3983 | 9bf05444 | bellard | goto fail;
|
3984 | 3b46e624 | ths | |
3985 | 9bf05444 | bellard | guest_port = strtol(p, &r, 0);
|
3986 | 9bf05444 | bellard | if (r == p)
|
3987 | 9bf05444 | bellard | goto fail;
|
3988 | 3b46e624 | ths | |
3989 | 9bf05444 | bellard | if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { |
3990 | 9bf05444 | bellard | fprintf(stderr, "qemu: could not set up redirection\n");
|
3991 | 9bf05444 | bellard | exit(1);
|
3992 | 9bf05444 | bellard | } |
3993 | 9bf05444 | bellard | return;
|
3994 | 9bf05444 | bellard | fail:
|
3995 | 9bf05444 | bellard | fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
|
3996 | 9bf05444 | bellard | exit(1);
|
3997 | 9bf05444 | bellard | } |
3998 | 3b46e624 | ths | |
3999 | c94c8d64 | bellard | #ifndef _WIN32
|
4000 | c94c8d64 | bellard | |
4001 | 9d728e8c | bellard | char smb_dir[1024]; |
4002 | 9d728e8c | bellard | |
4003 | 044fae83 | balrog | static void erase_dir(char *dir_name) |
4004 | 9d728e8c | bellard | { |
4005 | 9d728e8c | bellard | DIR *d; |
4006 | 9d728e8c | bellard | struct dirent *de;
|
4007 | 9d728e8c | bellard | char filename[1024]; |
4008 | 9d728e8c | bellard | |
4009 | 9d728e8c | bellard | /* erase all the files in the directory */
|
4010 | 044fae83 | balrog | if ((d = opendir(dir_name)) != 0) { |
4011 | 044fae83 | balrog | for(;;) {
|
4012 | 044fae83 | balrog | de = readdir(d); |
4013 | 044fae83 | balrog | if (!de)
|
4014 | 044fae83 | balrog | break;
|
4015 | 044fae83 | balrog | if (strcmp(de->d_name, ".") != 0 && |
4016 | 044fae83 | balrog | strcmp(de->d_name, "..") != 0) { |
4017 | 044fae83 | balrog | snprintf(filename, sizeof(filename), "%s/%s", |
4018 | 044fae83 | balrog | smb_dir, de->d_name); |
4019 | 044fae83 | balrog | if (unlink(filename) != 0) /* is it a directory? */ |
4020 | 044fae83 | balrog | erase_dir(filename); |
4021 | 044fae83 | balrog | } |
4022 | 9d728e8c | bellard | } |
4023 | 044fae83 | balrog | closedir(d); |
4024 | 044fae83 | balrog | rmdir(dir_name); |
4025 | 9d728e8c | bellard | } |
4026 | 044fae83 | balrog | } |
4027 | 044fae83 | balrog | |
4028 | 044fae83 | balrog | /* automatic user mode samba server configuration */
|
4029 | 044fae83 | balrog | static void smb_exit(void) |
4030 | 044fae83 | balrog | { |
4031 | 044fae83 | balrog | erase_dir(smb_dir); |
4032 | 9d728e8c | bellard | } |
4033 | 9d728e8c | bellard | |
4034 | 9d728e8c | bellard | /* automatic user mode samba server configuration */
|
4035 | 9596ebb7 | pbrook | static void net_slirp_smb(const char *exported_dir) |
4036 | 9d728e8c | bellard | { |
4037 | 9d728e8c | bellard | char smb_conf[1024]; |
4038 | 9d728e8c | bellard | char smb_cmdline[1024]; |
4039 | 9d728e8c | bellard | FILE *f; |
4040 | 9d728e8c | bellard | |
4041 | 9d728e8c | bellard | if (!slirp_inited) {
|
4042 | 9d728e8c | bellard | slirp_inited = 1;
|
4043 | 9d728e8c | bellard | slirp_init(); |
4044 | 9d728e8c | bellard | } |
4045 | 9d728e8c | bellard | |
4046 | 9d728e8c | bellard | /* XXX: better tmp dir construction */
|
4047 | 9d728e8c | bellard | snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); |
4048 | 9d728e8c | bellard | if (mkdir(smb_dir, 0700) < 0) { |
4049 | 9d728e8c | bellard | fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
|
4050 | 9d728e8c | bellard | exit(1);
|
4051 | 9d728e8c | bellard | } |
4052 | 9d728e8c | bellard | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); |
4053 | 3b46e624 | ths | |
4054 | 9d728e8c | bellard | f = fopen(smb_conf, "w");
|
4055 | 9d728e8c | bellard | if (!f) {
|
4056 | 9d728e8c | bellard | fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
|
4057 | 9d728e8c | bellard | exit(1);
|
4058 | 9d728e8c | bellard | } |
4059 | 5fafdf24 | ths | fprintf(f, |
4060 | 9d728e8c | bellard | "[global]\n"
|
4061 | 157777ef | bellard | "private dir=%s\n"
|
4062 | 157777ef | bellard | "smb ports=0\n"
|
4063 | 157777ef | bellard | "socket address=127.0.0.1\n"
|
4064 | 9d728e8c | bellard | "pid directory=%s\n"
|
4065 | 9d728e8c | bellard | "lock directory=%s\n"
|
4066 | 9d728e8c | bellard | "log file=%s/log.smbd\n"
|
4067 | 9d728e8c | bellard | "smb passwd file=%s/smbpasswd\n"
|
4068 | 03ffbb69 | bellard | "security = share\n"
|
4069 | 9d728e8c | bellard | "[qemu]\n"
|
4070 | 9d728e8c | bellard | "path=%s\n"
|
4071 | 9d728e8c | bellard | "read only=no\n"
|
4072 | 9d728e8c | bellard | "guest ok=yes\n",
|
4073 | 9d728e8c | bellard | smb_dir, |
4074 | 157777ef | bellard | smb_dir, |
4075 | 9d728e8c | bellard | smb_dir, |
4076 | 9d728e8c | bellard | smb_dir, |
4077 | 9d728e8c | bellard | smb_dir, |
4078 | 9d728e8c | bellard | exported_dir |
4079 | 9d728e8c | bellard | ); |
4080 | 9d728e8c | bellard | fclose(f); |
4081 | 9d728e8c | bellard | atexit(smb_exit); |
4082 | 9d728e8c | bellard | |
4083 | a14d6c8c | pbrook | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
4084 | a14d6c8c | pbrook | SMBD_COMMAND, smb_conf); |
4085 | 3b46e624 | ths | |
4086 | 9d728e8c | bellard | slirp_add_exec(0, smb_cmdline, 4, 139); |
4087 | 9d728e8c | bellard | } |
4088 | 9bf05444 | bellard | |
4089 | c94c8d64 | bellard | #endif /* !defined(_WIN32) */ |
4090 | 31a60e22 | blueswir1 | void do_info_slirp(void) |
4091 | 31a60e22 | blueswir1 | { |
4092 | 31a60e22 | blueswir1 | slirp_stats(); |
4093 | 31a60e22 | blueswir1 | } |
4094 | c94c8d64 | bellard | |
4095 | c20709aa | bellard | #endif /* CONFIG_SLIRP */ |
4096 | c20709aa | bellard | |
4097 | c20709aa | bellard | #if !defined(_WIN32)
|
4098 | 7c9d8e07 | bellard | |
4099 | 7c9d8e07 | bellard | typedef struct TAPState { |
4100 | 7c9d8e07 | bellard | VLANClientState *vc; |
4101 | 7c9d8e07 | bellard | int fd;
|
4102 | b46a8906 | ths | char down_script[1024]; |
4103 | 7c9d8e07 | bellard | } TAPState; |
4104 | 7c9d8e07 | bellard | |
4105 | 7c9d8e07 | bellard | static void tap_receive(void *opaque, const uint8_t *buf, int size) |
4106 | 7c9d8e07 | bellard | { |
4107 | 7c9d8e07 | bellard | TAPState *s = opaque; |
4108 | 7c9d8e07 | bellard | int ret;
|
4109 | 7c9d8e07 | bellard | for(;;) {
|
4110 | 7c9d8e07 | bellard | ret = write(s->fd, buf, size); |
4111 | 7c9d8e07 | bellard | if (ret < 0 && (errno == EINTR || errno == EAGAIN)) { |
4112 | 7c9d8e07 | bellard | } else {
|
4113 | 7c9d8e07 | bellard | break;
|
4114 | 7c9d8e07 | bellard | } |
4115 | 7c9d8e07 | bellard | } |
4116 | 7c9d8e07 | bellard | } |
4117 | 7c9d8e07 | bellard | |
4118 | 7c9d8e07 | bellard | static void tap_send(void *opaque) |
4119 | 7c9d8e07 | bellard | { |
4120 | 7c9d8e07 | bellard | TAPState *s = opaque; |
4121 | 7c9d8e07 | bellard | uint8_t buf[4096];
|
4122 | 7c9d8e07 | bellard | int size;
|
4123 | 7c9d8e07 | bellard | |
4124 | d5d10bc3 | ths | #ifdef __sun__
|
4125 | d5d10bc3 | ths | struct strbuf sbuf;
|
4126 | d5d10bc3 | ths | int f = 0; |
4127 | d5d10bc3 | ths | sbuf.maxlen = sizeof(buf);
|
4128 | d5d10bc3 | ths | sbuf.buf = buf; |
4129 | d5d10bc3 | ths | size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1; |
4130 | d5d10bc3 | ths | #else
|
4131 | 7c9d8e07 | bellard | size = read(s->fd, buf, sizeof(buf));
|
4132 | d5d10bc3 | ths | #endif
|
4133 | 7c9d8e07 | bellard | if (size > 0) { |
4134 | 7c9d8e07 | bellard | qemu_send_packet(s->vc, buf, size); |
4135 | 7c9d8e07 | bellard | } |
4136 | 7c9d8e07 | bellard | } |
4137 | 7c9d8e07 | bellard | |
4138 | 7c9d8e07 | bellard | /* fd support */
|
4139 | 7c9d8e07 | bellard | |
4140 | 7c9d8e07 | bellard | static TAPState *net_tap_fd_init(VLANState *vlan, int fd) |
4141 | 7c9d8e07 | bellard | { |
4142 | 7c9d8e07 | bellard | TAPState *s; |
4143 | 7c9d8e07 | bellard | |
4144 | 7c9d8e07 | bellard | s = qemu_mallocz(sizeof(TAPState));
|
4145 | 7c9d8e07 | bellard | if (!s)
|
4146 | 7c9d8e07 | bellard | return NULL; |
4147 | 7c9d8e07 | bellard | s->fd = fd; |
4148 | d861b05e | pbrook | s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
|
4149 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, tap_send, NULL, s);
|
4150 | 7c9d8e07 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd); |
4151 | 7c9d8e07 | bellard | return s;
|
4152 | 7c9d8e07 | bellard | } |
4153 | 7c9d8e07 | bellard | |
4154 | 5c40d2bd | ths | #if defined (_BSD) || defined (__FreeBSD_kernel__)
|
4155 | 7c9d8e07 | bellard | static int tap_open(char *ifname, int ifname_size) |
4156 | 7d3505c5 | bellard | { |
4157 | 7d3505c5 | bellard | int fd;
|
4158 | 7d3505c5 | bellard | char *dev;
|
4159 | 7d3505c5 | bellard | struct stat s;
|
4160 | 67b915a5 | bellard | |
4161 | aeb30be6 | balrog | TFR(fd = open("/dev/tap", O_RDWR));
|
4162 | 7d3505c5 | bellard | if (fd < 0) { |
4163 | 7d3505c5 | bellard | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
|
4164 | 7d3505c5 | bellard | return -1; |
4165 | 7d3505c5 | bellard | } |
4166 | 7d3505c5 | bellard | |
4167 | 7d3505c5 | bellard | fstat(fd, &s); |
4168 | 7d3505c5 | bellard | dev = devname(s.st_rdev, S_IFCHR); |
4169 | 7d3505c5 | bellard | pstrcpy(ifname, ifname_size, dev); |
4170 | 7d3505c5 | bellard | |
4171 | 7d3505c5 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
4172 | 7d3505c5 | bellard | return fd;
|
4173 | 7d3505c5 | bellard | } |
4174 | ec530c81 | bellard | #elif defined(__sun__)
|
4175 | d5d10bc3 | ths | #define TUNNEWPPA (('T'<<16) | 0x0001) |
4176 | 5fafdf24 | ths | /*
|
4177 | 5fafdf24 | ths | * Allocate TAP device, returns opened fd.
|
4178 | d5d10bc3 | ths | * Stores dev name in the first arg(must be large enough).
|
4179 | 3b46e624 | ths | */
|
4180 | d5d10bc3 | ths | int tap_alloc(char *dev) |
4181 | d5d10bc3 | ths | { |
4182 | d5d10bc3 | ths | int tap_fd, if_fd, ppa = -1; |
4183 | d5d10bc3 | ths | static int ip_fd = 0; |
4184 | d5d10bc3 | ths | char *ptr;
|
4185 | d5d10bc3 | ths | |
4186 | d5d10bc3 | ths | static int arp_fd = 0; |
4187 | d5d10bc3 | ths | int ip_muxid, arp_muxid;
|
4188 | d5d10bc3 | ths | struct strioctl strioc_if, strioc_ppa;
|
4189 | d5d10bc3 | ths | int link_type = I_PLINK;;
|
4190 | d5d10bc3 | ths | struct lifreq ifr;
|
4191 | d5d10bc3 | ths | char actual_name[32] = ""; |
4192 | d5d10bc3 | ths | |
4193 | d5d10bc3 | ths | memset(&ifr, 0x0, sizeof(ifr)); |
4194 | d5d10bc3 | ths | |
4195 | d5d10bc3 | ths | if( *dev ){
|
4196 | 5fafdf24 | ths | ptr = dev; |
4197 | 5fafdf24 | ths | while( *ptr && !isdigit((int)*ptr) ) ptr++; |
4198 | d5d10bc3 | ths | ppa = atoi(ptr); |
4199 | d5d10bc3 | ths | } |
4200 | d5d10bc3 | ths | |
4201 | d5d10bc3 | ths | /* Check if IP device was opened */
|
4202 | d5d10bc3 | ths | if( ip_fd )
|
4203 | d5d10bc3 | ths | close(ip_fd); |
4204 | d5d10bc3 | ths | |
4205 | aeb30be6 | balrog | TFR(ip_fd = open("/dev/udp", O_RDWR, 0)); |
4206 | aeb30be6 | balrog | if (ip_fd < 0) { |
4207 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
|
4208 | d5d10bc3 | ths | return -1; |
4209 | d5d10bc3 | ths | } |
4210 | d5d10bc3 | ths | |
4211 | aeb30be6 | balrog | TFR(tap_fd = open("/dev/tap", O_RDWR, 0)); |
4212 | aeb30be6 | balrog | if (tap_fd < 0) { |
4213 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't open /dev/tap");
|
4214 | d5d10bc3 | ths | return -1; |
4215 | d5d10bc3 | ths | } |
4216 | d5d10bc3 | ths | |
4217 | d5d10bc3 | ths | /* Assign a new PPA and get its unit number. */
|
4218 | d5d10bc3 | ths | strioc_ppa.ic_cmd = TUNNEWPPA; |
4219 | d5d10bc3 | ths | strioc_ppa.ic_timout = 0;
|
4220 | d5d10bc3 | ths | strioc_ppa.ic_len = sizeof(ppa);
|
4221 | d5d10bc3 | ths | strioc_ppa.ic_dp = (char *)&ppa;
|
4222 | d5d10bc3 | ths | if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) |
4223 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't assign new interface");
|
4224 | d5d10bc3 | ths | |
4225 | aeb30be6 | balrog | TFR(if_fd = open("/dev/tap", O_RDWR, 0)); |
4226 | aeb30be6 | balrog | if (if_fd < 0) { |
4227 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't open /dev/tap (2)");
|
4228 | d5d10bc3 | ths | return -1; |
4229 | d5d10bc3 | ths | } |
4230 | d5d10bc3 | ths | if(ioctl(if_fd, I_PUSH, "ip") < 0){ |
4231 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't push IP module");
|
4232 | d5d10bc3 | ths | return -1; |
4233 | d5d10bc3 | ths | } |
4234 | d5d10bc3 | ths | |
4235 | d5d10bc3 | ths | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) |
4236 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't get flags\n");
|
4237 | d5d10bc3 | ths | |
4238 | d5d10bc3 | ths | snprintf (actual_name, 32, "tap%d", ppa); |
4239 | d5d10bc3 | ths | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
|
4240 | d5d10bc3 | ths | |
4241 | d5d10bc3 | ths | ifr.lifr_ppa = ppa; |
4242 | d5d10bc3 | ths | /* Assign ppa according to the unit number returned by tun device */
|
4243 | d5d10bc3 | ths | |
4244 | d5d10bc3 | ths | if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) |
4245 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't set PPA %d", ppa);
|
4246 | d5d10bc3 | ths | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) |
4247 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't get flags\n");
|
4248 | d5d10bc3 | ths | /* Push arp module to if_fd */
|
4249 | d5d10bc3 | ths | if (ioctl (if_fd, I_PUSH, "arp") < 0) |
4250 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't push ARP module (2)");
|
4251 | d5d10bc3 | ths | |
4252 | d5d10bc3 | ths | /* Push arp module to ip_fd */
|
4253 | d5d10bc3 | ths | if (ioctl (ip_fd, I_POP, NULL) < 0) |
4254 | d5d10bc3 | ths | syslog (LOG_ERR, "I_POP failed\n");
|
4255 | d5d10bc3 | ths | if (ioctl (ip_fd, I_PUSH, "arp") < 0) |
4256 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't push ARP module (3)\n");
|
4257 | d5d10bc3 | ths | /* Open arp_fd */
|
4258 | aeb30be6 | balrog | TFR(arp_fd = open ("/dev/tap", O_RDWR, 0)); |
4259 | aeb30be6 | balrog | if (arp_fd < 0) |
4260 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't open %s\n", "/dev/tap"); |
4261 | d5d10bc3 | ths | |
4262 | d5d10bc3 | ths | /* Set ifname to arp */
|
4263 | d5d10bc3 | ths | strioc_if.ic_cmd = SIOCSLIFNAME; |
4264 | d5d10bc3 | ths | strioc_if.ic_timout = 0;
|
4265 | d5d10bc3 | ths | strioc_if.ic_len = sizeof(ifr);
|
4266 | d5d10bc3 | ths | strioc_if.ic_dp = (char *)𝔦
|
4267 | d5d10bc3 | ths | if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ |
4268 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't set ifname to arp\n");
|
4269 | d5d10bc3 | ths | } |
4270 | d5d10bc3 | ths | |
4271 | d5d10bc3 | ths | if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ |
4272 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't link TAP device to IP");
|
4273 | d5d10bc3 | ths | return -1; |
4274 | d5d10bc3 | ths | } |
4275 | d5d10bc3 | ths | |
4276 | d5d10bc3 | ths | if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) |
4277 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't link TAP device to ARP");
|
4278 | d5d10bc3 | ths | |
4279 | d5d10bc3 | ths | close (if_fd); |
4280 | d5d10bc3 | ths | |
4281 | d5d10bc3 | ths | memset(&ifr, 0x0, sizeof(ifr)); |
4282 | d5d10bc3 | ths | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
|
4283 | d5d10bc3 | ths | ifr.lifr_ip_muxid = ip_muxid; |
4284 | d5d10bc3 | ths | ifr.lifr_arp_muxid = arp_muxid; |
4285 | d5d10bc3 | ths | |
4286 | d5d10bc3 | ths | if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) |
4287 | d5d10bc3 | ths | { |
4288 | d5d10bc3 | ths | ioctl (ip_fd, I_PUNLINK , arp_muxid); |
4289 | d5d10bc3 | ths | ioctl (ip_fd, I_PUNLINK, ip_muxid); |
4290 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't set multiplexor id");
|
4291 | d5d10bc3 | ths | } |
4292 | d5d10bc3 | ths | |
4293 | d5d10bc3 | ths | sprintf(dev, "tap%d", ppa);
|
4294 | d5d10bc3 | ths | return tap_fd;
|
4295 | d5d10bc3 | ths | } |
4296 | d5d10bc3 | ths | |
4297 | ec530c81 | bellard | static int tap_open(char *ifname, int ifname_size) |
4298 | ec530c81 | bellard | { |
4299 | d5d10bc3 | ths | char dev[10]=""; |
4300 | d5d10bc3 | ths | int fd;
|
4301 | d5d10bc3 | ths | if( (fd = tap_alloc(dev)) < 0 ){ |
4302 | d5d10bc3 | ths | fprintf(stderr, "Cannot allocate TAP device\n");
|
4303 | d5d10bc3 | ths | return -1; |
4304 | d5d10bc3 | ths | } |
4305 | d5d10bc3 | ths | pstrcpy(ifname, ifname_size, dev); |
4306 | d5d10bc3 | ths | fcntl(fd, F_SETFL, O_NONBLOCK); |
4307 | d5d10bc3 | ths | return fd;
|
4308 | ec530c81 | bellard | } |
4309 | 7d3505c5 | bellard | #else
|
4310 | 7c9d8e07 | bellard | static int tap_open(char *ifname, int ifname_size) |
4311 | 330d0414 | bellard | { |
4312 | 80cabfad | bellard | struct ifreq ifr;
|
4313 | c4b1fcc0 | bellard | int fd, ret;
|
4314 | 3b46e624 | ths | |
4315 | aeb30be6 | balrog | TFR(fd = open("/dev/net/tun", O_RDWR));
|
4316 | 80cabfad | bellard | if (fd < 0) { |
4317 | 80cabfad | bellard | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
|
4318 | 80cabfad | bellard | return -1; |
4319 | 330d0414 | bellard | } |
4320 | 80cabfad | bellard | memset(&ifr, 0, sizeof(ifr)); |
4321 | 80cabfad | bellard | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
4322 | 7c9d8e07 | bellard | if (ifname[0] != '\0') |
4323 | 7c9d8e07 | bellard | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
4324 | 7c9d8e07 | bellard | else
|
4325 | 7c9d8e07 | bellard | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
|
4326 | 80cabfad | bellard | ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
|
4327 | 80cabfad | bellard | if (ret != 0) { |
4328 | 80cabfad | bellard | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
|
4329 | 80cabfad | bellard | close(fd); |
4330 | 80cabfad | bellard | return -1; |
4331 | 80cabfad | bellard | } |
4332 | c4b1fcc0 | bellard | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
4333 | 80cabfad | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
4334 | c4b1fcc0 | bellard | return fd;
|
4335 | c4b1fcc0 | bellard | } |
4336 | 7d3505c5 | bellard | #endif
|
4337 | 330d0414 | bellard | |
4338 | b46a8906 | ths | static int launch_script(const char *setup_script, const char *ifname, int fd) |
4339 | 7c9d8e07 | bellard | { |
4340 | b46a8906 | ths | int pid, status;
|
4341 | 7c9d8e07 | bellard | char *args[3]; |
4342 | 7c9d8e07 | bellard | char **parg;
|
4343 | 7c9d8e07 | bellard | |
4344 | b46a8906 | ths | /* try to launch network script */
|
4345 | 7c9d8e07 | bellard | pid = fork(); |
4346 | 7c9d8e07 | bellard | if (pid >= 0) { |
4347 | 7c9d8e07 | bellard | if (pid == 0) { |
4348 | 50d3eeae | ths | int open_max = sysconf (_SC_OPEN_MAX), i;
|
4349 | 50d3eeae | ths | for (i = 0; i < open_max; i++) |
4350 | 50d3eeae | ths | if (i != STDIN_FILENO &&
|
4351 | 50d3eeae | ths | i != STDOUT_FILENO && |
4352 | 50d3eeae | ths | i != STDERR_FILENO && |
4353 | 50d3eeae | ths | i != fd) |
4354 | 50d3eeae | ths | close(i); |
4355 | 50d3eeae | ths | |
4356 | 7c9d8e07 | bellard | parg = args; |
4357 | 7c9d8e07 | bellard | *parg++ = (char *)setup_script;
|
4358 | b46a8906 | ths | *parg++ = (char *)ifname;
|
4359 | 7c9d8e07 | bellard | *parg++ = NULL;
|
4360 | 7c9d8e07 | bellard | execv(setup_script, args); |
4361 | 4a38940d | bellard | _exit(1);
|
4362 | 7c9d8e07 | bellard | } |
4363 | 7c9d8e07 | bellard | while (waitpid(pid, &status, 0) != pid); |
4364 | 7c9d8e07 | bellard | if (!WIFEXITED(status) ||
|
4365 | 7c9d8e07 | bellard | WEXITSTATUS(status) != 0) {
|
4366 | 7c9d8e07 | bellard | fprintf(stderr, "%s: could not launch network script\n",
|
4367 | 7c9d8e07 | bellard | setup_script); |
4368 | 7c9d8e07 | bellard | return -1; |
4369 | 7c9d8e07 | bellard | } |
4370 | 7c9d8e07 | bellard | } |
4371 | b46a8906 | ths | return 0; |
4372 | b46a8906 | ths | } |
4373 | b46a8906 | ths | |
4374 | b46a8906 | ths | static int net_tap_init(VLANState *vlan, const char *ifname1, |
4375 | b46a8906 | ths | const char *setup_script, const char *down_script) |
4376 | b46a8906 | ths | { |
4377 | b46a8906 | ths | TAPState *s; |
4378 | b46a8906 | ths | int fd;
|
4379 | b46a8906 | ths | char ifname[128]; |
4380 | b46a8906 | ths | |
4381 | b46a8906 | ths | if (ifname1 != NULL) |
4382 | b46a8906 | ths | pstrcpy(ifname, sizeof(ifname), ifname1);
|
4383 | b46a8906 | ths | else
|
4384 | b46a8906 | ths | ifname[0] = '\0'; |
4385 | b46a8906 | ths | TFR(fd = tap_open(ifname, sizeof(ifname)));
|
4386 | b46a8906 | ths | if (fd < 0) |
4387 | b46a8906 | ths | return -1; |
4388 | b46a8906 | ths | |
4389 | b46a8906 | ths | if (!setup_script || !strcmp(setup_script, "no")) |
4390 | b46a8906 | ths | setup_script = "";
|
4391 | b46a8906 | ths | if (setup_script[0] != '\0') { |
4392 | b46a8906 | ths | if (launch_script(setup_script, ifname, fd))
|
4393 | b46a8906 | ths | return -1; |
4394 | 7c9d8e07 | bellard | } |
4395 | 7c9d8e07 | bellard | s = net_tap_fd_init(vlan, fd); |
4396 | 7c9d8e07 | bellard | if (!s)
|
4397 | 7c9d8e07 | bellard | return -1; |
4398 | 5fafdf24 | ths | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4399 | 7c9d8e07 | bellard | "tap: ifname=%s setup_script=%s", ifname, setup_script);
|
4400 | b46a8906 | ths | if (down_script && strcmp(down_script, "no")) |
4401 | b46a8906 | ths | snprintf(s->down_script, sizeof(s->down_script), "%s", down_script); |
4402 | 7c9d8e07 | bellard | return 0; |
4403 | 7c9d8e07 | bellard | } |
4404 | 7c9d8e07 | bellard | |
4405 | fd1dff4b | bellard | #endif /* !_WIN32 */ |
4406 | fd1dff4b | bellard | |
4407 | 7c9d8e07 | bellard | /* network connection */
|
4408 | 7c9d8e07 | bellard | typedef struct NetSocketState { |
4409 | 7c9d8e07 | bellard | VLANClientState *vc; |
4410 | 7c9d8e07 | bellard | int fd;
|
4411 | 7c9d8e07 | bellard | int state; /* 0 = getting length, 1 = getting data */ |
4412 | 7c9d8e07 | bellard | int index;
|
4413 | 7c9d8e07 | bellard | int packet_len;
|
4414 | 7c9d8e07 | bellard | uint8_t buf[4096];
|
4415 | 3d830459 | bellard | struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
4416 | 7c9d8e07 | bellard | } NetSocketState; |
4417 | 7c9d8e07 | bellard | |
4418 | 7c9d8e07 | bellard | typedef struct NetSocketListenState { |
4419 | 7c9d8e07 | bellard | VLANState *vlan; |
4420 | 7c9d8e07 | bellard | int fd;
|
4421 | 7c9d8e07 | bellard | } NetSocketListenState; |
4422 | 7c9d8e07 | bellard | |
4423 | 7c9d8e07 | bellard | /* XXX: we consider we can send the whole packet without blocking */
|
4424 | 7c9d8e07 | bellard | static void net_socket_receive(void *opaque, const uint8_t *buf, int size) |
4425 | c20709aa | bellard | { |
4426 | 7c9d8e07 | bellard | NetSocketState *s = opaque; |
4427 | 7c9d8e07 | bellard | uint32_t len; |
4428 | 7c9d8e07 | bellard | len = htonl(size); |
4429 | 7c9d8e07 | bellard | |
4430 | fd1dff4b | bellard | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
4431 | fd1dff4b | bellard | send_all(s->fd, buf, size); |
4432 | c20709aa | bellard | } |
4433 | c20709aa | bellard | |
4434 | 3d830459 | bellard | static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size) |
4435 | 3d830459 | bellard | { |
4436 | 3d830459 | bellard | NetSocketState *s = opaque; |
4437 | 5fafdf24 | ths | sendto(s->fd, buf, size, 0,
|
4438 | 3d830459 | bellard | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
4439 | 3d830459 | bellard | } |
4440 | 3d830459 | bellard | |
4441 | 7c9d8e07 | bellard | static void net_socket_send(void *opaque) |
4442 | c4b1fcc0 | bellard | { |
4443 | 7c9d8e07 | bellard | NetSocketState *s = opaque; |
4444 | fd1dff4b | bellard | int l, size, err;
|
4445 | 7c9d8e07 | bellard | uint8_t buf1[4096];
|
4446 | 7c9d8e07 | bellard | const uint8_t *buf;
|
4447 | 7c9d8e07 | bellard | |
4448 | fd1dff4b | bellard | size = recv(s->fd, buf1, sizeof(buf1), 0); |
4449 | fd1dff4b | bellard | if (size < 0) { |
4450 | fd1dff4b | bellard | err = socket_error(); |
4451 | 5fafdf24 | ths | if (err != EWOULDBLOCK)
|
4452 | fd1dff4b | bellard | goto eoc;
|
4453 | fd1dff4b | bellard | } else if (size == 0) { |
4454 | 7c9d8e07 | bellard | /* end of connection */
|
4455 | fd1dff4b | bellard | eoc:
|
4456 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
4457 | fd1dff4b | bellard | closesocket(s->fd); |
4458 | 7c9d8e07 | bellard | return;
|
4459 | 7c9d8e07 | bellard | } |
4460 | 7c9d8e07 | bellard | buf = buf1; |
4461 | 7c9d8e07 | bellard | while (size > 0) { |
4462 | 7c9d8e07 | bellard | /* reassemble a packet from the network */
|
4463 | 7c9d8e07 | bellard | switch(s->state) {
|
4464 | 7c9d8e07 | bellard | case 0: |
4465 | 7c9d8e07 | bellard | l = 4 - s->index;
|
4466 | 7c9d8e07 | bellard | if (l > size)
|
4467 | 7c9d8e07 | bellard | l = size; |
4468 | 7c9d8e07 | bellard | memcpy(s->buf + s->index, buf, l); |
4469 | 7c9d8e07 | bellard | buf += l; |
4470 | 7c9d8e07 | bellard | size -= l; |
4471 | 7c9d8e07 | bellard | s->index += l; |
4472 | 7c9d8e07 | bellard | if (s->index == 4) { |
4473 | 7c9d8e07 | bellard | /* got length */
|
4474 | 7c9d8e07 | bellard | s->packet_len = ntohl(*(uint32_t *)s->buf); |
4475 | 7c9d8e07 | bellard | s->index = 0;
|
4476 | 7c9d8e07 | bellard | s->state = 1;
|
4477 | 7c9d8e07 | bellard | } |
4478 | 7c9d8e07 | bellard | break;
|
4479 | 7c9d8e07 | bellard | case 1: |
4480 | 7c9d8e07 | bellard | l = s->packet_len - s->index; |
4481 | 7c9d8e07 | bellard | if (l > size)
|
4482 | 7c9d8e07 | bellard | l = size; |
4483 | 7c9d8e07 | bellard | memcpy(s->buf + s->index, buf, l); |
4484 | 7c9d8e07 | bellard | s->index += l; |
4485 | 7c9d8e07 | bellard | buf += l; |
4486 | 7c9d8e07 | bellard | size -= l; |
4487 | 7c9d8e07 | bellard | if (s->index >= s->packet_len) {
|
4488 | 7c9d8e07 | bellard | qemu_send_packet(s->vc, s->buf, s->packet_len); |
4489 | 7c9d8e07 | bellard | s->index = 0;
|
4490 | 7c9d8e07 | bellard | s->state = 0;
|
4491 | 7c9d8e07 | bellard | } |
4492 | 7c9d8e07 | bellard | break;
|
4493 | 7c9d8e07 | bellard | } |
4494 | 7c9d8e07 | bellard | } |
4495 | c20709aa | bellard | } |
4496 | c20709aa | bellard | |
4497 | 3d830459 | bellard | static void net_socket_send_dgram(void *opaque) |
4498 | 3d830459 | bellard | { |
4499 | 3d830459 | bellard | NetSocketState *s = opaque; |
4500 | 3d830459 | bellard | int size;
|
4501 | 3d830459 | bellard | |
4502 | 3d830459 | bellard | size = recv(s->fd, s->buf, sizeof(s->buf), 0); |
4503 | 5fafdf24 | ths | if (size < 0) |
4504 | 3d830459 | bellard | return;
|
4505 | 3d830459 | bellard | if (size == 0) { |
4506 | 3d830459 | bellard | /* end of connection */
|
4507 | 3d830459 | bellard | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
4508 | 3d830459 | bellard | return;
|
4509 | 3d830459 | bellard | } |
4510 | 3d830459 | bellard | qemu_send_packet(s->vc, s->buf, size); |
4511 | 3d830459 | bellard | } |
4512 | 3d830459 | bellard | |
4513 | 3d830459 | bellard | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
4514 | 3d830459 | bellard | { |
4515 | 3d830459 | bellard | struct ip_mreq imr;
|
4516 | 3d830459 | bellard | int fd;
|
4517 | 3d830459 | bellard | int val, ret;
|
4518 | 3d830459 | bellard | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
|
4519 | 3d830459 | bellard | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
|
4520 | 5fafdf24 | ths | inet_ntoa(mcastaddr->sin_addr), |
4521 | fd1dff4b | bellard | (int)ntohl(mcastaddr->sin_addr.s_addr));
|
4522 | 3d830459 | bellard | return -1; |
4523 | 3d830459 | bellard | |
4524 | 3d830459 | bellard | } |
4525 | 3d830459 | bellard | fd = socket(PF_INET, SOCK_DGRAM, 0);
|
4526 | 3d830459 | bellard | if (fd < 0) { |
4527 | 3d830459 | bellard | perror("socket(PF_INET, SOCK_DGRAM)");
|
4528 | 3d830459 | bellard | return -1; |
4529 | 3d830459 | bellard | } |
4530 | 3d830459 | bellard | |
4531 | fd1dff4b | bellard | val = 1;
|
4532 | 5fafdf24 | ths | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
4533 | fd1dff4b | bellard | (const char *)&val, sizeof(val)); |
4534 | fd1dff4b | bellard | if (ret < 0) { |
4535 | fd1dff4b | bellard | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
|
4536 | fd1dff4b | bellard | goto fail;
|
4537 | fd1dff4b | bellard | } |
4538 | fd1dff4b | bellard | |
4539 | fd1dff4b | bellard | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
4540 | fd1dff4b | bellard | if (ret < 0) { |
4541 | fd1dff4b | bellard | perror("bind");
|
4542 | fd1dff4b | bellard | goto fail;
|
4543 | fd1dff4b | bellard | } |
4544 | 3b46e624 | ths | |
4545 | 3d830459 | bellard | /* Add host to multicast group */
|
4546 | 3d830459 | bellard | imr.imr_multiaddr = mcastaddr->sin_addr; |
4547 | 3d830459 | bellard | imr.imr_interface.s_addr = htonl(INADDR_ANY); |
4548 | 3d830459 | bellard | |
4549 | 5fafdf24 | ths | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
4550 | fd1dff4b | bellard | (const char *)&imr, sizeof(struct ip_mreq)); |
4551 | 3d830459 | bellard | if (ret < 0) { |
4552 | 3d830459 | bellard | perror("setsockopt(IP_ADD_MEMBERSHIP)");
|
4553 | 3d830459 | bellard | goto fail;
|
4554 | 3d830459 | bellard | } |
4555 | 3d830459 | bellard | |
4556 | 3d830459 | bellard | /* Force mcast msgs to loopback (eg. several QEMUs in same host */
|
4557 | 3d830459 | bellard | val = 1;
|
4558 | 5fafdf24 | ths | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
4559 | fd1dff4b | bellard | (const char *)&val, sizeof(val)); |
4560 | 3d830459 | bellard | if (ret < 0) { |
4561 | 3d830459 | bellard | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
|
4562 | 3d830459 | bellard | goto fail;
|
4563 | 3d830459 | bellard | } |
4564 | 3d830459 | bellard | |
4565 | fd1dff4b | bellard | socket_set_nonblock(fd); |
4566 | 3d830459 | bellard | return fd;
|
4567 | 3d830459 | bellard | fail:
|
4568 | 5fafdf24 | ths | if (fd >= 0) |
4569 | 0bab00f3 | bellard | closesocket(fd); |
4570 | 3d830459 | bellard | return -1; |
4571 | 3d830459 | bellard | } |
4572 | 3d830459 | bellard | |
4573 | 5fafdf24 | ths | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, |
4574 | 3d830459 | bellard | int is_connected)
|
4575 | 3d830459 | bellard | { |
4576 | 3d830459 | bellard | struct sockaddr_in saddr;
|
4577 | 3d830459 | bellard | int newfd;
|
4578 | 3d830459 | bellard | socklen_t saddr_len; |
4579 | 3d830459 | bellard | NetSocketState *s; |
4580 | 3d830459 | bellard | |
4581 | 3d830459 | bellard | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
|
4582 | 5fafdf24 | ths | * Because this may be "shared" socket from a "master" process, datagrams would be recv()
|
4583 | 3d830459 | bellard | * by ONLY ONE process: we must "clone" this dgram socket --jjo
|
4584 | 3d830459 | bellard | */
|
4585 | 3d830459 | bellard | |
4586 | 3d830459 | bellard | if (is_connected) {
|
4587 | 3d830459 | bellard | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
4588 | 3d830459 | bellard | /* must be bound */
|
4589 | 3d830459 | bellard | if (saddr.sin_addr.s_addr==0) { |
4590 | 3d830459 | bellard | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
|
4591 | 3d830459 | bellard | fd); |
4592 | 3d830459 | bellard | return NULL; |
4593 | 3d830459 | bellard | } |
4594 | 3d830459 | bellard | /* clone dgram socket */
|
4595 | 3d830459 | bellard | newfd = net_socket_mcast_create(&saddr); |
4596 | 3d830459 | bellard | if (newfd < 0) { |
4597 | 3d830459 | bellard | /* error already reported by net_socket_mcast_create() */
|
4598 | 3d830459 | bellard | close(fd); |
4599 | 3d830459 | bellard | return NULL; |
4600 | 3d830459 | bellard | } |
4601 | 3d830459 | bellard | /* clone newfd to fd, close newfd */
|
4602 | 3d830459 | bellard | dup2(newfd, fd); |
4603 | 3d830459 | bellard | close(newfd); |
4604 | 5fafdf24 | ths | |
4605 | 3d830459 | bellard | } else {
|
4606 | 3d830459 | bellard | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
|
4607 | 3d830459 | bellard | fd, strerror(errno)); |
4608 | 3d830459 | bellard | return NULL; |
4609 | 3d830459 | bellard | } |
4610 | 3d830459 | bellard | } |
4611 | 3d830459 | bellard | |
4612 | 3d830459 | bellard | s = qemu_mallocz(sizeof(NetSocketState));
|
4613 | 3d830459 | bellard | if (!s)
|
4614 | 3d830459 | bellard | return NULL; |
4615 | 3d830459 | bellard | s->fd = fd; |
4616 | 3d830459 | bellard | |
4617 | d861b05e | pbrook | s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
|
4618 | 3d830459 | bellard | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
|
4619 | 3d830459 | bellard | |
4620 | 3d830459 | bellard | /* mcast: save bound address as dst */
|
4621 | 3d830459 | bellard | if (is_connected) s->dgram_dst=saddr;
|
4622 | 3d830459 | bellard | |
4623 | 3d830459 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4624 | 5fafdf24 | ths | "socket: fd=%d (%s mcast=%s:%d)",
|
4625 | 3d830459 | bellard | fd, is_connected? "cloned" : "", |
4626 | 3d830459 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4627 | 3d830459 | bellard | return s;
|
4628 | 3d830459 | bellard | } |
4629 | 3d830459 | bellard | |
4630 | 7c9d8e07 | bellard | static void net_socket_connect(void *opaque) |
4631 | c20709aa | bellard | { |
4632 | 7c9d8e07 | bellard | NetSocketState *s = opaque; |
4633 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
|
4634 | 7c9d8e07 | bellard | } |
4635 | c4b1fcc0 | bellard | |
4636 | 5fafdf24 | ths | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, |
4637 | 7c9d8e07 | bellard | int is_connected)
|
4638 | 7c9d8e07 | bellard | { |
4639 | 7c9d8e07 | bellard | NetSocketState *s; |
4640 | 7c9d8e07 | bellard | s = qemu_mallocz(sizeof(NetSocketState));
|
4641 | 7c9d8e07 | bellard | if (!s)
|
4642 | 7c9d8e07 | bellard | return NULL; |
4643 | 7c9d8e07 | bellard | s->fd = fd; |
4644 | 5fafdf24 | ths | s->vc = qemu_new_vlan_client(vlan, |
4645 | d861b05e | pbrook | net_socket_receive, NULL, s);
|
4646 | 7c9d8e07 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4647 | 7c9d8e07 | bellard | "socket: fd=%d", fd);
|
4648 | 7c9d8e07 | bellard | if (is_connected) {
|
4649 | 7c9d8e07 | bellard | net_socket_connect(s); |
4650 | 7c9d8e07 | bellard | } else {
|
4651 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
|
4652 | 7c9d8e07 | bellard | } |
4653 | 7c9d8e07 | bellard | return s;
|
4654 | 7c9d8e07 | bellard | } |
4655 | c4b1fcc0 | bellard | |
4656 | 5fafdf24 | ths | static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, |
4657 | 3d830459 | bellard | int is_connected)
|
4658 | 3d830459 | bellard | { |
4659 | 3d830459 | bellard | int so_type=-1, optlen=sizeof(so_type); |
4660 | 3d830459 | bellard | |
4661 | 69b34976 | ths | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, |
4662 | 69b34976 | ths | (socklen_t *)&optlen)< 0) {
|
4663 | 931f03ee | ths | fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
|
4664 | 3d830459 | bellard | return NULL; |
4665 | 3d830459 | bellard | } |
4666 | 3d830459 | bellard | switch(so_type) {
|
4667 | 3d830459 | bellard | case SOCK_DGRAM:
|
4668 | 3d830459 | bellard | return net_socket_fd_init_dgram(vlan, fd, is_connected);
|
4669 | 3d830459 | bellard | case SOCK_STREAM:
|
4670 | 3d830459 | bellard | return net_socket_fd_init_stream(vlan, fd, is_connected);
|
4671 | 3d830459 | bellard | default:
|
4672 | 3d830459 | bellard | /* who knows ... this could be a eg. a pty, do warn and continue as stream */
|
4673 | 3d830459 | bellard | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
|
4674 | 3d830459 | bellard | return net_socket_fd_init_stream(vlan, fd, is_connected);
|
4675 | 3d830459 | bellard | } |
4676 | 3d830459 | bellard | return NULL; |
4677 | 3d830459 | bellard | } |
4678 | 3d830459 | bellard | |
4679 | 7c9d8e07 | bellard | static void net_socket_accept(void *opaque) |
4680 | 7c9d8e07 | bellard | { |
4681 | 3b46e624 | ths | NetSocketListenState *s = opaque; |
4682 | 7c9d8e07 | bellard | NetSocketState *s1; |
4683 | 7c9d8e07 | bellard | struct sockaddr_in saddr;
|
4684 | 7c9d8e07 | bellard | socklen_t len; |
4685 | 7c9d8e07 | bellard | int fd;
|
4686 | 7c9d8e07 | bellard | |
4687 | 7c9d8e07 | bellard | for(;;) {
|
4688 | 7c9d8e07 | bellard | len = sizeof(saddr);
|
4689 | 7c9d8e07 | bellard | fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
|
4690 | 7c9d8e07 | bellard | if (fd < 0 && errno != EINTR) { |
4691 | 7c9d8e07 | bellard | return;
|
4692 | 7c9d8e07 | bellard | } else if (fd >= 0) { |
4693 | 7c9d8e07 | bellard | break;
|
4694 | 80cabfad | bellard | } |
4695 | 330d0414 | bellard | } |
4696 | 5fafdf24 | ths | s1 = net_socket_fd_init(s->vlan, fd, 1);
|
4697 | 7c9d8e07 | bellard | if (!s1) {
|
4698 | 0bab00f3 | bellard | closesocket(fd); |
4699 | 7c9d8e07 | bellard | } else {
|
4700 | 7c9d8e07 | bellard | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
|
4701 | 5fafdf24 | ths | "socket: connection from %s:%d",
|
4702 | 7c9d8e07 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4703 | 7c9d8e07 | bellard | } |
4704 | 7c9d8e07 | bellard | } |
4705 | 7c9d8e07 | bellard | |
4706 | 7c9d8e07 | bellard | static int net_socket_listen_init(VLANState *vlan, const char *host_str) |
4707 | 7c9d8e07 | bellard | { |
4708 | 7c9d8e07 | bellard | NetSocketListenState *s; |
4709 | 7c9d8e07 | bellard | int fd, val, ret;
|
4710 | 7c9d8e07 | bellard | struct sockaddr_in saddr;
|
4711 | 7c9d8e07 | bellard | |
4712 | 7c9d8e07 | bellard | if (parse_host_port(&saddr, host_str) < 0) |
4713 | 7c9d8e07 | bellard | return -1; |
4714 | 3b46e624 | ths | |
4715 | 7c9d8e07 | bellard | s = qemu_mallocz(sizeof(NetSocketListenState));
|
4716 | 7c9d8e07 | bellard | if (!s)
|
4717 | 7c9d8e07 | bellard | return -1; |
4718 | 7c9d8e07 | bellard | |
4719 | 7c9d8e07 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
4720 | 7c9d8e07 | bellard | if (fd < 0) { |
4721 | 7c9d8e07 | bellard | perror("socket");
|
4722 | 7c9d8e07 | bellard | return -1; |
4723 | 7c9d8e07 | bellard | } |
4724 | fd1dff4b | bellard | socket_set_nonblock(fd); |
4725 | 7c9d8e07 | bellard | |
4726 | 7c9d8e07 | bellard | /* allow fast reuse */
|
4727 | 7c9d8e07 | bellard | val = 1;
|
4728 | fd1dff4b | bellard | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
4729 | 3b46e624 | ths | |
4730 | 7c9d8e07 | bellard | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
4731 | 7c9d8e07 | bellard | if (ret < 0) { |
4732 | 7c9d8e07 | bellard | perror("bind");
|
4733 | 7c9d8e07 | bellard | return -1; |
4734 | 7c9d8e07 | bellard | } |
4735 | 7c9d8e07 | bellard | ret = listen(fd, 0);
|
4736 | 7c9d8e07 | bellard | if (ret < 0) { |
4737 | 7c9d8e07 | bellard | perror("listen");
|
4738 | 7c9d8e07 | bellard | return -1; |
4739 | 7c9d8e07 | bellard | } |
4740 | 7c9d8e07 | bellard | s->vlan = vlan; |
4741 | 7c9d8e07 | bellard | s->fd = fd; |
4742 | 7c9d8e07 | bellard | qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
|
4743 | 80cabfad | bellard | return 0; |
4744 | 330d0414 | bellard | } |
4745 | 330d0414 | bellard | |
4746 | 7c9d8e07 | bellard | static int net_socket_connect_init(VLANState *vlan, const char *host_str) |
4747 | 330d0414 | bellard | { |
4748 | 7c9d8e07 | bellard | NetSocketState *s; |
4749 | fd1dff4b | bellard | int fd, connected, ret, err;
|
4750 | 7c9d8e07 | bellard | struct sockaddr_in saddr;
|
4751 | 7c9d8e07 | bellard | |
4752 | 7c9d8e07 | bellard | if (parse_host_port(&saddr, host_str) < 0) |
4753 | 7c9d8e07 | bellard | return -1; |
4754 | 7c9d8e07 | bellard | |
4755 | 7c9d8e07 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
4756 | 7c9d8e07 | bellard | if (fd < 0) { |
4757 | 7c9d8e07 | bellard | perror("socket");
|
4758 | 7c9d8e07 | bellard | return -1; |
4759 | 7c9d8e07 | bellard | } |
4760 | fd1dff4b | bellard | socket_set_nonblock(fd); |
4761 | 7c9d8e07 | bellard | |
4762 | 7c9d8e07 | bellard | connected = 0;
|
4763 | 7c9d8e07 | bellard | for(;;) {
|
4764 | 7c9d8e07 | bellard | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
4765 | 7c9d8e07 | bellard | if (ret < 0) { |
4766 | fd1dff4b | bellard | err = socket_error(); |
4767 | fd1dff4b | bellard | if (err == EINTR || err == EWOULDBLOCK) {
|
4768 | fd1dff4b | bellard | } else if (err == EINPROGRESS) { |
4769 | 7c9d8e07 | bellard | break;
|
4770 | f5b12268 | ths | #ifdef _WIN32
|
4771 | f5b12268 | ths | } else if (err == WSAEALREADY) { |
4772 | f5b12268 | ths | break;
|
4773 | f5b12268 | ths | #endif
|
4774 | 7c9d8e07 | bellard | } else {
|
4775 | 7c9d8e07 | bellard | perror("connect");
|
4776 | fd1dff4b | bellard | closesocket(fd); |
4777 | 7c9d8e07 | bellard | return -1; |
4778 | 7c9d8e07 | bellard | } |
4779 | 7c9d8e07 | bellard | } else {
|
4780 | 7c9d8e07 | bellard | connected = 1;
|
4781 | 7c9d8e07 | bellard | break;
|
4782 | 7c9d8e07 | bellard | } |
4783 | 7c9d8e07 | bellard | } |
4784 | 7c9d8e07 | bellard | s = net_socket_fd_init(vlan, fd, connected); |
4785 | 7c9d8e07 | bellard | if (!s)
|
4786 | 7c9d8e07 | bellard | return -1; |
4787 | 7c9d8e07 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4788 | 5fafdf24 | ths | "socket: connect to %s:%d",
|
4789 | 7c9d8e07 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4790 | c20709aa | bellard | return 0; |
4791 | 80cabfad | bellard | } |
4792 | 330d0414 | bellard | |
4793 | 3d830459 | bellard | static int net_socket_mcast_init(VLANState *vlan, const char *host_str) |
4794 | 3d830459 | bellard | { |
4795 | 3d830459 | bellard | NetSocketState *s; |
4796 | 3d830459 | bellard | int fd;
|
4797 | 3d830459 | bellard | struct sockaddr_in saddr;
|
4798 | 3d830459 | bellard | |
4799 | 3d830459 | bellard | if (parse_host_port(&saddr, host_str) < 0) |
4800 | 3d830459 | bellard | return -1; |
4801 | 3d830459 | bellard | |
4802 | 3d830459 | bellard | |
4803 | 3d830459 | bellard | fd = net_socket_mcast_create(&saddr); |
4804 | 3d830459 | bellard | if (fd < 0) |
4805 | 3d830459 | bellard | return -1; |
4806 | 3d830459 | bellard | |
4807 | 3d830459 | bellard | s = net_socket_fd_init(vlan, fd, 0);
|
4808 | 3d830459 | bellard | if (!s)
|
4809 | 3d830459 | bellard | return -1; |
4810 | 3d830459 | bellard | |
4811 | 3d830459 | bellard | s->dgram_dst = saddr; |
4812 | 3b46e624 | ths | |
4813 | 3d830459 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4814 | 5fafdf24 | ths | "socket: mcast=%s:%d",
|
4815 | 3d830459 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4816 | 3d830459 | bellard | return 0; |
4817 | 3d830459 | bellard | |
4818 | 3d830459 | bellard | } |
4819 | 3d830459 | bellard | |
4820 | 609497ab | balrog | static const char *get_opt_name(char *buf, int buf_size, const char *p) |
4821 | 609497ab | balrog | { |
4822 | 609497ab | balrog | char *q;
|
4823 | 609497ab | balrog | |
4824 | 609497ab | balrog | q = buf; |
4825 | 609497ab | balrog | while (*p != '\0' && *p != '=') { |
4826 | 609497ab | balrog | if (q && (q - buf) < buf_size - 1) |
4827 | 609497ab | balrog | *q++ = *p; |
4828 | 609497ab | balrog | p++; |
4829 | 609497ab | balrog | } |
4830 | 609497ab | balrog | if (q)
|
4831 | 609497ab | balrog | *q = '\0';
|
4832 | 609497ab | balrog | |
4833 | 609497ab | balrog | return p;
|
4834 | 609497ab | balrog | } |
4835 | 609497ab | balrog | |
4836 | 609497ab | balrog | static const char *get_opt_value(char *buf, int buf_size, const char *p) |
4837 | e4bcb14c | ths | { |
4838 | e4bcb14c | ths | char *q;
|
4839 | e4bcb14c | ths | |
4840 | e4bcb14c | ths | q = buf; |
4841 | e4bcb14c | ths | while (*p != '\0') { |
4842 | 609497ab | balrog | if (*p == ',') { |
4843 | 609497ab | balrog | if (*(p + 1) != ',') |
4844 | e4bcb14c | ths | break;
|
4845 | e4bcb14c | ths | p++; |
4846 | 609497ab | balrog | } |
4847 | e4bcb14c | ths | if (q && (q - buf) < buf_size - 1) |
4848 | e4bcb14c | ths | *q++ = *p; |
4849 | e4bcb14c | ths | p++; |
4850 | e4bcb14c | ths | } |
4851 | e4bcb14c | ths | if (q)
|
4852 | e4bcb14c | ths | *q = '\0';
|
4853 | e4bcb14c | ths | |
4854 | e4bcb14c | ths | return p;
|
4855 | e4bcb14c | ths | } |
4856 | e4bcb14c | ths | |
4857 | 7c9d8e07 | bellard | static int get_param_value(char *buf, int buf_size, |
4858 | 7c9d8e07 | bellard | const char *tag, const char *str) |
4859 | 7c9d8e07 | bellard | { |
4860 | 7c9d8e07 | bellard | const char *p; |
4861 | 7c9d8e07 | bellard | char option[128]; |
4862 | 7c9d8e07 | bellard | |
4863 | 7c9d8e07 | bellard | p = str; |
4864 | 7c9d8e07 | bellard | for(;;) {
|
4865 | 609497ab | balrog | p = get_opt_name(option, sizeof(option), p);
|
4866 | 7c9d8e07 | bellard | if (*p != '=') |
4867 | 7c9d8e07 | bellard | break;
|
4868 | 7c9d8e07 | bellard | p++; |
4869 | 7c9d8e07 | bellard | if (!strcmp(tag, option)) {
|
4870 | 609497ab | balrog | (void)get_opt_value(buf, buf_size, p);
|
4871 | e4bcb14c | ths | return strlen(buf);
|
4872 | 7c9d8e07 | bellard | } else {
|
4873 | 609497ab | balrog | p = get_opt_value(NULL, 0, p); |
4874 | 7c9d8e07 | bellard | } |
4875 | 7c9d8e07 | bellard | if (*p != ',') |
4876 | 7c9d8e07 | bellard | break;
|
4877 | 7c9d8e07 | bellard | p++; |
4878 | 7c9d8e07 | bellard | } |
4879 | 7c9d8e07 | bellard | return 0; |
4880 | 7c9d8e07 | bellard | } |
4881 | 7c9d8e07 | bellard | |
4882 | e4bcb14c | ths | static int check_params(char *buf, int buf_size, |
4883 | e4bcb14c | ths | char **params, const char *str) |
4884 | e4bcb14c | ths | { |
4885 | e4bcb14c | ths | const char *p; |
4886 | e4bcb14c | ths | int i;
|
4887 | e4bcb14c | ths | |
4888 | e4bcb14c | ths | p = str; |
4889 | e4bcb14c | ths | for(;;) {
|
4890 | 609497ab | balrog | p = get_opt_name(buf, buf_size, p); |
4891 | e4bcb14c | ths | if (*p != '=') |
4892 | e4bcb14c | ths | return -1; |
4893 | e4bcb14c | ths | p++; |
4894 | e4bcb14c | ths | for(i = 0; params[i] != NULL; i++) |
4895 | e4bcb14c | ths | if (!strcmp(params[i], buf))
|
4896 | e4bcb14c | ths | break;
|
4897 | e4bcb14c | ths | if (params[i] == NULL) |
4898 | e4bcb14c | ths | return -1; |
4899 | 609497ab | balrog | p = get_opt_value(NULL, 0, p); |
4900 | e4bcb14c | ths | if (*p != ',') |
4901 | e4bcb14c | ths | break;
|
4902 | e4bcb14c | ths | p++; |
4903 | e4bcb14c | ths | } |
4904 | e4bcb14c | ths | return 0; |
4905 | e4bcb14c | ths | } |
4906 | e4bcb14c | ths | |
4907 | e4bcb14c | ths | |
4908 | 52f61fde | ths | static int net_client_init(const char *str) |
4909 | 7c9d8e07 | bellard | { |
4910 | 7c9d8e07 | bellard | const char *p; |
4911 | 7c9d8e07 | bellard | char *q;
|
4912 | 7c9d8e07 | bellard | char device[64]; |
4913 | 7c9d8e07 | bellard | char buf[1024]; |
4914 | 7c9d8e07 | bellard | int vlan_id, ret;
|
4915 | 7c9d8e07 | bellard | VLANState *vlan; |
4916 | 7c9d8e07 | bellard | |
4917 | 7c9d8e07 | bellard | p = str; |
4918 | 7c9d8e07 | bellard | q = device; |
4919 | 7c9d8e07 | bellard | while (*p != '\0' && *p != ',') { |
4920 | 7c9d8e07 | bellard | if ((q - device) < sizeof(device) - 1) |
4921 | 7c9d8e07 | bellard | *q++ = *p; |
4922 | 7c9d8e07 | bellard | p++; |
4923 | 7c9d8e07 | bellard | } |
4924 | 7c9d8e07 | bellard | *q = '\0';
|
4925 | 7c9d8e07 | bellard | if (*p == ',') |
4926 | 7c9d8e07 | bellard | p++; |
4927 | 7c9d8e07 | bellard | vlan_id = 0;
|
4928 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "vlan", p)) { |
4929 | 7c9d8e07 | bellard | vlan_id = strtol(buf, NULL, 0); |
4930 | 7c9d8e07 | bellard | } |
4931 | 7c9d8e07 | bellard | vlan = qemu_find_vlan(vlan_id); |
4932 | 7c9d8e07 | bellard | if (!vlan) {
|
4933 | 7c9d8e07 | bellard | fprintf(stderr, "Could not create vlan %d\n", vlan_id);
|
4934 | 7c9d8e07 | bellard | return -1; |
4935 | 7c9d8e07 | bellard | } |
4936 | 7c9d8e07 | bellard | if (!strcmp(device, "nic")) { |
4937 | 7c9d8e07 | bellard | NICInfo *nd; |
4938 | 7c9d8e07 | bellard | uint8_t *macaddr; |
4939 | 7c9d8e07 | bellard | |
4940 | 7c9d8e07 | bellard | if (nb_nics >= MAX_NICS) {
|
4941 | 7c9d8e07 | bellard | fprintf(stderr, "Too Many NICs\n");
|
4942 | 7c9d8e07 | bellard | return -1; |
4943 | 7c9d8e07 | bellard | } |
4944 | 7c9d8e07 | bellard | nd = &nd_table[nb_nics]; |
4945 | 7c9d8e07 | bellard | macaddr = nd->macaddr; |
4946 | 7c9d8e07 | bellard | macaddr[0] = 0x52; |
4947 | 7c9d8e07 | bellard | macaddr[1] = 0x54; |
4948 | 7c9d8e07 | bellard | macaddr[2] = 0x00; |
4949 | 7c9d8e07 | bellard | macaddr[3] = 0x12; |
4950 | 7c9d8e07 | bellard | macaddr[4] = 0x34; |
4951 | 7c9d8e07 | bellard | macaddr[5] = 0x56 + nb_nics; |
4952 | 7c9d8e07 | bellard | |
4953 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "macaddr", p)) { |
4954 | 7c9d8e07 | bellard | if (parse_macaddr(macaddr, buf) < 0) { |
4955 | 7c9d8e07 | bellard | fprintf(stderr, "invalid syntax for ethernet address\n");
|
4956 | 7c9d8e07 | bellard | return -1; |
4957 | 7c9d8e07 | bellard | } |
4958 | 7c9d8e07 | bellard | } |
4959 | a41b2ff2 | pbrook | if (get_param_value(buf, sizeof(buf), "model", p)) { |
4960 | a41b2ff2 | pbrook | nd->model = strdup(buf); |
4961 | a41b2ff2 | pbrook | } |
4962 | 7c9d8e07 | bellard | nd->vlan = vlan; |
4963 | 7c9d8e07 | bellard | nb_nics++; |
4964 | 833c7174 | blueswir1 | vlan->nb_guest_devs++; |
4965 | 7c9d8e07 | bellard | ret = 0;
|
4966 | 7c9d8e07 | bellard | } else
|
4967 | 7c9d8e07 | bellard | if (!strcmp(device, "none")) { |
4968 | 7c9d8e07 | bellard | /* does nothing. It is needed to signal that no network cards
|
4969 | 7c9d8e07 | bellard | are wanted */
|
4970 | 7c9d8e07 | bellard | ret = 0;
|
4971 | 7c9d8e07 | bellard | } else
|
4972 | 7c9d8e07 | bellard | #ifdef CONFIG_SLIRP
|
4973 | 7c9d8e07 | bellard | if (!strcmp(device, "user")) { |
4974 | 115defd1 | pbrook | if (get_param_value(buf, sizeof(buf), "hostname", p)) { |
4975 | 3f423c9c | bellard | pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
|
4976 | 115defd1 | pbrook | } |
4977 | 833c7174 | blueswir1 | vlan->nb_host_devs++; |
4978 | 7c9d8e07 | bellard | ret = net_slirp_init(vlan); |
4979 | 7c9d8e07 | bellard | } else
|
4980 | 7c9d8e07 | bellard | #endif
|
4981 | 7fb843f8 | bellard | #ifdef _WIN32
|
4982 | 7fb843f8 | bellard | if (!strcmp(device, "tap")) { |
4983 | 7fb843f8 | bellard | char ifname[64]; |
4984 | 7fb843f8 | bellard | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
4985 | 7fb843f8 | bellard | fprintf(stderr, "tap: no interface name\n");
|
4986 | 7fb843f8 | bellard | return -1; |
4987 | 7fb843f8 | bellard | } |
4988 | 833c7174 | blueswir1 | vlan->nb_host_devs++; |
4989 | 7fb843f8 | bellard | ret = tap_win32_init(vlan, ifname); |
4990 | 7fb843f8 | bellard | } else
|
4991 | 7fb843f8 | bellard | #else
|
4992 | 7c9d8e07 | bellard | if (!strcmp(device, "tap")) { |
4993 | 7c9d8e07 | bellard | char ifname[64]; |
4994 | b46a8906 | ths | char setup_script[1024], down_script[1024]; |
4995 | 7c9d8e07 | bellard | int fd;
|
4996 | 4f01035f | pbrook | vlan->nb_host_devs++; |
4997 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
4998 | 7c9d8e07 | bellard | fd = strtol(buf, NULL, 0); |
4999 | 64538cdf | pbrook | fcntl(fd, F_SETFL, O_NONBLOCK); |
5000 | 7c9d8e07 | bellard | ret = -1;
|
5001 | 7c9d8e07 | bellard | if (net_tap_fd_init(vlan, fd))
|
5002 | 7c9d8e07 | bellard | ret = 0;
|
5003 | 7c9d8e07 | bellard | } else {
|
5004 | bf8c5342 | bellard | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
5005 | bf8c5342 | bellard | ifname[0] = '\0'; |
5006 | bf8c5342 | bellard | } |
5007 | 7c9d8e07 | bellard | if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) { |
5008 | 7c9d8e07 | bellard | pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
|
5009 | 7c9d8e07 | bellard | } |
5010 | b46a8906 | ths | if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) { |
5011 | b46a8906 | ths | pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
|
5012 | b46a8906 | ths | } |
5013 | b46a8906 | ths | ret = net_tap_init(vlan, ifname, setup_script, down_script); |
5014 | 7c9d8e07 | bellard | } |
5015 | 7c9d8e07 | bellard | } else
|
5016 | fd1dff4b | bellard | #endif
|
5017 | 7c9d8e07 | bellard | if (!strcmp(device, "socket")) { |
5018 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
5019 | 7c9d8e07 | bellard | int fd;
|
5020 | 7c9d8e07 | bellard | fd = strtol(buf, NULL, 0); |
5021 | 7c9d8e07 | bellard | ret = -1;
|
5022 | 7c9d8e07 | bellard | if (net_socket_fd_init(vlan, fd, 1)) |
5023 | 7c9d8e07 | bellard | ret = 0;
|
5024 | 7c9d8e07 | bellard | } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) { |
5025 | 7c9d8e07 | bellard | ret = net_socket_listen_init(vlan, buf); |
5026 | 7c9d8e07 | bellard | } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { |
5027 | 7c9d8e07 | bellard | ret = net_socket_connect_init(vlan, buf); |
5028 | 3d830459 | bellard | } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { |
5029 | 3d830459 | bellard | ret = net_socket_mcast_init(vlan, buf); |
5030 | 7c9d8e07 | bellard | } else {
|
5031 | 7c9d8e07 | bellard | fprintf(stderr, "Unknown socket options: %s\n", p);
|
5032 | 7c9d8e07 | bellard | return -1; |
5033 | 7c9d8e07 | bellard | } |
5034 | 833c7174 | blueswir1 | vlan->nb_host_devs++; |
5035 | 7c9d8e07 | bellard | } else
|
5036 | 7c9d8e07 | bellard | { |
5037 | 7c9d8e07 | bellard | fprintf(stderr, "Unknown network device: %s\n", device);
|
5038 | 7c9d8e07 | bellard | return -1; |
5039 | 7c9d8e07 | bellard | } |
5040 | 7c9d8e07 | bellard | if (ret < 0) { |
5041 | 7c9d8e07 | bellard | fprintf(stderr, "Could not initialize device '%s'\n", device);
|
5042 | 7c9d8e07 | bellard | } |
5043 | 3b46e624 | ths | |
5044 | 7c9d8e07 | bellard | return ret;
|
5045 | 7c9d8e07 | bellard | } |
5046 | 7c9d8e07 | bellard | |
5047 | 7c9d8e07 | bellard | void do_info_network(void) |
5048 | 7c9d8e07 | bellard | { |
5049 | 7c9d8e07 | bellard | VLANState *vlan; |
5050 | 7c9d8e07 | bellard | VLANClientState *vc; |
5051 | 7c9d8e07 | bellard | |
5052 | 7c9d8e07 | bellard | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
5053 | 7c9d8e07 | bellard | term_printf("VLAN %d devices:\n", vlan->id);
|
5054 | 7c9d8e07 | bellard | for(vc = vlan->first_client; vc != NULL; vc = vc->next) |
5055 | 7c9d8e07 | bellard | term_printf(" %s\n", vc->info_str);
|
5056 | 7c9d8e07 | bellard | } |
5057 | 7c9d8e07 | bellard | } |
5058 | 42550fde | ths | |
5059 | 609497ab | balrog | #define HD_ALIAS "index=%d,media=disk" |
5060 | e4bcb14c | ths | #ifdef TARGET_PPC
|
5061 | e4bcb14c | ths | #define CDROM_ALIAS "index=1,media=cdrom" |
5062 | e4bcb14c | ths | #else
|
5063 | e4bcb14c | ths | #define CDROM_ALIAS "index=2,media=cdrom" |
5064 | e4bcb14c | ths | #endif
|
5065 | e4bcb14c | ths | #define FD_ALIAS "index=%d,if=floppy" |
5066 | 609497ab | balrog | #define PFLASH_ALIAS "if=pflash" |
5067 | 609497ab | balrog | #define MTD_ALIAS "if=mtd" |
5068 | 9d413d1d | balrog | #define SD_ALIAS "index=0,if=sd" |
5069 | e4bcb14c | ths | |
5070 | 609497ab | balrog | static int drive_add(const char *file, const char *fmt, ...) |
5071 | e4bcb14c | ths | { |
5072 | e4bcb14c | ths | va_list ap; |
5073 | e4bcb14c | ths | |
5074 | e4bcb14c | ths | if (nb_drives_opt >= MAX_DRIVES) {
|
5075 | e4bcb14c | ths | fprintf(stderr, "qemu: too many drives\n");
|
5076 | e4bcb14c | ths | exit(1);
|
5077 | e4bcb14c | ths | } |
5078 | e4bcb14c | ths | |
5079 | 609497ab | balrog | drives_opt[nb_drives_opt].file = file; |
5080 | e4bcb14c | ths | va_start(ap, fmt); |
5081 | 609497ab | balrog | vsnprintf(drives_opt[nb_drives_opt].opt, |
5082 | 609497ab | balrog | sizeof(drives_opt[0].opt), fmt, ap); |
5083 | e4bcb14c | ths | va_end(ap); |
5084 | e4bcb14c | ths | |
5085 | e4bcb14c | ths | return nb_drives_opt++;
|
5086 | e4bcb14c | ths | } |
5087 | e4bcb14c | ths | |
5088 | f60d39bc | ths | int drive_get_index(BlockInterfaceType type, int bus, int unit) |
5089 | e4bcb14c | ths | { |
5090 | e4bcb14c | ths | int index;
|
5091 | e4bcb14c | ths | |
5092 | e4bcb14c | ths | /* seek interface, bus and unit */
|
5093 | e4bcb14c | ths | |
5094 | e4bcb14c | ths | for (index = 0; index < nb_drives; index++) |
5095 | f60d39bc | ths | if (drives_table[index].type == type &&
|
5096 | e4bcb14c | ths | drives_table[index].bus == bus && |
5097 | e4bcb14c | ths | drives_table[index].unit == unit) |
5098 | e4bcb14c | ths | return index;
|
5099 | e4bcb14c | ths | |
5100 | e4bcb14c | ths | return -1; |
5101 | e4bcb14c | ths | } |
5102 | e4bcb14c | ths | |
5103 | f60d39bc | ths | int drive_get_max_bus(BlockInterfaceType type)
|
5104 | e4bcb14c | ths | { |
5105 | e4bcb14c | ths | int max_bus;
|
5106 | e4bcb14c | ths | int index;
|
5107 | e4bcb14c | ths | |
5108 | e4bcb14c | ths | max_bus = -1;
|
5109 | e4bcb14c | ths | for (index = 0; index < nb_drives; index++) { |
5110 | f60d39bc | ths | if(drives_table[index].type == type &&
|
5111 | e4bcb14c | ths | drives_table[index].bus > max_bus) |
5112 | e4bcb14c | ths | max_bus = drives_table[index].bus; |
5113 | e4bcb14c | ths | } |
5114 | e4bcb14c | ths | return max_bus;
|
5115 | e4bcb14c | ths | } |
5116 | e4bcb14c | ths | |
5117 | a1620fac | aurel32 | static void bdrv_format_print(void *opaque, const char *name) |
5118 | a1620fac | aurel32 | { |
5119 | a1620fac | aurel32 | fprintf(stderr, " %s", name);
|
5120 | a1620fac | aurel32 | } |
5121 | a1620fac | aurel32 | |
5122 | 609497ab | balrog | static int drive_init(struct drive_opt *arg, int snapshot, |
5123 | 609497ab | balrog | QEMUMachine *machine) |
5124 | e4bcb14c | ths | { |
5125 | e4bcb14c | ths | char buf[128]; |
5126 | e4bcb14c | ths | char file[1024]; |
5127 | c8522bdf | balrog | char devname[128]; |
5128 | c8522bdf | balrog | const char *mediastr = ""; |
5129 | f60d39bc | ths | BlockInterfaceType type; |
5130 | e4bcb14c | ths | enum { MEDIA_DISK, MEDIA_CDROM } media;
|
5131 | e4bcb14c | ths | int bus_id, unit_id;
|
5132 | e4bcb14c | ths | int cyls, heads, secs, translation;
|
5133 | e4bcb14c | ths | BlockDriverState *bdrv; |
5134 | 1e72d3b7 | aurel32 | BlockDriver *drv = NULL;
|
5135 | e4bcb14c | ths | int max_devs;
|
5136 | e4bcb14c | ths | int index;
|
5137 | 33f00271 | balrog | int cache;
|
5138 | 33f00271 | balrog | int bdrv_flags;
|
5139 | 609497ab | balrog | char *str = arg->opt;
|
5140 | e4bcb14c | ths | char *params[] = { "bus", "unit", "if", "index", "cyls", "heads", |
5141 | 33f00271 | balrog | "secs", "trans", "media", "snapshot", "file", |
5142 | 1e72d3b7 | aurel32 | "cache", "format", NULL }; |
5143 | e4bcb14c | ths | |
5144 | e4bcb14c | ths | if (check_params(buf, sizeof(buf), params, str) < 0) { |
5145 | ff993638 | balrog | fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
|
5146 | e4bcb14c | ths | buf, str); |
5147 | e4bcb14c | ths | return -1; |
5148 | e4bcb14c | ths | } |
5149 | e4bcb14c | ths | |
5150 | e4bcb14c | ths | file[0] = 0; |
5151 | e4bcb14c | ths | cyls = heads = secs = 0;
|
5152 | e4bcb14c | ths | bus_id = 0;
|
5153 | e4bcb14c | ths | unit_id = -1;
|
5154 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_AUTO; |
5155 | e4bcb14c | ths | index = -1;
|
5156 | 33f00271 | balrog | cache = 1;
|
5157 | e4bcb14c | ths | |
5158 | e4bcb14c | ths | if (!strcmp(machine->name, "realview") || |
5159 | e4bcb14c | ths | !strcmp(machine->name, "SS-5") ||
|
5160 | e4bcb14c | ths | !strcmp(machine->name, "SS-10") ||
|
5161 | e4bcb14c | ths | !strcmp(machine->name, "SS-600MP") ||
|
5162 | e4bcb14c | ths | !strcmp(machine->name, "versatilepb") ||
|
5163 | e4bcb14c | ths | !strcmp(machine->name, "versatileab")) {
|
5164 | f60d39bc | ths | type = IF_SCSI; |
5165 | e4bcb14c | ths | max_devs = MAX_SCSI_DEVS; |
5166 | c8522bdf | balrog | strcpy(devname, "scsi");
|
5167 | e4bcb14c | ths | } else {
|
5168 | f60d39bc | ths | type = IF_IDE; |
5169 | e4bcb14c | ths | max_devs = MAX_IDE_DEVS; |
5170 | c8522bdf | balrog | strcpy(devname, "ide");
|
5171 | e4bcb14c | ths | } |
5172 | e4bcb14c | ths | media = MEDIA_DISK; |
5173 | e4bcb14c | ths | |
5174 | e4bcb14c | ths | /* extract parameters */
|
5175 | e4bcb14c | ths | |
5176 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "bus", str)) { |
5177 | e4bcb14c | ths | bus_id = strtol(buf, NULL, 0); |
5178 | e4bcb14c | ths | if (bus_id < 0) { |
5179 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
|
5180 | e4bcb14c | ths | return -1; |
5181 | e4bcb14c | ths | } |
5182 | e4bcb14c | ths | } |
5183 | e4bcb14c | ths | |
5184 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "unit", str)) { |
5185 | e4bcb14c | ths | unit_id = strtol(buf, NULL, 0); |
5186 | e4bcb14c | ths | if (unit_id < 0) { |
5187 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
|
5188 | e4bcb14c | ths | return -1; |
5189 | e4bcb14c | ths | } |
5190 | e4bcb14c | ths | } |
5191 | e4bcb14c | ths | |
5192 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "if", str)) { |
5193 | ae45d369 | bellard | pstrcpy(devname, sizeof(devname), buf);
|
5194 | e4bcb14c | ths | if (!strcmp(buf, "ide")) { |
5195 | f60d39bc | ths | type = IF_IDE; |
5196 | e4bcb14c | ths | max_devs = MAX_IDE_DEVS; |
5197 | e4bcb14c | ths | } else if (!strcmp(buf, "scsi")) { |
5198 | f60d39bc | ths | type = IF_SCSI; |
5199 | e4bcb14c | ths | max_devs = MAX_SCSI_DEVS; |
5200 | e4bcb14c | ths | } else if (!strcmp(buf, "floppy")) { |
5201 | f60d39bc | ths | type = IF_FLOPPY; |
5202 | e4bcb14c | ths | max_devs = 0;
|
5203 | e4bcb14c | ths | } else if (!strcmp(buf, "pflash")) { |
5204 | f60d39bc | ths | type = IF_PFLASH; |
5205 | e4bcb14c | ths | max_devs = 0;
|
5206 | e4bcb14c | ths | } else if (!strcmp(buf, "mtd")) { |
5207 | f60d39bc | ths | type = IF_MTD; |
5208 | e4bcb14c | ths | max_devs = 0;
|
5209 | e4bcb14c | ths | } else if (!strcmp(buf, "sd")) { |
5210 | f60d39bc | ths | type = IF_SD; |
5211 | e4bcb14c | ths | max_devs = 0;
|
5212 | e4bcb14c | ths | } else {
|
5213 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
|
5214 | e4bcb14c | ths | return -1; |
5215 | e4bcb14c | ths | } |
5216 | e4bcb14c | ths | } |
5217 | e4bcb14c | ths | |
5218 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "index", str)) { |
5219 | e4bcb14c | ths | index = strtol(buf, NULL, 0); |
5220 | e4bcb14c | ths | if (index < 0) { |
5221 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid index\n", str);
|
5222 | e4bcb14c | ths | return -1; |
5223 | e4bcb14c | ths | } |
5224 | e4bcb14c | ths | } |
5225 | e4bcb14c | ths | |
5226 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "cyls", str)) { |
5227 | e4bcb14c | ths | cyls = strtol(buf, NULL, 0); |
5228 | e4bcb14c | ths | } |
5229 | e4bcb14c | ths | |
5230 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "heads", str)) { |
5231 | e4bcb14c | ths | heads = strtol(buf, NULL, 0); |
5232 | e4bcb14c | ths | } |
5233 | e4bcb14c | ths | |
5234 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "secs", str)) { |
5235 | e4bcb14c | ths | secs = strtol(buf, NULL, 0); |
5236 | e4bcb14c | ths | } |
5237 | e4bcb14c | ths | |
5238 | e4bcb14c | ths | if (cyls || heads || secs) {
|
5239 | e4bcb14c | ths | if (cyls < 1 || cyls > 16383) { |
5240 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
|
5241 | e4bcb14c | ths | return -1; |
5242 | e4bcb14c | ths | } |
5243 | e4bcb14c | ths | if (heads < 1 || heads > 16) { |
5244 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
|
5245 | e4bcb14c | ths | return -1; |
5246 | e4bcb14c | ths | } |
5247 | e4bcb14c | ths | if (secs < 1 || secs > 63) { |
5248 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
|
5249 | e4bcb14c | ths | return -1; |
5250 | e4bcb14c | ths | } |
5251 | e4bcb14c | ths | } |
5252 | e4bcb14c | ths | |
5253 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "trans", str)) { |
5254 | e4bcb14c | ths | if (!cyls) {
|
5255 | e4bcb14c | ths | fprintf(stderr, |
5256 | e4bcb14c | ths | "qemu: '%s' trans must be used with cyls,heads and secs\n",
|
5257 | e4bcb14c | ths | str); |
5258 | e4bcb14c | ths | return -1; |
5259 | e4bcb14c | ths | } |
5260 | e4bcb14c | ths | if (!strcmp(buf, "none")) |
5261 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_NONE; |
5262 | e4bcb14c | ths | else if (!strcmp(buf, "lba")) |
5263 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_LBA; |
5264 | e4bcb14c | ths | else if (!strcmp(buf, "auto")) |
5265 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_AUTO; |
5266 | e4bcb14c | ths | else {
|
5267 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
|
5268 | e4bcb14c | ths | return -1; |
5269 | e4bcb14c | ths | } |
5270 | e4bcb14c | ths | } |
5271 | e4bcb14c | ths | |
5272 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "media", str)) { |
5273 | e4bcb14c | ths | if (!strcmp(buf, "disk")) { |
5274 | e4bcb14c | ths | media = MEDIA_DISK; |
5275 | e4bcb14c | ths | } else if (!strcmp(buf, "cdrom")) { |
5276 | e4bcb14c | ths | if (cyls || secs || heads) {
|
5277 | e4bcb14c | ths | fprintf(stderr, |
5278 | e4bcb14c | ths | "qemu: '%s' invalid physical CHS format\n", str);
|
5279 | e4bcb14c | ths | return -1; |
5280 | e4bcb14c | ths | } |
5281 | e4bcb14c | ths | media = MEDIA_CDROM; |
5282 | e4bcb14c | ths | } else {
|
5283 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid media\n", str);
|
5284 | e4bcb14c | ths | return -1; |
5285 | e4bcb14c | ths | } |
5286 | e4bcb14c | ths | } |
5287 | e4bcb14c | ths | |
5288 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "snapshot", str)) { |
5289 | e4bcb14c | ths | if (!strcmp(buf, "on")) |
5290 | e4bcb14c | ths | snapshot = 1;
|
5291 | e4bcb14c | ths | else if (!strcmp(buf, "off")) |
5292 | e4bcb14c | ths | snapshot = 0;
|
5293 | e4bcb14c | ths | else {
|
5294 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
|
5295 | e4bcb14c | ths | return -1; |
5296 | e4bcb14c | ths | } |
5297 | e4bcb14c | ths | } |
5298 | e4bcb14c | ths | |
5299 | 33f00271 | balrog | if (get_param_value(buf, sizeof(buf), "cache", str)) { |
5300 | 33f00271 | balrog | if (!strcmp(buf, "off")) |
5301 | 33f00271 | balrog | cache = 0;
|
5302 | 33f00271 | balrog | else if (!strcmp(buf, "on")) |
5303 | 33f00271 | balrog | cache = 1;
|
5304 | 33f00271 | balrog | else {
|
5305 | 33f00271 | balrog | fprintf(stderr, "qemu: invalid cache option\n");
|
5306 | 33f00271 | balrog | return -1; |
5307 | 33f00271 | balrog | } |
5308 | 33f00271 | balrog | } |
5309 | 33f00271 | balrog | |
5310 | 1e72d3b7 | aurel32 | if (get_param_value(buf, sizeof(buf), "format", str)) { |
5311 | a1620fac | aurel32 | if (strcmp(buf, "?") == 0) { |
5312 | a1620fac | aurel32 | fprintf(stderr, "qemu: Supported formats:");
|
5313 | a1620fac | aurel32 | bdrv_iterate_format(bdrv_format_print, NULL);
|
5314 | a1620fac | aurel32 | fprintf(stderr, "\n");
|
5315 | a1620fac | aurel32 | return -1; |
5316 | a1620fac | aurel32 | } |
5317 | 1e72d3b7 | aurel32 | drv = bdrv_find_format(buf); |
5318 | 1e72d3b7 | aurel32 | if (!drv) {
|
5319 | 1e72d3b7 | aurel32 | fprintf(stderr, "qemu: '%s' invalid format\n", buf);
|
5320 | 1e72d3b7 | aurel32 | return -1; |
5321 | 1e72d3b7 | aurel32 | } |
5322 | 1e72d3b7 | aurel32 | } |
5323 | 1e72d3b7 | aurel32 | |
5324 | 609497ab | balrog | if (arg->file == NULL) |
5325 | 609497ab | balrog | get_param_value(file, sizeof(file), "file", str); |
5326 | 609497ab | balrog | else
|
5327 | 609497ab | balrog | pstrcpy(file, sizeof(file), arg->file);
|
5328 | e4bcb14c | ths | |
5329 | e4bcb14c | ths | /* compute bus and unit according index */
|
5330 | e4bcb14c | ths | |
5331 | e4bcb14c | ths | if (index != -1) { |
5332 | e4bcb14c | ths | if (bus_id != 0 || unit_id != -1) { |
5333 | e4bcb14c | ths | fprintf(stderr, |
5334 | e4bcb14c | ths | "qemu: '%s' index cannot be used with bus and unit\n", str);
|
5335 | e4bcb14c | ths | return -1; |
5336 | e4bcb14c | ths | } |
5337 | e4bcb14c | ths | if (max_devs == 0) |
5338 | e4bcb14c | ths | { |
5339 | e4bcb14c | ths | unit_id = index; |
5340 | e4bcb14c | ths | bus_id = 0;
|
5341 | e4bcb14c | ths | } else {
|
5342 | e4bcb14c | ths | unit_id = index % max_devs; |
5343 | e4bcb14c | ths | bus_id = index / max_devs; |
5344 | e4bcb14c | ths | } |
5345 | e4bcb14c | ths | } |
5346 | e4bcb14c | ths | |
5347 | e4bcb14c | ths | /* if user doesn't specify a unit_id,
|
5348 | e4bcb14c | ths | * try to find the first free
|
5349 | e4bcb14c | ths | */
|
5350 | e4bcb14c | ths | |
5351 | e4bcb14c | ths | if (unit_id == -1) { |
5352 | e4bcb14c | ths | unit_id = 0;
|
5353 | f60d39bc | ths | while (drive_get_index(type, bus_id, unit_id) != -1) { |
5354 | e4bcb14c | ths | unit_id++; |
5355 | e4bcb14c | ths | if (max_devs && unit_id >= max_devs) {
|
5356 | e4bcb14c | ths | unit_id -= max_devs; |
5357 | e4bcb14c | ths | bus_id++; |
5358 | e4bcb14c | ths | } |
5359 | e4bcb14c | ths | } |
5360 | e4bcb14c | ths | } |
5361 | e4bcb14c | ths | |
5362 | e4bcb14c | ths | /* check unit id */
|
5363 | e4bcb14c | ths | |
5364 | e4bcb14c | ths | if (max_devs && unit_id >= max_devs) {
|
5365 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
|
5366 | e4bcb14c | ths | str, unit_id, max_devs - 1);
|
5367 | e4bcb14c | ths | return -1; |
5368 | e4bcb14c | ths | } |
5369 | e4bcb14c | ths | |
5370 | e4bcb14c | ths | /*
|
5371 | e4bcb14c | ths | * ignore multiple definitions
|
5372 | e4bcb14c | ths | */
|
5373 | e4bcb14c | ths | |
5374 | f60d39bc | ths | if (drive_get_index(type, bus_id, unit_id) != -1) |
5375 | e4bcb14c | ths | return 0; |
5376 | e4bcb14c | ths | |
5377 | e4bcb14c | ths | /* init */
|
5378 | e4bcb14c | ths | |
5379 | f60d39bc | ths | if (type == IF_IDE || type == IF_SCSI)
|
5380 | c8522bdf | balrog | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
5381 | e6198a70 | balrog | if (max_devs)
|
5382 | e6198a70 | balrog | snprintf(buf, sizeof(buf), "%s%i%s%i", |
5383 | e6198a70 | balrog | devname, bus_id, mediastr, unit_id); |
5384 | e6198a70 | balrog | else
|
5385 | e6198a70 | balrog | snprintf(buf, sizeof(buf), "%s%s%i", |
5386 | e6198a70 | balrog | devname, mediastr, unit_id); |
5387 | e4bcb14c | ths | bdrv = bdrv_new(buf); |
5388 | e4bcb14c | ths | drives_table[nb_drives].bdrv = bdrv; |
5389 | f60d39bc | ths | drives_table[nb_drives].type = type; |
5390 | e4bcb14c | ths | drives_table[nb_drives].bus = bus_id; |
5391 | e4bcb14c | ths | drives_table[nb_drives].unit = unit_id; |
5392 | e4bcb14c | ths | nb_drives++; |
5393 | e4bcb14c | ths | |
5394 | f60d39bc | ths | switch(type) {
|
5395 | e4bcb14c | ths | case IF_IDE:
|
5396 | e4bcb14c | ths | case IF_SCSI:
|
5397 | e4bcb14c | ths | switch(media) {
|
5398 | e4bcb14c | ths | case MEDIA_DISK:
|
5399 | e4bcb14c | ths | if (cyls != 0) { |
5400 | e4bcb14c | ths | bdrv_set_geometry_hint(bdrv, cyls, heads, secs); |
5401 | e4bcb14c | ths | bdrv_set_translation_hint(bdrv, translation); |
5402 | e4bcb14c | ths | } |
5403 | e4bcb14c | ths | break;
|
5404 | e4bcb14c | ths | case MEDIA_CDROM:
|
5405 | e4bcb14c | ths | bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM); |
5406 | e4bcb14c | ths | break;
|
5407 | e4bcb14c | ths | } |
5408 | e4bcb14c | ths | break;
|
5409 | e4bcb14c | ths | case IF_SD:
|
5410 | e4bcb14c | ths | /* FIXME: This isn't really a floppy, but it's a reasonable
|
5411 | e4bcb14c | ths | approximation. */
|
5412 | e4bcb14c | ths | case IF_FLOPPY:
|
5413 | e4bcb14c | ths | bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY); |
5414 | e4bcb14c | ths | break;
|
5415 | e4bcb14c | ths | case IF_PFLASH:
|
5416 | e4bcb14c | ths | case IF_MTD:
|
5417 | e4bcb14c | ths | break;
|
5418 | e4bcb14c | ths | } |
5419 | e4bcb14c | ths | if (!file[0]) |
5420 | e4bcb14c | ths | return 0; |
5421 | 33f00271 | balrog | bdrv_flags = 0;
|
5422 | 33f00271 | balrog | if (snapshot)
|
5423 | 33f00271 | balrog | bdrv_flags |= BDRV_O_SNAPSHOT; |
5424 | 33f00271 | balrog | if (!cache)
|
5425 | 33f00271 | balrog | bdrv_flags |= BDRV_O_DIRECT; |
5426 | 1e72d3b7 | aurel32 | if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) { |
5427 | e4bcb14c | ths | fprintf(stderr, "qemu: could not open disk image %s\n",
|
5428 | e4bcb14c | ths | file); |
5429 | e4bcb14c | ths | return -1; |
5430 | e4bcb14c | ths | } |
5431 | e4bcb14c | ths | return 0; |
5432 | e4bcb14c | ths | } |
5433 | e4bcb14c | ths | |
5434 | 330d0414 | bellard | /***********************************************************/
|
5435 | a594cfbf | bellard | /* USB devices */
|
5436 | a594cfbf | bellard | |
5437 | 0d92ed30 | pbrook | static USBPort *used_usb_ports;
|
5438 | 0d92ed30 | pbrook | static USBPort *free_usb_ports;
|
5439 | 0d92ed30 | pbrook | |
5440 | 0d92ed30 | pbrook | /* ??? Maybe change this to register a hub to keep track of the topology. */
|
5441 | 0d92ed30 | pbrook | void qemu_register_usb_port(USBPort *port, void *opaque, int index, |
5442 | 0d92ed30 | pbrook | usb_attachfn attach) |
5443 | 0d92ed30 | pbrook | { |
5444 | 0d92ed30 | pbrook | port->opaque = opaque; |
5445 | 0d92ed30 | pbrook | port->index = index; |
5446 | 0d92ed30 | pbrook | port->attach = attach; |
5447 | 0d92ed30 | pbrook | port->next = free_usb_ports; |
5448 | 0d92ed30 | pbrook | free_usb_ports = port; |
5449 | 0d92ed30 | pbrook | } |
5450 | 0d92ed30 | pbrook | |
5451 | a594cfbf | bellard | static int usb_device_add(const char *devname) |
5452 | a594cfbf | bellard | { |
5453 | a594cfbf | bellard | const char *p; |
5454 | a594cfbf | bellard | USBDevice *dev; |
5455 | 0d92ed30 | pbrook | USBPort *port; |
5456 | a594cfbf | bellard | |
5457 | 0d92ed30 | pbrook | if (!free_usb_ports)
|
5458 | a594cfbf | bellard | return -1; |
5459 | a594cfbf | bellard | |
5460 | a594cfbf | bellard | if (strstart(devname, "host:", &p)) { |
5461 | a594cfbf | bellard | dev = usb_host_device_open(p); |
5462 | a594cfbf | bellard | } else if (!strcmp(devname, "mouse")) { |
5463 | a594cfbf | bellard | dev = usb_mouse_init(); |
5464 | 09b26c5e | bellard | } else if (!strcmp(devname, "tablet")) { |
5465 | 47b2d338 | balrog | dev = usb_tablet_init(); |
5466 | 47b2d338 | balrog | } else if (!strcmp(devname, "keyboard")) { |
5467 | 47b2d338 | balrog | dev = usb_keyboard_init(); |
5468 | 2e5d83bb | pbrook | } else if (strstart(devname, "disk:", &p)) { |
5469 | 2e5d83bb | pbrook | dev = usb_msd_init(p); |
5470 | f6d2a316 | balrog | } else if (!strcmp(devname, "wacom-tablet")) { |
5471 | f6d2a316 | balrog | dev = usb_wacom_init(); |
5472 | a7954218 | balrog | } else if (strstart(devname, "serial:", &p)) { |
5473 | a7954218 | balrog | dev = usb_serial_init(p); |
5474 | 2e4d9fb1 | aurel32 | #ifdef CONFIG_BRLAPI
|
5475 | 2e4d9fb1 | aurel32 | } else if (!strcmp(devname, "braille")) { |
5476 | 2e4d9fb1 | aurel32 | dev = usb_baum_init(); |
5477 | 2e4d9fb1 | aurel32 | #endif
|
5478 | a594cfbf | bellard | } else {
|
5479 | a594cfbf | bellard | return -1; |
5480 | a594cfbf | bellard | } |
5481 | 0d92ed30 | pbrook | if (!dev)
|
5482 | 0d92ed30 | pbrook | return -1; |
5483 | 0d92ed30 | pbrook | |
5484 | 0d92ed30 | pbrook | /* Find a USB port to add the device to. */
|
5485 | 0d92ed30 | pbrook | port = free_usb_ports; |
5486 | 0d92ed30 | pbrook | if (!port->next) {
|
5487 | 0d92ed30 | pbrook | USBDevice *hub; |
5488 | 0d92ed30 | pbrook | |
5489 | 0d92ed30 | pbrook | /* Create a new hub and chain it on. */
|
5490 | 0d92ed30 | pbrook | free_usb_ports = NULL;
|
5491 | 0d92ed30 | pbrook | port->next = used_usb_ports; |
5492 | 0d92ed30 | pbrook | used_usb_ports = port; |
5493 | 0d92ed30 | pbrook | |
5494 | 0d92ed30 | pbrook | hub = usb_hub_init(VM_USB_HUB_SIZE); |
5495 | 0d92ed30 | pbrook | usb_attach(port, hub); |
5496 | 0d92ed30 | pbrook | port = free_usb_ports; |
5497 | 0d92ed30 | pbrook | } |
5498 | 0d92ed30 | pbrook | |
5499 | 0d92ed30 | pbrook | free_usb_ports = port->next; |
5500 | 0d92ed30 | pbrook | port->next = used_usb_ports; |
5501 | 0d92ed30 | pbrook | used_usb_ports = port; |
5502 | 0d92ed30 | pbrook | usb_attach(port, dev); |
5503 | a594cfbf | bellard | return 0; |
5504 | a594cfbf | bellard | } |
5505 | a594cfbf | bellard | |
5506 | a594cfbf | bellard | static int usb_device_del(const char *devname) |
5507 | a594cfbf | bellard | { |
5508 | 0d92ed30 | pbrook | USBPort *port; |
5509 | 0d92ed30 | pbrook | USBPort **lastp; |
5510 | 059809e4 | bellard | USBDevice *dev; |
5511 | 0d92ed30 | pbrook | int bus_num, addr;
|
5512 | a594cfbf | bellard | const char *p; |
5513 | a594cfbf | bellard | |
5514 | 0d92ed30 | pbrook | if (!used_usb_ports)
|
5515 | a594cfbf | bellard | return -1; |
5516 | a594cfbf | bellard | |
5517 | a594cfbf | bellard | p = strchr(devname, '.');
|
5518 | 5fafdf24 | ths | if (!p)
|
5519 | a594cfbf | bellard | return -1; |
5520 | a594cfbf | bellard | bus_num = strtoul(devname, NULL, 0); |
5521 | a594cfbf | bellard | addr = strtoul(p + 1, NULL, 0); |
5522 | a594cfbf | bellard | if (bus_num != 0) |
5523 | a594cfbf | bellard | return -1; |
5524 | 0d92ed30 | pbrook | |
5525 | 0d92ed30 | pbrook | lastp = &used_usb_ports; |
5526 | 0d92ed30 | pbrook | port = used_usb_ports; |
5527 | 0d92ed30 | pbrook | while (port && port->dev->addr != addr) {
|
5528 | 0d92ed30 | pbrook | lastp = &port->next; |
5529 | 0d92ed30 | pbrook | port = port->next; |
5530 | a594cfbf | bellard | } |
5531 | 0d92ed30 | pbrook | |
5532 | 0d92ed30 | pbrook | if (!port)
|
5533 | a594cfbf | bellard | return -1; |
5534 | 0d92ed30 | pbrook | |
5535 | 059809e4 | bellard | dev = port->dev; |
5536 | 0d92ed30 | pbrook | *lastp = port->next; |
5537 | 0d92ed30 | pbrook | usb_attach(port, NULL);
|
5538 | 059809e4 | bellard | dev->handle_destroy(dev); |
5539 | 0d92ed30 | pbrook | port->next = free_usb_ports; |
5540 | 0d92ed30 | pbrook | free_usb_ports = port; |
5541 | a594cfbf | bellard | return 0; |
5542 | a594cfbf | bellard | } |
5543 | a594cfbf | bellard | |
5544 | a594cfbf | bellard | void do_usb_add(const char *devname) |
5545 | a594cfbf | bellard | { |
5546 | a594cfbf | bellard | int ret;
|
5547 | a594cfbf | bellard | ret = usb_device_add(devname); |
5548 | 5fafdf24 | ths | if (ret < 0) |
5549 | a594cfbf | bellard | term_printf("Could not add USB device '%s'\n", devname);
|
5550 | a594cfbf | bellard | } |
5551 | a594cfbf | bellard | |
5552 | a594cfbf | bellard | void do_usb_del(const char *devname) |
5553 | a594cfbf | bellard | { |
5554 | a594cfbf | bellard | int ret;
|
5555 | a594cfbf | bellard | ret = usb_device_del(devname); |
5556 | 5fafdf24 | ths | if (ret < 0) |
5557 | a594cfbf | bellard | term_printf("Could not remove USB device '%s'\n", devname);
|
5558 | a594cfbf | bellard | } |
5559 | a594cfbf | bellard | |
5560 | a594cfbf | bellard | void usb_info(void) |
5561 | a594cfbf | bellard | { |
5562 | a594cfbf | bellard | USBDevice *dev; |
5563 | 0d92ed30 | pbrook | USBPort *port; |
5564 | a594cfbf | bellard | const char *speed_str; |
5565 | a594cfbf | bellard | |
5566 | 0d92ed30 | pbrook | if (!usb_enabled) {
|
5567 | a594cfbf | bellard | term_printf("USB support not enabled\n");
|
5568 | a594cfbf | bellard | return;
|
5569 | a594cfbf | bellard | } |
5570 | a594cfbf | bellard | |
5571 | 0d92ed30 | pbrook | for (port = used_usb_ports; port; port = port->next) {
|
5572 | 0d92ed30 | pbrook | dev = port->dev; |
5573 | 0d92ed30 | pbrook | if (!dev)
|
5574 | 0d92ed30 | pbrook | continue;
|
5575 | 0d92ed30 | pbrook | switch(dev->speed) {
|
5576 | 5fafdf24 | ths | case USB_SPEED_LOW:
|
5577 | 5fafdf24 | ths | speed_str = "1.5";
|
5578 | 0d92ed30 | pbrook | break;
|
5579 | 5fafdf24 | ths | case USB_SPEED_FULL:
|
5580 | 5fafdf24 | ths | speed_str = "12";
|
5581 | 0d92ed30 | pbrook | break;
|
5582 | 5fafdf24 | ths | case USB_SPEED_HIGH:
|
5583 | 5fafdf24 | ths | speed_str = "480";
|
5584 | 0d92ed30 | pbrook | break;
|
5585 | 0d92ed30 | pbrook | default:
|
5586 | 5fafdf24 | ths | speed_str = "?";
|
5587 | 0d92ed30 | pbrook | break;
|
5588 | a594cfbf | bellard | } |
5589 | 5fafdf24 | ths | term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
|
5590 | 1f6e24e7 | bellard | 0, dev->addr, speed_str, dev->devname);
|
5591 | a594cfbf | bellard | } |
5592 | a594cfbf | bellard | } |
5593 | a594cfbf | bellard | |
5594 | f7cce898 | bellard | /***********************************************************/
|
5595 | 201a51fc | balrog | /* PCMCIA/Cardbus */
|
5596 | 201a51fc | balrog | |
5597 | 201a51fc | balrog | static struct pcmcia_socket_entry_s { |
5598 | 201a51fc | balrog | struct pcmcia_socket_s *socket;
|
5599 | 201a51fc | balrog | struct pcmcia_socket_entry_s *next;
|
5600 | 201a51fc | balrog | } *pcmcia_sockets = 0;
|
5601 | 201a51fc | balrog | |
5602 | 201a51fc | balrog | void pcmcia_socket_register(struct pcmcia_socket_s *socket) |
5603 | 201a51fc | balrog | { |
5604 | 201a51fc | balrog | struct pcmcia_socket_entry_s *entry;
|
5605 | 201a51fc | balrog | |
5606 | 201a51fc | balrog | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); |
5607 | 201a51fc | balrog | entry->socket = socket; |
5608 | 201a51fc | balrog | entry->next = pcmcia_sockets; |
5609 | 201a51fc | balrog | pcmcia_sockets = entry; |
5610 | 201a51fc | balrog | } |
5611 | 201a51fc | balrog | |
5612 | 201a51fc | balrog | void pcmcia_socket_unregister(struct pcmcia_socket_s *socket) |
5613 | 201a51fc | balrog | { |
5614 | 201a51fc | balrog | struct pcmcia_socket_entry_s *entry, **ptr;
|
5615 | 201a51fc | balrog | |
5616 | 201a51fc | balrog | ptr = &pcmcia_sockets; |
5617 | 201a51fc | balrog | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
|
5618 | 201a51fc | balrog | if (entry->socket == socket) {
|
5619 | 201a51fc | balrog | *ptr = entry->next; |
5620 | 201a51fc | balrog | qemu_free(entry); |
5621 | 201a51fc | balrog | } |
5622 | 201a51fc | balrog | } |
5623 | 201a51fc | balrog | |
5624 | 201a51fc | balrog | void pcmcia_info(void) |
5625 | 201a51fc | balrog | { |
5626 | 201a51fc | balrog | struct pcmcia_socket_entry_s *iter;
|
5627 | 201a51fc | balrog | if (!pcmcia_sockets)
|
5628 | 201a51fc | balrog | term_printf("No PCMCIA sockets\n");
|
5629 | 201a51fc | balrog | |
5630 | 201a51fc | balrog | for (iter = pcmcia_sockets; iter; iter = iter->next)
|
5631 | 201a51fc | balrog | term_printf("%s: %s\n", iter->socket->slot_string,
|
5632 | 201a51fc | balrog | iter->socket->attached ? iter->socket->card_string : |
5633 | 201a51fc | balrog | "Empty");
|
5634 | 201a51fc | balrog | } |
5635 | 201a51fc | balrog | |
5636 | 201a51fc | balrog | /***********************************************************/
|
5637 | 2ff89790 | ths | /* dumb display */
|
5638 | 2ff89790 | ths | |
5639 | 2ff89790 | ths | static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
5640 | 2ff89790 | ths | { |
5641 | 2ff89790 | ths | } |
5642 | 2ff89790 | ths | |
5643 | 2ff89790 | ths | static void dumb_resize(DisplayState *ds, int w, int h) |
5644 | 2ff89790 | ths | { |
5645 | 2ff89790 | ths | } |
5646 | 2ff89790 | ths | |
5647 | 2ff89790 | ths | static void dumb_refresh(DisplayState *ds) |
5648 | 2ff89790 | ths | { |
5649 | 2ff89790 | ths | #if defined(CONFIG_SDL)
|
5650 | 2ff89790 | ths | vga_hw_update(); |
5651 | 2ff89790 | ths | #endif
|
5652 | 2ff89790 | ths | } |
5653 | 2ff89790 | ths | |
5654 | 2ff89790 | ths | static void dumb_display_init(DisplayState *ds) |
5655 | 2ff89790 | ths | { |
5656 | 2ff89790 | ths | ds->data = NULL;
|
5657 | 2ff89790 | ths | ds->linesize = 0;
|
5658 | 2ff89790 | ths | ds->depth = 0;
|
5659 | 2ff89790 | ths | ds->dpy_update = dumb_update; |
5660 | 2ff89790 | ths | ds->dpy_resize = dumb_resize; |
5661 | 2ff89790 | ths | ds->dpy_refresh = dumb_refresh; |
5662 | 2ff89790 | ths | } |
5663 | 2ff89790 | ths | |
5664 | 2ff89790 | ths | /***********************************************************/
|
5665 | 8a7ddc38 | bellard | /* I/O handling */
|
5666 | 0824d6fc | bellard | |
5667 | c4b1fcc0 | bellard | #define MAX_IO_HANDLERS 64 |
5668 | c4b1fcc0 | bellard | |
5669 | c4b1fcc0 | bellard | typedef struct IOHandlerRecord { |
5670 | c4b1fcc0 | bellard | int fd;
|
5671 | 7c9d8e07 | bellard | IOCanRWHandler *fd_read_poll; |
5672 | 7c9d8e07 | bellard | IOHandler *fd_read; |
5673 | 7c9d8e07 | bellard | IOHandler *fd_write; |
5674 | cafffd40 | ths | int deleted;
|
5675 | c4b1fcc0 | bellard | void *opaque;
|
5676 | c4b1fcc0 | bellard | /* temporary data */
|
5677 | c4b1fcc0 | bellard | struct pollfd *ufd;
|
5678 | 8a7ddc38 | bellard | struct IOHandlerRecord *next;
|
5679 | c4b1fcc0 | bellard | } IOHandlerRecord; |
5680 | c4b1fcc0 | bellard | |
5681 | 8a7ddc38 | bellard | static IOHandlerRecord *first_io_handler;
|
5682 | c4b1fcc0 | bellard | |
5683 | 7c9d8e07 | bellard | /* XXX: fd_read_poll should be suppressed, but an API change is
|
5684 | 7c9d8e07 | bellard | necessary in the character devices to suppress fd_can_read(). */
|
5685 | 5fafdf24 | ths | int qemu_set_fd_handler2(int fd, |
5686 | 5fafdf24 | ths | IOCanRWHandler *fd_read_poll, |
5687 | 5fafdf24 | ths | IOHandler *fd_read, |
5688 | 5fafdf24 | ths | IOHandler *fd_write, |
5689 | 7c9d8e07 | bellard | void *opaque)
|
5690 | c4b1fcc0 | bellard | { |
5691 | 7c9d8e07 | bellard | IOHandlerRecord **pioh, *ioh; |
5692 | c4b1fcc0 | bellard | |
5693 | 7c9d8e07 | bellard | if (!fd_read && !fd_write) {
|
5694 | 7c9d8e07 | bellard | pioh = &first_io_handler; |
5695 | 7c9d8e07 | bellard | for(;;) {
|
5696 | 7c9d8e07 | bellard | ioh = *pioh; |
5697 | 7c9d8e07 | bellard | if (ioh == NULL) |
5698 | 7c9d8e07 | bellard | break;
|
5699 | 7c9d8e07 | bellard | if (ioh->fd == fd) {
|
5700 | cafffd40 | ths | ioh->deleted = 1;
|
5701 | 7c9d8e07 | bellard | break;
|
5702 | 7c9d8e07 | bellard | } |
5703 | 7c9d8e07 | bellard | pioh = &ioh->next; |
5704 | 7c9d8e07 | bellard | } |
5705 | 7c9d8e07 | bellard | } else {
|
5706 | 7c9d8e07 | bellard | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
5707 | 7c9d8e07 | bellard | if (ioh->fd == fd)
|
5708 | 7c9d8e07 | bellard | goto found;
|
5709 | 7c9d8e07 | bellard | } |
5710 | 7c9d8e07 | bellard | ioh = qemu_mallocz(sizeof(IOHandlerRecord));
|
5711 | 7c9d8e07 | bellard | if (!ioh)
|
5712 | 7c9d8e07 | bellard | return -1; |
5713 | 7c9d8e07 | bellard | ioh->next = first_io_handler; |
5714 | 7c9d8e07 | bellard | first_io_handler = ioh; |
5715 | 7c9d8e07 | bellard | found:
|
5716 | 7c9d8e07 | bellard | ioh->fd = fd; |
5717 | 7c9d8e07 | bellard | ioh->fd_read_poll = fd_read_poll; |
5718 | 7c9d8e07 | bellard | ioh->fd_read = fd_read; |
5719 | 7c9d8e07 | bellard | ioh->fd_write = fd_write; |
5720 | 7c9d8e07 | bellard | ioh->opaque = opaque; |
5721 | cafffd40 | ths | ioh->deleted = 0;
|
5722 | 7c9d8e07 | bellard | } |
5723 | c4b1fcc0 | bellard | return 0; |
5724 | c4b1fcc0 | bellard | } |
5725 | c4b1fcc0 | bellard | |
5726 | 5fafdf24 | ths | int qemu_set_fd_handler(int fd, |
5727 | 5fafdf24 | ths | IOHandler *fd_read, |
5728 | 5fafdf24 | ths | IOHandler *fd_write, |
5729 | 7c9d8e07 | bellard | void *opaque)
|
5730 | 8a7ddc38 | bellard | { |
5731 | 7c9d8e07 | bellard | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
5732 | 8a7ddc38 | bellard | } |
5733 | 8a7ddc38 | bellard | |
5734 | 8a7ddc38 | bellard | /***********************************************************/
|
5735 | f331110f | bellard | /* Polling handling */
|
5736 | f331110f | bellard | |
5737 | f331110f | bellard | typedef struct PollingEntry { |
5738 | f331110f | bellard | PollingFunc *func; |
5739 | f331110f | bellard | void *opaque;
|
5740 | f331110f | bellard | struct PollingEntry *next;
|
5741 | f331110f | bellard | } PollingEntry; |
5742 | f331110f | bellard | |
5743 | f331110f | bellard | static PollingEntry *first_polling_entry;
|
5744 | f331110f | bellard | |
5745 | f331110f | bellard | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
5746 | f331110f | bellard | { |
5747 | f331110f | bellard | PollingEntry **ppe, *pe; |
5748 | f331110f | bellard | pe = qemu_mallocz(sizeof(PollingEntry));
|
5749 | f331110f | bellard | if (!pe)
|
5750 | f331110f | bellard | return -1; |
5751 | f331110f | bellard | pe->func = func; |
5752 | f331110f | bellard | pe->opaque = opaque; |
5753 | f331110f | bellard | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
5754 | f331110f | bellard | *ppe = pe; |
5755 | f331110f | bellard | return 0; |
5756 | f331110f | bellard | } |
5757 | f331110f | bellard | |
5758 | f331110f | bellard | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
5759 | f331110f | bellard | { |
5760 | f331110f | bellard | PollingEntry **ppe, *pe; |
5761 | f331110f | bellard | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
5762 | f331110f | bellard | pe = *ppe; |
5763 | f331110f | bellard | if (pe->func == func && pe->opaque == opaque) {
|
5764 | f331110f | bellard | *ppe = pe->next; |
5765 | f331110f | bellard | qemu_free(pe); |
5766 | f331110f | bellard | break;
|
5767 | f331110f | bellard | } |
5768 | f331110f | bellard | } |
5769 | f331110f | bellard | } |
5770 | f331110f | bellard | |
5771 | a18e524a | bellard | #ifdef _WIN32
|
5772 | a18e524a | bellard | /***********************************************************/
|
5773 | a18e524a | bellard | /* Wait objects support */
|
5774 | a18e524a | bellard | typedef struct WaitObjects { |
5775 | a18e524a | bellard | int num;
|
5776 | a18e524a | bellard | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
|
5777 | a18e524a | bellard | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
|
5778 | a18e524a | bellard | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
5779 | a18e524a | bellard | } WaitObjects; |
5780 | a18e524a | bellard | |
5781 | a18e524a | bellard | static WaitObjects wait_objects = {0}; |
5782 | 3b46e624 | ths | |
5783 | a18e524a | bellard | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
5784 | a18e524a | bellard | { |
5785 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
5786 | a18e524a | bellard | |
5787 | a18e524a | bellard | if (w->num >= MAXIMUM_WAIT_OBJECTS)
|
5788 | a18e524a | bellard | return -1; |
5789 | a18e524a | bellard | w->events[w->num] = handle; |
5790 | a18e524a | bellard | w->func[w->num] = func; |
5791 | a18e524a | bellard | w->opaque[w->num] = opaque; |
5792 | a18e524a | bellard | w->num++; |
5793 | a18e524a | bellard | return 0; |
5794 | a18e524a | bellard | } |
5795 | a18e524a | bellard | |
5796 | a18e524a | bellard | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
5797 | a18e524a | bellard | { |
5798 | a18e524a | bellard | int i, found;
|
5799 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
5800 | a18e524a | bellard | |
5801 | a18e524a | bellard | found = 0;
|
5802 | a18e524a | bellard | for (i = 0; i < w->num; i++) { |
5803 | a18e524a | bellard | if (w->events[i] == handle)
|
5804 | a18e524a | bellard | found = 1;
|
5805 | a18e524a | bellard | if (found) {
|
5806 | a18e524a | bellard | w->events[i] = w->events[i + 1];
|
5807 | a18e524a | bellard | w->func[i] = w->func[i + 1];
|
5808 | a18e524a | bellard | w->opaque[i] = w->opaque[i + 1];
|
5809 | 3b46e624 | ths | } |
5810 | a18e524a | bellard | } |
5811 | a18e524a | bellard | if (found)
|
5812 | a18e524a | bellard | w->num--; |
5813 | a18e524a | bellard | } |
5814 | a18e524a | bellard | #endif
|
5815 | a18e524a | bellard | |
5816 | f331110f | bellard | /***********************************************************/
|
5817 | 8a7ddc38 | bellard | /* savevm/loadvm support */
|
5818 | 8a7ddc38 | bellard | |
5819 | faea38e7 | bellard | #define IO_BUF_SIZE 32768 |
5820 | faea38e7 | bellard | |
5821 | faea38e7 | bellard | struct QEMUFile {
|
5822 | faea38e7 | bellard | FILE *outfile; |
5823 | faea38e7 | bellard | BlockDriverState *bs; |
5824 | faea38e7 | bellard | int is_file;
|
5825 | faea38e7 | bellard | int is_writable;
|
5826 | faea38e7 | bellard | int64_t base_offset; |
5827 | faea38e7 | bellard | int64_t buf_offset; /* start of buffer when writing, end of buffer
|
5828 | faea38e7 | bellard | when reading */
|
5829 | faea38e7 | bellard | int buf_index;
|
5830 | faea38e7 | bellard | int buf_size; /* 0 when writing */ |
5831 | faea38e7 | bellard | uint8_t buf[IO_BUF_SIZE]; |
5832 | faea38e7 | bellard | }; |
5833 | faea38e7 | bellard | |
5834 | faea38e7 | bellard | QEMUFile *qemu_fopen(const char *filename, const char *mode) |
5835 | faea38e7 | bellard | { |
5836 | faea38e7 | bellard | QEMUFile *f; |
5837 | faea38e7 | bellard | |
5838 | faea38e7 | bellard | f = qemu_mallocz(sizeof(QEMUFile));
|
5839 | faea38e7 | bellard | if (!f)
|
5840 | faea38e7 | bellard | return NULL; |
5841 | faea38e7 | bellard | if (!strcmp(mode, "wb")) { |
5842 | faea38e7 | bellard | f->is_writable = 1;
|
5843 | faea38e7 | bellard | } else if (!strcmp(mode, "rb")) { |
5844 | faea38e7 | bellard | f->is_writable = 0;
|
5845 | faea38e7 | bellard | } else {
|
5846 | faea38e7 | bellard | goto fail;
|
5847 | faea38e7 | bellard | } |
5848 | faea38e7 | bellard | f->outfile = fopen(filename, mode); |
5849 | faea38e7 | bellard | if (!f->outfile)
|
5850 | faea38e7 | bellard | goto fail;
|
5851 | faea38e7 | bellard | f->is_file = 1;
|
5852 | faea38e7 | bellard | return f;
|
5853 | faea38e7 | bellard | fail:
|
5854 | faea38e7 | bellard | if (f->outfile)
|
5855 | faea38e7 | bellard | fclose(f->outfile); |
5856 | faea38e7 | bellard | qemu_free(f); |
5857 | faea38e7 | bellard | return NULL; |
5858 | faea38e7 | bellard | } |
5859 | faea38e7 | bellard | |
5860 | 9596ebb7 | pbrook | static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable) |
5861 | faea38e7 | bellard | { |
5862 | faea38e7 | bellard | QEMUFile *f; |
5863 | faea38e7 | bellard | |
5864 | faea38e7 | bellard | f = qemu_mallocz(sizeof(QEMUFile));
|
5865 | faea38e7 | bellard | if (!f)
|
5866 | faea38e7 | bellard | return NULL; |
5867 | faea38e7 | bellard | f->is_file = 0;
|
5868 | faea38e7 | bellard | f->bs = bs; |
5869 | faea38e7 | bellard | f->is_writable = is_writable; |
5870 | faea38e7 | bellard | f->base_offset = offset; |
5871 | faea38e7 | bellard | return f;
|
5872 | faea38e7 | bellard | } |
5873 | faea38e7 | bellard | |
5874 | faea38e7 | bellard | void qemu_fflush(QEMUFile *f)
|
5875 | faea38e7 | bellard | { |
5876 | faea38e7 | bellard | if (!f->is_writable)
|
5877 | faea38e7 | bellard | return;
|
5878 | faea38e7 | bellard | if (f->buf_index > 0) { |
5879 | faea38e7 | bellard | if (f->is_file) {
|
5880 | faea38e7 | bellard | fseek(f->outfile, f->buf_offset, SEEK_SET); |
5881 | faea38e7 | bellard | fwrite(f->buf, 1, f->buf_index, f->outfile);
|
5882 | faea38e7 | bellard | } else {
|
5883 | 5fafdf24 | ths | bdrv_pwrite(f->bs, f->base_offset + f->buf_offset, |
5884 | faea38e7 | bellard | f->buf, f->buf_index); |
5885 | faea38e7 | bellard | } |
5886 | faea38e7 | bellard | f->buf_offset += f->buf_index; |
5887 | faea38e7 | bellard | f->buf_index = 0;
|
5888 | faea38e7 | bellard | } |
5889 | faea38e7 | bellard | } |
5890 | faea38e7 | bellard | |
5891 | faea38e7 | bellard | static void qemu_fill_buffer(QEMUFile *f) |
5892 | faea38e7 | bellard | { |
5893 | faea38e7 | bellard | int len;
|
5894 | faea38e7 | bellard | |
5895 | faea38e7 | bellard | if (f->is_writable)
|
5896 | faea38e7 | bellard | return;
|
5897 | faea38e7 | bellard | if (f->is_file) {
|
5898 | faea38e7 | bellard | fseek(f->outfile, f->buf_offset, SEEK_SET); |
5899 | faea38e7 | bellard | len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile);
|
5900 | faea38e7 | bellard | if (len < 0) |
5901 | faea38e7 | bellard | len = 0;
|
5902 | faea38e7 | bellard | } else {
|
5903 | 5fafdf24 | ths | len = bdrv_pread(f->bs, f->base_offset + f->buf_offset, |
5904 | faea38e7 | bellard | f->buf, IO_BUF_SIZE); |
5905 | faea38e7 | bellard | if (len < 0) |
5906 | faea38e7 | bellard | len = 0;
|
5907 | faea38e7 | bellard | } |
5908 | faea38e7 | bellard | f->buf_index = 0;
|
5909 | faea38e7 | bellard | f->buf_size = len; |
5910 | faea38e7 | bellard | f->buf_offset += len; |
5911 | faea38e7 | bellard | } |
5912 | faea38e7 | bellard | |
5913 | faea38e7 | bellard | void qemu_fclose(QEMUFile *f)
|
5914 | faea38e7 | bellard | { |
5915 | faea38e7 | bellard | if (f->is_writable)
|
5916 | faea38e7 | bellard | qemu_fflush(f); |
5917 | faea38e7 | bellard | if (f->is_file) {
|
5918 | faea38e7 | bellard | fclose(f->outfile); |
5919 | faea38e7 | bellard | } |
5920 | faea38e7 | bellard | qemu_free(f); |
5921 | faea38e7 | bellard | } |
5922 | faea38e7 | bellard | |
5923 | 8a7ddc38 | bellard | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
5924 | b4608c04 | bellard | { |
5925 | faea38e7 | bellard | int l;
|
5926 | faea38e7 | bellard | while (size > 0) { |
5927 | faea38e7 | bellard | l = IO_BUF_SIZE - f->buf_index; |
5928 | faea38e7 | bellard | if (l > size)
|
5929 | faea38e7 | bellard | l = size; |
5930 | faea38e7 | bellard | memcpy(f->buf + f->buf_index, buf, l); |
5931 | faea38e7 | bellard | f->buf_index += l; |
5932 | faea38e7 | bellard | buf += l; |
5933 | faea38e7 | bellard | size -= l; |
5934 | faea38e7 | bellard | if (f->buf_index >= IO_BUF_SIZE)
|
5935 | faea38e7 | bellard | qemu_fflush(f); |
5936 | faea38e7 | bellard | } |
5937 | b4608c04 | bellard | } |
5938 | b4608c04 | bellard | |
5939 | 8a7ddc38 | bellard | void qemu_put_byte(QEMUFile *f, int v) |
5940 | b4608c04 | bellard | { |
5941 | faea38e7 | bellard | f->buf[f->buf_index++] = v; |
5942 | faea38e7 | bellard | if (f->buf_index >= IO_BUF_SIZE)
|
5943 | faea38e7 | bellard | qemu_fflush(f); |
5944 | faea38e7 | bellard | } |
5945 | faea38e7 | bellard | |
5946 | faea38e7 | bellard | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1) |
5947 | faea38e7 | bellard | { |
5948 | faea38e7 | bellard | int size, l;
|
5949 | faea38e7 | bellard | |
5950 | faea38e7 | bellard | size = size1; |
5951 | faea38e7 | bellard | while (size > 0) { |
5952 | faea38e7 | bellard | l = f->buf_size - f->buf_index; |
5953 | faea38e7 | bellard | if (l == 0) { |
5954 | faea38e7 | bellard | qemu_fill_buffer(f); |
5955 | faea38e7 | bellard | l = f->buf_size - f->buf_index; |
5956 | faea38e7 | bellard | if (l == 0) |
5957 | faea38e7 | bellard | break;
|
5958 | faea38e7 | bellard | } |
5959 | faea38e7 | bellard | if (l > size)
|
5960 | faea38e7 | bellard | l = size; |
5961 | faea38e7 | bellard | memcpy(buf, f->buf + f->buf_index, l); |
5962 | faea38e7 | bellard | f->buf_index += l; |
5963 | faea38e7 | bellard | buf += l; |
5964 | faea38e7 | bellard | size -= l; |
5965 | faea38e7 | bellard | } |
5966 | faea38e7 | bellard | return size1 - size;
|
5967 | faea38e7 | bellard | } |
5968 | faea38e7 | bellard | |
5969 | faea38e7 | bellard | int qemu_get_byte(QEMUFile *f)
|
5970 | faea38e7 | bellard | { |
5971 | faea38e7 | bellard | if (f->buf_index >= f->buf_size) {
|
5972 | faea38e7 | bellard | qemu_fill_buffer(f); |
5973 | faea38e7 | bellard | if (f->buf_index >= f->buf_size)
|
5974 | faea38e7 | bellard | return 0; |
5975 | faea38e7 | bellard | } |
5976 | faea38e7 | bellard | return f->buf[f->buf_index++];
|
5977 | faea38e7 | bellard | } |
5978 | faea38e7 | bellard | |
5979 | faea38e7 | bellard | int64_t qemu_ftell(QEMUFile *f) |
5980 | faea38e7 | bellard | { |
5981 | faea38e7 | bellard | return f->buf_offset - f->buf_size + f->buf_index;
|
5982 | faea38e7 | bellard | } |
5983 | faea38e7 | bellard | |
5984 | faea38e7 | bellard | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
|
5985 | faea38e7 | bellard | { |
5986 | faea38e7 | bellard | if (whence == SEEK_SET) {
|
5987 | faea38e7 | bellard | /* nothing to do */
|
5988 | faea38e7 | bellard | } else if (whence == SEEK_CUR) { |
5989 | faea38e7 | bellard | pos += qemu_ftell(f); |
5990 | faea38e7 | bellard | } else {
|
5991 | faea38e7 | bellard | /* SEEK_END not supported */
|
5992 | faea38e7 | bellard | return -1; |
5993 | faea38e7 | bellard | } |
5994 | faea38e7 | bellard | if (f->is_writable) {
|
5995 | faea38e7 | bellard | qemu_fflush(f); |
5996 | faea38e7 | bellard | f->buf_offset = pos; |
5997 | faea38e7 | bellard | } else {
|
5998 | faea38e7 | bellard | f->buf_offset = pos; |
5999 | faea38e7 | bellard | f->buf_index = 0;
|
6000 | faea38e7 | bellard | f->buf_size = 0;
|
6001 | faea38e7 | bellard | } |
6002 | faea38e7 | bellard | return pos;
|
6003 | 8a7ddc38 | bellard | } |
6004 | 8a7ddc38 | bellard | |
6005 | 8a7ddc38 | bellard | void qemu_put_be16(QEMUFile *f, unsigned int v) |
6006 | 8a7ddc38 | bellard | { |
6007 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 8);
|
6008 | 8a7ddc38 | bellard | qemu_put_byte(f, v); |
6009 | 8a7ddc38 | bellard | } |
6010 | 8a7ddc38 | bellard | |
6011 | 8a7ddc38 | bellard | void qemu_put_be32(QEMUFile *f, unsigned int v) |
6012 | 8a7ddc38 | bellard | { |
6013 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 24);
|
6014 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 16);
|
6015 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 8);
|
6016 | 8a7ddc38 | bellard | qemu_put_byte(f, v); |
6017 | 8a7ddc38 | bellard | } |
6018 | 8a7ddc38 | bellard | |
6019 | 8a7ddc38 | bellard | void qemu_put_be64(QEMUFile *f, uint64_t v)
|
6020 | 8a7ddc38 | bellard | { |
6021 | 8a7ddc38 | bellard | qemu_put_be32(f, v >> 32);
|
6022 | 8a7ddc38 | bellard | qemu_put_be32(f, v); |
6023 | 8a7ddc38 | bellard | } |
6024 | 8a7ddc38 | bellard | |
6025 | 8a7ddc38 | bellard | unsigned int qemu_get_be16(QEMUFile *f) |
6026 | 8a7ddc38 | bellard | { |
6027 | 8a7ddc38 | bellard | unsigned int v; |
6028 | 8a7ddc38 | bellard | v = qemu_get_byte(f) << 8;
|
6029 | 8a7ddc38 | bellard | v |= qemu_get_byte(f); |
6030 | 8a7ddc38 | bellard | return v;
|
6031 | 8a7ddc38 | bellard | } |
6032 | 8a7ddc38 | bellard | |
6033 | 8a7ddc38 | bellard | unsigned int qemu_get_be32(QEMUFile *f) |
6034 | 8a7ddc38 | bellard | { |
6035 | 8a7ddc38 | bellard | unsigned int v; |
6036 | 8a7ddc38 | bellard | v = qemu_get_byte(f) << 24;
|
6037 | 8a7ddc38 | bellard | v |= qemu_get_byte(f) << 16;
|
6038 | 8a7ddc38 | bellard | v |= qemu_get_byte(f) << 8;
|
6039 | 8a7ddc38 | bellard | v |= qemu_get_byte(f); |
6040 | 8a7ddc38 | bellard | return v;
|
6041 | 8a7ddc38 | bellard | } |
6042 | 8a7ddc38 | bellard | |
6043 | 8a7ddc38 | bellard | uint64_t qemu_get_be64(QEMUFile *f) |
6044 | 8a7ddc38 | bellard | { |
6045 | 8a7ddc38 | bellard | uint64_t v; |
6046 | 8a7ddc38 | bellard | v = (uint64_t)qemu_get_be32(f) << 32;
|
6047 | 8a7ddc38 | bellard | v |= qemu_get_be32(f); |
6048 | 8a7ddc38 | bellard | return v;
|
6049 | 8a7ddc38 | bellard | } |
6050 | 8a7ddc38 | bellard | |
6051 | 8a7ddc38 | bellard | typedef struct SaveStateEntry { |
6052 | 8a7ddc38 | bellard | char idstr[256]; |
6053 | 8a7ddc38 | bellard | int instance_id;
|
6054 | 8a7ddc38 | bellard | int version_id;
|
6055 | 8a7ddc38 | bellard | SaveStateHandler *save_state; |
6056 | 8a7ddc38 | bellard | LoadStateHandler *load_state; |
6057 | 8a7ddc38 | bellard | void *opaque;
|
6058 | 8a7ddc38 | bellard | struct SaveStateEntry *next;
|
6059 | 8a7ddc38 | bellard | } SaveStateEntry; |
6060 | b4608c04 | bellard | |
6061 | 8a7ddc38 | bellard | static SaveStateEntry *first_se;
|
6062 | 8a7ddc38 | bellard | |
6063 | 18be5187 | pbrook | /* TODO: Individual devices generally have very little idea about the rest
|
6064 | 18be5187 | pbrook | of the system, so instance_id should be removed/replaced. */
|
6065 | 5fafdf24 | ths | int register_savevm(const char *idstr, |
6066 | 5fafdf24 | ths | int instance_id,
|
6067 | 8a7ddc38 | bellard | int version_id,
|
6068 | 8a7ddc38 | bellard | SaveStateHandler *save_state, |
6069 | 8a7ddc38 | bellard | LoadStateHandler *load_state, |
6070 | 8a7ddc38 | bellard | void *opaque)
|
6071 | 8a7ddc38 | bellard | { |
6072 | 8a7ddc38 | bellard | SaveStateEntry *se, **pse; |
6073 | 8a7ddc38 | bellard | |
6074 | 8a7ddc38 | bellard | se = qemu_malloc(sizeof(SaveStateEntry));
|
6075 | 8a7ddc38 | bellard | if (!se)
|
6076 | 8a7ddc38 | bellard | return -1; |
6077 | 8a7ddc38 | bellard | pstrcpy(se->idstr, sizeof(se->idstr), idstr);
|
6078 | 18be5187 | pbrook | se->instance_id = (instance_id == -1) ? 0 : instance_id; |
6079 | 8a7ddc38 | bellard | se->version_id = version_id; |
6080 | 8a7ddc38 | bellard | se->save_state = save_state; |
6081 | 8a7ddc38 | bellard | se->load_state = load_state; |
6082 | 8a7ddc38 | bellard | se->opaque = opaque; |
6083 | 8a7ddc38 | bellard | se->next = NULL;
|
6084 | 8a7ddc38 | bellard | |
6085 | 8a7ddc38 | bellard | /* add at the end of list */
|
6086 | 8a7ddc38 | bellard | pse = &first_se; |
6087 | 18be5187 | pbrook | while (*pse != NULL) { |
6088 | 18be5187 | pbrook | if (instance_id == -1 |
6089 | 18be5187 | pbrook | && strcmp(se->idstr, (*pse)->idstr) == 0
|
6090 | 18be5187 | pbrook | && se->instance_id <= (*pse)->instance_id) |
6091 | 18be5187 | pbrook | se->instance_id = (*pse)->instance_id + 1;
|
6092 | 8a7ddc38 | bellard | pse = &(*pse)->next; |
6093 | 18be5187 | pbrook | } |
6094 | 8a7ddc38 | bellard | *pse = se; |
6095 | 8a7ddc38 | bellard | return 0; |
6096 | 8a7ddc38 | bellard | } |
6097 | 8a7ddc38 | bellard | |
6098 | 8a7ddc38 | bellard | #define QEMU_VM_FILE_MAGIC 0x5145564d |
6099 | faea38e7 | bellard | #define QEMU_VM_FILE_VERSION 0x00000002 |
6100 | 8a7ddc38 | bellard | |
6101 | 9596ebb7 | pbrook | static int qemu_savevm_state(QEMUFile *f) |
6102 | 8a7ddc38 | bellard | { |
6103 | 8a7ddc38 | bellard | SaveStateEntry *se; |
6104 | faea38e7 | bellard | int len, ret;
|
6105 | faea38e7 | bellard | int64_t cur_pos, len_pos, total_len_pos; |
6106 | 313aa567 | bellard | |
6107 | 8a7ddc38 | bellard | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
6108 | 8a7ddc38 | bellard | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
6109 | faea38e7 | bellard | total_len_pos = qemu_ftell(f); |
6110 | faea38e7 | bellard | qemu_put_be64(f, 0); /* total size */ |
6111 | 8a7ddc38 | bellard | |
6112 | 8a7ddc38 | bellard | for(se = first_se; se != NULL; se = se->next) { |
6113 | d978c02c | aurel32 | if (se->save_state == NULL) |
6114 | d978c02c | aurel32 | /* this one has a loader only, for backwards compatibility */
|
6115 | d978c02c | aurel32 | continue;
|
6116 | d978c02c | aurel32 | |
6117 | 8a7ddc38 | bellard | /* ID string */
|
6118 | 8a7ddc38 | bellard | len = strlen(se->idstr); |
6119 | 8a7ddc38 | bellard | qemu_put_byte(f, len); |
6120 | ffe8ab83 | ths | qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
6121 | 8a7ddc38 | bellard | |
6122 | 8a7ddc38 | bellard | qemu_put_be32(f, se->instance_id); |
6123 | 8a7ddc38 | bellard | qemu_put_be32(f, se->version_id); |
6124 | 8a7ddc38 | bellard | |
6125 | 8a7ddc38 | bellard | /* record size: filled later */
|
6126 | faea38e7 | bellard | len_pos = qemu_ftell(f); |
6127 | 8a7ddc38 | bellard | qemu_put_be32(f, 0);
|
6128 | 8a7ddc38 | bellard | se->save_state(f, se->opaque); |
6129 | 8a7ddc38 | bellard | |
6130 | 8a7ddc38 | bellard | /* fill record size */
|
6131 | faea38e7 | bellard | cur_pos = qemu_ftell(f); |
6132 | faea38e7 | bellard | len = cur_pos - len_pos - 4;
|
6133 | faea38e7 | bellard | qemu_fseek(f, len_pos, SEEK_SET); |
6134 | 8a7ddc38 | bellard | qemu_put_be32(f, len); |
6135 | faea38e7 | bellard | qemu_fseek(f, cur_pos, SEEK_SET); |
6136 | 8a7ddc38 | bellard | } |
6137 | faea38e7 | bellard | cur_pos = qemu_ftell(f); |
6138 | faea38e7 | bellard | qemu_fseek(f, total_len_pos, SEEK_SET); |
6139 | faea38e7 | bellard | qemu_put_be64(f, cur_pos - total_len_pos - 8);
|
6140 | faea38e7 | bellard | qemu_fseek(f, cur_pos, SEEK_SET); |
6141 | 8a7ddc38 | bellard | |
6142 | 8a7ddc38 | bellard | ret = 0;
|
6143 | 8a7ddc38 | bellard | return ret;
|
6144 | 8a7ddc38 | bellard | } |
6145 | 8a7ddc38 | bellard | |
6146 | 8a7ddc38 | bellard | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
6147 | 8a7ddc38 | bellard | { |
6148 | 8a7ddc38 | bellard | SaveStateEntry *se; |
6149 | 8a7ddc38 | bellard | |
6150 | 8a7ddc38 | bellard | for(se = first_se; se != NULL; se = se->next) { |
6151 | 5fafdf24 | ths | if (!strcmp(se->idstr, idstr) &&
|
6152 | 8a7ddc38 | bellard | instance_id == se->instance_id) |
6153 | 8a7ddc38 | bellard | return se;
|
6154 | 8a7ddc38 | bellard | } |
6155 | 8a7ddc38 | bellard | return NULL; |
6156 | 8a7ddc38 | bellard | } |
6157 | 8a7ddc38 | bellard | |
6158 | 9596ebb7 | pbrook | static int qemu_loadvm_state(QEMUFile *f) |
6159 | 8a7ddc38 | bellard | { |
6160 | 8a7ddc38 | bellard | SaveStateEntry *se; |
6161 | faea38e7 | bellard | int len, ret, instance_id, record_len, version_id;
|
6162 | faea38e7 | bellard | int64_t total_len, end_pos, cur_pos; |
6163 | 8a7ddc38 | bellard | unsigned int v; |
6164 | 8a7ddc38 | bellard | char idstr[256]; |
6165 | 3b46e624 | ths | |
6166 | 8a7ddc38 | bellard | v = qemu_get_be32(f); |
6167 | 8a7ddc38 | bellard | if (v != QEMU_VM_FILE_MAGIC)
|
6168 | 8a7ddc38 | bellard | goto fail;
|
6169 | 8a7ddc38 | bellard | v = qemu_get_be32(f); |
6170 | 8a7ddc38 | bellard | if (v != QEMU_VM_FILE_VERSION) {
|
6171 | 8a7ddc38 | bellard | fail:
|
6172 | 8a7ddc38 | bellard | ret = -1;
|
6173 | 8a7ddc38 | bellard | goto the_end;
|
6174 | 8a7ddc38 | bellard | } |
6175 | faea38e7 | bellard | total_len = qemu_get_be64(f); |
6176 | faea38e7 | bellard | end_pos = total_len + qemu_ftell(f); |
6177 | b4608c04 | bellard | for(;;) {
|
6178 | faea38e7 | bellard | if (qemu_ftell(f) >= end_pos)
|
6179 | cd4c3e88 | bellard | break;
|
6180 | faea38e7 | bellard | len = qemu_get_byte(f); |
6181 | ffe8ab83 | ths | qemu_get_buffer(f, (uint8_t *)idstr, len); |
6182 | 8a7ddc38 | bellard | idstr[len] = '\0';
|
6183 | 8a7ddc38 | bellard | instance_id = qemu_get_be32(f); |
6184 | 8a7ddc38 | bellard | version_id = qemu_get_be32(f); |
6185 | 8a7ddc38 | bellard | record_len = qemu_get_be32(f); |
6186 | 8a7ddc38 | bellard | #if 0
|
6187 | 5fafdf24 | ths | printf("idstr=%s instance=0x%x version=%d len=%d\n",
|
6188 | 8a7ddc38 | bellard | idstr, instance_id, version_id, record_len);
|
6189 | 8a7ddc38 | bellard | #endif
|
6190 | faea38e7 | bellard | cur_pos = qemu_ftell(f); |
6191 | 8a7ddc38 | bellard | se = find_se(idstr, instance_id); |
6192 | 8a7ddc38 | bellard | if (!se) {
|
6193 | 5fafdf24 | ths | fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
|
6194 | 8a7ddc38 | bellard | instance_id, idstr); |
6195 | 8a7ddc38 | bellard | } else {
|
6196 | 8a7ddc38 | bellard | ret = se->load_state(f, se->opaque, version_id); |
6197 | 8a7ddc38 | bellard | if (ret < 0) { |
6198 | 5fafdf24 | ths | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
|
6199 | 8a7ddc38 | bellard | instance_id, idstr); |
6200 | 8a7ddc38 | bellard | } |
6201 | 34865134 | bellard | } |
6202 | 8a7ddc38 | bellard | /* always seek to exact end of record */
|
6203 | 8a7ddc38 | bellard | qemu_fseek(f, cur_pos + record_len, SEEK_SET); |
6204 | 8a7ddc38 | bellard | } |
6205 | 8a7ddc38 | bellard | ret = 0;
|
6206 | 8a7ddc38 | bellard | the_end:
|
6207 | faea38e7 | bellard | return ret;
|
6208 | faea38e7 | bellard | } |
6209 | faea38e7 | bellard | |
6210 | faea38e7 | bellard | /* device can contain snapshots */
|
6211 | faea38e7 | bellard | static int bdrv_can_snapshot(BlockDriverState *bs) |
6212 | faea38e7 | bellard | { |
6213 | faea38e7 | bellard | return (bs &&
|
6214 | faea38e7 | bellard | !bdrv_is_removable(bs) && |
6215 | faea38e7 | bellard | !bdrv_is_read_only(bs)); |
6216 | faea38e7 | bellard | } |
6217 | faea38e7 | bellard | |
6218 | faea38e7 | bellard | /* device must be snapshots in order to have a reliable snapshot */
|
6219 | faea38e7 | bellard | static int bdrv_has_snapshot(BlockDriverState *bs) |
6220 | faea38e7 | bellard | { |
6221 | faea38e7 | bellard | return (bs &&
|
6222 | faea38e7 | bellard | !bdrv_is_removable(bs) && |
6223 | faea38e7 | bellard | !bdrv_is_read_only(bs)); |
6224 | faea38e7 | bellard | } |
6225 | faea38e7 | bellard | |
6226 | faea38e7 | bellard | static BlockDriverState *get_bs_snapshots(void) |
6227 | faea38e7 | bellard | { |
6228 | faea38e7 | bellard | BlockDriverState *bs; |
6229 | faea38e7 | bellard | int i;
|
6230 | faea38e7 | bellard | |
6231 | faea38e7 | bellard | if (bs_snapshots)
|
6232 | faea38e7 | bellard | return bs_snapshots;
|
6233 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6234 | e4bcb14c | ths | bs = drives_table[i].bdrv; |
6235 | faea38e7 | bellard | if (bdrv_can_snapshot(bs))
|
6236 | faea38e7 | bellard | goto ok;
|
6237 | faea38e7 | bellard | } |
6238 | faea38e7 | bellard | return NULL; |
6239 | faea38e7 | bellard | ok:
|
6240 | faea38e7 | bellard | bs_snapshots = bs; |
6241 | faea38e7 | bellard | return bs;
|
6242 | faea38e7 | bellard | } |
6243 | faea38e7 | bellard | |
6244 | faea38e7 | bellard | static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
6245 | faea38e7 | bellard | const char *name) |
6246 | faea38e7 | bellard | { |
6247 | faea38e7 | bellard | QEMUSnapshotInfo *sn_tab, *sn; |
6248 | faea38e7 | bellard | int nb_sns, i, ret;
|
6249 | 3b46e624 | ths | |
6250 | faea38e7 | bellard | ret = -ENOENT; |
6251 | faea38e7 | bellard | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
6252 | faea38e7 | bellard | if (nb_sns < 0) |
6253 | faea38e7 | bellard | return ret;
|
6254 | faea38e7 | bellard | for(i = 0; i < nb_sns; i++) { |
6255 | faea38e7 | bellard | sn = &sn_tab[i]; |
6256 | faea38e7 | bellard | if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
|
6257 | faea38e7 | bellard | *sn_info = *sn; |
6258 | faea38e7 | bellard | ret = 0;
|
6259 | faea38e7 | bellard | break;
|
6260 | faea38e7 | bellard | } |
6261 | faea38e7 | bellard | } |
6262 | faea38e7 | bellard | qemu_free(sn_tab); |
6263 | faea38e7 | bellard | return ret;
|
6264 | faea38e7 | bellard | } |
6265 | faea38e7 | bellard | |
6266 | faea38e7 | bellard | void do_savevm(const char *name) |
6267 | faea38e7 | bellard | { |
6268 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6269 | faea38e7 | bellard | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; |
6270 | faea38e7 | bellard | int must_delete, ret, i;
|
6271 | faea38e7 | bellard | BlockDriverInfo bdi1, *bdi = &bdi1; |
6272 | faea38e7 | bellard | QEMUFile *f; |
6273 | faea38e7 | bellard | int saved_vm_running;
|
6274 | 4c279bdf | bellard | #ifdef _WIN32
|
6275 | 4c279bdf | bellard | struct _timeb tb;
|
6276 | 4c279bdf | bellard | #else
|
6277 | faea38e7 | bellard | struct timeval tv;
|
6278 | 4c279bdf | bellard | #endif
|
6279 | faea38e7 | bellard | |
6280 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6281 | faea38e7 | bellard | if (!bs) {
|
6282 | faea38e7 | bellard | term_printf("No block device can accept snapshots\n");
|
6283 | faea38e7 | bellard | return;
|
6284 | faea38e7 | bellard | } |
6285 | faea38e7 | bellard | |
6286 | 6192bc37 | pbrook | /* ??? Should this occur after vm_stop? */
|
6287 | 6192bc37 | pbrook | qemu_aio_flush(); |
6288 | 6192bc37 | pbrook | |
6289 | faea38e7 | bellard | saved_vm_running = vm_running; |
6290 | faea38e7 | bellard | vm_stop(0);
|
6291 | 3b46e624 | ths | |
6292 | faea38e7 | bellard | must_delete = 0;
|
6293 | faea38e7 | bellard | if (name) {
|
6294 | faea38e7 | bellard | ret = bdrv_snapshot_find(bs, old_sn, name); |
6295 | faea38e7 | bellard | if (ret >= 0) { |
6296 | faea38e7 | bellard | must_delete = 1;
|
6297 | faea38e7 | bellard | } |
6298 | faea38e7 | bellard | } |
6299 | faea38e7 | bellard | memset(sn, 0, sizeof(*sn)); |
6300 | faea38e7 | bellard | if (must_delete) {
|
6301 | faea38e7 | bellard | pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
|
6302 | faea38e7 | bellard | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
|
6303 | faea38e7 | bellard | } else {
|
6304 | faea38e7 | bellard | if (name)
|
6305 | faea38e7 | bellard | pstrcpy(sn->name, sizeof(sn->name), name);
|
6306 | faea38e7 | bellard | } |
6307 | faea38e7 | bellard | |
6308 | faea38e7 | bellard | /* fill auxiliary fields */
|
6309 | 4c279bdf | bellard | #ifdef _WIN32
|
6310 | 4c279bdf | bellard | _ftime(&tb); |
6311 | 4c279bdf | bellard | sn->date_sec = tb.time; |
6312 | 4c279bdf | bellard | sn->date_nsec = tb.millitm * 1000000;
|
6313 | 4c279bdf | bellard | #else
|
6314 | faea38e7 | bellard | gettimeofday(&tv, NULL);
|
6315 | faea38e7 | bellard | sn->date_sec = tv.tv_sec; |
6316 | faea38e7 | bellard | sn->date_nsec = tv.tv_usec * 1000;
|
6317 | 4c279bdf | bellard | #endif
|
6318 | faea38e7 | bellard | sn->vm_clock_nsec = qemu_get_clock(vm_clock); |
6319 | 3b46e624 | ths | |
6320 | faea38e7 | bellard | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
6321 | faea38e7 | bellard | term_printf("Device %s does not support VM state snapshots\n",
|
6322 | faea38e7 | bellard | bdrv_get_device_name(bs)); |
6323 | faea38e7 | bellard | goto the_end;
|
6324 | faea38e7 | bellard | } |
6325 | 3b46e624 | ths | |
6326 | faea38e7 | bellard | /* save the VM state */
|
6327 | faea38e7 | bellard | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
|
6328 | faea38e7 | bellard | if (!f) {
|
6329 | faea38e7 | bellard | term_printf("Could not open VM state file\n");
|
6330 | faea38e7 | bellard | goto the_end;
|
6331 | faea38e7 | bellard | } |
6332 | faea38e7 | bellard | ret = qemu_savevm_state(f); |
6333 | faea38e7 | bellard | sn->vm_state_size = qemu_ftell(f); |
6334 | faea38e7 | bellard | qemu_fclose(f); |
6335 | faea38e7 | bellard | if (ret < 0) { |
6336 | faea38e7 | bellard | term_printf("Error %d while writing VM\n", ret);
|
6337 | faea38e7 | bellard | goto the_end;
|
6338 | faea38e7 | bellard | } |
6339 | 3b46e624 | ths | |
6340 | faea38e7 | bellard | /* create the snapshots */
|
6341 | faea38e7 | bellard | |
6342 | e4bcb14c | ths | for(i = 0; i < nb_drives; i++) { |
6343 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6344 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6345 | faea38e7 | bellard | if (must_delete) {
|
6346 | faea38e7 | bellard | ret = bdrv_snapshot_delete(bs1, old_sn->id_str); |
6347 | faea38e7 | bellard | if (ret < 0) { |
6348 | faea38e7 | bellard | term_printf("Error while deleting snapshot on '%s'\n",
|
6349 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6350 | faea38e7 | bellard | } |
6351 | faea38e7 | bellard | } |
6352 | faea38e7 | bellard | ret = bdrv_snapshot_create(bs1, sn); |
6353 | faea38e7 | bellard | if (ret < 0) { |
6354 | faea38e7 | bellard | term_printf("Error while creating snapshot on '%s'\n",
|
6355 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6356 | faea38e7 | bellard | } |
6357 | faea38e7 | bellard | } |
6358 | faea38e7 | bellard | } |
6359 | faea38e7 | bellard | |
6360 | faea38e7 | bellard | the_end:
|
6361 | 8a7ddc38 | bellard | if (saved_vm_running)
|
6362 | 8a7ddc38 | bellard | vm_start(); |
6363 | faea38e7 | bellard | } |
6364 | faea38e7 | bellard | |
6365 | faea38e7 | bellard | void do_loadvm(const char *name) |
6366 | faea38e7 | bellard | { |
6367 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6368 | faea38e7 | bellard | BlockDriverInfo bdi1, *bdi = &bdi1; |
6369 | faea38e7 | bellard | QEMUFile *f; |
6370 | faea38e7 | bellard | int i, ret;
|
6371 | faea38e7 | bellard | int saved_vm_running;
|
6372 | faea38e7 | bellard | |
6373 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6374 | faea38e7 | bellard | if (!bs) {
|
6375 | faea38e7 | bellard | term_printf("No block device supports snapshots\n");
|
6376 | faea38e7 | bellard | return;
|
6377 | faea38e7 | bellard | } |
6378 | 3b46e624 | ths | |
6379 | 6192bc37 | pbrook | /* Flush all IO requests so they don't interfere with the new state. */
|
6380 | 6192bc37 | pbrook | qemu_aio_flush(); |
6381 | 6192bc37 | pbrook | |
6382 | faea38e7 | bellard | saved_vm_running = vm_running; |
6383 | faea38e7 | bellard | vm_stop(0);
|
6384 | faea38e7 | bellard | |
6385 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6386 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6387 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6388 | faea38e7 | bellard | ret = bdrv_snapshot_goto(bs1, name); |
6389 | faea38e7 | bellard | if (ret < 0) { |
6390 | faea38e7 | bellard | if (bs != bs1)
|
6391 | faea38e7 | bellard | term_printf("Warning: ");
|
6392 | faea38e7 | bellard | switch(ret) {
|
6393 | faea38e7 | bellard | case -ENOTSUP:
|
6394 | faea38e7 | bellard | term_printf("Snapshots not supported on device '%s'\n",
|
6395 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6396 | faea38e7 | bellard | break;
|
6397 | faea38e7 | bellard | case -ENOENT:
|
6398 | faea38e7 | bellard | term_printf("Could not find snapshot '%s' on device '%s'\n",
|
6399 | faea38e7 | bellard | name, bdrv_get_device_name(bs1)); |
6400 | faea38e7 | bellard | break;
|
6401 | faea38e7 | bellard | default:
|
6402 | faea38e7 | bellard | term_printf("Error %d while activating snapshot on '%s'\n",
|
6403 | faea38e7 | bellard | ret, bdrv_get_device_name(bs1)); |
6404 | faea38e7 | bellard | break;
|
6405 | faea38e7 | bellard | } |
6406 | faea38e7 | bellard | /* fatal on snapshot block device */
|
6407 | faea38e7 | bellard | if (bs == bs1)
|
6408 | faea38e7 | bellard | goto the_end;
|
6409 | faea38e7 | bellard | } |
6410 | faea38e7 | bellard | } |
6411 | faea38e7 | bellard | } |
6412 | faea38e7 | bellard | |
6413 | faea38e7 | bellard | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
6414 | faea38e7 | bellard | term_printf("Device %s does not support VM state snapshots\n",
|
6415 | faea38e7 | bellard | bdrv_get_device_name(bs)); |
6416 | faea38e7 | bellard | return;
|
6417 | faea38e7 | bellard | } |
6418 | 3b46e624 | ths | |
6419 | faea38e7 | bellard | /* restore the VM state */
|
6420 | faea38e7 | bellard | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
|
6421 | faea38e7 | bellard | if (!f) {
|
6422 | faea38e7 | bellard | term_printf("Could not open VM state file\n");
|
6423 | faea38e7 | bellard | goto the_end;
|
6424 | faea38e7 | bellard | } |
6425 | faea38e7 | bellard | ret = qemu_loadvm_state(f); |
6426 | faea38e7 | bellard | qemu_fclose(f); |
6427 | faea38e7 | bellard | if (ret < 0) { |
6428 | faea38e7 | bellard | term_printf("Error %d while loading VM state\n", ret);
|
6429 | faea38e7 | bellard | } |
6430 | faea38e7 | bellard | the_end:
|
6431 | faea38e7 | bellard | if (saved_vm_running)
|
6432 | faea38e7 | bellard | vm_start(); |
6433 | faea38e7 | bellard | } |
6434 | faea38e7 | bellard | |
6435 | faea38e7 | bellard | void do_delvm(const char *name) |
6436 | faea38e7 | bellard | { |
6437 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6438 | faea38e7 | bellard | int i, ret;
|
6439 | faea38e7 | bellard | |
6440 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6441 | faea38e7 | bellard | if (!bs) {
|
6442 | faea38e7 | bellard | term_printf("No block device supports snapshots\n");
|
6443 | faea38e7 | bellard | return;
|
6444 | faea38e7 | bellard | } |
6445 | 3b46e624 | ths | |
6446 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6447 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6448 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6449 | faea38e7 | bellard | ret = bdrv_snapshot_delete(bs1, name); |
6450 | faea38e7 | bellard | if (ret < 0) { |
6451 | faea38e7 | bellard | if (ret == -ENOTSUP)
|
6452 | faea38e7 | bellard | term_printf("Snapshots not supported on device '%s'\n",
|
6453 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6454 | faea38e7 | bellard | else
|
6455 | faea38e7 | bellard | term_printf("Error %d while deleting snapshot on '%s'\n",
|
6456 | faea38e7 | bellard | ret, bdrv_get_device_name(bs1)); |
6457 | faea38e7 | bellard | } |
6458 | faea38e7 | bellard | } |
6459 | faea38e7 | bellard | } |
6460 | faea38e7 | bellard | } |
6461 | faea38e7 | bellard | |
6462 | faea38e7 | bellard | void do_info_snapshots(void) |
6463 | faea38e7 | bellard | { |
6464 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6465 | faea38e7 | bellard | QEMUSnapshotInfo *sn_tab, *sn; |
6466 | faea38e7 | bellard | int nb_sns, i;
|
6467 | faea38e7 | bellard | char buf[256]; |
6468 | faea38e7 | bellard | |
6469 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6470 | faea38e7 | bellard | if (!bs) {
|
6471 | faea38e7 | bellard | term_printf("No available block device supports snapshots\n");
|
6472 | faea38e7 | bellard | return;
|
6473 | faea38e7 | bellard | } |
6474 | faea38e7 | bellard | term_printf("Snapshot devices:");
|
6475 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6476 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6477 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6478 | faea38e7 | bellard | if (bs == bs1)
|
6479 | faea38e7 | bellard | term_printf(" %s", bdrv_get_device_name(bs1));
|
6480 | faea38e7 | bellard | } |
6481 | faea38e7 | bellard | } |
6482 | faea38e7 | bellard | term_printf("\n");
|
6483 | faea38e7 | bellard | |
6484 | faea38e7 | bellard | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
6485 | faea38e7 | bellard | if (nb_sns < 0) { |
6486 | faea38e7 | bellard | term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
|
6487 | faea38e7 | bellard | return;
|
6488 | faea38e7 | bellard | } |
6489 | faea38e7 | bellard | term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
|
6490 | faea38e7 | bellard | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
6491 | faea38e7 | bellard | for(i = 0; i < nb_sns; i++) { |
6492 | faea38e7 | bellard | sn = &sn_tab[i]; |
6493 | faea38e7 | bellard | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
6494 | faea38e7 | bellard | } |
6495 | faea38e7 | bellard | qemu_free(sn_tab); |
6496 | 8a7ddc38 | bellard | } |
6497 | 8a7ddc38 | bellard | |
6498 | 8a7ddc38 | bellard | /***********************************************************/
|
6499 | 8a7ddc38 | bellard | /* ram save/restore */
|
6500 | 8a7ddc38 | bellard | |
6501 | 8a7ddc38 | bellard | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
6502 | 8a7ddc38 | bellard | { |
6503 | 8a7ddc38 | bellard | int v;
|
6504 | 8a7ddc38 | bellard | |
6505 | 8a7ddc38 | bellard | v = qemu_get_byte(f); |
6506 | 8a7ddc38 | bellard | switch(v) {
|
6507 | 8a7ddc38 | bellard | case 0: |
6508 | 8a7ddc38 | bellard | if (qemu_get_buffer(f, buf, len) != len)
|
6509 | 8a7ddc38 | bellard | return -EIO;
|
6510 | 8a7ddc38 | bellard | break;
|
6511 | 8a7ddc38 | bellard | case 1: |
6512 | 8a7ddc38 | bellard | v = qemu_get_byte(f); |
6513 | 8a7ddc38 | bellard | memset(buf, v, len); |
6514 | 8a7ddc38 | bellard | break;
|
6515 | 8a7ddc38 | bellard | default:
|
6516 | 8a7ddc38 | bellard | return -EINVAL;
|
6517 | 8a7ddc38 | bellard | } |
6518 | 8a7ddc38 | bellard | return 0; |
6519 | 8a7ddc38 | bellard | } |
6520 | 8a7ddc38 | bellard | |
6521 | c88676f8 | bellard | static int ram_load_v1(QEMUFile *f, void *opaque) |
6522 | c88676f8 | bellard | { |
6523 | 00f82b8a | aurel32 | int ret;
|
6524 | 00f82b8a | aurel32 | ram_addr_t i; |
6525 | c88676f8 | bellard | |
6526 | c88676f8 | bellard | if (qemu_get_be32(f) != phys_ram_size)
|
6527 | c88676f8 | bellard | return -EINVAL;
|
6528 | c88676f8 | bellard | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
6529 | c88676f8 | bellard | ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
6530 | c88676f8 | bellard | if (ret)
|
6531 | c88676f8 | bellard | return ret;
|
6532 | c88676f8 | bellard | } |
6533 | c88676f8 | bellard | return 0; |
6534 | c88676f8 | bellard | } |
6535 | c88676f8 | bellard | |
6536 | c88676f8 | bellard | #define BDRV_HASH_BLOCK_SIZE 1024 |
6537 | c88676f8 | bellard | #define IOBUF_SIZE 4096 |
6538 | c88676f8 | bellard | #define RAM_CBLOCK_MAGIC 0xfabe |
6539 | c88676f8 | bellard | |
6540 | c88676f8 | bellard | typedef struct RamCompressState { |
6541 | c88676f8 | bellard | z_stream zstream; |
6542 | c88676f8 | bellard | QEMUFile *f; |
6543 | c88676f8 | bellard | uint8_t buf[IOBUF_SIZE]; |
6544 | c88676f8 | bellard | } RamCompressState; |
6545 | c88676f8 | bellard | |
6546 | c88676f8 | bellard | static int ram_compress_open(RamCompressState *s, QEMUFile *f) |
6547 | c88676f8 | bellard | { |
6548 | c88676f8 | bellard | int ret;
|
6549 | c88676f8 | bellard | memset(s, 0, sizeof(*s)); |
6550 | c88676f8 | bellard | s->f = f; |
6551 | c88676f8 | bellard | ret = deflateInit2(&s->zstream, 1,
|
6552 | 5fafdf24 | ths | Z_DEFLATED, 15,
|
6553 | c88676f8 | bellard | 9, Z_DEFAULT_STRATEGY);
|
6554 | c88676f8 | bellard | if (ret != Z_OK)
|
6555 | c88676f8 | bellard | return -1; |
6556 | c88676f8 | bellard | s->zstream.avail_out = IOBUF_SIZE; |
6557 | c88676f8 | bellard | s->zstream.next_out = s->buf; |
6558 | c88676f8 | bellard | return 0; |
6559 | c88676f8 | bellard | } |
6560 | c88676f8 | bellard | |
6561 | c88676f8 | bellard | static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len) |
6562 | c88676f8 | bellard | { |
6563 | c88676f8 | bellard | qemu_put_be16(s->f, RAM_CBLOCK_MAGIC); |
6564 | c88676f8 | bellard | qemu_put_be16(s->f, len); |
6565 | c88676f8 | bellard | qemu_put_buffer(s->f, buf, len); |
6566 | c88676f8 | bellard | } |
6567 | c88676f8 | bellard | |
6568 | c88676f8 | bellard | static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len) |
6569 | c88676f8 | bellard | { |
6570 | c88676f8 | bellard | int ret;
|
6571 | c88676f8 | bellard | |
6572 | c88676f8 | bellard | s->zstream.avail_in = len; |
6573 | c88676f8 | bellard | s->zstream.next_in = (uint8_t *)buf; |
6574 | c88676f8 | bellard | while (s->zstream.avail_in > 0) { |
6575 | c88676f8 | bellard | ret = deflate(&s->zstream, Z_NO_FLUSH); |
6576 | c88676f8 | bellard | if (ret != Z_OK)
|
6577 | c88676f8 | bellard | return -1; |
6578 | c88676f8 | bellard | if (s->zstream.avail_out == 0) { |
6579 | c88676f8 | bellard | ram_put_cblock(s, s->buf, IOBUF_SIZE); |
6580 | c88676f8 | bellard | s->zstream.avail_out = IOBUF_SIZE; |
6581 | c88676f8 | bellard | s->zstream.next_out = s->buf; |
6582 | c88676f8 | bellard | } |
6583 | c88676f8 | bellard | } |
6584 | c88676f8 | bellard | return 0; |
6585 | c88676f8 | bellard | } |
6586 | c88676f8 | bellard | |
6587 | c88676f8 | bellard | static void ram_compress_close(RamCompressState *s) |
6588 | c88676f8 | bellard | { |
6589 | c88676f8 | bellard | int len, ret;
|
6590 | c88676f8 | bellard | |
6591 | c88676f8 | bellard | /* compress last bytes */
|
6592 | c88676f8 | bellard | for(;;) {
|
6593 | c88676f8 | bellard | ret = deflate(&s->zstream, Z_FINISH); |
6594 | c88676f8 | bellard | if (ret == Z_OK || ret == Z_STREAM_END) {
|
6595 | c88676f8 | bellard | len = IOBUF_SIZE - s->zstream.avail_out; |
6596 | c88676f8 | bellard | if (len > 0) { |
6597 | c88676f8 | bellard | ram_put_cblock(s, s->buf, len); |
6598 | c88676f8 | bellard | } |
6599 | c88676f8 | bellard | s->zstream.avail_out = IOBUF_SIZE; |
6600 | c88676f8 | bellard | s->zstream.next_out = s->buf; |
6601 | c88676f8 | bellard | if (ret == Z_STREAM_END)
|
6602 | c88676f8 | bellard | break;
|
6603 | c88676f8 | bellard | } else {
|
6604 | c88676f8 | bellard | goto fail;
|
6605 | c88676f8 | bellard | } |
6606 | c88676f8 | bellard | } |
6607 | c88676f8 | bellard | fail:
|
6608 | c88676f8 | bellard | deflateEnd(&s->zstream); |
6609 | c88676f8 | bellard | } |
6610 | c88676f8 | bellard | |
6611 | c88676f8 | bellard | typedef struct RamDecompressState { |
6612 | c88676f8 | bellard | z_stream zstream; |
6613 | c88676f8 | bellard | QEMUFile *f; |
6614 | c88676f8 | bellard | uint8_t buf[IOBUF_SIZE]; |
6615 | c88676f8 | bellard | } RamDecompressState; |
6616 | c88676f8 | bellard | |
6617 | c88676f8 | bellard | static int ram_decompress_open(RamDecompressState *s, QEMUFile *f) |
6618 | c88676f8 | bellard | { |
6619 | c88676f8 | bellard | int ret;
|
6620 | c88676f8 | bellard | memset(s, 0, sizeof(*s)); |
6621 | c88676f8 | bellard | s->f = f; |
6622 | c88676f8 | bellard | ret = inflateInit(&s->zstream); |
6623 | c88676f8 | bellard | if (ret != Z_OK)
|
6624 | c88676f8 | bellard | return -1; |
6625 | c88676f8 | bellard | return 0; |
6626 | c88676f8 | bellard | } |
6627 | c88676f8 | bellard | |
6628 | c88676f8 | bellard | static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len) |
6629 | c88676f8 | bellard | { |
6630 | c88676f8 | bellard | int ret, clen;
|
6631 | c88676f8 | bellard | |
6632 | c88676f8 | bellard | s->zstream.avail_out = len; |
6633 | c88676f8 | bellard | s->zstream.next_out = buf; |
6634 | c88676f8 | bellard | while (s->zstream.avail_out > 0) { |
6635 | c88676f8 | bellard | if (s->zstream.avail_in == 0) { |
6636 | c88676f8 | bellard | if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
|
6637 | c88676f8 | bellard | return -1; |
6638 | c88676f8 | bellard | clen = qemu_get_be16(s->f); |
6639 | c88676f8 | bellard | if (clen > IOBUF_SIZE)
|
6640 | c88676f8 | bellard | return -1; |
6641 | c88676f8 | bellard | qemu_get_buffer(s->f, s->buf, clen); |
6642 | c88676f8 | bellard | s->zstream.avail_in = clen; |
6643 | c88676f8 | bellard | s->zstream.next_in = s->buf; |
6644 | c88676f8 | bellard | } |
6645 | c88676f8 | bellard | ret = inflate(&s->zstream, Z_PARTIAL_FLUSH); |
6646 | c88676f8 | bellard | if (ret != Z_OK && ret != Z_STREAM_END) {
|
6647 | c88676f8 | bellard | return -1; |
6648 | c88676f8 | bellard | } |
6649 | c88676f8 | bellard | } |
6650 | c88676f8 | bellard | return 0; |
6651 | c88676f8 | bellard | } |
6652 | c88676f8 | bellard | |
6653 | c88676f8 | bellard | static void ram_decompress_close(RamDecompressState *s) |
6654 | c88676f8 | bellard | { |
6655 | c88676f8 | bellard | inflateEnd(&s->zstream); |
6656 | c88676f8 | bellard | } |
6657 | c88676f8 | bellard | |
6658 | 8a7ddc38 | bellard | static void ram_save(QEMUFile *f, void *opaque) |
6659 | 8a7ddc38 | bellard | { |
6660 | 00f82b8a | aurel32 | ram_addr_t i; |
6661 | c88676f8 | bellard | RamCompressState s1, *s = &s1; |
6662 | c88676f8 | bellard | uint8_t buf[10];
|
6663 | 3b46e624 | ths | |
6664 | 8a7ddc38 | bellard | qemu_put_be32(f, phys_ram_size); |
6665 | c88676f8 | bellard | if (ram_compress_open(s, f) < 0) |
6666 | c88676f8 | bellard | return;
|
6667 | c88676f8 | bellard | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
6668 | c88676f8 | bellard | #if 0
|
6669 | c88676f8 | bellard | if (tight_savevm_enabled) {
|
6670 | c88676f8 | bellard | int64_t sector_num;
|
6671 | c88676f8 | bellard | int j;
|
6672 | c88676f8 | bellard | |
6673 | c88676f8 | bellard | /* find if the memory block is available on a virtual
|
6674 | c88676f8 | bellard | block device */
|
6675 | c88676f8 | bellard | sector_num = -1;
|
6676 | e4bcb14c | ths | for(j = 0; j < nb_drives; j++) {
|
6677 | e4bcb14c | ths | sector_num = bdrv_hash_find(drives_table[j].bdrv,
|
6678 | e4bcb14c | ths | phys_ram_base + i,
|
6679 | e4bcb14c | ths | BDRV_HASH_BLOCK_SIZE);
|
6680 | e4bcb14c | ths | if (sector_num >= 0)
|
6681 | e4bcb14c | ths | break;
|
6682 | c88676f8 | bellard | }
|
6683 | e4bcb14c | ths | if (j == nb_drives)
|
6684 | c88676f8 | bellard | goto normal_compress;
|
6685 | c88676f8 | bellard | buf[0] = 1;
|
6686 | c88676f8 | bellard | buf[1] = j;
|
6687 | c88676f8 | bellard | cpu_to_be64wu((uint64_t *)(buf + 2), sector_num);
|
6688 | c88676f8 | bellard | ram_compress_buf(s, buf, 10);
|
6689 | 5fafdf24 | ths | } else
|
6690 | c88676f8 | bellard | #endif
|
6691 | c88676f8 | bellard | { |
6692 | c88676f8 | bellard | // normal_compress:
|
6693 | c88676f8 | bellard | buf[0] = 0; |
6694 | c88676f8 | bellard | ram_compress_buf(s, buf, 1);
|
6695 | c88676f8 | bellard | ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); |
6696 | c88676f8 | bellard | } |
6697 | 8a7ddc38 | bellard | } |
6698 | c88676f8 | bellard | ram_compress_close(s); |
6699 | 8a7ddc38 | bellard | } |
6700 | 8a7ddc38 | bellard | |
6701 | 8a7ddc38 | bellard | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
6702 | 8a7ddc38 | bellard | { |
6703 | c88676f8 | bellard | RamDecompressState s1, *s = &s1; |
6704 | c88676f8 | bellard | uint8_t buf[10];
|
6705 | 00f82b8a | aurel32 | ram_addr_t i; |
6706 | 8a7ddc38 | bellard | |
6707 | c88676f8 | bellard | if (version_id == 1) |
6708 | c88676f8 | bellard | return ram_load_v1(f, opaque);
|
6709 | c88676f8 | bellard | if (version_id != 2) |
6710 | 8a7ddc38 | bellard | return -EINVAL;
|
6711 | 8a7ddc38 | bellard | if (qemu_get_be32(f) != phys_ram_size)
|
6712 | 8a7ddc38 | bellard | return -EINVAL;
|
6713 | c88676f8 | bellard | if (ram_decompress_open(s, f) < 0) |
6714 | c88676f8 | bellard | return -EINVAL;
|
6715 | c88676f8 | bellard | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
6716 | c88676f8 | bellard | if (ram_decompress_buf(s, buf, 1) < 0) { |
6717 | c88676f8 | bellard | fprintf(stderr, "Error while reading ram block header\n");
|
6718 | c88676f8 | bellard | goto error;
|
6719 | c88676f8 | bellard | } |
6720 | c88676f8 | bellard | if (buf[0] == 0) { |
6721 | c88676f8 | bellard | if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) { |
6722 | 00f82b8a | aurel32 | fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
|
6723 | c88676f8 | bellard | goto error;
|
6724 | c88676f8 | bellard | } |
6725 | 5fafdf24 | ths | } else
|
6726 | c88676f8 | bellard | #if 0
|
6727 | c88676f8 | bellard | if (buf[0] == 1) {
|
6728 | c88676f8 | bellard | int bs_index;
|
6729 | c88676f8 | bellard | int64_t sector_num;
|
6730 | c88676f8 | bellard | |
6731 | c88676f8 | bellard | ram_decompress_buf(s, buf + 1, 9);
|
6732 | c88676f8 | bellard | bs_index = buf[1];
|
6733 | c88676f8 | bellard | sector_num = be64_to_cpupu((const uint64_t *)(buf + 2));
|
6734 | e4bcb14c | ths | if (bs_index >= nb_drives) {
|
6735 | c88676f8 | bellard | fprintf(stderr, "Invalid block device index %d\n", bs_index);
|
6736 | c88676f8 | bellard | goto error;
|
6737 | c88676f8 | bellard | }
|
6738 | e4bcb14c | ths | if (bdrv_read(drives_table[bs_index].bdrv, sector_num,
|
6739 | e4bcb14c | ths | phys_ram_base + i,
|
6740 | c88676f8 | bellard | BDRV_HASH_BLOCK_SIZE / 512) < 0) {
|
6741 | 5fafdf24 | ths | fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n",
|
6742 | c88676f8 | bellard | bs_index, sector_num);
|
6743 | c88676f8 | bellard | goto error;
|
6744 | c88676f8 | bellard | }
|
6745 | 5fafdf24 | ths | } else
|
6746 | c88676f8 | bellard | #endif
|
6747 | c88676f8 | bellard | { |
6748 | c88676f8 | bellard | error:
|
6749 | c88676f8 | bellard | printf("Error block header\n");
|
6750 | c88676f8 | bellard | return -EINVAL;
|
6751 | c88676f8 | bellard | } |
6752 | 8a7ddc38 | bellard | } |
6753 | c88676f8 | bellard | ram_decompress_close(s); |
6754 | 8a7ddc38 | bellard | return 0; |
6755 | 8a7ddc38 | bellard | } |
6756 | 8a7ddc38 | bellard | |
6757 | 8a7ddc38 | bellard | /***********************************************************/
|
6758 | 83f64091 | bellard | /* bottom halves (can be seen as timers which expire ASAP) */
|
6759 | 83f64091 | bellard | |
6760 | 83f64091 | bellard | struct QEMUBH {
|
6761 | 83f64091 | bellard | QEMUBHFunc *cb; |
6762 | 83f64091 | bellard | void *opaque;
|
6763 | 83f64091 | bellard | int scheduled;
|
6764 | 83f64091 | bellard | QEMUBH *next; |
6765 | 83f64091 | bellard | }; |
6766 | 83f64091 | bellard | |
6767 | 83f64091 | bellard | static QEMUBH *first_bh = NULL; |
6768 | 83f64091 | bellard | |
6769 | 83f64091 | bellard | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
|
6770 | 83f64091 | bellard | { |
6771 | 83f64091 | bellard | QEMUBH *bh; |
6772 | 83f64091 | bellard | bh = qemu_mallocz(sizeof(QEMUBH));
|
6773 | 83f64091 | bellard | if (!bh)
|
6774 | 83f64091 | bellard | return NULL; |
6775 | 83f64091 | bellard | bh->cb = cb; |
6776 | 83f64091 | bellard | bh->opaque = opaque; |
6777 | 83f64091 | bellard | return bh;
|
6778 | 83f64091 | bellard | } |
6779 | 83f64091 | bellard | |
6780 | 6eb5733a | bellard | int qemu_bh_poll(void) |
6781 | 83f64091 | bellard | { |
6782 | 83f64091 | bellard | QEMUBH *bh, **pbh; |
6783 | 6eb5733a | bellard | int ret;
|
6784 | 83f64091 | bellard | |
6785 | 6eb5733a | bellard | ret = 0;
|
6786 | 83f64091 | bellard | for(;;) {
|
6787 | 83f64091 | bellard | pbh = &first_bh; |
6788 | 83f64091 | bellard | bh = *pbh; |
6789 | 83f64091 | bellard | if (!bh)
|
6790 | 83f64091 | bellard | break;
|
6791 | 6eb5733a | bellard | ret = 1;
|
6792 | 83f64091 | bellard | *pbh = bh->next; |
6793 | 83f64091 | bellard | bh->scheduled = 0;
|
6794 | 83f64091 | bellard | bh->cb(bh->opaque); |
6795 | 83f64091 | bellard | } |
6796 | 6eb5733a | bellard | return ret;
|
6797 | 83f64091 | bellard | } |
6798 | 83f64091 | bellard | |
6799 | 83f64091 | bellard | void qemu_bh_schedule(QEMUBH *bh)
|
6800 | 83f64091 | bellard | { |
6801 | 83f64091 | bellard | CPUState *env = cpu_single_env; |
6802 | 83f64091 | bellard | if (bh->scheduled)
|
6803 | 83f64091 | bellard | return;
|
6804 | 83f64091 | bellard | bh->scheduled = 1;
|
6805 | 83f64091 | bellard | bh->next = first_bh; |
6806 | 83f64091 | bellard | first_bh = bh; |
6807 | 83f64091 | bellard | |
6808 | 83f64091 | bellard | /* stop the currently executing CPU to execute the BH ASAP */
|
6809 | 83f64091 | bellard | if (env) {
|
6810 | 83f64091 | bellard | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
6811 | 83f64091 | bellard | } |
6812 | 83f64091 | bellard | } |
6813 | 83f64091 | bellard | |
6814 | 83f64091 | bellard | void qemu_bh_cancel(QEMUBH *bh)
|
6815 | 83f64091 | bellard | { |
6816 | 83f64091 | bellard | QEMUBH **pbh; |
6817 | 83f64091 | bellard | if (bh->scheduled) {
|
6818 | 83f64091 | bellard | pbh = &first_bh; |
6819 | 83f64091 | bellard | while (*pbh != bh)
|
6820 | 83f64091 | bellard | pbh = &(*pbh)->next; |
6821 | 83f64091 | bellard | *pbh = bh->next; |
6822 | 83f64091 | bellard | bh->scheduled = 0;
|
6823 | 83f64091 | bellard | } |
6824 | 83f64091 | bellard | } |
6825 | 83f64091 | bellard | |
6826 | 83f64091 | bellard | void qemu_bh_delete(QEMUBH *bh)
|
6827 | 83f64091 | bellard | { |
6828 | 83f64091 | bellard | qemu_bh_cancel(bh); |
6829 | 83f64091 | bellard | qemu_free(bh); |
6830 | 83f64091 | bellard | } |
6831 | 83f64091 | bellard | |
6832 | 83f64091 | bellard | /***********************************************************/
|
6833 | cc1daa40 | bellard | /* machine registration */
|
6834 | cc1daa40 | bellard | |
6835 | cc1daa40 | bellard | QEMUMachine *first_machine = NULL;
|
6836 | cc1daa40 | bellard | |
6837 | cc1daa40 | bellard | int qemu_register_machine(QEMUMachine *m)
|
6838 | cc1daa40 | bellard | { |
6839 | cc1daa40 | bellard | QEMUMachine **pm; |
6840 | cc1daa40 | bellard | pm = &first_machine; |
6841 | cc1daa40 | bellard | while (*pm != NULL) |
6842 | cc1daa40 | bellard | pm = &(*pm)->next; |
6843 | cc1daa40 | bellard | m->next = NULL;
|
6844 | cc1daa40 | bellard | *pm = m; |
6845 | cc1daa40 | bellard | return 0; |
6846 | cc1daa40 | bellard | } |
6847 | cc1daa40 | bellard | |
6848 | 9596ebb7 | pbrook | static QEMUMachine *find_machine(const char *name) |
6849 | cc1daa40 | bellard | { |
6850 | cc1daa40 | bellard | QEMUMachine *m; |
6851 | cc1daa40 | bellard | |
6852 | cc1daa40 | bellard | for(m = first_machine; m != NULL; m = m->next) { |
6853 | cc1daa40 | bellard | if (!strcmp(m->name, name))
|
6854 | cc1daa40 | bellard | return m;
|
6855 | cc1daa40 | bellard | } |
6856 | cc1daa40 | bellard | return NULL; |
6857 | cc1daa40 | bellard | } |
6858 | cc1daa40 | bellard | |
6859 | cc1daa40 | bellard | /***********************************************************/
|
6860 | 8a7ddc38 | bellard | /* main execution loop */
|
6861 | 8a7ddc38 | bellard | |
6862 | 9596ebb7 | pbrook | static void gui_update(void *opaque) |
6863 | 8a7ddc38 | bellard | { |
6864 | 740733bb | ths | DisplayState *ds = opaque; |
6865 | 740733bb | ths | ds->dpy_refresh(ds); |
6866 | f442e08b | aurel32 | qemu_mod_timer(ds->gui_timer, |
6867 | f442e08b | aurel32 | (ds->gui_timer_interval ? |
6868 | f442e08b | aurel32 | ds->gui_timer_interval : |
6869 | f442e08b | aurel32 | GUI_REFRESH_INTERVAL) |
6870 | f442e08b | aurel32 | + qemu_get_clock(rt_clock)); |
6871 | 8a7ddc38 | bellard | } |
6872 | 8a7ddc38 | bellard | |
6873 | 0bd48850 | bellard | struct vm_change_state_entry {
|
6874 | 0bd48850 | bellard | VMChangeStateHandler *cb; |
6875 | 0bd48850 | bellard | void *opaque;
|
6876 | 0bd48850 | bellard | LIST_ENTRY (vm_change_state_entry) entries; |
6877 | 0bd48850 | bellard | }; |
6878 | 0bd48850 | bellard | |
6879 | 0bd48850 | bellard | static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
|
6880 | 0bd48850 | bellard | |
6881 | 0bd48850 | bellard | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
6882 | 0bd48850 | bellard | void *opaque)
|
6883 | 0bd48850 | bellard | { |
6884 | 0bd48850 | bellard | VMChangeStateEntry *e; |
6885 | 0bd48850 | bellard | |
6886 | 0bd48850 | bellard | e = qemu_mallocz(sizeof (*e));
|
6887 | 0bd48850 | bellard | if (!e)
|
6888 | 0bd48850 | bellard | return NULL; |
6889 | 0bd48850 | bellard | |
6890 | 0bd48850 | bellard | e->cb = cb; |
6891 | 0bd48850 | bellard | e->opaque = opaque; |
6892 | 0bd48850 | bellard | LIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
6893 | 0bd48850 | bellard | return e;
|
6894 | 0bd48850 | bellard | } |
6895 | 0bd48850 | bellard | |
6896 | 0bd48850 | bellard | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
|
6897 | 0bd48850 | bellard | { |
6898 | 0bd48850 | bellard | LIST_REMOVE (e, entries); |
6899 | 0bd48850 | bellard | qemu_free (e); |
6900 | 0bd48850 | bellard | } |
6901 | 0bd48850 | bellard | |
6902 | 0bd48850 | bellard | static void vm_state_notify(int running) |
6903 | 0bd48850 | bellard | { |
6904 | 0bd48850 | bellard | VMChangeStateEntry *e; |
6905 | 0bd48850 | bellard | |
6906 | 0bd48850 | bellard | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
|
6907 | 0bd48850 | bellard | e->cb(e->opaque, running); |
6908 | 0bd48850 | bellard | } |
6909 | 0bd48850 | bellard | } |
6910 | 0bd48850 | bellard | |
6911 | 8a7ddc38 | bellard | /* XXX: support several handlers */
|
6912 | 0bd48850 | bellard | static VMStopHandler *vm_stop_cb;
|
6913 | 0bd48850 | bellard | static void *vm_stop_opaque; |
6914 | 8a7ddc38 | bellard | |
6915 | 8a7ddc38 | bellard | int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) |
6916 | 8a7ddc38 | bellard | { |
6917 | 8a7ddc38 | bellard | vm_stop_cb = cb; |
6918 | 8a7ddc38 | bellard | vm_stop_opaque = opaque; |
6919 | 8a7ddc38 | bellard | return 0; |
6920 | 8a7ddc38 | bellard | } |
6921 | 8a7ddc38 | bellard | |
6922 | 8a7ddc38 | bellard | void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) |
6923 | 8a7ddc38 | bellard | { |
6924 | 8a7ddc38 | bellard | vm_stop_cb = NULL;
|
6925 | 8a7ddc38 | bellard | } |
6926 | 8a7ddc38 | bellard | |
6927 | 8a7ddc38 | bellard | void vm_start(void) |
6928 | 8a7ddc38 | bellard | { |
6929 | 8a7ddc38 | bellard | if (!vm_running) {
|
6930 | 8a7ddc38 | bellard | cpu_enable_ticks(); |
6931 | 8a7ddc38 | bellard | vm_running = 1;
|
6932 | 0bd48850 | bellard | vm_state_notify(1);
|
6933 | efe75411 | ths | qemu_rearm_alarm_timer(alarm_timer); |
6934 | 8a7ddc38 | bellard | } |
6935 | 8a7ddc38 | bellard | } |
6936 | 8a7ddc38 | bellard | |
6937 | 5fafdf24 | ths | void vm_stop(int reason) |
6938 | 8a7ddc38 | bellard | { |
6939 | 8a7ddc38 | bellard | if (vm_running) {
|
6940 | 8a7ddc38 | bellard | cpu_disable_ticks(); |
6941 | 8a7ddc38 | bellard | vm_running = 0;
|
6942 | 8a7ddc38 | bellard | if (reason != 0) { |
6943 | 8a7ddc38 | bellard | if (vm_stop_cb) {
|
6944 | 8a7ddc38 | bellard | vm_stop_cb(vm_stop_opaque, reason); |
6945 | 8a7ddc38 | bellard | } |
6946 | 34865134 | bellard | } |
6947 | 0bd48850 | bellard | vm_state_notify(0);
|
6948 | 8a7ddc38 | bellard | } |
6949 | 8a7ddc38 | bellard | } |
6950 | 8a7ddc38 | bellard | |
6951 | bb0c6722 | bellard | /* reset/shutdown handler */
|
6952 | bb0c6722 | bellard | |
6953 | bb0c6722 | bellard | typedef struct QEMUResetEntry { |
6954 | bb0c6722 | bellard | QEMUResetHandler *func; |
6955 | bb0c6722 | bellard | void *opaque;
|
6956 | bb0c6722 | bellard | struct QEMUResetEntry *next;
|
6957 | bb0c6722 | bellard | } QEMUResetEntry; |
6958 | bb0c6722 | bellard | |
6959 | bb0c6722 | bellard | static QEMUResetEntry *first_reset_entry;
|
6960 | bb0c6722 | bellard | static int reset_requested; |
6961 | bb0c6722 | bellard | static int shutdown_requested; |
6962 | 3475187d | bellard | static int powerdown_requested; |
6963 | bb0c6722 | bellard | |
6964 | cf7a2fe2 | aurel32 | int qemu_shutdown_requested(void) |
6965 | cf7a2fe2 | aurel32 | { |
6966 | cf7a2fe2 | aurel32 | int r = shutdown_requested;
|
6967 | cf7a2fe2 | aurel32 | shutdown_requested = 0;
|
6968 | cf7a2fe2 | aurel32 | return r;
|
6969 | cf7a2fe2 | aurel32 | } |
6970 | cf7a2fe2 | aurel32 | |
6971 | cf7a2fe2 | aurel32 | int qemu_reset_requested(void) |
6972 | cf7a2fe2 | aurel32 | { |
6973 | cf7a2fe2 | aurel32 | int r = reset_requested;
|
6974 | cf7a2fe2 | aurel32 | reset_requested = 0;
|
6975 | cf7a2fe2 | aurel32 | return r;
|
6976 | cf7a2fe2 | aurel32 | } |
6977 | cf7a2fe2 | aurel32 | |
6978 | cf7a2fe2 | aurel32 | int qemu_powerdown_requested(void) |
6979 | cf7a2fe2 | aurel32 | { |
6980 | cf7a2fe2 | aurel32 | int r = powerdown_requested;
|
6981 | cf7a2fe2 | aurel32 | powerdown_requested = 0;
|
6982 | cf7a2fe2 | aurel32 | return r;
|
6983 | cf7a2fe2 | aurel32 | } |
6984 | cf7a2fe2 | aurel32 | |
6985 | bb0c6722 | bellard | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
6986 | bb0c6722 | bellard | { |
6987 | bb0c6722 | bellard | QEMUResetEntry **pre, *re; |
6988 | bb0c6722 | bellard | |
6989 | bb0c6722 | bellard | pre = &first_reset_entry; |
6990 | bb0c6722 | bellard | while (*pre != NULL) |
6991 | bb0c6722 | bellard | pre = &(*pre)->next; |
6992 | bb0c6722 | bellard | re = qemu_mallocz(sizeof(QEMUResetEntry));
|
6993 | bb0c6722 | bellard | re->func = func; |
6994 | bb0c6722 | bellard | re->opaque = opaque; |
6995 | bb0c6722 | bellard | re->next = NULL;
|
6996 | bb0c6722 | bellard | *pre = re; |
6997 | bb0c6722 | bellard | } |
6998 | bb0c6722 | bellard | |
6999 | cf7a2fe2 | aurel32 | void qemu_system_reset(void) |
7000 | bb0c6722 | bellard | { |
7001 | bb0c6722 | bellard | QEMUResetEntry *re; |
7002 | bb0c6722 | bellard | |
7003 | bb0c6722 | bellard | /* reset all devices */
|
7004 | bb0c6722 | bellard | for(re = first_reset_entry; re != NULL; re = re->next) { |
7005 | bb0c6722 | bellard | re->func(re->opaque); |
7006 | bb0c6722 | bellard | } |
7007 | bb0c6722 | bellard | } |
7008 | bb0c6722 | bellard | |
7009 | bb0c6722 | bellard | void qemu_system_reset_request(void) |
7010 | bb0c6722 | bellard | { |
7011 | d1beab82 | bellard | if (no_reboot) {
|
7012 | d1beab82 | bellard | shutdown_requested = 1;
|
7013 | d1beab82 | bellard | } else {
|
7014 | d1beab82 | bellard | reset_requested = 1;
|
7015 | d1beab82 | bellard | } |
7016 | 6a00d601 | bellard | if (cpu_single_env)
|
7017 | 6a00d601 | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
7018 | bb0c6722 | bellard | } |
7019 | bb0c6722 | bellard | |
7020 | bb0c6722 | bellard | void qemu_system_shutdown_request(void) |
7021 | bb0c6722 | bellard | { |
7022 | bb0c6722 | bellard | shutdown_requested = 1;
|
7023 | 6a00d601 | bellard | if (cpu_single_env)
|
7024 | 6a00d601 | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
7025 | bb0c6722 | bellard | } |
7026 | bb0c6722 | bellard | |
7027 | 3475187d | bellard | void qemu_system_powerdown_request(void) |
7028 | 3475187d | bellard | { |
7029 | 3475187d | bellard | powerdown_requested = 1;
|
7030 | 6a00d601 | bellard | if (cpu_single_env)
|
7031 | 6a00d601 | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
7032 | bb0c6722 | bellard | } |
7033 | bb0c6722 | bellard | |
7034 | 5905b2e5 | bellard | void main_loop_wait(int timeout) |
7035 | 8a7ddc38 | bellard | { |
7036 | cafffd40 | ths | IOHandlerRecord *ioh; |
7037 | e035649e | bellard | fd_set rfds, wfds, xfds; |
7038 | 877cf882 | ths | int ret, nfds;
|
7039 | 877cf882 | ths | #ifdef _WIN32
|
7040 | 877cf882 | ths | int ret2, i;
|
7041 | 877cf882 | ths | #endif
|
7042 | fd1dff4b | bellard | struct timeval tv;
|
7043 | f331110f | bellard | PollingEntry *pe; |
7044 | f331110f | bellard | |
7045 | c4b1fcc0 | bellard | |
7046 | f331110f | bellard | /* XXX: need to suppress polling by better using win32 events */
|
7047 | f331110f | bellard | ret = 0;
|
7048 | f331110f | bellard | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
7049 | f331110f | bellard | ret |= pe->func(pe->opaque); |
7050 | f331110f | bellard | } |
7051 | 38e205a2 | bellard | #ifdef _WIN32
|
7052 | e6b1e558 | ths | if (ret == 0) { |
7053 | a18e524a | bellard | int err;
|
7054 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
7055 | 3b46e624 | ths | |
7056 | a18e524a | bellard | ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout); |
7057 | a18e524a | bellard | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
7058 | a18e524a | bellard | if (w->func[ret - WAIT_OBJECT_0])
|
7059 | a18e524a | bellard | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
7060 | 3b46e624 | ths | |
7061 | 5fafdf24 | ths | /* Check for additional signaled events */
|
7062 | e6b1e558 | ths | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
7063 | 3b46e624 | ths | |
7064 | e6b1e558 | ths | /* Check if event is signaled */
|
7065 | e6b1e558 | ths | ret2 = WaitForSingleObject(w->events[i], 0);
|
7066 | e6b1e558 | ths | if(ret2 == WAIT_OBJECT_0) {
|
7067 | e6b1e558 | ths | if (w->func[i])
|
7068 | e6b1e558 | ths | w->func[i](w->opaque[i]); |
7069 | e6b1e558 | ths | } else if (ret2 == WAIT_TIMEOUT) { |
7070 | e6b1e558 | ths | } else {
|
7071 | e6b1e558 | ths | err = GetLastError(); |
7072 | e6b1e558 | ths | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
|
7073 | 3b46e624 | ths | } |
7074 | 3b46e624 | ths | } |
7075 | a18e524a | bellard | } else if (ret == WAIT_TIMEOUT) { |
7076 | a18e524a | bellard | } else {
|
7077 | a18e524a | bellard | err = GetLastError(); |
7078 | e6b1e558 | ths | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
|
7079 | a18e524a | bellard | } |
7080 | f331110f | bellard | } |
7081 | fd1dff4b | bellard | #endif
|
7082 | fd1dff4b | bellard | /* poll any events */
|
7083 | fd1dff4b | bellard | /* XXX: separate device handlers from system ones */
|
7084 | fd1dff4b | bellard | nfds = -1;
|
7085 | fd1dff4b | bellard | FD_ZERO(&rfds); |
7086 | fd1dff4b | bellard | FD_ZERO(&wfds); |
7087 | e035649e | bellard | FD_ZERO(&xfds); |
7088 | fd1dff4b | bellard | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
7089 | cafffd40 | ths | if (ioh->deleted)
|
7090 | cafffd40 | ths | continue;
|
7091 | fd1dff4b | bellard | if (ioh->fd_read &&
|
7092 | fd1dff4b | bellard | (!ioh->fd_read_poll || |
7093 | fd1dff4b | bellard | ioh->fd_read_poll(ioh->opaque) != 0)) {
|
7094 | fd1dff4b | bellard | FD_SET(ioh->fd, &rfds); |
7095 | fd1dff4b | bellard | if (ioh->fd > nfds)
|
7096 | fd1dff4b | bellard | nfds = ioh->fd; |
7097 | fd1dff4b | bellard | } |
7098 | fd1dff4b | bellard | if (ioh->fd_write) {
|
7099 | fd1dff4b | bellard | FD_SET(ioh->fd, &wfds); |
7100 | fd1dff4b | bellard | if (ioh->fd > nfds)
|
7101 | fd1dff4b | bellard | nfds = ioh->fd; |
7102 | fd1dff4b | bellard | } |
7103 | fd1dff4b | bellard | } |
7104 | 3b46e624 | ths | |
7105 | fd1dff4b | bellard | tv.tv_sec = 0;
|
7106 | fd1dff4b | bellard | #ifdef _WIN32
|
7107 | fd1dff4b | bellard | tv.tv_usec = 0;
|
7108 | 38e205a2 | bellard | #else
|
7109 | fd1dff4b | bellard | tv.tv_usec = timeout * 1000;
|
7110 | fd1dff4b | bellard | #endif
|
7111 | e035649e | bellard | #if defined(CONFIG_SLIRP)
|
7112 | e035649e | bellard | if (slirp_inited) {
|
7113 | e035649e | bellard | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
7114 | e035649e | bellard | } |
7115 | e035649e | bellard | #endif
|
7116 | e035649e | bellard | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
|
7117 | fd1dff4b | bellard | if (ret > 0) { |
7118 | cafffd40 | ths | IOHandlerRecord **pioh; |
7119 | cafffd40 | ths | |
7120 | cafffd40 | ths | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
7121 | 6ab43fdc | ths | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
|
7122 | fd1dff4b | bellard | ioh->fd_read(ioh->opaque); |
7123 | 7c9d8e07 | bellard | } |
7124 | 6ab43fdc | ths | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
|
7125 | fd1dff4b | bellard | ioh->fd_write(ioh->opaque); |
7126 | c4b1fcc0 | bellard | } |
7127 | b4608c04 | bellard | } |
7128 | cafffd40 | ths | |
7129 | cafffd40 | ths | /* remove deleted IO handlers */
|
7130 | cafffd40 | ths | pioh = &first_io_handler; |
7131 | cafffd40 | ths | while (*pioh) {
|
7132 | cafffd40 | ths | ioh = *pioh; |
7133 | cafffd40 | ths | if (ioh->deleted) {
|
7134 | cafffd40 | ths | *pioh = ioh->next; |
7135 | cafffd40 | ths | qemu_free(ioh); |
7136 | 5fafdf24 | ths | } else
|
7137 | cafffd40 | ths | pioh = &ioh->next; |
7138 | cafffd40 | ths | } |
7139 | fd1dff4b | bellard | } |
7140 | c20709aa | bellard | #if defined(CONFIG_SLIRP)
|
7141 | fd1dff4b | bellard | if (slirp_inited) {
|
7142 | e035649e | bellard | if (ret < 0) { |
7143 | e035649e | bellard | FD_ZERO(&rfds); |
7144 | e035649e | bellard | FD_ZERO(&wfds); |
7145 | e035649e | bellard | FD_ZERO(&xfds); |
7146 | c20709aa | bellard | } |
7147 | e035649e | bellard | slirp_select_poll(&rfds, &wfds, &xfds); |
7148 | fd1dff4b | bellard | } |
7149 | c20709aa | bellard | #endif
|
7150 | 83f64091 | bellard | qemu_aio_poll(); |
7151 | c20709aa | bellard | |
7152 | fd1dff4b | bellard | if (vm_running) {
|
7153 | 21b20814 | edgar_igl | if (likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
|
7154 | 5fafdf24 | ths | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
7155 | fd1dff4b | bellard | qemu_get_clock(vm_clock)); |
7156 | fd1dff4b | bellard | /* run dma transfers, if any */
|
7157 | fd1dff4b | bellard | DMA_run(); |
7158 | fd1dff4b | bellard | } |
7159 | 423f0742 | pbrook | |
7160 | fd1dff4b | bellard | /* real time timers */
|
7161 | 5fafdf24 | ths | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
7162 | fd1dff4b | bellard | qemu_get_clock(rt_clock)); |
7163 | 423f0742 | pbrook | |
7164 | d5d08334 | balrog | if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
|
7165 | d5d08334 | balrog | alarm_timer->flags &= ~(ALARM_FLAG_EXPIRED); |
7166 | d5d08334 | balrog | qemu_rearm_alarm_timer(alarm_timer); |
7167 | d5d08334 | balrog | } |
7168 | b99dc0d1 | balrog | |
7169 | 423f0742 | pbrook | /* Check bottom-halves last in case any of the earlier events triggered
|
7170 | 423f0742 | pbrook | them. */
|
7171 | 423f0742 | pbrook | qemu_bh_poll(); |
7172 | 3b46e624 | ths | |
7173 | 5905b2e5 | bellard | } |
7174 | 5905b2e5 | bellard | |
7175 | 9596ebb7 | pbrook | static int main_loop(void) |
7176 | 5905b2e5 | bellard | { |
7177 | 5905b2e5 | bellard | int ret, timeout;
|
7178 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7179 | 89bfc105 | bellard | int64_t ti; |
7180 | 89bfc105 | bellard | #endif
|
7181 | 6a00d601 | bellard | CPUState *env; |
7182 | 5905b2e5 | bellard | |
7183 | 6a00d601 | bellard | cur_cpu = first_cpu; |
7184 | ee5605e5 | balrog | next_cpu = cur_cpu->next_cpu ?: first_cpu; |
7185 | 5905b2e5 | bellard | for(;;) {
|
7186 | 5905b2e5 | bellard | if (vm_running) {
|
7187 | 15a76449 | bellard | |
7188 | 15a76449 | bellard | for(;;) {
|
7189 | 15a76449 | bellard | /* get next cpu */
|
7190 | ee5605e5 | balrog | env = next_cpu; |
7191 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7192 | 89bfc105 | bellard | ti = profile_getclock(); |
7193 | 89bfc105 | bellard | #endif
|
7194 | 2e70f6ef | pbrook | if (use_icount) {
|
7195 | 2e70f6ef | pbrook | int64_t count; |
7196 | 2e70f6ef | pbrook | int decr;
|
7197 | 2e70f6ef | pbrook | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
7198 | 2e70f6ef | pbrook | env->icount_decr.u16.low = 0;
|
7199 | 2e70f6ef | pbrook | env->icount_extra = 0;
|
7200 | 2e70f6ef | pbrook | count = qemu_next_deadline(); |
7201 | 2e70f6ef | pbrook | count = (count + (1 << icount_time_shift) - 1) |
7202 | 2e70f6ef | pbrook | >> icount_time_shift; |
7203 | 2e70f6ef | pbrook | qemu_icount += count; |
7204 | 2e70f6ef | pbrook | decr = (count > 0xffff) ? 0xffff : count; |
7205 | 2e70f6ef | pbrook | count -= decr; |
7206 | 2e70f6ef | pbrook | env->icount_decr.u16.low = decr; |
7207 | 2e70f6ef | pbrook | env->icount_extra = count; |
7208 | 2e70f6ef | pbrook | } |
7209 | 6a00d601 | bellard | ret = cpu_exec(env); |
7210 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7211 | 89bfc105 | bellard | qemu_time += profile_getclock() - ti; |
7212 | 89bfc105 | bellard | #endif
|
7213 | 2e70f6ef | pbrook | if (use_icount) {
|
7214 | 2e70f6ef | pbrook | /* Fold pending instructions back into the
|
7215 | 2e70f6ef | pbrook | instruction counter, and clear the interrupt flag. */
|
7216 | 2e70f6ef | pbrook | qemu_icount -= (env->icount_decr.u16.low |
7217 | 2e70f6ef | pbrook | + env->icount_extra); |
7218 | 2e70f6ef | pbrook | env->icount_decr.u32 = 0;
|
7219 | 2e70f6ef | pbrook | env->icount_extra = 0;
|
7220 | 2e70f6ef | pbrook | } |
7221 | ee5605e5 | balrog | next_cpu = env->next_cpu ?: first_cpu; |
7222 | 95b01009 | aurel32 | if (event_pending && likely(ret != EXCP_DEBUG)) {
|
7223 | ee5605e5 | balrog | ret = EXCP_INTERRUPT; |
7224 | ee5605e5 | balrog | event_pending = 0;
|
7225 | ee5605e5 | balrog | break;
|
7226 | ee5605e5 | balrog | } |
7227 | bd967e05 | pbrook | if (ret == EXCP_HLT) {
|
7228 | bd967e05 | pbrook | /* Give the next CPU a chance to run. */
|
7229 | bd967e05 | pbrook | cur_cpu = env; |
7230 | bd967e05 | pbrook | continue;
|
7231 | bd967e05 | pbrook | } |
7232 | 15a76449 | bellard | if (ret != EXCP_HALTED)
|
7233 | 15a76449 | bellard | break;
|
7234 | 15a76449 | bellard | /* all CPUs are halted ? */
|
7235 | bd967e05 | pbrook | if (env == cur_cpu)
|
7236 | 15a76449 | bellard | break;
|
7237 | 15a76449 | bellard | } |
7238 | 15a76449 | bellard | cur_cpu = env; |
7239 | 15a76449 | bellard | |
7240 | 5905b2e5 | bellard | if (shutdown_requested) {
|
7241 | 3475187d | bellard | ret = EXCP_INTERRUPT; |
7242 | b2f76161 | aurel32 | if (no_shutdown) {
|
7243 | b2f76161 | aurel32 | vm_stop(0);
|
7244 | b2f76161 | aurel32 | no_shutdown = 0;
|
7245 | b2f76161 | aurel32 | } |
7246 | b2f76161 | aurel32 | else
|
7247 | b2f76161 | aurel32 | break;
|
7248 | 5905b2e5 | bellard | } |
7249 | 5905b2e5 | bellard | if (reset_requested) {
|
7250 | 5905b2e5 | bellard | reset_requested = 0;
|
7251 | 5905b2e5 | bellard | qemu_system_reset(); |
7252 | 3475187d | bellard | ret = EXCP_INTERRUPT; |
7253 | 3475187d | bellard | } |
7254 | 3475187d | bellard | if (powerdown_requested) {
|
7255 | 3475187d | bellard | powerdown_requested = 0;
|
7256 | 3475187d | bellard | qemu_system_powerdown(); |
7257 | 3475187d | bellard | ret = EXCP_INTERRUPT; |
7258 | 5905b2e5 | bellard | } |
7259 | 95b01009 | aurel32 | if (unlikely(ret == EXCP_DEBUG)) {
|
7260 | 5905b2e5 | bellard | vm_stop(EXCP_DEBUG); |
7261 | 5905b2e5 | bellard | } |
7262 | bd967e05 | pbrook | /* If all cpus are halted then wait until the next IRQ */
|
7263 | 5905b2e5 | bellard | /* XXX: use timeout computed from timers */
|
7264 | 2e70f6ef | pbrook | if (ret == EXCP_HALTED) {
|
7265 | 2e70f6ef | pbrook | if (use_icount) {
|
7266 | 2e70f6ef | pbrook | int64_t add; |
7267 | 2e70f6ef | pbrook | int64_t delta; |
7268 | 2e70f6ef | pbrook | /* Advance virtual time to the next event. */
|
7269 | 2e70f6ef | pbrook | if (use_icount == 1) { |
7270 | 2e70f6ef | pbrook | /* When not using an adaptive execution frequency
|
7271 | 2e70f6ef | pbrook | we tend to get badly out of sync with real time,
|
7272 | bf20dc07 | ths | so just delay for a reasonable amount of time. */
|
7273 | 2e70f6ef | pbrook | delta = 0;
|
7274 | 2e70f6ef | pbrook | } else {
|
7275 | 2e70f6ef | pbrook | delta = cpu_get_icount() - cpu_get_clock(); |
7276 | 2e70f6ef | pbrook | } |
7277 | 2e70f6ef | pbrook | if (delta > 0) { |
7278 | 2e70f6ef | pbrook | /* If virtual time is ahead of real time then just
|
7279 | 2e70f6ef | pbrook | wait for IO. */
|
7280 | 2e70f6ef | pbrook | timeout = (delta / 1000000) + 1; |
7281 | 2e70f6ef | pbrook | } else {
|
7282 | 2e70f6ef | pbrook | /* Wait for either IO to occur or the next
|
7283 | 2e70f6ef | pbrook | timer event. */
|
7284 | 2e70f6ef | pbrook | add = qemu_next_deadline(); |
7285 | 2e70f6ef | pbrook | /* We advance the timer before checking for IO.
|
7286 | 2e70f6ef | pbrook | Limit the amount we advance so that early IO
|
7287 | 2e70f6ef | pbrook | activity won't get the guest too far ahead. */
|
7288 | 2e70f6ef | pbrook | if (add > 10000000) |
7289 | 2e70f6ef | pbrook | add = 10000000;
|
7290 | 2e70f6ef | pbrook | delta += add; |
7291 | 2e70f6ef | pbrook | add = (add + (1 << icount_time_shift) - 1) |
7292 | 2e70f6ef | pbrook | >> icount_time_shift; |
7293 | 2e70f6ef | pbrook | qemu_icount += add; |
7294 | 2e70f6ef | pbrook | timeout = delta / 1000000;
|
7295 | 2e70f6ef | pbrook | if (timeout < 0) |
7296 | 2e70f6ef | pbrook | timeout = 0;
|
7297 | 2e70f6ef | pbrook | } |
7298 | 2e70f6ef | pbrook | } else {
|
7299 | 2e70f6ef | pbrook | timeout = 10;
|
7300 | 2e70f6ef | pbrook | } |
7301 | 2e70f6ef | pbrook | } else {
|
7302 | 5905b2e5 | bellard | timeout = 0;
|
7303 | 2e70f6ef | pbrook | } |
7304 | 5905b2e5 | bellard | } else {
|
7305 | 5905b2e5 | bellard | timeout = 10;
|
7306 | 5905b2e5 | bellard | } |
7307 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7308 | 89bfc105 | bellard | ti = profile_getclock(); |
7309 | 89bfc105 | bellard | #endif
|
7310 | 5905b2e5 | bellard | main_loop_wait(timeout); |
7311 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7312 | 89bfc105 | bellard | dev_time += profile_getclock() - ti; |
7313 | 89bfc105 | bellard | #endif
|
7314 | b4608c04 | bellard | } |
7315 | 34865134 | bellard | cpu_disable_ticks(); |
7316 | 34865134 | bellard | return ret;
|
7317 | b4608c04 | bellard | } |
7318 | b4608c04 | bellard | |
7319 | 15f82208 | ths | static void help(int exitcode) |
7320 | 0824d6fc | bellard | { |
7321 | 68d0f70e | bellard | printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n" |
7322 | 0db63474 | bellard | "usage: %s [options] [disk_image]\n"
|
7323 | 0824d6fc | bellard | "\n"
|
7324 | a20dd508 | bellard | "'disk_image' is a raw hard image image for IDE hard disk 0\n"
|
7325 | fc01f7e7 | bellard | "\n"
|
7326 | a20dd508 | bellard | "Standard options:\n"
|
7327 | cc1daa40 | bellard | "-M machine select emulated machine (-M ? for list)\n"
|
7328 | 5adb4839 | pbrook | "-cpu cpu select CPU (-cpu ? for list)\n"
|
7329 | c45886db | bellard | "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
|
7330 | 36b486bb | bellard | "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
|
7331 | 36b486bb | bellard | "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
|
7332 | c4b1fcc0 | bellard | "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
|
7333 | a1620fac | aurel32 | "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
|
7334 | a1620fac | aurel32 | " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
|
7335 | a1620fac | aurel32 | " [,cache=on|off][,format=f]\n"
|
7336 | e4bcb14c | ths | " use 'file' as a drive image\n"
|
7337 | 3e3d5815 | balrog | "-mtdblock file use 'file' as on-board Flash memory image\n"
|
7338 | a1bb27b1 | pbrook | "-sd file use 'file' as SecureDigital card image\n"
|
7339 | 86f55663 | j_mayer | "-pflash file use 'file' as a parallel flash image\n"
|
7340 | eec85c2a | ths | "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
|
7341 | 667accab | ths | "-snapshot write to temporary files instead of disk image files\n"
|
7342 | 667accab | ths | #ifdef CONFIG_SDL
|
7343 | 43523e93 | ths | "-no-frame open SDL window without a frame and window decorations\n"
|
7344 | 3780e197 | ths | "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
|
7345 | 667accab | ths | "-no-quit disable SDL window close capability\n"
|
7346 | 667accab | ths | #endif
|
7347 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7348 | 52ca8d6a | bellard | "-no-fd-bootchk disable boot signature checking for floppy disks\n"
|
7349 | 52ca8d6a | bellard | #endif
|
7350 | a00bad7e | bellard | "-m megs set virtual RAM size to megs MB [default=%d]\n"
|
7351 | 91fc2119 | bellard | "-smp n set the number of CPUs to 'n' [default=1]\n"
|
7352 | c4b1fcc0 | bellard | "-nographic disable graphical output and redirect serial I/Os to console\n"
|
7353 | a171fe39 | balrog | "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
|
7354 | 4ca0074c | bellard | #ifndef _WIN32
|
7355 | 667accab | ths | "-k language use keyboard layout (for example \"fr\" for French)\n"
|
7356 | 4ca0074c | bellard | #endif
|
7357 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7358 | 1d14ffa9 | bellard | "-audio-help print list of audio drivers and their options\n"
|
7359 | c0fe3827 | bellard | "-soundhw c1,... enable audio support\n"
|
7360 | c0fe3827 | bellard | " and only specified sound cards (comma separated list)\n"
|
7361 | c0fe3827 | bellard | " use -soundhw ? to get the list of supported cards\n"
|
7362 | 6a36d84e | bellard | " use -soundhw all to enable all of them\n"
|
7363 | 1d14ffa9 | bellard | #endif
|
7364 | 89980284 | bellard | "-localtime set the real time clock to local time [default=utc]\n"
|
7365 | d63d307f | bellard | "-full-screen start in full screen\n"
|
7366 | a09db21f | bellard | #ifdef TARGET_I386
|
7367 | a09db21f | bellard | "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
|
7368 | a09db21f | bellard | #endif
|
7369 | b389dbfb | bellard | "-usb enable the USB driver (will be the default soon)\n"
|
7370 | b389dbfb | bellard | "-usbdevice name add the host or guest USB device 'name'\n"
|
7371 | 6f7e9aec | bellard | #if defined(TARGET_PPC) || defined(TARGET_SPARC)
|
7372 | 6f7e9aec | bellard | "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
|
7373 | bb0c6722 | bellard | #endif
|
7374 | c35734b2 | ths | "-name string set the name of the guest\n"
|
7375 | c4b1fcc0 | bellard | "\n"
|
7376 | c4b1fcc0 | bellard | "Network options:\n"
|
7377 | a41b2ff2 | pbrook | "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
|
7378 | 7c9d8e07 | bellard | " create a new Network Interface Card and connect it to VLAN 'n'\n"
|
7379 | c20709aa | bellard | #ifdef CONFIG_SLIRP
|
7380 | 115defd1 | pbrook | "-net user[,vlan=n][,hostname=host]\n"
|
7381 | 115defd1 | pbrook | " connect the user mode network stack to VLAN 'n' and send\n"
|
7382 | 115defd1 | pbrook | " hostname 'host' to DHCP clients\n"
|
7383 | 7c9d8e07 | bellard | #endif
|
7384 | 7fb843f8 | bellard | #ifdef _WIN32
|
7385 | 7fb843f8 | bellard | "-net tap[,vlan=n],ifname=name\n"
|
7386 | 7fb843f8 | bellard | " connect the host TAP network interface to VLAN 'n'\n"
|
7387 | 7fb843f8 | bellard | #else
|
7388 | b46a8906 | ths | "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
|
7389 | b46a8906 | ths | " connect the host TAP network interface to VLAN 'n' and use the\n"
|
7390 | b46a8906 | ths | " network scripts 'file' (default=%s)\n"
|
7391 | b46a8906 | ths | " and 'dfile' (default=%s);\n"
|
7392 | b46a8906 | ths | " use '[down]script=no' to disable script execution;\n"
|
7393 | 7c9d8e07 | bellard | " use 'fd=h' to connect to an already opened TAP interface\n"
|
7394 | 7fb843f8 | bellard | #endif
|
7395 | 6a00d601 | bellard | "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
|
7396 | 7c9d8e07 | bellard | " connect the vlan 'n' to another VLAN using a socket connection\n"
|
7397 | 3d830459 | bellard | "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
|
7398 | 3d830459 | bellard | " connect the vlan 'n' to multicast maddr and port\n"
|
7399 | 7c9d8e07 | bellard | "-net none use it alone to have zero network devices; if no -net option\n"
|
7400 | 7c9d8e07 | bellard | " is provided, the default is '-net nic -net user'\n"
|
7401 | 7c9d8e07 | bellard | "\n"
|
7402 | 7c9d8e07 | bellard | #ifdef CONFIG_SLIRP
|
7403 | 0db1137d | ths | "-tftp dir allow tftp access to files in dir [-net user]\n"
|
7404 | 47d5d01a | ths | "-bootp file advertise file in BOOTP replies\n"
|
7405 | 7c9d8e07 | bellard | #ifndef _WIN32
|
7406 | 7c9d8e07 | bellard | "-smb dir allow SMB access to files in 'dir' [-net user]\n"
|
7407 | c94c8d64 | bellard | #endif
|
7408 | 9bf05444 | bellard | "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
|
7409 | 7c9d8e07 | bellard | " redirect TCP or UDP connections from host to guest [-net user]\n"
|
7410 | c20709aa | bellard | #endif
|
7411 | a20dd508 | bellard | "\n"
|
7412 | c4b1fcc0 | bellard | "Linux boot specific:\n"
|
7413 | a20dd508 | bellard | "-kernel bzImage use 'bzImage' as kernel image\n"
|
7414 | a20dd508 | bellard | "-append cmdline use 'cmdline' as kernel command line\n"
|
7415 | a20dd508 | bellard | "-initrd file use 'file' as initial ram disk\n"
|
7416 | fc01f7e7 | bellard | "\n"
|
7417 | 330d0414 | bellard | "Debug/Expert options:\n"
|
7418 | 82c643ff | bellard | "-monitor dev redirect the monitor to char device 'dev'\n"
|
7419 | 82c643ff | bellard | "-serial dev redirect the serial port to char device 'dev'\n"
|
7420 | 6508fe59 | bellard | "-parallel dev redirect the parallel port to char device 'dev'\n"
|
7421 | f7cce898 | bellard | "-pidfile file Write PID to 'file'\n"
|
7422 | cd6f1169 | bellard | "-S freeze CPU at startup (use 'c' to start execution)\n"
|
7423 | cfc3475a | pbrook | "-s wait gdb connection to port\n"
|
7424 | cfc3475a | pbrook | "-p port set gdb connection port [default=%s]\n"
|
7425 | f193c797 | bellard | "-d item1,... output log to %s (use -d ? for a list of log items)\n"
|
7426 | 46d4767d | bellard | "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
|
7427 | 46d4767d | bellard | " translation (t=none or lba) (usually qemu can guess them)\n"
|
7428 | 87b47350 | bellard | "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
|
7429 | d993e026 | bellard | #ifdef USE_KQEMU
|
7430 | 6515b203 | bellard | "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
|
7431 | d993e026 | bellard | "-no-kqemu disable KQEMU kernel module usage\n"
|
7432 | d993e026 | bellard | #endif
|
7433 | bb0c6722 | bellard | #ifdef TARGET_I386
|
7434 | 1bfe856e | bellard | "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
|
7435 | 1bfe856e | bellard | " (default is CL-GD5446 PCI VGA)\n"
|
7436 | 6515b203 | bellard | "-no-acpi disable ACPI\n"
|
7437 | bb0c6722 | bellard | #endif
|
7438 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
7439 | 4d3b6f6e | balrog | "-curses use a curses/ncurses interface instead of SDL\n"
|
7440 | 4d3b6f6e | balrog | #endif
|
7441 | d1beab82 | bellard | "-no-reboot exit instead of rebooting\n"
|
7442 | b2f76161 | aurel32 | "-no-shutdown stop before shutdown\n"
|
7443 | a8080006 | aurel32 | "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
|
7444 | 24236869 | bellard | "-vnc display start a VNC server on display\n"
|
7445 | 71e3ceb8 | ths | #ifndef _WIN32
|
7446 | 71e3ceb8 | ths | "-daemonize daemonize QEMU after initializing\n"
|
7447 | 71e3ceb8 | ths | #endif
|
7448 | 9ae02555 | ths | "-option-rom rom load a file, rom, into the option ROM space\n"
|
7449 | 66508601 | blueswir1 | #ifdef TARGET_SPARC
|
7450 | 66508601 | blueswir1 | "-prom-env variable=value set OpenBIOS nvram variables\n"
|
7451 | 66508601 | blueswir1 | #endif
|
7452 | f3dcfada | ths | "-clock force the use of the given methods for timer alarm.\n"
|
7453 | 3adda04c | aurel32 | " To see what timers are available use -clock ?\n"
|
7454 | bce61846 | bellard | "-startdate select initial date of the clock\n"
|
7455 | 2e70f6ef | pbrook | "-icount [N|auto]\n"
|
7456 | dd5d6fe9 | pbrook | " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
|
7457 | 0824d6fc | bellard | "\n"
|
7458 | 82c643ff | bellard | "During emulation, the following keys are useful:\n"
|
7459 | 032a8c9e | bellard | "ctrl-alt-f toggle full screen\n"
|
7460 | 032a8c9e | bellard | "ctrl-alt-n switch to virtual console 'n'\n"
|
7461 | 032a8c9e | bellard | "ctrl-alt toggle mouse and keyboard grab\n"
|
7462 | 82c643ff | bellard | "\n"
|
7463 | 82c643ff | bellard | "When using -nographic, press 'ctrl-a h' to get some help.\n"
|
7464 | 82c643ff | bellard | , |
7465 | 0db63474 | bellard | "qemu",
|
7466 | a00bad7e | bellard | DEFAULT_RAM_SIZE, |
7467 | 7c9d8e07 | bellard | #ifndef _WIN32
|
7468 | a00bad7e | bellard | DEFAULT_NETWORK_SCRIPT, |
7469 | b46a8906 | ths | DEFAULT_NETWORK_DOWN_SCRIPT, |
7470 | 7c9d8e07 | bellard | #endif
|
7471 | 6e44ba7f | bellard | DEFAULT_GDBSTUB_PORT, |
7472 | bce61846 | bellard | "/tmp/qemu.log");
|
7473 | 15f82208 | ths | exit(exitcode); |
7474 | 0824d6fc | bellard | } |
7475 | 0824d6fc | bellard | |
7476 | cd6f1169 | bellard | #define HAS_ARG 0x0001 |
7477 | cd6f1169 | bellard | |
7478 | cd6f1169 | bellard | enum {
|
7479 | cd6f1169 | bellard | QEMU_OPTION_h, |
7480 | cd6f1169 | bellard | |
7481 | cc1daa40 | bellard | QEMU_OPTION_M, |
7482 | 94fc95cd | j_mayer | QEMU_OPTION_cpu, |
7483 | cd6f1169 | bellard | QEMU_OPTION_fda, |
7484 | cd6f1169 | bellard | QEMU_OPTION_fdb, |
7485 | cd6f1169 | bellard | QEMU_OPTION_hda, |
7486 | cd6f1169 | bellard | QEMU_OPTION_hdb, |
7487 | cd6f1169 | bellard | QEMU_OPTION_hdc, |
7488 | cd6f1169 | bellard | QEMU_OPTION_hdd, |
7489 | e4bcb14c | ths | QEMU_OPTION_drive, |
7490 | cd6f1169 | bellard | QEMU_OPTION_cdrom, |
7491 | 3e3d5815 | balrog | QEMU_OPTION_mtdblock, |
7492 | a1bb27b1 | pbrook | QEMU_OPTION_sd, |
7493 | 86f55663 | j_mayer | QEMU_OPTION_pflash, |
7494 | cd6f1169 | bellard | QEMU_OPTION_boot, |
7495 | cd6f1169 | bellard | QEMU_OPTION_snapshot, |
7496 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7497 | 52ca8d6a | bellard | QEMU_OPTION_no_fd_bootchk, |
7498 | 52ca8d6a | bellard | #endif
|
7499 | cd6f1169 | bellard | QEMU_OPTION_m, |
7500 | cd6f1169 | bellard | QEMU_OPTION_nographic, |
7501 | a171fe39 | balrog | QEMU_OPTION_portrait, |
7502 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7503 | 1d14ffa9 | bellard | QEMU_OPTION_audio_help, |
7504 | 1d14ffa9 | bellard | QEMU_OPTION_soundhw, |
7505 | 1d14ffa9 | bellard | #endif
|
7506 | cd6f1169 | bellard | |
7507 | 7c9d8e07 | bellard | QEMU_OPTION_net, |
7508 | c7f74643 | bellard | QEMU_OPTION_tftp, |
7509 | 47d5d01a | ths | QEMU_OPTION_bootp, |
7510 | 9d728e8c | bellard | QEMU_OPTION_smb, |
7511 | 9bf05444 | bellard | QEMU_OPTION_redir, |
7512 | cd6f1169 | bellard | |
7513 | cd6f1169 | bellard | QEMU_OPTION_kernel, |
7514 | cd6f1169 | bellard | QEMU_OPTION_append, |
7515 | cd6f1169 | bellard | QEMU_OPTION_initrd, |
7516 | cd6f1169 | bellard | |
7517 | cd6f1169 | bellard | QEMU_OPTION_S, |
7518 | cd6f1169 | bellard | QEMU_OPTION_s, |
7519 | cd6f1169 | bellard | QEMU_OPTION_p, |
7520 | cd6f1169 | bellard | QEMU_OPTION_d, |
7521 | cd6f1169 | bellard | QEMU_OPTION_hdachs, |
7522 | cd6f1169 | bellard | QEMU_OPTION_L, |
7523 | 1192dad8 | j_mayer | QEMU_OPTION_bios, |
7524 | 3d11d0eb | bellard | QEMU_OPTION_k, |
7525 | ee22c2f7 | bellard | QEMU_OPTION_localtime, |
7526 | 1f04275e | bellard | QEMU_OPTION_cirrusvga, |
7527 | d34cab9f | ths | QEMU_OPTION_vmsvga, |
7528 | e9b137c2 | bellard | QEMU_OPTION_g, |
7529 | 1bfe856e | bellard | QEMU_OPTION_std_vga, |
7530 | 20d8a3ed | ths | QEMU_OPTION_echr, |
7531 | 82c643ff | bellard | QEMU_OPTION_monitor, |
7532 | 82c643ff | bellard | QEMU_OPTION_serial, |
7533 | 6508fe59 | bellard | QEMU_OPTION_parallel, |
7534 | d63d307f | bellard | QEMU_OPTION_loadvm, |
7535 | d63d307f | bellard | QEMU_OPTION_full_screen, |
7536 | 43523e93 | ths | QEMU_OPTION_no_frame, |
7537 | 3780e197 | ths | QEMU_OPTION_alt_grab, |
7538 | 667accab | ths | QEMU_OPTION_no_quit, |
7539 | f7cce898 | bellard | QEMU_OPTION_pidfile, |
7540 | d993e026 | bellard | QEMU_OPTION_no_kqemu, |
7541 | 89bfc105 | bellard | QEMU_OPTION_kernel_kqemu, |
7542 | a09db21f | bellard | QEMU_OPTION_win2k_hack, |
7543 | bb36d470 | bellard | QEMU_OPTION_usb, |
7544 | a594cfbf | bellard | QEMU_OPTION_usbdevice, |
7545 | 6a00d601 | bellard | QEMU_OPTION_smp, |
7546 | 24236869 | bellard | QEMU_OPTION_vnc, |
7547 | 6515b203 | bellard | QEMU_OPTION_no_acpi, |
7548 | 4d3b6f6e | balrog | QEMU_OPTION_curses, |
7549 | d1beab82 | bellard | QEMU_OPTION_no_reboot, |
7550 | b2f76161 | aurel32 | QEMU_OPTION_no_shutdown, |
7551 | 9467cd46 | balrog | QEMU_OPTION_show_cursor, |
7552 | 71e3ceb8 | ths | QEMU_OPTION_daemonize, |
7553 | 9ae02555 | ths | QEMU_OPTION_option_rom, |
7554 | c35734b2 | ths | QEMU_OPTION_semihosting, |
7555 | c35734b2 | ths | QEMU_OPTION_name, |
7556 | 66508601 | blueswir1 | QEMU_OPTION_prom_env, |
7557 | 2b8f2d41 | balrog | QEMU_OPTION_old_param, |
7558 | f3dcfada | ths | QEMU_OPTION_clock, |
7559 | 7e0af5d0 | bellard | QEMU_OPTION_startdate, |
7560 | 26a5f13b | bellard | QEMU_OPTION_tb_size, |
7561 | 2e70f6ef | pbrook | QEMU_OPTION_icount, |
7562 | cd6f1169 | bellard | }; |
7563 | cd6f1169 | bellard | |
7564 | cd6f1169 | bellard | typedef struct QEMUOption { |
7565 | cd6f1169 | bellard | const char *name; |
7566 | cd6f1169 | bellard | int flags;
|
7567 | cd6f1169 | bellard | int index;
|
7568 | cd6f1169 | bellard | } QEMUOption; |
7569 | cd6f1169 | bellard | |
7570 | cd6f1169 | bellard | const QEMUOption qemu_options[] = {
|
7571 | cd6f1169 | bellard | { "h", 0, QEMU_OPTION_h }, |
7572 | 64423fb2 | pbrook | { "help", 0, QEMU_OPTION_h }, |
7573 | cd6f1169 | bellard | |
7574 | cc1daa40 | bellard | { "M", HAS_ARG, QEMU_OPTION_M },
|
7575 | 94fc95cd | j_mayer | { "cpu", HAS_ARG, QEMU_OPTION_cpu },
|
7576 | cd6f1169 | bellard | { "fda", HAS_ARG, QEMU_OPTION_fda },
|
7577 | cd6f1169 | bellard | { "fdb", HAS_ARG, QEMU_OPTION_fdb },
|
7578 | cd6f1169 | bellard | { "hda", HAS_ARG, QEMU_OPTION_hda },
|
7579 | cd6f1169 | bellard | { "hdb", HAS_ARG, QEMU_OPTION_hdb },
|
7580 | cd6f1169 | bellard | { "hdc", HAS_ARG, QEMU_OPTION_hdc },
|
7581 | cd6f1169 | bellard | { "hdd", HAS_ARG, QEMU_OPTION_hdd },
|
7582 | e4bcb14c | ths | { "drive", HAS_ARG, QEMU_OPTION_drive },
|
7583 | cd6f1169 | bellard | { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
|
7584 | 3e3d5815 | balrog | { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
|
7585 | a1bb27b1 | pbrook | { "sd", HAS_ARG, QEMU_OPTION_sd },
|
7586 | 86f55663 | j_mayer | { "pflash", HAS_ARG, QEMU_OPTION_pflash },
|
7587 | cd6f1169 | bellard | { "boot", HAS_ARG, QEMU_OPTION_boot },
|
7588 | cd6f1169 | bellard | { "snapshot", 0, QEMU_OPTION_snapshot }, |
7589 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7590 | 52ca8d6a | bellard | { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk }, |
7591 | 52ca8d6a | bellard | #endif
|
7592 | cd6f1169 | bellard | { "m", HAS_ARG, QEMU_OPTION_m },
|
7593 | cd6f1169 | bellard | { "nographic", 0, QEMU_OPTION_nographic }, |
7594 | a171fe39 | balrog | { "portrait", 0, QEMU_OPTION_portrait }, |
7595 | 3d11d0eb | bellard | { "k", HAS_ARG, QEMU_OPTION_k },
|
7596 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7597 | 1d14ffa9 | bellard | { "audio-help", 0, QEMU_OPTION_audio_help }, |
7598 | 1d14ffa9 | bellard | { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
|
7599 | 1d14ffa9 | bellard | #endif
|
7600 | cd6f1169 | bellard | |
7601 | 7c9d8e07 | bellard | { "net", HAS_ARG, QEMU_OPTION_net},
|
7602 | 158156d1 | bellard | #ifdef CONFIG_SLIRP
|
7603 | c7f74643 | bellard | { "tftp", HAS_ARG, QEMU_OPTION_tftp },
|
7604 | 47d5d01a | ths | { "bootp", HAS_ARG, QEMU_OPTION_bootp },
|
7605 | c94c8d64 | bellard | #ifndef _WIN32
|
7606 | 9d728e8c | bellard | { "smb", HAS_ARG, QEMU_OPTION_smb },
|
7607 | c94c8d64 | bellard | #endif
|
7608 | 9bf05444 | bellard | { "redir", HAS_ARG, QEMU_OPTION_redir },
|
7609 | 158156d1 | bellard | #endif
|
7610 | cd6f1169 | bellard | |
7611 | cd6f1169 | bellard | { "kernel", HAS_ARG, QEMU_OPTION_kernel },
|
7612 | cd6f1169 | bellard | { "append", HAS_ARG, QEMU_OPTION_append },
|
7613 | cd6f1169 | bellard | { "initrd", HAS_ARG, QEMU_OPTION_initrd },
|
7614 | cd6f1169 | bellard | |
7615 | cd6f1169 | bellard | { "S", 0, QEMU_OPTION_S }, |
7616 | cd6f1169 | bellard | { "s", 0, QEMU_OPTION_s }, |
7617 | cd6f1169 | bellard | { "p", HAS_ARG, QEMU_OPTION_p },
|
7618 | cd6f1169 | bellard | { "d", HAS_ARG, QEMU_OPTION_d },
|
7619 | cd6f1169 | bellard | { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
|
7620 | cd6f1169 | bellard | { "L", HAS_ARG, QEMU_OPTION_L },
|
7621 | 1192dad8 | j_mayer | { "bios", HAS_ARG, QEMU_OPTION_bios },
|
7622 | d993e026 | bellard | #ifdef USE_KQEMU
|
7623 | d993e026 | bellard | { "no-kqemu", 0, QEMU_OPTION_no_kqemu }, |
7624 | 89bfc105 | bellard | { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu }, |
7625 | d993e026 | bellard | #endif
|
7626 | 6f7e9aec | bellard | #if defined(TARGET_PPC) || defined(TARGET_SPARC)
|
7627 | e9b137c2 | bellard | { "g", 1, QEMU_OPTION_g }, |
7628 | 77d4bc34 | bellard | #endif
|
7629 | ee22c2f7 | bellard | { "localtime", 0, QEMU_OPTION_localtime }, |
7630 | 1bfe856e | bellard | { "std-vga", 0, QEMU_OPTION_std_vga }, |
7631 | 8b6e0729 | balrog | { "echr", HAS_ARG, QEMU_OPTION_echr },
|
7632 | 8b6e0729 | balrog | { "monitor", HAS_ARG, QEMU_OPTION_monitor },
|
7633 | 8b6e0729 | balrog | { "serial", HAS_ARG, QEMU_OPTION_serial },
|
7634 | 8b6e0729 | balrog | { "parallel", HAS_ARG, QEMU_OPTION_parallel },
|
7635 | d63d307f | bellard | { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
|
7636 | d63d307f | bellard | { "full-screen", 0, QEMU_OPTION_full_screen }, |
7637 | 667accab | ths | #ifdef CONFIG_SDL
|
7638 | 43523e93 | ths | { "no-frame", 0, QEMU_OPTION_no_frame }, |
7639 | 3780e197 | ths | { "alt-grab", 0, QEMU_OPTION_alt_grab }, |
7640 | 667accab | ths | { "no-quit", 0, QEMU_OPTION_no_quit }, |
7641 | 667accab | ths | #endif
|
7642 | f7cce898 | bellard | { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
|
7643 | a09db21f | bellard | { "win2k-hack", 0, QEMU_OPTION_win2k_hack }, |
7644 | a594cfbf | bellard | { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
|
7645 | 6a00d601 | bellard | { "smp", HAS_ARG, QEMU_OPTION_smp },
|
7646 | 24236869 | bellard | { "vnc", HAS_ARG, QEMU_OPTION_vnc },
|
7647 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
7648 | 4d3b6f6e | balrog | { "curses", 0, QEMU_OPTION_curses }, |
7649 | 4d3b6f6e | balrog | #endif
|
7650 | 96d30e48 | ths | |
7651 | 1f04275e | bellard | /* temporary options */
|
7652 | a594cfbf | bellard | { "usb", 0, QEMU_OPTION_usb }, |
7653 | 1f04275e | bellard | { "cirrusvga", 0, QEMU_OPTION_cirrusvga }, |
7654 | d34cab9f | ths | { "vmwarevga", 0, QEMU_OPTION_vmsvga }, |
7655 | 6515b203 | bellard | { "no-acpi", 0, QEMU_OPTION_no_acpi }, |
7656 | d1beab82 | bellard | { "no-reboot", 0, QEMU_OPTION_no_reboot }, |
7657 | b2f76161 | aurel32 | { "no-shutdown", 0, QEMU_OPTION_no_shutdown }, |
7658 | 9467cd46 | balrog | { "show-cursor", 0, QEMU_OPTION_show_cursor }, |
7659 | 71e3ceb8 | ths | { "daemonize", 0, QEMU_OPTION_daemonize }, |
7660 | 9ae02555 | ths | { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
|
7661 | a87295e8 | pbrook | #if defined(TARGET_ARM) || defined(TARGET_M68K)
|
7662 | 8e71621f | pbrook | { "semihosting", 0, QEMU_OPTION_semihosting }, |
7663 | 8e71621f | pbrook | #endif
|
7664 | c35734b2 | ths | { "name", HAS_ARG, QEMU_OPTION_name },
|
7665 | 66508601 | blueswir1 | #if defined(TARGET_SPARC)
|
7666 | 66508601 | blueswir1 | { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
|
7667 | 66508601 | blueswir1 | #endif
|
7668 | 2b8f2d41 | balrog | #if defined(TARGET_ARM)
|
7669 | 2b8f2d41 | balrog | { "old-param", 0, QEMU_OPTION_old_param }, |
7670 | 2b8f2d41 | balrog | #endif
|
7671 | f3dcfada | ths | { "clock", HAS_ARG, QEMU_OPTION_clock },
|
7672 | 7e0af5d0 | bellard | { "startdate", HAS_ARG, QEMU_OPTION_startdate },
|
7673 | 26a5f13b | bellard | { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
|
7674 | 2e70f6ef | pbrook | { "icount", HAS_ARG, QEMU_OPTION_icount },
|
7675 | cd6f1169 | bellard | { NULL },
|
7676 | fc01f7e7 | bellard | }; |
7677 | fc01f7e7 | bellard | |
7678 | 5905b2e5 | bellard | /* password input */
|
7679 | 5905b2e5 | bellard | |
7680 | 2bac6019 | balrog | int qemu_key_check(BlockDriverState *bs, const char *name) |
7681 | 2bac6019 | balrog | { |
7682 | 2bac6019 | balrog | char password[256]; |
7683 | 2bac6019 | balrog | int i;
|
7684 | 2bac6019 | balrog | |
7685 | 2bac6019 | balrog | if (!bdrv_is_encrypted(bs))
|
7686 | 2bac6019 | balrog | return 0; |
7687 | 2bac6019 | balrog | |
7688 | 2bac6019 | balrog | term_printf("%s is encrypted.\n", name);
|
7689 | 2bac6019 | balrog | for(i = 0; i < 3; i++) { |
7690 | 2bac6019 | balrog | monitor_readline("Password: ", 1, password, sizeof(password)); |
7691 | 2bac6019 | balrog | if (bdrv_set_key(bs, password) == 0) |
7692 | 2bac6019 | balrog | return 0; |
7693 | 2bac6019 | balrog | term_printf("invalid password\n");
|
7694 | 2bac6019 | balrog | } |
7695 | 2bac6019 | balrog | return -EPERM;
|
7696 | 2bac6019 | balrog | } |
7697 | 2bac6019 | balrog | |
7698 | 5905b2e5 | bellard | static BlockDriverState *get_bdrv(int index) |
7699 | 5905b2e5 | bellard | { |
7700 | e4bcb14c | ths | if (index > nb_drives)
|
7701 | e4bcb14c | ths | return NULL; |
7702 | e4bcb14c | ths | return drives_table[index].bdrv;
|
7703 | 5905b2e5 | bellard | } |
7704 | 5905b2e5 | bellard | |
7705 | 5905b2e5 | bellard | static void read_passwords(void) |
7706 | 5905b2e5 | bellard | { |
7707 | 5905b2e5 | bellard | BlockDriverState *bs; |
7708 | 2bac6019 | balrog | int i;
|
7709 | 5905b2e5 | bellard | |
7710 | 5905b2e5 | bellard | for(i = 0; i < 6; i++) { |
7711 | 5905b2e5 | bellard | bs = get_bdrv(i); |
7712 | 2bac6019 | balrog | if (bs)
|
7713 | 2bac6019 | balrog | qemu_key_check(bs, bdrv_get_device_name(bs)); |
7714 | 5905b2e5 | bellard | } |
7715 | 5905b2e5 | bellard | } |
7716 | 5905b2e5 | bellard | |
7717 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7718 | 6a36d84e | bellard | struct soundhw soundhw[] = {
|
7719 | b00052e4 | balrog | #ifdef HAS_AUDIO_CHOICE
|
7720 | 4ce7ff6e | aurel32 | #if defined(TARGET_I386) || defined(TARGET_MIPS)
|
7721 | fd06c375 | bellard | { |
7722 | fd06c375 | bellard | "pcspk",
|
7723 | fd06c375 | bellard | "PC speaker",
|
7724 | fd06c375 | bellard | 0,
|
7725 | fd06c375 | bellard | 1,
|
7726 | fd06c375 | bellard | { .init_isa = pcspk_audio_init } |
7727 | fd06c375 | bellard | }, |
7728 | fd06c375 | bellard | #endif
|
7729 | 6a36d84e | bellard | { |
7730 | 6a36d84e | bellard | "sb16",
|
7731 | 6a36d84e | bellard | "Creative Sound Blaster 16",
|
7732 | 6a36d84e | bellard | 0,
|
7733 | 6a36d84e | bellard | 1,
|
7734 | 6a36d84e | bellard | { .init_isa = SB16_init } |
7735 | 6a36d84e | bellard | }, |
7736 | 6a36d84e | bellard | |
7737 | cc53d26d | malc | #ifdef CONFIG_CS4231A
|
7738 | cc53d26d | malc | { |
7739 | cc53d26d | malc | "cs4231a",
|
7740 | cc53d26d | malc | "CS4231A",
|
7741 | cc53d26d | malc | 0,
|
7742 | cc53d26d | malc | 1,
|
7743 | cc53d26d | malc | { .init_isa = cs4231a_init } |
7744 | cc53d26d | malc | }, |
7745 | cc53d26d | malc | #endif
|
7746 | cc53d26d | malc | |
7747 | 1d14ffa9 | bellard | #ifdef CONFIG_ADLIB
|
7748 | 6a36d84e | bellard | { |
7749 | 6a36d84e | bellard | "adlib",
|
7750 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
7751 | 6a36d84e | bellard | "Yamaha YMF262 (OPL3)",
|
7752 | 1d14ffa9 | bellard | #else
|
7753 | 6a36d84e | bellard | "Yamaha YM3812 (OPL2)",
|
7754 | 1d14ffa9 | bellard | #endif
|
7755 | 6a36d84e | bellard | 0,
|
7756 | 6a36d84e | bellard | 1,
|
7757 | 6a36d84e | bellard | { .init_isa = Adlib_init } |
7758 | 6a36d84e | bellard | }, |
7759 | 1d14ffa9 | bellard | #endif
|
7760 | 6a36d84e | bellard | |
7761 | 1d14ffa9 | bellard | #ifdef CONFIG_GUS
|
7762 | 6a36d84e | bellard | { |
7763 | 6a36d84e | bellard | "gus",
|
7764 | 6a36d84e | bellard | "Gravis Ultrasound GF1",
|
7765 | 6a36d84e | bellard | 0,
|
7766 | 6a36d84e | bellard | 1,
|
7767 | 6a36d84e | bellard | { .init_isa = GUS_init } |
7768 | 6a36d84e | bellard | }, |
7769 | 1d14ffa9 | bellard | #endif
|
7770 | 6a36d84e | bellard | |
7771 | e5c9a13e | balrog | #ifdef CONFIG_AC97
|
7772 | e5c9a13e | balrog | { |
7773 | e5c9a13e | balrog | "ac97",
|
7774 | e5c9a13e | balrog | "Intel 82801AA AC97 Audio",
|
7775 | e5c9a13e | balrog | 0,
|
7776 | e5c9a13e | balrog | 0,
|
7777 | e5c9a13e | balrog | { .init_pci = ac97_init } |
7778 | e5c9a13e | balrog | }, |
7779 | e5c9a13e | balrog | #endif
|
7780 | e5c9a13e | balrog | |
7781 | 6a36d84e | bellard | { |
7782 | 6a36d84e | bellard | "es1370",
|
7783 | 6a36d84e | bellard | "ENSONIQ AudioPCI ES1370",
|
7784 | 6a36d84e | bellard | 0,
|
7785 | 6a36d84e | bellard | 0,
|
7786 | 6a36d84e | bellard | { .init_pci = es1370_init } |
7787 | 6a36d84e | bellard | }, |
7788 | b00052e4 | balrog | #endif
|
7789 | 6a36d84e | bellard | |
7790 | 6a36d84e | bellard | { NULL, NULL, 0, 0, { NULL } } |
7791 | 6a36d84e | bellard | }; |
7792 | 6a36d84e | bellard | |
7793 | 6a36d84e | bellard | static void select_soundhw (const char *optarg) |
7794 | 6a36d84e | bellard | { |
7795 | 6a36d84e | bellard | struct soundhw *c;
|
7796 | 6a36d84e | bellard | |
7797 | 6a36d84e | bellard | if (*optarg == '?') { |
7798 | 6a36d84e | bellard | show_valid_cards:
|
7799 | 6a36d84e | bellard | |
7800 | 6a36d84e | bellard | printf ("Valid sound card names (comma separated):\n");
|
7801 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
7802 | 6a36d84e | bellard | printf ("%-11s %s\n", c->name, c->descr);
|
7803 | 6a36d84e | bellard | } |
7804 | 6a36d84e | bellard | printf ("\n-soundhw all will enable all of the above\n");
|
7805 | 1d14ffa9 | bellard | exit (*optarg != '?');
|
7806 | 1d14ffa9 | bellard | } |
7807 | 1d14ffa9 | bellard | else {
|
7808 | 6a36d84e | bellard | size_t l; |
7809 | 1d14ffa9 | bellard | const char *p; |
7810 | 1d14ffa9 | bellard | char *e;
|
7811 | 1d14ffa9 | bellard | int bad_card = 0; |
7812 | 1d14ffa9 | bellard | |
7813 | 6a36d84e | bellard | if (!strcmp (optarg, "all")) { |
7814 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
7815 | 6a36d84e | bellard | c->enabled = 1;
|
7816 | 6a36d84e | bellard | } |
7817 | 6a36d84e | bellard | return;
|
7818 | 6a36d84e | bellard | } |
7819 | 1d14ffa9 | bellard | |
7820 | 6a36d84e | bellard | p = optarg; |
7821 | 1d14ffa9 | bellard | while (*p) {
|
7822 | 1d14ffa9 | bellard | e = strchr (p, ',');
|
7823 | 1d14ffa9 | bellard | l = !e ? strlen (p) : (size_t) (e - p); |
7824 | 6a36d84e | bellard | |
7825 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
7826 | 6a36d84e | bellard | if (!strncmp (c->name, p, l)) {
|
7827 | 6a36d84e | bellard | c->enabled = 1;
|
7828 | 1d14ffa9 | bellard | break;
|
7829 | 1d14ffa9 | bellard | } |
7830 | 1d14ffa9 | bellard | } |
7831 | 6a36d84e | bellard | |
7832 | 6a36d84e | bellard | if (!c->name) {
|
7833 | 1d14ffa9 | bellard | if (l > 80) { |
7834 | 1d14ffa9 | bellard | fprintf (stderr, |
7835 | 1d14ffa9 | bellard | "Unknown sound card name (too big to show)\n");
|
7836 | 1d14ffa9 | bellard | } |
7837 | 1d14ffa9 | bellard | else {
|
7838 | 1d14ffa9 | bellard | fprintf (stderr, "Unknown sound card name `%.*s'\n",
|
7839 | 1d14ffa9 | bellard | (int) l, p);
|
7840 | 1d14ffa9 | bellard | } |
7841 | 1d14ffa9 | bellard | bad_card = 1;
|
7842 | 1d14ffa9 | bellard | } |
7843 | 1d14ffa9 | bellard | p += l + (e != NULL);
|
7844 | 1d14ffa9 | bellard | } |
7845 | 1d14ffa9 | bellard | |
7846 | 1d14ffa9 | bellard | if (bad_card)
|
7847 | 1d14ffa9 | bellard | goto show_valid_cards;
|
7848 | 1d14ffa9 | bellard | } |
7849 | 1d14ffa9 | bellard | } |
7850 | 1d14ffa9 | bellard | #endif
|
7851 | 1d14ffa9 | bellard | |
7852 | 3587d7e6 | bellard | #ifdef _WIN32
|
7853 | 3587d7e6 | bellard | static BOOL WINAPI qemu_ctrl_handler(DWORD type)
|
7854 | 3587d7e6 | bellard | { |
7855 | 3587d7e6 | bellard | exit(STATUS_CONTROL_C_EXIT); |
7856 | 3587d7e6 | bellard | return TRUE;
|
7857 | 3587d7e6 | bellard | } |
7858 | 3587d7e6 | bellard | #endif
|
7859 | 3587d7e6 | bellard | |
7860 | 7c9d8e07 | bellard | #define MAX_NET_CLIENTS 32 |
7861 | c20709aa | bellard | |
7862 | 0824d6fc | bellard | int main(int argc, char **argv) |
7863 | 0824d6fc | bellard | { |
7864 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
7865 | cfc3475a | pbrook | int use_gdbstub;
|
7866 | cfc3475a | pbrook | const char *gdbstub_port; |
7867 | 67b915a5 | bellard | #endif
|
7868 | 28c5af54 | j_mayer | uint32_t boot_devices_bitmap = 0;
|
7869 | e4bcb14c | ths | int i;
|
7870 | 28c5af54 | j_mayer | int snapshot, linux_boot, net_boot;
|
7871 | 7f7f9873 | bellard | const char *initrd_filename; |
7872 | a20dd508 | bellard | const char *kernel_filename, *kernel_cmdline; |
7873 | 28c5af54 | j_mayer | const char *boot_devices = ""; |
7874 | 313aa567 | bellard | DisplayState *ds = &display_state; |
7875 | 46d4767d | bellard | int cyls, heads, secs, translation;
|
7876 | fd5f393a | pbrook | const char *net_clients[MAX_NET_CLIENTS]; |
7877 | 7c9d8e07 | bellard | int nb_net_clients;
|
7878 | e4bcb14c | ths | int hda_index;
|
7879 | cd6f1169 | bellard | int optind;
|
7880 | cd6f1169 | bellard | const char *r, *optarg; |
7881 | 82c643ff | bellard | CharDriverState *monitor_hd; |
7882 | fd5f393a | pbrook | const char *monitor_device; |
7883 | fd5f393a | pbrook | const char *serial_devices[MAX_SERIAL_PORTS]; |
7884 | 8d11df9e | bellard | int serial_device_index;
|
7885 | fd5f393a | pbrook | const char *parallel_devices[MAX_PARALLEL_PORTS]; |
7886 | 6508fe59 | bellard | int parallel_device_index;
|
7887 | d63d307f | bellard | const char *loadvm = NULL; |
7888 | cc1daa40 | bellard | QEMUMachine *machine; |
7889 | 94fc95cd | j_mayer | const char *cpu_model; |
7890 | fd5f393a | pbrook | const char *usb_devices[MAX_USB_CMDLINE]; |
7891 | a594cfbf | bellard | int usb_devices_index;
|
7892 | 71e3ceb8 | ths | int fds[2]; |
7893 | 26a5f13b | bellard | int tb_size;
|
7894 | 93815bc2 | ths | const char *pid_file = NULL; |
7895 | 833c7174 | blueswir1 | VLANState *vlan; |
7896 | 0bd48850 | bellard | |
7897 | 0bd48850 | bellard | LIST_INIT (&vm_change_state_head); |
7898 | be995c27 | bellard | #ifndef _WIN32
|
7899 | be995c27 | bellard | { |
7900 | be995c27 | bellard | struct sigaction act;
|
7901 | be995c27 | bellard | sigfillset(&act.sa_mask); |
7902 | be995c27 | bellard | act.sa_flags = 0;
|
7903 | be995c27 | bellard | act.sa_handler = SIG_IGN; |
7904 | be995c27 | bellard | sigaction(SIGPIPE, &act, NULL);
|
7905 | be995c27 | bellard | } |
7906 | 3587d7e6 | bellard | #else
|
7907 | 3587d7e6 | bellard | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
7908 | a8e5ac33 | bellard | /* Note: cpu_interrupt() is currently not SMP safe, so we force
|
7909 | a8e5ac33 | bellard | QEMU to run on a single CPU */
|
7910 | a8e5ac33 | bellard | { |
7911 | a8e5ac33 | bellard | HANDLE h; |
7912 | a8e5ac33 | bellard | DWORD mask, smask; |
7913 | a8e5ac33 | bellard | int i;
|
7914 | a8e5ac33 | bellard | h = GetCurrentProcess(); |
7915 | a8e5ac33 | bellard | if (GetProcessAffinityMask(h, &mask, &smask)) {
|
7916 | a8e5ac33 | bellard | for(i = 0; i < 32; i++) { |
7917 | a8e5ac33 | bellard | if (mask & (1 << i)) |
7918 | a8e5ac33 | bellard | break;
|
7919 | a8e5ac33 | bellard | } |
7920 | a8e5ac33 | bellard | if (i != 32) { |
7921 | a8e5ac33 | bellard | mask = 1 << i;
|
7922 | a8e5ac33 | bellard | SetProcessAffinityMask(h, mask); |
7923 | a8e5ac33 | bellard | } |
7924 | a8e5ac33 | bellard | } |
7925 | a8e5ac33 | bellard | } |
7926 | 67b915a5 | bellard | #endif
|
7927 | be995c27 | bellard | |
7928 | cc1daa40 | bellard | register_machines(); |
7929 | cc1daa40 | bellard | machine = first_machine; |
7930 | 94fc95cd | j_mayer | cpu_model = NULL;
|
7931 | fc01f7e7 | bellard | initrd_filename = NULL;
|
7932 | 4fc5d071 | aurel32 | ram_size = 0;
|
7933 | 313aa567 | bellard | vga_ram_size = VGA_RAM_SIZE; |
7934 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
7935 | b4608c04 | bellard | use_gdbstub = 0;
|
7936 | c636bb66 | bellard | gdbstub_port = DEFAULT_GDBSTUB_PORT; |
7937 | 67b915a5 | bellard | #endif
|
7938 | 33e3963e | bellard | snapshot = 0;
|
7939 | a20dd508 | bellard | nographic = 0;
|
7940 | 4d3b6f6e | balrog | curses = 0;
|
7941 | a20dd508 | bellard | kernel_filename = NULL;
|
7942 | a20dd508 | bellard | kernel_cmdline = "";
|
7943 | c4b1fcc0 | bellard | cyls = heads = secs = 0;
|
7944 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_AUTO; |
7945 | c60e08d9 | pbrook | monitor_device = "vc";
|
7946 | c4b1fcc0 | bellard | |
7947 | c75a823c | aurel32 | serial_devices[0] = "vc:80Cx24C"; |
7948 | 8d11df9e | bellard | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
7949 | fd5f393a | pbrook | serial_devices[i] = NULL;
|
7950 | 8d11df9e | bellard | serial_device_index = 0;
|
7951 | 3b46e624 | ths | |
7952 | c75a823c | aurel32 | parallel_devices[0] = "vc:640x480"; |
7953 | 6508fe59 | bellard | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
7954 | fd5f393a | pbrook | parallel_devices[i] = NULL;
|
7955 | 6508fe59 | bellard | parallel_device_index = 0;
|
7956 | 3b46e624 | ths | |
7957 | a594cfbf | bellard | usb_devices_index = 0;
|
7958 | 3b46e624 | ths | |
7959 | 7c9d8e07 | bellard | nb_net_clients = 0;
|
7960 | e4bcb14c | ths | nb_drives = 0;
|
7961 | e4bcb14c | ths | nb_drives_opt = 0;
|
7962 | e4bcb14c | ths | hda_index = -1;
|
7963 | 7c9d8e07 | bellard | |
7964 | 7c9d8e07 | bellard | nb_nics = 0;
|
7965 | 3b46e624 | ths | |
7966 | 26a5f13b | bellard | tb_size = 0;
|
7967 | 26a5f13b | bellard | |
7968 | cd6f1169 | bellard | optind = 1;
|
7969 | 0824d6fc | bellard | for(;;) {
|
7970 | cd6f1169 | bellard | if (optind >= argc)
|
7971 | 0824d6fc | bellard | break;
|
7972 | cd6f1169 | bellard | r = argv[optind]; |
7973 | cd6f1169 | bellard | if (r[0] != '-') { |
7974 | 609497ab | balrog | hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
|
7975 | cd6f1169 | bellard | } else {
|
7976 | cd6f1169 | bellard | const QEMUOption *popt;
|
7977 | cd6f1169 | bellard | |
7978 | cd6f1169 | bellard | optind++; |
7979 | dff5efc8 | pbrook | /* Treat --foo the same as -foo. */
|
7980 | dff5efc8 | pbrook | if (r[1] == '-') |
7981 | dff5efc8 | pbrook | r++; |
7982 | cd6f1169 | bellard | popt = qemu_options; |
7983 | cd6f1169 | bellard | for(;;) {
|
7984 | cd6f1169 | bellard | if (!popt->name) {
|
7985 | 5fafdf24 | ths | fprintf(stderr, "%s: invalid option -- '%s'\n",
|
7986 | cd6f1169 | bellard | argv[0], r);
|
7987 | cd6f1169 | bellard | exit(1);
|
7988 | cd6f1169 | bellard | } |
7989 | cd6f1169 | bellard | if (!strcmp(popt->name, r + 1)) |
7990 | cd6f1169 | bellard | break;
|
7991 | cd6f1169 | bellard | popt++; |
7992 | cd6f1169 | bellard | } |
7993 | cd6f1169 | bellard | if (popt->flags & HAS_ARG) {
|
7994 | cd6f1169 | bellard | if (optind >= argc) {
|
7995 | cd6f1169 | bellard | fprintf(stderr, "%s: option '%s' requires an argument\n",
|
7996 | cd6f1169 | bellard | argv[0], r);
|
7997 | cd6f1169 | bellard | exit(1);
|
7998 | cd6f1169 | bellard | } |
7999 | cd6f1169 | bellard | optarg = argv[optind++]; |
8000 | cd6f1169 | bellard | } else {
|
8001 | cd6f1169 | bellard | optarg = NULL;
|
8002 | cd6f1169 | bellard | } |
8003 | cd6f1169 | bellard | |
8004 | cd6f1169 | bellard | switch(popt->index) {
|
8005 | cc1daa40 | bellard | case QEMU_OPTION_M:
|
8006 | cc1daa40 | bellard | machine = find_machine(optarg); |
8007 | cc1daa40 | bellard | if (!machine) {
|
8008 | cc1daa40 | bellard | QEMUMachine *m; |
8009 | cc1daa40 | bellard | printf("Supported machines are:\n");
|
8010 | cc1daa40 | bellard | for(m = first_machine; m != NULL; m = m->next) { |
8011 | cc1daa40 | bellard | printf("%-10s %s%s\n",
|
8012 | 5fafdf24 | ths | m->name, m->desc, |
8013 | cc1daa40 | bellard | m == first_machine ? " (default)" : ""); |
8014 | cc1daa40 | bellard | } |
8015 | 15f82208 | ths | exit(*optarg != '?');
|
8016 | cc1daa40 | bellard | } |
8017 | cc1daa40 | bellard | break;
|
8018 | 94fc95cd | j_mayer | case QEMU_OPTION_cpu:
|
8019 | 94fc95cd | j_mayer | /* hw initialization will check this */
|
8020 | 15f82208 | ths | if (*optarg == '?') { |
8021 | c732abe2 | j_mayer | /* XXX: implement xxx_cpu_list for targets that still miss it */
|
8022 | c732abe2 | j_mayer | #if defined(cpu_list)
|
8023 | c732abe2 | j_mayer | cpu_list(stdout, &fprintf); |
8024 | 94fc95cd | j_mayer | #endif
|
8025 | 15f82208 | ths | exit(0);
|
8026 | 94fc95cd | j_mayer | } else {
|
8027 | 94fc95cd | j_mayer | cpu_model = optarg; |
8028 | 94fc95cd | j_mayer | } |
8029 | 94fc95cd | j_mayer | break;
|
8030 | cd6f1169 | bellard | case QEMU_OPTION_initrd:
|
8031 | fc01f7e7 | bellard | initrd_filename = optarg; |
8032 | fc01f7e7 | bellard | break;
|
8033 | cd6f1169 | bellard | case QEMU_OPTION_hda:
|
8034 | e4bcb14c | ths | if (cyls == 0) |
8035 | 609497ab | balrog | hda_index = drive_add(optarg, HD_ALIAS, 0);
|
8036 | e4bcb14c | ths | else
|
8037 | 609497ab | balrog | hda_index = drive_add(optarg, HD_ALIAS |
8038 | e4bcb14c | ths | ",cyls=%d,heads=%d,secs=%d%s",
|
8039 | 609497ab | balrog | 0, cyls, heads, secs,
|
8040 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_LBA ? |
8041 | e4bcb14c | ths | ",trans=lba" :
|
8042 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_NONE ? |
8043 | e4bcb14c | ths | ",trans=none" : ""); |
8044 | e4bcb14c | ths | break;
|
8045 | cd6f1169 | bellard | case QEMU_OPTION_hdb:
|
8046 | cc1daa40 | bellard | case QEMU_OPTION_hdc:
|
8047 | cc1daa40 | bellard | case QEMU_OPTION_hdd:
|
8048 | 609497ab | balrog | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
8049 | fc01f7e7 | bellard | break;
|
8050 | e4bcb14c | ths | case QEMU_OPTION_drive:
|
8051 | 609497ab | balrog | drive_add(NULL, "%s", optarg); |
8052 | e4bcb14c | ths | break;
|
8053 | 3e3d5815 | balrog | case QEMU_OPTION_mtdblock:
|
8054 | 609497ab | balrog | drive_add(optarg, MTD_ALIAS); |
8055 | 3e3d5815 | balrog | break;
|
8056 | a1bb27b1 | pbrook | case QEMU_OPTION_sd:
|
8057 | 609497ab | balrog | drive_add(optarg, SD_ALIAS); |
8058 | a1bb27b1 | pbrook | break;
|
8059 | 86f55663 | j_mayer | case QEMU_OPTION_pflash:
|
8060 | 609497ab | balrog | drive_add(optarg, PFLASH_ALIAS); |
8061 | 86f55663 | j_mayer | break;
|
8062 | cd6f1169 | bellard | case QEMU_OPTION_snapshot:
|
8063 | 33e3963e | bellard | snapshot = 1;
|
8064 | 33e3963e | bellard | break;
|
8065 | cd6f1169 | bellard | case QEMU_OPTION_hdachs:
|
8066 | 330d0414 | bellard | { |
8067 | 330d0414 | bellard | const char *p; |
8068 | 330d0414 | bellard | p = optarg; |
8069 | 330d0414 | bellard | cyls = strtol(p, (char **)&p, 0); |
8070 | 46d4767d | bellard | if (cyls < 1 || cyls > 16383) |
8071 | 46d4767d | bellard | goto chs_fail;
|
8072 | 330d0414 | bellard | if (*p != ',') |
8073 | 330d0414 | bellard | goto chs_fail;
|
8074 | 330d0414 | bellard | p++; |
8075 | 330d0414 | bellard | heads = strtol(p, (char **)&p, 0); |
8076 | 46d4767d | bellard | if (heads < 1 || heads > 16) |
8077 | 46d4767d | bellard | goto chs_fail;
|
8078 | 330d0414 | bellard | if (*p != ',') |
8079 | 330d0414 | bellard | goto chs_fail;
|
8080 | 330d0414 | bellard | p++; |
8081 | 330d0414 | bellard | secs = strtol(p, (char **)&p, 0); |
8082 | 46d4767d | bellard | if (secs < 1 || secs > 63) |
8083 | 46d4767d | bellard | goto chs_fail;
|
8084 | 46d4767d | bellard | if (*p == ',') { |
8085 | 46d4767d | bellard | p++; |
8086 | 46d4767d | bellard | if (!strcmp(p, "none")) |
8087 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_NONE; |
8088 | 46d4767d | bellard | else if (!strcmp(p, "lba")) |
8089 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_LBA; |
8090 | 46d4767d | bellard | else if (!strcmp(p, "auto")) |
8091 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_AUTO; |
8092 | 46d4767d | bellard | else
|
8093 | 46d4767d | bellard | goto chs_fail;
|
8094 | 46d4767d | bellard | } else if (*p != '\0') { |
8095 | c4b1fcc0 | bellard | chs_fail:
|
8096 | 46d4767d | bellard | fprintf(stderr, "qemu: invalid physical CHS format\n");
|
8097 | 46d4767d | bellard | exit(1);
|
8098 | c4b1fcc0 | bellard | } |
8099 | e4bcb14c | ths | if (hda_index != -1) |
8100 | 609497ab | balrog | snprintf(drives_opt[hda_index].opt, |
8101 | 609497ab | balrog | sizeof(drives_opt[hda_index].opt),
|
8102 | 609497ab | balrog | HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
|
8103 | 609497ab | balrog | 0, cyls, heads, secs,
|
8104 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_LBA ? |
8105 | e4bcb14c | ths | ",trans=lba" :
|
8106 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_NONE ? |
8107 | e4bcb14c | ths | ",trans=none" : ""); |
8108 | 330d0414 | bellard | } |
8109 | 330d0414 | bellard | break;
|
8110 | cd6f1169 | bellard | case QEMU_OPTION_nographic:
|
8111 | fd5f393a | pbrook | serial_devices[0] = "stdio"; |
8112 | fd5f393a | pbrook | parallel_devices[0] = "null"; |
8113 | fd5f393a | pbrook | monitor_device = "stdio";
|
8114 | a20dd508 | bellard | nographic = 1;
|
8115 | a20dd508 | bellard | break;
|
8116 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
8117 | 4d3b6f6e | balrog | case QEMU_OPTION_curses:
|
8118 | 4d3b6f6e | balrog | curses = 1;
|
8119 | 4d3b6f6e | balrog | break;
|
8120 | 4d3b6f6e | balrog | #endif
|
8121 | a171fe39 | balrog | case QEMU_OPTION_portrait:
|
8122 | a171fe39 | balrog | graphic_rotate = 1;
|
8123 | a171fe39 | balrog | break;
|
8124 | cd6f1169 | bellard | case QEMU_OPTION_kernel:
|
8125 | a20dd508 | bellard | kernel_filename = optarg; |
8126 | a20dd508 | bellard | break;
|
8127 | cd6f1169 | bellard | case QEMU_OPTION_append:
|
8128 | a20dd508 | bellard | kernel_cmdline = optarg; |
8129 | 313aa567 | bellard | break;
|
8130 | cd6f1169 | bellard | case QEMU_OPTION_cdrom:
|
8131 | 609497ab | balrog | drive_add(optarg, CDROM_ALIAS); |
8132 | 36b486bb | bellard | break;
|
8133 | cd6f1169 | bellard | case QEMU_OPTION_boot:
|
8134 | 28c5af54 | j_mayer | boot_devices = optarg; |
8135 | 28c5af54 | j_mayer | /* We just do some generic consistency checks */
|
8136 | 28c5af54 | j_mayer | { |
8137 | 28c5af54 | j_mayer | /* Could easily be extended to 64 devices if needed */
|
8138 | 60fe76f3 | ths | const char *p; |
8139 | 28c5af54 | j_mayer | |
8140 | 28c5af54 | j_mayer | boot_devices_bitmap = 0;
|
8141 | 28c5af54 | j_mayer | for (p = boot_devices; *p != '\0'; p++) { |
8142 | 28c5af54 | j_mayer | /* Allowed boot devices are:
|
8143 | 28c5af54 | j_mayer | * a b : floppy disk drives
|
8144 | 28c5af54 | j_mayer | * c ... f : IDE disk drives
|
8145 | 28c5af54 | j_mayer | * g ... m : machine implementation dependant drives
|
8146 | 28c5af54 | j_mayer | * n ... p : network devices
|
8147 | 28c5af54 | j_mayer | * It's up to each machine implementation to check
|
8148 | 28c5af54 | j_mayer | * if the given boot devices match the actual hardware
|
8149 | 28c5af54 | j_mayer | * implementation and firmware features.
|
8150 | 28c5af54 | j_mayer | */
|
8151 | 28c5af54 | j_mayer | if (*p < 'a' || *p > 'q') { |
8152 | 28c5af54 | j_mayer | fprintf(stderr, "Invalid boot device '%c'\n", *p);
|
8153 | 28c5af54 | j_mayer | exit(1);
|
8154 | 28c5af54 | j_mayer | } |
8155 | 28c5af54 | j_mayer | if (boot_devices_bitmap & (1 << (*p - 'a'))) { |
8156 | 28c5af54 | j_mayer | fprintf(stderr, |
8157 | 28c5af54 | j_mayer | "Boot device '%c' was given twice\n",*p);
|
8158 | 28c5af54 | j_mayer | exit(1);
|
8159 | 28c5af54 | j_mayer | } |
8160 | 28c5af54 | j_mayer | boot_devices_bitmap |= 1 << (*p - 'a'); |
8161 | 28c5af54 | j_mayer | } |
8162 | 36b486bb | bellard | } |
8163 | 36b486bb | bellard | break;
|
8164 | cd6f1169 | bellard | case QEMU_OPTION_fda:
|
8165 | cd6f1169 | bellard | case QEMU_OPTION_fdb:
|
8166 | 609497ab | balrog | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
8167 | c45886db | bellard | break;
|
8168 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
8169 | 52ca8d6a | bellard | case QEMU_OPTION_no_fd_bootchk:
|
8170 | 52ca8d6a | bellard | fd_bootchk = 0;
|
8171 | 52ca8d6a | bellard | break;
|
8172 | 52ca8d6a | bellard | #endif
|
8173 | 7c9d8e07 | bellard | case QEMU_OPTION_net:
|
8174 | 7c9d8e07 | bellard | if (nb_net_clients >= MAX_NET_CLIENTS) {
|
8175 | 7c9d8e07 | bellard | fprintf(stderr, "qemu: too many network clients\n");
|
8176 | c4b1fcc0 | bellard | exit(1);
|
8177 | c4b1fcc0 | bellard | } |
8178 | fd5f393a | pbrook | net_clients[nb_net_clients] = optarg; |
8179 | 7c9d8e07 | bellard | nb_net_clients++; |
8180 | 702c651c | bellard | break;
|
8181 | c7f74643 | bellard | #ifdef CONFIG_SLIRP
|
8182 | c7f74643 | bellard | case QEMU_OPTION_tftp:
|
8183 | c7f74643 | bellard | tftp_prefix = optarg; |
8184 | 9bf05444 | bellard | break;
|
8185 | 47d5d01a | ths | case QEMU_OPTION_bootp:
|
8186 | 47d5d01a | ths | bootp_filename = optarg; |
8187 | 47d5d01a | ths | break;
|
8188 | c94c8d64 | bellard | #ifndef _WIN32
|
8189 | 9d728e8c | bellard | case QEMU_OPTION_smb:
|
8190 | 9d728e8c | bellard | net_slirp_smb(optarg); |
8191 | 9d728e8c | bellard | break;
|
8192 | c94c8d64 | bellard | #endif
|
8193 | 9bf05444 | bellard | case QEMU_OPTION_redir:
|
8194 | 3b46e624 | ths | net_slirp_redir(optarg); |
8195 | 9bf05444 | bellard | break;
|
8196 | c7f74643 | bellard | #endif
|
8197 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
8198 | 1d14ffa9 | bellard | case QEMU_OPTION_audio_help:
|
8199 | 1d14ffa9 | bellard | AUD_help (); |
8200 | 1d14ffa9 | bellard | exit (0);
|
8201 | 1d14ffa9 | bellard | break;
|
8202 | 1d14ffa9 | bellard | case QEMU_OPTION_soundhw:
|
8203 | 1d14ffa9 | bellard | select_soundhw (optarg); |
8204 | 1d14ffa9 | bellard | break;
|
8205 | 1d14ffa9 | bellard | #endif
|
8206 | cd6f1169 | bellard | case QEMU_OPTION_h:
|
8207 | 15f82208 | ths | help(0);
|
8208 | cd6f1169 | bellard | break;
|
8209 | 00f82b8a | aurel32 | case QEMU_OPTION_m: {
|
8210 | 00f82b8a | aurel32 | uint64_t value; |
8211 | 00f82b8a | aurel32 | char *ptr;
|
8212 | 00f82b8a | aurel32 | |
8213 | 00f82b8a | aurel32 | value = strtoul(optarg, &ptr, 10);
|
8214 | 00f82b8a | aurel32 | switch (*ptr) {
|
8215 | 00f82b8a | aurel32 | case 0: case 'M': case 'm': |
8216 | 00f82b8a | aurel32 | value <<= 20;
|
8217 | 00f82b8a | aurel32 | break;
|
8218 | 00f82b8a | aurel32 | case 'G': case 'g': |
8219 | 00f82b8a | aurel32 | value <<= 30;
|
8220 | 00f82b8a | aurel32 | break;
|
8221 | 00f82b8a | aurel32 | default:
|
8222 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
|
8223 | cd6f1169 | bellard | exit(1);
|
8224 | cd6f1169 | bellard | } |
8225 | 00f82b8a | aurel32 | |
8226 | 00f82b8a | aurel32 | /* On 32-bit hosts, QEMU is limited by virtual address space */
|
8227 | 00f82b8a | aurel32 | if (value > (2047 << 20) |
8228 | 00f82b8a | aurel32 | #ifndef USE_KQEMU
|
8229 | 00f82b8a | aurel32 | && HOST_LONG_BITS == 32
|
8230 | 00f82b8a | aurel32 | #endif
|
8231 | 00f82b8a | aurel32 | ) { |
8232 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
|
8233 | 00f82b8a | aurel32 | exit(1);
|
8234 | 00f82b8a | aurel32 | } |
8235 | 00f82b8a | aurel32 | if (value != (uint64_t)(ram_addr_t)value) {
|
8236 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: ram size too large\n");
|
8237 | 00f82b8a | aurel32 | exit(1);
|
8238 | 00f82b8a | aurel32 | } |
8239 | 00f82b8a | aurel32 | ram_size = value; |
8240 | cd6f1169 | bellard | break;
|
8241 | 00f82b8a | aurel32 | } |
8242 | cd6f1169 | bellard | case QEMU_OPTION_d:
|
8243 | cd6f1169 | bellard | { |
8244 | cd6f1169 | bellard | int mask;
|
8245 | cd6f1169 | bellard | CPULogItem *item; |
8246 | 3b46e624 | ths | |
8247 | cd6f1169 | bellard | mask = cpu_str_to_log_mask(optarg); |
8248 | cd6f1169 | bellard | if (!mask) {
|
8249 | cd6f1169 | bellard | printf("Log items (comma separated):\n");
|
8250 | f193c797 | bellard | for(item = cpu_log_items; item->mask != 0; item++) { |
8251 | f193c797 | bellard | printf("%-10s %s\n", item->name, item->help);
|
8252 | f193c797 | bellard | } |
8253 | f193c797 | bellard | exit(1);
|
8254 | cd6f1169 | bellard | } |
8255 | cd6f1169 | bellard | cpu_set_log(mask); |
8256 | f193c797 | bellard | } |
8257 | cd6f1169 | bellard | break;
|
8258 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
8259 | cd6f1169 | bellard | case QEMU_OPTION_s:
|
8260 | cd6f1169 | bellard | use_gdbstub = 1;
|
8261 | cd6f1169 | bellard | break;
|
8262 | cd6f1169 | bellard | case QEMU_OPTION_p:
|
8263 | cfc3475a | pbrook | gdbstub_port = optarg; |
8264 | cd6f1169 | bellard | break;
|
8265 | 67b915a5 | bellard | #endif
|
8266 | cd6f1169 | bellard | case QEMU_OPTION_L:
|
8267 | cd6f1169 | bellard | bios_dir = optarg; |
8268 | cd6f1169 | bellard | break;
|
8269 | 1192dad8 | j_mayer | case QEMU_OPTION_bios:
|
8270 | 1192dad8 | j_mayer | bios_name = optarg; |
8271 | 1192dad8 | j_mayer | break;
|
8272 | cd6f1169 | bellard | case QEMU_OPTION_S:
|
8273 | 3c07f8e8 | pbrook | autostart = 0;
|
8274 | cd6f1169 | bellard | break;
|
8275 | 3d11d0eb | bellard | case QEMU_OPTION_k:
|
8276 | 3d11d0eb | bellard | keyboard_layout = optarg; |
8277 | 3d11d0eb | bellard | break;
|
8278 | ee22c2f7 | bellard | case QEMU_OPTION_localtime:
|
8279 | ee22c2f7 | bellard | rtc_utc = 0;
|
8280 | ee22c2f7 | bellard | break;
|
8281 | 1f04275e | bellard | case QEMU_OPTION_cirrusvga:
|
8282 | 1f04275e | bellard | cirrus_vga_enabled = 1;
|
8283 | d34cab9f | ths | vmsvga_enabled = 0;
|
8284 | d34cab9f | ths | break;
|
8285 | d34cab9f | ths | case QEMU_OPTION_vmsvga:
|
8286 | d34cab9f | ths | cirrus_vga_enabled = 0;
|
8287 | d34cab9f | ths | vmsvga_enabled = 1;
|
8288 | 1f04275e | bellard | break;
|
8289 | 1bfe856e | bellard | case QEMU_OPTION_std_vga:
|
8290 | 1bfe856e | bellard | cirrus_vga_enabled = 0;
|
8291 | d34cab9f | ths | vmsvga_enabled = 0;
|
8292 | 1bfe856e | bellard | break;
|
8293 | e9b137c2 | bellard | case QEMU_OPTION_g:
|
8294 | e9b137c2 | bellard | { |
8295 | e9b137c2 | bellard | const char *p; |
8296 | e9b137c2 | bellard | int w, h, depth;
|
8297 | e9b137c2 | bellard | p = optarg; |
8298 | e9b137c2 | bellard | w = strtol(p, (char **)&p, 10); |
8299 | e9b137c2 | bellard | if (w <= 0) { |
8300 | e9b137c2 | bellard | graphic_error:
|
8301 | e9b137c2 | bellard | fprintf(stderr, "qemu: invalid resolution or depth\n");
|
8302 | e9b137c2 | bellard | exit(1);
|
8303 | e9b137c2 | bellard | } |
8304 | e9b137c2 | bellard | if (*p != 'x') |
8305 | e9b137c2 | bellard | goto graphic_error;
|
8306 | e9b137c2 | bellard | p++; |
8307 | e9b137c2 | bellard | h = strtol(p, (char **)&p, 10); |
8308 | e9b137c2 | bellard | if (h <= 0) |
8309 | e9b137c2 | bellard | goto graphic_error;
|
8310 | e9b137c2 | bellard | if (*p == 'x') { |
8311 | e9b137c2 | bellard | p++; |
8312 | e9b137c2 | bellard | depth = strtol(p, (char **)&p, 10); |
8313 | 5fafdf24 | ths | if (depth != 8 && depth != 15 && depth != 16 && |
8314 | e9b137c2 | bellard | depth != 24 && depth != 32) |
8315 | e9b137c2 | bellard | goto graphic_error;
|
8316 | e9b137c2 | bellard | } else if (*p == '\0') { |
8317 | e9b137c2 | bellard | depth = graphic_depth; |
8318 | e9b137c2 | bellard | } else {
|
8319 | e9b137c2 | bellard | goto graphic_error;
|
8320 | e9b137c2 | bellard | } |
8321 | 3b46e624 | ths | |
8322 | e9b137c2 | bellard | graphic_width = w; |
8323 | e9b137c2 | bellard | graphic_height = h; |
8324 | e9b137c2 | bellard | graphic_depth = depth; |
8325 | e9b137c2 | bellard | } |
8326 | e9b137c2 | bellard | break;
|
8327 | 20d8a3ed | ths | case QEMU_OPTION_echr:
|
8328 | 20d8a3ed | ths | { |
8329 | 20d8a3ed | ths | char *r;
|
8330 | 20d8a3ed | ths | term_escape_char = strtol(optarg, &r, 0);
|
8331 | 20d8a3ed | ths | if (r == optarg)
|
8332 | 20d8a3ed | ths | printf("Bad argument to echr\n");
|
8333 | 20d8a3ed | ths | break;
|
8334 | 20d8a3ed | ths | } |
8335 | 82c643ff | bellard | case QEMU_OPTION_monitor:
|
8336 | fd5f393a | pbrook | monitor_device = optarg; |
8337 | 82c643ff | bellard | break;
|
8338 | 82c643ff | bellard | case QEMU_OPTION_serial:
|
8339 | 8d11df9e | bellard | if (serial_device_index >= MAX_SERIAL_PORTS) {
|
8340 | 8d11df9e | bellard | fprintf(stderr, "qemu: too many serial ports\n");
|
8341 | 8d11df9e | bellard | exit(1);
|
8342 | 8d11df9e | bellard | } |
8343 | fd5f393a | pbrook | serial_devices[serial_device_index] = optarg; |
8344 | 8d11df9e | bellard | serial_device_index++; |
8345 | 82c643ff | bellard | break;
|
8346 | 6508fe59 | bellard | case QEMU_OPTION_parallel:
|
8347 | 6508fe59 | bellard | if (parallel_device_index >= MAX_PARALLEL_PORTS) {
|
8348 | 6508fe59 | bellard | fprintf(stderr, "qemu: too many parallel ports\n");
|
8349 | 6508fe59 | bellard | exit(1);
|
8350 | 6508fe59 | bellard | } |
8351 | fd5f393a | pbrook | parallel_devices[parallel_device_index] = optarg; |
8352 | 6508fe59 | bellard | parallel_device_index++; |
8353 | 6508fe59 | bellard | break;
|
8354 | d63d307f | bellard | case QEMU_OPTION_loadvm:
|
8355 | d63d307f | bellard | loadvm = optarg; |
8356 | d63d307f | bellard | break;
|
8357 | d63d307f | bellard | case QEMU_OPTION_full_screen:
|
8358 | d63d307f | bellard | full_screen = 1;
|
8359 | d63d307f | bellard | break;
|
8360 | 667accab | ths | #ifdef CONFIG_SDL
|
8361 | 43523e93 | ths | case QEMU_OPTION_no_frame:
|
8362 | 43523e93 | ths | no_frame = 1;
|
8363 | 43523e93 | ths | break;
|
8364 | 3780e197 | ths | case QEMU_OPTION_alt_grab:
|
8365 | 3780e197 | ths | alt_grab = 1;
|
8366 | 3780e197 | ths | break;
|
8367 | 667accab | ths | case QEMU_OPTION_no_quit:
|
8368 | 667accab | ths | no_quit = 1;
|
8369 | 667accab | ths | break;
|
8370 | 667accab | ths | #endif
|
8371 | f7cce898 | bellard | case QEMU_OPTION_pidfile:
|
8372 | 93815bc2 | ths | pid_file = optarg; |
8373 | f7cce898 | bellard | break;
|
8374 | a09db21f | bellard | #ifdef TARGET_I386
|
8375 | a09db21f | bellard | case QEMU_OPTION_win2k_hack:
|
8376 | a09db21f | bellard | win2k_install_hack = 1;
|
8377 | a09db21f | bellard | break;
|
8378 | a09db21f | bellard | #endif
|
8379 | d993e026 | bellard | #ifdef USE_KQEMU
|
8380 | d993e026 | bellard | case QEMU_OPTION_no_kqemu:
|
8381 | d993e026 | bellard | kqemu_allowed = 0;
|
8382 | d993e026 | bellard | break;
|
8383 | 89bfc105 | bellard | case QEMU_OPTION_kernel_kqemu:
|
8384 | 89bfc105 | bellard | kqemu_allowed = 2;
|
8385 | 89bfc105 | bellard | break;
|
8386 | d993e026 | bellard | #endif
|
8387 | bb36d470 | bellard | case QEMU_OPTION_usb:
|
8388 | bb36d470 | bellard | usb_enabled = 1;
|
8389 | bb36d470 | bellard | break;
|
8390 | a594cfbf | bellard | case QEMU_OPTION_usbdevice:
|
8391 | a594cfbf | bellard | usb_enabled = 1;
|
8392 | 0d92ed30 | pbrook | if (usb_devices_index >= MAX_USB_CMDLINE) {
|
8393 | a594cfbf | bellard | fprintf(stderr, "Too many USB devices\n");
|
8394 | a594cfbf | bellard | exit(1);
|
8395 | a594cfbf | bellard | } |
8396 | fd5f393a | pbrook | usb_devices[usb_devices_index] = optarg; |
8397 | a594cfbf | bellard | usb_devices_index++; |
8398 | a594cfbf | bellard | break;
|
8399 | 6a00d601 | bellard | case QEMU_OPTION_smp:
|
8400 | 6a00d601 | bellard | smp_cpus = atoi(optarg); |
8401 | ba3c64fb | bellard | if (smp_cpus < 1 || smp_cpus > MAX_CPUS) { |
8402 | 6a00d601 | bellard | fprintf(stderr, "Invalid number of CPUs\n");
|
8403 | 6a00d601 | bellard | exit(1);
|
8404 | 6a00d601 | bellard | } |
8405 | 6a00d601 | bellard | break;
|
8406 | 24236869 | bellard | case QEMU_OPTION_vnc:
|
8407 | 73fc9742 | ths | vnc_display = optarg; |
8408 | 24236869 | bellard | break;
|
8409 | 6515b203 | bellard | case QEMU_OPTION_no_acpi:
|
8410 | 6515b203 | bellard | acpi_enabled = 0;
|
8411 | 6515b203 | bellard | break;
|
8412 | d1beab82 | bellard | case QEMU_OPTION_no_reboot:
|
8413 | d1beab82 | bellard | no_reboot = 1;
|
8414 | d1beab82 | bellard | break;
|
8415 | b2f76161 | aurel32 | case QEMU_OPTION_no_shutdown:
|
8416 | b2f76161 | aurel32 | no_shutdown = 1;
|
8417 | b2f76161 | aurel32 | break;
|
8418 | 9467cd46 | balrog | case QEMU_OPTION_show_cursor:
|
8419 | 9467cd46 | balrog | cursor_hide = 0;
|
8420 | 9467cd46 | balrog | break;
|
8421 | 71e3ceb8 | ths | case QEMU_OPTION_daemonize:
|
8422 | 71e3ceb8 | ths | daemonize = 1;
|
8423 | 71e3ceb8 | ths | break;
|
8424 | 9ae02555 | ths | case QEMU_OPTION_option_rom:
|
8425 | 9ae02555 | ths | if (nb_option_roms >= MAX_OPTION_ROMS) {
|
8426 | 9ae02555 | ths | fprintf(stderr, "Too many option ROMs\n");
|
8427 | 9ae02555 | ths | exit(1);
|
8428 | 9ae02555 | ths | } |
8429 | 9ae02555 | ths | option_rom[nb_option_roms] = optarg; |
8430 | 9ae02555 | ths | nb_option_roms++; |
8431 | 9ae02555 | ths | break;
|
8432 | 8e71621f | pbrook | case QEMU_OPTION_semihosting:
|
8433 | 8e71621f | pbrook | semihosting_enabled = 1;
|
8434 | 8e71621f | pbrook | break;
|
8435 | c35734b2 | ths | case QEMU_OPTION_name:
|
8436 | c35734b2 | ths | qemu_name = optarg; |
8437 | c35734b2 | ths | break;
|
8438 | 66508601 | blueswir1 | #ifdef TARGET_SPARC
|
8439 | 66508601 | blueswir1 | case QEMU_OPTION_prom_env:
|
8440 | 66508601 | blueswir1 | if (nb_prom_envs >= MAX_PROM_ENVS) {
|
8441 | 66508601 | blueswir1 | fprintf(stderr, "Too many prom variables\n");
|
8442 | 66508601 | blueswir1 | exit(1);
|
8443 | 66508601 | blueswir1 | } |
8444 | 66508601 | blueswir1 | prom_envs[nb_prom_envs] = optarg; |
8445 | 66508601 | blueswir1 | nb_prom_envs++; |
8446 | 66508601 | blueswir1 | break;
|
8447 | 66508601 | blueswir1 | #endif
|
8448 | 2b8f2d41 | balrog | #ifdef TARGET_ARM
|
8449 | 2b8f2d41 | balrog | case QEMU_OPTION_old_param:
|
8450 | 2b8f2d41 | balrog | old_param = 1;
|
8451 | 05ebd537 | ths | break;
|
8452 | 2b8f2d41 | balrog | #endif
|
8453 | f3dcfada | ths | case QEMU_OPTION_clock:
|
8454 | f3dcfada | ths | configure_alarms(optarg); |
8455 | f3dcfada | ths | break;
|
8456 | 7e0af5d0 | bellard | case QEMU_OPTION_startdate:
|
8457 | 7e0af5d0 | bellard | { |
8458 | 7e0af5d0 | bellard | struct tm tm;
|
8459 | f6503059 | balrog | time_t rtc_start_date; |
8460 | 7e0af5d0 | bellard | if (!strcmp(optarg, "now")) { |
8461 | f6503059 | balrog | rtc_date_offset = -1;
|
8462 | 7e0af5d0 | bellard | } else {
|
8463 | 7e0af5d0 | bellard | if (sscanf(optarg, "%d-%d-%dT%d:%d:%d", |
8464 | 7e0af5d0 | bellard | &tm.tm_year, |
8465 | 7e0af5d0 | bellard | &tm.tm_mon, |
8466 | 7e0af5d0 | bellard | &tm.tm_mday, |
8467 | 7e0af5d0 | bellard | &tm.tm_hour, |
8468 | 7e0af5d0 | bellard | &tm.tm_min, |
8469 | 7e0af5d0 | bellard | &tm.tm_sec) == 6) {
|
8470 | 7e0af5d0 | bellard | /* OK */
|
8471 | 7e0af5d0 | bellard | } else if (sscanf(optarg, "%d-%d-%d", |
8472 | 7e0af5d0 | bellard | &tm.tm_year, |
8473 | 7e0af5d0 | bellard | &tm.tm_mon, |
8474 | 7e0af5d0 | bellard | &tm.tm_mday) == 3) {
|
8475 | 7e0af5d0 | bellard | tm.tm_hour = 0;
|
8476 | 7e0af5d0 | bellard | tm.tm_min = 0;
|
8477 | 7e0af5d0 | bellard | tm.tm_sec = 0;
|
8478 | 7e0af5d0 | bellard | } else {
|
8479 | 7e0af5d0 | bellard | goto date_fail;
|
8480 | 7e0af5d0 | bellard | } |
8481 | 7e0af5d0 | bellard | tm.tm_year -= 1900;
|
8482 | 7e0af5d0 | bellard | tm.tm_mon--; |
8483 | 3c6b2088 | bellard | rtc_start_date = mktimegm(&tm); |
8484 | 7e0af5d0 | bellard | if (rtc_start_date == -1) { |
8485 | 7e0af5d0 | bellard | date_fail:
|
8486 | 7e0af5d0 | bellard | fprintf(stderr, "Invalid date format. Valid format are:\n"
|
8487 | 7e0af5d0 | bellard | "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
|
8488 | 7e0af5d0 | bellard | exit(1);
|
8489 | 7e0af5d0 | bellard | } |
8490 | f6503059 | balrog | rtc_date_offset = time(NULL) - rtc_start_date;
|
8491 | 7e0af5d0 | bellard | } |
8492 | 7e0af5d0 | bellard | } |
8493 | 7e0af5d0 | bellard | break;
|
8494 | 26a5f13b | bellard | case QEMU_OPTION_tb_size:
|
8495 | 26a5f13b | bellard | tb_size = strtol(optarg, NULL, 0); |
8496 | 26a5f13b | bellard | if (tb_size < 0) |
8497 | 26a5f13b | bellard | tb_size = 0;
|
8498 | 26a5f13b | bellard | break;
|
8499 | 2e70f6ef | pbrook | case QEMU_OPTION_icount:
|
8500 | 2e70f6ef | pbrook | use_icount = 1;
|
8501 | 2e70f6ef | pbrook | if (strcmp(optarg, "auto") == 0) { |
8502 | 2e70f6ef | pbrook | icount_time_shift = -1;
|
8503 | 2e70f6ef | pbrook | } else {
|
8504 | 2e70f6ef | pbrook | icount_time_shift = strtol(optarg, NULL, 0); |
8505 | 2e70f6ef | pbrook | } |
8506 | 2e70f6ef | pbrook | break;
|
8507 | cd6f1169 | bellard | } |
8508 | 0824d6fc | bellard | } |
8509 | 0824d6fc | bellard | } |
8510 | 330d0414 | bellard | |
8511 | 71e3ceb8 | ths | #ifndef _WIN32
|
8512 | 71e3ceb8 | ths | if (daemonize && !nographic && vnc_display == NULL) { |
8513 | 71e3ceb8 | ths | fprintf(stderr, "Can only daemonize if using -nographic or -vnc\n");
|
8514 | 71e3ceb8 | ths | daemonize = 0;
|
8515 | 71e3ceb8 | ths | } |
8516 | 71e3ceb8 | ths | |
8517 | 71e3ceb8 | ths | if (daemonize) {
|
8518 | 71e3ceb8 | ths | pid_t pid; |
8519 | 71e3ceb8 | ths | |
8520 | 71e3ceb8 | ths | if (pipe(fds) == -1) |
8521 | 71e3ceb8 | ths | exit(1);
|
8522 | 71e3ceb8 | ths | |
8523 | 71e3ceb8 | ths | pid = fork(); |
8524 | 71e3ceb8 | ths | if (pid > 0) { |
8525 | 71e3ceb8 | ths | uint8_t status; |
8526 | 71e3ceb8 | ths | ssize_t len; |
8527 | 71e3ceb8 | ths | |
8528 | 71e3ceb8 | ths | close(fds[1]);
|
8529 | 71e3ceb8 | ths | |
8530 | 71e3ceb8 | ths | again:
|
8531 | 93815bc2 | ths | len = read(fds[0], &status, 1); |
8532 | 93815bc2 | ths | if (len == -1 && (errno == EINTR)) |
8533 | 93815bc2 | ths | goto again;
|
8534 | 93815bc2 | ths | |
8535 | 93815bc2 | ths | if (len != 1) |
8536 | 93815bc2 | ths | exit(1);
|
8537 | 93815bc2 | ths | else if (status == 1) { |
8538 | 93815bc2 | ths | fprintf(stderr, "Could not acquire pidfile\n");
|
8539 | 93815bc2 | ths | exit(1);
|
8540 | 93815bc2 | ths | } else
|
8541 | 93815bc2 | ths | exit(0);
|
8542 | 71e3ceb8 | ths | } else if (pid < 0) |
8543 | 93815bc2 | ths | exit(1);
|
8544 | 71e3ceb8 | ths | |
8545 | 71e3ceb8 | ths | setsid(); |
8546 | 71e3ceb8 | ths | |
8547 | 71e3ceb8 | ths | pid = fork(); |
8548 | 71e3ceb8 | ths | if (pid > 0) |
8549 | 71e3ceb8 | ths | exit(0);
|
8550 | 71e3ceb8 | ths | else if (pid < 0) |
8551 | 71e3ceb8 | ths | exit(1);
|
8552 | 71e3ceb8 | ths | |
8553 | 71e3ceb8 | ths | umask(027);
|
8554 | 71e3ceb8 | ths | chdir("/");
|
8555 | 71e3ceb8 | ths | |
8556 | 71e3ceb8 | ths | signal(SIGTSTP, SIG_IGN); |
8557 | 71e3ceb8 | ths | signal(SIGTTOU, SIG_IGN); |
8558 | 71e3ceb8 | ths | signal(SIGTTIN, SIG_IGN); |
8559 | 71e3ceb8 | ths | } |
8560 | 71e3ceb8 | ths | #endif
|
8561 | 71e3ceb8 | ths | |
8562 | aa26bb2d | ths | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
8563 | 93815bc2 | ths | if (daemonize) {
|
8564 | 93815bc2 | ths | uint8_t status = 1;
|
8565 | 93815bc2 | ths | write(fds[1], &status, 1); |
8566 | 93815bc2 | ths | } else
|
8567 | 93815bc2 | ths | fprintf(stderr, "Could not acquire pid file\n");
|
8568 | 93815bc2 | ths | exit(1);
|
8569 | 93815bc2 | ths | } |
8570 | 93815bc2 | ths | |
8571 | ff3fbb30 | bellard | #ifdef USE_KQEMU
|
8572 | ff3fbb30 | bellard | if (smp_cpus > 1) |
8573 | ff3fbb30 | bellard | kqemu_allowed = 0;
|
8574 | ff3fbb30 | bellard | #endif
|
8575 | a20dd508 | bellard | linux_boot = (kernel_filename != NULL);
|
8576 | 7317b8ca | balrog | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
8577 | 6c41b272 | balrog | |
8578 | 28c5af54 | j_mayer | /* XXX: this should not be: some embedded targets just have flash */
|
8579 | 28c5af54 | j_mayer | if (!linux_boot && net_boot == 0 && |
8580 | e4bcb14c | ths | nb_drives_opt == 0)
|
8581 | 15f82208 | ths | help(1);
|
8582 | 0824d6fc | bellard | |
8583 | f8d39c01 | ths | if (!linux_boot && *kernel_cmdline != '\0') { |
8584 | f8d39c01 | ths | fprintf(stderr, "-append only allowed with -kernel option\n");
|
8585 | f8d39c01 | ths | exit(1);
|
8586 | f8d39c01 | ths | } |
8587 | f8d39c01 | ths | |
8588 | f8d39c01 | ths | if (!linux_boot && initrd_filename != NULL) { |
8589 | f8d39c01 | ths | fprintf(stderr, "-initrd only allowed with -kernel option\n");
|
8590 | f8d39c01 | ths | exit(1);
|
8591 | f8d39c01 | ths | } |
8592 | f8d39c01 | ths | |
8593 | 96d30e48 | ths | /* boot to floppy or the default cd if no hard disk defined yet */
|
8594 | 28c5af54 | j_mayer | if (!boot_devices[0]) { |
8595 | e4bcb14c | ths | boot_devices = "cad";
|
8596 | 96d30e48 | ths | } |
8597 | b118d61e | bellard | setvbuf(stdout, NULL, _IOLBF, 0); |
8598 | 3b46e624 | ths | |
8599 | 634fce96 | pbrook | init_timers(); |
8600 | 634fce96 | pbrook | init_timer_alarm(); |
8601 | 83f64091 | bellard | qemu_aio_init(); |
8602 | 2e70f6ef | pbrook | if (use_icount && icount_time_shift < 0) { |
8603 | 2e70f6ef | pbrook | use_icount = 2;
|
8604 | 2e70f6ef | pbrook | /* 125MIPS seems a reasonable initial guess at the guest speed.
|
8605 | 2e70f6ef | pbrook | It will be corrected fairly quickly anyway. */
|
8606 | 2e70f6ef | pbrook | icount_time_shift = 3;
|
8607 | 2e70f6ef | pbrook | init_icount_adjust(); |
8608 | 2e70f6ef | pbrook | } |
8609 | 634fce96 | pbrook | |
8610 | fd1dff4b | bellard | #ifdef _WIN32
|
8611 | fd1dff4b | bellard | socket_init(); |
8612 | fd1dff4b | bellard | #endif
|
8613 | fd1dff4b | bellard | |
8614 | 7c9d8e07 | bellard | /* init network clients */
|
8615 | 7c9d8e07 | bellard | if (nb_net_clients == 0) { |
8616 | 7c9d8e07 | bellard | /* if no clients, we use a default config */
|
8617 | fd5f393a | pbrook | net_clients[0] = "nic"; |
8618 | fd5f393a | pbrook | net_clients[1] = "user"; |
8619 | 7c9d8e07 | bellard | nb_net_clients = 2;
|
8620 | c20709aa | bellard | } |
8621 | c20709aa | bellard | |
8622 | 7c9d8e07 | bellard | for(i = 0;i < nb_net_clients; i++) { |
8623 | 7c9d8e07 | bellard | if (net_client_init(net_clients[i]) < 0) |
8624 | 7c9d8e07 | bellard | exit(1);
|
8625 | 702c651c | bellard | } |
8626 | 833c7174 | blueswir1 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
8627 | 833c7174 | blueswir1 | if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) |
8628 | 833c7174 | blueswir1 | continue;
|
8629 | 833c7174 | blueswir1 | if (vlan->nb_guest_devs == 0) { |
8630 | 833c7174 | blueswir1 | fprintf(stderr, "Invalid vlan (%d) with no nics\n", vlan->id);
|
8631 | 833c7174 | blueswir1 | exit(1);
|
8632 | 833c7174 | blueswir1 | } |
8633 | 833c7174 | blueswir1 | if (vlan->nb_host_devs == 0) |
8634 | 833c7174 | blueswir1 | fprintf(stderr, |
8635 | 833c7174 | blueswir1 | "Warning: vlan %d is not connected to host network\n",
|
8636 | 833c7174 | blueswir1 | vlan->id); |
8637 | 833c7174 | blueswir1 | } |
8638 | f1510b2c | bellard | |
8639 | eec85c2a | ths | #ifdef TARGET_I386
|
8640 | ed494d87 | balrog | /* XXX: this should be moved in the PC machine instantiation code */
|
8641 | 28c5af54 | j_mayer | if (net_boot != 0) { |
8642 | 28c5af54 | j_mayer | int netroms = 0; |
8643 | 28c5af54 | j_mayer | for (i = 0; i < nb_nics && i < 4; i++) { |
8644 | eec85c2a | ths | const char *model = nd_table[i].model; |
8645 | eec85c2a | ths | char buf[1024]; |
8646 | 28c5af54 | j_mayer | if (net_boot & (1 << i)) { |
8647 | 28c5af54 | j_mayer | if (model == NULL) |
8648 | 28c5af54 | j_mayer | model = "ne2k_pci";
|
8649 | 28c5af54 | j_mayer | snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model); |
8650 | 28c5af54 | j_mayer | if (get_image_size(buf) > 0) { |
8651 | 28c5af54 | j_mayer | if (nb_option_roms >= MAX_OPTION_ROMS) {
|
8652 | 28c5af54 | j_mayer | fprintf(stderr, "Too many option ROMs\n");
|
8653 | 28c5af54 | j_mayer | exit(1);
|
8654 | 28c5af54 | j_mayer | } |
8655 | 28c5af54 | j_mayer | option_rom[nb_option_roms] = strdup(buf); |
8656 | 28c5af54 | j_mayer | nb_option_roms++; |
8657 | 28c5af54 | j_mayer | netroms++; |
8658 | 28c5af54 | j_mayer | } |
8659 | 28c5af54 | j_mayer | } |
8660 | eec85c2a | ths | } |
8661 | 28c5af54 | j_mayer | if (netroms == 0) { |
8662 | eec85c2a | ths | fprintf(stderr, "No valid PXE rom found for network device\n");
|
8663 | eec85c2a | ths | exit(1);
|
8664 | eec85c2a | ths | } |
8665 | eec85c2a | ths | } |
8666 | eec85c2a | ths | #endif
|
8667 | eec85c2a | ths | |
8668 | 0824d6fc | bellard | /* init the memory */
|
8669 | 7fb4fdcf | balrog | phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED; |
8670 | 7fb4fdcf | balrog | |
8671 | 7fb4fdcf | balrog | if (machine->ram_require & RAMSIZE_FIXED) {
|
8672 | 7fb4fdcf | balrog | if (ram_size > 0) { |
8673 | 7fb4fdcf | balrog | if (ram_size < phys_ram_size) {
|
8674 | cd940061 | aurel32 | fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
|
8675 | cd940061 | aurel32 | machine->name, (unsigned long long) phys_ram_size); |
8676 | 7fb4fdcf | balrog | exit(-1);
|
8677 | 7fb4fdcf | balrog | } |
8678 | 7fb4fdcf | balrog | |
8679 | 7fb4fdcf | balrog | phys_ram_size = ram_size; |
8680 | 7fb4fdcf | balrog | } else
|
8681 | 7fb4fdcf | balrog | ram_size = phys_ram_size; |
8682 | 7fb4fdcf | balrog | } else {
|
8683 | 4fc5d071 | aurel32 | if (ram_size == 0) |
8684 | 7fb4fdcf | balrog | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
8685 | 7fb4fdcf | balrog | |
8686 | 7fb4fdcf | balrog | phys_ram_size += ram_size; |
8687 | 7fb4fdcf | balrog | } |
8688 | 9ae02555 | ths | |
8689 | d993e026 | bellard | phys_ram_base = qemu_vmalloc(phys_ram_size); |
8690 | 7f7f9873 | bellard | if (!phys_ram_base) {
|
8691 | 7f7f9873 | bellard | fprintf(stderr, "Could not allocate physical memory\n");
|
8692 | 0824d6fc | bellard | exit(1);
|
8693 | 0824d6fc | bellard | } |
8694 | 0824d6fc | bellard | |
8695 | 26a5f13b | bellard | /* init the dynamic translator */
|
8696 | 26a5f13b | bellard | cpu_exec_init_all(tb_size * 1024 * 1024); |
8697 | 26a5f13b | bellard | |
8698 | 5905b2e5 | bellard | bdrv_init(); |
8699 | c4b1fcc0 | bellard | |
8700 | e4bcb14c | ths | /* we always create the cdrom drive, even if no disk is there */
|
8701 | c4b1fcc0 | bellard | |
8702 | e4bcb14c | ths | if (nb_drives_opt < MAX_DRIVES)
|
8703 | 609497ab | balrog | drive_add(NULL, CDROM_ALIAS);
|
8704 | c4b1fcc0 | bellard | |
8705 | 9d413d1d | balrog | /* we always create at least one floppy */
|
8706 | 33e3963e | bellard | |
8707 | e4bcb14c | ths | if (nb_drives_opt < MAX_DRIVES)
|
8708 | 609497ab | balrog | drive_add(NULL, FD_ALIAS, 0); |
8709 | 86f55663 | j_mayer | |
8710 | 9d413d1d | balrog | /* we always create one sd slot, even if no card is in it */
|
8711 | 9d413d1d | balrog | |
8712 | 9d413d1d | balrog | if (nb_drives_opt < MAX_DRIVES)
|
8713 | 609497ab | balrog | drive_add(NULL, SD_ALIAS);
|
8714 | 9d413d1d | balrog | |
8715 | e4bcb14c | ths | /* open the virtual block devices */
|
8716 | e4bcb14c | ths | |
8717 | e4bcb14c | ths | for(i = 0; i < nb_drives_opt; i++) |
8718 | 609497ab | balrog | if (drive_init(&drives_opt[i], snapshot, machine) == -1) |
8719 | e4bcb14c | ths | exit(1);
|
8720 | 3e3d5815 | balrog | |
8721 | c88676f8 | bellard | register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
8722 | c88676f8 | bellard | register_savevm("ram", 0, 2, ram_save, ram_load, NULL); |
8723 | 8a7ddc38 | bellard | |
8724 | 330d0414 | bellard | init_ioports(); |
8725 | 0824d6fc | bellard | |
8726 | 313aa567 | bellard | /* terminal init */
|
8727 | 740733bb | ths | memset(&display_state, 0, sizeof(display_state)); |
8728 | a20dd508 | bellard | if (nographic) {
|
8729 | 4d3b6f6e | balrog | if (curses) {
|
8730 | 4d3b6f6e | balrog | fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
|
8731 | 4d3b6f6e | balrog | exit(1);
|
8732 | 4d3b6f6e | balrog | } |
8733 | 2ff89790 | ths | /* nearly nothing to do */
|
8734 | 2ff89790 | ths | dumb_display_init(ds); |
8735 | 73fc9742 | ths | } else if (vnc_display != NULL) { |
8736 | 71cab5ca | ths | vnc_display_init(ds); |
8737 | 71cab5ca | ths | if (vnc_display_open(ds, vnc_display) < 0) |
8738 | 71cab5ca | ths | exit(1);
|
8739 | 4d3b6f6e | balrog | } else
|
8740 | 4d3b6f6e | balrog | #if defined(CONFIG_CURSES)
|
8741 | 4d3b6f6e | balrog | if (curses) {
|
8742 | 4d3b6f6e | balrog | curses_display_init(ds, full_screen); |
8743 | 4d3b6f6e | balrog | } else
|
8744 | 4d3b6f6e | balrog | #endif
|
8745 | 4d3b6f6e | balrog | { |
8746 | 5b0753e0 | bellard | #if defined(CONFIG_SDL)
|
8747 | 43523e93 | ths | sdl_display_init(ds, full_screen, no_frame); |
8748 | 5b0753e0 | bellard | #elif defined(CONFIG_COCOA)
|
8749 | 5b0753e0 | bellard | cocoa_display_init(ds, full_screen); |
8750 | 67276f53 | pbrook | #else
|
8751 | 67276f53 | pbrook | dumb_display_init(ds); |
8752 | 313aa567 | bellard | #endif
|
8753 | 313aa567 | bellard | } |
8754 | 0824d6fc | bellard | |
8755 | 20d8a3ed | ths | /* Maintain compatibility with multiple stdio monitors */
|
8756 | 20d8a3ed | ths | if (!strcmp(monitor_device,"stdio")) { |
8757 | 20d8a3ed | ths | for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
8758 | fd5f393a | pbrook | const char *devname = serial_devices[i]; |
8759 | fd5f393a | pbrook | if (devname && !strcmp(devname,"mon:stdio")) { |
8760 | fd5f393a | pbrook | monitor_device = NULL;
|
8761 | 20d8a3ed | ths | break;
|
8762 | fd5f393a | pbrook | } else if (devname && !strcmp(devname,"stdio")) { |
8763 | fd5f393a | pbrook | monitor_device = NULL;
|
8764 | fd5f393a | pbrook | serial_devices[i] = "mon:stdio";
|
8765 | 20d8a3ed | ths | break;
|
8766 | 20d8a3ed | ths | } |
8767 | 20d8a3ed | ths | } |
8768 | 20d8a3ed | ths | } |
8769 | fd5f393a | pbrook | if (monitor_device) {
|
8770 | 20d8a3ed | ths | monitor_hd = qemu_chr_open(monitor_device); |
8771 | 20d8a3ed | ths | if (!monitor_hd) {
|
8772 | 20d8a3ed | ths | fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
|
8773 | 20d8a3ed | ths | exit(1);
|
8774 | 20d8a3ed | ths | } |
8775 | 20d8a3ed | ths | monitor_init(monitor_hd, !nographic); |
8776 | 82c643ff | bellard | } |
8777 | 82c643ff | bellard | |
8778 | 8d11df9e | bellard | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
8779 | c03b0f0f | bellard | const char *devname = serial_devices[i]; |
8780 | fd5f393a | pbrook | if (devname && strcmp(devname, "none")) { |
8781 | c03b0f0f | bellard | serial_hds[i] = qemu_chr_open(devname); |
8782 | 8d11df9e | bellard | if (!serial_hds[i]) {
|
8783 | 5fafdf24 | ths | fprintf(stderr, "qemu: could not open serial device '%s'\n",
|
8784 | c03b0f0f | bellard | devname); |
8785 | 8d11df9e | bellard | exit(1);
|
8786 | 8d11df9e | bellard | } |
8787 | af3a9031 | ths | if (strstart(devname, "vc", 0)) |
8788 | 7ba1260a | bellard | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
|
8789 | 8d11df9e | bellard | } |
8790 | 82c643ff | bellard | } |
8791 | 82c643ff | bellard | |
8792 | 6508fe59 | bellard | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
8793 | c03b0f0f | bellard | const char *devname = parallel_devices[i]; |
8794 | fd5f393a | pbrook | if (devname && strcmp(devname, "none")) { |
8795 | c03b0f0f | bellard | parallel_hds[i] = qemu_chr_open(devname); |
8796 | 6508fe59 | bellard | if (!parallel_hds[i]) {
|
8797 | 5fafdf24 | ths | fprintf(stderr, "qemu: could not open parallel device '%s'\n",
|
8798 | c03b0f0f | bellard | devname); |
8799 | 6508fe59 | bellard | exit(1);
|
8800 | 6508fe59 | bellard | } |
8801 | af3a9031 | ths | if (strstart(devname, "vc", 0)) |
8802 | 7ba1260a | bellard | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
|
8803 | 6508fe59 | bellard | } |
8804 | 6508fe59 | bellard | } |
8805 | 6508fe59 | bellard | |
8806 | b881c2c6 | blueswir1 | machine->init(ram_size, vga_ram_size, boot_devices, ds, |
8807 | 94fc95cd | j_mayer | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
8808 | 73332e5c | bellard | |
8809 | 0d92ed30 | pbrook | /* init USB devices */
|
8810 | 0d92ed30 | pbrook | if (usb_enabled) {
|
8811 | 0d92ed30 | pbrook | for(i = 0; i < usb_devices_index; i++) { |
8812 | 0d92ed30 | pbrook | if (usb_device_add(usb_devices[i]) < 0) { |
8813 | 0d92ed30 | pbrook | fprintf(stderr, "Warning: could not add USB device %s\n",
|
8814 | 0d92ed30 | pbrook | usb_devices[i]); |
8815 | 0d92ed30 | pbrook | } |
8816 | 0d92ed30 | pbrook | } |
8817 | 0d92ed30 | pbrook | } |
8818 | 0d92ed30 | pbrook | |
8819 | 740733bb | ths | if (display_state.dpy_refresh) {
|
8820 | 740733bb | ths | display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state); |
8821 | 740733bb | ths | qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock)); |
8822 | 740733bb | ths | } |
8823 | 7f7f9873 | bellard | |
8824 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
8825 | b4608c04 | bellard | if (use_gdbstub) {
|
8826 | c636bb66 | bellard | /* XXX: use standard host:port notation and modify options
|
8827 | c636bb66 | bellard | accordingly. */
|
8828 | cfc3475a | pbrook | if (gdbserver_start(gdbstub_port) < 0) { |
8829 | cfc3475a | pbrook | fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
|
8830 | c636bb66 | bellard | gdbstub_port); |
8831 | 8a7ddc38 | bellard | exit(1);
|
8832 | 8a7ddc38 | bellard | } |
8833 | 45669e00 | balrog | } |
8834 | 67b915a5 | bellard | #endif
|
8835 | 45669e00 | balrog | |
8836 | d63d307f | bellard | if (loadvm)
|
8837 | faea38e7 | bellard | do_loadvm(loadvm); |
8838 | d63d307f | bellard | |
8839 | 67b915a5 | bellard | { |
8840 | 5905b2e5 | bellard | /* XXX: simplify init */
|
8841 | 5905b2e5 | bellard | read_passwords(); |
8842 | 3c07f8e8 | pbrook | if (autostart) {
|
8843 | 5905b2e5 | bellard | vm_start(); |
8844 | 5905b2e5 | bellard | } |
8845 | 0824d6fc | bellard | } |
8846 | ffd843bc | ths | |
8847 | 71e3ceb8 | ths | if (daemonize) {
|
8848 | 71e3ceb8 | ths | uint8_t status = 0;
|
8849 | 71e3ceb8 | ths | ssize_t len; |
8850 | 71e3ceb8 | ths | int fd;
|
8851 | 71e3ceb8 | ths | |
8852 | 71e3ceb8 | ths | again1:
|
8853 | 71e3ceb8 | ths | len = write(fds[1], &status, 1); |
8854 | 71e3ceb8 | ths | if (len == -1 && (errno == EINTR)) |
8855 | 71e3ceb8 | ths | goto again1;
|
8856 | 71e3ceb8 | ths | |
8857 | 71e3ceb8 | ths | if (len != 1) |
8858 | 71e3ceb8 | ths | exit(1);
|
8859 | 71e3ceb8 | ths | |
8860 | aeb30be6 | balrog | TFR(fd = open("/dev/null", O_RDWR));
|
8861 | 71e3ceb8 | ths | if (fd == -1) |
8862 | 71e3ceb8 | ths | exit(1);
|
8863 | 71e3ceb8 | ths | |
8864 | 71e3ceb8 | ths | dup2(fd, 0);
|
8865 | 71e3ceb8 | ths | dup2(fd, 1);
|
8866 | 71e3ceb8 | ths | dup2(fd, 2);
|
8867 | 71e3ceb8 | ths | |
8868 | 71e3ceb8 | ths | close(fd); |
8869 | 71e3ceb8 | ths | } |
8870 | 71e3ceb8 | ths | |
8871 | 8a7ddc38 | bellard | main_loop(); |
8872 | 40c3bac3 | bellard | quit_timers(); |
8873 | b46a8906 | ths | |
8874 | 7d294b61 | ths | #if !defined(_WIN32)
|
8875 | b46a8906 | ths | /* close network clients */
|
8876 | b46a8906 | ths | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
8877 | b46a8906 | ths | VLANClientState *vc; |
8878 | b46a8906 | ths | |
8879 | 7d294b61 | ths | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
8880 | b46a8906 | ths | if (vc->fd_read == tap_receive) {
|
8881 | b46a8906 | ths | char ifname[64]; |
8882 | b46a8906 | ths | TAPState *s = vc->opaque; |
8883 | b46a8906 | ths | |
8884 | b46a8906 | ths | if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 && |
8885 | b46a8906 | ths | s->down_script[0])
|
8886 | b46a8906 | ths | launch_script(s->down_script, ifname, s->fd); |
8887 | b46a8906 | ths | } |
8888 | 4fddf62a | ths | } |
8889 | 7d294b61 | ths | } |
8890 | 7d294b61 | ths | #endif
|
8891 | 0824d6fc | bellard | return 0; |
8892 | 0824d6fc | bellard | } |