root / vl.c @ ed23fbd9
History | View | Annotate | Download (239.5 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 | 128ab2ff | blueswir1 | #if !defined(__APPLE__) && !defined(__OpenBSD__)
|
65 | 7d3505c5 | bellard | #include <libutil.h> |
66 | 83fb7adf | bellard | #endif
|
67 | 128ab2ff | blueswir1 | #ifdef __OpenBSD__
|
68 | 128ab2ff | blueswir1 | #include <net/if.h> |
69 | 128ab2ff | blueswir1 | #endif
|
70 | 5c40d2bd | ths | #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
|
71 | 5c40d2bd | ths | #include <freebsd/stdlib.h> |
72 | 7d3505c5 | bellard | #else
|
73 | ec530c81 | bellard | #ifndef __sun__
|
74 | f1510b2c | bellard | #include <linux/if.h> |
75 | f1510b2c | bellard | #include <linux/if_tun.h> |
76 | 7d3505c5 | bellard | #include <pty.h> |
77 | 7d3505c5 | bellard | #include <malloc.h> |
78 | fd872598 | bellard | #include <linux/rtc.h> |
79 | bd494f4c | ths | |
80 | bd494f4c | ths | /* For the benefit of older linux systems which don't supply it,
|
81 | bd494f4c | ths | we use a local copy of hpet.h. */
|
82 | bd494f4c | ths | /* #include <linux/hpet.h> */
|
83 | bd494f4c | ths | #include "hpet.h" |
84 | bd494f4c | ths | |
85 | e57a8c0e | bellard | #include <linux/ppdev.h> |
86 | 5867c88a | ths | #include <linux/parport.h> |
87 | d5d10bc3 | ths | #else
|
88 | d5d10bc3 | ths | #include <sys/stat.h> |
89 | d5d10bc3 | ths | #include <sys/ethernet.h> |
90 | d5d10bc3 | ths | #include <sys/sockio.h> |
91 | d5d10bc3 | ths | #include <netinet/arp.h> |
92 | d5d10bc3 | ths | #include <netinet/in.h> |
93 | d5d10bc3 | ths | #include <netinet/in_systm.h> |
94 | d5d10bc3 | ths | #include <netinet/ip.h> |
95 | d5d10bc3 | ths | #include <netinet/ip_icmp.h> // must come after ip.h |
96 | d5d10bc3 | ths | #include <netinet/udp.h> |
97 | d5d10bc3 | ths | #include <netinet/tcp.h> |
98 | d5d10bc3 | ths | #include <net/if.h> |
99 | d5d10bc3 | ths | #include <syslog.h> |
100 | d5d10bc3 | ths | #include <stropts.h> |
101 | 67b915a5 | bellard | #endif
|
102 | 7d3505c5 | bellard | #endif
|
103 | cb4b976b | ths | #else
|
104 | cb4b976b | ths | #include <winsock2.h> |
105 | cb4b976b | ths | int inet_aton(const char *cp, struct in_addr *ia); |
106 | ec530c81 | bellard | #endif
|
107 | 67b915a5 | bellard | |
108 | c20709aa | bellard | #if defined(CONFIG_SLIRP)
|
109 | c20709aa | bellard | #include "libslirp.h" |
110 | c20709aa | bellard | #endif
|
111 | c20709aa | bellard | |
112 | 9892fbfb | blueswir1 | #if defined(__OpenBSD__)
|
113 | 9892fbfb | blueswir1 | #include <util.h> |
114 | 9892fbfb | blueswir1 | #endif
|
115 | 9892fbfb | blueswir1 | |
116 | 8a16d273 | ths | #if defined(CONFIG_VDE)
|
117 | 8a16d273 | ths | #include <libvdeplug.h> |
118 | 8a16d273 | ths | #endif
|
119 | 8a16d273 | ths | |
120 | 67b915a5 | bellard | #ifdef _WIN32
|
121 | 7d3505c5 | bellard | #include <malloc.h> |
122 | 67b915a5 | bellard | #include <sys/timeb.h> |
123 | 4fddf62a | ths | #include <mmsystem.h> |
124 | 67b915a5 | bellard | #define getopt_long_only getopt_long
|
125 | 67b915a5 | bellard | #define memalign(align, size) malloc(size)
|
126 | 67b915a5 | bellard | #endif
|
127 | 67b915a5 | bellard | |
128 | 6ca957f0 | bellard | #include "qemu_socket.h" |
129 | 6ca957f0 | bellard | |
130 | 73332e5c | bellard | #ifdef CONFIG_SDL
|
131 | 96bcd4f8 | bellard | #ifdef __APPLE__
|
132 | 83fb7adf | bellard | #include <SDL/SDL.h> |
133 | 96bcd4f8 | bellard | #endif
|
134 | 73332e5c | bellard | #endif /* CONFIG_SDL */ |
135 | 0824d6fc | bellard | |
136 | 5b0753e0 | bellard | #ifdef CONFIG_COCOA
|
137 | 5b0753e0 | bellard | #undef main
|
138 | 5b0753e0 | bellard | #define main qemu_main
|
139 | 5b0753e0 | bellard | #endif /* CONFIG_COCOA */ |
140 | 5b0753e0 | bellard | |
141 | 0824d6fc | bellard | #include "disas.h" |
142 | fc01f7e7 | bellard | |
143 | 8a7ddc38 | bellard | #include "exec-all.h" |
144 | 0824d6fc | bellard | |
145 | 5a67135a | bellard | #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
146 | b46a8906 | ths | #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown" |
147 | a14d6c8c | pbrook | #ifdef __sun__
|
148 | a14d6c8c | pbrook | #define SMBD_COMMAND "/usr/sfw/sbin/smbd" |
149 | a14d6c8c | pbrook | #else
|
150 | a14d6c8c | pbrook | #define SMBD_COMMAND "/usr/sbin/smbd" |
151 | a14d6c8c | pbrook | #endif
|
152 | f1510b2c | bellard | |
153 | 0824d6fc | bellard | //#define DEBUG_UNUSED_IOPORT
|
154 | fd872598 | bellard | //#define DEBUG_IOPORT
|
155 | 330d0414 | bellard | |
156 | 77d4bc34 | bellard | #ifdef TARGET_PPC
|
157 | 77d4bc34 | bellard | #define DEFAULT_RAM_SIZE 144 |
158 | 77d4bc34 | bellard | #else
|
159 | 1bfe856e | bellard | #define DEFAULT_RAM_SIZE 128 |
160 | 77d4bc34 | bellard | #endif
|
161 | 313aa567 | bellard | |
162 | 0d92ed30 | pbrook | /* Max number of USB devices that can be specified on the commandline. */
|
163 | 0d92ed30 | pbrook | #define MAX_USB_CMDLINE 8 |
164 | 0d92ed30 | pbrook | |
165 | 7dea1da4 | bellard | /* XXX: use a two level table to limit memory usage */
|
166 | 7dea1da4 | bellard | #define MAX_IOPORTS 65536 |
167 | 0824d6fc | bellard | |
168 | 80cabfad | bellard | const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
169 | 1192dad8 | j_mayer | const char *bios_name = NULL; |
170 | c4b1fcc0 | bellard | void *ioport_opaque[MAX_IOPORTS];
|
171 | fc01f7e7 | bellard | IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
|
172 | fc01f7e7 | bellard | IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
|
173 | e4bcb14c | ths | /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
|
174 | faea38e7 | bellard | to store the VM snapshots */
|
175 | e4bcb14c | ths | DriveInfo drives_table[MAX_DRIVES+1];
|
176 | e4bcb14c | ths | int nb_drives;
|
177 | faea38e7 | bellard | /* point to the block driver where the snapshots are managed */
|
178 | faea38e7 | bellard | BlockDriverState *bs_snapshots; |
179 | 313aa567 | bellard | int vga_ram_size;
|
180 | 313aa567 | bellard | static DisplayState display_state;
|
181 | a20dd508 | bellard | int nographic;
|
182 | 4d3b6f6e | balrog | int curses;
|
183 | 3d11d0eb | bellard | const char* keyboard_layout = NULL; |
184 | 313aa567 | bellard | int64_t ticks_per_sec; |
185 | 00f82b8a | aurel32 | ram_addr_t ram_size; |
186 | 80cabfad | bellard | int pit_min_timer_count = 0; |
187 | c4b1fcc0 | bellard | int nb_nics;
|
188 | 7c9d8e07 | bellard | NICInfo nd_table[MAX_NICS]; |
189 | 8a7ddc38 | bellard | int vm_running;
|
190 | f6503059 | balrog | static int rtc_utc = 1; |
191 | f6503059 | balrog | static int rtc_date_offset = -1; /* -1 means no change */ |
192 | 1bfe856e | bellard | int cirrus_vga_enabled = 1; |
193 | d34cab9f | ths | int vmsvga_enabled = 0; |
194 | d827220b | bellard | #ifdef TARGET_SPARC
|
195 | d827220b | bellard | int graphic_width = 1024; |
196 | d827220b | bellard | int graphic_height = 768; |
197 | eee0b836 | blueswir1 | int graphic_depth = 8; |
198 | d827220b | bellard | #else
|
199 | 1bfe856e | bellard | int graphic_width = 800; |
200 | 1bfe856e | bellard | int graphic_height = 600; |
201 | e9b137c2 | bellard | int graphic_depth = 15; |
202 | eee0b836 | blueswir1 | #endif
|
203 | d63d307f | bellard | int full_screen = 0; |
204 | 43523e93 | ths | int no_frame = 0; |
205 | 667accab | ths | int no_quit = 0; |
206 | 8d11df9e | bellard | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
207 | 6508fe59 | bellard | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
208 | a09db21f | bellard | #ifdef TARGET_I386
|
209 | a09db21f | bellard | int win2k_install_hack = 0; |
210 | a09db21f | bellard | #endif
|
211 | bb36d470 | bellard | int usb_enabled = 0; |
212 | 7c9d8e07 | bellard | static VLANState *first_vlan;
|
213 | 6a00d601 | bellard | int smp_cpus = 1; |
214 | 73fc9742 | ths | const char *vnc_display; |
215 | d3e9db93 | bellard | #if defined(TARGET_SPARC)
|
216 | ba3c64fb | bellard | #define MAX_CPUS 16 |
217 | d3e9db93 | bellard | #elif defined(TARGET_I386)
|
218 | d3e9db93 | bellard | #define MAX_CPUS 255 |
219 | ba3c64fb | bellard | #else
|
220 | d3e9db93 | bellard | #define MAX_CPUS 1 |
221 | ba3c64fb | bellard | #endif
|
222 | 6515b203 | bellard | int acpi_enabled = 1; |
223 | 52ca8d6a | bellard | int fd_bootchk = 1; |
224 | d1beab82 | bellard | int no_reboot = 0; |
225 | b2f76161 | aurel32 | int no_shutdown = 0; |
226 | 9467cd46 | balrog | int cursor_hide = 1; |
227 | a171fe39 | balrog | int graphic_rotate = 0; |
228 | 71e3ceb8 | ths | int daemonize = 0; |
229 | 9ae02555 | ths | const char *option_rom[MAX_OPTION_ROMS]; |
230 | 9ae02555 | ths | int nb_option_roms;
|
231 | 8e71621f | pbrook | int semihosting_enabled = 0; |
232 | 3c07f8e8 | pbrook | int autostart = 1; |
233 | 2b8f2d41 | balrog | #ifdef TARGET_ARM
|
234 | 2b8f2d41 | balrog | int old_param = 0; |
235 | 2b8f2d41 | balrog | #endif
|
236 | c35734b2 | ths | const char *qemu_name; |
237 | 3780e197 | ths | int alt_grab = 0; |
238 | 66508601 | blueswir1 | #ifdef TARGET_SPARC
|
239 | 66508601 | blueswir1 | unsigned int nb_prom_envs = 0; |
240 | 66508601 | blueswir1 | const char *prom_envs[MAX_PROM_ENVS]; |
241 | 66508601 | blueswir1 | #endif
|
242 | e4bcb14c | ths | int nb_drives_opt;
|
243 | 609497ab | balrog | struct drive_opt {
|
244 | 609497ab | balrog | const char *file; |
245 | 609497ab | balrog | char opt[1024]; |
246 | 609497ab | balrog | } drives_opt[MAX_DRIVES]; |
247 | 0824d6fc | bellard | |
248 | ee5605e5 | balrog | static CPUState *cur_cpu;
|
249 | ee5605e5 | balrog | static CPUState *next_cpu;
|
250 | 76ea08f9 | balrog | static int event_pending = 1; |
251 | bf20dc07 | ths | /* Conversion factor from emulated instructions to virtual clock ticks. */
|
252 | 2e70f6ef | pbrook | static int icount_time_shift; |
253 | bf20dc07 | ths | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
|
254 | 2e70f6ef | pbrook | #define MAX_ICOUNT_SHIFT 10 |
255 | 2e70f6ef | pbrook | /* Compensate for varying guest execution speed. */
|
256 | 2e70f6ef | pbrook | static int64_t qemu_icount_bias;
|
257 | 2e70f6ef | pbrook | QEMUTimer *icount_rt_timer; |
258 | 2e70f6ef | pbrook | QEMUTimer *icount_vm_timer; |
259 | ee5605e5 | balrog | |
260 | aeb30be6 | balrog | #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR) |
261 | aeb30be6 | balrog | |
262 | 0824d6fc | bellard | /***********************************************************/
|
263 | 26aa7d72 | bellard | /* x86 ISA bus support */
|
264 | 26aa7d72 | bellard | |
265 | 26aa7d72 | bellard | target_phys_addr_t isa_mem_base = 0;
|
266 | 3de388f6 | bellard | PicState2 *isa_pic; |
267 | 0824d6fc | bellard | |
268 | 477e3edf | aliguori | static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
|
269 | 477e3edf | aliguori | static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
|
270 | 477e3edf | aliguori | |
271 | 477e3edf | aliguori | static uint32_t ioport_read(int index, uint32_t address) |
272 | 477e3edf | aliguori | { |
273 | 477e3edf | aliguori | static IOPortReadFunc *default_func[3] = { |
274 | 477e3edf | aliguori | default_ioport_readb, |
275 | 477e3edf | aliguori | default_ioport_readw, |
276 | 477e3edf | aliguori | default_ioport_readl |
277 | 477e3edf | aliguori | }; |
278 | 477e3edf | aliguori | IOPortReadFunc *func = ioport_read_table[index][address]; |
279 | 477e3edf | aliguori | if (!func)
|
280 | 477e3edf | aliguori | func = default_func[index]; |
281 | 477e3edf | aliguori | return func(ioport_opaque[address], address);
|
282 | 477e3edf | aliguori | } |
283 | 477e3edf | aliguori | |
284 | 477e3edf | aliguori | static void ioport_write(int index, uint32_t address, uint32_t data) |
285 | 477e3edf | aliguori | { |
286 | 477e3edf | aliguori | static IOPortWriteFunc *default_func[3] = { |
287 | 477e3edf | aliguori | default_ioport_writeb, |
288 | 477e3edf | aliguori | default_ioport_writew, |
289 | 477e3edf | aliguori | default_ioport_writel |
290 | 477e3edf | aliguori | }; |
291 | 477e3edf | aliguori | IOPortWriteFunc *func = ioport_write_table[index][address]; |
292 | 477e3edf | aliguori | if (!func)
|
293 | 477e3edf | aliguori | func = default_func[index]; |
294 | 477e3edf | aliguori | func(ioport_opaque[address], address, data); |
295 | 477e3edf | aliguori | } |
296 | 477e3edf | aliguori | |
297 | 9596ebb7 | pbrook | static uint32_t default_ioport_readb(void *opaque, uint32_t address) |
298 | 0824d6fc | bellard | { |
299 | 0824d6fc | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
300 | 1196be37 | ths | fprintf(stderr, "unused inb: port=0x%04x\n", address);
|
301 | 0824d6fc | bellard | #endif
|
302 | fc01f7e7 | bellard | return 0xff; |
303 | 0824d6fc | bellard | } |
304 | 0824d6fc | bellard | |
305 | 9596ebb7 | pbrook | static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
306 | 0824d6fc | bellard | { |
307 | 0824d6fc | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
308 | 1196be37 | ths | fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
|
309 | 0824d6fc | bellard | #endif
|
310 | 0824d6fc | bellard | } |
311 | 0824d6fc | bellard | |
312 | 0824d6fc | bellard | /* default is to make two byte accesses */
|
313 | 9596ebb7 | pbrook | static uint32_t default_ioport_readw(void *opaque, uint32_t address) |
314 | 0824d6fc | bellard | { |
315 | 0824d6fc | bellard | uint32_t data; |
316 | 477e3edf | aliguori | data = ioport_read(0, address);
|
317 | db45c29a | bellard | address = (address + 1) & (MAX_IOPORTS - 1); |
318 | 477e3edf | aliguori | data |= ioport_read(0, address) << 8; |
319 | 0824d6fc | bellard | return data;
|
320 | 0824d6fc | bellard | } |
321 | 0824d6fc | bellard | |
322 | 9596ebb7 | pbrook | static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
323 | 0824d6fc | bellard | { |
324 | 477e3edf | aliguori | ioport_write(0, address, data & 0xff); |
325 | db45c29a | bellard | address = (address + 1) & (MAX_IOPORTS - 1); |
326 | 477e3edf | aliguori | ioport_write(0, address, (data >> 8) & 0xff); |
327 | 0824d6fc | bellard | } |
328 | 0824d6fc | bellard | |
329 | 9596ebb7 | pbrook | static uint32_t default_ioport_readl(void *opaque, uint32_t address) |
330 | 0824d6fc | bellard | { |
331 | fc01f7e7 | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
332 | 1196be37 | ths | fprintf(stderr, "unused inl: port=0x%04x\n", address);
|
333 | fc01f7e7 | bellard | #endif
|
334 | fc01f7e7 | bellard | return 0xffffffff; |
335 | 0824d6fc | bellard | } |
336 | 0824d6fc | bellard | |
337 | 9596ebb7 | pbrook | static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
338 | 0824d6fc | bellard | { |
339 | fc01f7e7 | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
340 | 1196be37 | ths | fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
|
341 | fc01f7e7 | bellard | #endif
|
342 | 0824d6fc | bellard | } |
343 | 0824d6fc | bellard | |
344 | fc01f7e7 | bellard | /* size is the word size in byte */
|
345 | 5fafdf24 | ths | int register_ioport_read(int start, int length, int size, |
346 | c4b1fcc0 | bellard | IOPortReadFunc *func, void *opaque)
|
347 | f1510b2c | bellard | { |
348 | fc01f7e7 | bellard | int i, bsize;
|
349 | f1510b2c | bellard | |
350 | c4b1fcc0 | bellard | if (size == 1) { |
351 | fc01f7e7 | bellard | bsize = 0;
|
352 | c4b1fcc0 | bellard | } else if (size == 2) { |
353 | fc01f7e7 | bellard | bsize = 1;
|
354 | c4b1fcc0 | bellard | } else if (size == 4) { |
355 | fc01f7e7 | bellard | bsize = 2;
|
356 | c4b1fcc0 | bellard | } else {
|
357 | 88fdf56f | balrog | hw_error("register_ioport_read: invalid size");
|
358 | fc01f7e7 | bellard | return -1; |
359 | c4b1fcc0 | bellard | } |
360 | c4b1fcc0 | bellard | for(i = start; i < start + length; i += size) {
|
361 | fc01f7e7 | bellard | ioport_read_table[bsize][i] = func; |
362 | c4b1fcc0 | bellard | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
363 | 88fdf56f | balrog | hw_error("register_ioport_read: invalid opaque");
|
364 | c4b1fcc0 | bellard | ioport_opaque[i] = opaque; |
365 | c4b1fcc0 | bellard | } |
366 | f1510b2c | bellard | return 0; |
367 | f1510b2c | bellard | } |
368 | f1510b2c | bellard | |
369 | fc01f7e7 | bellard | /* size is the word size in byte */
|
370 | 5fafdf24 | ths | int register_ioport_write(int start, int length, int size, |
371 | c4b1fcc0 | bellard | IOPortWriteFunc *func, void *opaque)
|
372 | f1510b2c | bellard | { |
373 | fc01f7e7 | bellard | int i, bsize;
|
374 | f1510b2c | bellard | |
375 | c4b1fcc0 | bellard | if (size == 1) { |
376 | fc01f7e7 | bellard | bsize = 0;
|
377 | c4b1fcc0 | bellard | } else if (size == 2) { |
378 | fc01f7e7 | bellard | bsize = 1;
|
379 | c4b1fcc0 | bellard | } else if (size == 4) { |
380 | fc01f7e7 | bellard | bsize = 2;
|
381 | c4b1fcc0 | bellard | } else {
|
382 | 88fdf56f | balrog | hw_error("register_ioport_write: invalid size");
|
383 | fc01f7e7 | bellard | return -1; |
384 | c4b1fcc0 | bellard | } |
385 | c4b1fcc0 | bellard | for(i = start; i < start + length; i += size) {
|
386 | fc01f7e7 | bellard | ioport_write_table[bsize][i] = func; |
387 | 88fdf56f | balrog | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
388 | 88fdf56f | balrog | hw_error("register_ioport_write: invalid opaque");
|
389 | c4b1fcc0 | bellard | ioport_opaque[i] = opaque; |
390 | c4b1fcc0 | bellard | } |
391 | f1510b2c | bellard | return 0; |
392 | f1510b2c | bellard | } |
393 | f1510b2c | bellard | |
394 | 69b91039 | bellard | void isa_unassign_ioport(int start, int length) |
395 | 69b91039 | bellard | { |
396 | 69b91039 | bellard | int i;
|
397 | 69b91039 | bellard | |
398 | 69b91039 | bellard | for(i = start; i < start + length; i++) {
|
399 | 69b91039 | bellard | ioport_read_table[0][i] = default_ioport_readb;
|
400 | 69b91039 | bellard | ioport_read_table[1][i] = default_ioport_readw;
|
401 | 69b91039 | bellard | ioport_read_table[2][i] = default_ioport_readl;
|
402 | 69b91039 | bellard | |
403 | 69b91039 | bellard | ioport_write_table[0][i] = default_ioport_writeb;
|
404 | 69b91039 | bellard | ioport_write_table[1][i] = default_ioport_writew;
|
405 | 69b91039 | bellard | ioport_write_table[2][i] = default_ioport_writel;
|
406 | 69b91039 | bellard | } |
407 | 69b91039 | bellard | } |
408 | 69b91039 | bellard | |
409 | 20f32282 | bellard | /***********************************************************/
|
410 | 20f32282 | bellard | |
411 | c45886db | bellard | void cpu_outb(CPUState *env, int addr, int val) |
412 | 0824d6fc | bellard | { |
413 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
414 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
415 | fd872598 | bellard | fprintf(logfile, "outb: %04x %02x\n", addr, val);
|
416 | 3b46e624 | ths | #endif
|
417 | 477e3edf | aliguori | ioport_write(0, addr, val);
|
418 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
419 | 89bfc105 | bellard | if (env)
|
420 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
421 | 89bfc105 | bellard | #endif
|
422 | 0824d6fc | bellard | } |
423 | 0824d6fc | bellard | |
424 | c45886db | bellard | void cpu_outw(CPUState *env, int addr, int val) |
425 | 0824d6fc | bellard | { |
426 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
427 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
428 | fd872598 | bellard | fprintf(logfile, "outw: %04x %04x\n", addr, val);
|
429 | 3b46e624 | ths | #endif
|
430 | 477e3edf | aliguori | ioport_write(1, addr, val);
|
431 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
432 | 89bfc105 | bellard | if (env)
|
433 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
434 | 89bfc105 | bellard | #endif
|
435 | 0824d6fc | bellard | } |
436 | 0824d6fc | bellard | |
437 | c45886db | bellard | void cpu_outl(CPUState *env, int addr, int val) |
438 | 0824d6fc | bellard | { |
439 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
440 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
441 | fd872598 | bellard | fprintf(logfile, "outl: %04x %08x\n", addr, val);
|
442 | fd872598 | bellard | #endif
|
443 | 477e3edf | aliguori | ioport_write(2, addr, val);
|
444 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
445 | 89bfc105 | bellard | if (env)
|
446 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
447 | 89bfc105 | bellard | #endif
|
448 | 0824d6fc | bellard | } |
449 | 0824d6fc | bellard | |
450 | c45886db | bellard | int cpu_inb(CPUState *env, int addr) |
451 | 0824d6fc | bellard | { |
452 | fd872598 | bellard | int val;
|
453 | 477e3edf | aliguori | val = ioport_read(0, addr);
|
454 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
455 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
456 | fd872598 | bellard | fprintf(logfile, "inb : %04x %02x\n", addr, val);
|
457 | fd872598 | bellard | #endif
|
458 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
459 | 89bfc105 | bellard | if (env)
|
460 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
461 | 89bfc105 | bellard | #endif
|
462 | fd872598 | bellard | return val;
|
463 | 0824d6fc | bellard | } |
464 | 0824d6fc | bellard | |
465 | c45886db | bellard | int cpu_inw(CPUState *env, int addr) |
466 | 0824d6fc | bellard | { |
467 | fd872598 | bellard | int val;
|
468 | 477e3edf | aliguori | val = ioport_read(1, addr);
|
469 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
470 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
471 | fd872598 | bellard | fprintf(logfile, "inw : %04x %04x\n", addr, val);
|
472 | fd872598 | bellard | #endif
|
473 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
474 | 89bfc105 | bellard | if (env)
|
475 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
476 | 89bfc105 | bellard | #endif
|
477 | fd872598 | bellard | return val;
|
478 | 0824d6fc | bellard | } |
479 | 0824d6fc | bellard | |
480 | c45886db | bellard | int cpu_inl(CPUState *env, int addr) |
481 | 0824d6fc | bellard | { |
482 | fd872598 | bellard | int val;
|
483 | 477e3edf | aliguori | val = ioport_read(2, addr);
|
484 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
485 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
486 | fd872598 | bellard | fprintf(logfile, "inl : %04x %08x\n", addr, val);
|
487 | fd872598 | bellard | #endif
|
488 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
489 | 89bfc105 | bellard | if (env)
|
490 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
491 | 89bfc105 | bellard | #endif
|
492 | fd872598 | bellard | return val;
|
493 | 0824d6fc | bellard | } |
494 | 0824d6fc | bellard | |
495 | 0824d6fc | bellard | /***********************************************************/
|
496 | 0824d6fc | bellard | void hw_error(const char *fmt, ...) |
497 | 0824d6fc | bellard | { |
498 | 0824d6fc | bellard | va_list ap; |
499 | 6a00d601 | bellard | CPUState *env; |
500 | 0824d6fc | bellard | |
501 | 0824d6fc | bellard | va_start(ap, fmt); |
502 | 0824d6fc | bellard | fprintf(stderr, "qemu: hardware error: ");
|
503 | 0824d6fc | bellard | vfprintf(stderr, fmt, ap); |
504 | 0824d6fc | bellard | fprintf(stderr, "\n");
|
505 | 6a00d601 | bellard | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
506 | 6a00d601 | bellard | fprintf(stderr, "CPU #%d:\n", env->cpu_index);
|
507 | 0824d6fc | bellard | #ifdef TARGET_I386
|
508 | 6a00d601 | bellard | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
509 | c45886db | bellard | #else
|
510 | 6a00d601 | bellard | cpu_dump_state(env, stderr, fprintf, 0);
|
511 | 0824d6fc | bellard | #endif
|
512 | 6a00d601 | bellard | } |
513 | 0824d6fc | bellard | va_end(ap); |
514 | 0824d6fc | bellard | abort(); |
515 | 0824d6fc | bellard | } |
516 | 0824d6fc | bellard | |
517 | 8a7ddc38 | bellard | /***********************************************************/
|
518 | 63066f4f | bellard | /* keyboard/mouse */
|
519 | 63066f4f | bellard | |
520 | 63066f4f | bellard | static QEMUPutKBDEvent *qemu_put_kbd_event;
|
521 | 63066f4f | bellard | static void *qemu_put_kbd_event_opaque; |
522 | 455204eb | ths | static QEMUPutMouseEntry *qemu_put_mouse_event_head;
|
523 | 455204eb | ths | static QEMUPutMouseEntry *qemu_put_mouse_event_current;
|
524 | 63066f4f | bellard | |
525 | 63066f4f | bellard | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
526 | 63066f4f | bellard | { |
527 | 63066f4f | bellard | qemu_put_kbd_event_opaque = opaque; |
528 | 63066f4f | bellard | qemu_put_kbd_event = func; |
529 | 63066f4f | bellard | } |
530 | 63066f4f | bellard | |
531 | 455204eb | ths | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
532 | 455204eb | ths | void *opaque, int absolute, |
533 | 455204eb | ths | const char *name) |
534 | 63066f4f | bellard | { |
535 | 455204eb | ths | QEMUPutMouseEntry *s, *cursor; |
536 | 455204eb | ths | |
537 | 455204eb | ths | s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
|
538 | 455204eb | ths | if (!s)
|
539 | 455204eb | ths | return NULL; |
540 | 455204eb | ths | |
541 | 455204eb | ths | s->qemu_put_mouse_event = func; |
542 | 455204eb | ths | s->qemu_put_mouse_event_opaque = opaque; |
543 | 455204eb | ths | s->qemu_put_mouse_event_absolute = absolute; |
544 | 455204eb | ths | s->qemu_put_mouse_event_name = qemu_strdup(name); |
545 | 455204eb | ths | s->next = NULL;
|
546 | 455204eb | ths | |
547 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
548 | 455204eb | ths | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
549 | 455204eb | ths | return s;
|
550 | 455204eb | ths | } |
551 | 455204eb | ths | |
552 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
553 | 455204eb | ths | while (cursor->next != NULL) |
554 | 455204eb | ths | cursor = cursor->next; |
555 | 455204eb | ths | |
556 | 455204eb | ths | cursor->next = s; |
557 | 455204eb | ths | qemu_put_mouse_event_current = s; |
558 | 455204eb | ths | |
559 | 455204eb | ths | return s;
|
560 | 455204eb | ths | } |
561 | 455204eb | ths | |
562 | 455204eb | ths | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
|
563 | 455204eb | ths | { |
564 | 455204eb | ths | QEMUPutMouseEntry *prev = NULL, *cursor;
|
565 | 455204eb | ths | |
566 | 455204eb | ths | if (!qemu_put_mouse_event_head || entry == NULL) |
567 | 455204eb | ths | return;
|
568 | 455204eb | ths | |
569 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
570 | 455204eb | ths | while (cursor != NULL && cursor != entry) { |
571 | 455204eb | ths | prev = cursor; |
572 | 455204eb | ths | cursor = cursor->next; |
573 | 455204eb | ths | } |
574 | 455204eb | ths | |
575 | 455204eb | ths | if (cursor == NULL) // does not exist or list empty |
576 | 455204eb | ths | return;
|
577 | 455204eb | ths | else if (prev == NULL) { // entry is head |
578 | 455204eb | ths | qemu_put_mouse_event_head = cursor->next; |
579 | 455204eb | ths | if (qemu_put_mouse_event_current == entry)
|
580 | 455204eb | ths | qemu_put_mouse_event_current = cursor->next; |
581 | 455204eb | ths | qemu_free(entry->qemu_put_mouse_event_name); |
582 | 455204eb | ths | qemu_free(entry); |
583 | 455204eb | ths | return;
|
584 | 455204eb | ths | } |
585 | 455204eb | ths | |
586 | 455204eb | ths | prev->next = entry->next; |
587 | 455204eb | ths | |
588 | 455204eb | ths | if (qemu_put_mouse_event_current == entry)
|
589 | 455204eb | ths | qemu_put_mouse_event_current = prev; |
590 | 455204eb | ths | |
591 | 455204eb | ths | qemu_free(entry->qemu_put_mouse_event_name); |
592 | 455204eb | ths | qemu_free(entry); |
593 | 63066f4f | bellard | } |
594 | 63066f4f | bellard | |
595 | 63066f4f | bellard | void kbd_put_keycode(int keycode) |
596 | 63066f4f | bellard | { |
597 | 63066f4f | bellard | if (qemu_put_kbd_event) {
|
598 | 63066f4f | bellard | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
599 | 63066f4f | bellard | } |
600 | 63066f4f | bellard | } |
601 | 63066f4f | bellard | |
602 | 63066f4f | bellard | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
603 | 63066f4f | bellard | { |
604 | 455204eb | ths | QEMUPutMouseEvent *mouse_event; |
605 | 455204eb | ths | void *mouse_event_opaque;
|
606 | a171fe39 | balrog | int width;
|
607 | 455204eb | ths | |
608 | 455204eb | ths | if (!qemu_put_mouse_event_current) {
|
609 | 455204eb | ths | return;
|
610 | 455204eb | ths | } |
611 | 455204eb | ths | |
612 | 455204eb | ths | mouse_event = |
613 | 455204eb | ths | qemu_put_mouse_event_current->qemu_put_mouse_event; |
614 | 455204eb | ths | mouse_event_opaque = |
615 | 455204eb | ths | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
616 | 455204eb | ths | |
617 | 455204eb | ths | if (mouse_event) {
|
618 | a171fe39 | balrog | if (graphic_rotate) {
|
619 | a171fe39 | balrog | if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
|
620 | a171fe39 | balrog | width = 0x7fff;
|
621 | a171fe39 | balrog | else
|
622 | b94ed577 | aurel32 | width = graphic_width - 1;
|
623 | a171fe39 | balrog | mouse_event(mouse_event_opaque, |
624 | a171fe39 | balrog | width - dy, dx, dz, buttons_state); |
625 | a171fe39 | balrog | } else
|
626 | a171fe39 | balrog | mouse_event(mouse_event_opaque, |
627 | a171fe39 | balrog | dx, dy, dz, buttons_state); |
628 | 63066f4f | bellard | } |
629 | 63066f4f | bellard | } |
630 | 63066f4f | bellard | |
631 | 09b26c5e | bellard | int kbd_mouse_is_absolute(void) |
632 | 09b26c5e | bellard | { |
633 | 455204eb | ths | if (!qemu_put_mouse_event_current)
|
634 | 455204eb | ths | return 0; |
635 | 455204eb | ths | |
636 | 455204eb | ths | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
|
637 | 455204eb | ths | } |
638 | 455204eb | ths | |
639 | 455204eb | ths | void do_info_mice(void) |
640 | 455204eb | ths | { |
641 | 455204eb | ths | QEMUPutMouseEntry *cursor; |
642 | 455204eb | ths | int index = 0; |
643 | 455204eb | ths | |
644 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
645 | 455204eb | ths | term_printf("No mouse devices connected\n");
|
646 | 455204eb | ths | return;
|
647 | 455204eb | ths | } |
648 | 455204eb | ths | |
649 | 455204eb | ths | term_printf("Mouse devices available:\n");
|
650 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
651 | 455204eb | ths | while (cursor != NULL) { |
652 | 455204eb | ths | term_printf("%c Mouse #%d: %s\n",
|
653 | 455204eb | ths | (cursor == qemu_put_mouse_event_current ? '*' : ' '), |
654 | 455204eb | ths | index, cursor->qemu_put_mouse_event_name); |
655 | 455204eb | ths | index++; |
656 | 455204eb | ths | cursor = cursor->next; |
657 | 455204eb | ths | } |
658 | 455204eb | ths | } |
659 | 455204eb | ths | |
660 | 455204eb | ths | void do_mouse_set(int index) |
661 | 455204eb | ths | { |
662 | 455204eb | ths | QEMUPutMouseEntry *cursor; |
663 | 455204eb | ths | int i = 0; |
664 | 455204eb | ths | |
665 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
666 | 455204eb | ths | term_printf("No mouse devices connected\n");
|
667 | 455204eb | ths | return;
|
668 | 455204eb | ths | } |
669 | 455204eb | ths | |
670 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
671 | 455204eb | ths | while (cursor != NULL && index != i) { |
672 | 455204eb | ths | i++; |
673 | 455204eb | ths | cursor = cursor->next; |
674 | 455204eb | ths | } |
675 | 455204eb | ths | |
676 | 455204eb | ths | if (cursor != NULL) |
677 | 455204eb | ths | qemu_put_mouse_event_current = cursor; |
678 | 455204eb | ths | else
|
679 | 455204eb | ths | term_printf("Mouse at given index not found\n");
|
680 | 09b26c5e | bellard | } |
681 | 09b26c5e | bellard | |
682 | 1dce7c3c | bellard | /* compute with 96 bit intermediate result: (a*b)/c */
|
683 | 1dce7c3c | bellard | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
684 | 0824d6fc | bellard | { |
685 | 1dce7c3c | bellard | union {
|
686 | 1dce7c3c | bellard | uint64_t ll; |
687 | 1dce7c3c | bellard | struct {
|
688 | 1dce7c3c | bellard | #ifdef WORDS_BIGENDIAN
|
689 | 1dce7c3c | bellard | uint32_t high, low; |
690 | 1dce7c3c | bellard | #else
|
691 | 1dce7c3c | bellard | uint32_t low, high; |
692 | 3b46e624 | ths | #endif
|
693 | 1dce7c3c | bellard | } l; |
694 | 1dce7c3c | bellard | } u, res; |
695 | 1dce7c3c | bellard | uint64_t rl, rh; |
696 | 0824d6fc | bellard | |
697 | 1dce7c3c | bellard | u.ll = a; |
698 | 1dce7c3c | bellard | rl = (uint64_t)u.l.low * (uint64_t)b; |
699 | 1dce7c3c | bellard | rh = (uint64_t)u.l.high * (uint64_t)b; |
700 | 1dce7c3c | bellard | rh += (rl >> 32);
|
701 | 1dce7c3c | bellard | res.l.high = rh / c; |
702 | 1dce7c3c | bellard | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
703 | 1dce7c3c | bellard | return res.ll;
|
704 | 34865134 | bellard | } |
705 | 34865134 | bellard | |
706 | 1dce7c3c | bellard | /***********************************************************/
|
707 | 1dce7c3c | bellard | /* real time host monotonic timer */
|
708 | 34865134 | bellard | |
709 | 1dce7c3c | bellard | #define QEMU_TIMER_BASE 1000000000LL |
710 | 34865134 | bellard | |
711 | 1dce7c3c | bellard | #ifdef WIN32
|
712 | 0824d6fc | bellard | |
713 | 1dce7c3c | bellard | static int64_t clock_freq;
|
714 | 1115dde7 | bellard | |
715 | 1dce7c3c | bellard | static void init_get_clock(void) |
716 | 1115dde7 | bellard | { |
717 | a8e5ac33 | bellard | LARGE_INTEGER freq; |
718 | a8e5ac33 | bellard | int ret;
|
719 | 1dce7c3c | bellard | ret = QueryPerformanceFrequency(&freq); |
720 | 1dce7c3c | bellard | if (ret == 0) { |
721 | 1dce7c3c | bellard | fprintf(stderr, "Could not calibrate ticks\n");
|
722 | 1dce7c3c | bellard | exit(1);
|
723 | 1dce7c3c | bellard | } |
724 | 1dce7c3c | bellard | clock_freq = freq.QuadPart; |
725 | 1115dde7 | bellard | } |
726 | 1115dde7 | bellard | |
727 | 1dce7c3c | bellard | static int64_t get_clock(void) |
728 | b8076a74 | bellard | { |
729 | 1dce7c3c | bellard | LARGE_INTEGER ti; |
730 | 1dce7c3c | bellard | QueryPerformanceCounter(&ti); |
731 | 1dce7c3c | bellard | return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
|
732 | b8076a74 | bellard | } |
733 | b8076a74 | bellard | |
734 | 1dce7c3c | bellard | #else
|
735 | 90cb9493 | bellard | |
736 | 1dce7c3c | bellard | static int use_rt_clock; |
737 | 1dce7c3c | bellard | |
738 | 1dce7c3c | bellard | static void init_get_clock(void) |
739 | 90cb9493 | bellard | { |
740 | 1dce7c3c | bellard | use_rt_clock = 0;
|
741 | 1dce7c3c | bellard | #if defined(__linux__)
|
742 | 1dce7c3c | bellard | { |
743 | 1dce7c3c | bellard | struct timespec ts;
|
744 | 1dce7c3c | bellard | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
745 | 1dce7c3c | bellard | use_rt_clock = 1;
|
746 | 1dce7c3c | bellard | } |
747 | 1dce7c3c | bellard | } |
748 | 1dce7c3c | bellard | #endif
|
749 | 90cb9493 | bellard | } |
750 | 90cb9493 | bellard | |
751 | 1dce7c3c | bellard | static int64_t get_clock(void) |
752 | fdbb4691 | bellard | { |
753 | 1dce7c3c | bellard | #if defined(__linux__)
|
754 | 1dce7c3c | bellard | if (use_rt_clock) {
|
755 | 1dce7c3c | bellard | struct timespec ts;
|
756 | 1dce7c3c | bellard | clock_gettime(CLOCK_MONOTONIC, &ts); |
757 | 1dce7c3c | bellard | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
758 | 5fafdf24 | ths | } else
|
759 | fdbb4691 | bellard | #endif
|
760 | 1dce7c3c | bellard | { |
761 | 1dce7c3c | bellard | /* XXX: using gettimeofday leads to problems if the date
|
762 | 1dce7c3c | bellard | changes, so it should be avoided. */
|
763 | 1dce7c3c | bellard | struct timeval tv;
|
764 | 1dce7c3c | bellard | gettimeofday(&tv, NULL);
|
765 | 1dce7c3c | bellard | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
766 | 1dce7c3c | bellard | } |
767 | fdbb4691 | bellard | } |
768 | 34865134 | bellard | #endif
|
769 | 34865134 | bellard | |
770 | 2e70f6ef | pbrook | /* Return the virtual CPU time, based on the instruction counter. */
|
771 | 2e70f6ef | pbrook | static int64_t cpu_get_icount(void) |
772 | 2e70f6ef | pbrook | { |
773 | 2e70f6ef | pbrook | int64_t icount; |
774 | 2e70f6ef | pbrook | CPUState *env = cpu_single_env;; |
775 | 2e70f6ef | pbrook | icount = qemu_icount; |
776 | 2e70f6ef | pbrook | if (env) {
|
777 | 2e70f6ef | pbrook | if (!can_do_io(env))
|
778 | 2e70f6ef | pbrook | fprintf(stderr, "Bad clock read\n");
|
779 | 2e70f6ef | pbrook | icount -= (env->icount_decr.u16.low + env->icount_extra); |
780 | 2e70f6ef | pbrook | } |
781 | 2e70f6ef | pbrook | return qemu_icount_bias + (icount << icount_time_shift);
|
782 | 2e70f6ef | pbrook | } |
783 | 2e70f6ef | pbrook | |
784 | 1dce7c3c | bellard | /***********************************************************/
|
785 | 1dce7c3c | bellard | /* guest cycle counter */
|
786 | 1dce7c3c | bellard | |
787 | eade0f19 | bellard | static int64_t cpu_ticks_prev;
|
788 | 34865134 | bellard | static int64_t cpu_ticks_offset;
|
789 | 1dce7c3c | bellard | static int64_t cpu_clock_offset;
|
790 | 8a7ddc38 | bellard | static int cpu_ticks_enabled; |
791 | 34865134 | bellard | |
792 | 1dce7c3c | bellard | /* return the host CPU cycle counter and handle stop/restart */
|
793 | 1dce7c3c | bellard | int64_t cpu_get_ticks(void)
|
794 | 34865134 | bellard | { |
795 | 2e70f6ef | pbrook | if (use_icount) {
|
796 | 2e70f6ef | pbrook | return cpu_get_icount();
|
797 | 2e70f6ef | pbrook | } |
798 | 8a7ddc38 | bellard | if (!cpu_ticks_enabled) {
|
799 | 8a7ddc38 | bellard | return cpu_ticks_offset;
|
800 | 8a7ddc38 | bellard | } else {
|
801 | eade0f19 | bellard | int64_t ticks; |
802 | eade0f19 | bellard | ticks = cpu_get_real_ticks(); |
803 | eade0f19 | bellard | if (cpu_ticks_prev > ticks) {
|
804 | eade0f19 | bellard | /* Note: non increasing ticks may happen if the host uses
|
805 | eade0f19 | bellard | software suspend */
|
806 | eade0f19 | bellard | cpu_ticks_offset += cpu_ticks_prev - ticks; |
807 | eade0f19 | bellard | } |
808 | eade0f19 | bellard | cpu_ticks_prev = ticks; |
809 | eade0f19 | bellard | return ticks + cpu_ticks_offset;
|
810 | 8a7ddc38 | bellard | } |
811 | 34865134 | bellard | } |
812 | 34865134 | bellard | |
813 | 1dce7c3c | bellard | /* return the host CPU monotonic timer and handle stop/restart */
|
814 | 1dce7c3c | bellard | static int64_t cpu_get_clock(void) |
815 | 1dce7c3c | bellard | { |
816 | 1dce7c3c | bellard | int64_t ti; |
817 | 1dce7c3c | bellard | if (!cpu_ticks_enabled) {
|
818 | 1dce7c3c | bellard | return cpu_clock_offset;
|
819 | 1dce7c3c | bellard | } else {
|
820 | 1dce7c3c | bellard | ti = get_clock(); |
821 | 1dce7c3c | bellard | return ti + cpu_clock_offset;
|
822 | 1dce7c3c | bellard | } |
823 | 1dce7c3c | bellard | } |
824 | 1dce7c3c | bellard | |
825 | 34865134 | bellard | /* enable cpu_get_ticks() */
|
826 | 34865134 | bellard | void cpu_enable_ticks(void) |
827 | 34865134 | bellard | { |
828 | 8a7ddc38 | bellard | if (!cpu_ticks_enabled) {
|
829 | 8a7ddc38 | bellard | cpu_ticks_offset -= cpu_get_real_ticks(); |
830 | 1dce7c3c | bellard | cpu_clock_offset -= get_clock(); |
831 | 8a7ddc38 | bellard | cpu_ticks_enabled = 1;
|
832 | 8a7ddc38 | bellard | } |
833 | 34865134 | bellard | } |
834 | 34865134 | bellard | |
835 | 34865134 | bellard | /* disable cpu_get_ticks() : the clock is stopped. You must not call
|
836 | 34865134 | bellard | cpu_get_ticks() after that. */
|
837 | 34865134 | bellard | void cpu_disable_ticks(void) |
838 | 34865134 | bellard | { |
839 | 8a7ddc38 | bellard | if (cpu_ticks_enabled) {
|
840 | 8a7ddc38 | bellard | cpu_ticks_offset = cpu_get_ticks(); |
841 | 1dce7c3c | bellard | cpu_clock_offset = cpu_get_clock(); |
842 | 8a7ddc38 | bellard | cpu_ticks_enabled = 0;
|
843 | 8a7ddc38 | bellard | } |
844 | 34865134 | bellard | } |
845 | 34865134 | bellard | |
846 | 1dce7c3c | bellard | /***********************************************************/
|
847 | 1dce7c3c | bellard | /* timers */
|
848 | 5fafdf24 | ths | |
849 | 8a7ddc38 | bellard | #define QEMU_TIMER_REALTIME 0 |
850 | 8a7ddc38 | bellard | #define QEMU_TIMER_VIRTUAL 1 |
851 | 8a7ddc38 | bellard | |
852 | 8a7ddc38 | bellard | struct QEMUClock {
|
853 | 8a7ddc38 | bellard | int type;
|
854 | 8a7ddc38 | bellard | /* XXX: add frequency */
|
855 | 8a7ddc38 | bellard | }; |
856 | 8a7ddc38 | bellard | |
857 | 8a7ddc38 | bellard | struct QEMUTimer {
|
858 | 8a7ddc38 | bellard | QEMUClock *clock; |
859 | 8a7ddc38 | bellard | int64_t expire_time; |
860 | 8a7ddc38 | bellard | QEMUTimerCB *cb; |
861 | 8a7ddc38 | bellard | void *opaque;
|
862 | 8a7ddc38 | bellard | struct QEMUTimer *next;
|
863 | 8a7ddc38 | bellard | }; |
864 | 8a7ddc38 | bellard | |
865 | c8994013 | ths | struct qemu_alarm_timer {
|
866 | c8994013 | ths | char const *name; |
867 | efe75411 | ths | unsigned int flags; |
868 | c8994013 | ths | |
869 | c8994013 | ths | int (*start)(struct qemu_alarm_timer *t); |
870 | c8994013 | ths | void (*stop)(struct qemu_alarm_timer *t); |
871 | efe75411 | ths | void (*rearm)(struct qemu_alarm_timer *t); |
872 | c8994013 | ths | void *priv;
|
873 | c8994013 | ths | }; |
874 | c8994013 | ths | |
875 | efe75411 | ths | #define ALARM_FLAG_DYNTICKS 0x1 |
876 | d5d08334 | balrog | #define ALARM_FLAG_EXPIRED 0x2 |
877 | efe75411 | ths | |
878 | efe75411 | ths | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
879 | efe75411 | ths | { |
880 | efe75411 | ths | return t->flags & ALARM_FLAG_DYNTICKS;
|
881 | efe75411 | ths | } |
882 | efe75411 | ths | |
883 | efe75411 | ths | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
884 | efe75411 | ths | { |
885 | efe75411 | ths | if (!alarm_has_dynticks(t))
|
886 | efe75411 | ths | return;
|
887 | efe75411 | ths | |
888 | efe75411 | ths | t->rearm(t); |
889 | efe75411 | ths | } |
890 | efe75411 | ths | |
891 | efe75411 | ths | /* TODO: MIN_TIMER_REARM_US should be optimized */
|
892 | efe75411 | ths | #define MIN_TIMER_REARM_US 250 |
893 | efe75411 | ths | |
894 | c8994013 | ths | static struct qemu_alarm_timer *alarm_timer; |
895 | 8a7ddc38 | bellard | |
896 | 40c3bac3 | bellard | #ifdef _WIN32
|
897 | c8994013 | ths | |
898 | c8994013 | ths | struct qemu_alarm_win32 {
|
899 | c8994013 | ths | MMRESULT timerId; |
900 | c8994013 | ths | HANDLE host_alarm; |
901 | c8994013 | ths | unsigned int period; |
902 | c8994013 | ths | } alarm_win32_data = {0, NULL, -1}; |
903 | c8994013 | ths | |
904 | c8994013 | ths | static int win32_start_timer(struct qemu_alarm_timer *t); |
905 | c8994013 | ths | static void win32_stop_timer(struct qemu_alarm_timer *t); |
906 | efe75411 | ths | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
907 | c8994013 | ths | |
908 | 40c3bac3 | bellard | #else
|
909 | c8994013 | ths | |
910 | c8994013 | ths | static int unix_start_timer(struct qemu_alarm_timer *t); |
911 | c8994013 | ths | static void unix_stop_timer(struct qemu_alarm_timer *t); |
912 | c8994013 | ths | |
913 | 231c6586 | ths | #ifdef __linux__
|
914 | 231c6586 | ths | |
915 | efe75411 | ths | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
916 | efe75411 | ths | static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
917 | efe75411 | ths | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
918 | efe75411 | ths | |
919 | c40ec5a9 | ths | static int hpet_start_timer(struct qemu_alarm_timer *t); |
920 | c40ec5a9 | ths | static void hpet_stop_timer(struct qemu_alarm_timer *t); |
921 | c40ec5a9 | ths | |
922 | c8994013 | ths | static int rtc_start_timer(struct qemu_alarm_timer *t); |
923 | c8994013 | ths | static void rtc_stop_timer(struct qemu_alarm_timer *t); |
924 | c8994013 | ths | |
925 | efe75411 | ths | #endif /* __linux__ */ |
926 | 8a7ddc38 | bellard | |
927 | c8994013 | ths | #endif /* _WIN32 */ |
928 | c8994013 | ths | |
929 | 2e70f6ef | pbrook | /* Correlation between real and virtual time is always going to be
|
930 | bf20dc07 | ths | fairly approximate, so ignore small variation.
|
931 | 2e70f6ef | pbrook | When the guest is idle real and virtual time will be aligned in
|
932 | 2e70f6ef | pbrook | the IO wait loop. */
|
933 | 2e70f6ef | pbrook | #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10) |
934 | 2e70f6ef | pbrook | |
935 | 2e70f6ef | pbrook | static void icount_adjust(void) |
936 | 2e70f6ef | pbrook | { |
937 | 2e70f6ef | pbrook | int64_t cur_time; |
938 | 2e70f6ef | pbrook | int64_t cur_icount; |
939 | 2e70f6ef | pbrook | int64_t delta; |
940 | 2e70f6ef | pbrook | static int64_t last_delta;
|
941 | 2e70f6ef | pbrook | /* If the VM is not running, then do nothing. */
|
942 | 2e70f6ef | pbrook | if (!vm_running)
|
943 | 2e70f6ef | pbrook | return;
|
944 | 2e70f6ef | pbrook | |
945 | 2e70f6ef | pbrook | cur_time = cpu_get_clock(); |
946 | 2e70f6ef | pbrook | cur_icount = qemu_get_clock(vm_clock); |
947 | 2e70f6ef | pbrook | delta = cur_icount - cur_time; |
948 | 2e70f6ef | pbrook | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
|
949 | 2e70f6ef | pbrook | if (delta > 0 |
950 | 2e70f6ef | pbrook | && last_delta + ICOUNT_WOBBLE < delta * 2
|
951 | 2e70f6ef | pbrook | && icount_time_shift > 0) {
|
952 | 2e70f6ef | pbrook | /* The guest is getting too far ahead. Slow time down. */
|
953 | 2e70f6ef | pbrook | icount_time_shift--; |
954 | 2e70f6ef | pbrook | } |
955 | 2e70f6ef | pbrook | if (delta < 0 |
956 | 2e70f6ef | pbrook | && last_delta - ICOUNT_WOBBLE > delta * 2
|
957 | 2e70f6ef | pbrook | && icount_time_shift < MAX_ICOUNT_SHIFT) { |
958 | 2e70f6ef | pbrook | /* The guest is getting too far behind. Speed time up. */
|
959 | 2e70f6ef | pbrook | icount_time_shift++; |
960 | 2e70f6ef | pbrook | } |
961 | 2e70f6ef | pbrook | last_delta = delta; |
962 | 2e70f6ef | pbrook | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
963 | 2e70f6ef | pbrook | } |
964 | 2e70f6ef | pbrook | |
965 | 2e70f6ef | pbrook | static void icount_adjust_rt(void * opaque) |
966 | 2e70f6ef | pbrook | { |
967 | 2e70f6ef | pbrook | qemu_mod_timer(icount_rt_timer, |
968 | 2e70f6ef | pbrook | qemu_get_clock(rt_clock) + 1000);
|
969 | 2e70f6ef | pbrook | icount_adjust(); |
970 | 2e70f6ef | pbrook | } |
971 | 2e70f6ef | pbrook | |
972 | 2e70f6ef | pbrook | static void icount_adjust_vm(void * opaque) |
973 | 2e70f6ef | pbrook | { |
974 | 2e70f6ef | pbrook | qemu_mod_timer(icount_vm_timer, |
975 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
|
976 | 2e70f6ef | pbrook | icount_adjust(); |
977 | 2e70f6ef | pbrook | } |
978 | 2e70f6ef | pbrook | |
979 | 2e70f6ef | pbrook | static void init_icount_adjust(void) |
980 | 2e70f6ef | pbrook | { |
981 | 2e70f6ef | pbrook | /* Have both realtime and virtual time triggers for speed adjustment.
|
982 | 2e70f6ef | pbrook | The realtime trigger catches emulated time passing too slowly,
|
983 | 2e70f6ef | pbrook | the virtual time trigger catches emulated time passing too fast.
|
984 | 2e70f6ef | pbrook | Realtime triggers occur even when idle, so use them less frequently
|
985 | 2e70f6ef | pbrook | than VM triggers. */
|
986 | 2e70f6ef | pbrook | icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
|
987 | 2e70f6ef | pbrook | qemu_mod_timer(icount_rt_timer, |
988 | 2e70f6ef | pbrook | qemu_get_clock(rt_clock) + 1000);
|
989 | 2e70f6ef | pbrook | icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
|
990 | 2e70f6ef | pbrook | qemu_mod_timer(icount_vm_timer, |
991 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
|
992 | 2e70f6ef | pbrook | } |
993 | 2e70f6ef | pbrook | |
994 | c8994013 | ths | static struct qemu_alarm_timer alarm_timers[] = { |
995 | efe75411 | ths | #ifndef _WIN32
|
996 | 231c6586 | ths | #ifdef __linux__
|
997 | efe75411 | ths | {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
|
998 | efe75411 | ths | dynticks_stop_timer, dynticks_rearm_timer, NULL},
|
999 | c40ec5a9 | ths | /* HPET - if available - is preferred */
|
1000 | efe75411 | ths | {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
1001 | c40ec5a9 | ths | /* ...otherwise try RTC */
|
1002 | efe75411 | ths | {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
1003 | c8994013 | ths | #endif
|
1004 | efe75411 | ths | {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
1005 | c8994013 | ths | #else
|
1006 | efe75411 | ths | {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
|
1007 | efe75411 | ths | win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
1008 | efe75411 | ths | {"win32", 0, win32_start_timer, |
1009 | efe75411 | ths | win32_stop_timer, NULL, &alarm_win32_data},
|
1010 | c8994013 | ths | #endif
|
1011 | c8994013 | ths | {NULL, }
|
1012 | c8994013 | ths | }; |
1013 | c8994013 | ths | |
1014 | 3f47aa8c | blueswir1 | static void show_available_alarms(void) |
1015 | f3dcfada | ths | { |
1016 | f3dcfada | ths | int i;
|
1017 | f3dcfada | ths | |
1018 | f3dcfada | ths | printf("Available alarm timers, in order of precedence:\n");
|
1019 | f3dcfada | ths | for (i = 0; alarm_timers[i].name; i++) |
1020 | f3dcfada | ths | printf("%s\n", alarm_timers[i].name);
|
1021 | f3dcfada | ths | } |
1022 | f3dcfada | ths | |
1023 | f3dcfada | ths | static void configure_alarms(char const *opt) |
1024 | f3dcfada | ths | { |
1025 | f3dcfada | ths | int i;
|
1026 | f3dcfada | ths | int cur = 0; |
1027 | f3dcfada | ths | int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1; |
1028 | f3dcfada | ths | char *arg;
|
1029 | f3dcfada | ths | char *name;
|
1030 | 2e70f6ef | pbrook | struct qemu_alarm_timer tmp;
|
1031 | f3dcfada | ths | |
1032 | 3adda04c | aurel32 | if (!strcmp(opt, "?")) { |
1033 | f3dcfada | ths | show_available_alarms(); |
1034 | f3dcfada | ths | exit(0);
|
1035 | f3dcfada | ths | } |
1036 | f3dcfada | ths | |
1037 | f3dcfada | ths | arg = strdup(opt); |
1038 | f3dcfada | ths | |
1039 | f3dcfada | ths | /* Reorder the array */
|
1040 | f3dcfada | ths | name = strtok(arg, ",");
|
1041 | f3dcfada | ths | while (name) {
|
1042 | e2b577e5 | balrog | for (i = 0; i < count && alarm_timers[i].name; i++) { |
1043 | f3dcfada | ths | if (!strcmp(alarm_timers[i].name, name))
|
1044 | f3dcfada | ths | break;
|
1045 | f3dcfada | ths | } |
1046 | f3dcfada | ths | |
1047 | f3dcfada | ths | if (i == count) {
|
1048 | f3dcfada | ths | fprintf(stderr, "Unknown clock %s\n", name);
|
1049 | f3dcfada | ths | goto next;
|
1050 | f3dcfada | ths | } |
1051 | f3dcfada | ths | |
1052 | f3dcfada | ths | if (i < cur)
|
1053 | f3dcfada | ths | /* Ignore */
|
1054 | f3dcfada | ths | goto next;
|
1055 | f3dcfada | ths | |
1056 | f3dcfada | ths | /* Swap */
|
1057 | f3dcfada | ths | tmp = alarm_timers[i]; |
1058 | f3dcfada | ths | alarm_timers[i] = alarm_timers[cur]; |
1059 | f3dcfada | ths | alarm_timers[cur] = tmp; |
1060 | f3dcfada | ths | |
1061 | f3dcfada | ths | cur++; |
1062 | f3dcfada | ths | next:
|
1063 | f3dcfada | ths | name = strtok(NULL, ","); |
1064 | f3dcfada | ths | } |
1065 | f3dcfada | ths | |
1066 | f3dcfada | ths | free(arg); |
1067 | f3dcfada | ths | |
1068 | f3dcfada | ths | if (cur) {
|
1069 | 2e70f6ef | pbrook | /* Disable remaining timers */
|
1070 | f3dcfada | ths | for (i = cur; i < count; i++)
|
1071 | f3dcfada | ths | alarm_timers[i].name = NULL;
|
1072 | 3adda04c | aurel32 | } else {
|
1073 | 3adda04c | aurel32 | show_available_alarms(); |
1074 | 3adda04c | aurel32 | exit(1);
|
1075 | f3dcfada | ths | } |
1076 | f3dcfada | ths | } |
1077 | f3dcfada | ths | |
1078 | c8994013 | ths | QEMUClock *rt_clock; |
1079 | c8994013 | ths | QEMUClock *vm_clock; |
1080 | c8994013 | ths | |
1081 | c8994013 | ths | static QEMUTimer *active_timers[2]; |
1082 | c8994013 | ths | |
1083 | 9596ebb7 | pbrook | static QEMUClock *qemu_new_clock(int type) |
1084 | 8a7ddc38 | bellard | { |
1085 | 8a7ddc38 | bellard | QEMUClock *clock; |
1086 | 8a7ddc38 | bellard | clock = qemu_mallocz(sizeof(QEMUClock));
|
1087 | 8a7ddc38 | bellard | if (!clock)
|
1088 | 8a7ddc38 | bellard | return NULL; |
1089 | 8a7ddc38 | bellard | clock->type = type; |
1090 | 8a7ddc38 | bellard | return clock;
|
1091 | 8a7ddc38 | bellard | } |
1092 | 8a7ddc38 | bellard | |
1093 | 8a7ddc38 | bellard | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
|
1094 | 8a7ddc38 | bellard | { |
1095 | 8a7ddc38 | bellard | QEMUTimer *ts; |
1096 | 8a7ddc38 | bellard | |
1097 | 8a7ddc38 | bellard | ts = qemu_mallocz(sizeof(QEMUTimer));
|
1098 | 8a7ddc38 | bellard | ts->clock = clock; |
1099 | 8a7ddc38 | bellard | ts->cb = cb; |
1100 | 8a7ddc38 | bellard | ts->opaque = opaque; |
1101 | 8a7ddc38 | bellard | return ts;
|
1102 | 8a7ddc38 | bellard | } |
1103 | 8a7ddc38 | bellard | |
1104 | 8a7ddc38 | bellard | void qemu_free_timer(QEMUTimer *ts)
|
1105 | 8a7ddc38 | bellard | { |
1106 | 8a7ddc38 | bellard | qemu_free(ts); |
1107 | 8a7ddc38 | bellard | } |
1108 | 8a7ddc38 | bellard | |
1109 | 8a7ddc38 | bellard | /* stop a timer, but do not dealloc it */
|
1110 | 8a7ddc38 | bellard | void qemu_del_timer(QEMUTimer *ts)
|
1111 | 8a7ddc38 | bellard | { |
1112 | 8a7ddc38 | bellard | QEMUTimer **pt, *t; |
1113 | 8a7ddc38 | bellard | |
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 | 8a7ddc38 | bellard | if (t == ts) {
|
1122 | 8a7ddc38 | bellard | *pt = t->next; |
1123 | 8a7ddc38 | bellard | break;
|
1124 | 8a7ddc38 | bellard | } |
1125 | 8a7ddc38 | bellard | pt = &t->next; |
1126 | 8a7ddc38 | bellard | } |
1127 | 8a7ddc38 | bellard | } |
1128 | 8a7ddc38 | bellard | |
1129 | 8a7ddc38 | bellard | /* modify the current timer so that it will be fired when current_time
|
1130 | 8a7ddc38 | bellard | >= expire_time. The corresponding callback will be called. */
|
1131 | 8a7ddc38 | bellard | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
|
1132 | 8a7ddc38 | bellard | { |
1133 | 8a7ddc38 | bellard | QEMUTimer **pt, *t; |
1134 | 8a7ddc38 | bellard | |
1135 | 8a7ddc38 | bellard | qemu_del_timer(ts); |
1136 | 8a7ddc38 | bellard | |
1137 | 8a7ddc38 | bellard | /* add the timer in the sorted list */
|
1138 | 8a7ddc38 | bellard | /* NOTE: this code must be signal safe because
|
1139 | 8a7ddc38 | bellard | qemu_timer_expired() can be called from a signal. */
|
1140 | 8a7ddc38 | bellard | pt = &active_timers[ts->clock->type]; |
1141 | 8a7ddc38 | bellard | for(;;) {
|
1142 | 8a7ddc38 | bellard | t = *pt; |
1143 | 8a7ddc38 | bellard | if (!t)
|
1144 | 8a7ddc38 | bellard | break;
|
1145 | 5fafdf24 | ths | if (t->expire_time > expire_time)
|
1146 | 8a7ddc38 | bellard | break;
|
1147 | 8a7ddc38 | bellard | pt = &t->next; |
1148 | 8a7ddc38 | bellard | } |
1149 | 8a7ddc38 | bellard | ts->expire_time = expire_time; |
1150 | 8a7ddc38 | bellard | ts->next = *pt; |
1151 | 8a7ddc38 | bellard | *pt = ts; |
1152 | d5d08334 | balrog | |
1153 | d5d08334 | balrog | /* Rearm if necessary */
|
1154 | 2e70f6ef | pbrook | if (pt == &active_timers[ts->clock->type]) {
|
1155 | 2e70f6ef | pbrook | if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { |
1156 | 2e70f6ef | pbrook | qemu_rearm_alarm_timer(alarm_timer); |
1157 | 2e70f6ef | pbrook | } |
1158 | 2e70f6ef | pbrook | /* Interrupt execution to force deadline recalculation. */
|
1159 | 2e70f6ef | pbrook | if (use_icount && cpu_single_env) {
|
1160 | 2e70f6ef | pbrook | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
1161 | 2e70f6ef | pbrook | } |
1162 | 2e70f6ef | pbrook | } |
1163 | 8a7ddc38 | bellard | } |
1164 | 8a7ddc38 | bellard | |
1165 | 8a7ddc38 | bellard | int qemu_timer_pending(QEMUTimer *ts)
|
1166 | 8a7ddc38 | bellard | { |
1167 | 8a7ddc38 | bellard | QEMUTimer *t; |
1168 | 8a7ddc38 | bellard | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
1169 | 8a7ddc38 | bellard | if (t == ts)
|
1170 | 8a7ddc38 | bellard | return 1; |
1171 | 8a7ddc38 | bellard | } |
1172 | 8a7ddc38 | bellard | return 0; |
1173 | 8a7ddc38 | bellard | } |
1174 | 8a7ddc38 | bellard | |
1175 | 8a7ddc38 | bellard | static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
1176 | 8a7ddc38 | bellard | { |
1177 | 8a7ddc38 | bellard | if (!timer_head)
|
1178 | 8a7ddc38 | bellard | return 0; |
1179 | 8a7ddc38 | bellard | return (timer_head->expire_time <= current_time);
|
1180 | 8a7ddc38 | bellard | } |
1181 | 8a7ddc38 | bellard | |
1182 | 8a7ddc38 | bellard | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
1183 | 8a7ddc38 | bellard | { |
1184 | 8a7ddc38 | bellard | QEMUTimer *ts; |
1185 | 3b46e624 | ths | |
1186 | 8a7ddc38 | bellard | for(;;) {
|
1187 | 8a7ddc38 | bellard | ts = *ptimer_head; |
1188 | e95c8d51 | bellard | if (!ts || ts->expire_time > current_time)
|
1189 | 8a7ddc38 | bellard | break;
|
1190 | 8a7ddc38 | bellard | /* remove timer from the list before calling the callback */
|
1191 | 8a7ddc38 | bellard | *ptimer_head = ts->next; |
1192 | 8a7ddc38 | bellard | ts->next = NULL;
|
1193 | 3b46e624 | ths | |
1194 | 8a7ddc38 | bellard | /* run the callback (the timer list can be modified) */
|
1195 | 8a7ddc38 | bellard | ts->cb(ts->opaque); |
1196 | 8a7ddc38 | bellard | } |
1197 | 8a7ddc38 | bellard | } |
1198 | 8a7ddc38 | bellard | |
1199 | 8a7ddc38 | bellard | int64_t qemu_get_clock(QEMUClock *clock) |
1200 | 8a7ddc38 | bellard | { |
1201 | 8a7ddc38 | bellard | switch(clock->type) {
|
1202 | 8a7ddc38 | bellard | case QEMU_TIMER_REALTIME:
|
1203 | 1dce7c3c | bellard | return get_clock() / 1000000; |
1204 | 8a7ddc38 | bellard | default:
|
1205 | 8a7ddc38 | bellard | case QEMU_TIMER_VIRTUAL:
|
1206 | 2e70f6ef | pbrook | if (use_icount) {
|
1207 | 2e70f6ef | pbrook | return cpu_get_icount();
|
1208 | 2e70f6ef | pbrook | } else {
|
1209 | 2e70f6ef | pbrook | return cpu_get_clock();
|
1210 | 2e70f6ef | pbrook | } |
1211 | 8a7ddc38 | bellard | } |
1212 | 8a7ddc38 | bellard | } |
1213 | 8a7ddc38 | bellard | |
1214 | 1dce7c3c | bellard | static void init_timers(void) |
1215 | 1dce7c3c | bellard | { |
1216 | 1dce7c3c | bellard | init_get_clock(); |
1217 | 1dce7c3c | bellard | ticks_per_sec = QEMU_TIMER_BASE; |
1218 | 1dce7c3c | bellard | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
1219 | 1dce7c3c | bellard | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
1220 | 1dce7c3c | bellard | } |
1221 | 1dce7c3c | bellard | |
1222 | 8a7ddc38 | bellard | /* save a timer */
|
1223 | 8a7ddc38 | bellard | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
|
1224 | 8a7ddc38 | bellard | { |
1225 | 8a7ddc38 | bellard | uint64_t expire_time; |
1226 | 8a7ddc38 | bellard | |
1227 | 8a7ddc38 | bellard | if (qemu_timer_pending(ts)) {
|
1228 | 8a7ddc38 | bellard | expire_time = ts->expire_time; |
1229 | 8a7ddc38 | bellard | } else {
|
1230 | 8a7ddc38 | bellard | expire_time = -1;
|
1231 | 8a7ddc38 | bellard | } |
1232 | 8a7ddc38 | bellard | qemu_put_be64(f, expire_time); |
1233 | 8a7ddc38 | bellard | } |
1234 | 8a7ddc38 | bellard | |
1235 | 8a7ddc38 | bellard | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
|
1236 | 8a7ddc38 | bellard | { |
1237 | 8a7ddc38 | bellard | uint64_t expire_time; |
1238 | 8a7ddc38 | bellard | |
1239 | 8a7ddc38 | bellard | expire_time = qemu_get_be64(f); |
1240 | 8a7ddc38 | bellard | if (expire_time != -1) { |
1241 | 8a7ddc38 | bellard | qemu_mod_timer(ts, expire_time); |
1242 | 8a7ddc38 | bellard | } else {
|
1243 | 8a7ddc38 | bellard | qemu_del_timer(ts); |
1244 | 8a7ddc38 | bellard | } |
1245 | 8a7ddc38 | bellard | } |
1246 | 8a7ddc38 | bellard | |
1247 | 8a7ddc38 | bellard | static void timer_save(QEMUFile *f, void *opaque) |
1248 | 8a7ddc38 | bellard | { |
1249 | 8a7ddc38 | bellard | if (cpu_ticks_enabled) {
|
1250 | 8a7ddc38 | bellard | hw_error("cannot save state if virtual timers are running");
|
1251 | 8a7ddc38 | bellard | } |
1252 | bee8d684 | ths | qemu_put_be64(f, cpu_ticks_offset); |
1253 | bee8d684 | ths | qemu_put_be64(f, ticks_per_sec); |
1254 | bee8d684 | ths | qemu_put_be64(f, cpu_clock_offset); |
1255 | 8a7ddc38 | bellard | } |
1256 | 8a7ddc38 | bellard | |
1257 | 8a7ddc38 | bellard | static int timer_load(QEMUFile *f, void *opaque, int version_id) |
1258 | 8a7ddc38 | bellard | { |
1259 | c88676f8 | bellard | if (version_id != 1 && version_id != 2) |
1260 | 8a7ddc38 | bellard | return -EINVAL;
|
1261 | 8a7ddc38 | bellard | if (cpu_ticks_enabled) {
|
1262 | 8a7ddc38 | bellard | return -EINVAL;
|
1263 | 8a7ddc38 | bellard | } |
1264 | bee8d684 | ths | cpu_ticks_offset=qemu_get_be64(f); |
1265 | bee8d684 | ths | ticks_per_sec=qemu_get_be64(f); |
1266 | c88676f8 | bellard | if (version_id == 2) { |
1267 | bee8d684 | ths | cpu_clock_offset=qemu_get_be64(f); |
1268 | c88676f8 | bellard | } |
1269 | 8a7ddc38 | bellard | return 0; |
1270 | 8a7ddc38 | bellard | } |
1271 | 8a7ddc38 | bellard | |
1272 | 67b915a5 | bellard | #ifdef _WIN32
|
1273 | 5fafdf24 | ths | void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
|
1274 | 67b915a5 | bellard | DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
1275 | 67b915a5 | bellard | #else
|
1276 | 8a7ddc38 | bellard | static void host_alarm_handler(int host_signum) |
1277 | 67b915a5 | bellard | #endif
|
1278 | 8a7ddc38 | bellard | { |
1279 | 02ba45c5 | bellard | #if 0
|
1280 | 02ba45c5 | bellard | #define DISP_FREQ 1000
|
1281 | 02ba45c5 | bellard | {
|
1282 | 02ba45c5 | bellard | static int64_t delta_min = INT64_MAX;
|
1283 | 02ba45c5 | bellard | static int64_t delta_max, delta_cum, last_clock, delta, ti;
|
1284 | 02ba45c5 | bellard | static int count;
|
1285 | 02ba45c5 | bellard | ti = qemu_get_clock(vm_clock);
|
1286 | 02ba45c5 | bellard | if (last_clock != 0) {
|
1287 | 02ba45c5 | bellard | delta = ti - last_clock;
|
1288 | 02ba45c5 | bellard | if (delta < delta_min)
|
1289 | 02ba45c5 | bellard | delta_min = delta;
|
1290 | 02ba45c5 | bellard | if (delta > delta_max)
|
1291 | 02ba45c5 | bellard | delta_max = delta;
|
1292 | 02ba45c5 | bellard | delta_cum += delta;
|
1293 | 02ba45c5 | bellard | if (++count == DISP_FREQ) {
|
1294 | 26a76461 | bellard | printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
|
1295 | 02ba45c5 | bellard | muldiv64(delta_min, 1000000, ticks_per_sec),
|
1296 | 02ba45c5 | bellard | muldiv64(delta_max, 1000000, ticks_per_sec),
|
1297 | 02ba45c5 | bellard | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
|
1298 | 02ba45c5 | bellard | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
|
1299 | 02ba45c5 | bellard | count = 0;
|
1300 | 02ba45c5 | bellard | delta_min = INT64_MAX;
|
1301 | 02ba45c5 | bellard | delta_max = 0;
|
1302 | 02ba45c5 | bellard | delta_cum = 0;
|
1303 | 02ba45c5 | bellard | }
|
1304 | 02ba45c5 | bellard | }
|
1305 | 02ba45c5 | bellard | last_clock = ti;
|
1306 | 02ba45c5 | bellard | }
|
1307 | 02ba45c5 | bellard | #endif
|
1308 | efe75411 | ths | if (alarm_has_dynticks(alarm_timer) ||
|
1309 | 2e70f6ef | pbrook | (!use_icount && |
1310 | 2e70f6ef | pbrook | qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
1311 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock))) || |
1312 | 8a7ddc38 | bellard | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
1313 | 8a7ddc38 | bellard | qemu_get_clock(rt_clock))) { |
1314 | 06d9f2f7 | bellard | #ifdef _WIN32
|
1315 | c8994013 | ths | struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv; |
1316 | c8994013 | ths | SetEvent(data->host_alarm); |
1317 | 06d9f2f7 | bellard | #endif
|
1318 | ee5605e5 | balrog | CPUState *env = next_cpu; |
1319 | ee5605e5 | balrog | |
1320 | d5d08334 | balrog | alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
1321 | d5d08334 | balrog | |
1322 | 4f8eb8da | balrog | if (env) {
|
1323 | 4f8eb8da | balrog | /* stop the currently executing cpu because a timer occured */
|
1324 | 4f8eb8da | balrog | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
1325 | a332e112 | bellard | #ifdef USE_KQEMU
|
1326 | 4f8eb8da | balrog | if (env->kqemu_enabled) {
|
1327 | 4f8eb8da | balrog | kqemu_cpu_interrupt(env); |
1328 | 4f8eb8da | balrog | } |
1329 | ee5605e5 | balrog | #endif
|
1330 | 4f8eb8da | balrog | } |
1331 | ee5605e5 | balrog | event_pending = 1;
|
1332 | 8a7ddc38 | bellard | } |
1333 | 8a7ddc38 | bellard | } |
1334 | 8a7ddc38 | bellard | |
1335 | 2e70f6ef | pbrook | static int64_t qemu_next_deadline(void) |
1336 | efe75411 | ths | { |
1337 | 2e70f6ef | pbrook | int64_t delta; |
1338 | efe75411 | ths | |
1339 | efe75411 | ths | if (active_timers[QEMU_TIMER_VIRTUAL]) {
|
1340 | 2e70f6ef | pbrook | delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time - |
1341 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock); |
1342 | 2e70f6ef | pbrook | } else {
|
1343 | 2e70f6ef | pbrook | /* To avoid problems with overflow limit this to 2^32. */
|
1344 | 2e70f6ef | pbrook | delta = INT32_MAX; |
1345 | efe75411 | ths | } |
1346 | efe75411 | ths | |
1347 | 2e70f6ef | pbrook | if (delta < 0) |
1348 | 2e70f6ef | pbrook | delta = 0;
|
1349 | efe75411 | ths | |
1350 | 2e70f6ef | pbrook | return delta;
|
1351 | 2e70f6ef | pbrook | } |
1352 | 2e70f6ef | pbrook | |
1353 | 2e70f6ef | pbrook | static uint64_t qemu_next_deadline_dyntick(void) |
1354 | 2e70f6ef | pbrook | { |
1355 | 2e70f6ef | pbrook | int64_t delta; |
1356 | 2e70f6ef | pbrook | int64_t rtdelta; |
1357 | 2e70f6ef | pbrook | |
1358 | 2e70f6ef | pbrook | if (use_icount)
|
1359 | 2e70f6ef | pbrook | delta = INT32_MAX; |
1360 | 2e70f6ef | pbrook | else
|
1361 | 2e70f6ef | pbrook | delta = (qemu_next_deadline() + 999) / 1000; |
1362 | 2e70f6ef | pbrook | |
1363 | 2e70f6ef | pbrook | if (active_timers[QEMU_TIMER_REALTIME]) {
|
1364 | 2e70f6ef | pbrook | rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time - |
1365 | 2e70f6ef | pbrook | qemu_get_clock(rt_clock))*1000;
|
1366 | 2e70f6ef | pbrook | if (rtdelta < delta)
|
1367 | 2e70f6ef | pbrook | delta = rtdelta; |
1368 | 2e70f6ef | pbrook | } |
1369 | 2e70f6ef | pbrook | |
1370 | 2e70f6ef | pbrook | if (delta < MIN_TIMER_REARM_US)
|
1371 | 2e70f6ef | pbrook | delta = MIN_TIMER_REARM_US; |
1372 | 2e70f6ef | pbrook | |
1373 | 2e70f6ef | pbrook | return delta;
|
1374 | efe75411 | ths | } |
1375 | efe75411 | ths | |
1376 | fd872598 | bellard | #ifndef _WIN32
|
1377 | fd872598 | bellard | |
1378 | 829309c7 | bellard | #if defined(__linux__)
|
1379 | 829309c7 | bellard | |
1380 | fd872598 | bellard | #define RTC_FREQ 1024 |
1381 | fd872598 | bellard | |
1382 | c8994013 | ths | static void enable_sigio_timer(int fd) |
1383 | c8994013 | ths | { |
1384 | c8994013 | ths | struct sigaction act;
|
1385 | c8994013 | ths | |
1386 | c8994013 | ths | /* timer signal */
|
1387 | c8994013 | ths | sigfillset(&act.sa_mask); |
1388 | c8994013 | ths | act.sa_flags = 0;
|
1389 | c8994013 | ths | act.sa_handler = host_alarm_handler; |
1390 | c8994013 | ths | |
1391 | c8994013 | ths | sigaction(SIGIO, &act, NULL);
|
1392 | c8994013 | ths | fcntl(fd, F_SETFL, O_ASYNC); |
1393 | c8994013 | ths | fcntl(fd, F_SETOWN, getpid()); |
1394 | c8994013 | ths | } |
1395 | 829309c7 | bellard | |
1396 | c40ec5a9 | ths | static int hpet_start_timer(struct qemu_alarm_timer *t) |
1397 | c40ec5a9 | ths | { |
1398 | c40ec5a9 | ths | struct hpet_info info;
|
1399 | c40ec5a9 | ths | int r, fd;
|
1400 | c40ec5a9 | ths | |
1401 | c40ec5a9 | ths | fd = open("/dev/hpet", O_RDONLY);
|
1402 | c40ec5a9 | ths | if (fd < 0) |
1403 | c40ec5a9 | ths | return -1; |
1404 | c40ec5a9 | ths | |
1405 | c40ec5a9 | ths | /* Set frequency */
|
1406 | c40ec5a9 | ths | r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
1407 | c40ec5a9 | ths | if (r < 0) { |
1408 | c40ec5a9 | ths | fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
|
1409 | c40ec5a9 | ths | "error, but for better emulation accuracy type:\n"
|
1410 | c40ec5a9 | ths | "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
|
1411 | c40ec5a9 | ths | goto fail;
|
1412 | c40ec5a9 | ths | } |
1413 | c40ec5a9 | ths | |
1414 | c40ec5a9 | ths | /* Check capabilities */
|
1415 | c40ec5a9 | ths | r = ioctl(fd, HPET_INFO, &info); |
1416 | c40ec5a9 | ths | if (r < 0) |
1417 | c40ec5a9 | ths | goto fail;
|
1418 | c40ec5a9 | ths | |
1419 | c40ec5a9 | ths | /* Enable periodic mode */
|
1420 | c40ec5a9 | ths | r = ioctl(fd, HPET_EPI, 0);
|
1421 | c40ec5a9 | ths | if (info.hi_flags && (r < 0)) |
1422 | c40ec5a9 | ths | goto fail;
|
1423 | c40ec5a9 | ths | |
1424 | c40ec5a9 | ths | /* Enable interrupt */
|
1425 | c40ec5a9 | ths | r = ioctl(fd, HPET_IE_ON, 0);
|
1426 | c40ec5a9 | ths | if (r < 0) |
1427 | c40ec5a9 | ths | goto fail;
|
1428 | c40ec5a9 | ths | |
1429 | c40ec5a9 | ths | enable_sigio_timer(fd); |
1430 | fcdc2129 | pbrook | t->priv = (void *)(long)fd; |
1431 | c40ec5a9 | ths | |
1432 | c40ec5a9 | ths | return 0; |
1433 | c40ec5a9 | ths | fail:
|
1434 | c40ec5a9 | ths | close(fd); |
1435 | c40ec5a9 | ths | return -1; |
1436 | c40ec5a9 | ths | } |
1437 | c40ec5a9 | ths | |
1438 | c40ec5a9 | ths | static void hpet_stop_timer(struct qemu_alarm_timer *t) |
1439 | c40ec5a9 | ths | { |
1440 | fcdc2129 | pbrook | int fd = (long)t->priv; |
1441 | c40ec5a9 | ths | |
1442 | c40ec5a9 | ths | close(fd); |
1443 | c40ec5a9 | ths | } |
1444 | c40ec5a9 | ths | |
1445 | c8994013 | ths | static int rtc_start_timer(struct qemu_alarm_timer *t) |
1446 | fd872598 | bellard | { |
1447 | c8994013 | ths | int rtc_fd;
|
1448 | b5a23ad4 | balrog | unsigned long current_rtc_freq = 0; |
1449 | c8994013 | ths | |
1450 | aeb30be6 | balrog | TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
|
1451 | fd872598 | bellard | if (rtc_fd < 0) |
1452 | fd872598 | bellard | return -1; |
1453 | b5a23ad4 | balrog | ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
1454 | b5a23ad4 | balrog | if (current_rtc_freq != RTC_FREQ &&
|
1455 | b5a23ad4 | balrog | ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
|
1456 | fd872598 | bellard | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
|
1457 | fd872598 | bellard | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
|
1458 | fd872598 | bellard | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
|
1459 | fd872598 | bellard | goto fail;
|
1460 | fd872598 | bellard | } |
1461 | fd872598 | bellard | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
1462 | fd872598 | bellard | fail:
|
1463 | fd872598 | bellard | close(rtc_fd); |
1464 | fd872598 | bellard | return -1; |
1465 | fd872598 | bellard | } |
1466 | c8994013 | ths | |
1467 | c8994013 | ths | enable_sigio_timer(rtc_fd); |
1468 | c8994013 | ths | |
1469 | fcdc2129 | pbrook | t->priv = (void *)(long)rtc_fd; |
1470 | c8994013 | ths | |
1471 | fd872598 | bellard | return 0; |
1472 | fd872598 | bellard | } |
1473 | fd872598 | bellard | |
1474 | c8994013 | ths | static void rtc_stop_timer(struct qemu_alarm_timer *t) |
1475 | 829309c7 | bellard | { |
1476 | fcdc2129 | pbrook | int rtc_fd = (long)t->priv; |
1477 | c8994013 | ths | |
1478 | c8994013 | ths | close(rtc_fd); |
1479 | 829309c7 | bellard | } |
1480 | 829309c7 | bellard | |
1481 | efe75411 | ths | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
1482 | efe75411 | ths | { |
1483 | efe75411 | ths | struct sigevent ev;
|
1484 | efe75411 | ths | timer_t host_timer; |
1485 | efe75411 | ths | struct sigaction act;
|
1486 | efe75411 | ths | |
1487 | efe75411 | ths | sigfillset(&act.sa_mask); |
1488 | efe75411 | ths | act.sa_flags = 0;
|
1489 | efe75411 | ths | act.sa_handler = host_alarm_handler; |
1490 | efe75411 | ths | |
1491 | efe75411 | ths | sigaction(SIGALRM, &act, NULL);
|
1492 | efe75411 | ths | |
1493 | efe75411 | ths | ev.sigev_value.sival_int = 0;
|
1494 | efe75411 | ths | ev.sigev_notify = SIGEV_SIGNAL; |
1495 | efe75411 | ths | ev.sigev_signo = SIGALRM; |
1496 | efe75411 | ths | |
1497 | efe75411 | ths | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
|
1498 | efe75411 | ths | perror("timer_create");
|
1499 | efe75411 | ths | |
1500 | efe75411 | ths | /* disable dynticks */
|
1501 | efe75411 | ths | fprintf(stderr, "Dynamic Ticks disabled\n");
|
1502 | efe75411 | ths | |
1503 | efe75411 | ths | return -1; |
1504 | efe75411 | ths | } |
1505 | efe75411 | ths | |
1506 | efe75411 | ths | t->priv = (void *)host_timer;
|
1507 | efe75411 | ths | |
1508 | efe75411 | ths | return 0; |
1509 | efe75411 | ths | } |
1510 | efe75411 | ths | |
1511 | efe75411 | ths | static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
1512 | efe75411 | ths | { |
1513 | efe75411 | ths | timer_t host_timer = (timer_t)t->priv; |
1514 | efe75411 | ths | |
1515 | efe75411 | ths | timer_delete(host_timer); |
1516 | efe75411 | ths | } |
1517 | efe75411 | ths | |
1518 | efe75411 | ths | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
1519 | efe75411 | ths | { |
1520 | efe75411 | ths | timer_t host_timer = (timer_t)t->priv; |
1521 | efe75411 | ths | struct itimerspec timeout;
|
1522 | efe75411 | ths | int64_t nearest_delta_us = INT64_MAX; |
1523 | efe75411 | ths | int64_t current_us; |
1524 | efe75411 | ths | |
1525 | efe75411 | ths | if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1526 | efe75411 | ths | !active_timers[QEMU_TIMER_VIRTUAL]) |
1527 | d5d08334 | balrog | return;
|
1528 | efe75411 | ths | |
1529 | 2e70f6ef | pbrook | nearest_delta_us = qemu_next_deadline_dyntick(); |
1530 | efe75411 | ths | |
1531 | efe75411 | ths | /* check whether a timer is already running */
|
1532 | efe75411 | ths | if (timer_gettime(host_timer, &timeout)) {
|
1533 | efe75411 | ths | perror("gettime");
|
1534 | efe75411 | ths | fprintf(stderr, "Internal timer error: aborting\n");
|
1535 | efe75411 | ths | exit(1);
|
1536 | efe75411 | ths | } |
1537 | efe75411 | ths | current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
1538 | efe75411 | ths | if (current_us && current_us <= nearest_delta_us)
|
1539 | efe75411 | ths | return;
|
1540 | efe75411 | ths | |
1541 | efe75411 | ths | timeout.it_interval.tv_sec = 0;
|
1542 | efe75411 | ths | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
1543 | efe75411 | ths | timeout.it_value.tv_sec = nearest_delta_us / 1000000;
|
1544 | efe75411 | ths | timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
1545 | efe75411 | ths | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
1546 | efe75411 | ths | perror("settime");
|
1547 | efe75411 | ths | fprintf(stderr, "Internal timer error: aborting\n");
|
1548 | efe75411 | ths | exit(1);
|
1549 | efe75411 | ths | } |
1550 | efe75411 | ths | } |
1551 | efe75411 | ths | |
1552 | 70744b3a | ths | #endif /* defined(__linux__) */ |
1553 | 231c6586 | ths | |
1554 | c8994013 | ths | static int unix_start_timer(struct qemu_alarm_timer *t) |
1555 | c8994013 | ths | { |
1556 | c8994013 | ths | struct sigaction act;
|
1557 | c8994013 | ths | struct itimerval itv;
|
1558 | c8994013 | ths | int err;
|
1559 | c8994013 | ths | |
1560 | c8994013 | ths | /* timer signal */
|
1561 | c8994013 | ths | sigfillset(&act.sa_mask); |
1562 | c8994013 | ths | act.sa_flags = 0;
|
1563 | c8994013 | ths | act.sa_handler = host_alarm_handler; |
1564 | c8994013 | ths | |
1565 | c8994013 | ths | sigaction(SIGALRM, &act, NULL);
|
1566 | c8994013 | ths | |
1567 | c8994013 | ths | itv.it_interval.tv_sec = 0;
|
1568 | c8994013 | ths | /* for i386 kernel 2.6 to get 1 ms */
|
1569 | c8994013 | ths | itv.it_interval.tv_usec = 999;
|
1570 | c8994013 | ths | itv.it_value.tv_sec = 0;
|
1571 | c8994013 | ths | itv.it_value.tv_usec = 10 * 1000; |
1572 | c8994013 | ths | |
1573 | c8994013 | ths | err = setitimer(ITIMER_REAL, &itv, NULL);
|
1574 | c8994013 | ths | if (err)
|
1575 | c8994013 | ths | return -1; |
1576 | c8994013 | ths | |
1577 | c8994013 | ths | return 0; |
1578 | c8994013 | ths | } |
1579 | c8994013 | ths | |
1580 | c8994013 | ths | static void unix_stop_timer(struct qemu_alarm_timer *t) |
1581 | c8994013 | ths | { |
1582 | c8994013 | ths | struct itimerval itv;
|
1583 | c8994013 | ths | |
1584 | c8994013 | ths | memset(&itv, 0, sizeof(itv)); |
1585 | c8994013 | ths | setitimer(ITIMER_REAL, &itv, NULL);
|
1586 | c8994013 | ths | } |
1587 | c8994013 | ths | |
1588 | 829309c7 | bellard | #endif /* !defined(_WIN32) */ |
1589 | fd872598 | bellard | |
1590 | c8994013 | ths | #ifdef _WIN32
|
1591 | c8994013 | ths | |
1592 | c8994013 | ths | static int win32_start_timer(struct qemu_alarm_timer *t) |
1593 | c8994013 | ths | { |
1594 | c8994013 | ths | TIMECAPS tc; |
1595 | c8994013 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1596 | efe75411 | ths | UINT flags; |
1597 | c8994013 | ths | |
1598 | c8994013 | ths | data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL); |
1599 | c8994013 | ths | if (!data->host_alarm) {
|
1600 | c8994013 | ths | perror("Failed CreateEvent");
|
1601 | c396a7f0 | ths | return -1; |
1602 | c8994013 | ths | } |
1603 | c8994013 | ths | |
1604 | c8994013 | ths | memset(&tc, 0, sizeof(tc)); |
1605 | c8994013 | ths | timeGetDevCaps(&tc, sizeof(tc));
|
1606 | c8994013 | ths | |
1607 | c8994013 | ths | if (data->period < tc.wPeriodMin)
|
1608 | c8994013 | ths | data->period = tc.wPeriodMin; |
1609 | c8994013 | ths | |
1610 | c8994013 | ths | timeBeginPeriod(data->period); |
1611 | c8994013 | ths | |
1612 | efe75411 | ths | flags = TIME_CALLBACK_FUNCTION; |
1613 | efe75411 | ths | if (alarm_has_dynticks(t))
|
1614 | efe75411 | ths | flags |= TIME_ONESHOT; |
1615 | efe75411 | ths | else
|
1616 | efe75411 | ths | flags |= TIME_PERIODIC; |
1617 | efe75411 | ths | |
1618 | c8994013 | ths | data->timerId = timeSetEvent(1, // interval (ms) |
1619 | c8994013 | ths | data->period, // resolution
|
1620 | c8994013 | ths | host_alarm_handler, // function
|
1621 | c8994013 | ths | (DWORD)t, // parameter
|
1622 | efe75411 | ths | flags); |
1623 | c8994013 | ths | |
1624 | c8994013 | ths | if (!data->timerId) {
|
1625 | c8994013 | ths | perror("Failed to initialize win32 alarm timer");
|
1626 | c8994013 | ths | |
1627 | c8994013 | ths | timeEndPeriod(data->period); |
1628 | c8994013 | ths | CloseHandle(data->host_alarm); |
1629 | c8994013 | ths | return -1; |
1630 | c8994013 | ths | } |
1631 | c8994013 | ths | |
1632 | c8994013 | ths | qemu_add_wait_object(data->host_alarm, NULL, NULL); |
1633 | c8994013 | ths | |
1634 | c8994013 | ths | return 0; |
1635 | c8994013 | ths | } |
1636 | c8994013 | ths | |
1637 | c8994013 | ths | static void win32_stop_timer(struct qemu_alarm_timer *t) |
1638 | c8994013 | ths | { |
1639 | c8994013 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1640 | c8994013 | ths | |
1641 | c8994013 | ths | timeKillEvent(data->timerId); |
1642 | c8994013 | ths | timeEndPeriod(data->period); |
1643 | c8994013 | ths | |
1644 | c8994013 | ths | CloseHandle(data->host_alarm); |
1645 | c8994013 | ths | } |
1646 | c8994013 | ths | |
1647 | efe75411 | ths | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
1648 | efe75411 | ths | { |
1649 | efe75411 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1650 | efe75411 | ths | uint64_t nearest_delta_us; |
1651 | efe75411 | ths | |
1652 | efe75411 | ths | if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1653 | efe75411 | ths | !active_timers[QEMU_TIMER_VIRTUAL]) |
1654 | d5d08334 | balrog | return;
|
1655 | efe75411 | ths | |
1656 | 2e70f6ef | pbrook | nearest_delta_us = qemu_next_deadline_dyntick(); |
1657 | efe75411 | ths | nearest_delta_us /= 1000;
|
1658 | efe75411 | ths | |
1659 | efe75411 | ths | timeKillEvent(data->timerId); |
1660 | efe75411 | ths | |
1661 | efe75411 | ths | data->timerId = timeSetEvent(1,
|
1662 | efe75411 | ths | data->period, |
1663 | efe75411 | ths | host_alarm_handler, |
1664 | efe75411 | ths | (DWORD)t, |
1665 | efe75411 | ths | TIME_ONESHOT | TIME_PERIODIC); |
1666 | efe75411 | ths | |
1667 | efe75411 | ths | if (!data->timerId) {
|
1668 | efe75411 | ths | perror("Failed to re-arm win32 alarm timer");
|
1669 | efe75411 | ths | |
1670 | efe75411 | ths | timeEndPeriod(data->period); |
1671 | efe75411 | ths | CloseHandle(data->host_alarm); |
1672 | efe75411 | ths | exit(1);
|
1673 | efe75411 | ths | } |
1674 | efe75411 | ths | } |
1675 | efe75411 | ths | |
1676 | c8994013 | ths | #endif /* _WIN32 */ |
1677 | c8994013 | ths | |
1678 | 1dce7c3c | bellard | static void init_timer_alarm(void) |
1679 | 8a7ddc38 | bellard | { |
1680 | c8994013 | ths | struct qemu_alarm_timer *t;
|
1681 | c8994013 | ths | int i, err = -1; |
1682 | c8994013 | ths | |
1683 | c8994013 | ths | for (i = 0; alarm_timers[i].name; i++) { |
1684 | c8994013 | ths | t = &alarm_timers[i]; |
1685 | c8994013 | ths | |
1686 | c8994013 | ths | err = t->start(t); |
1687 | c8994013 | ths | if (!err)
|
1688 | c8994013 | ths | break;
|
1689 | 67b915a5 | bellard | } |
1690 | fd872598 | bellard | |
1691 | c8994013 | ths | if (err) {
|
1692 | c8994013 | ths | fprintf(stderr, "Unable to find any suitable alarm timer.\n");
|
1693 | c8994013 | ths | fprintf(stderr, "Terminating\n");
|
1694 | c8994013 | ths | exit(1);
|
1695 | 67b915a5 | bellard | } |
1696 | c8994013 | ths | |
1697 | c8994013 | ths | alarm_timer = t; |
1698 | 8a7ddc38 | bellard | } |
1699 | 8a7ddc38 | bellard | |
1700 | 9596ebb7 | pbrook | static void quit_timers(void) |
1701 | 40c3bac3 | bellard | { |
1702 | c8994013 | ths | alarm_timer->stop(alarm_timer); |
1703 | c8994013 | ths | alarm_timer = NULL;
|
1704 | 40c3bac3 | bellard | } |
1705 | 40c3bac3 | bellard | |
1706 | c4b1fcc0 | bellard | /***********************************************************/
|
1707 | f6503059 | balrog | /* host time/date access */
|
1708 | f6503059 | balrog | void qemu_get_timedate(struct tm *tm, int offset) |
1709 | f6503059 | balrog | { |
1710 | f6503059 | balrog | time_t ti; |
1711 | f6503059 | balrog | struct tm *ret;
|
1712 | f6503059 | balrog | |
1713 | f6503059 | balrog | time(&ti); |
1714 | f6503059 | balrog | ti += offset; |
1715 | f6503059 | balrog | if (rtc_date_offset == -1) { |
1716 | f6503059 | balrog | if (rtc_utc)
|
1717 | f6503059 | balrog | ret = gmtime(&ti); |
1718 | f6503059 | balrog | else
|
1719 | f6503059 | balrog | ret = localtime(&ti); |
1720 | f6503059 | balrog | } else {
|
1721 | f6503059 | balrog | ti -= rtc_date_offset; |
1722 | f6503059 | balrog | ret = gmtime(&ti); |
1723 | f6503059 | balrog | } |
1724 | f6503059 | balrog | |
1725 | f6503059 | balrog | memcpy(tm, ret, sizeof(struct tm)); |
1726 | f6503059 | balrog | } |
1727 | f6503059 | balrog | |
1728 | f6503059 | balrog | int qemu_timedate_diff(struct tm *tm) |
1729 | f6503059 | balrog | { |
1730 | f6503059 | balrog | time_t seconds; |
1731 | f6503059 | balrog | |
1732 | f6503059 | balrog | if (rtc_date_offset == -1) |
1733 | f6503059 | balrog | if (rtc_utc)
|
1734 | f6503059 | balrog | seconds = mktimegm(tm); |
1735 | f6503059 | balrog | else
|
1736 | f6503059 | balrog | seconds = mktime(tm); |
1737 | f6503059 | balrog | else
|
1738 | f6503059 | balrog | seconds = mktimegm(tm) + rtc_date_offset; |
1739 | f6503059 | balrog | |
1740 | f6503059 | balrog | return seconds - time(NULL); |
1741 | f6503059 | balrog | } |
1742 | f6503059 | balrog | |
1743 | f6503059 | balrog | /***********************************************************/
|
1744 | 82c643ff | bellard | /* character device */
|
1745 | 313aa567 | bellard | |
1746 | e5b0bc44 | pbrook | static void qemu_chr_event(CharDriverState *s, int event) |
1747 | e5b0bc44 | pbrook | { |
1748 | e5b0bc44 | pbrook | if (!s->chr_event)
|
1749 | e5b0bc44 | pbrook | return;
|
1750 | e5b0bc44 | pbrook | s->chr_event(s->handler_opaque, event); |
1751 | e5b0bc44 | pbrook | } |
1752 | e5b0bc44 | pbrook | |
1753 | 86e94dea | ths | static void qemu_chr_reset_bh(void *opaque) |
1754 | 86e94dea | ths | { |
1755 | 86e94dea | ths | CharDriverState *s = opaque; |
1756 | e5b0bc44 | pbrook | qemu_chr_event(s, CHR_EVENT_RESET); |
1757 | 86e94dea | ths | qemu_bh_delete(s->bh); |
1758 | 86e94dea | ths | s->bh = NULL;
|
1759 | 86e94dea | ths | } |
1760 | 86e94dea | ths | |
1761 | 86e94dea | ths | void qemu_chr_reset(CharDriverState *s)
|
1762 | 86e94dea | ths | { |
1763 | 86e94dea | ths | if (s->bh == NULL) { |
1764 | 86e94dea | ths | s->bh = qemu_bh_new(qemu_chr_reset_bh, s); |
1765 | 86e94dea | ths | qemu_bh_schedule(s->bh); |
1766 | 86e94dea | ths | } |
1767 | 86e94dea | ths | } |
1768 | 86e94dea | ths | |
1769 | 82c643ff | bellard | int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
1770 | 82c643ff | bellard | { |
1771 | 82c643ff | bellard | return s->chr_write(s, buf, len);
|
1772 | 82c643ff | bellard | } |
1773 | 67b915a5 | bellard | |
1774 | e57a8c0e | bellard | int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg) |
1775 | f8d179e3 | bellard | { |
1776 | e57a8c0e | bellard | if (!s->chr_ioctl)
|
1777 | e57a8c0e | bellard | return -ENOTSUP;
|
1778 | e57a8c0e | bellard | return s->chr_ioctl(s, cmd, arg);
|
1779 | f8d179e3 | bellard | } |
1780 | f8d179e3 | bellard | |
1781 | e5b0bc44 | pbrook | int qemu_chr_can_read(CharDriverState *s)
|
1782 | e5b0bc44 | pbrook | { |
1783 | e5b0bc44 | pbrook | if (!s->chr_can_read)
|
1784 | e5b0bc44 | pbrook | return 0; |
1785 | e5b0bc44 | pbrook | return s->chr_can_read(s->handler_opaque);
|
1786 | e5b0bc44 | pbrook | } |
1787 | e5b0bc44 | pbrook | |
1788 | e5b0bc44 | pbrook | void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len) |
1789 | e5b0bc44 | pbrook | { |
1790 | e5b0bc44 | pbrook | s->chr_read(s->handler_opaque, buf, len); |
1791 | e5b0bc44 | pbrook | } |
1792 | e5b0bc44 | pbrook | |
1793 | bd9bdce6 | balrog | void qemu_chr_accept_input(CharDriverState *s)
|
1794 | bd9bdce6 | balrog | { |
1795 | bd9bdce6 | balrog | if (s->chr_accept_input)
|
1796 | bd9bdce6 | balrog | s->chr_accept_input(s); |
1797 | bd9bdce6 | balrog | } |
1798 | e5b0bc44 | pbrook | |
1799 | 82c643ff | bellard | void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
1800 | 67b915a5 | bellard | { |
1801 | 82c643ff | bellard | char buf[4096]; |
1802 | 82c643ff | bellard | va_list ap; |
1803 | 82c643ff | bellard | va_start(ap, fmt); |
1804 | 82c643ff | bellard | vsnprintf(buf, sizeof(buf), fmt, ap);
|
1805 | ffe8ab83 | ths | qemu_chr_write(s, (uint8_t *)buf, strlen(buf)); |
1806 | 82c643ff | bellard | va_end(ap); |
1807 | 67b915a5 | bellard | } |
1808 | 67b915a5 | bellard | |
1809 | 5905b2e5 | bellard | void qemu_chr_send_event(CharDriverState *s, int event) |
1810 | 5905b2e5 | bellard | { |
1811 | 5905b2e5 | bellard | if (s->chr_send_event)
|
1812 | 5905b2e5 | bellard | s->chr_send_event(s, event); |
1813 | 5905b2e5 | bellard | } |
1814 | 5905b2e5 | bellard | |
1815 | 5fafdf24 | ths | void qemu_chr_add_handlers(CharDriverState *s,
|
1816 | 5fafdf24 | ths | IOCanRWHandler *fd_can_read, |
1817 | e5b0bc44 | pbrook | IOReadHandler *fd_read, |
1818 | e5b0bc44 | pbrook | IOEventHandler *fd_event, |
1819 | e5b0bc44 | pbrook | void *opaque)
|
1820 | 82c643ff | bellard | { |
1821 | e5b0bc44 | pbrook | s->chr_can_read = fd_can_read; |
1822 | e5b0bc44 | pbrook | s->chr_read = fd_read; |
1823 | e5b0bc44 | pbrook | s->chr_event = fd_event; |
1824 | e5b0bc44 | pbrook | s->handler_opaque = opaque; |
1825 | e5b0bc44 | pbrook | if (s->chr_update_read_handler)
|
1826 | e5b0bc44 | pbrook | s->chr_update_read_handler(s); |
1827 | 82c643ff | bellard | } |
1828 | 3b46e624 | ths | |
1829 | 82c643ff | bellard | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1830 | 313aa567 | bellard | { |
1831 | 82c643ff | bellard | return len;
|
1832 | 82c643ff | bellard | } |
1833 | 82c643ff | bellard | |
1834 | 52f61fde | ths | static CharDriverState *qemu_chr_open_null(void) |
1835 | 82c643ff | bellard | { |
1836 | 82c643ff | bellard | CharDriverState *chr; |
1837 | 82c643ff | bellard | |
1838 | 82c643ff | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
1839 | 82c643ff | bellard | if (!chr)
|
1840 | 82c643ff | bellard | return NULL; |
1841 | 82c643ff | bellard | chr->chr_write = null_chr_write; |
1842 | 82c643ff | bellard | return chr;
|
1843 | 82c643ff | bellard | } |
1844 | 82c643ff | bellard | |
1845 | 20d8a3ed | ths | /* MUX driver for serial I/O splitting */
|
1846 | 20d8a3ed | ths | static int term_timestamps; |
1847 | 20d8a3ed | ths | static int64_t term_timestamps_start;
|
1848 | 9c1de612 | ths | #define MAX_MUX 4 |
1849 | bd9bdce6 | balrog | #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ |
1850 | bd9bdce6 | balrog | #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) |
1851 | 20d8a3ed | ths | typedef struct { |
1852 | 20d8a3ed | ths | IOCanRWHandler *chr_can_read[MAX_MUX]; |
1853 | 20d8a3ed | ths | IOReadHandler *chr_read[MAX_MUX]; |
1854 | 20d8a3ed | ths | IOEventHandler *chr_event[MAX_MUX]; |
1855 | 20d8a3ed | ths | void *ext_opaque[MAX_MUX];
|
1856 | 20d8a3ed | ths | CharDriverState *drv; |
1857 | bd9bdce6 | balrog | unsigned char buffer[MUX_BUFFER_SIZE]; |
1858 | bd9bdce6 | balrog | int prod;
|
1859 | bd9bdce6 | balrog | int cons;
|
1860 | 20d8a3ed | ths | int mux_cnt;
|
1861 | 20d8a3ed | ths | int term_got_escape;
|
1862 | 20d8a3ed | ths | int max_size;
|
1863 | 20d8a3ed | ths | } MuxDriver; |
1864 | 20d8a3ed | ths | |
1865 | 20d8a3ed | ths | |
1866 | 20d8a3ed | ths | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1867 | 20d8a3ed | ths | { |
1868 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
1869 | 20d8a3ed | ths | int ret;
|
1870 | 20d8a3ed | ths | if (!term_timestamps) {
|
1871 | 20d8a3ed | ths | ret = d->drv->chr_write(d->drv, buf, len); |
1872 | 20d8a3ed | ths | } else {
|
1873 | 20d8a3ed | ths | int i;
|
1874 | 20d8a3ed | ths | |
1875 | 20d8a3ed | ths | ret = 0;
|
1876 | 20d8a3ed | ths | for(i = 0; i < len; i++) { |
1877 | 20d8a3ed | ths | ret += d->drv->chr_write(d->drv, buf+i, 1);
|
1878 | 20d8a3ed | ths | if (buf[i] == '\n') { |
1879 | 20d8a3ed | ths | char buf1[64]; |
1880 | 20d8a3ed | ths | int64_t ti; |
1881 | 20d8a3ed | ths | int secs;
|
1882 | 20d8a3ed | ths | |
1883 | 20d8a3ed | ths | ti = get_clock(); |
1884 | 20d8a3ed | ths | if (term_timestamps_start == -1) |
1885 | 20d8a3ed | ths | term_timestamps_start = ti; |
1886 | 20d8a3ed | ths | ti -= term_timestamps_start; |
1887 | 20d8a3ed | ths | secs = ti / 1000000000;
|
1888 | 20d8a3ed | ths | snprintf(buf1, sizeof(buf1),
|
1889 | 20d8a3ed | ths | "[%02d:%02d:%02d.%03d] ",
|
1890 | 20d8a3ed | ths | secs / 3600,
|
1891 | 20d8a3ed | ths | (secs / 60) % 60, |
1892 | 20d8a3ed | ths | secs % 60,
|
1893 | 20d8a3ed | ths | (int)((ti / 1000000) % 1000)); |
1894 | ffe8ab83 | ths | d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
1895 | 20d8a3ed | ths | } |
1896 | 20d8a3ed | ths | } |
1897 | 20d8a3ed | ths | } |
1898 | 20d8a3ed | ths | return ret;
|
1899 | 20d8a3ed | ths | } |
1900 | 20d8a3ed | ths | |
1901 | 20d8a3ed | ths | static char *mux_help[] = { |
1902 | 20d8a3ed | ths | "% h print this help\n\r",
|
1903 | 20d8a3ed | ths | "% x exit emulator\n\r",
|
1904 | 20d8a3ed | ths | "% s save disk data back to file (if -snapshot)\n\r",
|
1905 | 20d8a3ed | ths | "% t toggle console timestamps\n\r"
|
1906 | 20d8a3ed | ths | "% b send break (magic sysrq)\n\r",
|
1907 | 20d8a3ed | ths | "% c switch between console and monitor\n\r",
|
1908 | 20d8a3ed | ths | "% % sends %\n\r",
|
1909 | 20d8a3ed | ths | NULL
|
1910 | 20d8a3ed | ths | }; |
1911 | 20d8a3ed | ths | |
1912 | 20d8a3ed | ths | static int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
1913 | 20d8a3ed | ths | static void mux_print_help(CharDriverState *chr) |
1914 | 20d8a3ed | ths | { |
1915 | 20d8a3ed | ths | int i, j;
|
1916 | 20d8a3ed | ths | char ebuf[15] = "Escape-Char"; |
1917 | 20d8a3ed | ths | char cbuf[50] = "\n\r"; |
1918 | 20d8a3ed | ths | |
1919 | 20d8a3ed | ths | if (term_escape_char > 0 && term_escape_char < 26) { |
1920 | 363a37d5 | blueswir1 | snprintf(cbuf, sizeof(cbuf), "\n\r"); |
1921 | 363a37d5 | blueswir1 | snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a'); |
1922 | 20d8a3ed | ths | } else {
|
1923 | 363a37d5 | blueswir1 | snprintf(cbuf, sizeof(cbuf),
|
1924 | 363a37d5 | blueswir1 | "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
|
1925 | 363a37d5 | blueswir1 | term_escape_char); |
1926 | 20d8a3ed | ths | } |
1927 | ffe8ab83 | ths | chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf)); |
1928 | 20d8a3ed | ths | for (i = 0; mux_help[i] != NULL; i++) { |
1929 | 20d8a3ed | ths | for (j=0; mux_help[i][j] != '\0'; j++) { |
1930 | 20d8a3ed | ths | if (mux_help[i][j] == '%') |
1931 | ffe8ab83 | ths | chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf)); |
1932 | 20d8a3ed | ths | else
|
1933 | ffe8ab83 | ths | chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
|
1934 | 20d8a3ed | ths | } |
1935 | 20d8a3ed | ths | } |
1936 | 20d8a3ed | ths | } |
1937 | 20d8a3ed | ths | |
1938 | 20d8a3ed | ths | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
1939 | 20d8a3ed | ths | { |
1940 | 20d8a3ed | ths | if (d->term_got_escape) {
|
1941 | 20d8a3ed | ths | d->term_got_escape = 0;
|
1942 | 20d8a3ed | ths | if (ch == term_escape_char)
|
1943 | 20d8a3ed | ths | goto send_char;
|
1944 | 20d8a3ed | ths | switch(ch) {
|
1945 | 20d8a3ed | ths | case '?': |
1946 | 20d8a3ed | ths | case 'h': |
1947 | 20d8a3ed | ths | mux_print_help(chr); |
1948 | 20d8a3ed | ths | break;
|
1949 | 20d8a3ed | ths | case 'x': |
1950 | 20d8a3ed | ths | { |
1951 | 20d8a3ed | ths | char *term = "QEMU: Terminated\n\r"; |
1952 | ffe8ab83 | ths | chr->chr_write(chr,(uint8_t *)term,strlen(term)); |
1953 | 20d8a3ed | ths | exit(0);
|
1954 | 20d8a3ed | ths | break;
|
1955 | 20d8a3ed | ths | } |
1956 | 20d8a3ed | ths | case 's': |
1957 | 20d8a3ed | ths | { |
1958 | 20d8a3ed | ths | int i;
|
1959 | e4bcb14c | ths | for (i = 0; i < nb_drives; i++) { |
1960 | e4bcb14c | ths | bdrv_commit(drives_table[i].bdrv); |
1961 | 20d8a3ed | ths | } |
1962 | 20d8a3ed | ths | } |
1963 | 20d8a3ed | ths | break;
|
1964 | 20d8a3ed | ths | case 'b': |
1965 | 36ddb83b | balrog | qemu_chr_event(chr, CHR_EVENT_BREAK); |
1966 | 20d8a3ed | ths | break;
|
1967 | 20d8a3ed | ths | case 'c': |
1968 | 20d8a3ed | ths | /* Switch to the next registered device */
|
1969 | 20d8a3ed | ths | chr->focus++; |
1970 | 20d8a3ed | ths | if (chr->focus >= d->mux_cnt)
|
1971 | 20d8a3ed | ths | chr->focus = 0;
|
1972 | 20d8a3ed | ths | break;
|
1973 | 20d8a3ed | ths | case 't': |
1974 | 20d8a3ed | ths | term_timestamps = !term_timestamps; |
1975 | 20d8a3ed | ths | term_timestamps_start = -1;
|
1976 | 20d8a3ed | ths | break;
|
1977 | 20d8a3ed | ths | } |
1978 | 20d8a3ed | ths | } else if (ch == term_escape_char) { |
1979 | 20d8a3ed | ths | d->term_got_escape = 1;
|
1980 | 20d8a3ed | ths | } else {
|
1981 | 20d8a3ed | ths | send_char:
|
1982 | 20d8a3ed | ths | return 1; |
1983 | 20d8a3ed | ths | } |
1984 | 20d8a3ed | ths | return 0; |
1985 | 20d8a3ed | ths | } |
1986 | 20d8a3ed | ths | |
1987 | bd9bdce6 | balrog | static void mux_chr_accept_input(CharDriverState *chr) |
1988 | bd9bdce6 | balrog | { |
1989 | bd9bdce6 | balrog | int m = chr->focus;
|
1990 | bd9bdce6 | balrog | MuxDriver *d = chr->opaque; |
1991 | bd9bdce6 | balrog | |
1992 | bd9bdce6 | balrog | while (d->prod != d->cons &&
|
1993 | bd9bdce6 | balrog | d->chr_can_read[m] && |
1994 | bd9bdce6 | balrog | d->chr_can_read[m](d->ext_opaque[m])) { |
1995 | bd9bdce6 | balrog | d->chr_read[m](d->ext_opaque[m], |
1996 | bd9bdce6 | balrog | &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
|
1997 | bd9bdce6 | balrog | } |
1998 | bd9bdce6 | balrog | } |
1999 | bd9bdce6 | balrog | |
2000 | 20d8a3ed | ths | static int mux_chr_can_read(void *opaque) |
2001 | 20d8a3ed | ths | { |
2002 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2003 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
2004 | bd9bdce6 | balrog | |
2005 | bd9bdce6 | balrog | if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
|
2006 | bd9bdce6 | balrog | return 1; |
2007 | 20d8a3ed | ths | if (d->chr_can_read[chr->focus])
|
2008 | bd9bdce6 | balrog | return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
|
2009 | 20d8a3ed | ths | return 0; |
2010 | 20d8a3ed | ths | } |
2011 | 20d8a3ed | ths | |
2012 | 20d8a3ed | ths | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
2013 | 20d8a3ed | ths | { |
2014 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2015 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
2016 | bd9bdce6 | balrog | int m = chr->focus;
|
2017 | 20d8a3ed | ths | int i;
|
2018 | bd9bdce6 | balrog | |
2019 | bd9bdce6 | balrog | mux_chr_accept_input (opaque); |
2020 | bd9bdce6 | balrog | |
2021 | 20d8a3ed | ths | for(i = 0; i < size; i++) |
2022 | bd9bdce6 | balrog | if (mux_proc_byte(chr, d, buf[i])) {
|
2023 | bd9bdce6 | balrog | if (d->prod == d->cons &&
|
2024 | bd9bdce6 | balrog | d->chr_can_read[m] && |
2025 | bd9bdce6 | balrog | d->chr_can_read[m](d->ext_opaque[m])) |
2026 | bd9bdce6 | balrog | d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
|
2027 | bd9bdce6 | balrog | else
|
2028 | bd9bdce6 | balrog | d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i]; |
2029 | bd9bdce6 | balrog | } |
2030 | 20d8a3ed | ths | } |
2031 | 20d8a3ed | ths | |
2032 | 20d8a3ed | ths | static void mux_chr_event(void *opaque, int event) |
2033 | 20d8a3ed | ths | { |
2034 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2035 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
2036 | 20d8a3ed | ths | int i;
|
2037 | 20d8a3ed | ths | |
2038 | 20d8a3ed | ths | /* Send the event to all registered listeners */
|
2039 | 20d8a3ed | ths | for (i = 0; i < d->mux_cnt; i++) |
2040 | 20d8a3ed | ths | if (d->chr_event[i])
|
2041 | 20d8a3ed | ths | d->chr_event[i](d->ext_opaque[i], event); |
2042 | 20d8a3ed | ths | } |
2043 | 20d8a3ed | ths | |
2044 | 20d8a3ed | ths | static void mux_chr_update_read_handler(CharDriverState *chr) |
2045 | 20d8a3ed | ths | { |
2046 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
2047 | 20d8a3ed | ths | |
2048 | 20d8a3ed | ths | if (d->mux_cnt >= MAX_MUX) {
|
2049 | 20d8a3ed | ths | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
|
2050 | 20d8a3ed | ths | return;
|
2051 | 20d8a3ed | ths | } |
2052 | 20d8a3ed | ths | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
2053 | 20d8a3ed | ths | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
2054 | 20d8a3ed | ths | d->chr_read[d->mux_cnt] = chr->chr_read; |
2055 | 20d8a3ed | ths | d->chr_event[d->mux_cnt] = chr->chr_event; |
2056 | 20d8a3ed | ths | /* Fix up the real driver with mux routines */
|
2057 | 20d8a3ed | ths | if (d->mux_cnt == 0) { |
2058 | 20d8a3ed | ths | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
2059 | 20d8a3ed | ths | mux_chr_event, chr); |
2060 | 20d8a3ed | ths | } |
2061 | 20d8a3ed | ths | chr->focus = d->mux_cnt; |
2062 | 20d8a3ed | ths | d->mux_cnt++; |
2063 | 20d8a3ed | ths | } |
2064 | 20d8a3ed | ths | |
2065 | 9596ebb7 | pbrook | static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
|
2066 | 20d8a3ed | ths | { |
2067 | 20d8a3ed | ths | CharDriverState *chr; |
2068 | 20d8a3ed | ths | MuxDriver *d; |
2069 | 20d8a3ed | ths | |
2070 | 20d8a3ed | ths | chr = qemu_mallocz(sizeof(CharDriverState));
|
2071 | 20d8a3ed | ths | if (!chr)
|
2072 | 20d8a3ed | ths | return NULL; |
2073 | 20d8a3ed | ths | d = qemu_mallocz(sizeof(MuxDriver));
|
2074 | 20d8a3ed | ths | if (!d) {
|
2075 | 20d8a3ed | ths | free(chr); |
2076 | 20d8a3ed | ths | return NULL; |
2077 | 20d8a3ed | ths | } |
2078 | 20d8a3ed | ths | |
2079 | 20d8a3ed | ths | chr->opaque = d; |
2080 | 20d8a3ed | ths | d->drv = drv; |
2081 | 20d8a3ed | ths | chr->focus = -1;
|
2082 | 20d8a3ed | ths | chr->chr_write = mux_chr_write; |
2083 | 20d8a3ed | ths | chr->chr_update_read_handler = mux_chr_update_read_handler; |
2084 | bd9bdce6 | balrog | chr->chr_accept_input = mux_chr_accept_input; |
2085 | 20d8a3ed | ths | return chr;
|
2086 | 20d8a3ed | ths | } |
2087 | 20d8a3ed | ths | |
2088 | 20d8a3ed | ths | |
2089 | fd1dff4b | bellard | #ifdef _WIN32
|
2090 | 82c643ff | bellard | |
2091 | fd1dff4b | bellard | static void socket_cleanup(void) |
2092 | fd1dff4b | bellard | { |
2093 | fd1dff4b | bellard | WSACleanup(); |
2094 | fd1dff4b | bellard | } |
2095 | 82c643ff | bellard | |
2096 | fd1dff4b | bellard | static int socket_init(void) |
2097 | fd1dff4b | bellard | { |
2098 | fd1dff4b | bellard | WSADATA Data; |
2099 | fd1dff4b | bellard | int ret, err;
|
2100 | fd1dff4b | bellard | |
2101 | fd1dff4b | bellard | ret = WSAStartup(MAKEWORD(2,2), &Data); |
2102 | fd1dff4b | bellard | if (ret != 0) { |
2103 | fd1dff4b | bellard | err = WSAGetLastError(); |
2104 | fd1dff4b | bellard | fprintf(stderr, "WSAStartup: %d\n", err);
|
2105 | fd1dff4b | bellard | return -1; |
2106 | fd1dff4b | bellard | } |
2107 | fd1dff4b | bellard | atexit(socket_cleanup); |
2108 | fd1dff4b | bellard | return 0; |
2109 | fd1dff4b | bellard | } |
2110 | fd1dff4b | bellard | |
2111 | fd1dff4b | bellard | static int send_all(int fd, const uint8_t *buf, int len1) |
2112 | fd1dff4b | bellard | { |
2113 | fd1dff4b | bellard | int ret, len;
|
2114 | 3b46e624 | ths | |
2115 | fd1dff4b | bellard | len = len1; |
2116 | fd1dff4b | bellard | while (len > 0) { |
2117 | fd1dff4b | bellard | ret = send(fd, buf, len, 0);
|
2118 | fd1dff4b | bellard | if (ret < 0) { |
2119 | fd1dff4b | bellard | int errno;
|
2120 | fd1dff4b | bellard | errno = WSAGetLastError(); |
2121 | fd1dff4b | bellard | if (errno != WSAEWOULDBLOCK) {
|
2122 | fd1dff4b | bellard | return -1; |
2123 | fd1dff4b | bellard | } |
2124 | fd1dff4b | bellard | } else if (ret == 0) { |
2125 | fd1dff4b | bellard | break;
|
2126 | fd1dff4b | bellard | } else {
|
2127 | fd1dff4b | bellard | buf += ret; |
2128 | fd1dff4b | bellard | len -= ret; |
2129 | fd1dff4b | bellard | } |
2130 | fd1dff4b | bellard | } |
2131 | fd1dff4b | bellard | return len1 - len;
|
2132 | fd1dff4b | bellard | } |
2133 | fd1dff4b | bellard | |
2134 | fd1dff4b | bellard | void socket_set_nonblock(int fd) |
2135 | fd1dff4b | bellard | { |
2136 | fd1dff4b | bellard | unsigned long opt = 1; |
2137 | fd1dff4b | bellard | ioctlsocket(fd, FIONBIO, &opt); |
2138 | fd1dff4b | bellard | } |
2139 | fd1dff4b | bellard | |
2140 | fd1dff4b | bellard | #else
|
2141 | fd1dff4b | bellard | |
2142 | 1d96905d | bellard | static int unix_write(int fd, const uint8_t *buf, int len1) |
2143 | 1d96905d | bellard | { |
2144 | 1d96905d | bellard | int ret, len;
|
2145 | 1d96905d | bellard | |
2146 | 1d96905d | bellard | len = len1; |
2147 | 1d96905d | bellard | while (len > 0) { |
2148 | 1d96905d | bellard | ret = write(fd, buf, len); |
2149 | 1d96905d | bellard | if (ret < 0) { |
2150 | 1d96905d | bellard | if (errno != EINTR && errno != EAGAIN)
|
2151 | 1d96905d | bellard | return -1; |
2152 | 1d96905d | bellard | } else if (ret == 0) { |
2153 | 1d96905d | bellard | break;
|
2154 | 1d96905d | bellard | } else {
|
2155 | 1d96905d | bellard | buf += ret; |
2156 | 1d96905d | bellard | len -= ret; |
2157 | 1d96905d | bellard | } |
2158 | 1d96905d | bellard | } |
2159 | 1d96905d | bellard | return len1 - len;
|
2160 | 1d96905d | bellard | } |
2161 | 1d96905d | bellard | |
2162 | fd1dff4b | bellard | static inline int send_all(int fd, const uint8_t *buf, int len1) |
2163 | fd1dff4b | bellard | { |
2164 | fd1dff4b | bellard | return unix_write(fd, buf, len1);
|
2165 | fd1dff4b | bellard | } |
2166 | fd1dff4b | bellard | |
2167 | fd1dff4b | bellard | void socket_set_nonblock(int fd) |
2168 | fd1dff4b | bellard | { |
2169 | fd58ff9d | ths | int f;
|
2170 | fd58ff9d | ths | f = fcntl(fd, F_GETFL); |
2171 | fd58ff9d | ths | fcntl(fd, F_SETFL, f | O_NONBLOCK); |
2172 | fd1dff4b | bellard | } |
2173 | fd1dff4b | bellard | #endif /* !_WIN32 */ |
2174 | fd1dff4b | bellard | |
2175 | fd1dff4b | bellard | #ifndef _WIN32
|
2176 | fd1dff4b | bellard | |
2177 | fd1dff4b | bellard | typedef struct { |
2178 | fd1dff4b | bellard | int fd_in, fd_out;
|
2179 | fd1dff4b | bellard | int max_size;
|
2180 | fd1dff4b | bellard | } FDCharDriver; |
2181 | fd1dff4b | bellard | |
2182 | 20d8a3ed | ths | #define STDIO_MAX_CLIENTS 1 |
2183 | 20d8a3ed | ths | static int stdio_nb_clients = 0; |
2184 | fd1dff4b | bellard | |
2185 | 82c643ff | bellard | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
2186 | 82c643ff | bellard | { |
2187 | 82c643ff | bellard | FDCharDriver *s = chr->opaque; |
2188 | 1d96905d | bellard | return unix_write(s->fd_out, buf, len);
|
2189 | 82c643ff | bellard | } |
2190 | 82c643ff | bellard | |
2191 | 7c9d8e07 | bellard | static int fd_chr_read_poll(void *opaque) |
2192 | 7c9d8e07 | bellard | { |
2193 | 7c9d8e07 | bellard | CharDriverState *chr = opaque; |
2194 | 7c9d8e07 | bellard | FDCharDriver *s = chr->opaque; |
2195 | 7c9d8e07 | bellard | |
2196 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
2197 | 7c9d8e07 | bellard | return s->max_size;
|
2198 | 7c9d8e07 | bellard | } |
2199 | 7c9d8e07 | bellard | |
2200 | 7c9d8e07 | bellard | static void fd_chr_read(void *opaque) |
2201 | 7c9d8e07 | bellard | { |
2202 | 7c9d8e07 | bellard | CharDriverState *chr = opaque; |
2203 | 7c9d8e07 | bellard | FDCharDriver *s = chr->opaque; |
2204 | 7c9d8e07 | bellard | int size, len;
|
2205 | 7c9d8e07 | bellard | uint8_t buf[1024];
|
2206 | 3b46e624 | ths | |
2207 | 7c9d8e07 | bellard | len = sizeof(buf);
|
2208 | 7c9d8e07 | bellard | if (len > s->max_size)
|
2209 | 7c9d8e07 | bellard | len = s->max_size; |
2210 | 7c9d8e07 | bellard | if (len == 0) |
2211 | 7c9d8e07 | bellard | return;
|
2212 | 7c9d8e07 | bellard | size = read(s->fd_in, buf, len); |
2213 | 188157fe | pbrook | if (size == 0) { |
2214 | 188157fe | pbrook | /* FD has been closed. Remove it from the active list. */
|
2215 | 188157fe | pbrook | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
2216 | 188157fe | pbrook | return;
|
2217 | 188157fe | pbrook | } |
2218 | 7c9d8e07 | bellard | if (size > 0) { |
2219 | e5b0bc44 | pbrook | qemu_chr_read(chr, buf, size); |
2220 | 7c9d8e07 | bellard | } |
2221 | 7c9d8e07 | bellard | } |
2222 | 7c9d8e07 | bellard | |
2223 | e5b0bc44 | pbrook | static void fd_chr_update_read_handler(CharDriverState *chr) |
2224 | 82c643ff | bellard | { |
2225 | 82c643ff | bellard | FDCharDriver *s = chr->opaque; |
2226 | 82c643ff | bellard | |
2227 | f8d179e3 | bellard | if (s->fd_in >= 0) { |
2228 | f8d179e3 | bellard | if (nographic && s->fd_in == 0) { |
2229 | f8d179e3 | bellard | } else {
|
2230 | 5fafdf24 | ths | qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, |
2231 | 7c9d8e07 | bellard | fd_chr_read, NULL, chr);
|
2232 | f8d179e3 | bellard | } |
2233 | 82c643ff | bellard | } |
2234 | 82c643ff | bellard | } |
2235 | 82c643ff | bellard | |
2236 | a11d070e | balrog | static void fd_chr_close(struct CharDriverState *chr) |
2237 | a11d070e | balrog | { |
2238 | a11d070e | balrog | FDCharDriver *s = chr->opaque; |
2239 | a11d070e | balrog | |
2240 | a11d070e | balrog | if (s->fd_in >= 0) { |
2241 | a11d070e | balrog | if (nographic && s->fd_in == 0) { |
2242 | a11d070e | balrog | } else {
|
2243 | a11d070e | balrog | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
2244 | a11d070e | balrog | } |
2245 | a11d070e | balrog | } |
2246 | a11d070e | balrog | |
2247 | a11d070e | balrog | qemu_free(s); |
2248 | a11d070e | balrog | } |
2249 | a11d070e | balrog | |
2250 | 82c643ff | bellard | /* open a character device to a unix fd */
|
2251 | 52f61fde | ths | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
2252 | 82c643ff | bellard | { |
2253 | 82c643ff | bellard | CharDriverState *chr; |
2254 | 82c643ff | bellard | FDCharDriver *s; |
2255 | 82c643ff | bellard | |
2256 | 82c643ff | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
2257 | 82c643ff | bellard | if (!chr)
|
2258 | 82c643ff | bellard | return NULL; |
2259 | 82c643ff | bellard | s = qemu_mallocz(sizeof(FDCharDriver));
|
2260 | 82c643ff | bellard | if (!s) {
|
2261 | 82c643ff | bellard | free(chr); |
2262 | 82c643ff | bellard | return NULL; |
2263 | 82c643ff | bellard | } |
2264 | 82c643ff | bellard | s->fd_in = fd_in; |
2265 | 82c643ff | bellard | s->fd_out = fd_out; |
2266 | 82c643ff | bellard | chr->opaque = s; |
2267 | 82c643ff | bellard | chr->chr_write = fd_chr_write; |
2268 | e5b0bc44 | pbrook | chr->chr_update_read_handler = fd_chr_update_read_handler; |
2269 | a11d070e | balrog | chr->chr_close = fd_chr_close; |
2270 | 86e94dea | ths | |
2271 | 86e94dea | ths | qemu_chr_reset(chr); |
2272 | 86e94dea | ths | |
2273 | 82c643ff | bellard | return chr;
|
2274 | 82c643ff | bellard | } |
2275 | 82c643ff | bellard | |
2276 | 52f61fde | ths | static CharDriverState *qemu_chr_open_file_out(const char *file_out) |
2277 | f8d179e3 | bellard | { |
2278 | f8d179e3 | bellard | int fd_out;
|
2279 | f8d179e3 | bellard | |
2280 | aeb30be6 | balrog | TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
|
2281 | f8d179e3 | bellard | if (fd_out < 0) |
2282 | f8d179e3 | bellard | return NULL; |
2283 | f8d179e3 | bellard | return qemu_chr_open_fd(-1, fd_out); |
2284 | f8d179e3 | bellard | } |
2285 | f8d179e3 | bellard | |
2286 | 52f61fde | ths | static CharDriverState *qemu_chr_open_pipe(const char *filename) |
2287 | f8d179e3 | bellard | { |
2288 | c26c1c4b | ths | int fd_in, fd_out;
|
2289 | c26c1c4b | ths | char filename_in[256], filename_out[256]; |
2290 | c26c1c4b | ths | |
2291 | c26c1c4b | ths | snprintf(filename_in, 256, "%s.in", filename); |
2292 | c26c1c4b | ths | snprintf(filename_out, 256, "%s.out", filename); |
2293 | aeb30be6 | balrog | TFR(fd_in = open(filename_in, O_RDWR | O_BINARY)); |
2294 | aeb30be6 | balrog | TFR(fd_out = open(filename_out, O_RDWR | O_BINARY)); |
2295 | c26c1c4b | ths | if (fd_in < 0 || fd_out < 0) { |
2296 | c26c1c4b | ths | if (fd_in >= 0) |
2297 | c26c1c4b | ths | close(fd_in); |
2298 | c26c1c4b | ths | if (fd_out >= 0) |
2299 | c26c1c4b | ths | close(fd_out); |
2300 | aeb30be6 | balrog | TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY)); |
2301 | c26c1c4b | ths | if (fd_in < 0) |
2302 | c26c1c4b | ths | return NULL; |
2303 | c26c1c4b | ths | } |
2304 | c26c1c4b | ths | return qemu_chr_open_fd(fd_in, fd_out);
|
2305 | f8d179e3 | bellard | } |
2306 | f8d179e3 | bellard | |
2307 | f8d179e3 | bellard | |
2308 | 82c643ff | bellard | /* for STDIO, we handle the case where several clients use it
|
2309 | 82c643ff | bellard | (nographic mode) */
|
2310 | 82c643ff | bellard | |
2311 | aa0bc6b6 | bellard | #define TERM_FIFO_MAX_SIZE 1 |
2312 | aa0bc6b6 | bellard | |
2313 | aa0bc6b6 | bellard | static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
|
2314 | 1dce7c3c | bellard | static int term_fifo_size; |
2315 | 330d0414 | bellard | |
2316 | 7c9d8e07 | bellard | static int stdio_read_poll(void *opaque) |
2317 | 82c643ff | bellard | { |
2318 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2319 | aa0bc6b6 | bellard | |
2320 | 20d8a3ed | ths | /* try to flush the queue if needed */
|
2321 | 20d8a3ed | ths | if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) { |
2322 | 20d8a3ed | ths | qemu_chr_read(chr, term_fifo, 1);
|
2323 | 20d8a3ed | ths | term_fifo_size = 0;
|
2324 | aa0bc6b6 | bellard | } |
2325 | 20d8a3ed | ths | /* see if we can absorb more chars */
|
2326 | 20d8a3ed | ths | if (term_fifo_size == 0) |
2327 | 20d8a3ed | ths | return 1; |
2328 | 20d8a3ed | ths | else
|
2329 | 20d8a3ed | ths | return 0; |
2330 | 82c643ff | bellard | } |
2331 | 82c643ff | bellard | |
2332 | 7c9d8e07 | bellard | static void stdio_read(void *opaque) |
2333 | 82c643ff | bellard | { |
2334 | 7c9d8e07 | bellard | int size;
|
2335 | 7c9d8e07 | bellard | uint8_t buf[1];
|
2336 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2337 | 20d8a3ed | ths | |
2338 | 7c9d8e07 | bellard | size = read(0, buf, 1); |
2339 | 519945df | pbrook | if (size == 0) { |
2340 | 519945df | pbrook | /* stdin has been closed. Remove it from the active list. */
|
2341 | 519945df | pbrook | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
2342 | 519945df | pbrook | return;
|
2343 | 519945df | pbrook | } |
2344 | 20d8a3ed | ths | if (size > 0) { |
2345 | 20d8a3ed | ths | if (qemu_chr_can_read(chr) > 0) { |
2346 | 20d8a3ed | ths | qemu_chr_read(chr, buf, 1);
|
2347 | 20d8a3ed | ths | } else if (term_fifo_size == 0) { |
2348 | 20d8a3ed | ths | term_fifo[term_fifo_size++] = buf[0];
|
2349 | 1dce7c3c | bellard | } |
2350 | 1dce7c3c | bellard | } |
2351 | 1dce7c3c | bellard | } |
2352 | 1dce7c3c | bellard | |
2353 | 8d11df9e | bellard | /* init terminal so that we can grab keys */
|
2354 | 8d11df9e | bellard | static struct termios oldtty; |
2355 | 8d11df9e | bellard | static int old_fd0_flags; |
2356 | a11d070e | balrog | static int term_atexit_done; |
2357 | 8d11df9e | bellard | |
2358 | 8d11df9e | bellard | static void term_exit(void) |
2359 | 8d11df9e | bellard | { |
2360 | 8d11df9e | bellard | tcsetattr (0, TCSANOW, &oldtty);
|
2361 | 8d11df9e | bellard | fcntl(0, F_SETFL, old_fd0_flags);
|
2362 | 8d11df9e | bellard | } |
2363 | 8d11df9e | bellard | |
2364 | 8d11df9e | bellard | static void term_init(void) |
2365 | 8d11df9e | bellard | { |
2366 | 8d11df9e | bellard | struct termios tty;
|
2367 | 8d11df9e | bellard | |
2368 | 8d11df9e | bellard | tcgetattr (0, &tty);
|
2369 | 8d11df9e | bellard | oldtty = tty; |
2370 | 8d11df9e | bellard | old_fd0_flags = fcntl(0, F_GETFL);
|
2371 | 8d11df9e | bellard | |
2372 | 8d11df9e | bellard | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
2373 | 8d11df9e | bellard | |INLCR|IGNCR|ICRNL|IXON); |
2374 | 8d11df9e | bellard | tty.c_oflag |= OPOST; |
2375 | 8d11df9e | bellard | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
2376 | 8d11df9e | bellard | /* if graphical mode, we allow Ctrl-C handling */
|
2377 | 8d11df9e | bellard | if (nographic)
|
2378 | 8d11df9e | bellard | tty.c_lflag &= ~ISIG; |
2379 | 8d11df9e | bellard | tty.c_cflag &= ~(CSIZE|PARENB); |
2380 | 8d11df9e | bellard | tty.c_cflag |= CS8; |
2381 | 8d11df9e | bellard | tty.c_cc[VMIN] = 1;
|
2382 | 8d11df9e | bellard | tty.c_cc[VTIME] = 0;
|
2383 | 3b46e624 | ths | |
2384 | 8d11df9e | bellard | tcsetattr (0, TCSANOW, &tty);
|
2385 | 8d11df9e | bellard | |
2386 | a11d070e | balrog | if (!term_atexit_done++)
|
2387 | a11d070e | balrog | atexit(term_exit); |
2388 | 8d11df9e | bellard | |
2389 | 8d11df9e | bellard | fcntl(0, F_SETFL, O_NONBLOCK);
|
2390 | 8d11df9e | bellard | } |
2391 | 8d11df9e | bellard | |
2392 | a11d070e | balrog | static void qemu_chr_close_stdio(struct CharDriverState *chr) |
2393 | a11d070e | balrog | { |
2394 | a11d070e | balrog | term_exit(); |
2395 | a11d070e | balrog | stdio_nb_clients--; |
2396 | a11d070e | balrog | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
2397 | a11d070e | balrog | fd_chr_close(chr); |
2398 | a11d070e | balrog | } |
2399 | a11d070e | balrog | |
2400 | 52f61fde | ths | static CharDriverState *qemu_chr_open_stdio(void) |
2401 | 82c643ff | bellard | { |
2402 | 82c643ff | bellard | CharDriverState *chr; |
2403 | 82c643ff | bellard | |
2404 | 20d8a3ed | ths | if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
|
2405 | 20d8a3ed | ths | return NULL; |
2406 | 20d8a3ed | ths | chr = qemu_chr_open_fd(0, 1); |
2407 | a11d070e | balrog | chr->chr_close = qemu_chr_close_stdio; |
2408 | 20d8a3ed | ths | qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr); |
2409 | 20d8a3ed | ths | stdio_nb_clients++; |
2410 | 20d8a3ed | ths | term_init(); |
2411 | 20d8a3ed | ths | |
2412 | 82c643ff | bellard | return chr;
|
2413 | 82c643ff | bellard | } |
2414 | 82c643ff | bellard | |
2415 | 64b7b733 | aurel32 | #ifdef __sun__
|
2416 | 64b7b733 | aurel32 | /* Once Solaris has openpty(), this is going to be removed. */
|
2417 | 64b7b733 | aurel32 | int openpty(int *amaster, int *aslave, char *name, |
2418 | 64b7b733 | aurel32 | struct termios *termp, struct winsize *winp) |
2419 | 64b7b733 | aurel32 | { |
2420 | 64b7b733 | aurel32 | const char *slave; |
2421 | 64b7b733 | aurel32 | int mfd = -1, sfd = -1; |
2422 | 64b7b733 | aurel32 | |
2423 | 64b7b733 | aurel32 | *amaster = *aslave = -1;
|
2424 | 64b7b733 | aurel32 | |
2425 | 64b7b733 | aurel32 | mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
2426 | 64b7b733 | aurel32 | if (mfd < 0) |
2427 | 64b7b733 | aurel32 | goto err;
|
2428 | 64b7b733 | aurel32 | |
2429 | 64b7b733 | aurel32 | if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) |
2430 | 64b7b733 | aurel32 | goto err;
|
2431 | 64b7b733 | aurel32 | |
2432 | 64b7b733 | aurel32 | if ((slave = ptsname(mfd)) == NULL) |
2433 | 64b7b733 | aurel32 | goto err;
|
2434 | 64b7b733 | aurel32 | |
2435 | 64b7b733 | aurel32 | if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) |
2436 | 64b7b733 | aurel32 | goto err;
|
2437 | 64b7b733 | aurel32 | |
2438 | 64b7b733 | aurel32 | if (ioctl(sfd, I_PUSH, "ptem") == -1 || |
2439 | 64b7b733 | aurel32 | (termp != NULL && tcgetattr(sfd, termp) < 0)) |
2440 | 64b7b733 | aurel32 | goto err;
|
2441 | 64b7b733 | aurel32 | |
2442 | 64b7b733 | aurel32 | if (amaster)
|
2443 | 64b7b733 | aurel32 | *amaster = mfd; |
2444 | 64b7b733 | aurel32 | if (aslave)
|
2445 | 64b7b733 | aurel32 | *aslave = sfd; |
2446 | 64b7b733 | aurel32 | if (winp)
|
2447 | 64b7b733 | aurel32 | ioctl(sfd, TIOCSWINSZ, winp); |
2448 | 64b7b733 | aurel32 | |
2449 | 64b7b733 | aurel32 | return 0; |
2450 | 64b7b733 | aurel32 | |
2451 | 64b7b733 | aurel32 | err:
|
2452 | 64b7b733 | aurel32 | if (sfd != -1) |
2453 | 64b7b733 | aurel32 | close(sfd); |
2454 | 64b7b733 | aurel32 | close(mfd); |
2455 | 64b7b733 | aurel32 | return -1; |
2456 | 64b7b733 | aurel32 | } |
2457 | 64b7b733 | aurel32 | |
2458 | 64b7b733 | aurel32 | void cfmakeraw (struct termios *termios_p) |
2459 | 64b7b733 | aurel32 | { |
2460 | 64b7b733 | aurel32 | termios_p->c_iflag &= |
2461 | 64b7b733 | aurel32 | ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); |
2462 | 64b7b733 | aurel32 | termios_p->c_oflag &= ~OPOST; |
2463 | 64b7b733 | aurel32 | termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); |
2464 | 64b7b733 | aurel32 | termios_p->c_cflag &= ~(CSIZE|PARENB); |
2465 | 64b7b733 | aurel32 | termios_p->c_cflag |= CS8; |
2466 | 64b7b733 | aurel32 | |
2467 | 64b7b733 | aurel32 | termios_p->c_cc[VMIN] = 0;
|
2468 | 64b7b733 | aurel32 | termios_p->c_cc[VTIME] = 0;
|
2469 | 64b7b733 | aurel32 | } |
2470 | 64b7b733 | aurel32 | #endif
|
2471 | 64b7b733 | aurel32 | |
2472 | 9892fbfb | blueswir1 | #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
|
2473 | 9892fbfb | blueswir1 | || defined(__NetBSD__) || defined(__OpenBSD__) |
2474 | 279e694b | aliguori | |
2475 | 279e694b | aliguori | typedef struct { |
2476 | 279e694b | aliguori | int fd;
|
2477 | 279e694b | aliguori | int connected;
|
2478 | 279e694b | aliguori | int polling;
|
2479 | 279e694b | aliguori | int read_bytes;
|
2480 | 279e694b | aliguori | QEMUTimer *timer; |
2481 | 279e694b | aliguori | } PtyCharDriver; |
2482 | 279e694b | aliguori | |
2483 | 279e694b | aliguori | static void pty_chr_update_read_handler(CharDriverState *chr); |
2484 | 279e694b | aliguori | static void pty_chr_state(CharDriverState *chr, int connected); |
2485 | 279e694b | aliguori | |
2486 | 279e694b | aliguori | static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
2487 | 279e694b | aliguori | { |
2488 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2489 | 279e694b | aliguori | |
2490 | 279e694b | aliguori | if (!s->connected) {
|
2491 | 279e694b | aliguori | /* guest sends data, check for (re-)connect */
|
2492 | 279e694b | aliguori | pty_chr_update_read_handler(chr); |
2493 | 279e694b | aliguori | return 0; |
2494 | 279e694b | aliguori | } |
2495 | 279e694b | aliguori | return unix_write(s->fd, buf, len);
|
2496 | 279e694b | aliguori | } |
2497 | 279e694b | aliguori | |
2498 | 279e694b | aliguori | static int pty_chr_read_poll(void *opaque) |
2499 | 279e694b | aliguori | { |
2500 | 279e694b | aliguori | CharDriverState *chr = opaque; |
2501 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2502 | 279e694b | aliguori | |
2503 | 279e694b | aliguori | s->read_bytes = qemu_chr_can_read(chr); |
2504 | 279e694b | aliguori | return s->read_bytes;
|
2505 | 279e694b | aliguori | } |
2506 | 279e694b | aliguori | |
2507 | 279e694b | aliguori | static void pty_chr_read(void *opaque) |
2508 | 279e694b | aliguori | { |
2509 | 279e694b | aliguori | CharDriverState *chr = opaque; |
2510 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2511 | 279e694b | aliguori | int size, len;
|
2512 | 279e694b | aliguori | uint8_t buf[1024];
|
2513 | 279e694b | aliguori | |
2514 | 279e694b | aliguori | len = sizeof(buf);
|
2515 | 279e694b | aliguori | if (len > s->read_bytes)
|
2516 | 279e694b | aliguori | len = s->read_bytes; |
2517 | 279e694b | aliguori | if (len == 0) |
2518 | 279e694b | aliguori | return;
|
2519 | 279e694b | aliguori | size = read(s->fd, buf, len); |
2520 | 279e694b | aliguori | if ((size == -1 && errno == EIO) || |
2521 | 279e694b | aliguori | (size == 0)) {
|
2522 | 279e694b | aliguori | pty_chr_state(chr, 0);
|
2523 | 279e694b | aliguori | return;
|
2524 | 279e694b | aliguori | } |
2525 | 279e694b | aliguori | if (size > 0) { |
2526 | 279e694b | aliguori | pty_chr_state(chr, 1);
|
2527 | 279e694b | aliguori | qemu_chr_read(chr, buf, size); |
2528 | 279e694b | aliguori | } |
2529 | 279e694b | aliguori | } |
2530 | 279e694b | aliguori | |
2531 | 279e694b | aliguori | static void pty_chr_update_read_handler(CharDriverState *chr) |
2532 | 279e694b | aliguori | { |
2533 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2534 | 279e694b | aliguori | |
2535 | 279e694b | aliguori | qemu_set_fd_handler2(s->fd, pty_chr_read_poll, |
2536 | 279e694b | aliguori | pty_chr_read, NULL, chr);
|
2537 | 279e694b | aliguori | s->polling = 1;
|
2538 | 279e694b | aliguori | /*
|
2539 | 279e694b | aliguori | * Short timeout here: just need wait long enougth that qemu makes
|
2540 | 279e694b | aliguori | * it through the poll loop once. When reconnected we want a
|
2541 | 279e694b | aliguori | * short timeout so we notice it almost instantly. Otherwise
|
2542 | 279e694b | aliguori | * read() gives us -EIO instantly, making pty_chr_state() reset the
|
2543 | 279e694b | aliguori | * timeout to the normal (much longer) poll interval before the
|
2544 | 279e694b | aliguori | * timer triggers.
|
2545 | 279e694b | aliguori | */
|
2546 | 279e694b | aliguori | qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
|
2547 | 279e694b | aliguori | } |
2548 | 279e694b | aliguori | |
2549 | 279e694b | aliguori | static void pty_chr_state(CharDriverState *chr, int connected) |
2550 | 279e694b | aliguori | { |
2551 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2552 | 279e694b | aliguori | |
2553 | 279e694b | aliguori | if (!connected) {
|
2554 | 279e694b | aliguori | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
2555 | 279e694b | aliguori | s->connected = 0;
|
2556 | 279e694b | aliguori | s->polling = 0;
|
2557 | 279e694b | aliguori | /* (re-)connect poll interval for idle guests: once per second.
|
2558 | 279e694b | aliguori | * We check more frequently in case the guests sends data to
|
2559 | 279e694b | aliguori | * the virtual device linked to our pty. */
|
2560 | 279e694b | aliguori | qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
|
2561 | 279e694b | aliguori | } else {
|
2562 | 279e694b | aliguori | if (!s->connected)
|
2563 | 279e694b | aliguori | qemu_chr_reset(chr); |
2564 | 279e694b | aliguori | s->connected = 1;
|
2565 | 279e694b | aliguori | } |
2566 | 279e694b | aliguori | } |
2567 | 279e694b | aliguori | |
2568 | 8fcd3692 | blueswir1 | static void pty_chr_timer(void *opaque) |
2569 | 279e694b | aliguori | { |
2570 | 279e694b | aliguori | struct CharDriverState *chr = opaque;
|
2571 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2572 | 279e694b | aliguori | |
2573 | 279e694b | aliguori | if (s->connected)
|
2574 | 279e694b | aliguori | return;
|
2575 | 279e694b | aliguori | if (s->polling) {
|
2576 | 279e694b | aliguori | /* If we arrive here without polling being cleared due
|
2577 | 279e694b | aliguori | * read returning -EIO, then we are (re-)connected */
|
2578 | 279e694b | aliguori | pty_chr_state(chr, 1);
|
2579 | 279e694b | aliguori | return;
|
2580 | 279e694b | aliguori | } |
2581 | 279e694b | aliguori | |
2582 | 279e694b | aliguori | /* Next poll ... */
|
2583 | 279e694b | aliguori | pty_chr_update_read_handler(chr); |
2584 | 279e694b | aliguori | } |
2585 | 279e694b | aliguori | |
2586 | 279e694b | aliguori | static void pty_chr_close(struct CharDriverState *chr) |
2587 | 279e694b | aliguori | { |
2588 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2589 | 279e694b | aliguori | |
2590 | 279e694b | aliguori | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
2591 | 279e694b | aliguori | close(s->fd); |
2592 | 279e694b | aliguori | qemu_free(s); |
2593 | 279e694b | aliguori | } |
2594 | 279e694b | aliguori | |
2595 | 52f61fde | ths | static CharDriverState *qemu_chr_open_pty(void) |
2596 | 82c643ff | bellard | { |
2597 | 279e694b | aliguori | CharDriverState *chr; |
2598 | 279e694b | aliguori | PtyCharDriver *s; |
2599 | 91fc2119 | bellard | struct termios tty;
|
2600 | 279e694b | aliguori | int slave_fd;
|
2601 | 9892fbfb | blueswir1 | #if defined(__OpenBSD__)
|
2602 | 9892fbfb | blueswir1 | char pty_name[PATH_MAX];
|
2603 | 9892fbfb | blueswir1 | #define q_ptsname(x) pty_name
|
2604 | 9892fbfb | blueswir1 | #else
|
2605 | 9892fbfb | blueswir1 | char *pty_name = NULL; |
2606 | 9892fbfb | blueswir1 | #define q_ptsname(x) ptsname(x)
|
2607 | 9892fbfb | blueswir1 | #endif
|
2608 | 279e694b | aliguori | |
2609 | 279e694b | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
2610 | 279e694b | aliguori | if (!chr)
|
2611 | 279e694b | aliguori | return NULL; |
2612 | 279e694b | aliguori | s = qemu_mallocz(sizeof(PtyCharDriver));
|
2613 | 279e694b | aliguori | if (!s) {
|
2614 | 279e694b | aliguori | qemu_free(chr); |
2615 | 279e694b | aliguori | return NULL; |
2616 | 279e694b | aliguori | } |
2617 | 3b46e624 | ths | |
2618 | 9892fbfb | blueswir1 | if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) { |
2619 | 82c643ff | bellard | return NULL; |
2620 | 82c643ff | bellard | } |
2621 | 3b46e624 | ths | |
2622 | 64b7b733 | aurel32 | /* Set raw attributes on the pty. */
|
2623 | 64b7b733 | aurel32 | cfmakeraw(&tty); |
2624 | 64b7b733 | aurel32 | tcsetattr(slave_fd, TCSAFLUSH, &tty); |
2625 | 279e694b | aliguori | close(slave_fd); |
2626 | 279e694b | aliguori | |
2627 | 9892fbfb | blueswir1 | fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
|
2628 | 91fc2119 | bellard | |
2629 | 279e694b | aliguori | chr->opaque = s; |
2630 | 279e694b | aliguori | chr->chr_write = pty_chr_write; |
2631 | 279e694b | aliguori | chr->chr_update_read_handler = pty_chr_update_read_handler; |
2632 | 279e694b | aliguori | chr->chr_close = pty_chr_close; |
2633 | 279e694b | aliguori | |
2634 | 279e694b | aliguori | s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr); |
2635 | 279e694b | aliguori | |
2636 | 279e694b | aliguori | return chr;
|
2637 | 82c643ff | bellard | } |
2638 | f8d179e3 | bellard | |
2639 | 5fafdf24 | ths | static void tty_serial_init(int fd, int speed, |
2640 | f8d179e3 | bellard | int parity, int data_bits, int stop_bits) |
2641 | f8d179e3 | bellard | { |
2642 | f8d179e3 | bellard | struct termios tty;
|
2643 | f8d179e3 | bellard | speed_t spd; |
2644 | f8d179e3 | bellard | |
2645 | e57a8c0e | bellard | #if 0
|
2646 | 5fafdf24 | ths | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
|
2647 | e57a8c0e | bellard | speed, parity, data_bits, stop_bits);
|
2648 | e57a8c0e | bellard | #endif
|
2649 | e57a8c0e | bellard | tcgetattr (fd, &tty); |
2650 | f8d179e3 | bellard | |
2651 | a7954218 | balrog | #define MARGIN 1.1 |
2652 | a7954218 | balrog | if (speed <= 50 * MARGIN) |
2653 | f8d179e3 | bellard | spd = B50; |
2654 | a7954218 | balrog | else if (speed <= 75 * MARGIN) |
2655 | f8d179e3 | bellard | spd = B75; |
2656 | a7954218 | balrog | else if (speed <= 300 * MARGIN) |
2657 | f8d179e3 | bellard | spd = B300; |
2658 | a7954218 | balrog | else if (speed <= 600 * MARGIN) |
2659 | f8d179e3 | bellard | spd = B600; |
2660 | a7954218 | balrog | else if (speed <= 1200 * MARGIN) |
2661 | f8d179e3 | bellard | spd = B1200; |
2662 | a7954218 | balrog | else if (speed <= 2400 * MARGIN) |
2663 | f8d179e3 | bellard | spd = B2400; |
2664 | a7954218 | balrog | else if (speed <= 4800 * MARGIN) |
2665 | f8d179e3 | bellard | spd = B4800; |
2666 | a7954218 | balrog | else if (speed <= 9600 * MARGIN) |
2667 | f8d179e3 | bellard | spd = B9600; |
2668 | a7954218 | balrog | else if (speed <= 19200 * MARGIN) |
2669 | f8d179e3 | bellard | spd = B19200; |
2670 | a7954218 | balrog | else if (speed <= 38400 * MARGIN) |
2671 | f8d179e3 | bellard | spd = B38400; |
2672 | a7954218 | balrog | else if (speed <= 57600 * MARGIN) |
2673 | f8d179e3 | bellard | spd = B57600; |
2674 | a7954218 | balrog | else if (speed <= 115200 * MARGIN) |
2675 | a7954218 | balrog | spd = B115200; |
2676 | a7954218 | balrog | else
|
2677 | f8d179e3 | bellard | spd = B115200; |
2678 | f8d179e3 | bellard | |
2679 | f8d179e3 | bellard | cfsetispeed(&tty, spd); |
2680 | f8d179e3 | bellard | cfsetospeed(&tty, spd); |
2681 | f8d179e3 | bellard | |
2682 | f8d179e3 | bellard | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
2683 | f8d179e3 | bellard | |INLCR|IGNCR|ICRNL|IXON); |
2684 | f8d179e3 | bellard | tty.c_oflag |= OPOST; |
2685 | f8d179e3 | bellard | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
2686 | 094eed6c | bellard | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
2687 | f8d179e3 | bellard | switch(data_bits) {
|
2688 | f8d179e3 | bellard | default:
|
2689 | f8d179e3 | bellard | case 8: |
2690 | f8d179e3 | bellard | tty.c_cflag |= CS8; |
2691 | f8d179e3 | bellard | break;
|
2692 | f8d179e3 | bellard | case 7: |
2693 | f8d179e3 | bellard | tty.c_cflag |= CS7; |
2694 | f8d179e3 | bellard | break;
|
2695 | f8d179e3 | bellard | case 6: |
2696 | f8d179e3 | bellard | tty.c_cflag |= CS6; |
2697 | f8d179e3 | bellard | break;
|
2698 | f8d179e3 | bellard | case 5: |
2699 | f8d179e3 | bellard | tty.c_cflag |= CS5; |
2700 | f8d179e3 | bellard | break;
|
2701 | f8d179e3 | bellard | } |
2702 | f8d179e3 | bellard | switch(parity) {
|
2703 | f8d179e3 | bellard | default:
|
2704 | f8d179e3 | bellard | case 'N': |
2705 | f8d179e3 | bellard | break;
|
2706 | f8d179e3 | bellard | case 'E': |
2707 | f8d179e3 | bellard | tty.c_cflag |= PARENB; |
2708 | f8d179e3 | bellard | break;
|
2709 | f8d179e3 | bellard | case 'O': |
2710 | f8d179e3 | bellard | tty.c_cflag |= PARENB | PARODD; |
2711 | f8d179e3 | bellard | break;
|
2712 | f8d179e3 | bellard | } |
2713 | 094eed6c | bellard | if (stop_bits == 2) |
2714 | 094eed6c | bellard | tty.c_cflag |= CSTOPB; |
2715 | 3b46e624 | ths | |
2716 | f8d179e3 | bellard | tcsetattr (fd, TCSANOW, &tty); |
2717 | f8d179e3 | bellard | } |
2718 | f8d179e3 | bellard | |
2719 | e57a8c0e | bellard | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
2720 | f8d179e3 | bellard | { |
2721 | f8d179e3 | bellard | FDCharDriver *s = chr->opaque; |
2722 | 3b46e624 | ths | |
2723 | e57a8c0e | bellard | switch(cmd) {
|
2724 | e57a8c0e | bellard | case CHR_IOCTL_SERIAL_SET_PARAMS:
|
2725 | e57a8c0e | bellard | { |
2726 | e57a8c0e | bellard | QEMUSerialSetParams *ssp = arg; |
2727 | 5fafdf24 | ths | tty_serial_init(s->fd_in, ssp->speed, ssp->parity, |
2728 | e57a8c0e | bellard | ssp->data_bits, ssp->stop_bits); |
2729 | e57a8c0e | bellard | } |
2730 | e57a8c0e | bellard | break;
|
2731 | e57a8c0e | bellard | case CHR_IOCTL_SERIAL_SET_BREAK:
|
2732 | e57a8c0e | bellard | { |
2733 | e57a8c0e | bellard | int enable = *(int *)arg; |
2734 | e57a8c0e | bellard | if (enable)
|
2735 | e57a8c0e | bellard | tcsendbreak(s->fd_in, 1);
|
2736 | e57a8c0e | bellard | } |
2737 | e57a8c0e | bellard | break;
|
2738 | 81174dae | aliguori | case CHR_IOCTL_SERIAL_GET_TIOCM:
|
2739 | 81174dae | aliguori | { |
2740 | 81174dae | aliguori | int sarg = 0; |
2741 | 81174dae | aliguori | int *targ = (int *)arg; |
2742 | 81174dae | aliguori | ioctl(s->fd_in, TIOCMGET, &sarg); |
2743 | 81174dae | aliguori | *targ = 0;
|
2744 | 81174dae | aliguori | if (sarg | TIOCM_CTS)
|
2745 | 81174dae | aliguori | *targ |= CHR_TIOCM_CTS; |
2746 | 81174dae | aliguori | if (sarg | TIOCM_CAR)
|
2747 | 81174dae | aliguori | *targ |= CHR_TIOCM_CAR; |
2748 | 81174dae | aliguori | if (sarg | TIOCM_DSR)
|
2749 | 81174dae | aliguori | *targ |= CHR_TIOCM_DSR; |
2750 | 81174dae | aliguori | if (sarg | TIOCM_RI)
|
2751 | 81174dae | aliguori | *targ |= CHR_TIOCM_RI; |
2752 | 81174dae | aliguori | if (sarg | TIOCM_DTR)
|
2753 | 81174dae | aliguori | *targ |= CHR_TIOCM_DTR; |
2754 | 81174dae | aliguori | if (sarg | TIOCM_RTS)
|
2755 | 81174dae | aliguori | *targ |= CHR_TIOCM_RTS; |
2756 | 81174dae | aliguori | } |
2757 | 81174dae | aliguori | break;
|
2758 | 81174dae | aliguori | case CHR_IOCTL_SERIAL_SET_TIOCM:
|
2759 | 81174dae | aliguori | { |
2760 | 81174dae | aliguori | int sarg = *(int *)arg; |
2761 | 81174dae | aliguori | int targ = 0; |
2762 | 81174dae | aliguori | if (sarg | CHR_TIOCM_DTR)
|
2763 | 81174dae | aliguori | targ |= TIOCM_DTR; |
2764 | 81174dae | aliguori | if (sarg | CHR_TIOCM_RTS)
|
2765 | 81174dae | aliguori | targ |= TIOCM_RTS; |
2766 | 81174dae | aliguori | ioctl(s->fd_in, TIOCMSET, &targ); |
2767 | 81174dae | aliguori | } |
2768 | 81174dae | aliguori | break;
|
2769 | e57a8c0e | bellard | default:
|
2770 | e57a8c0e | bellard | return -ENOTSUP;
|
2771 | e57a8c0e | bellard | } |
2772 | e57a8c0e | bellard | return 0; |
2773 | f8d179e3 | bellard | } |
2774 | f8d179e3 | bellard | |
2775 | 52f61fde | ths | static CharDriverState *qemu_chr_open_tty(const char *filename) |
2776 | f8d179e3 | bellard | { |
2777 | f8d179e3 | bellard | CharDriverState *chr; |
2778 | f8d179e3 | bellard | int fd;
|
2779 | f8d179e3 | bellard | |
2780 | aeb30be6 | balrog | TFR(fd = open(filename, O_RDWR | O_NONBLOCK)); |
2781 | f8d179e3 | bellard | tty_serial_init(fd, 115200, 'N', 8, 1); |
2782 | f8d179e3 | bellard | chr = qemu_chr_open_fd(fd, fd); |
2783 | aeb30be6 | balrog | if (!chr) {
|
2784 | aeb30be6 | balrog | close(fd); |
2785 | f8d179e3 | bellard | return NULL; |
2786 | aeb30be6 | balrog | } |
2787 | e57a8c0e | bellard | chr->chr_ioctl = tty_serial_ioctl; |
2788 | 86e94dea | ths | qemu_chr_reset(chr); |
2789 | e57a8c0e | bellard | return chr;
|
2790 | e57a8c0e | bellard | } |
2791 | aec62507 | ths | #else /* ! __linux__ && ! __sun__ */ |
2792 | aec62507 | ths | static CharDriverState *qemu_chr_open_pty(void) |
2793 | aec62507 | ths | { |
2794 | aec62507 | ths | return NULL; |
2795 | aec62507 | ths | } |
2796 | aec62507 | ths | #endif /* __linux__ || __sun__ */ |
2797 | e57a8c0e | bellard | |
2798 | aec62507 | ths | #if defined(__linux__)
|
2799 | 5867c88a | ths | typedef struct { |
2800 | 5867c88a | ths | int fd;
|
2801 | 5867c88a | ths | int mode;
|
2802 | 5867c88a | ths | } ParallelCharDriver; |
2803 | 5867c88a | ths | |
2804 | 5867c88a | ths | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
2805 | 5867c88a | ths | { |
2806 | 5867c88a | ths | if (s->mode != mode) {
|
2807 | 5867c88a | ths | int m = mode;
|
2808 | 5867c88a | ths | if (ioctl(s->fd, PPSETMODE, &m) < 0) |
2809 | 5867c88a | ths | return 0; |
2810 | 5867c88a | ths | s->mode = mode; |
2811 | 5867c88a | ths | } |
2812 | 5867c88a | ths | return 1; |
2813 | 5867c88a | ths | } |
2814 | 5867c88a | ths | |
2815 | e57a8c0e | bellard | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
2816 | e57a8c0e | bellard | { |
2817 | 5867c88a | ths | ParallelCharDriver *drv = chr->opaque; |
2818 | 5867c88a | ths | int fd = drv->fd;
|
2819 | e57a8c0e | bellard | uint8_t b; |
2820 | e57a8c0e | bellard | |
2821 | e57a8c0e | bellard | switch(cmd) {
|
2822 | e57a8c0e | bellard | case CHR_IOCTL_PP_READ_DATA:
|
2823 | e57a8c0e | bellard | if (ioctl(fd, PPRDATA, &b) < 0) |
2824 | e57a8c0e | bellard | return -ENOTSUP;
|
2825 | e57a8c0e | bellard | *(uint8_t *)arg = b; |
2826 | e57a8c0e | bellard | break;
|
2827 | e57a8c0e | bellard | case CHR_IOCTL_PP_WRITE_DATA:
|
2828 | e57a8c0e | bellard | b = *(uint8_t *)arg; |
2829 | e57a8c0e | bellard | if (ioctl(fd, PPWDATA, &b) < 0) |
2830 | e57a8c0e | bellard | return -ENOTSUP;
|
2831 | e57a8c0e | bellard | break;
|
2832 | e57a8c0e | bellard | case CHR_IOCTL_PP_READ_CONTROL:
|
2833 | e57a8c0e | bellard | if (ioctl(fd, PPRCONTROL, &b) < 0) |
2834 | e57a8c0e | bellard | return -ENOTSUP;
|
2835 | 5867c88a | ths | /* Linux gives only the lowest bits, and no way to know data
|
2836 | 5867c88a | ths | direction! For better compatibility set the fixed upper
|
2837 | 5867c88a | ths | bits. */
|
2838 | 5867c88a | ths | *(uint8_t *)arg = b | 0xc0;
|
2839 | e57a8c0e | bellard | break;
|
2840 | e57a8c0e | bellard | case CHR_IOCTL_PP_WRITE_CONTROL:
|
2841 | e57a8c0e | bellard | b = *(uint8_t *)arg; |
2842 | e57a8c0e | bellard | if (ioctl(fd, PPWCONTROL, &b) < 0) |
2843 | e57a8c0e | bellard | return -ENOTSUP;
|
2844 | e57a8c0e | bellard | break;
|
2845 | e57a8c0e | bellard | case CHR_IOCTL_PP_READ_STATUS:
|
2846 | e57a8c0e | bellard | if (ioctl(fd, PPRSTATUS, &b) < 0) |
2847 | e57a8c0e | bellard | return -ENOTSUP;
|
2848 | e57a8c0e | bellard | *(uint8_t *)arg = b; |
2849 | e57a8c0e | bellard | break;
|
2850 | 563e3c6e | aurel32 | case CHR_IOCTL_PP_DATA_DIR:
|
2851 | 563e3c6e | aurel32 | if (ioctl(fd, PPDATADIR, (int *)arg) < 0) |
2852 | 563e3c6e | aurel32 | return -ENOTSUP;
|
2853 | 563e3c6e | aurel32 | break;
|
2854 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_READ_ADDR:
|
2855 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
2856 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2857 | 5867c88a | ths | int n = read(fd, parg->buffer, parg->count);
|
2858 | 5867c88a | ths | if (n != parg->count) {
|
2859 | 5867c88a | ths | return -EIO;
|
2860 | 5867c88a | ths | } |
2861 | 5867c88a | ths | } |
2862 | 5867c88a | ths | break;
|
2863 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_READ:
|
2864 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
2865 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2866 | 5867c88a | ths | int n = read(fd, parg->buffer, parg->count);
|
2867 | 5867c88a | ths | if (n != parg->count) {
|
2868 | 5867c88a | ths | return -EIO;
|
2869 | 5867c88a | ths | } |
2870 | 5867c88a | ths | } |
2871 | 5867c88a | ths | break;
|
2872 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_WRITE_ADDR:
|
2873 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
2874 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2875 | 5867c88a | ths | int n = write(fd, parg->buffer, parg->count);
|
2876 | 5867c88a | ths | if (n != parg->count) {
|
2877 | 5867c88a | ths | return -EIO;
|
2878 | 5867c88a | ths | } |
2879 | 5867c88a | ths | } |
2880 | 5867c88a | ths | break;
|
2881 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_WRITE:
|
2882 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
2883 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2884 | 5867c88a | ths | int n = write(fd, parg->buffer, parg->count);
|
2885 | 5867c88a | ths | if (n != parg->count) {
|
2886 | 5867c88a | ths | return -EIO;
|
2887 | 5867c88a | ths | } |
2888 | 5867c88a | ths | } |
2889 | 5867c88a | ths | break;
|
2890 | e57a8c0e | bellard | default:
|
2891 | e57a8c0e | bellard | return -ENOTSUP;
|
2892 | e57a8c0e | bellard | } |
2893 | e57a8c0e | bellard | return 0; |
2894 | e57a8c0e | bellard | } |
2895 | e57a8c0e | bellard | |
2896 | 5867c88a | ths | static void pp_close(CharDriverState *chr) |
2897 | 5867c88a | ths | { |
2898 | 5867c88a | ths | ParallelCharDriver *drv = chr->opaque; |
2899 | 5867c88a | ths | int fd = drv->fd;
|
2900 | 5867c88a | ths | |
2901 | 5867c88a | ths | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
2902 | 5867c88a | ths | ioctl(fd, PPRELEASE); |
2903 | 5867c88a | ths | close(fd); |
2904 | 5867c88a | ths | qemu_free(drv); |
2905 | 5867c88a | ths | } |
2906 | 5867c88a | ths | |
2907 | 52f61fde | ths | static CharDriverState *qemu_chr_open_pp(const char *filename) |
2908 | e57a8c0e | bellard | { |
2909 | e57a8c0e | bellard | CharDriverState *chr; |
2910 | 5867c88a | ths | ParallelCharDriver *drv; |
2911 | e57a8c0e | bellard | int fd;
|
2912 | e57a8c0e | bellard | |
2913 | aeb30be6 | balrog | TFR(fd = open(filename, O_RDWR)); |
2914 | e57a8c0e | bellard | if (fd < 0) |
2915 | e57a8c0e | bellard | return NULL; |
2916 | e57a8c0e | bellard | |
2917 | e57a8c0e | bellard | if (ioctl(fd, PPCLAIM) < 0) { |
2918 | e57a8c0e | bellard | close(fd); |
2919 | e57a8c0e | bellard | return NULL; |
2920 | e57a8c0e | bellard | } |
2921 | e57a8c0e | bellard | |
2922 | 5867c88a | ths | drv = qemu_mallocz(sizeof(ParallelCharDriver));
|
2923 | 5867c88a | ths | if (!drv) {
|
2924 | 5867c88a | ths | close(fd); |
2925 | 5867c88a | ths | return NULL; |
2926 | 5867c88a | ths | } |
2927 | 5867c88a | ths | drv->fd = fd; |
2928 | 5867c88a | ths | drv->mode = IEEE1284_MODE_COMPAT; |
2929 | 5867c88a | ths | |
2930 | e57a8c0e | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
2931 | e57a8c0e | bellard | if (!chr) {
|
2932 | 5867c88a | ths | qemu_free(drv); |
2933 | e57a8c0e | bellard | close(fd); |
2934 | e57a8c0e | bellard | return NULL; |
2935 | e57a8c0e | bellard | } |
2936 | e57a8c0e | bellard | chr->chr_write = null_chr_write; |
2937 | e57a8c0e | bellard | chr->chr_ioctl = pp_ioctl; |
2938 | 5867c88a | ths | chr->chr_close = pp_close; |
2939 | 5867c88a | ths | chr->opaque = drv; |
2940 | 86e94dea | ths | |
2941 | 86e94dea | ths | qemu_chr_reset(chr); |
2942 | 86e94dea | ths | |
2943 | f8d179e3 | bellard | return chr;
|
2944 | f8d179e3 | bellard | } |
2945 | aec62507 | ths | #endif /* __linux__ */ |
2946 | f8d179e3 | bellard | |
2947 | aec62507 | ths | #else /* _WIN32 */ |
2948 | 82c643ff | bellard | |
2949 | f331110f | bellard | typedef struct { |
2950 | f331110f | bellard | int max_size;
|
2951 | f331110f | bellard | HANDLE hcom, hrecv, hsend; |
2952 | f331110f | bellard | OVERLAPPED orecv, osend; |
2953 | f331110f | bellard | BOOL fpipe; |
2954 | f331110f | bellard | DWORD len; |
2955 | f331110f | bellard | } WinCharState; |
2956 | f331110f | bellard | |
2957 | f331110f | bellard | #define NSENDBUF 2048 |
2958 | f331110f | bellard | #define NRECVBUF 2048 |
2959 | f331110f | bellard | #define MAXCONNECT 1 |
2960 | f331110f | bellard | #define NTIMEOUT 5000 |
2961 | f331110f | bellard | |
2962 | f331110f | bellard | static int win_chr_poll(void *opaque); |
2963 | f331110f | bellard | static int win_chr_pipe_poll(void *opaque); |
2964 | f331110f | bellard | |
2965 | 087f4ae0 | ths | static void win_chr_close(CharDriverState *chr) |
2966 | f331110f | bellard | { |
2967 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2968 | 087f4ae0 | ths | |
2969 | f331110f | bellard | if (s->hsend) {
|
2970 | f331110f | bellard | CloseHandle(s->hsend); |
2971 | f331110f | bellard | s->hsend = NULL;
|
2972 | f331110f | bellard | } |
2973 | f331110f | bellard | if (s->hrecv) {
|
2974 | f331110f | bellard | CloseHandle(s->hrecv); |
2975 | f331110f | bellard | s->hrecv = NULL;
|
2976 | f331110f | bellard | } |
2977 | f331110f | bellard | if (s->hcom) {
|
2978 | f331110f | bellard | CloseHandle(s->hcom); |
2979 | f331110f | bellard | s->hcom = NULL;
|
2980 | f331110f | bellard | } |
2981 | f331110f | bellard | if (s->fpipe)
|
2982 | 087f4ae0 | ths | qemu_del_polling_cb(win_chr_pipe_poll, chr); |
2983 | f331110f | bellard | else
|
2984 | 087f4ae0 | ths | qemu_del_polling_cb(win_chr_poll, chr); |
2985 | f331110f | bellard | } |
2986 | f331110f | bellard | |
2987 | 087f4ae0 | ths | static int win_chr_init(CharDriverState *chr, const char *filename) |
2988 | f331110f | bellard | { |
2989 | f331110f | bellard | WinCharState *s = chr->opaque; |
2990 | f331110f | bellard | COMMCONFIG comcfg; |
2991 | f331110f | bellard | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
2992 | f331110f | bellard | COMSTAT comstat; |
2993 | f331110f | bellard | DWORD size; |
2994 | f331110f | bellard | DWORD err; |
2995 | 3b46e624 | ths | |
2996 | f331110f | bellard | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
2997 | f331110f | bellard | if (!s->hsend) {
|
2998 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
2999 | f331110f | bellard | goto fail;
|
3000 | f331110f | bellard | } |
3001 | f331110f | bellard | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
3002 | f331110f | bellard | if (!s->hrecv) {
|
3003 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
3004 | f331110f | bellard | goto fail;
|
3005 | f331110f | bellard | } |
3006 | f331110f | bellard | |
3007 | f331110f | bellard | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
3008 | f331110f | bellard | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
|
3009 | f331110f | bellard | if (s->hcom == INVALID_HANDLE_VALUE) {
|
3010 | f331110f | bellard | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
|
3011 | f331110f | bellard | s->hcom = NULL;
|
3012 | f331110f | bellard | goto fail;
|
3013 | f331110f | bellard | } |
3014 | 3b46e624 | ths | |
3015 | f331110f | bellard | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
|
3016 | f331110f | bellard | fprintf(stderr, "Failed SetupComm\n");
|
3017 | f331110f | bellard | goto fail;
|
3018 | f331110f | bellard | } |
3019 | 3b46e624 | ths | |
3020 | f331110f | bellard | ZeroMemory(&comcfg, sizeof(COMMCONFIG));
|
3021 | f331110f | bellard | size = sizeof(COMMCONFIG);
|
3022 | f331110f | bellard | GetDefaultCommConfig(filename, &comcfg, &size); |
3023 | f331110f | bellard | comcfg.dcb.DCBlength = sizeof(DCB);
|
3024 | f331110f | bellard | CommConfigDialog(filename, NULL, &comcfg);
|
3025 | f331110f | bellard | |
3026 | f331110f | bellard | if (!SetCommState(s->hcom, &comcfg.dcb)) {
|
3027 | f331110f | bellard | fprintf(stderr, "Failed SetCommState\n");
|
3028 | f331110f | bellard | goto fail;
|
3029 | f331110f | bellard | } |
3030 | f331110f | bellard | |
3031 | f331110f | bellard | if (!SetCommMask(s->hcom, EV_ERR)) {
|
3032 | f331110f | bellard | fprintf(stderr, "Failed SetCommMask\n");
|
3033 | f331110f | bellard | goto fail;
|
3034 | f331110f | bellard | } |
3035 | f331110f | bellard | |
3036 | f331110f | bellard | cto.ReadIntervalTimeout = MAXDWORD; |
3037 | f331110f | bellard | if (!SetCommTimeouts(s->hcom, &cto)) {
|
3038 | f331110f | bellard | fprintf(stderr, "Failed SetCommTimeouts\n");
|
3039 | f331110f | bellard | goto fail;
|
3040 | f331110f | bellard | } |
3041 | 3b46e624 | ths | |
3042 | f331110f | bellard | if (!ClearCommError(s->hcom, &err, &comstat)) {
|
3043 | f331110f | bellard | fprintf(stderr, "Failed ClearCommError\n");
|
3044 | f331110f | bellard | goto fail;
|
3045 | f331110f | bellard | } |
3046 | 087f4ae0 | ths | qemu_add_polling_cb(win_chr_poll, chr); |
3047 | f331110f | bellard | return 0; |
3048 | f331110f | bellard | |
3049 | f331110f | bellard | fail:
|
3050 | 087f4ae0 | ths | win_chr_close(chr); |
3051 | f331110f | bellard | return -1; |
3052 | f331110f | bellard | } |
3053 | f331110f | bellard | |
3054 | f331110f | bellard | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
3055 | f331110f | bellard | { |
3056 | f331110f | bellard | WinCharState *s = chr->opaque; |
3057 | f331110f | bellard | DWORD len, ret, size, err; |
3058 | f331110f | bellard | |
3059 | f331110f | bellard | len = len1; |
3060 | f331110f | bellard | ZeroMemory(&s->osend, sizeof(s->osend));
|
3061 | f331110f | bellard | s->osend.hEvent = s->hsend; |
3062 | f331110f | bellard | while (len > 0) { |
3063 | f331110f | bellard | if (s->hsend)
|
3064 | f331110f | bellard | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
3065 | f331110f | bellard | else
|
3066 | f331110f | bellard | ret = WriteFile(s->hcom, buf, len, &size, NULL);
|
3067 | f331110f | bellard | if (!ret) {
|
3068 | f331110f | bellard | err = GetLastError(); |
3069 | f331110f | bellard | if (err == ERROR_IO_PENDING) {
|
3070 | f331110f | bellard | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
3071 | f331110f | bellard | if (ret) {
|
3072 | f331110f | bellard | buf += size; |
3073 | f331110f | bellard | len -= size; |
3074 | f331110f | bellard | } else {
|
3075 | f331110f | bellard | break;
|
3076 | f331110f | bellard | } |
3077 | f331110f | bellard | } else {
|
3078 | f331110f | bellard | break;
|
3079 | f331110f | bellard | } |
3080 | f331110f | bellard | } else {
|
3081 | f331110f | bellard | buf += size; |
3082 | f331110f | bellard | len -= size; |
3083 | f331110f | bellard | } |
3084 | f331110f | bellard | } |
3085 | f331110f | bellard | return len1 - len;
|
3086 | f331110f | bellard | } |
3087 | f331110f | bellard | |
3088 | 087f4ae0 | ths | static int win_chr_read_poll(CharDriverState *chr) |
3089 | f331110f | bellard | { |
3090 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3091 | 087f4ae0 | ths | |
3092 | 087f4ae0 | ths | s->max_size = qemu_chr_can_read(chr); |
3093 | f331110f | bellard | return s->max_size;
|
3094 | f331110f | bellard | } |
3095 | e5b0bc44 | pbrook | |
3096 | 087f4ae0 | ths | static void win_chr_readfile(CharDriverState *chr) |
3097 | f331110f | bellard | { |
3098 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3099 | f331110f | bellard | int ret, err;
|
3100 | f331110f | bellard | uint8_t buf[1024];
|
3101 | f331110f | bellard | DWORD size; |
3102 | 3b46e624 | ths | |
3103 | f331110f | bellard | ZeroMemory(&s->orecv, sizeof(s->orecv));
|
3104 | f331110f | bellard | s->orecv.hEvent = s->hrecv; |
3105 | f331110f | bellard | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
3106 | f331110f | bellard | if (!ret) {
|
3107 | f331110f | bellard | err = GetLastError(); |
3108 | f331110f | bellard | if (err == ERROR_IO_PENDING) {
|
3109 | f331110f | bellard | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
3110 | f331110f | bellard | } |
3111 | f331110f | bellard | } |
3112 | f331110f | bellard | |
3113 | f331110f | bellard | if (size > 0) { |
3114 | 087f4ae0 | ths | qemu_chr_read(chr, buf, size); |
3115 | f331110f | bellard | } |
3116 | f331110f | bellard | } |
3117 | f331110f | bellard | |
3118 | 087f4ae0 | ths | static void win_chr_read(CharDriverState *chr) |
3119 | f331110f | bellard | { |
3120 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3121 | 087f4ae0 | ths | |
3122 | f331110f | bellard | if (s->len > s->max_size)
|
3123 | f331110f | bellard | s->len = s->max_size; |
3124 | f331110f | bellard | if (s->len == 0) |
3125 | f331110f | bellard | return;
|
3126 | 3b46e624 | ths | |
3127 | 087f4ae0 | ths | win_chr_readfile(chr); |
3128 | f331110f | bellard | } |
3129 | f331110f | bellard | |
3130 | f331110f | bellard | static int win_chr_poll(void *opaque) |
3131 | f331110f | bellard | { |
3132 | 087f4ae0 | ths | CharDriverState *chr = opaque; |
3133 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3134 | f331110f | bellard | COMSTAT status; |
3135 | f331110f | bellard | DWORD comerr; |
3136 | 3b46e624 | ths | |
3137 | f331110f | bellard | ClearCommError(s->hcom, &comerr, &status); |
3138 | f331110f | bellard | if (status.cbInQue > 0) { |
3139 | f331110f | bellard | s->len = status.cbInQue; |
3140 | 087f4ae0 | ths | win_chr_read_poll(chr); |
3141 | 087f4ae0 | ths | win_chr_read(chr); |
3142 | f331110f | bellard | return 1; |
3143 | f331110f | bellard | } |
3144 | f331110f | bellard | return 0; |
3145 | f331110f | bellard | } |
3146 | f331110f | bellard | |
3147 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win(const char *filename) |
3148 | f331110f | bellard | { |
3149 | f331110f | bellard | CharDriverState *chr; |
3150 | f331110f | bellard | WinCharState *s; |
3151 | 3b46e624 | ths | |
3152 | f331110f | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3153 | f331110f | bellard | if (!chr)
|
3154 | f331110f | bellard | return NULL; |
3155 | f331110f | bellard | s = qemu_mallocz(sizeof(WinCharState));
|
3156 | f331110f | bellard | if (!s) {
|
3157 | f331110f | bellard | free(chr); |
3158 | f331110f | bellard | return NULL; |
3159 | f331110f | bellard | } |
3160 | f331110f | bellard | chr->opaque = s; |
3161 | f331110f | bellard | chr->chr_write = win_chr_write; |
3162 | f331110f | bellard | chr->chr_close = win_chr_close; |
3163 | f331110f | bellard | |
3164 | 087f4ae0 | ths | if (win_chr_init(chr, filename) < 0) { |
3165 | f331110f | bellard | free(s); |
3166 | f331110f | bellard | free(chr); |
3167 | f331110f | bellard | return NULL; |
3168 | f331110f | bellard | } |
3169 | 86e94dea | ths | qemu_chr_reset(chr); |
3170 | f331110f | bellard | return chr;
|
3171 | f331110f | bellard | } |
3172 | f331110f | bellard | |
3173 | f331110f | bellard | static int win_chr_pipe_poll(void *opaque) |
3174 | f331110f | bellard | { |
3175 | 087f4ae0 | ths | CharDriverState *chr = opaque; |
3176 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3177 | f331110f | bellard | DWORD size; |
3178 | f331110f | bellard | |
3179 | f331110f | bellard | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
3180 | f331110f | bellard | if (size > 0) { |
3181 | f331110f | bellard | s->len = size; |
3182 | 087f4ae0 | ths | win_chr_read_poll(chr); |
3183 | 087f4ae0 | ths | win_chr_read(chr); |
3184 | f331110f | bellard | return 1; |
3185 | f331110f | bellard | } |
3186 | f331110f | bellard | return 0; |
3187 | f331110f | bellard | } |
3188 | f331110f | bellard | |
3189 | 087f4ae0 | ths | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) |
3190 | f331110f | bellard | { |
3191 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3192 | f331110f | bellard | OVERLAPPED ov; |
3193 | f331110f | bellard | int ret;
|
3194 | f331110f | bellard | DWORD size; |
3195 | f331110f | bellard | char openname[256]; |
3196 | 3b46e624 | ths | |
3197 | f331110f | bellard | s->fpipe = TRUE; |
3198 | f331110f | bellard | |
3199 | f331110f | bellard | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
3200 | f331110f | bellard | if (!s->hsend) {
|
3201 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
3202 | f331110f | bellard | goto fail;
|
3203 | f331110f | bellard | } |
3204 | f331110f | bellard | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
3205 | f331110f | bellard | if (!s->hrecv) {
|
3206 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
3207 | f331110f | bellard | goto fail;
|
3208 | f331110f | bellard | } |
3209 | 3b46e624 | ths | |
3210 | f331110f | bellard | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
3211 | f331110f | bellard | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
3212 | f331110f | bellard | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
3213 | f331110f | bellard | PIPE_WAIT, |
3214 | f331110f | bellard | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
|
3215 | f331110f | bellard | if (s->hcom == INVALID_HANDLE_VALUE) {
|
3216 | f331110f | bellard | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
|
3217 | f331110f | bellard | s->hcom = NULL;
|
3218 | f331110f | bellard | goto fail;
|
3219 | f331110f | bellard | } |
3220 | f331110f | bellard | |
3221 | f331110f | bellard | ZeroMemory(&ov, sizeof(ov));
|
3222 | f331110f | bellard | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
3223 | f331110f | bellard | ret = ConnectNamedPipe(s->hcom, &ov); |
3224 | f331110f | bellard | if (ret) {
|
3225 | f331110f | bellard | fprintf(stderr, "Failed ConnectNamedPipe\n");
|
3226 | f331110f | bellard | goto fail;
|
3227 | f331110f | bellard | } |
3228 | f331110f | bellard | |
3229 | f331110f | bellard | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
3230 | f331110f | bellard | if (!ret) {
|
3231 | f331110f | bellard | fprintf(stderr, "Failed GetOverlappedResult\n");
|
3232 | f331110f | bellard | if (ov.hEvent) {
|
3233 | f331110f | bellard | CloseHandle(ov.hEvent); |
3234 | f331110f | bellard | ov.hEvent = NULL;
|
3235 | f331110f | bellard | } |
3236 | f331110f | bellard | goto fail;
|
3237 | f331110f | bellard | } |
3238 | f331110f | bellard | |
3239 | f331110f | bellard | if (ov.hEvent) {
|
3240 | f331110f | bellard | CloseHandle(ov.hEvent); |
3241 | f331110f | bellard | ov.hEvent = NULL;
|
3242 | f331110f | bellard | } |
3243 | 087f4ae0 | ths | qemu_add_polling_cb(win_chr_pipe_poll, chr); |
3244 | f331110f | bellard | return 0; |
3245 | f331110f | bellard | |
3246 | f331110f | bellard | fail:
|
3247 | 087f4ae0 | ths | win_chr_close(chr); |
3248 | f331110f | bellard | return -1; |
3249 | f331110f | bellard | } |
3250 | f331110f | bellard | |
3251 | f331110f | bellard | |
3252 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win_pipe(const char *filename) |
3253 | f331110f | bellard | { |
3254 | f331110f | bellard | CharDriverState *chr; |
3255 | f331110f | bellard | WinCharState *s; |
3256 | f331110f | bellard | |
3257 | f331110f | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3258 | f331110f | bellard | if (!chr)
|
3259 | f331110f | bellard | return NULL; |
3260 | f331110f | bellard | s = qemu_mallocz(sizeof(WinCharState));
|
3261 | f331110f | bellard | if (!s) {
|
3262 | f331110f | bellard | free(chr); |
3263 | f331110f | bellard | return NULL; |
3264 | f331110f | bellard | } |
3265 | f331110f | bellard | chr->opaque = s; |
3266 | f331110f | bellard | chr->chr_write = win_chr_write; |
3267 | f331110f | bellard | chr->chr_close = win_chr_close; |
3268 | 3b46e624 | ths | |
3269 | 087f4ae0 | ths | if (win_chr_pipe_init(chr, filename) < 0) { |
3270 | f331110f | bellard | free(s); |
3271 | f331110f | bellard | free(chr); |
3272 | f331110f | bellard | return NULL; |
3273 | f331110f | bellard | } |
3274 | 86e94dea | ths | qemu_chr_reset(chr); |
3275 | f331110f | bellard | return chr;
|
3276 | f331110f | bellard | } |
3277 | f331110f | bellard | |
3278 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
|
3279 | f331110f | bellard | { |
3280 | f331110f | bellard | CharDriverState *chr; |
3281 | f331110f | bellard | WinCharState *s; |
3282 | f331110f | bellard | |
3283 | f331110f | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3284 | f331110f | bellard | if (!chr)
|
3285 | f331110f | bellard | return NULL; |
3286 | f331110f | bellard | s = qemu_mallocz(sizeof(WinCharState));
|
3287 | f331110f | bellard | if (!s) {
|
3288 | f331110f | bellard | free(chr); |
3289 | f331110f | bellard | return NULL; |
3290 | f331110f | bellard | } |
3291 | f331110f | bellard | s->hcom = fd_out; |
3292 | f331110f | bellard | chr->opaque = s; |
3293 | f331110f | bellard | chr->chr_write = win_chr_write; |
3294 | 86e94dea | ths | qemu_chr_reset(chr); |
3295 | f331110f | bellard | return chr;
|
3296 | f331110f | bellard | } |
3297 | 72d46479 | ths | |
3298 | 72d46479 | ths | static CharDriverState *qemu_chr_open_win_con(const char *filename) |
3299 | 72d46479 | ths | { |
3300 | 72d46479 | ths | return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
|
3301 | 72d46479 | ths | } |
3302 | 72d46479 | ths | |
3303 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win_file_out(const char *file_out) |
3304 | f331110f | bellard | { |
3305 | f331110f | bellard | HANDLE fd_out; |
3306 | 3b46e624 | ths | |
3307 | f331110f | bellard | fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
|
3308 | f331110f | bellard | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
3309 | f331110f | bellard | if (fd_out == INVALID_HANDLE_VALUE)
|
3310 | f331110f | bellard | return NULL; |
3311 | f331110f | bellard | |
3312 | f331110f | bellard | return qemu_chr_open_win_file(fd_out);
|
3313 | f331110f | bellard | } |
3314 | aec62507 | ths | #endif /* !_WIN32 */ |
3315 | f331110f | bellard | |
3316 | 0bab00f3 | bellard | /***********************************************************/
|
3317 | 0bab00f3 | bellard | /* UDP Net console */
|
3318 | 0bab00f3 | bellard | |
3319 | 0bab00f3 | bellard | typedef struct { |
3320 | 0bab00f3 | bellard | int fd;
|
3321 | 0bab00f3 | bellard | struct sockaddr_in daddr;
|
3322 | 60fe76f3 | ths | uint8_t buf[1024];
|
3323 | 0bab00f3 | bellard | int bufcnt;
|
3324 | 0bab00f3 | bellard | int bufptr;
|
3325 | 0bab00f3 | bellard | int max_size;
|
3326 | 0bab00f3 | bellard | } NetCharDriver; |
3327 | 0bab00f3 | bellard | |
3328 | 0bab00f3 | bellard | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
3329 | 0bab00f3 | bellard | { |
3330 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3331 | 0bab00f3 | bellard | |
3332 | 0bab00f3 | bellard | return sendto(s->fd, buf, len, 0, |
3333 | 0bab00f3 | bellard | (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in)); |
3334 | 0bab00f3 | bellard | } |
3335 | 0bab00f3 | bellard | |
3336 | 0bab00f3 | bellard | static int udp_chr_read_poll(void *opaque) |
3337 | 0bab00f3 | bellard | { |
3338 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3339 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3340 | 0bab00f3 | bellard | |
3341 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3342 | 0bab00f3 | bellard | |
3343 | 0bab00f3 | bellard | /* If there were any stray characters in the queue process them
|
3344 | 0bab00f3 | bellard | * first
|
3345 | 0bab00f3 | bellard | */
|
3346 | 0bab00f3 | bellard | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
3347 | e5b0bc44 | pbrook | qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
3348 | 0bab00f3 | bellard | s->bufptr++; |
3349 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3350 | 0bab00f3 | bellard | } |
3351 | 0bab00f3 | bellard | return s->max_size;
|
3352 | 0bab00f3 | bellard | } |
3353 | 0bab00f3 | bellard | |
3354 | 0bab00f3 | bellard | static void udp_chr_read(void *opaque) |
3355 | 0bab00f3 | bellard | { |
3356 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3357 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3358 | 0bab00f3 | bellard | |
3359 | 0bab00f3 | bellard | if (s->max_size == 0) |
3360 | 0bab00f3 | bellard | return;
|
3361 | 0bab00f3 | bellard | s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0); |
3362 | 0bab00f3 | bellard | s->bufptr = s->bufcnt; |
3363 | 0bab00f3 | bellard | if (s->bufcnt <= 0) |
3364 | 0bab00f3 | bellard | return;
|
3365 | 0bab00f3 | bellard | |
3366 | 0bab00f3 | bellard | s->bufptr = 0;
|
3367 | 0bab00f3 | bellard | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
3368 | e5b0bc44 | pbrook | qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
3369 | 0bab00f3 | bellard | s->bufptr++; |
3370 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3371 | 0bab00f3 | bellard | } |
3372 | 0bab00f3 | bellard | } |
3373 | 0bab00f3 | bellard | |
3374 | e5b0bc44 | pbrook | static void udp_chr_update_read_handler(CharDriverState *chr) |
3375 | 0bab00f3 | bellard | { |
3376 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3377 | 0bab00f3 | bellard | |
3378 | 0bab00f3 | bellard | if (s->fd >= 0) { |
3379 | 0bab00f3 | bellard | qemu_set_fd_handler2(s->fd, udp_chr_read_poll, |
3380 | 0bab00f3 | bellard | udp_chr_read, NULL, chr);
|
3381 | 0bab00f3 | bellard | } |
3382 | 0bab00f3 | bellard | } |
3383 | 0bab00f3 | bellard | |
3384 | 0bab00f3 | bellard | int parse_host_port(struct sockaddr_in *saddr, const char *str); |
3385 | 52f61fde | ths | #ifndef _WIN32
|
3386 | 52f61fde | ths | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str); |
3387 | 52f61fde | ths | #endif
|
3388 | 951f1351 | bellard | int parse_host_src_port(struct sockaddr_in *haddr, |
3389 | 951f1351 | bellard | struct sockaddr_in *saddr,
|
3390 | 951f1351 | bellard | const char *str); |
3391 | 0bab00f3 | bellard | |
3392 | 52f61fde | ths | static CharDriverState *qemu_chr_open_udp(const char *def) |
3393 | 0bab00f3 | bellard | { |
3394 | 0bab00f3 | bellard | CharDriverState *chr = NULL;
|
3395 | 0bab00f3 | bellard | NetCharDriver *s = NULL;
|
3396 | 0bab00f3 | bellard | int fd = -1; |
3397 | 951f1351 | bellard | struct sockaddr_in saddr;
|
3398 | 0bab00f3 | bellard | |
3399 | 0bab00f3 | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3400 | 0bab00f3 | bellard | if (!chr)
|
3401 | 0bab00f3 | bellard | goto return_err;
|
3402 | 0bab00f3 | bellard | s = qemu_mallocz(sizeof(NetCharDriver));
|
3403 | 0bab00f3 | bellard | if (!s)
|
3404 | 0bab00f3 | bellard | goto return_err;
|
3405 | 0bab00f3 | bellard | |
3406 | 0bab00f3 | bellard | fd = socket(PF_INET, SOCK_DGRAM, 0);
|
3407 | 0bab00f3 | bellard | if (fd < 0) { |
3408 | 0bab00f3 | bellard | perror("socket(PF_INET, SOCK_DGRAM)");
|
3409 | 0bab00f3 | bellard | goto return_err;
|
3410 | 0bab00f3 | bellard | } |
3411 | 0bab00f3 | bellard | |
3412 | 951f1351 | bellard | if (parse_host_src_port(&s->daddr, &saddr, def) < 0) { |
3413 | 951f1351 | bellard | printf("Could not parse: %s\n", def);
|
3414 | 951f1351 | bellard | goto return_err;
|
3415 | 0bab00f3 | bellard | } |
3416 | 0bab00f3 | bellard | |
3417 | 951f1351 | bellard | if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) |
3418 | 0bab00f3 | bellard | { |
3419 | 0bab00f3 | bellard | perror("bind");
|
3420 | 0bab00f3 | bellard | goto return_err;
|
3421 | 0bab00f3 | bellard | } |
3422 | 0bab00f3 | bellard | |
3423 | 0bab00f3 | bellard | s->fd = fd; |
3424 | 0bab00f3 | bellard | s->bufcnt = 0;
|
3425 | 0bab00f3 | bellard | s->bufptr = 0;
|
3426 | 0bab00f3 | bellard | chr->opaque = s; |
3427 | 0bab00f3 | bellard | chr->chr_write = udp_chr_write; |
3428 | e5b0bc44 | pbrook | chr->chr_update_read_handler = udp_chr_update_read_handler; |
3429 | 0bab00f3 | bellard | return chr;
|
3430 | 0bab00f3 | bellard | |
3431 | 0bab00f3 | bellard | return_err:
|
3432 | 0bab00f3 | bellard | if (chr)
|
3433 | 0bab00f3 | bellard | free(chr); |
3434 | 0bab00f3 | bellard | if (s)
|
3435 | 0bab00f3 | bellard | free(s); |
3436 | 0bab00f3 | bellard | if (fd >= 0) |
3437 | 0bab00f3 | bellard | closesocket(fd); |
3438 | 0bab00f3 | bellard | return NULL; |
3439 | 0bab00f3 | bellard | } |
3440 | 0bab00f3 | bellard | |
3441 | 0bab00f3 | bellard | /***********************************************************/
|
3442 | 0bab00f3 | bellard | /* TCP Net console */
|
3443 | 0bab00f3 | bellard | |
3444 | 0bab00f3 | bellard | typedef struct { |
3445 | 0bab00f3 | bellard | int fd, listen_fd;
|
3446 | 0bab00f3 | bellard | int connected;
|
3447 | 0bab00f3 | bellard | int max_size;
|
3448 | 951f1351 | bellard | int do_telnetopt;
|
3449 | e5b0bc44 | pbrook | int do_nodelay;
|
3450 | ffd843bc | ths | int is_unix;
|
3451 | 0bab00f3 | bellard | } TCPCharDriver; |
3452 | 0bab00f3 | bellard | |
3453 | 0bab00f3 | bellard | static void tcp_chr_accept(void *opaque); |
3454 | 0bab00f3 | bellard | |
3455 | 0bab00f3 | bellard | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
3456 | 0bab00f3 | bellard | { |
3457 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3458 | 0bab00f3 | bellard | if (s->connected) {
|
3459 | 0bab00f3 | bellard | return send_all(s->fd, buf, len);
|
3460 | 0bab00f3 | bellard | } else {
|
3461 | 0bab00f3 | bellard | /* XXX: indicate an error ? */
|
3462 | 0bab00f3 | bellard | return len;
|
3463 | 0bab00f3 | bellard | } |
3464 | 0bab00f3 | bellard | } |
3465 | 0bab00f3 | bellard | |
3466 | 0bab00f3 | bellard | static int tcp_chr_read_poll(void *opaque) |
3467 | 0bab00f3 | bellard | { |
3468 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3469 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3470 | 0bab00f3 | bellard | if (!s->connected)
|
3471 | 0bab00f3 | bellard | return 0; |
3472 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3473 | 0bab00f3 | bellard | return s->max_size;
|
3474 | 0bab00f3 | bellard | } |
3475 | 0bab00f3 | bellard | |
3476 | 951f1351 | bellard | #define IAC 255 |
3477 | 951f1351 | bellard | #define IAC_BREAK 243 |
3478 | 951f1351 | bellard | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
3479 | 951f1351 | bellard | TCPCharDriver *s, |
3480 | 60fe76f3 | ths | uint8_t *buf, int *size)
|
3481 | 951f1351 | bellard | { |
3482 | 951f1351 | bellard | /* Handle any telnet client's basic IAC options to satisfy char by
|
3483 | 951f1351 | bellard | * char mode with no echo. All IAC options will be removed from
|
3484 | 951f1351 | bellard | * the buf and the do_telnetopt variable will be used to track the
|
3485 | 951f1351 | bellard | * state of the width of the IAC information.
|
3486 | 951f1351 | bellard | *
|
3487 | 951f1351 | bellard | * IAC commands come in sets of 3 bytes with the exception of the
|
3488 | 951f1351 | bellard | * "IAC BREAK" command and the double IAC.
|
3489 | 951f1351 | bellard | */
|
3490 | 951f1351 | bellard | |
3491 | 951f1351 | bellard | int i;
|
3492 | 951f1351 | bellard | int j = 0; |
3493 | 951f1351 | bellard | |
3494 | 951f1351 | bellard | for (i = 0; i < *size; i++) { |
3495 | 951f1351 | bellard | if (s->do_telnetopt > 1) { |
3496 | 951f1351 | bellard | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
3497 | 951f1351 | bellard | /* Double IAC means send an IAC */
|
3498 | 951f1351 | bellard | if (j != i)
|
3499 | 951f1351 | bellard | buf[j] = buf[i]; |
3500 | 951f1351 | bellard | j++; |
3501 | 951f1351 | bellard | s->do_telnetopt = 1;
|
3502 | 951f1351 | bellard | } else {
|
3503 | 951f1351 | bellard | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
3504 | 951f1351 | bellard | /* Handle IAC break commands by sending a serial break */
|
3505 | e5b0bc44 | pbrook | qemu_chr_event(chr, CHR_EVENT_BREAK); |
3506 | 951f1351 | bellard | s->do_telnetopt++; |
3507 | 951f1351 | bellard | } |
3508 | 951f1351 | bellard | s->do_telnetopt++; |
3509 | 951f1351 | bellard | } |
3510 | 951f1351 | bellard | if (s->do_telnetopt >= 4) { |
3511 | 951f1351 | bellard | s->do_telnetopt = 1;
|
3512 | 951f1351 | bellard | } |
3513 | 951f1351 | bellard | } else {
|
3514 | 951f1351 | bellard | if ((unsigned char)buf[i] == IAC) { |
3515 | 951f1351 | bellard | s->do_telnetopt = 2;
|
3516 | 951f1351 | bellard | } else {
|
3517 | 951f1351 | bellard | if (j != i)
|
3518 | 951f1351 | bellard | buf[j] = buf[i]; |
3519 | 951f1351 | bellard | j++; |
3520 | 951f1351 | bellard | } |
3521 | 951f1351 | bellard | } |
3522 | 951f1351 | bellard | } |
3523 | 951f1351 | bellard | *size = j; |
3524 | 951f1351 | bellard | } |
3525 | 951f1351 | bellard | |
3526 | 0bab00f3 | bellard | static void tcp_chr_read(void *opaque) |
3527 | 0bab00f3 | bellard | { |
3528 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3529 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3530 | 0bab00f3 | bellard | uint8_t buf[1024];
|
3531 | 0bab00f3 | bellard | int len, size;
|
3532 | 0bab00f3 | bellard | |
3533 | 0bab00f3 | bellard | if (!s->connected || s->max_size <= 0) |
3534 | 0bab00f3 | bellard | return;
|
3535 | 0bab00f3 | bellard | len = sizeof(buf);
|
3536 | 0bab00f3 | bellard | if (len > s->max_size)
|
3537 | 0bab00f3 | bellard | len = s->max_size; |
3538 | 0bab00f3 | bellard | size = recv(s->fd, buf, len, 0);
|
3539 | 0bab00f3 | bellard | if (size == 0) { |
3540 | 0bab00f3 | bellard | /* connection closed */
|
3541 | 0bab00f3 | bellard | s->connected = 0;
|
3542 | 0bab00f3 | bellard | if (s->listen_fd >= 0) { |
3543 | 0bab00f3 | bellard | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
3544 | 0bab00f3 | bellard | } |
3545 | 0bab00f3 | bellard | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
3546 | 0bab00f3 | bellard | closesocket(s->fd); |
3547 | 0bab00f3 | bellard | s->fd = -1;
|
3548 | 0bab00f3 | bellard | } else if (size > 0) { |
3549 | 951f1351 | bellard | if (s->do_telnetopt)
|
3550 | 951f1351 | bellard | tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
3551 | 951f1351 | bellard | if (size > 0) |
3552 | e5b0bc44 | pbrook | qemu_chr_read(chr, buf, size); |
3553 | 0bab00f3 | bellard | } |
3554 | 0bab00f3 | bellard | } |
3555 | 0bab00f3 | bellard | |
3556 | 0bab00f3 | bellard | static void tcp_chr_connect(void *opaque) |
3557 | 0bab00f3 | bellard | { |
3558 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3559 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3560 | 0bab00f3 | bellard | |
3561 | 0bab00f3 | bellard | s->connected = 1;
|
3562 | 0bab00f3 | bellard | qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, |
3563 | 0bab00f3 | bellard | tcp_chr_read, NULL, chr);
|
3564 | 86e94dea | ths | qemu_chr_reset(chr); |
3565 | 0bab00f3 | bellard | } |
3566 | 0bab00f3 | bellard | |
3567 | 951f1351 | bellard | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
3568 | 951f1351 | bellard | static void tcp_chr_telnet_init(int fd) |
3569 | 951f1351 | bellard | { |
3570 | 951f1351 | bellard | char buf[3]; |
3571 | 951f1351 | bellard | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
|
3572 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
3573 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3574 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
3575 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3576 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
3577 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3578 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
3579 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3580 | 951f1351 | bellard | } |
3581 | 951f1351 | bellard | |
3582 | f7499989 | pbrook | static void socket_set_nodelay(int fd) |
3583 | f7499989 | pbrook | { |
3584 | f7499989 | pbrook | int val = 1; |
3585 | f7499989 | pbrook | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
3586 | f7499989 | pbrook | } |
3587 | f7499989 | pbrook | |
3588 | 0bab00f3 | bellard | static void tcp_chr_accept(void *opaque) |
3589 | 0bab00f3 | bellard | { |
3590 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3591 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3592 | 0bab00f3 | bellard | struct sockaddr_in saddr;
|
3593 | ffd843bc | ths | #ifndef _WIN32
|
3594 | ffd843bc | ths | struct sockaddr_un uaddr;
|
3595 | ffd843bc | ths | #endif
|
3596 | ffd843bc | ths | struct sockaddr *addr;
|
3597 | 0bab00f3 | bellard | socklen_t len; |
3598 | 0bab00f3 | bellard | int fd;
|
3599 | 0bab00f3 | bellard | |
3600 | 0bab00f3 | bellard | for(;;) {
|
3601 | ffd843bc | ths | #ifndef _WIN32
|
3602 | ffd843bc | ths | if (s->is_unix) {
|
3603 | ffd843bc | ths | len = sizeof(uaddr);
|
3604 | ffd843bc | ths | addr = (struct sockaddr *)&uaddr;
|
3605 | ffd843bc | ths | } else
|
3606 | ffd843bc | ths | #endif
|
3607 | ffd843bc | ths | { |
3608 | ffd843bc | ths | len = sizeof(saddr);
|
3609 | ffd843bc | ths | addr = (struct sockaddr *)&saddr;
|
3610 | ffd843bc | ths | } |
3611 | ffd843bc | ths | fd = accept(s->listen_fd, addr, &len); |
3612 | 0bab00f3 | bellard | if (fd < 0 && errno != EINTR) { |
3613 | 0bab00f3 | bellard | return;
|
3614 | 0bab00f3 | bellard | } else if (fd >= 0) { |
3615 | 951f1351 | bellard | if (s->do_telnetopt)
|
3616 | 951f1351 | bellard | tcp_chr_telnet_init(fd); |
3617 | 0bab00f3 | bellard | break;
|
3618 | 0bab00f3 | bellard | } |
3619 | 0bab00f3 | bellard | } |
3620 | 0bab00f3 | bellard | socket_set_nonblock(fd); |
3621 | f7499989 | pbrook | if (s->do_nodelay)
|
3622 | f7499989 | pbrook | socket_set_nodelay(fd); |
3623 | 0bab00f3 | bellard | s->fd = fd; |
3624 | 0bab00f3 | bellard | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
3625 | 0bab00f3 | bellard | tcp_chr_connect(chr); |
3626 | 0bab00f3 | bellard | } |
3627 | 0bab00f3 | bellard | |
3628 | 0bab00f3 | bellard | static void tcp_chr_close(CharDriverState *chr) |
3629 | 0bab00f3 | bellard | { |
3630 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3631 | 0bab00f3 | bellard | if (s->fd >= 0) |
3632 | 0bab00f3 | bellard | closesocket(s->fd); |
3633 | 0bab00f3 | bellard | if (s->listen_fd >= 0) |
3634 | 0bab00f3 | bellard | closesocket(s->listen_fd); |
3635 | 0bab00f3 | bellard | qemu_free(s); |
3636 | 0bab00f3 | bellard | } |
3637 | 0bab00f3 | bellard | |
3638 | 5fafdf24 | ths | static CharDriverState *qemu_chr_open_tcp(const char *host_str, |
3639 | ffd843bc | ths | int is_telnet,
|
3640 | ffd843bc | ths | int is_unix)
|
3641 | 0bab00f3 | bellard | { |
3642 | 0bab00f3 | bellard | CharDriverState *chr = NULL;
|
3643 | 0bab00f3 | bellard | TCPCharDriver *s = NULL;
|
3644 | 0bab00f3 | bellard | int fd = -1, ret, err, val; |
3645 | 951f1351 | bellard | int is_listen = 0; |
3646 | 951f1351 | bellard | int is_waitconnect = 1; |
3647 | f7499989 | pbrook | int do_nodelay = 0; |
3648 | 951f1351 | bellard | const char *ptr; |
3649 | 0bab00f3 | bellard | struct sockaddr_in saddr;
|
3650 | ffd843bc | ths | #ifndef _WIN32
|
3651 | ffd843bc | ths | struct sockaddr_un uaddr;
|
3652 | ffd843bc | ths | #endif
|
3653 | ffd843bc | ths | struct sockaddr *addr;
|
3654 | ffd843bc | ths | socklen_t addrlen; |
3655 | 0bab00f3 | bellard | |
3656 | ffd843bc | ths | #ifndef _WIN32
|
3657 | ffd843bc | ths | if (is_unix) {
|
3658 | ffd843bc | ths | addr = (struct sockaddr *)&uaddr;
|
3659 | ffd843bc | ths | addrlen = sizeof(uaddr);
|
3660 | ffd843bc | ths | if (parse_unix_path(&uaddr, host_str) < 0) |
3661 | ffd843bc | ths | goto fail;
|
3662 | ffd843bc | ths | } else
|
3663 | ffd843bc | ths | #endif
|
3664 | ffd843bc | ths | { |
3665 | ffd843bc | ths | addr = (struct sockaddr *)&saddr;
|
3666 | ffd843bc | ths | addrlen = sizeof(saddr);
|
3667 | ffd843bc | ths | if (parse_host_port(&saddr, host_str) < 0) |
3668 | ffd843bc | ths | goto fail;
|
3669 | ffd843bc | ths | } |
3670 | 0bab00f3 | bellard | |
3671 | 951f1351 | bellard | ptr = host_str; |
3672 | 951f1351 | bellard | while((ptr = strchr(ptr,','))) { |
3673 | 951f1351 | bellard | ptr++; |
3674 | 951f1351 | bellard | if (!strncmp(ptr,"server",6)) { |
3675 | 951f1351 | bellard | is_listen = 1;
|
3676 | 951f1351 | bellard | } else if (!strncmp(ptr,"nowait",6)) { |
3677 | 951f1351 | bellard | is_waitconnect = 0;
|
3678 | f7499989 | pbrook | } else if (!strncmp(ptr,"nodelay",6)) { |
3679 | f7499989 | pbrook | do_nodelay = 1;
|
3680 | 951f1351 | bellard | } else {
|
3681 | 951f1351 | bellard | printf("Unknown option: %s\n", ptr);
|
3682 | 951f1351 | bellard | goto fail;
|
3683 | 951f1351 | bellard | } |
3684 | 951f1351 | bellard | } |
3685 | 951f1351 | bellard | if (!is_listen)
|
3686 | 951f1351 | bellard | is_waitconnect = 0;
|
3687 | 951f1351 | bellard | |
3688 | 0bab00f3 | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3689 | 0bab00f3 | bellard | if (!chr)
|
3690 | 0bab00f3 | bellard | goto fail;
|
3691 | 0bab00f3 | bellard | s = qemu_mallocz(sizeof(TCPCharDriver));
|
3692 | 0bab00f3 | bellard | if (!s)
|
3693 | 0bab00f3 | bellard | goto fail;
|
3694 | ffd843bc | ths | |
3695 | ffd843bc | ths | #ifndef _WIN32
|
3696 | ffd843bc | ths | if (is_unix)
|
3697 | ffd843bc | ths | fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
3698 | ffd843bc | ths | else
|
3699 | ffd843bc | ths | #endif
|
3700 | ffd843bc | ths | fd = socket(PF_INET, SOCK_STREAM, 0);
|
3701 | 5fafdf24 | ths | |
3702 | 5fafdf24 | ths | if (fd < 0) |
3703 | 0bab00f3 | bellard | goto fail;
|
3704 | 951f1351 | bellard | |
3705 | 951f1351 | bellard | if (!is_waitconnect)
|
3706 | 951f1351 | bellard | socket_set_nonblock(fd); |
3707 | 0bab00f3 | bellard | |
3708 | 0bab00f3 | bellard | s->connected = 0;
|
3709 | 0bab00f3 | bellard | s->fd = -1;
|
3710 | 0bab00f3 | bellard | s->listen_fd = -1;
|
3711 | ffd843bc | ths | s->is_unix = is_unix; |
3712 | f7499989 | pbrook | s->do_nodelay = do_nodelay && !is_unix; |
3713 | ffd843bc | ths | |
3714 | ffd843bc | ths | chr->opaque = s; |
3715 | ffd843bc | ths | chr->chr_write = tcp_chr_write; |
3716 | ffd843bc | ths | chr->chr_close = tcp_chr_close; |
3717 | ffd843bc | ths | |
3718 | 0bab00f3 | bellard | if (is_listen) {
|
3719 | 0bab00f3 | bellard | /* allow fast reuse */
|
3720 | ffd843bc | ths | #ifndef _WIN32
|
3721 | ffd843bc | ths | if (is_unix) {
|
3722 | ffd843bc | ths | char path[109]; |
3723 | ae45d369 | bellard | pstrcpy(path, sizeof(path), uaddr.sun_path);
|
3724 | ffd843bc | ths | unlink(path); |
3725 | ffd843bc | ths | } else
|
3726 | ffd843bc | ths | #endif
|
3727 | ffd843bc | ths | { |
3728 | ffd843bc | ths | val = 1;
|
3729 | ffd843bc | ths | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
3730 | ffd843bc | ths | } |
3731 | 3b46e624 | ths | |
3732 | ffd843bc | ths | ret = bind(fd, addr, addrlen); |
3733 | ffd843bc | ths | if (ret < 0) |
3734 | 0bab00f3 | bellard | goto fail;
|
3735 | ffd843bc | ths | |
3736 | 0bab00f3 | bellard | ret = listen(fd, 0);
|
3737 | 0bab00f3 | bellard | if (ret < 0) |
3738 | 0bab00f3 | bellard | goto fail;
|
3739 | ffd843bc | ths | |
3740 | 0bab00f3 | bellard | s->listen_fd = fd; |
3741 | 0bab00f3 | bellard | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
3742 | 951f1351 | bellard | if (is_telnet)
|
3743 | 951f1351 | bellard | s->do_telnetopt = 1;
|
3744 | 0bab00f3 | bellard | } else {
|
3745 | 0bab00f3 | bellard | for(;;) {
|
3746 | ffd843bc | ths | ret = connect(fd, addr, addrlen); |
3747 | 0bab00f3 | bellard | if (ret < 0) { |
3748 | 0bab00f3 | bellard | err = socket_error(); |
3749 | 0bab00f3 | bellard | if (err == EINTR || err == EWOULDBLOCK) {
|
3750 | 0bab00f3 | bellard | } else if (err == EINPROGRESS) { |
3751 | 0bab00f3 | bellard | break;
|
3752 | f5b12268 | ths | #ifdef _WIN32
|
3753 | f5b12268 | ths | } else if (err == WSAEALREADY) { |
3754 | f5b12268 | ths | break;
|
3755 | f5b12268 | ths | #endif
|
3756 | 0bab00f3 | bellard | } else {
|
3757 | 0bab00f3 | bellard | goto fail;
|
3758 | 0bab00f3 | bellard | } |
3759 | 0bab00f3 | bellard | } else {
|
3760 | 0bab00f3 | bellard | s->connected = 1;
|
3761 | 0bab00f3 | bellard | break;
|
3762 | 0bab00f3 | bellard | } |
3763 | 0bab00f3 | bellard | } |
3764 | 0bab00f3 | bellard | s->fd = fd; |
3765 | f7499989 | pbrook | socket_set_nodelay(fd); |
3766 | 0bab00f3 | bellard | if (s->connected)
|
3767 | 0bab00f3 | bellard | tcp_chr_connect(chr); |
3768 | 0bab00f3 | bellard | else
|
3769 | 0bab00f3 | bellard | qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
|
3770 | 0bab00f3 | bellard | } |
3771 | 3b46e624 | ths | |
3772 | 951f1351 | bellard | if (is_listen && is_waitconnect) {
|
3773 | 951f1351 | bellard | printf("QEMU waiting for connection on: %s\n", host_str);
|
3774 | 951f1351 | bellard | tcp_chr_accept(chr); |
3775 | 951f1351 | bellard | socket_set_nonblock(s->listen_fd); |
3776 | 951f1351 | bellard | } |
3777 | 951f1351 | bellard | |
3778 | 0bab00f3 | bellard | return chr;
|
3779 | 0bab00f3 | bellard | fail:
|
3780 | 0bab00f3 | bellard | if (fd >= 0) |
3781 | 0bab00f3 | bellard | closesocket(fd); |
3782 | 0bab00f3 | bellard | qemu_free(s); |
3783 | 0bab00f3 | bellard | qemu_free(chr); |
3784 | 0bab00f3 | bellard | return NULL; |
3785 | 0bab00f3 | bellard | } |
3786 | 0bab00f3 | bellard | |
3787 | 82c643ff | bellard | CharDriverState *qemu_chr_open(const char *filename) |
3788 | 82c643ff | bellard | { |
3789 | f8d179e3 | bellard | const char *p; |
3790 | fd1dff4b | bellard | |
3791 | 82c643ff | bellard | if (!strcmp(filename, "vc")) { |
3792 | af3a9031 | ths | return text_console_init(&display_state, 0); |
3793 | af3a9031 | ths | } else if (strstart(filename, "vc:", &p)) { |
3794 | af3a9031 | ths | return text_console_init(&display_state, p);
|
3795 | 82c643ff | bellard | } else if (!strcmp(filename, "null")) { |
3796 | 82c643ff | bellard | return qemu_chr_open_null();
|
3797 | 5fafdf24 | ths | } else
|
3798 | 0bab00f3 | bellard | if (strstart(filename, "tcp:", &p)) { |
3799 | ffd843bc | ths | return qemu_chr_open_tcp(p, 0, 0); |
3800 | 0bab00f3 | bellard | } else
|
3801 | 951f1351 | bellard | if (strstart(filename, "telnet:", &p)) { |
3802 | ffd843bc | ths | return qemu_chr_open_tcp(p, 1, 0); |
3803 | 0bab00f3 | bellard | } else
|
3804 | 0bab00f3 | bellard | if (strstart(filename, "udp:", &p)) { |
3805 | 0bab00f3 | bellard | return qemu_chr_open_udp(p);
|
3806 | 0bab00f3 | bellard | } else
|
3807 | 20d8a3ed | ths | if (strstart(filename, "mon:", &p)) { |
3808 | 20d8a3ed | ths | CharDriverState *drv = qemu_chr_open(p); |
3809 | 20d8a3ed | ths | if (drv) {
|
3810 | 20d8a3ed | ths | drv = qemu_chr_open_mux(drv); |
3811 | 20d8a3ed | ths | monitor_init(drv, !nographic); |
3812 | 20d8a3ed | ths | return drv;
|
3813 | 20d8a3ed | ths | } |
3814 | 20d8a3ed | ths | printf("Unable to open driver: %s\n", p);
|
3815 | 20d8a3ed | ths | return 0; |
3816 | 20d8a3ed | ths | } else
|
3817 | 7664728b | bellard | #ifndef _WIN32
|
3818 | ffd843bc | ths | if (strstart(filename, "unix:", &p)) { |
3819 | ffd843bc | ths | return qemu_chr_open_tcp(p, 0, 1); |
3820 | ffd843bc | ths | } else if (strstart(filename, "file:", &p)) { |
3821 | f8d179e3 | bellard | return qemu_chr_open_file_out(p);
|
3822 | f8d179e3 | bellard | } else if (strstart(filename, "pipe:", &p)) { |
3823 | f8d179e3 | bellard | return qemu_chr_open_pipe(p);
|
3824 | 7664728b | bellard | } else if (!strcmp(filename, "pty")) { |
3825 | 82c643ff | bellard | return qemu_chr_open_pty();
|
3826 | 82c643ff | bellard | } else if (!strcmp(filename, "stdio")) { |
3827 | 82c643ff | bellard | return qemu_chr_open_stdio();
|
3828 | 5fafdf24 | ths | } else
|
3829 | f8d179e3 | bellard | #if defined(__linux__)
|
3830 | e57a8c0e | bellard | if (strstart(filename, "/dev/parport", NULL)) { |
3831 | e57a8c0e | bellard | return qemu_chr_open_pp(filename);
|
3832 | 5fafdf24 | ths | } else
|
3833 | aec62507 | ths | #endif
|
3834 | 9892fbfb | blueswir1 | #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
|
3835 | 9892fbfb | blueswir1 | || defined(__NetBSD__) || defined(__OpenBSD__) |
3836 | f8d179e3 | bellard | if (strstart(filename, "/dev/", NULL)) { |
3837 | f8d179e3 | bellard | return qemu_chr_open_tty(filename);
|
3838 | 3fda388a | ths | } else
|
3839 | 3fda388a | ths | #endif
|
3840 | aec62507 | ths | #else /* !_WIN32 */ |
3841 | f331110f | bellard | if (strstart(filename, "COM", NULL)) { |
3842 | f331110f | bellard | return qemu_chr_open_win(filename);
|
3843 | f331110f | bellard | } else
|
3844 | f331110f | bellard | if (strstart(filename, "pipe:", &p)) { |
3845 | f331110f | bellard | return qemu_chr_open_win_pipe(p);
|
3846 | f331110f | bellard | } else
|
3847 | 72d46479 | ths | if (strstart(filename, "con:", NULL)) { |
3848 | 72d46479 | ths | return qemu_chr_open_win_con(filename);
|
3849 | 72d46479 | ths | } else
|
3850 | f331110f | bellard | if (strstart(filename, "file:", &p)) { |
3851 | f331110f | bellard | return qemu_chr_open_win_file_out(p);
|
3852 | 2e4d9fb1 | aurel32 | } else
|
3853 | 2e4d9fb1 | aurel32 | #endif
|
3854 | 2e4d9fb1 | aurel32 | #ifdef CONFIG_BRLAPI
|
3855 | 2e4d9fb1 | aurel32 | if (!strcmp(filename, "braille")) { |
3856 | 2e4d9fb1 | aurel32 | return chr_baum_init();
|
3857 | 2e4d9fb1 | aurel32 | } else
|
3858 | f331110f | bellard | #endif
|
3859 | 82c643ff | bellard | { |
3860 | 82c643ff | bellard | return NULL; |
3861 | 82c643ff | bellard | } |
3862 | 82c643ff | bellard | } |
3863 | 82c643ff | bellard | |
3864 | f331110f | bellard | void qemu_chr_close(CharDriverState *chr)
|
3865 | f331110f | bellard | { |
3866 | f331110f | bellard | if (chr->chr_close)
|
3867 | f331110f | bellard | chr->chr_close(chr); |
3868 | a11d070e | balrog | qemu_free(chr); |
3869 | f331110f | bellard | } |
3870 | f331110f | bellard | |
3871 | 80cabfad | bellard | /***********************************************************/
|
3872 | 7c9d8e07 | bellard | /* network device redirectors */
|
3873 | 330d0414 | bellard | |
3874 | 3f4afa14 | j_mayer | __attribute__ (( unused )) |
3875 | 9596ebb7 | pbrook | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
3876 | c20709aa | bellard | { |
3877 | c20709aa | bellard | int len, i, j, c;
|
3878 | c20709aa | bellard | |
3879 | c20709aa | bellard | for(i=0;i<size;i+=16) { |
3880 | c20709aa | bellard | len = size - i; |
3881 | c20709aa | bellard | if (len > 16) |
3882 | c20709aa | bellard | len = 16;
|
3883 | c20709aa | bellard | fprintf(f, "%08x ", i);
|
3884 | c20709aa | bellard | for(j=0;j<16;j++) { |
3885 | c20709aa | bellard | if (j < len)
|
3886 | c20709aa | bellard | fprintf(f, " %02x", buf[i+j]);
|
3887 | c20709aa | bellard | else
|
3888 | c20709aa | bellard | fprintf(f, " ");
|
3889 | c20709aa | bellard | } |
3890 | c20709aa | bellard | fprintf(f, " ");
|
3891 | c20709aa | bellard | for(j=0;j<len;j++) { |
3892 | c20709aa | bellard | c = buf[i+j]; |
3893 | c20709aa | bellard | if (c < ' ' || c > '~') |
3894 | c20709aa | bellard | c = '.';
|
3895 | c20709aa | bellard | fprintf(f, "%c", c);
|
3896 | c20709aa | bellard | } |
3897 | c20709aa | bellard | fprintf(f, "\n");
|
3898 | c20709aa | bellard | } |
3899 | c20709aa | bellard | } |
3900 | c20709aa | bellard | |
3901 | 7c9d8e07 | bellard | static int parse_macaddr(uint8_t *macaddr, const char *p) |
3902 | c20709aa | bellard | { |
3903 | 7c9d8e07 | bellard | int i;
|
3904 | 76ea08f9 | balrog | char *last_char;
|
3905 | 76ea08f9 | balrog | long int offset; |
3906 | 76ea08f9 | balrog | |
3907 | 76ea08f9 | balrog | errno = 0;
|
3908 | 76ea08f9 | balrog | offset = strtol(p, &last_char, 0);
|
3909 | 76ea08f9 | balrog | if (0 == errno && '\0' == *last_char && |
3910 | 76ea08f9 | balrog | offset >= 0 && offset <= 0xFFFFFF) { |
3911 | 76ea08f9 | balrog | macaddr[3] = (offset & 0xFF0000) >> 16; |
3912 | 76ea08f9 | balrog | macaddr[4] = (offset & 0xFF00) >> 8; |
3913 | 76ea08f9 | balrog | macaddr[5] = offset & 0xFF; |
3914 | 76ea08f9 | balrog | return 0; |
3915 | 76ea08f9 | balrog | } else {
|
3916 | 76ea08f9 | balrog | for(i = 0; i < 6; i++) { |
3917 | 76ea08f9 | balrog | macaddr[i] = strtol(p, (char **)&p, 16); |
3918 | 76ea08f9 | balrog | if (i == 5) { |
3919 | 76ea08f9 | balrog | if (*p != '\0') |
3920 | 76ea08f9 | balrog | return -1; |
3921 | 76ea08f9 | balrog | } else {
|
3922 | 76ea08f9 | balrog | if (*p != ':' && *p != '-') |
3923 | 76ea08f9 | balrog | return -1; |
3924 | 76ea08f9 | balrog | p++; |
3925 | 76ea08f9 | balrog | } |
3926 | 7c9d8e07 | bellard | } |
3927 | 76ea08f9 | balrog | return 0; |
3928 | 7c9d8e07 | bellard | } |
3929 | 76ea08f9 | balrog | |
3930 | 76ea08f9 | balrog | return -1; |
3931 | c20709aa | bellard | } |
3932 | 67b915a5 | bellard | |
3933 | 7c9d8e07 | bellard | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
3934 | 67b915a5 | bellard | { |
3935 | 7c9d8e07 | bellard | const char *p, *p1; |
3936 | 7c9d8e07 | bellard | int len;
|
3937 | 7c9d8e07 | bellard | p = *pp; |
3938 | 7c9d8e07 | bellard | p1 = strchr(p, sep); |
3939 | 7c9d8e07 | bellard | if (!p1)
|
3940 | 7c9d8e07 | bellard | return -1; |
3941 | 7c9d8e07 | bellard | len = p1 - p; |
3942 | 7c9d8e07 | bellard | p1++; |
3943 | 7c9d8e07 | bellard | if (buf_size > 0) { |
3944 | 7c9d8e07 | bellard | if (len > buf_size - 1) |
3945 | 7c9d8e07 | bellard | len = buf_size - 1;
|
3946 | 7c9d8e07 | bellard | memcpy(buf, p, len); |
3947 | 7c9d8e07 | bellard | buf[len] = '\0';
|
3948 | 7c9d8e07 | bellard | } |
3949 | 7c9d8e07 | bellard | *pp = p1; |
3950 | 7c9d8e07 | bellard | return 0; |
3951 | c20709aa | bellard | } |
3952 | c20709aa | bellard | |
3953 | 951f1351 | bellard | int parse_host_src_port(struct sockaddr_in *haddr, |
3954 | 951f1351 | bellard | struct sockaddr_in *saddr,
|
3955 | 951f1351 | bellard | const char *input_str) |
3956 | 951f1351 | bellard | { |
3957 | 951f1351 | bellard | char *str = strdup(input_str);
|
3958 | 951f1351 | bellard | char *host_str = str;
|
3959 | 951f1351 | bellard | char *src_str;
|
3960 | 951f1351 | bellard | char *ptr;
|
3961 | 951f1351 | bellard | |
3962 | 951f1351 | bellard | /*
|
3963 | 951f1351 | bellard | * Chop off any extra arguments at the end of the string which
|
3964 | 951f1351 | bellard | * would start with a comma, then fill in the src port information
|
3965 | 951f1351 | bellard | * if it was provided else use the "any address" and "any port".
|
3966 | 951f1351 | bellard | */
|
3967 | 951f1351 | bellard | if ((ptr = strchr(str,','))) |
3968 | 951f1351 | bellard | *ptr = '\0';
|
3969 | 951f1351 | bellard | |
3970 | 951f1351 | bellard | if ((src_str = strchr(input_str,'@'))) { |
3971 | 951f1351 | bellard | *src_str = '\0';
|
3972 | 951f1351 | bellard | src_str++; |
3973 | 951f1351 | bellard | } |
3974 | 951f1351 | bellard | |
3975 | 951f1351 | bellard | if (parse_host_port(haddr, host_str) < 0) |
3976 | 951f1351 | bellard | goto fail;
|
3977 | 951f1351 | bellard | |
3978 | 951f1351 | bellard | if (!src_str || *src_str == '\0') |
3979 | 951f1351 | bellard | src_str = ":0";
|
3980 | 951f1351 | bellard | |
3981 | 951f1351 | bellard | if (parse_host_port(saddr, src_str) < 0) |
3982 | 951f1351 | bellard | goto fail;
|
3983 | 951f1351 | bellard | |
3984 | 951f1351 | bellard | free(str); |
3985 | 951f1351 | bellard | return(0); |
3986 | 951f1351 | bellard | |
3987 | 951f1351 | bellard | fail:
|
3988 | 951f1351 | bellard | free(str); |
3989 | 951f1351 | bellard | return -1; |
3990 | 951f1351 | bellard | } |
3991 | 951f1351 | bellard | |
3992 | 7c9d8e07 | bellard | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
3993 | 7c9d8e07 | bellard | { |
3994 | 7c9d8e07 | bellard | char buf[512]; |
3995 | 7c9d8e07 | bellard | struct hostent *he;
|
3996 | 7c9d8e07 | bellard | const char *p, *r; |
3997 | 7c9d8e07 | bellard | int port;
|
3998 | 7c9d8e07 | bellard | |
3999 | 7c9d8e07 | bellard | p = str; |
4000 | 7c9d8e07 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
4001 | 7c9d8e07 | bellard | return -1; |
4002 | 7c9d8e07 | bellard | saddr->sin_family = AF_INET; |
4003 | 7c9d8e07 | bellard | if (buf[0] == '\0') { |
4004 | 7c9d8e07 | bellard | saddr->sin_addr.s_addr = 0;
|
4005 | 7c9d8e07 | bellard | } else {
|
4006 | 7c9d8e07 | bellard | if (isdigit(buf[0])) { |
4007 | 7c9d8e07 | bellard | if (!inet_aton(buf, &saddr->sin_addr))
|
4008 | 7c9d8e07 | bellard | return -1; |
4009 | 7c9d8e07 | bellard | } else {
|
4010 | 7c9d8e07 | bellard | if ((he = gethostbyname(buf)) == NULL) |
4011 | 7c9d8e07 | bellard | return - 1; |
4012 | 7c9d8e07 | bellard | saddr->sin_addr = *(struct in_addr *)he->h_addr;
|
4013 | 7c9d8e07 | bellard | } |
4014 | 7c9d8e07 | bellard | } |
4015 | 7c9d8e07 | bellard | port = strtol(p, (char **)&r, 0); |
4016 | 7c9d8e07 | bellard | if (r == p)
|
4017 | 7c9d8e07 | bellard | return -1; |
4018 | 7c9d8e07 | bellard | saddr->sin_port = htons(port); |
4019 | 7c9d8e07 | bellard | return 0; |
4020 | 7c9d8e07 | bellard | } |
4021 | c20709aa | bellard | |
4022 | 52f61fde | ths | #ifndef _WIN32
|
4023 | 52f61fde | ths | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) |
4024 | ffd843bc | ths | { |
4025 | ffd843bc | ths | const char *p; |
4026 | ffd843bc | ths | int len;
|
4027 | ffd843bc | ths | |
4028 | ffd843bc | ths | len = MIN(108, strlen(str));
|
4029 | ffd843bc | ths | p = strchr(str, ',');
|
4030 | ffd843bc | ths | if (p)
|
4031 | ffd843bc | ths | len = MIN(len, p - str); |
4032 | ffd843bc | ths | |
4033 | ffd843bc | ths | memset(uaddr, 0, sizeof(*uaddr)); |
4034 | ffd843bc | ths | |
4035 | ffd843bc | ths | uaddr->sun_family = AF_UNIX; |
4036 | ffd843bc | ths | memcpy(uaddr->sun_path, str, len); |
4037 | ffd843bc | ths | |
4038 | ffd843bc | ths | return 0; |
4039 | ffd843bc | ths | } |
4040 | 52f61fde | ths | #endif
|
4041 | ffd843bc | ths | |
4042 | 7c9d8e07 | bellard | /* find or alloc a new VLAN */
|
4043 | 7c9d8e07 | bellard | VLANState *qemu_find_vlan(int id)
|
4044 | c20709aa | bellard | { |
4045 | 7c9d8e07 | bellard | VLANState **pvlan, *vlan; |
4046 | 7c9d8e07 | bellard | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
4047 | 7c9d8e07 | bellard | if (vlan->id == id)
|
4048 | 7c9d8e07 | bellard | return vlan;
|
4049 | 7c9d8e07 | bellard | } |
4050 | 7c9d8e07 | bellard | vlan = qemu_mallocz(sizeof(VLANState));
|
4051 | 7c9d8e07 | bellard | if (!vlan)
|
4052 | 7c9d8e07 | bellard | return NULL; |
4053 | 7c9d8e07 | bellard | vlan->id = id; |
4054 | 7c9d8e07 | bellard | vlan->next = NULL;
|
4055 | 7c9d8e07 | bellard | pvlan = &first_vlan; |
4056 | 7c9d8e07 | bellard | while (*pvlan != NULL) |
4057 | 7c9d8e07 | bellard | pvlan = &(*pvlan)->next; |
4058 | 7c9d8e07 | bellard | *pvlan = vlan; |
4059 | 7c9d8e07 | bellard | return vlan;
|
4060 | c20709aa | bellard | } |
4061 | c20709aa | bellard | |
4062 | 7c9d8e07 | bellard | VLANClientState *qemu_new_vlan_client(VLANState *vlan, |
4063 | d861b05e | pbrook | IOReadHandler *fd_read, |
4064 | d861b05e | pbrook | IOCanRWHandler *fd_can_read, |
4065 | d861b05e | pbrook | void *opaque)
|
4066 | c20709aa | bellard | { |
4067 | 7c9d8e07 | bellard | VLANClientState *vc, **pvc; |
4068 | 7c9d8e07 | bellard | vc = qemu_mallocz(sizeof(VLANClientState));
|
4069 | 7c9d8e07 | bellard | if (!vc)
|
4070 | 7c9d8e07 | bellard | return NULL; |
4071 | 7c9d8e07 | bellard | vc->fd_read = fd_read; |
4072 | d861b05e | pbrook | vc->fd_can_read = fd_can_read; |
4073 | 7c9d8e07 | bellard | vc->opaque = opaque; |
4074 | 7c9d8e07 | bellard | vc->vlan = vlan; |
4075 | 7c9d8e07 | bellard | |
4076 | 7c9d8e07 | bellard | vc->next = NULL;
|
4077 | 7c9d8e07 | bellard | pvc = &vlan->first_client; |
4078 | 7c9d8e07 | bellard | while (*pvc != NULL) |
4079 | 7c9d8e07 | bellard | pvc = &(*pvc)->next; |
4080 | 7c9d8e07 | bellard | *pvc = vc; |
4081 | 7c9d8e07 | bellard | return vc;
|
4082 | c20709aa | bellard | } |
4083 | c20709aa | bellard | |
4084 | dcf414d6 | balrog | void qemu_del_vlan_client(VLANClientState *vc)
|
4085 | dcf414d6 | balrog | { |
4086 | dcf414d6 | balrog | VLANClientState **pvc = &vc->vlan->first_client; |
4087 | dcf414d6 | balrog | |
4088 | dcf414d6 | balrog | while (*pvc != NULL) |
4089 | dcf414d6 | balrog | if (*pvc == vc) {
|
4090 | dcf414d6 | balrog | *pvc = vc->next; |
4091 | dcf414d6 | balrog | free(vc); |
4092 | dcf414d6 | balrog | break;
|
4093 | dcf414d6 | balrog | } else
|
4094 | dcf414d6 | balrog | pvc = &(*pvc)->next; |
4095 | dcf414d6 | balrog | } |
4096 | dcf414d6 | balrog | |
4097 | d861b05e | pbrook | int qemu_can_send_packet(VLANClientState *vc1)
|
4098 | d861b05e | pbrook | { |
4099 | d861b05e | pbrook | VLANState *vlan = vc1->vlan; |
4100 | d861b05e | pbrook | VLANClientState *vc; |
4101 | d861b05e | pbrook | |
4102 | d861b05e | pbrook | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
4103 | d861b05e | pbrook | if (vc != vc1) {
|
4104 | fbd1711d | balrog | if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
|
4105 | fbd1711d | balrog | return 1; |
4106 | d861b05e | pbrook | } |
4107 | d861b05e | pbrook | } |
4108 | fbd1711d | balrog | return 0; |
4109 | d861b05e | pbrook | } |
4110 | d861b05e | pbrook | |
4111 | 7c9d8e07 | bellard | void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size) |
4112 | c20709aa | bellard | { |
4113 | 7c9d8e07 | bellard | VLANState *vlan = vc1->vlan; |
4114 | 7c9d8e07 | bellard | VLANClientState *vc; |
4115 | 7c9d8e07 | bellard | |
4116 | 7c9d8e07 | bellard | #if 0
|
4117 | 7c9d8e07 | bellard | printf("vlan %d send:\n", vlan->id);
|
4118 | 7c9d8e07 | bellard | hex_dump(stdout, buf, size);
|
4119 | 7c9d8e07 | bellard | #endif
|
4120 | 7c9d8e07 | bellard | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
4121 | 7c9d8e07 | bellard | if (vc != vc1) {
|
4122 | 7c9d8e07 | bellard | vc->fd_read(vc->opaque, buf, size); |
4123 | 7c9d8e07 | bellard | } |
4124 | 7c9d8e07 | bellard | } |
4125 | 67b915a5 | bellard | } |
4126 | 67b915a5 | bellard | |
4127 | c20709aa | bellard | #if defined(CONFIG_SLIRP)
|
4128 | c20709aa | bellard | |
4129 | c20709aa | bellard | /* slirp network adapter */
|
4130 | c20709aa | bellard | |
4131 | c20709aa | bellard | static int slirp_inited; |
4132 | 7c9d8e07 | bellard | static VLANClientState *slirp_vc;
|
4133 | c20709aa | bellard | |
4134 | c20709aa | bellard | int slirp_can_output(void) |
4135 | c20709aa | bellard | { |
4136 | 3b7f5d47 | pbrook | return !slirp_vc || qemu_can_send_packet(slirp_vc);
|
4137 | c20709aa | bellard | } |
4138 | c20709aa | bellard | |
4139 | c20709aa | bellard | void slirp_output(const uint8_t *pkt, int pkt_len) |
4140 | 67b915a5 | bellard | { |
4141 | c20709aa | bellard | #if 0
|
4142 | 7c9d8e07 | bellard | printf("slirp output:\n");
|
4143 | c20709aa | bellard | hex_dump(stdout, pkt, pkt_len);
|
4144 | c20709aa | bellard | #endif
|
4145 | 3b7f5d47 | pbrook | if (!slirp_vc)
|
4146 | 3b7f5d47 | pbrook | return;
|
4147 | 7c9d8e07 | bellard | qemu_send_packet(slirp_vc, pkt, pkt_len); |
4148 | 67b915a5 | bellard | } |
4149 | 67b915a5 | bellard | |
4150 | 7c9d8e07 | bellard | static void slirp_receive(void *opaque, const uint8_t *buf, int size) |
4151 | c20709aa | bellard | { |
4152 | c20709aa | bellard | #if 0
|
4153 | 7c9d8e07 | bellard | printf("slirp input:\n");
|
4154 | c20709aa | bellard | hex_dump(stdout, buf, size);
|
4155 | c20709aa | bellard | #endif
|
4156 | c20709aa | bellard | slirp_input(buf, size); |
4157 | c20709aa | bellard | } |
4158 | c20709aa | bellard | |
4159 | 7c9d8e07 | bellard | static int net_slirp_init(VLANState *vlan) |
4160 | c20709aa | bellard | { |
4161 | c20709aa | bellard | if (!slirp_inited) {
|
4162 | c20709aa | bellard | slirp_inited = 1;
|
4163 | c20709aa | bellard | slirp_init(); |
4164 | c20709aa | bellard | } |
4165 | 5fafdf24 | ths | slirp_vc = qemu_new_vlan_client(vlan, |
4166 | d861b05e | pbrook | slirp_receive, NULL, NULL); |
4167 | 7c9d8e07 | bellard | snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector"); |
4168 | 9bf05444 | bellard | return 0; |
4169 | 9bf05444 | bellard | } |
4170 | 9bf05444 | bellard | |
4171 | 9bf05444 | bellard | static void net_slirp_redir(const char *redir_str) |
4172 | 9bf05444 | bellard | { |
4173 | 9bf05444 | bellard | int is_udp;
|
4174 | 9bf05444 | bellard | char buf[256], *r; |
4175 | 9bf05444 | bellard | const char *p; |
4176 | 9bf05444 | bellard | struct in_addr guest_addr;
|
4177 | 9bf05444 | bellard | int host_port, guest_port;
|
4178 | 3b46e624 | ths | |
4179 | 9bf05444 | bellard | if (!slirp_inited) {
|
4180 | 9bf05444 | bellard | slirp_inited = 1;
|
4181 | 9bf05444 | bellard | slirp_init(); |
4182 | 9bf05444 | bellard | } |
4183 | 9bf05444 | bellard | |
4184 | 9bf05444 | bellard | p = redir_str; |
4185 | 9bf05444 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
4186 | 9bf05444 | bellard | goto fail;
|
4187 | 9bf05444 | bellard | if (!strcmp(buf, "tcp")) { |
4188 | 9bf05444 | bellard | is_udp = 0;
|
4189 | 9bf05444 | bellard | } else if (!strcmp(buf, "udp")) { |
4190 | 9bf05444 | bellard | is_udp = 1;
|
4191 | 9bf05444 | bellard | } else {
|
4192 | 9bf05444 | bellard | goto fail;
|
4193 | 9bf05444 | bellard | } |
4194 | 9bf05444 | bellard | |
4195 | 9bf05444 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
4196 | 9bf05444 | bellard | goto fail;
|
4197 | 9bf05444 | bellard | host_port = strtol(buf, &r, 0);
|
4198 | 9bf05444 | bellard | if (r == buf)
|
4199 | 9bf05444 | bellard | goto fail;
|
4200 | 9bf05444 | bellard | |
4201 | 9bf05444 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
4202 | 9bf05444 | bellard | goto fail;
|
4203 | 9bf05444 | bellard | if (buf[0] == '\0') { |
4204 | 9bf05444 | bellard | pstrcpy(buf, sizeof(buf), "10.0.2.15"); |
4205 | 9bf05444 | bellard | } |
4206 | 9bf05444 | bellard | if (!inet_aton(buf, &guest_addr))
|
4207 | 9bf05444 | bellard | goto fail;
|
4208 | 3b46e624 | ths | |
4209 | 9bf05444 | bellard | guest_port = strtol(p, &r, 0);
|
4210 | 9bf05444 | bellard | if (r == p)
|
4211 | 9bf05444 | bellard | goto fail;
|
4212 | 3b46e624 | ths | |
4213 | 9bf05444 | bellard | if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { |
4214 | 9bf05444 | bellard | fprintf(stderr, "qemu: could not set up redirection\n");
|
4215 | 9bf05444 | bellard | exit(1);
|
4216 | 9bf05444 | bellard | } |
4217 | 9bf05444 | bellard | return;
|
4218 | 9bf05444 | bellard | fail:
|
4219 | 9bf05444 | bellard | fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
|
4220 | 9bf05444 | bellard | exit(1);
|
4221 | 9bf05444 | bellard | } |
4222 | 3b46e624 | ths | |
4223 | c94c8d64 | bellard | #ifndef _WIN32
|
4224 | c94c8d64 | bellard | |
4225 | 9d728e8c | bellard | char smb_dir[1024]; |
4226 | 9d728e8c | bellard | |
4227 | 044fae83 | balrog | static void erase_dir(char *dir_name) |
4228 | 9d728e8c | bellard | { |
4229 | 9d728e8c | bellard | DIR *d; |
4230 | 9d728e8c | bellard | struct dirent *de;
|
4231 | 9d728e8c | bellard | char filename[1024]; |
4232 | 9d728e8c | bellard | |
4233 | 9d728e8c | bellard | /* erase all the files in the directory */
|
4234 | 044fae83 | balrog | if ((d = opendir(dir_name)) != 0) { |
4235 | 044fae83 | balrog | for(;;) {
|
4236 | 044fae83 | balrog | de = readdir(d); |
4237 | 044fae83 | balrog | if (!de)
|
4238 | 044fae83 | balrog | break;
|
4239 | 044fae83 | balrog | if (strcmp(de->d_name, ".") != 0 && |
4240 | 044fae83 | balrog | strcmp(de->d_name, "..") != 0) { |
4241 | 044fae83 | balrog | snprintf(filename, sizeof(filename), "%s/%s", |
4242 | 044fae83 | balrog | smb_dir, de->d_name); |
4243 | 044fae83 | balrog | if (unlink(filename) != 0) /* is it a directory? */ |
4244 | 044fae83 | balrog | erase_dir(filename); |
4245 | 044fae83 | balrog | } |
4246 | 9d728e8c | bellard | } |
4247 | 044fae83 | balrog | closedir(d); |
4248 | 044fae83 | balrog | rmdir(dir_name); |
4249 | 9d728e8c | bellard | } |
4250 | 044fae83 | balrog | } |
4251 | 044fae83 | balrog | |
4252 | 044fae83 | balrog | /* automatic user mode samba server configuration */
|
4253 | 044fae83 | balrog | static void smb_exit(void) |
4254 | 044fae83 | balrog | { |
4255 | 044fae83 | balrog | erase_dir(smb_dir); |
4256 | 9d728e8c | bellard | } |
4257 | 9d728e8c | bellard | |
4258 | 9d728e8c | bellard | /* automatic user mode samba server configuration */
|
4259 | 9596ebb7 | pbrook | static void net_slirp_smb(const char *exported_dir) |
4260 | 9d728e8c | bellard | { |
4261 | 9d728e8c | bellard | char smb_conf[1024]; |
4262 | 9d728e8c | bellard | char smb_cmdline[1024]; |
4263 | 9d728e8c | bellard | FILE *f; |
4264 | 9d728e8c | bellard | |
4265 | 9d728e8c | bellard | if (!slirp_inited) {
|
4266 | 9d728e8c | bellard | slirp_inited = 1;
|
4267 | 9d728e8c | bellard | slirp_init(); |
4268 | 9d728e8c | bellard | } |
4269 | 9d728e8c | bellard | |
4270 | 9d728e8c | bellard | /* XXX: better tmp dir construction */
|
4271 | 9d728e8c | bellard | snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); |
4272 | 9d728e8c | bellard | if (mkdir(smb_dir, 0700) < 0) { |
4273 | 9d728e8c | bellard | fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
|
4274 | 9d728e8c | bellard | exit(1);
|
4275 | 9d728e8c | bellard | } |
4276 | 9d728e8c | bellard | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); |
4277 | 3b46e624 | ths | |
4278 | 9d728e8c | bellard | f = fopen(smb_conf, "w");
|
4279 | 9d728e8c | bellard | if (!f) {
|
4280 | 9d728e8c | bellard | fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
|
4281 | 9d728e8c | bellard | exit(1);
|
4282 | 9d728e8c | bellard | } |
4283 | 5fafdf24 | ths | fprintf(f, |
4284 | 9d728e8c | bellard | "[global]\n"
|
4285 | 157777ef | bellard | "private dir=%s\n"
|
4286 | 157777ef | bellard | "smb ports=0\n"
|
4287 | 157777ef | bellard | "socket address=127.0.0.1\n"
|
4288 | 9d728e8c | bellard | "pid directory=%s\n"
|
4289 | 9d728e8c | bellard | "lock directory=%s\n"
|
4290 | 9d728e8c | bellard | "log file=%s/log.smbd\n"
|
4291 | 9d728e8c | bellard | "smb passwd file=%s/smbpasswd\n"
|
4292 | 03ffbb69 | bellard | "security = share\n"
|
4293 | 9d728e8c | bellard | "[qemu]\n"
|
4294 | 9d728e8c | bellard | "path=%s\n"
|
4295 | 9d728e8c | bellard | "read only=no\n"
|
4296 | 9d728e8c | bellard | "guest ok=yes\n",
|
4297 | 9d728e8c | bellard | smb_dir, |
4298 | 157777ef | bellard | smb_dir, |
4299 | 9d728e8c | bellard | smb_dir, |
4300 | 9d728e8c | bellard | smb_dir, |
4301 | 9d728e8c | bellard | smb_dir, |
4302 | 9d728e8c | bellard | exported_dir |
4303 | 9d728e8c | bellard | ); |
4304 | 9d728e8c | bellard | fclose(f); |
4305 | 9d728e8c | bellard | atexit(smb_exit); |
4306 | 9d728e8c | bellard | |
4307 | a14d6c8c | pbrook | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
4308 | a14d6c8c | pbrook | SMBD_COMMAND, smb_conf); |
4309 | 3b46e624 | ths | |
4310 | 9d728e8c | bellard | slirp_add_exec(0, smb_cmdline, 4, 139); |
4311 | 9d728e8c | bellard | } |
4312 | 9bf05444 | bellard | |
4313 | c94c8d64 | bellard | #endif /* !defined(_WIN32) */ |
4314 | 31a60e22 | blueswir1 | void do_info_slirp(void) |
4315 | 31a60e22 | blueswir1 | { |
4316 | 31a60e22 | blueswir1 | slirp_stats(); |
4317 | 31a60e22 | blueswir1 | } |
4318 | c94c8d64 | bellard | |
4319 | c20709aa | bellard | #endif /* CONFIG_SLIRP */ |
4320 | c20709aa | bellard | |
4321 | c20709aa | bellard | #if !defined(_WIN32)
|
4322 | 7c9d8e07 | bellard | |
4323 | 7c9d8e07 | bellard | typedef struct TAPState { |
4324 | 7c9d8e07 | bellard | VLANClientState *vc; |
4325 | 7c9d8e07 | bellard | int fd;
|
4326 | b46a8906 | ths | char down_script[1024]; |
4327 | 7c9d8e07 | bellard | } TAPState; |
4328 | 7c9d8e07 | bellard | |
4329 | 7c9d8e07 | bellard | static void tap_receive(void *opaque, const uint8_t *buf, int size) |
4330 | 7c9d8e07 | bellard | { |
4331 | 7c9d8e07 | bellard | TAPState *s = opaque; |
4332 | 7c9d8e07 | bellard | int ret;
|
4333 | 7c9d8e07 | bellard | for(;;) {
|
4334 | 7c9d8e07 | bellard | ret = write(s->fd, buf, size); |
4335 | 7c9d8e07 | bellard | if (ret < 0 && (errno == EINTR || errno == EAGAIN)) { |
4336 | 7c9d8e07 | bellard | } else {
|
4337 | 7c9d8e07 | bellard | break;
|
4338 | 7c9d8e07 | bellard | } |
4339 | 7c9d8e07 | bellard | } |
4340 | 7c9d8e07 | bellard | } |
4341 | 7c9d8e07 | bellard | |
4342 | 7c9d8e07 | bellard | static void tap_send(void *opaque) |
4343 | 7c9d8e07 | bellard | { |
4344 | 7c9d8e07 | bellard | TAPState *s = opaque; |
4345 | 7c9d8e07 | bellard | uint8_t buf[4096];
|
4346 | 7c9d8e07 | bellard | int size;
|
4347 | 7c9d8e07 | bellard | |
4348 | d5d10bc3 | ths | #ifdef __sun__
|
4349 | d5d10bc3 | ths | struct strbuf sbuf;
|
4350 | d5d10bc3 | ths | int f = 0; |
4351 | d5d10bc3 | ths | sbuf.maxlen = sizeof(buf);
|
4352 | d5d10bc3 | ths | sbuf.buf = buf; |
4353 | d5d10bc3 | ths | size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1; |
4354 | d5d10bc3 | ths | #else
|
4355 | 7c9d8e07 | bellard | size = read(s->fd, buf, sizeof(buf));
|
4356 | d5d10bc3 | ths | #endif
|
4357 | 7c9d8e07 | bellard | if (size > 0) { |
4358 | 7c9d8e07 | bellard | qemu_send_packet(s->vc, buf, size); |
4359 | 7c9d8e07 | bellard | } |
4360 | 7c9d8e07 | bellard | } |
4361 | 7c9d8e07 | bellard | |
4362 | 7c9d8e07 | bellard | /* fd support */
|
4363 | 7c9d8e07 | bellard | |
4364 | 7c9d8e07 | bellard | static TAPState *net_tap_fd_init(VLANState *vlan, int fd) |
4365 | 7c9d8e07 | bellard | { |
4366 | 7c9d8e07 | bellard | TAPState *s; |
4367 | 7c9d8e07 | bellard | |
4368 | 7c9d8e07 | bellard | s = qemu_mallocz(sizeof(TAPState));
|
4369 | 7c9d8e07 | bellard | if (!s)
|
4370 | 7c9d8e07 | bellard | return NULL; |
4371 | 7c9d8e07 | bellard | s->fd = fd; |
4372 | d861b05e | pbrook | s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
|
4373 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, tap_send, NULL, s);
|
4374 | 7c9d8e07 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd); |
4375 | 7c9d8e07 | bellard | return s;
|
4376 | 7c9d8e07 | bellard | } |
4377 | 7c9d8e07 | bellard | |
4378 | 5c40d2bd | ths | #if defined (_BSD) || defined (__FreeBSD_kernel__)
|
4379 | 7c9d8e07 | bellard | static int tap_open(char *ifname, int ifname_size) |
4380 | 7d3505c5 | bellard | { |
4381 | 7d3505c5 | bellard | int fd;
|
4382 | 7d3505c5 | bellard | char *dev;
|
4383 | 7d3505c5 | bellard | struct stat s;
|
4384 | 67b915a5 | bellard | |
4385 | aeb30be6 | balrog | TFR(fd = open("/dev/tap", O_RDWR));
|
4386 | 7d3505c5 | bellard | if (fd < 0) { |
4387 | 7d3505c5 | bellard | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
|
4388 | 7d3505c5 | bellard | return -1; |
4389 | 7d3505c5 | bellard | } |
4390 | 7d3505c5 | bellard | |
4391 | 7d3505c5 | bellard | fstat(fd, &s); |
4392 | 7d3505c5 | bellard | dev = devname(s.st_rdev, S_IFCHR); |
4393 | 7d3505c5 | bellard | pstrcpy(ifname, ifname_size, dev); |
4394 | 7d3505c5 | bellard | |
4395 | 7d3505c5 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
4396 | 7d3505c5 | bellard | return fd;
|
4397 | 7d3505c5 | bellard | } |
4398 | ec530c81 | bellard | #elif defined(__sun__)
|
4399 | d5d10bc3 | ths | #define TUNNEWPPA (('T'<<16) | 0x0001) |
4400 | 5fafdf24 | ths | /*
|
4401 | 5fafdf24 | ths | * Allocate TAP device, returns opened fd.
|
4402 | d5d10bc3 | ths | * Stores dev name in the first arg(must be large enough).
|
4403 | 3b46e624 | ths | */
|
4404 | 363a37d5 | blueswir1 | int tap_alloc(char *dev, size_t dev_size) |
4405 | d5d10bc3 | ths | { |
4406 | d5d10bc3 | ths | int tap_fd, if_fd, ppa = -1; |
4407 | d5d10bc3 | ths | static int ip_fd = 0; |
4408 | d5d10bc3 | ths | char *ptr;
|
4409 | d5d10bc3 | ths | |
4410 | d5d10bc3 | ths | static int arp_fd = 0; |
4411 | d5d10bc3 | ths | int ip_muxid, arp_muxid;
|
4412 | d5d10bc3 | ths | struct strioctl strioc_if, strioc_ppa;
|
4413 | d5d10bc3 | ths | int link_type = I_PLINK;;
|
4414 | d5d10bc3 | ths | struct lifreq ifr;
|
4415 | d5d10bc3 | ths | char actual_name[32] = ""; |
4416 | d5d10bc3 | ths | |
4417 | d5d10bc3 | ths | memset(&ifr, 0x0, sizeof(ifr)); |
4418 | d5d10bc3 | ths | |
4419 | d5d10bc3 | ths | if( *dev ){
|
4420 | 5fafdf24 | ths | ptr = dev; |
4421 | 5fafdf24 | ths | while( *ptr && !isdigit((int)*ptr) ) ptr++; |
4422 | d5d10bc3 | ths | ppa = atoi(ptr); |
4423 | d5d10bc3 | ths | } |
4424 | d5d10bc3 | ths | |
4425 | d5d10bc3 | ths | /* Check if IP device was opened */
|
4426 | d5d10bc3 | ths | if( ip_fd )
|
4427 | d5d10bc3 | ths | close(ip_fd); |
4428 | d5d10bc3 | ths | |
4429 | aeb30be6 | balrog | TFR(ip_fd = open("/dev/udp", O_RDWR, 0)); |
4430 | aeb30be6 | balrog | if (ip_fd < 0) { |
4431 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
|
4432 | d5d10bc3 | ths | return -1; |
4433 | d5d10bc3 | ths | } |
4434 | d5d10bc3 | ths | |
4435 | aeb30be6 | balrog | TFR(tap_fd = open("/dev/tap", O_RDWR, 0)); |
4436 | aeb30be6 | balrog | if (tap_fd < 0) { |
4437 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't open /dev/tap");
|
4438 | d5d10bc3 | ths | return -1; |
4439 | d5d10bc3 | ths | } |
4440 | d5d10bc3 | ths | |
4441 | d5d10bc3 | ths | /* Assign a new PPA and get its unit number. */
|
4442 | d5d10bc3 | ths | strioc_ppa.ic_cmd = TUNNEWPPA; |
4443 | d5d10bc3 | ths | strioc_ppa.ic_timout = 0;
|
4444 | d5d10bc3 | ths | strioc_ppa.ic_len = sizeof(ppa);
|
4445 | d5d10bc3 | ths | strioc_ppa.ic_dp = (char *)&ppa;
|
4446 | d5d10bc3 | ths | if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) |
4447 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't assign new interface");
|
4448 | d5d10bc3 | ths | |
4449 | aeb30be6 | balrog | TFR(if_fd = open("/dev/tap", O_RDWR, 0)); |
4450 | aeb30be6 | balrog | if (if_fd < 0) { |
4451 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't open /dev/tap (2)");
|
4452 | d5d10bc3 | ths | return -1; |
4453 | d5d10bc3 | ths | } |
4454 | d5d10bc3 | ths | if(ioctl(if_fd, I_PUSH, "ip") < 0){ |
4455 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't push IP module");
|
4456 | d5d10bc3 | ths | return -1; |
4457 | d5d10bc3 | ths | } |
4458 | d5d10bc3 | ths | |
4459 | d5d10bc3 | ths | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) |
4460 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't get flags\n");
|
4461 | d5d10bc3 | ths | |
4462 | d5d10bc3 | ths | snprintf (actual_name, 32, "tap%d", ppa); |
4463 | d5d10bc3 | ths | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
|
4464 | d5d10bc3 | ths | |
4465 | d5d10bc3 | ths | ifr.lifr_ppa = ppa; |
4466 | d5d10bc3 | ths | /* Assign ppa according to the unit number returned by tun device */
|
4467 | d5d10bc3 | ths | |
4468 | d5d10bc3 | ths | if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) |
4469 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't set PPA %d", ppa);
|
4470 | d5d10bc3 | ths | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) |
4471 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't get flags\n");
|
4472 | d5d10bc3 | ths | /* Push arp module to if_fd */
|
4473 | d5d10bc3 | ths | if (ioctl (if_fd, I_PUSH, "arp") < 0) |
4474 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't push ARP module (2)");
|
4475 | d5d10bc3 | ths | |
4476 | d5d10bc3 | ths | /* Push arp module to ip_fd */
|
4477 | d5d10bc3 | ths | if (ioctl (ip_fd, I_POP, NULL) < 0) |
4478 | d5d10bc3 | ths | syslog (LOG_ERR, "I_POP failed\n");
|
4479 | d5d10bc3 | ths | if (ioctl (ip_fd, I_PUSH, "arp") < 0) |
4480 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't push ARP module (3)\n");
|
4481 | d5d10bc3 | ths | /* Open arp_fd */
|
4482 | aeb30be6 | balrog | TFR(arp_fd = open ("/dev/tap", O_RDWR, 0)); |
4483 | aeb30be6 | balrog | if (arp_fd < 0) |
4484 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't open %s\n", "/dev/tap"); |
4485 | d5d10bc3 | ths | |
4486 | d5d10bc3 | ths | /* Set ifname to arp */
|
4487 | d5d10bc3 | ths | strioc_if.ic_cmd = SIOCSLIFNAME; |
4488 | d5d10bc3 | ths | strioc_if.ic_timout = 0;
|
4489 | d5d10bc3 | ths | strioc_if.ic_len = sizeof(ifr);
|
4490 | d5d10bc3 | ths | strioc_if.ic_dp = (char *)𝔦
|
4491 | d5d10bc3 | ths | if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ |
4492 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't set ifname to arp\n");
|
4493 | d5d10bc3 | ths | } |
4494 | d5d10bc3 | ths | |
4495 | d5d10bc3 | ths | if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ |
4496 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't link TAP device to IP");
|
4497 | d5d10bc3 | ths | return -1; |
4498 | d5d10bc3 | ths | } |
4499 | d5d10bc3 | ths | |
4500 | d5d10bc3 | ths | if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) |
4501 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't link TAP device to ARP");
|
4502 | d5d10bc3 | ths | |
4503 | d5d10bc3 | ths | close (if_fd); |
4504 | d5d10bc3 | ths | |
4505 | d5d10bc3 | ths | memset(&ifr, 0x0, sizeof(ifr)); |
4506 | d5d10bc3 | ths | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
|
4507 | d5d10bc3 | ths | ifr.lifr_ip_muxid = ip_muxid; |
4508 | d5d10bc3 | ths | ifr.lifr_arp_muxid = arp_muxid; |
4509 | d5d10bc3 | ths | |
4510 | d5d10bc3 | ths | if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) |
4511 | d5d10bc3 | ths | { |
4512 | d5d10bc3 | ths | ioctl (ip_fd, I_PUNLINK , arp_muxid); |
4513 | d5d10bc3 | ths | ioctl (ip_fd, I_PUNLINK, ip_muxid); |
4514 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't set multiplexor id");
|
4515 | d5d10bc3 | ths | } |
4516 | d5d10bc3 | ths | |
4517 | 363a37d5 | blueswir1 | snprintf(dev, dev_size, "tap%d", ppa);
|
4518 | d5d10bc3 | ths | return tap_fd;
|
4519 | d5d10bc3 | ths | } |
4520 | d5d10bc3 | ths | |
4521 | ec530c81 | bellard | static int tap_open(char *ifname, int ifname_size) |
4522 | ec530c81 | bellard | { |
4523 | d5d10bc3 | ths | char dev[10]=""; |
4524 | d5d10bc3 | ths | int fd;
|
4525 | 363a37d5 | blueswir1 | if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){ |
4526 | d5d10bc3 | ths | fprintf(stderr, "Cannot allocate TAP device\n");
|
4527 | d5d10bc3 | ths | return -1; |
4528 | d5d10bc3 | ths | } |
4529 | d5d10bc3 | ths | pstrcpy(ifname, ifname_size, dev); |
4530 | d5d10bc3 | ths | fcntl(fd, F_SETFL, O_NONBLOCK); |
4531 | d5d10bc3 | ths | return fd;
|
4532 | ec530c81 | bellard | } |
4533 | 7d3505c5 | bellard | #else
|
4534 | 7c9d8e07 | bellard | static int tap_open(char *ifname, int ifname_size) |
4535 | 330d0414 | bellard | { |
4536 | 80cabfad | bellard | struct ifreq ifr;
|
4537 | c4b1fcc0 | bellard | int fd, ret;
|
4538 | 3b46e624 | ths | |
4539 | aeb30be6 | balrog | TFR(fd = open("/dev/net/tun", O_RDWR));
|
4540 | 80cabfad | bellard | if (fd < 0) { |
4541 | 80cabfad | bellard | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
|
4542 | 80cabfad | bellard | return -1; |
4543 | 330d0414 | bellard | } |
4544 | 80cabfad | bellard | memset(&ifr, 0, sizeof(ifr)); |
4545 | 80cabfad | bellard | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
4546 | 7c9d8e07 | bellard | if (ifname[0] != '\0') |
4547 | 7c9d8e07 | bellard | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
4548 | 7c9d8e07 | bellard | else
|
4549 | 7c9d8e07 | bellard | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
|
4550 | 80cabfad | bellard | ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
|
4551 | 80cabfad | bellard | if (ret != 0) { |
4552 | 80cabfad | bellard | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
|
4553 | 80cabfad | bellard | close(fd); |
4554 | 80cabfad | bellard | return -1; |
4555 | 80cabfad | bellard | } |
4556 | c4b1fcc0 | bellard | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
4557 | 80cabfad | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
4558 | c4b1fcc0 | bellard | return fd;
|
4559 | c4b1fcc0 | bellard | } |
4560 | 7d3505c5 | bellard | #endif
|
4561 | 330d0414 | bellard | |
4562 | b46a8906 | ths | static int launch_script(const char *setup_script, const char *ifname, int fd) |
4563 | 7c9d8e07 | bellard | { |
4564 | b46a8906 | ths | int pid, status;
|
4565 | 7c9d8e07 | bellard | char *args[3]; |
4566 | 7c9d8e07 | bellard | char **parg;
|
4567 | 7c9d8e07 | bellard | |
4568 | b46a8906 | ths | /* try to launch network script */
|
4569 | 7c9d8e07 | bellard | pid = fork(); |
4570 | 7c9d8e07 | bellard | if (pid >= 0) { |
4571 | 7c9d8e07 | bellard | if (pid == 0) { |
4572 | 50d3eeae | ths | int open_max = sysconf (_SC_OPEN_MAX), i;
|
4573 | 50d3eeae | ths | for (i = 0; i < open_max; i++) |
4574 | 50d3eeae | ths | if (i != STDIN_FILENO &&
|
4575 | 50d3eeae | ths | i != STDOUT_FILENO && |
4576 | 50d3eeae | ths | i != STDERR_FILENO && |
4577 | 50d3eeae | ths | i != fd) |
4578 | 50d3eeae | ths | close(i); |
4579 | 50d3eeae | ths | |
4580 | 7c9d8e07 | bellard | parg = args; |
4581 | 7c9d8e07 | bellard | *parg++ = (char *)setup_script;
|
4582 | b46a8906 | ths | *parg++ = (char *)ifname;
|
4583 | 7c9d8e07 | bellard | *parg++ = NULL;
|
4584 | 7c9d8e07 | bellard | execv(setup_script, args); |
4585 | 4a38940d | bellard | _exit(1);
|
4586 | 7c9d8e07 | bellard | } |
4587 | 7c9d8e07 | bellard | while (waitpid(pid, &status, 0) != pid); |
4588 | 7c9d8e07 | bellard | if (!WIFEXITED(status) ||
|
4589 | 7c9d8e07 | bellard | WEXITSTATUS(status) != 0) {
|
4590 | 7c9d8e07 | bellard | fprintf(stderr, "%s: could not launch network script\n",
|
4591 | 7c9d8e07 | bellard | setup_script); |
4592 | 7c9d8e07 | bellard | return -1; |
4593 | 7c9d8e07 | bellard | } |
4594 | 7c9d8e07 | bellard | } |
4595 | b46a8906 | ths | return 0; |
4596 | b46a8906 | ths | } |
4597 | b46a8906 | ths | |
4598 | b46a8906 | ths | static int net_tap_init(VLANState *vlan, const char *ifname1, |
4599 | b46a8906 | ths | const char *setup_script, const char *down_script) |
4600 | b46a8906 | ths | { |
4601 | b46a8906 | ths | TAPState *s; |
4602 | b46a8906 | ths | int fd;
|
4603 | b46a8906 | ths | char ifname[128]; |
4604 | b46a8906 | ths | |
4605 | b46a8906 | ths | if (ifname1 != NULL) |
4606 | b46a8906 | ths | pstrcpy(ifname, sizeof(ifname), ifname1);
|
4607 | b46a8906 | ths | else
|
4608 | b46a8906 | ths | ifname[0] = '\0'; |
4609 | b46a8906 | ths | TFR(fd = tap_open(ifname, sizeof(ifname)));
|
4610 | b46a8906 | ths | if (fd < 0) |
4611 | b46a8906 | ths | return -1; |
4612 | b46a8906 | ths | |
4613 | b46a8906 | ths | if (!setup_script || !strcmp(setup_script, "no")) |
4614 | b46a8906 | ths | setup_script = "";
|
4615 | b46a8906 | ths | if (setup_script[0] != '\0') { |
4616 | b46a8906 | ths | if (launch_script(setup_script, ifname, fd))
|
4617 | b46a8906 | ths | return -1; |
4618 | 7c9d8e07 | bellard | } |
4619 | 7c9d8e07 | bellard | s = net_tap_fd_init(vlan, fd); |
4620 | 7c9d8e07 | bellard | if (!s)
|
4621 | 7c9d8e07 | bellard | return -1; |
4622 | 5fafdf24 | ths | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4623 | 7c9d8e07 | bellard | "tap: ifname=%s setup_script=%s", ifname, setup_script);
|
4624 | b46a8906 | ths | if (down_script && strcmp(down_script, "no")) |
4625 | b46a8906 | ths | snprintf(s->down_script, sizeof(s->down_script), "%s", down_script); |
4626 | 7c9d8e07 | bellard | return 0; |
4627 | 7c9d8e07 | bellard | } |
4628 | 7c9d8e07 | bellard | |
4629 | fd1dff4b | bellard | #endif /* !_WIN32 */ |
4630 | fd1dff4b | bellard | |
4631 | 8a16d273 | ths | #if defined(CONFIG_VDE)
|
4632 | 8a16d273 | ths | typedef struct VDEState { |
4633 | 8a16d273 | ths | VLANClientState *vc; |
4634 | 8a16d273 | ths | VDECONN *vde; |
4635 | 8a16d273 | ths | } VDEState; |
4636 | 8a16d273 | ths | |
4637 | 8a16d273 | ths | static void vde_to_qemu(void *opaque) |
4638 | 8a16d273 | ths | { |
4639 | 8a16d273 | ths | VDEState *s = opaque; |
4640 | 8a16d273 | ths | uint8_t buf[4096];
|
4641 | 8a16d273 | ths | int size;
|
4642 | 8a16d273 | ths | |
4643 | 8a16d273 | ths | size = vde_recv(s->vde, buf, sizeof(buf), 0); |
4644 | 8a16d273 | ths | if (size > 0) { |
4645 | 8a16d273 | ths | qemu_send_packet(s->vc, buf, size); |
4646 | 8a16d273 | ths | } |
4647 | 8a16d273 | ths | } |
4648 | 8a16d273 | ths | |
4649 | 8a16d273 | ths | static void vde_from_qemu(void *opaque, const uint8_t *buf, int size) |
4650 | 8a16d273 | ths | { |
4651 | 8a16d273 | ths | VDEState *s = opaque; |
4652 | 8a16d273 | ths | int ret;
|
4653 | 8a16d273 | ths | for(;;) {
|
4654 | 8a16d273 | ths | ret = vde_send(s->vde, buf, size, 0);
|
4655 | 8a16d273 | ths | if (ret < 0 && errno == EINTR) { |
4656 | 8a16d273 | ths | } else {
|
4657 | 8a16d273 | ths | break;
|
4658 | 8a16d273 | ths | } |
4659 | 8a16d273 | ths | } |
4660 | 8a16d273 | ths | } |
4661 | 8a16d273 | ths | |
4662 | 8a16d273 | ths | static int net_vde_init(VLANState *vlan, const char *sock, int port, |
4663 | 8a16d273 | ths | const char *group, int mode) |
4664 | 8a16d273 | ths | { |
4665 | 8a16d273 | ths | VDEState *s; |
4666 | 8a16d273 | ths | char *init_group = strlen(group) ? (char *)group : NULL; |
4667 | 8a16d273 | ths | char *init_sock = strlen(sock) ? (char *)sock : NULL; |
4668 | 8a16d273 | ths | |
4669 | 8a16d273 | ths | struct vde_open_args args = {
|
4670 | 8a16d273 | ths | .port = port, |
4671 | 8a16d273 | ths | .group = init_group, |
4672 | 8a16d273 | ths | .mode = mode, |
4673 | 8a16d273 | ths | }; |
4674 | 8a16d273 | ths | |
4675 | 8a16d273 | ths | s = qemu_mallocz(sizeof(VDEState));
|
4676 | 8a16d273 | ths | if (!s)
|
4677 | 8a16d273 | ths | return -1; |
4678 | 8a16d273 | ths | s->vde = vde_open(init_sock, "QEMU", &args);
|
4679 | 8a16d273 | ths | if (!s->vde){
|
4680 | 8a16d273 | ths | free(s); |
4681 | 8a16d273 | ths | return -1; |
4682 | 8a16d273 | ths | } |
4683 | 8a16d273 | ths | s->vc = qemu_new_vlan_client(vlan, vde_from_qemu, NULL, s);
|
4684 | 8a16d273 | ths | qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
|
4685 | 8a16d273 | ths | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "vde: sock=%s fd=%d", |
4686 | 8a16d273 | ths | sock, vde_datafd(s->vde)); |
4687 | 8a16d273 | ths | return 0; |
4688 | 8a16d273 | ths | } |
4689 | 8a16d273 | ths | #endif
|
4690 | 8a16d273 | ths | |
4691 | 7c9d8e07 | bellard | /* network connection */
|
4692 | 7c9d8e07 | bellard | typedef struct NetSocketState { |
4693 | 7c9d8e07 | bellard | VLANClientState *vc; |
4694 | 7c9d8e07 | bellard | int fd;
|
4695 | 7c9d8e07 | bellard | int state; /* 0 = getting length, 1 = getting data */ |
4696 | 7c9d8e07 | bellard | int index;
|
4697 | 7c9d8e07 | bellard | int packet_len;
|
4698 | 7c9d8e07 | bellard | uint8_t buf[4096];
|
4699 | 3d830459 | bellard | struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
4700 | 7c9d8e07 | bellard | } NetSocketState; |
4701 | 7c9d8e07 | bellard | |
4702 | 7c9d8e07 | bellard | typedef struct NetSocketListenState { |
4703 | 7c9d8e07 | bellard | VLANState *vlan; |
4704 | 7c9d8e07 | bellard | int fd;
|
4705 | 7c9d8e07 | bellard | } NetSocketListenState; |
4706 | 7c9d8e07 | bellard | |
4707 | 7c9d8e07 | bellard | /* XXX: we consider we can send the whole packet without blocking */
|
4708 | 7c9d8e07 | bellard | static void net_socket_receive(void *opaque, const uint8_t *buf, int size) |
4709 | c20709aa | bellard | { |
4710 | 7c9d8e07 | bellard | NetSocketState *s = opaque; |
4711 | 7c9d8e07 | bellard | uint32_t len; |
4712 | 7c9d8e07 | bellard | len = htonl(size); |
4713 | 7c9d8e07 | bellard | |
4714 | fd1dff4b | bellard | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
4715 | fd1dff4b | bellard | send_all(s->fd, buf, size); |
4716 | c20709aa | bellard | } |
4717 | c20709aa | bellard | |
4718 | 3d830459 | bellard | static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size) |
4719 | 3d830459 | bellard | { |
4720 | 3d830459 | bellard | NetSocketState *s = opaque; |
4721 | 5fafdf24 | ths | sendto(s->fd, buf, size, 0,
|
4722 | 3d830459 | bellard | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
4723 | 3d830459 | bellard | } |
4724 | 3d830459 | bellard | |
4725 | 7c9d8e07 | bellard | static void net_socket_send(void *opaque) |
4726 | c4b1fcc0 | bellard | { |
4727 | 7c9d8e07 | bellard | NetSocketState *s = opaque; |
4728 | fd1dff4b | bellard | int l, size, err;
|
4729 | 7c9d8e07 | bellard | uint8_t buf1[4096];
|
4730 | 7c9d8e07 | bellard | const uint8_t *buf;
|
4731 | 7c9d8e07 | bellard | |
4732 | fd1dff4b | bellard | size = recv(s->fd, buf1, sizeof(buf1), 0); |
4733 | fd1dff4b | bellard | if (size < 0) { |
4734 | fd1dff4b | bellard | err = socket_error(); |
4735 | 5fafdf24 | ths | if (err != EWOULDBLOCK)
|
4736 | fd1dff4b | bellard | goto eoc;
|
4737 | fd1dff4b | bellard | } else if (size == 0) { |
4738 | 7c9d8e07 | bellard | /* end of connection */
|
4739 | fd1dff4b | bellard | eoc:
|
4740 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
4741 | fd1dff4b | bellard | closesocket(s->fd); |
4742 | 7c9d8e07 | bellard | return;
|
4743 | 7c9d8e07 | bellard | } |
4744 | 7c9d8e07 | bellard | buf = buf1; |
4745 | 7c9d8e07 | bellard | while (size > 0) { |
4746 | 7c9d8e07 | bellard | /* reassemble a packet from the network */
|
4747 | 7c9d8e07 | bellard | switch(s->state) {
|
4748 | 7c9d8e07 | bellard | case 0: |
4749 | 7c9d8e07 | bellard | l = 4 - s->index;
|
4750 | 7c9d8e07 | bellard | if (l > size)
|
4751 | 7c9d8e07 | bellard | l = size; |
4752 | 7c9d8e07 | bellard | memcpy(s->buf + s->index, buf, l); |
4753 | 7c9d8e07 | bellard | buf += l; |
4754 | 7c9d8e07 | bellard | size -= l; |
4755 | 7c9d8e07 | bellard | s->index += l; |
4756 | 7c9d8e07 | bellard | if (s->index == 4) { |
4757 | 7c9d8e07 | bellard | /* got length */
|
4758 | 7c9d8e07 | bellard | s->packet_len = ntohl(*(uint32_t *)s->buf); |
4759 | 7c9d8e07 | bellard | s->index = 0;
|
4760 | 7c9d8e07 | bellard | s->state = 1;
|
4761 | 7c9d8e07 | bellard | } |
4762 | 7c9d8e07 | bellard | break;
|
4763 | 7c9d8e07 | bellard | case 1: |
4764 | 7c9d8e07 | bellard | l = s->packet_len - s->index; |
4765 | 7c9d8e07 | bellard | if (l > size)
|
4766 | 7c9d8e07 | bellard | l = size; |
4767 | 7c9d8e07 | bellard | memcpy(s->buf + s->index, buf, l); |
4768 | 7c9d8e07 | bellard | s->index += l; |
4769 | 7c9d8e07 | bellard | buf += l; |
4770 | 7c9d8e07 | bellard | size -= l; |
4771 | 7c9d8e07 | bellard | if (s->index >= s->packet_len) {
|
4772 | 7c9d8e07 | bellard | qemu_send_packet(s->vc, s->buf, s->packet_len); |
4773 | 7c9d8e07 | bellard | s->index = 0;
|
4774 | 7c9d8e07 | bellard | s->state = 0;
|
4775 | 7c9d8e07 | bellard | } |
4776 | 7c9d8e07 | bellard | break;
|
4777 | 7c9d8e07 | bellard | } |
4778 | 7c9d8e07 | bellard | } |
4779 | c20709aa | bellard | } |
4780 | c20709aa | bellard | |
4781 | 3d830459 | bellard | static void net_socket_send_dgram(void *opaque) |
4782 | 3d830459 | bellard | { |
4783 | 3d830459 | bellard | NetSocketState *s = opaque; |
4784 | 3d830459 | bellard | int size;
|
4785 | 3d830459 | bellard | |
4786 | 3d830459 | bellard | size = recv(s->fd, s->buf, sizeof(s->buf), 0); |
4787 | 5fafdf24 | ths | if (size < 0) |
4788 | 3d830459 | bellard | return;
|
4789 | 3d830459 | bellard | if (size == 0) { |
4790 | 3d830459 | bellard | /* end of connection */
|
4791 | 3d830459 | bellard | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
4792 | 3d830459 | bellard | return;
|
4793 | 3d830459 | bellard | } |
4794 | 3d830459 | bellard | qemu_send_packet(s->vc, s->buf, size); |
4795 | 3d830459 | bellard | } |
4796 | 3d830459 | bellard | |
4797 | 3d830459 | bellard | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
4798 | 3d830459 | bellard | { |
4799 | 3d830459 | bellard | struct ip_mreq imr;
|
4800 | 3d830459 | bellard | int fd;
|
4801 | 3d830459 | bellard | int val, ret;
|
4802 | 3d830459 | bellard | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
|
4803 | 3d830459 | bellard | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
|
4804 | 5fafdf24 | ths | inet_ntoa(mcastaddr->sin_addr), |
4805 | fd1dff4b | bellard | (int)ntohl(mcastaddr->sin_addr.s_addr));
|
4806 | 3d830459 | bellard | return -1; |
4807 | 3d830459 | bellard | |
4808 | 3d830459 | bellard | } |
4809 | 3d830459 | bellard | fd = socket(PF_INET, SOCK_DGRAM, 0);
|
4810 | 3d830459 | bellard | if (fd < 0) { |
4811 | 3d830459 | bellard | perror("socket(PF_INET, SOCK_DGRAM)");
|
4812 | 3d830459 | bellard | return -1; |
4813 | 3d830459 | bellard | } |
4814 | 3d830459 | bellard | |
4815 | fd1dff4b | bellard | val = 1;
|
4816 | 5fafdf24 | ths | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
4817 | fd1dff4b | bellard | (const char *)&val, sizeof(val)); |
4818 | fd1dff4b | bellard | if (ret < 0) { |
4819 | fd1dff4b | bellard | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
|
4820 | fd1dff4b | bellard | goto fail;
|
4821 | fd1dff4b | bellard | } |
4822 | fd1dff4b | bellard | |
4823 | fd1dff4b | bellard | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
4824 | fd1dff4b | bellard | if (ret < 0) { |
4825 | fd1dff4b | bellard | perror("bind");
|
4826 | fd1dff4b | bellard | goto fail;
|
4827 | fd1dff4b | bellard | } |
4828 | 3b46e624 | ths | |
4829 | 3d830459 | bellard | /* Add host to multicast group */
|
4830 | 3d830459 | bellard | imr.imr_multiaddr = mcastaddr->sin_addr; |
4831 | 3d830459 | bellard | imr.imr_interface.s_addr = htonl(INADDR_ANY); |
4832 | 3d830459 | bellard | |
4833 | 5fafdf24 | ths | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
4834 | fd1dff4b | bellard | (const char *)&imr, sizeof(struct ip_mreq)); |
4835 | 3d830459 | bellard | if (ret < 0) { |
4836 | 3d830459 | bellard | perror("setsockopt(IP_ADD_MEMBERSHIP)");
|
4837 | 3d830459 | bellard | goto fail;
|
4838 | 3d830459 | bellard | } |
4839 | 3d830459 | bellard | |
4840 | 3d830459 | bellard | /* Force mcast msgs to loopback (eg. several QEMUs in same host */
|
4841 | 3d830459 | bellard | val = 1;
|
4842 | 5fafdf24 | ths | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
4843 | fd1dff4b | bellard | (const char *)&val, sizeof(val)); |
4844 | 3d830459 | bellard | if (ret < 0) { |
4845 | 3d830459 | bellard | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
|
4846 | 3d830459 | bellard | goto fail;
|
4847 | 3d830459 | bellard | } |
4848 | 3d830459 | bellard | |
4849 | fd1dff4b | bellard | socket_set_nonblock(fd); |
4850 | 3d830459 | bellard | return fd;
|
4851 | 3d830459 | bellard | fail:
|
4852 | 5fafdf24 | ths | if (fd >= 0) |
4853 | 0bab00f3 | bellard | closesocket(fd); |
4854 | 3d830459 | bellard | return -1; |
4855 | 3d830459 | bellard | } |
4856 | 3d830459 | bellard | |
4857 | 5fafdf24 | ths | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, |
4858 | 3d830459 | bellard | int is_connected)
|
4859 | 3d830459 | bellard | { |
4860 | 3d830459 | bellard | struct sockaddr_in saddr;
|
4861 | 3d830459 | bellard | int newfd;
|
4862 | 3d830459 | bellard | socklen_t saddr_len; |
4863 | 3d830459 | bellard | NetSocketState *s; |
4864 | 3d830459 | bellard | |
4865 | 3d830459 | bellard | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
|
4866 | 5fafdf24 | ths | * Because this may be "shared" socket from a "master" process, datagrams would be recv()
|
4867 | 3d830459 | bellard | * by ONLY ONE process: we must "clone" this dgram socket --jjo
|
4868 | 3d830459 | bellard | */
|
4869 | 3d830459 | bellard | |
4870 | 3d830459 | bellard | if (is_connected) {
|
4871 | 3d830459 | bellard | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
4872 | 3d830459 | bellard | /* must be bound */
|
4873 | 3d830459 | bellard | if (saddr.sin_addr.s_addr==0) { |
4874 | 3d830459 | bellard | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
|
4875 | 3d830459 | bellard | fd); |
4876 | 3d830459 | bellard | return NULL; |
4877 | 3d830459 | bellard | } |
4878 | 3d830459 | bellard | /* clone dgram socket */
|
4879 | 3d830459 | bellard | newfd = net_socket_mcast_create(&saddr); |
4880 | 3d830459 | bellard | if (newfd < 0) { |
4881 | 3d830459 | bellard | /* error already reported by net_socket_mcast_create() */
|
4882 | 3d830459 | bellard | close(fd); |
4883 | 3d830459 | bellard | return NULL; |
4884 | 3d830459 | bellard | } |
4885 | 3d830459 | bellard | /* clone newfd to fd, close newfd */
|
4886 | 3d830459 | bellard | dup2(newfd, fd); |
4887 | 3d830459 | bellard | close(newfd); |
4888 | 5fafdf24 | ths | |
4889 | 3d830459 | bellard | } else {
|
4890 | 3d830459 | bellard | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
|
4891 | 3d830459 | bellard | fd, strerror(errno)); |
4892 | 3d830459 | bellard | return NULL; |
4893 | 3d830459 | bellard | } |
4894 | 3d830459 | bellard | } |
4895 | 3d830459 | bellard | |
4896 | 3d830459 | bellard | s = qemu_mallocz(sizeof(NetSocketState));
|
4897 | 3d830459 | bellard | if (!s)
|
4898 | 3d830459 | bellard | return NULL; |
4899 | 3d830459 | bellard | s->fd = fd; |
4900 | 3d830459 | bellard | |
4901 | d861b05e | pbrook | s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
|
4902 | 3d830459 | bellard | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
|
4903 | 3d830459 | bellard | |
4904 | 3d830459 | bellard | /* mcast: save bound address as dst */
|
4905 | 3d830459 | bellard | if (is_connected) s->dgram_dst=saddr;
|
4906 | 3d830459 | bellard | |
4907 | 3d830459 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4908 | 5fafdf24 | ths | "socket: fd=%d (%s mcast=%s:%d)",
|
4909 | 3d830459 | bellard | fd, is_connected? "cloned" : "", |
4910 | 3d830459 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4911 | 3d830459 | bellard | return s;
|
4912 | 3d830459 | bellard | } |
4913 | 3d830459 | bellard | |
4914 | 7c9d8e07 | bellard | static void net_socket_connect(void *opaque) |
4915 | c20709aa | bellard | { |
4916 | 7c9d8e07 | bellard | NetSocketState *s = opaque; |
4917 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
|
4918 | 7c9d8e07 | bellard | } |
4919 | c4b1fcc0 | bellard | |
4920 | 5fafdf24 | ths | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, |
4921 | 7c9d8e07 | bellard | int is_connected)
|
4922 | 7c9d8e07 | bellard | { |
4923 | 7c9d8e07 | bellard | NetSocketState *s; |
4924 | 7c9d8e07 | bellard | s = qemu_mallocz(sizeof(NetSocketState));
|
4925 | 7c9d8e07 | bellard | if (!s)
|
4926 | 7c9d8e07 | bellard | return NULL; |
4927 | 7c9d8e07 | bellard | s->fd = fd; |
4928 | 5fafdf24 | ths | s->vc = qemu_new_vlan_client(vlan, |
4929 | d861b05e | pbrook | net_socket_receive, NULL, s);
|
4930 | 7c9d8e07 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4931 | 7c9d8e07 | bellard | "socket: fd=%d", fd);
|
4932 | 7c9d8e07 | bellard | if (is_connected) {
|
4933 | 7c9d8e07 | bellard | net_socket_connect(s); |
4934 | 7c9d8e07 | bellard | } else {
|
4935 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
|
4936 | 7c9d8e07 | bellard | } |
4937 | 7c9d8e07 | bellard | return s;
|
4938 | 7c9d8e07 | bellard | } |
4939 | c4b1fcc0 | bellard | |
4940 | 5fafdf24 | ths | static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, |
4941 | 3d830459 | bellard | int is_connected)
|
4942 | 3d830459 | bellard | { |
4943 | 3d830459 | bellard | int so_type=-1, optlen=sizeof(so_type); |
4944 | 3d830459 | bellard | |
4945 | 69b34976 | ths | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, |
4946 | 69b34976 | ths | (socklen_t *)&optlen)< 0) {
|
4947 | 931f03ee | ths | fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
|
4948 | 3d830459 | bellard | return NULL; |
4949 | 3d830459 | bellard | } |
4950 | 3d830459 | bellard | switch(so_type) {
|
4951 | 3d830459 | bellard | case SOCK_DGRAM:
|
4952 | 3d830459 | bellard | return net_socket_fd_init_dgram(vlan, fd, is_connected);
|
4953 | 3d830459 | bellard | case SOCK_STREAM:
|
4954 | 3d830459 | bellard | return net_socket_fd_init_stream(vlan, fd, is_connected);
|
4955 | 3d830459 | bellard | default:
|
4956 | 3d830459 | bellard | /* who knows ... this could be a eg. a pty, do warn and continue as stream */
|
4957 | 3d830459 | bellard | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
|
4958 | 3d830459 | bellard | return net_socket_fd_init_stream(vlan, fd, is_connected);
|
4959 | 3d830459 | bellard | } |
4960 | 3d830459 | bellard | return NULL; |
4961 | 3d830459 | bellard | } |
4962 | 3d830459 | bellard | |
4963 | 7c9d8e07 | bellard | static void net_socket_accept(void *opaque) |
4964 | 7c9d8e07 | bellard | { |
4965 | 3b46e624 | ths | NetSocketListenState *s = opaque; |
4966 | 7c9d8e07 | bellard | NetSocketState *s1; |
4967 | 7c9d8e07 | bellard | struct sockaddr_in saddr;
|
4968 | 7c9d8e07 | bellard | socklen_t len; |
4969 | 7c9d8e07 | bellard | int fd;
|
4970 | 7c9d8e07 | bellard | |
4971 | 7c9d8e07 | bellard | for(;;) {
|
4972 | 7c9d8e07 | bellard | len = sizeof(saddr);
|
4973 | 7c9d8e07 | bellard | fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
|
4974 | 7c9d8e07 | bellard | if (fd < 0 && errno != EINTR) { |
4975 | 7c9d8e07 | bellard | return;
|
4976 | 7c9d8e07 | bellard | } else if (fd >= 0) { |
4977 | 7c9d8e07 | bellard | break;
|
4978 | 80cabfad | bellard | } |
4979 | 330d0414 | bellard | } |
4980 | 5fafdf24 | ths | s1 = net_socket_fd_init(s->vlan, fd, 1);
|
4981 | 7c9d8e07 | bellard | if (!s1) {
|
4982 | 0bab00f3 | bellard | closesocket(fd); |
4983 | 7c9d8e07 | bellard | } else {
|
4984 | 7c9d8e07 | bellard | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
|
4985 | 5fafdf24 | ths | "socket: connection from %s:%d",
|
4986 | 7c9d8e07 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4987 | 7c9d8e07 | bellard | } |
4988 | 7c9d8e07 | bellard | } |
4989 | 7c9d8e07 | bellard | |
4990 | 7c9d8e07 | bellard | static int net_socket_listen_init(VLANState *vlan, const char *host_str) |
4991 | 7c9d8e07 | bellard | { |
4992 | 7c9d8e07 | bellard | NetSocketListenState *s; |
4993 | 7c9d8e07 | bellard | int fd, val, ret;
|
4994 | 7c9d8e07 | bellard | struct sockaddr_in saddr;
|
4995 | 7c9d8e07 | bellard | |
4996 | 7c9d8e07 | bellard | if (parse_host_port(&saddr, host_str) < 0) |
4997 | 7c9d8e07 | bellard | return -1; |
4998 | 3b46e624 | ths | |
4999 | 7c9d8e07 | bellard | s = qemu_mallocz(sizeof(NetSocketListenState));
|
5000 | 7c9d8e07 | bellard | if (!s)
|
5001 | 7c9d8e07 | bellard | return -1; |
5002 | 7c9d8e07 | bellard | |
5003 | 7c9d8e07 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
5004 | 7c9d8e07 | bellard | if (fd < 0) { |
5005 | 7c9d8e07 | bellard | perror("socket");
|
5006 | 7c9d8e07 | bellard | return -1; |
5007 | 7c9d8e07 | bellard | } |
5008 | fd1dff4b | bellard | socket_set_nonblock(fd); |
5009 | 7c9d8e07 | bellard | |
5010 | 7c9d8e07 | bellard | /* allow fast reuse */
|
5011 | 7c9d8e07 | bellard | val = 1;
|
5012 | fd1dff4b | bellard | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
5013 | 3b46e624 | ths | |
5014 | 7c9d8e07 | bellard | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
5015 | 7c9d8e07 | bellard | if (ret < 0) { |
5016 | 7c9d8e07 | bellard | perror("bind");
|
5017 | 7c9d8e07 | bellard | return -1; |
5018 | 7c9d8e07 | bellard | } |
5019 | 7c9d8e07 | bellard | ret = listen(fd, 0);
|
5020 | 7c9d8e07 | bellard | if (ret < 0) { |
5021 | 7c9d8e07 | bellard | perror("listen");
|
5022 | 7c9d8e07 | bellard | return -1; |
5023 | 7c9d8e07 | bellard | } |
5024 | 7c9d8e07 | bellard | s->vlan = vlan; |
5025 | 7c9d8e07 | bellard | s->fd = fd; |
5026 | 7c9d8e07 | bellard | qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
|
5027 | 80cabfad | bellard | return 0; |
5028 | 330d0414 | bellard | } |
5029 | 330d0414 | bellard | |
5030 | 7c9d8e07 | bellard | static int net_socket_connect_init(VLANState *vlan, const char *host_str) |
5031 | 330d0414 | bellard | { |
5032 | 7c9d8e07 | bellard | NetSocketState *s; |
5033 | fd1dff4b | bellard | int fd, connected, ret, err;
|
5034 | 7c9d8e07 | bellard | struct sockaddr_in saddr;
|
5035 | 7c9d8e07 | bellard | |
5036 | 7c9d8e07 | bellard | if (parse_host_port(&saddr, host_str) < 0) |
5037 | 7c9d8e07 | bellard | return -1; |
5038 | 7c9d8e07 | bellard | |
5039 | 7c9d8e07 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
5040 | 7c9d8e07 | bellard | if (fd < 0) { |
5041 | 7c9d8e07 | bellard | perror("socket");
|
5042 | 7c9d8e07 | bellard | return -1; |
5043 | 7c9d8e07 | bellard | } |
5044 | fd1dff4b | bellard | socket_set_nonblock(fd); |
5045 | 7c9d8e07 | bellard | |
5046 | 7c9d8e07 | bellard | connected = 0;
|
5047 | 7c9d8e07 | bellard | for(;;) {
|
5048 | 7c9d8e07 | bellard | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
5049 | 7c9d8e07 | bellard | if (ret < 0) { |
5050 | fd1dff4b | bellard | err = socket_error(); |
5051 | fd1dff4b | bellard | if (err == EINTR || err == EWOULDBLOCK) {
|
5052 | fd1dff4b | bellard | } else if (err == EINPROGRESS) { |
5053 | 7c9d8e07 | bellard | break;
|
5054 | f5b12268 | ths | #ifdef _WIN32
|
5055 | f5b12268 | ths | } else if (err == WSAEALREADY) { |
5056 | f5b12268 | ths | break;
|
5057 | f5b12268 | ths | #endif
|
5058 | 7c9d8e07 | bellard | } else {
|
5059 | 7c9d8e07 | bellard | perror("connect");
|
5060 | fd1dff4b | bellard | closesocket(fd); |
5061 | 7c9d8e07 | bellard | return -1; |
5062 | 7c9d8e07 | bellard | } |
5063 | 7c9d8e07 | bellard | } else {
|
5064 | 7c9d8e07 | bellard | connected = 1;
|
5065 | 7c9d8e07 | bellard | break;
|
5066 | 7c9d8e07 | bellard | } |
5067 | 7c9d8e07 | bellard | } |
5068 | 7c9d8e07 | bellard | s = net_socket_fd_init(vlan, fd, connected); |
5069 | 7c9d8e07 | bellard | if (!s)
|
5070 | 7c9d8e07 | bellard | return -1; |
5071 | 7c9d8e07 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
5072 | 5fafdf24 | ths | "socket: connect to %s:%d",
|
5073 | 7c9d8e07 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
5074 | c20709aa | bellard | return 0; |
5075 | 80cabfad | bellard | } |
5076 | 330d0414 | bellard | |
5077 | 3d830459 | bellard | static int net_socket_mcast_init(VLANState *vlan, const char *host_str) |
5078 | 3d830459 | bellard | { |
5079 | 3d830459 | bellard | NetSocketState *s; |
5080 | 3d830459 | bellard | int fd;
|
5081 | 3d830459 | bellard | struct sockaddr_in saddr;
|
5082 | 3d830459 | bellard | |
5083 | 3d830459 | bellard | if (parse_host_port(&saddr, host_str) < 0) |
5084 | 3d830459 | bellard | return -1; |
5085 | 3d830459 | bellard | |
5086 | 3d830459 | bellard | |
5087 | 3d830459 | bellard | fd = net_socket_mcast_create(&saddr); |
5088 | 3d830459 | bellard | if (fd < 0) |
5089 | 3d830459 | bellard | return -1; |
5090 | 3d830459 | bellard | |
5091 | 3d830459 | bellard | s = net_socket_fd_init(vlan, fd, 0);
|
5092 | 3d830459 | bellard | if (!s)
|
5093 | 3d830459 | bellard | return -1; |
5094 | 3d830459 | bellard | |
5095 | 3d830459 | bellard | s->dgram_dst = saddr; |
5096 | 3b46e624 | ths | |
5097 | 3d830459 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
5098 | 5fafdf24 | ths | "socket: mcast=%s:%d",
|
5099 | 3d830459 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
5100 | 3d830459 | bellard | return 0; |
5101 | 3d830459 | bellard | |
5102 | 3d830459 | bellard | } |
5103 | 3d830459 | bellard | |
5104 | 609497ab | balrog | static const char *get_opt_name(char *buf, int buf_size, const char *p) |
5105 | 609497ab | balrog | { |
5106 | 609497ab | balrog | char *q;
|
5107 | 609497ab | balrog | |
5108 | 609497ab | balrog | q = buf; |
5109 | 609497ab | balrog | while (*p != '\0' && *p != '=') { |
5110 | 609497ab | balrog | if (q && (q - buf) < buf_size - 1) |
5111 | 609497ab | balrog | *q++ = *p; |
5112 | 609497ab | balrog | p++; |
5113 | 609497ab | balrog | } |
5114 | 609497ab | balrog | if (q)
|
5115 | 609497ab | balrog | *q = '\0';
|
5116 | 609497ab | balrog | |
5117 | 609497ab | balrog | return p;
|
5118 | 609497ab | balrog | } |
5119 | 609497ab | balrog | |
5120 | 609497ab | balrog | static const char *get_opt_value(char *buf, int buf_size, const char *p) |
5121 | e4bcb14c | ths | { |
5122 | e4bcb14c | ths | char *q;
|
5123 | e4bcb14c | ths | |
5124 | e4bcb14c | ths | q = buf; |
5125 | e4bcb14c | ths | while (*p != '\0') { |
5126 | 609497ab | balrog | if (*p == ',') { |
5127 | 609497ab | balrog | if (*(p + 1) != ',') |
5128 | e4bcb14c | ths | break;
|
5129 | e4bcb14c | ths | p++; |
5130 | 609497ab | balrog | } |
5131 | e4bcb14c | ths | if (q && (q - buf) < buf_size - 1) |
5132 | e4bcb14c | ths | *q++ = *p; |
5133 | e4bcb14c | ths | p++; |
5134 | e4bcb14c | ths | } |
5135 | e4bcb14c | ths | if (q)
|
5136 | e4bcb14c | ths | *q = '\0';
|
5137 | e4bcb14c | ths | |
5138 | e4bcb14c | ths | return p;
|
5139 | e4bcb14c | ths | } |
5140 | e4bcb14c | ths | |
5141 | 7c9d8e07 | bellard | static int get_param_value(char *buf, int buf_size, |
5142 | 7c9d8e07 | bellard | const char *tag, const char *str) |
5143 | 7c9d8e07 | bellard | { |
5144 | 7c9d8e07 | bellard | const char *p; |
5145 | 7c9d8e07 | bellard | char option[128]; |
5146 | 7c9d8e07 | bellard | |
5147 | 7c9d8e07 | bellard | p = str; |
5148 | 7c9d8e07 | bellard | for(;;) {
|
5149 | 609497ab | balrog | p = get_opt_name(option, sizeof(option), p);
|
5150 | 7c9d8e07 | bellard | if (*p != '=') |
5151 | 7c9d8e07 | bellard | break;
|
5152 | 7c9d8e07 | bellard | p++; |
5153 | 7c9d8e07 | bellard | if (!strcmp(tag, option)) {
|
5154 | 609497ab | balrog | (void)get_opt_value(buf, buf_size, p);
|
5155 | e4bcb14c | ths | return strlen(buf);
|
5156 | 7c9d8e07 | bellard | } else {
|
5157 | 609497ab | balrog | p = get_opt_value(NULL, 0, p); |
5158 | 7c9d8e07 | bellard | } |
5159 | 7c9d8e07 | bellard | if (*p != ',') |
5160 | 7c9d8e07 | bellard | break;
|
5161 | 7c9d8e07 | bellard | p++; |
5162 | 7c9d8e07 | bellard | } |
5163 | 7c9d8e07 | bellard | return 0; |
5164 | 7c9d8e07 | bellard | } |
5165 | 7c9d8e07 | bellard | |
5166 | e4bcb14c | ths | static int check_params(char *buf, int buf_size, |
5167 | e4bcb14c | ths | char **params, const char *str) |
5168 | e4bcb14c | ths | { |
5169 | e4bcb14c | ths | const char *p; |
5170 | e4bcb14c | ths | int i;
|
5171 | e4bcb14c | ths | |
5172 | e4bcb14c | ths | p = str; |
5173 | e4bcb14c | ths | for(;;) {
|
5174 | 609497ab | balrog | p = get_opt_name(buf, buf_size, p); |
5175 | e4bcb14c | ths | if (*p != '=') |
5176 | e4bcb14c | ths | return -1; |
5177 | e4bcb14c | ths | p++; |
5178 | e4bcb14c | ths | for(i = 0; params[i] != NULL; i++) |
5179 | e4bcb14c | ths | if (!strcmp(params[i], buf))
|
5180 | e4bcb14c | ths | break;
|
5181 | e4bcb14c | ths | if (params[i] == NULL) |
5182 | e4bcb14c | ths | return -1; |
5183 | 609497ab | balrog | p = get_opt_value(NULL, 0, p); |
5184 | e4bcb14c | ths | if (*p != ',') |
5185 | e4bcb14c | ths | break;
|
5186 | e4bcb14c | ths | p++; |
5187 | e4bcb14c | ths | } |
5188 | e4bcb14c | ths | return 0; |
5189 | e4bcb14c | ths | } |
5190 | e4bcb14c | ths | |
5191 | 9ad97e65 | balrog | static int net_client_init(const char *device, const char *p) |
5192 | 7c9d8e07 | bellard | { |
5193 | 7c9d8e07 | bellard | char buf[1024]; |
5194 | 7c9d8e07 | bellard | int vlan_id, ret;
|
5195 | 7c9d8e07 | bellard | VLANState *vlan; |
5196 | 7c9d8e07 | bellard | |
5197 | 7c9d8e07 | bellard | vlan_id = 0;
|
5198 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "vlan", p)) { |
5199 | 7c9d8e07 | bellard | vlan_id = strtol(buf, NULL, 0); |
5200 | 7c9d8e07 | bellard | } |
5201 | 7c9d8e07 | bellard | vlan = qemu_find_vlan(vlan_id); |
5202 | 7c9d8e07 | bellard | if (!vlan) {
|
5203 | 7c9d8e07 | bellard | fprintf(stderr, "Could not create vlan %d\n", vlan_id);
|
5204 | 7c9d8e07 | bellard | return -1; |
5205 | 7c9d8e07 | bellard | } |
5206 | 7c9d8e07 | bellard | if (!strcmp(device, "nic")) { |
5207 | 7c9d8e07 | bellard | NICInfo *nd; |
5208 | 7c9d8e07 | bellard | uint8_t *macaddr; |
5209 | 7c9d8e07 | bellard | |
5210 | 7c9d8e07 | bellard | if (nb_nics >= MAX_NICS) {
|
5211 | 7c9d8e07 | bellard | fprintf(stderr, "Too Many NICs\n");
|
5212 | 7c9d8e07 | bellard | return -1; |
5213 | 7c9d8e07 | bellard | } |
5214 | 7c9d8e07 | bellard | nd = &nd_table[nb_nics]; |
5215 | 7c9d8e07 | bellard | macaddr = nd->macaddr; |
5216 | 7c9d8e07 | bellard | macaddr[0] = 0x52; |
5217 | 7c9d8e07 | bellard | macaddr[1] = 0x54; |
5218 | 7c9d8e07 | bellard | macaddr[2] = 0x00; |
5219 | 7c9d8e07 | bellard | macaddr[3] = 0x12; |
5220 | 7c9d8e07 | bellard | macaddr[4] = 0x34; |
5221 | 7c9d8e07 | bellard | macaddr[5] = 0x56 + nb_nics; |
5222 | 7c9d8e07 | bellard | |
5223 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "macaddr", p)) { |
5224 | 7c9d8e07 | bellard | if (parse_macaddr(macaddr, buf) < 0) { |
5225 | 7c9d8e07 | bellard | fprintf(stderr, "invalid syntax for ethernet address\n");
|
5226 | 7c9d8e07 | bellard | return -1; |
5227 | 7c9d8e07 | bellard | } |
5228 | 7c9d8e07 | bellard | } |
5229 | a41b2ff2 | pbrook | if (get_param_value(buf, sizeof(buf), "model", p)) { |
5230 | a41b2ff2 | pbrook | nd->model = strdup(buf); |
5231 | a41b2ff2 | pbrook | } |
5232 | 7c9d8e07 | bellard | nd->vlan = vlan; |
5233 | 7c9d8e07 | bellard | nb_nics++; |
5234 | 833c7174 | blueswir1 | vlan->nb_guest_devs++; |
5235 | 7c9d8e07 | bellard | ret = 0;
|
5236 | 7c9d8e07 | bellard | } else
|
5237 | 7c9d8e07 | bellard | if (!strcmp(device, "none")) { |
5238 | 7c9d8e07 | bellard | /* does nothing. It is needed to signal that no network cards
|
5239 | 7c9d8e07 | bellard | are wanted */
|
5240 | 7c9d8e07 | bellard | ret = 0;
|
5241 | 7c9d8e07 | bellard | } else
|
5242 | 7c9d8e07 | bellard | #ifdef CONFIG_SLIRP
|
5243 | 7c9d8e07 | bellard | if (!strcmp(device, "user")) { |
5244 | 115defd1 | pbrook | if (get_param_value(buf, sizeof(buf), "hostname", p)) { |
5245 | 3f423c9c | bellard | pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
|
5246 | 115defd1 | pbrook | } |
5247 | 833c7174 | blueswir1 | vlan->nb_host_devs++; |
5248 | 7c9d8e07 | bellard | ret = net_slirp_init(vlan); |
5249 | 7c9d8e07 | bellard | } else
|
5250 | 7c9d8e07 | bellard | #endif
|
5251 | 7fb843f8 | bellard | #ifdef _WIN32
|
5252 | 7fb843f8 | bellard | if (!strcmp(device, "tap")) { |
5253 | 7fb843f8 | bellard | char ifname[64]; |
5254 | 7fb843f8 | bellard | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
5255 | 7fb843f8 | bellard | fprintf(stderr, "tap: no interface name\n");
|
5256 | 7fb843f8 | bellard | return -1; |
5257 | 7fb843f8 | bellard | } |
5258 | 833c7174 | blueswir1 | vlan->nb_host_devs++; |
5259 | 7fb843f8 | bellard | ret = tap_win32_init(vlan, ifname); |
5260 | 7fb843f8 | bellard | } else
|
5261 | 7fb843f8 | bellard | #else
|
5262 | 7c9d8e07 | bellard | if (!strcmp(device, "tap")) { |
5263 | 7c9d8e07 | bellard | char ifname[64]; |
5264 | b46a8906 | ths | char setup_script[1024], down_script[1024]; |
5265 | 7c9d8e07 | bellard | int fd;
|
5266 | 4f01035f | pbrook | vlan->nb_host_devs++; |
5267 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
5268 | 7c9d8e07 | bellard | fd = strtol(buf, NULL, 0); |
5269 | 64538cdf | pbrook | fcntl(fd, F_SETFL, O_NONBLOCK); |
5270 | 7c9d8e07 | bellard | ret = -1;
|
5271 | 7c9d8e07 | bellard | if (net_tap_fd_init(vlan, fd))
|
5272 | 7c9d8e07 | bellard | ret = 0;
|
5273 | 7c9d8e07 | bellard | } else {
|
5274 | bf8c5342 | bellard | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
5275 | bf8c5342 | bellard | ifname[0] = '\0'; |
5276 | bf8c5342 | bellard | } |
5277 | 7c9d8e07 | bellard | if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) { |
5278 | 7c9d8e07 | bellard | pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
|
5279 | 7c9d8e07 | bellard | } |
5280 | b46a8906 | ths | if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) { |
5281 | b46a8906 | ths | pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
|
5282 | b46a8906 | ths | } |
5283 | b46a8906 | ths | ret = net_tap_init(vlan, ifname, setup_script, down_script); |
5284 | 7c9d8e07 | bellard | } |
5285 | 7c9d8e07 | bellard | } else
|
5286 | fd1dff4b | bellard | #endif
|
5287 | 7c9d8e07 | bellard | if (!strcmp(device, "socket")) { |
5288 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
5289 | 7c9d8e07 | bellard | int fd;
|
5290 | 7c9d8e07 | bellard | fd = strtol(buf, NULL, 0); |
5291 | 7c9d8e07 | bellard | ret = -1;
|
5292 | 7c9d8e07 | bellard | if (net_socket_fd_init(vlan, fd, 1)) |
5293 | 7c9d8e07 | bellard | ret = 0;
|
5294 | 7c9d8e07 | bellard | } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) { |
5295 | 7c9d8e07 | bellard | ret = net_socket_listen_init(vlan, buf); |
5296 | 7c9d8e07 | bellard | } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { |
5297 | 7c9d8e07 | bellard | ret = net_socket_connect_init(vlan, buf); |
5298 | 3d830459 | bellard | } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { |
5299 | 3d830459 | bellard | ret = net_socket_mcast_init(vlan, buf); |
5300 | 7c9d8e07 | bellard | } else {
|
5301 | 7c9d8e07 | bellard | fprintf(stderr, "Unknown socket options: %s\n", p);
|
5302 | 7c9d8e07 | bellard | return -1; |
5303 | 7c9d8e07 | bellard | } |
5304 | 833c7174 | blueswir1 | vlan->nb_host_devs++; |
5305 | 7c9d8e07 | bellard | } else
|
5306 | 8a16d273 | ths | #ifdef CONFIG_VDE
|
5307 | 8a16d273 | ths | if (!strcmp(device, "vde")) { |
5308 | 8a16d273 | ths | char vde_sock[1024], vde_group[512]; |
5309 | 8a16d273 | ths | int vde_port, vde_mode;
|
5310 | 8a16d273 | ths | vlan->nb_host_devs++; |
5311 | 8a16d273 | ths | if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) { |
5312 | 8a16d273 | ths | vde_sock[0] = '\0'; |
5313 | 8a16d273 | ths | } |
5314 | 8a16d273 | ths | if (get_param_value(buf, sizeof(buf), "port", p) > 0) { |
5315 | 8a16d273 | ths | vde_port = strtol(buf, NULL, 10); |
5316 | 8a16d273 | ths | } else {
|
5317 | 8a16d273 | ths | vde_port = 0;
|
5318 | 8a16d273 | ths | } |
5319 | 8a16d273 | ths | if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) { |
5320 | 8a16d273 | ths | vde_group[0] = '\0'; |
5321 | 8a16d273 | ths | } |
5322 | 8a16d273 | ths | if (get_param_value(buf, sizeof(buf), "mode", p) > 0) { |
5323 | 8a16d273 | ths | vde_mode = strtol(buf, NULL, 8); |
5324 | 8a16d273 | ths | } else {
|
5325 | 8a16d273 | ths | vde_mode = 0700;
|
5326 | 8a16d273 | ths | } |
5327 | 8a16d273 | ths | ret = net_vde_init(vlan, vde_sock, vde_port, vde_group, vde_mode); |
5328 | 8a16d273 | ths | } else
|
5329 | 8a16d273 | ths | #endif
|
5330 | 7c9d8e07 | bellard | { |
5331 | 7c9d8e07 | bellard | fprintf(stderr, "Unknown network device: %s\n", device);
|
5332 | 7c9d8e07 | bellard | return -1; |
5333 | 7c9d8e07 | bellard | } |
5334 | 7c9d8e07 | bellard | if (ret < 0) { |
5335 | 7c9d8e07 | bellard | fprintf(stderr, "Could not initialize device '%s'\n", device);
|
5336 | 7c9d8e07 | bellard | } |
5337 | 3b46e624 | ths | |
5338 | 7c9d8e07 | bellard | return ret;
|
5339 | 7c9d8e07 | bellard | } |
5340 | 7c9d8e07 | bellard | |
5341 | 9ad97e65 | balrog | static int net_client_parse(const char *str) |
5342 | 9ad97e65 | balrog | { |
5343 | 9ad97e65 | balrog | const char *p; |
5344 | 9ad97e65 | balrog | char *q;
|
5345 | 9ad97e65 | balrog | char device[64]; |
5346 | 9ad97e65 | balrog | |
5347 | 9ad97e65 | balrog | p = str; |
5348 | 9ad97e65 | balrog | q = device; |
5349 | 9ad97e65 | balrog | while (*p != '\0' && *p != ',') { |
5350 | 9ad97e65 | balrog | if ((q - device) < sizeof(device) - 1) |
5351 | 9ad97e65 | balrog | *q++ = *p; |
5352 | 9ad97e65 | balrog | p++; |
5353 | 9ad97e65 | balrog | } |
5354 | 9ad97e65 | balrog | *q = '\0';
|
5355 | 9ad97e65 | balrog | if (*p == ',') |
5356 | 9ad97e65 | balrog | p++; |
5357 | 9ad97e65 | balrog | |
5358 | 9ad97e65 | balrog | return net_client_init(device, p);
|
5359 | 9ad97e65 | balrog | } |
5360 | 9ad97e65 | balrog | |
5361 | 7c9d8e07 | bellard | void do_info_network(void) |
5362 | 7c9d8e07 | bellard | { |
5363 | 7c9d8e07 | bellard | VLANState *vlan; |
5364 | 7c9d8e07 | bellard | VLANClientState *vc; |
5365 | 7c9d8e07 | bellard | |
5366 | 7c9d8e07 | bellard | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
5367 | 7c9d8e07 | bellard | term_printf("VLAN %d devices:\n", vlan->id);
|
5368 | 7c9d8e07 | bellard | for(vc = vlan->first_client; vc != NULL; vc = vc->next) |
5369 | 7c9d8e07 | bellard | term_printf(" %s\n", vc->info_str);
|
5370 | 7c9d8e07 | bellard | } |
5371 | 7c9d8e07 | bellard | } |
5372 | 42550fde | ths | |
5373 | 609497ab | balrog | #define HD_ALIAS "index=%d,media=disk" |
5374 | e4bcb14c | ths | #ifdef TARGET_PPC
|
5375 | e4bcb14c | ths | #define CDROM_ALIAS "index=1,media=cdrom" |
5376 | e4bcb14c | ths | #else
|
5377 | e4bcb14c | ths | #define CDROM_ALIAS "index=2,media=cdrom" |
5378 | e4bcb14c | ths | #endif
|
5379 | e4bcb14c | ths | #define FD_ALIAS "index=%d,if=floppy" |
5380 | 609497ab | balrog | #define PFLASH_ALIAS "if=pflash" |
5381 | 609497ab | balrog | #define MTD_ALIAS "if=mtd" |
5382 | 9d413d1d | balrog | #define SD_ALIAS "index=0,if=sd" |
5383 | e4bcb14c | ths | |
5384 | 609497ab | balrog | static int drive_add(const char *file, const char *fmt, ...) |
5385 | e4bcb14c | ths | { |
5386 | e4bcb14c | ths | va_list ap; |
5387 | e4bcb14c | ths | |
5388 | e4bcb14c | ths | if (nb_drives_opt >= MAX_DRIVES) {
|
5389 | e4bcb14c | ths | fprintf(stderr, "qemu: too many drives\n");
|
5390 | e4bcb14c | ths | exit(1);
|
5391 | e4bcb14c | ths | } |
5392 | e4bcb14c | ths | |
5393 | 609497ab | balrog | drives_opt[nb_drives_opt].file = file; |
5394 | e4bcb14c | ths | va_start(ap, fmt); |
5395 | 609497ab | balrog | vsnprintf(drives_opt[nb_drives_opt].opt, |
5396 | 609497ab | balrog | sizeof(drives_opt[0].opt), fmt, ap); |
5397 | e4bcb14c | ths | va_end(ap); |
5398 | e4bcb14c | ths | |
5399 | e4bcb14c | ths | return nb_drives_opt++;
|
5400 | e4bcb14c | ths | } |
5401 | e4bcb14c | ths | |
5402 | f60d39bc | ths | int drive_get_index(BlockInterfaceType type, int bus, int unit) |
5403 | e4bcb14c | ths | { |
5404 | e4bcb14c | ths | int index;
|
5405 | e4bcb14c | ths | |
5406 | e4bcb14c | ths | /* seek interface, bus and unit */
|
5407 | e4bcb14c | ths | |
5408 | e4bcb14c | ths | for (index = 0; index < nb_drives; index++) |
5409 | f60d39bc | ths | if (drives_table[index].type == type &&
|
5410 | e4bcb14c | ths | drives_table[index].bus == bus && |
5411 | e4bcb14c | ths | drives_table[index].unit == unit) |
5412 | e4bcb14c | ths | return index;
|
5413 | e4bcb14c | ths | |
5414 | e4bcb14c | ths | return -1; |
5415 | e4bcb14c | ths | } |
5416 | e4bcb14c | ths | |
5417 | f60d39bc | ths | int drive_get_max_bus(BlockInterfaceType type)
|
5418 | e4bcb14c | ths | { |
5419 | e4bcb14c | ths | int max_bus;
|
5420 | e4bcb14c | ths | int index;
|
5421 | e4bcb14c | ths | |
5422 | e4bcb14c | ths | max_bus = -1;
|
5423 | e4bcb14c | ths | for (index = 0; index < nb_drives; index++) { |
5424 | f60d39bc | ths | if(drives_table[index].type == type &&
|
5425 | e4bcb14c | ths | drives_table[index].bus > max_bus) |
5426 | e4bcb14c | ths | max_bus = drives_table[index].bus; |
5427 | e4bcb14c | ths | } |
5428 | e4bcb14c | ths | return max_bus;
|
5429 | e4bcb14c | ths | } |
5430 | e4bcb14c | ths | |
5431 | a1620fac | aurel32 | static void bdrv_format_print(void *opaque, const char *name) |
5432 | a1620fac | aurel32 | { |
5433 | a1620fac | aurel32 | fprintf(stderr, " %s", name);
|
5434 | a1620fac | aurel32 | } |
5435 | a1620fac | aurel32 | |
5436 | 609497ab | balrog | static int drive_init(struct drive_opt *arg, int snapshot, |
5437 | 609497ab | balrog | QEMUMachine *machine) |
5438 | e4bcb14c | ths | { |
5439 | e4bcb14c | ths | char buf[128]; |
5440 | e4bcb14c | ths | char file[1024]; |
5441 | c8522bdf | balrog | char devname[128]; |
5442 | c8522bdf | balrog | const char *mediastr = ""; |
5443 | f60d39bc | ths | BlockInterfaceType type; |
5444 | e4bcb14c | ths | enum { MEDIA_DISK, MEDIA_CDROM } media;
|
5445 | e4bcb14c | ths | int bus_id, unit_id;
|
5446 | e4bcb14c | ths | int cyls, heads, secs, translation;
|
5447 | e4bcb14c | ths | BlockDriverState *bdrv; |
5448 | 1e72d3b7 | aurel32 | BlockDriver *drv = NULL;
|
5449 | e4bcb14c | ths | int max_devs;
|
5450 | e4bcb14c | ths | int index;
|
5451 | 33f00271 | balrog | int cache;
|
5452 | 33f00271 | balrog | int bdrv_flags;
|
5453 | 609497ab | balrog | char *str = arg->opt;
|
5454 | e4bcb14c | ths | char *params[] = { "bus", "unit", "if", "index", "cyls", "heads", |
5455 | 33f00271 | balrog | "secs", "trans", "media", "snapshot", "file", |
5456 | 1e72d3b7 | aurel32 | "cache", "format", NULL }; |
5457 | e4bcb14c | ths | |
5458 | e4bcb14c | ths | if (check_params(buf, sizeof(buf), params, str) < 0) { |
5459 | ff993638 | balrog | fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
|
5460 | e4bcb14c | ths | buf, str); |
5461 | e4bcb14c | ths | return -1; |
5462 | e4bcb14c | ths | } |
5463 | e4bcb14c | ths | |
5464 | e4bcb14c | ths | file[0] = 0; |
5465 | e4bcb14c | ths | cyls = heads = secs = 0;
|
5466 | e4bcb14c | ths | bus_id = 0;
|
5467 | e4bcb14c | ths | unit_id = -1;
|
5468 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_AUTO; |
5469 | e4bcb14c | ths | index = -1;
|
5470 | 33f00271 | balrog | cache = 1;
|
5471 | e4bcb14c | ths | |
5472 | e4bcb14c | ths | if (!strcmp(machine->name, "realview") || |
5473 | e4bcb14c | ths | !strcmp(machine->name, "SS-5") ||
|
5474 | e4bcb14c | ths | !strcmp(machine->name, "SS-10") ||
|
5475 | e4bcb14c | ths | !strcmp(machine->name, "SS-600MP") ||
|
5476 | e4bcb14c | ths | !strcmp(machine->name, "versatilepb") ||
|
5477 | e4bcb14c | ths | !strcmp(machine->name, "versatileab")) {
|
5478 | f60d39bc | ths | type = IF_SCSI; |
5479 | e4bcb14c | ths | max_devs = MAX_SCSI_DEVS; |
5480 | 363a37d5 | blueswir1 | pstrcpy(devname, sizeof(devname), "scsi"); |
5481 | e4bcb14c | ths | } else {
|
5482 | f60d39bc | ths | type = IF_IDE; |
5483 | e4bcb14c | ths | max_devs = MAX_IDE_DEVS; |
5484 | 363a37d5 | blueswir1 | pstrcpy(devname, sizeof(devname), "ide"); |
5485 | e4bcb14c | ths | } |
5486 | e4bcb14c | ths | media = MEDIA_DISK; |
5487 | e4bcb14c | ths | |
5488 | e4bcb14c | ths | /* extract parameters */
|
5489 | e4bcb14c | ths | |
5490 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "bus", str)) { |
5491 | e4bcb14c | ths | bus_id = strtol(buf, NULL, 0); |
5492 | e4bcb14c | ths | if (bus_id < 0) { |
5493 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
|
5494 | e4bcb14c | ths | return -1; |
5495 | e4bcb14c | ths | } |
5496 | e4bcb14c | ths | } |
5497 | e4bcb14c | ths | |
5498 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "unit", str)) { |
5499 | e4bcb14c | ths | unit_id = strtol(buf, NULL, 0); |
5500 | e4bcb14c | ths | if (unit_id < 0) { |
5501 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
|
5502 | e4bcb14c | ths | return -1; |
5503 | e4bcb14c | ths | } |
5504 | e4bcb14c | ths | } |
5505 | e4bcb14c | ths | |
5506 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "if", str)) { |
5507 | ae45d369 | bellard | pstrcpy(devname, sizeof(devname), buf);
|
5508 | e4bcb14c | ths | if (!strcmp(buf, "ide")) { |
5509 | f60d39bc | ths | type = IF_IDE; |
5510 | e4bcb14c | ths | max_devs = MAX_IDE_DEVS; |
5511 | e4bcb14c | ths | } else if (!strcmp(buf, "scsi")) { |
5512 | f60d39bc | ths | type = IF_SCSI; |
5513 | e4bcb14c | ths | max_devs = MAX_SCSI_DEVS; |
5514 | e4bcb14c | ths | } else if (!strcmp(buf, "floppy")) { |
5515 | f60d39bc | ths | type = IF_FLOPPY; |
5516 | e4bcb14c | ths | max_devs = 0;
|
5517 | e4bcb14c | ths | } else if (!strcmp(buf, "pflash")) { |
5518 | f60d39bc | ths | type = IF_PFLASH; |
5519 | e4bcb14c | ths | max_devs = 0;
|
5520 | e4bcb14c | ths | } else if (!strcmp(buf, "mtd")) { |
5521 | f60d39bc | ths | type = IF_MTD; |
5522 | e4bcb14c | ths | max_devs = 0;
|
5523 | e4bcb14c | ths | } else if (!strcmp(buf, "sd")) { |
5524 | f60d39bc | ths | type = IF_SD; |
5525 | e4bcb14c | ths | max_devs = 0;
|
5526 | e4bcb14c | ths | } else {
|
5527 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
|
5528 | e4bcb14c | ths | return -1; |
5529 | e4bcb14c | ths | } |
5530 | e4bcb14c | ths | } |
5531 | e4bcb14c | ths | |
5532 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "index", str)) { |
5533 | e4bcb14c | ths | index = strtol(buf, NULL, 0); |
5534 | e4bcb14c | ths | if (index < 0) { |
5535 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid index\n", str);
|
5536 | e4bcb14c | ths | return -1; |
5537 | e4bcb14c | ths | } |
5538 | e4bcb14c | ths | } |
5539 | e4bcb14c | ths | |
5540 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "cyls", str)) { |
5541 | e4bcb14c | ths | cyls = strtol(buf, NULL, 0); |
5542 | e4bcb14c | ths | } |
5543 | e4bcb14c | ths | |
5544 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "heads", str)) { |
5545 | e4bcb14c | ths | heads = strtol(buf, NULL, 0); |
5546 | e4bcb14c | ths | } |
5547 | e4bcb14c | ths | |
5548 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "secs", str)) { |
5549 | e4bcb14c | ths | secs = strtol(buf, NULL, 0); |
5550 | e4bcb14c | ths | } |
5551 | e4bcb14c | ths | |
5552 | e4bcb14c | ths | if (cyls || heads || secs) {
|
5553 | e4bcb14c | ths | if (cyls < 1 || cyls > 16383) { |
5554 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
|
5555 | e4bcb14c | ths | return -1; |
5556 | e4bcb14c | ths | } |
5557 | e4bcb14c | ths | if (heads < 1 || heads > 16) { |
5558 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
|
5559 | e4bcb14c | ths | return -1; |
5560 | e4bcb14c | ths | } |
5561 | e4bcb14c | ths | if (secs < 1 || secs > 63) { |
5562 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
|
5563 | e4bcb14c | ths | return -1; |
5564 | e4bcb14c | ths | } |
5565 | e4bcb14c | ths | } |
5566 | e4bcb14c | ths | |
5567 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "trans", str)) { |
5568 | e4bcb14c | ths | if (!cyls) {
|
5569 | e4bcb14c | ths | fprintf(stderr, |
5570 | e4bcb14c | ths | "qemu: '%s' trans must be used with cyls,heads and secs\n",
|
5571 | e4bcb14c | ths | str); |
5572 | e4bcb14c | ths | return -1; |
5573 | e4bcb14c | ths | } |
5574 | e4bcb14c | ths | if (!strcmp(buf, "none")) |
5575 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_NONE; |
5576 | e4bcb14c | ths | else if (!strcmp(buf, "lba")) |
5577 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_LBA; |
5578 | e4bcb14c | ths | else if (!strcmp(buf, "auto")) |
5579 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_AUTO; |
5580 | e4bcb14c | ths | else {
|
5581 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
|
5582 | e4bcb14c | ths | return -1; |
5583 | e4bcb14c | ths | } |
5584 | e4bcb14c | ths | } |
5585 | e4bcb14c | ths | |
5586 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "media", str)) { |
5587 | e4bcb14c | ths | if (!strcmp(buf, "disk")) { |
5588 | e4bcb14c | ths | media = MEDIA_DISK; |
5589 | e4bcb14c | ths | } else if (!strcmp(buf, "cdrom")) { |
5590 | e4bcb14c | ths | if (cyls || secs || heads) {
|
5591 | e4bcb14c | ths | fprintf(stderr, |
5592 | e4bcb14c | ths | "qemu: '%s' invalid physical CHS format\n", str);
|
5593 | e4bcb14c | ths | return -1; |
5594 | e4bcb14c | ths | } |
5595 | e4bcb14c | ths | media = MEDIA_CDROM; |
5596 | e4bcb14c | ths | } else {
|
5597 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid media\n", str);
|
5598 | e4bcb14c | ths | return -1; |
5599 | e4bcb14c | ths | } |
5600 | e4bcb14c | ths | } |
5601 | e4bcb14c | ths | |
5602 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "snapshot", str)) { |
5603 | e4bcb14c | ths | if (!strcmp(buf, "on")) |
5604 | e4bcb14c | ths | snapshot = 1;
|
5605 | e4bcb14c | ths | else if (!strcmp(buf, "off")) |
5606 | e4bcb14c | ths | snapshot = 0;
|
5607 | e4bcb14c | ths | else {
|
5608 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
|
5609 | e4bcb14c | ths | return -1; |
5610 | e4bcb14c | ths | } |
5611 | e4bcb14c | ths | } |
5612 | e4bcb14c | ths | |
5613 | 33f00271 | balrog | if (get_param_value(buf, sizeof(buf), "cache", str)) { |
5614 | 33f00271 | balrog | if (!strcmp(buf, "off")) |
5615 | 33f00271 | balrog | cache = 0;
|
5616 | 33f00271 | balrog | else if (!strcmp(buf, "on")) |
5617 | 33f00271 | balrog | cache = 1;
|
5618 | 33f00271 | balrog | else {
|
5619 | 33f00271 | balrog | fprintf(stderr, "qemu: invalid cache option\n");
|
5620 | 33f00271 | balrog | return -1; |
5621 | 33f00271 | balrog | } |
5622 | 33f00271 | balrog | } |
5623 | 33f00271 | balrog | |
5624 | 1e72d3b7 | aurel32 | if (get_param_value(buf, sizeof(buf), "format", str)) { |
5625 | a1620fac | aurel32 | if (strcmp(buf, "?") == 0) { |
5626 | a1620fac | aurel32 | fprintf(stderr, "qemu: Supported formats:");
|
5627 | a1620fac | aurel32 | bdrv_iterate_format(bdrv_format_print, NULL);
|
5628 | a1620fac | aurel32 | fprintf(stderr, "\n");
|
5629 | a1620fac | aurel32 | return -1; |
5630 | a1620fac | aurel32 | } |
5631 | 1e72d3b7 | aurel32 | drv = bdrv_find_format(buf); |
5632 | 1e72d3b7 | aurel32 | if (!drv) {
|
5633 | 1e72d3b7 | aurel32 | fprintf(stderr, "qemu: '%s' invalid format\n", buf);
|
5634 | 1e72d3b7 | aurel32 | return -1; |
5635 | 1e72d3b7 | aurel32 | } |
5636 | 1e72d3b7 | aurel32 | } |
5637 | 1e72d3b7 | aurel32 | |
5638 | 609497ab | balrog | if (arg->file == NULL) |
5639 | 609497ab | balrog | get_param_value(file, sizeof(file), "file", str); |
5640 | 609497ab | balrog | else
|
5641 | 609497ab | balrog | pstrcpy(file, sizeof(file), arg->file);
|
5642 | e4bcb14c | ths | |
5643 | e4bcb14c | ths | /* compute bus and unit according index */
|
5644 | e4bcb14c | ths | |
5645 | e4bcb14c | ths | if (index != -1) { |
5646 | e4bcb14c | ths | if (bus_id != 0 || unit_id != -1) { |
5647 | e4bcb14c | ths | fprintf(stderr, |
5648 | e4bcb14c | ths | "qemu: '%s' index cannot be used with bus and unit\n", str);
|
5649 | e4bcb14c | ths | return -1; |
5650 | e4bcb14c | ths | } |
5651 | e4bcb14c | ths | if (max_devs == 0) |
5652 | e4bcb14c | ths | { |
5653 | e4bcb14c | ths | unit_id = index; |
5654 | e4bcb14c | ths | bus_id = 0;
|
5655 | e4bcb14c | ths | } else {
|
5656 | e4bcb14c | ths | unit_id = index % max_devs; |
5657 | e4bcb14c | ths | bus_id = index / max_devs; |
5658 | e4bcb14c | ths | } |
5659 | e4bcb14c | ths | } |
5660 | e4bcb14c | ths | |
5661 | e4bcb14c | ths | /* if user doesn't specify a unit_id,
|
5662 | e4bcb14c | ths | * try to find the first free
|
5663 | e4bcb14c | ths | */
|
5664 | e4bcb14c | ths | |
5665 | e4bcb14c | ths | if (unit_id == -1) { |
5666 | e4bcb14c | ths | unit_id = 0;
|
5667 | f60d39bc | ths | while (drive_get_index(type, bus_id, unit_id) != -1) { |
5668 | e4bcb14c | ths | unit_id++; |
5669 | e4bcb14c | ths | if (max_devs && unit_id >= max_devs) {
|
5670 | e4bcb14c | ths | unit_id -= max_devs; |
5671 | e4bcb14c | ths | bus_id++; |
5672 | e4bcb14c | ths | } |
5673 | e4bcb14c | ths | } |
5674 | e4bcb14c | ths | } |
5675 | e4bcb14c | ths | |
5676 | e4bcb14c | ths | /* check unit id */
|
5677 | e4bcb14c | ths | |
5678 | e4bcb14c | ths | if (max_devs && unit_id >= max_devs) {
|
5679 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
|
5680 | e4bcb14c | ths | str, unit_id, max_devs - 1);
|
5681 | e4bcb14c | ths | return -1; |
5682 | e4bcb14c | ths | } |
5683 | e4bcb14c | ths | |
5684 | e4bcb14c | ths | /*
|
5685 | e4bcb14c | ths | * ignore multiple definitions
|
5686 | e4bcb14c | ths | */
|
5687 | e4bcb14c | ths | |
5688 | f60d39bc | ths | if (drive_get_index(type, bus_id, unit_id) != -1) |
5689 | e4bcb14c | ths | return 0; |
5690 | e4bcb14c | ths | |
5691 | e4bcb14c | ths | /* init */
|
5692 | e4bcb14c | ths | |
5693 | f60d39bc | ths | if (type == IF_IDE || type == IF_SCSI)
|
5694 | c8522bdf | balrog | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
5695 | e6198a70 | balrog | if (max_devs)
|
5696 | e6198a70 | balrog | snprintf(buf, sizeof(buf), "%s%i%s%i", |
5697 | e6198a70 | balrog | devname, bus_id, mediastr, unit_id); |
5698 | e6198a70 | balrog | else
|
5699 | e6198a70 | balrog | snprintf(buf, sizeof(buf), "%s%s%i", |
5700 | e6198a70 | balrog | devname, mediastr, unit_id); |
5701 | e4bcb14c | ths | bdrv = bdrv_new(buf); |
5702 | e4bcb14c | ths | drives_table[nb_drives].bdrv = bdrv; |
5703 | f60d39bc | ths | drives_table[nb_drives].type = type; |
5704 | e4bcb14c | ths | drives_table[nb_drives].bus = bus_id; |
5705 | e4bcb14c | ths | drives_table[nb_drives].unit = unit_id; |
5706 | e4bcb14c | ths | nb_drives++; |
5707 | e4bcb14c | ths | |
5708 | f60d39bc | ths | switch(type) {
|
5709 | e4bcb14c | ths | case IF_IDE:
|
5710 | e4bcb14c | ths | case IF_SCSI:
|
5711 | e4bcb14c | ths | switch(media) {
|
5712 | e4bcb14c | ths | case MEDIA_DISK:
|
5713 | e4bcb14c | ths | if (cyls != 0) { |
5714 | e4bcb14c | ths | bdrv_set_geometry_hint(bdrv, cyls, heads, secs); |
5715 | e4bcb14c | ths | bdrv_set_translation_hint(bdrv, translation); |
5716 | e4bcb14c | ths | } |
5717 | e4bcb14c | ths | break;
|
5718 | e4bcb14c | ths | case MEDIA_CDROM:
|
5719 | e4bcb14c | ths | bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM); |
5720 | e4bcb14c | ths | break;
|
5721 | e4bcb14c | ths | } |
5722 | e4bcb14c | ths | break;
|
5723 | e4bcb14c | ths | case IF_SD:
|
5724 | e4bcb14c | ths | /* FIXME: This isn't really a floppy, but it's a reasonable
|
5725 | e4bcb14c | ths | approximation. */
|
5726 | e4bcb14c | ths | case IF_FLOPPY:
|
5727 | e4bcb14c | ths | bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY); |
5728 | e4bcb14c | ths | break;
|
5729 | e4bcb14c | ths | case IF_PFLASH:
|
5730 | e4bcb14c | ths | case IF_MTD:
|
5731 | e4bcb14c | ths | break;
|
5732 | e4bcb14c | ths | } |
5733 | e4bcb14c | ths | if (!file[0]) |
5734 | e4bcb14c | ths | return 0; |
5735 | 33f00271 | balrog | bdrv_flags = 0;
|
5736 | 33f00271 | balrog | if (snapshot)
|
5737 | 33f00271 | balrog | bdrv_flags |= BDRV_O_SNAPSHOT; |
5738 | 33f00271 | balrog | if (!cache)
|
5739 | 33f00271 | balrog | bdrv_flags |= BDRV_O_DIRECT; |
5740 | 83ab7950 | aliguori | if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) { |
5741 | e4bcb14c | ths | fprintf(stderr, "qemu: could not open disk image %s\n",
|
5742 | e4bcb14c | ths | file); |
5743 | e4bcb14c | ths | return -1; |
5744 | e4bcb14c | ths | } |
5745 | e4bcb14c | ths | return 0; |
5746 | e4bcb14c | ths | } |
5747 | e4bcb14c | ths | |
5748 | 330d0414 | bellard | /***********************************************************/
|
5749 | a594cfbf | bellard | /* USB devices */
|
5750 | a594cfbf | bellard | |
5751 | 0d92ed30 | pbrook | static USBPort *used_usb_ports;
|
5752 | 0d92ed30 | pbrook | static USBPort *free_usb_ports;
|
5753 | 0d92ed30 | pbrook | |
5754 | 0d92ed30 | pbrook | /* ??? Maybe change this to register a hub to keep track of the topology. */
|
5755 | 0d92ed30 | pbrook | void qemu_register_usb_port(USBPort *port, void *opaque, int index, |
5756 | 0d92ed30 | pbrook | usb_attachfn attach) |
5757 | 0d92ed30 | pbrook | { |
5758 | 0d92ed30 | pbrook | port->opaque = opaque; |
5759 | 0d92ed30 | pbrook | port->index = index; |
5760 | 0d92ed30 | pbrook | port->attach = attach; |
5761 | 0d92ed30 | pbrook | port->next = free_usb_ports; |
5762 | 0d92ed30 | pbrook | free_usb_ports = port; |
5763 | 0d92ed30 | pbrook | } |
5764 | 0d92ed30 | pbrook | |
5765 | 4b096fc9 | aliguori | int usb_device_add_dev(USBDevice *dev)
|
5766 | 4b096fc9 | aliguori | { |
5767 | 4b096fc9 | aliguori | USBPort *port; |
5768 | 4b096fc9 | aliguori | |
5769 | 4b096fc9 | aliguori | /* Find a USB port to add the device to. */
|
5770 | 4b096fc9 | aliguori | port = free_usb_ports; |
5771 | 4b096fc9 | aliguori | if (!port->next) {
|
5772 | 4b096fc9 | aliguori | USBDevice *hub; |
5773 | 4b096fc9 | aliguori | |
5774 | 4b096fc9 | aliguori | /* Create a new hub and chain it on. */
|
5775 | 4b096fc9 | aliguori | free_usb_ports = NULL;
|
5776 | 4b096fc9 | aliguori | port->next = used_usb_ports; |
5777 | 4b096fc9 | aliguori | used_usb_ports = port; |
5778 | 4b096fc9 | aliguori | |
5779 | 4b096fc9 | aliguori | hub = usb_hub_init(VM_USB_HUB_SIZE); |
5780 | 4b096fc9 | aliguori | usb_attach(port, hub); |
5781 | 4b096fc9 | aliguori | port = free_usb_ports; |
5782 | 4b096fc9 | aliguori | } |
5783 | 4b096fc9 | aliguori | |
5784 | 4b096fc9 | aliguori | free_usb_ports = port->next; |
5785 | 4b096fc9 | aliguori | port->next = used_usb_ports; |
5786 | 4b096fc9 | aliguori | used_usb_ports = port; |
5787 | 4b096fc9 | aliguori | usb_attach(port, dev); |
5788 | 4b096fc9 | aliguori | return 0; |
5789 | 4b096fc9 | aliguori | } |
5790 | 4b096fc9 | aliguori | |
5791 | a594cfbf | bellard | static int usb_device_add(const char *devname) |
5792 | a594cfbf | bellard | { |
5793 | a594cfbf | bellard | const char *p; |
5794 | a594cfbf | bellard | USBDevice *dev; |
5795 | 0d92ed30 | pbrook | USBPort *port; |
5796 | a594cfbf | bellard | |
5797 | 0d92ed30 | pbrook | if (!free_usb_ports)
|
5798 | a594cfbf | bellard | return -1; |
5799 | a594cfbf | bellard | |
5800 | a594cfbf | bellard | if (strstart(devname, "host:", &p)) { |
5801 | a594cfbf | bellard | dev = usb_host_device_open(p); |
5802 | a594cfbf | bellard | } else if (!strcmp(devname, "mouse")) { |
5803 | a594cfbf | bellard | dev = usb_mouse_init(); |
5804 | 09b26c5e | bellard | } else if (!strcmp(devname, "tablet")) { |
5805 | 47b2d338 | balrog | dev = usb_tablet_init(); |
5806 | 47b2d338 | balrog | } else if (!strcmp(devname, "keyboard")) { |
5807 | 47b2d338 | balrog | dev = usb_keyboard_init(); |
5808 | 2e5d83bb | pbrook | } else if (strstart(devname, "disk:", &p)) { |
5809 | 2e5d83bb | pbrook | dev = usb_msd_init(p); |
5810 | f6d2a316 | balrog | } else if (!strcmp(devname, "wacom-tablet")) { |
5811 | f6d2a316 | balrog | dev = usb_wacom_init(); |
5812 | a7954218 | balrog | } else if (strstart(devname, "serial:", &p)) { |
5813 | a7954218 | balrog | dev = usb_serial_init(p); |
5814 | 2e4d9fb1 | aurel32 | #ifdef CONFIG_BRLAPI
|
5815 | 2e4d9fb1 | aurel32 | } else if (!strcmp(devname, "braille")) { |
5816 | 2e4d9fb1 | aurel32 | dev = usb_baum_init(); |
5817 | 2e4d9fb1 | aurel32 | #endif
|
5818 | 6c9f886c | balrog | } else if (strstart(devname, "net:", &p)) { |
5819 | 9ad97e65 | balrog | int nic = nb_nics;
|
5820 | 6c9f886c | balrog | |
5821 | 9ad97e65 | balrog | if (net_client_init("nic", p) < 0) |
5822 | 6c9f886c | balrog | return -1; |
5823 | 9ad97e65 | balrog | nd_table[nic].model = "usb";
|
5824 | 9ad97e65 | balrog | dev = usb_net_init(&nd_table[nic]); |
5825 | a594cfbf | bellard | } else {
|
5826 | a594cfbf | bellard | return -1; |
5827 | a594cfbf | bellard | } |
5828 | 0d92ed30 | pbrook | if (!dev)
|
5829 | 0d92ed30 | pbrook | return -1; |
5830 | 0d92ed30 | pbrook | |
5831 | 4b096fc9 | aliguori | return usb_device_add_dev(dev);
|
5832 | a594cfbf | bellard | } |
5833 | a594cfbf | bellard | |
5834 | 1f3870ab | aliguori | int usb_device_del_addr(int bus_num, int addr) |
5835 | a594cfbf | bellard | { |
5836 | 0d92ed30 | pbrook | USBPort *port; |
5837 | 0d92ed30 | pbrook | USBPort **lastp; |
5838 | 059809e4 | bellard | USBDevice *dev; |
5839 | a594cfbf | bellard | |
5840 | 0d92ed30 | pbrook | if (!used_usb_ports)
|
5841 | a594cfbf | bellard | return -1; |
5842 | a594cfbf | bellard | |
5843 | a594cfbf | bellard | if (bus_num != 0) |
5844 | a594cfbf | bellard | return -1; |
5845 | 0d92ed30 | pbrook | |
5846 | 0d92ed30 | pbrook | lastp = &used_usb_ports; |
5847 | 0d92ed30 | pbrook | port = used_usb_ports; |
5848 | 0d92ed30 | pbrook | while (port && port->dev->addr != addr) {
|
5849 | 0d92ed30 | pbrook | lastp = &port->next; |
5850 | 0d92ed30 | pbrook | port = port->next; |
5851 | a594cfbf | bellard | } |
5852 | 0d92ed30 | pbrook | |
5853 | 0d92ed30 | pbrook | if (!port)
|
5854 | a594cfbf | bellard | return -1; |
5855 | 0d92ed30 | pbrook | |
5856 | 059809e4 | bellard | dev = port->dev; |
5857 | 0d92ed30 | pbrook | *lastp = port->next; |
5858 | 0d92ed30 | pbrook | usb_attach(port, NULL);
|
5859 | 059809e4 | bellard | dev->handle_destroy(dev); |
5860 | 0d92ed30 | pbrook | port->next = free_usb_ports; |
5861 | 0d92ed30 | pbrook | free_usb_ports = port; |
5862 | a594cfbf | bellard | return 0; |
5863 | a594cfbf | bellard | } |
5864 | a594cfbf | bellard | |
5865 | 1f3870ab | aliguori | static int usb_device_del(const char *devname) |
5866 | 1f3870ab | aliguori | { |
5867 | 1f3870ab | aliguori | int bus_num, addr;
|
5868 | 1f3870ab | aliguori | const char *p; |
5869 | 1f3870ab | aliguori | |
5870 | 1f3870ab | aliguori | if (!used_usb_ports)
|
5871 | 1f3870ab | aliguori | return -1; |
5872 | 1f3870ab | aliguori | |
5873 | 1f3870ab | aliguori | p = strchr(devname, '.');
|
5874 | 1f3870ab | aliguori | if (!p)
|
5875 | 1f3870ab | aliguori | return -1; |
5876 | 1f3870ab | aliguori | bus_num = strtoul(devname, NULL, 0); |
5877 | 1f3870ab | aliguori | addr = strtoul(p + 1, NULL, 0); |
5878 | 1f3870ab | aliguori | |
5879 | 1f3870ab | aliguori | return usb_device_del_addr(bus_num, addr);
|
5880 | 1f3870ab | aliguori | } |
5881 | 1f3870ab | aliguori | |
5882 | a594cfbf | bellard | void do_usb_add(const char *devname) |
5883 | a594cfbf | bellard | { |
5884 | 4b096fc9 | aliguori | usb_device_add(devname); |
5885 | a594cfbf | bellard | } |
5886 | a594cfbf | bellard | |
5887 | a594cfbf | bellard | void do_usb_del(const char *devname) |
5888 | a594cfbf | bellard | { |
5889 | 4b096fc9 | aliguori | usb_device_del(devname); |
5890 | a594cfbf | bellard | } |
5891 | a594cfbf | bellard | |
5892 | a594cfbf | bellard | void usb_info(void) |
5893 | a594cfbf | bellard | { |
5894 | a594cfbf | bellard | USBDevice *dev; |
5895 | 0d92ed30 | pbrook | USBPort *port; |
5896 | a594cfbf | bellard | const char *speed_str; |
5897 | a594cfbf | bellard | |
5898 | 0d92ed30 | pbrook | if (!usb_enabled) {
|
5899 | a594cfbf | bellard | term_printf("USB support not enabled\n");
|
5900 | a594cfbf | bellard | return;
|
5901 | a594cfbf | bellard | } |
5902 | a594cfbf | bellard | |
5903 | 0d92ed30 | pbrook | for (port = used_usb_ports; port; port = port->next) {
|
5904 | 0d92ed30 | pbrook | dev = port->dev; |
5905 | 0d92ed30 | pbrook | if (!dev)
|
5906 | 0d92ed30 | pbrook | continue;
|
5907 | 0d92ed30 | pbrook | switch(dev->speed) {
|
5908 | 5fafdf24 | ths | case USB_SPEED_LOW:
|
5909 | 5fafdf24 | ths | speed_str = "1.5";
|
5910 | 0d92ed30 | pbrook | break;
|
5911 | 5fafdf24 | ths | case USB_SPEED_FULL:
|
5912 | 5fafdf24 | ths | speed_str = "12";
|
5913 | 0d92ed30 | pbrook | break;
|
5914 | 5fafdf24 | ths | case USB_SPEED_HIGH:
|
5915 | 5fafdf24 | ths | speed_str = "480";
|
5916 | 0d92ed30 | pbrook | break;
|
5917 | 0d92ed30 | pbrook | default:
|
5918 | 5fafdf24 | ths | speed_str = "?";
|
5919 | 0d92ed30 | pbrook | break;
|
5920 | a594cfbf | bellard | } |
5921 | 5fafdf24 | ths | term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
|
5922 | 1f6e24e7 | bellard | 0, dev->addr, speed_str, dev->devname);
|
5923 | a594cfbf | bellard | } |
5924 | a594cfbf | bellard | } |
5925 | a594cfbf | bellard | |
5926 | f7cce898 | bellard | /***********************************************************/
|
5927 | 201a51fc | balrog | /* PCMCIA/Cardbus */
|
5928 | 201a51fc | balrog | |
5929 | 201a51fc | balrog | static struct pcmcia_socket_entry_s { |
5930 | 201a51fc | balrog | struct pcmcia_socket_s *socket;
|
5931 | 201a51fc | balrog | struct pcmcia_socket_entry_s *next;
|
5932 | 201a51fc | balrog | } *pcmcia_sockets = 0;
|
5933 | 201a51fc | balrog | |
5934 | 201a51fc | balrog | void pcmcia_socket_register(struct pcmcia_socket_s *socket) |
5935 | 201a51fc | balrog | { |
5936 | 201a51fc | balrog | struct pcmcia_socket_entry_s *entry;
|
5937 | 201a51fc | balrog | |
5938 | 201a51fc | balrog | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); |
5939 | 201a51fc | balrog | entry->socket = socket; |
5940 | 201a51fc | balrog | entry->next = pcmcia_sockets; |
5941 | 201a51fc | balrog | pcmcia_sockets = entry; |
5942 | 201a51fc | balrog | } |
5943 | 201a51fc | balrog | |
5944 | 201a51fc | balrog | void pcmcia_socket_unregister(struct pcmcia_socket_s *socket) |
5945 | 201a51fc | balrog | { |
5946 | 201a51fc | balrog | struct pcmcia_socket_entry_s *entry, **ptr;
|
5947 | 201a51fc | balrog | |
5948 | 201a51fc | balrog | ptr = &pcmcia_sockets; |
5949 | 201a51fc | balrog | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
|
5950 | 201a51fc | balrog | if (entry->socket == socket) {
|
5951 | 201a51fc | balrog | *ptr = entry->next; |
5952 | 201a51fc | balrog | qemu_free(entry); |
5953 | 201a51fc | balrog | } |
5954 | 201a51fc | balrog | } |
5955 | 201a51fc | balrog | |
5956 | 201a51fc | balrog | void pcmcia_info(void) |
5957 | 201a51fc | balrog | { |
5958 | 201a51fc | balrog | struct pcmcia_socket_entry_s *iter;
|
5959 | 201a51fc | balrog | if (!pcmcia_sockets)
|
5960 | 201a51fc | balrog | term_printf("No PCMCIA sockets\n");
|
5961 | 201a51fc | balrog | |
5962 | 201a51fc | balrog | for (iter = pcmcia_sockets; iter; iter = iter->next)
|
5963 | 201a51fc | balrog | term_printf("%s: %s\n", iter->socket->slot_string,
|
5964 | 201a51fc | balrog | iter->socket->attached ? iter->socket->card_string : |
5965 | 201a51fc | balrog | "Empty");
|
5966 | 201a51fc | balrog | } |
5967 | 201a51fc | balrog | |
5968 | 201a51fc | balrog | /***********************************************************/
|
5969 | 2ff89790 | ths | /* dumb display */
|
5970 | 2ff89790 | ths | |
5971 | 2ff89790 | ths | static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
5972 | 2ff89790 | ths | { |
5973 | 2ff89790 | ths | } |
5974 | 2ff89790 | ths | |
5975 | 2ff89790 | ths | static void dumb_resize(DisplayState *ds, int w, int h) |
5976 | 2ff89790 | ths | { |
5977 | 2ff89790 | ths | } |
5978 | 2ff89790 | ths | |
5979 | 2ff89790 | ths | static void dumb_refresh(DisplayState *ds) |
5980 | 2ff89790 | ths | { |
5981 | 2ff89790 | ths | #if defined(CONFIG_SDL)
|
5982 | 2ff89790 | ths | vga_hw_update(); |
5983 | 2ff89790 | ths | #endif
|
5984 | 2ff89790 | ths | } |
5985 | 2ff89790 | ths | |
5986 | 2ff89790 | ths | static void dumb_display_init(DisplayState *ds) |
5987 | 2ff89790 | ths | { |
5988 | 2ff89790 | ths | ds->data = NULL;
|
5989 | 2ff89790 | ths | ds->linesize = 0;
|
5990 | 2ff89790 | ths | ds->depth = 0;
|
5991 | 2ff89790 | ths | ds->dpy_update = dumb_update; |
5992 | 2ff89790 | ths | ds->dpy_resize = dumb_resize; |
5993 | 2ff89790 | ths | ds->dpy_refresh = dumb_refresh; |
5994 | bcfad70f | aliguori | ds->gui_timer_interval = 500;
|
5995 | bcfad70f | aliguori | ds->idle = 1;
|
5996 | 2ff89790 | ths | } |
5997 | 2ff89790 | ths | |
5998 | 2ff89790 | ths | /***********************************************************/
|
5999 | 8a7ddc38 | bellard | /* I/O handling */
|
6000 | 0824d6fc | bellard | |
6001 | c4b1fcc0 | bellard | #define MAX_IO_HANDLERS 64 |
6002 | c4b1fcc0 | bellard | |
6003 | c4b1fcc0 | bellard | typedef struct IOHandlerRecord { |
6004 | c4b1fcc0 | bellard | int fd;
|
6005 | 7c9d8e07 | bellard | IOCanRWHandler *fd_read_poll; |
6006 | 7c9d8e07 | bellard | IOHandler *fd_read; |
6007 | 7c9d8e07 | bellard | IOHandler *fd_write; |
6008 | cafffd40 | ths | int deleted;
|
6009 | c4b1fcc0 | bellard | void *opaque;
|
6010 | c4b1fcc0 | bellard | /* temporary data */
|
6011 | c4b1fcc0 | bellard | struct pollfd *ufd;
|
6012 | 8a7ddc38 | bellard | struct IOHandlerRecord *next;
|
6013 | c4b1fcc0 | bellard | } IOHandlerRecord; |
6014 | c4b1fcc0 | bellard | |
6015 | 8a7ddc38 | bellard | static IOHandlerRecord *first_io_handler;
|
6016 | c4b1fcc0 | bellard | |
6017 | 7c9d8e07 | bellard | /* XXX: fd_read_poll should be suppressed, but an API change is
|
6018 | 7c9d8e07 | bellard | necessary in the character devices to suppress fd_can_read(). */
|
6019 | 5fafdf24 | ths | int qemu_set_fd_handler2(int fd, |
6020 | 5fafdf24 | ths | IOCanRWHandler *fd_read_poll, |
6021 | 5fafdf24 | ths | IOHandler *fd_read, |
6022 | 5fafdf24 | ths | IOHandler *fd_write, |
6023 | 7c9d8e07 | bellard | void *opaque)
|
6024 | c4b1fcc0 | bellard | { |
6025 | 7c9d8e07 | bellard | IOHandlerRecord **pioh, *ioh; |
6026 | c4b1fcc0 | bellard | |
6027 | 7c9d8e07 | bellard | if (!fd_read && !fd_write) {
|
6028 | 7c9d8e07 | bellard | pioh = &first_io_handler; |
6029 | 7c9d8e07 | bellard | for(;;) {
|
6030 | 7c9d8e07 | bellard | ioh = *pioh; |
6031 | 7c9d8e07 | bellard | if (ioh == NULL) |
6032 | 7c9d8e07 | bellard | break;
|
6033 | 7c9d8e07 | bellard | if (ioh->fd == fd) {
|
6034 | cafffd40 | ths | ioh->deleted = 1;
|
6035 | 7c9d8e07 | bellard | break;
|
6036 | 7c9d8e07 | bellard | } |
6037 | 7c9d8e07 | bellard | pioh = &ioh->next; |
6038 | 7c9d8e07 | bellard | } |
6039 | 7c9d8e07 | bellard | } else {
|
6040 | 7c9d8e07 | bellard | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
6041 | 7c9d8e07 | bellard | if (ioh->fd == fd)
|
6042 | 7c9d8e07 | bellard | goto found;
|
6043 | 7c9d8e07 | bellard | } |
6044 | 7c9d8e07 | bellard | ioh = qemu_mallocz(sizeof(IOHandlerRecord));
|
6045 | 7c9d8e07 | bellard | if (!ioh)
|
6046 | 7c9d8e07 | bellard | return -1; |
6047 | 7c9d8e07 | bellard | ioh->next = first_io_handler; |
6048 | 7c9d8e07 | bellard | first_io_handler = ioh; |
6049 | 7c9d8e07 | bellard | found:
|
6050 | 7c9d8e07 | bellard | ioh->fd = fd; |
6051 | 7c9d8e07 | bellard | ioh->fd_read_poll = fd_read_poll; |
6052 | 7c9d8e07 | bellard | ioh->fd_read = fd_read; |
6053 | 7c9d8e07 | bellard | ioh->fd_write = fd_write; |
6054 | 7c9d8e07 | bellard | ioh->opaque = opaque; |
6055 | cafffd40 | ths | ioh->deleted = 0;
|
6056 | 7c9d8e07 | bellard | } |
6057 | c4b1fcc0 | bellard | return 0; |
6058 | c4b1fcc0 | bellard | } |
6059 | c4b1fcc0 | bellard | |
6060 | 5fafdf24 | ths | int qemu_set_fd_handler(int fd, |
6061 | 5fafdf24 | ths | IOHandler *fd_read, |
6062 | 5fafdf24 | ths | IOHandler *fd_write, |
6063 | 7c9d8e07 | bellard | void *opaque)
|
6064 | 8a7ddc38 | bellard | { |
6065 | 7c9d8e07 | bellard | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
6066 | 8a7ddc38 | bellard | } |
6067 | 8a7ddc38 | bellard | |
6068 | 8a7ddc38 | bellard | /***********************************************************/
|
6069 | f331110f | bellard | /* Polling handling */
|
6070 | f331110f | bellard | |
6071 | f331110f | bellard | typedef struct PollingEntry { |
6072 | f331110f | bellard | PollingFunc *func; |
6073 | f331110f | bellard | void *opaque;
|
6074 | f331110f | bellard | struct PollingEntry *next;
|
6075 | f331110f | bellard | } PollingEntry; |
6076 | f331110f | bellard | |
6077 | f331110f | bellard | static PollingEntry *first_polling_entry;
|
6078 | f331110f | bellard | |
6079 | f331110f | bellard | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
6080 | f331110f | bellard | { |
6081 | f331110f | bellard | PollingEntry **ppe, *pe; |
6082 | f331110f | bellard | pe = qemu_mallocz(sizeof(PollingEntry));
|
6083 | f331110f | bellard | if (!pe)
|
6084 | f331110f | bellard | return -1; |
6085 | f331110f | bellard | pe->func = func; |
6086 | f331110f | bellard | pe->opaque = opaque; |
6087 | f331110f | bellard | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
6088 | f331110f | bellard | *ppe = pe; |
6089 | f331110f | bellard | return 0; |
6090 | f331110f | bellard | } |
6091 | f331110f | bellard | |
6092 | f331110f | bellard | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
6093 | f331110f | bellard | { |
6094 | f331110f | bellard | PollingEntry **ppe, *pe; |
6095 | f331110f | bellard | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
6096 | f331110f | bellard | pe = *ppe; |
6097 | f331110f | bellard | if (pe->func == func && pe->opaque == opaque) {
|
6098 | f331110f | bellard | *ppe = pe->next; |
6099 | f331110f | bellard | qemu_free(pe); |
6100 | f331110f | bellard | break;
|
6101 | f331110f | bellard | } |
6102 | f331110f | bellard | } |
6103 | f331110f | bellard | } |
6104 | f331110f | bellard | |
6105 | a18e524a | bellard | #ifdef _WIN32
|
6106 | a18e524a | bellard | /***********************************************************/
|
6107 | a18e524a | bellard | /* Wait objects support */
|
6108 | a18e524a | bellard | typedef struct WaitObjects { |
6109 | a18e524a | bellard | int num;
|
6110 | a18e524a | bellard | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
|
6111 | a18e524a | bellard | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
|
6112 | a18e524a | bellard | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
6113 | a18e524a | bellard | } WaitObjects; |
6114 | a18e524a | bellard | |
6115 | a18e524a | bellard | static WaitObjects wait_objects = {0}; |
6116 | 3b46e624 | ths | |
6117 | a18e524a | bellard | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
6118 | a18e524a | bellard | { |
6119 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
6120 | a18e524a | bellard | |
6121 | a18e524a | bellard | if (w->num >= MAXIMUM_WAIT_OBJECTS)
|
6122 | a18e524a | bellard | return -1; |
6123 | a18e524a | bellard | w->events[w->num] = handle; |
6124 | a18e524a | bellard | w->func[w->num] = func; |
6125 | a18e524a | bellard | w->opaque[w->num] = opaque; |
6126 | a18e524a | bellard | w->num++; |
6127 | a18e524a | bellard | return 0; |
6128 | a18e524a | bellard | } |
6129 | a18e524a | bellard | |
6130 | a18e524a | bellard | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
6131 | a18e524a | bellard | { |
6132 | a18e524a | bellard | int i, found;
|
6133 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
6134 | a18e524a | bellard | |
6135 | a18e524a | bellard | found = 0;
|
6136 | a18e524a | bellard | for (i = 0; i < w->num; i++) { |
6137 | a18e524a | bellard | if (w->events[i] == handle)
|
6138 | a18e524a | bellard | found = 1;
|
6139 | a18e524a | bellard | if (found) {
|
6140 | a18e524a | bellard | w->events[i] = w->events[i + 1];
|
6141 | a18e524a | bellard | w->func[i] = w->func[i + 1];
|
6142 | a18e524a | bellard | w->opaque[i] = w->opaque[i + 1];
|
6143 | 3b46e624 | ths | } |
6144 | a18e524a | bellard | } |
6145 | a18e524a | bellard | if (found)
|
6146 | a18e524a | bellard | w->num--; |
6147 | a18e524a | bellard | } |
6148 | a18e524a | bellard | #endif
|
6149 | a18e524a | bellard | |
6150 | f331110f | bellard | /***********************************************************/
|
6151 | 8a7ddc38 | bellard | /* savevm/loadvm support */
|
6152 | 8a7ddc38 | bellard | |
6153 | faea38e7 | bellard | #define IO_BUF_SIZE 32768 |
6154 | faea38e7 | bellard | |
6155 | faea38e7 | bellard | struct QEMUFile {
|
6156 | faea38e7 | bellard | FILE *outfile; |
6157 | faea38e7 | bellard | BlockDriverState *bs; |
6158 | faea38e7 | bellard | int is_file;
|
6159 | faea38e7 | bellard | int is_writable;
|
6160 | faea38e7 | bellard | int64_t base_offset; |
6161 | faea38e7 | bellard | int64_t buf_offset; /* start of buffer when writing, end of buffer
|
6162 | faea38e7 | bellard | when reading */
|
6163 | faea38e7 | bellard | int buf_index;
|
6164 | faea38e7 | bellard | int buf_size; /* 0 when writing */ |
6165 | faea38e7 | bellard | uint8_t buf[IO_BUF_SIZE]; |
6166 | faea38e7 | bellard | }; |
6167 | faea38e7 | bellard | |
6168 | faea38e7 | bellard | QEMUFile *qemu_fopen(const char *filename, const char *mode) |
6169 | faea38e7 | bellard | { |
6170 | faea38e7 | bellard | QEMUFile *f; |
6171 | faea38e7 | bellard | |
6172 | faea38e7 | bellard | f = qemu_mallocz(sizeof(QEMUFile));
|
6173 | faea38e7 | bellard | if (!f)
|
6174 | faea38e7 | bellard | return NULL; |
6175 | faea38e7 | bellard | if (!strcmp(mode, "wb")) { |
6176 | faea38e7 | bellard | f->is_writable = 1;
|
6177 | faea38e7 | bellard | } else if (!strcmp(mode, "rb")) { |
6178 | faea38e7 | bellard | f->is_writable = 0;
|
6179 | faea38e7 | bellard | } else {
|
6180 | faea38e7 | bellard | goto fail;
|
6181 | faea38e7 | bellard | } |
6182 | faea38e7 | bellard | f->outfile = fopen(filename, mode); |
6183 | faea38e7 | bellard | if (!f->outfile)
|
6184 | faea38e7 | bellard | goto fail;
|
6185 | faea38e7 | bellard | f->is_file = 1;
|
6186 | faea38e7 | bellard | return f;
|
6187 | faea38e7 | bellard | fail:
|
6188 | faea38e7 | bellard | if (f->outfile)
|
6189 | faea38e7 | bellard | fclose(f->outfile); |
6190 | faea38e7 | bellard | qemu_free(f); |
6191 | faea38e7 | bellard | return NULL; |
6192 | faea38e7 | bellard | } |
6193 | faea38e7 | bellard | |
6194 | 9596ebb7 | pbrook | static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable) |
6195 | faea38e7 | bellard | { |
6196 | faea38e7 | bellard | QEMUFile *f; |
6197 | faea38e7 | bellard | |
6198 | faea38e7 | bellard | f = qemu_mallocz(sizeof(QEMUFile));
|
6199 | faea38e7 | bellard | if (!f)
|
6200 | faea38e7 | bellard | return NULL; |
6201 | faea38e7 | bellard | f->is_file = 0;
|
6202 | faea38e7 | bellard | f->bs = bs; |
6203 | faea38e7 | bellard | f->is_writable = is_writable; |
6204 | faea38e7 | bellard | f->base_offset = offset; |
6205 | faea38e7 | bellard | return f;
|
6206 | faea38e7 | bellard | } |
6207 | faea38e7 | bellard | |
6208 | faea38e7 | bellard | void qemu_fflush(QEMUFile *f)
|
6209 | faea38e7 | bellard | { |
6210 | faea38e7 | bellard | if (!f->is_writable)
|
6211 | faea38e7 | bellard | return;
|
6212 | faea38e7 | bellard | if (f->buf_index > 0) { |
6213 | faea38e7 | bellard | if (f->is_file) {
|
6214 | faea38e7 | bellard | fseek(f->outfile, f->buf_offset, SEEK_SET); |
6215 | faea38e7 | bellard | fwrite(f->buf, 1, f->buf_index, f->outfile);
|
6216 | faea38e7 | bellard | } else {
|
6217 | 5fafdf24 | ths | bdrv_pwrite(f->bs, f->base_offset + f->buf_offset, |
6218 | faea38e7 | bellard | f->buf, f->buf_index); |
6219 | faea38e7 | bellard | } |
6220 | faea38e7 | bellard | f->buf_offset += f->buf_index; |
6221 | faea38e7 | bellard | f->buf_index = 0;
|
6222 | faea38e7 | bellard | } |
6223 | faea38e7 | bellard | } |
6224 | faea38e7 | bellard | |
6225 | faea38e7 | bellard | static void qemu_fill_buffer(QEMUFile *f) |
6226 | faea38e7 | bellard | { |
6227 | faea38e7 | bellard | int len;
|
6228 | faea38e7 | bellard | |
6229 | faea38e7 | bellard | if (f->is_writable)
|
6230 | faea38e7 | bellard | return;
|
6231 | faea38e7 | bellard | if (f->is_file) {
|
6232 | faea38e7 | bellard | fseek(f->outfile, f->buf_offset, SEEK_SET); |
6233 | faea38e7 | bellard | len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile);
|
6234 | faea38e7 | bellard | if (len < 0) |
6235 | faea38e7 | bellard | len = 0;
|
6236 | faea38e7 | bellard | } else {
|
6237 | 5fafdf24 | ths | len = bdrv_pread(f->bs, f->base_offset + f->buf_offset, |
6238 | faea38e7 | bellard | f->buf, IO_BUF_SIZE); |
6239 | faea38e7 | bellard | if (len < 0) |
6240 | faea38e7 | bellard | len = 0;
|
6241 | faea38e7 | bellard | } |
6242 | faea38e7 | bellard | f->buf_index = 0;
|
6243 | faea38e7 | bellard | f->buf_size = len; |
6244 | faea38e7 | bellard | f->buf_offset += len; |
6245 | faea38e7 | bellard | } |
6246 | faea38e7 | bellard | |
6247 | faea38e7 | bellard | void qemu_fclose(QEMUFile *f)
|
6248 | faea38e7 | bellard | { |
6249 | faea38e7 | bellard | if (f->is_writable)
|
6250 | faea38e7 | bellard | qemu_fflush(f); |
6251 | faea38e7 | bellard | if (f->is_file) {
|
6252 | faea38e7 | bellard | fclose(f->outfile); |
6253 | faea38e7 | bellard | } |
6254 | faea38e7 | bellard | qemu_free(f); |
6255 | faea38e7 | bellard | } |
6256 | faea38e7 | bellard | |
6257 | 8a7ddc38 | bellard | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
6258 | b4608c04 | bellard | { |
6259 | faea38e7 | bellard | int l;
|
6260 | faea38e7 | bellard | while (size > 0) { |
6261 | faea38e7 | bellard | l = IO_BUF_SIZE - f->buf_index; |
6262 | faea38e7 | bellard | if (l > size)
|
6263 | faea38e7 | bellard | l = size; |
6264 | faea38e7 | bellard | memcpy(f->buf + f->buf_index, buf, l); |
6265 | faea38e7 | bellard | f->buf_index += l; |
6266 | faea38e7 | bellard | buf += l; |
6267 | faea38e7 | bellard | size -= l; |
6268 | faea38e7 | bellard | if (f->buf_index >= IO_BUF_SIZE)
|
6269 | faea38e7 | bellard | qemu_fflush(f); |
6270 | faea38e7 | bellard | } |
6271 | b4608c04 | bellard | } |
6272 | b4608c04 | bellard | |
6273 | 8a7ddc38 | bellard | void qemu_put_byte(QEMUFile *f, int v) |
6274 | b4608c04 | bellard | { |
6275 | faea38e7 | bellard | f->buf[f->buf_index++] = v; |
6276 | faea38e7 | bellard | if (f->buf_index >= IO_BUF_SIZE)
|
6277 | faea38e7 | bellard | qemu_fflush(f); |
6278 | faea38e7 | bellard | } |
6279 | faea38e7 | bellard | |
6280 | faea38e7 | bellard | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1) |
6281 | faea38e7 | bellard | { |
6282 | faea38e7 | bellard | int size, l;
|
6283 | faea38e7 | bellard | |
6284 | faea38e7 | bellard | size = size1; |
6285 | faea38e7 | bellard | while (size > 0) { |
6286 | faea38e7 | bellard | l = f->buf_size - f->buf_index; |
6287 | faea38e7 | bellard | if (l == 0) { |
6288 | faea38e7 | bellard | qemu_fill_buffer(f); |
6289 | faea38e7 | bellard | l = f->buf_size - f->buf_index; |
6290 | faea38e7 | bellard | if (l == 0) |
6291 | faea38e7 | bellard | break;
|
6292 | faea38e7 | bellard | } |
6293 | faea38e7 | bellard | if (l > size)
|
6294 | faea38e7 | bellard | l = size; |
6295 | faea38e7 | bellard | memcpy(buf, f->buf + f->buf_index, l); |
6296 | faea38e7 | bellard | f->buf_index += l; |
6297 | faea38e7 | bellard | buf += l; |
6298 | faea38e7 | bellard | size -= l; |
6299 | faea38e7 | bellard | } |
6300 | faea38e7 | bellard | return size1 - size;
|
6301 | faea38e7 | bellard | } |
6302 | faea38e7 | bellard | |
6303 | faea38e7 | bellard | int qemu_get_byte(QEMUFile *f)
|
6304 | faea38e7 | bellard | { |
6305 | faea38e7 | bellard | if (f->buf_index >= f->buf_size) {
|
6306 | faea38e7 | bellard | qemu_fill_buffer(f); |
6307 | faea38e7 | bellard | if (f->buf_index >= f->buf_size)
|
6308 | faea38e7 | bellard | return 0; |
6309 | faea38e7 | bellard | } |
6310 | faea38e7 | bellard | return f->buf[f->buf_index++];
|
6311 | faea38e7 | bellard | } |
6312 | faea38e7 | bellard | |
6313 | faea38e7 | bellard | int64_t qemu_ftell(QEMUFile *f) |
6314 | faea38e7 | bellard | { |
6315 | faea38e7 | bellard | return f->buf_offset - f->buf_size + f->buf_index;
|
6316 | faea38e7 | bellard | } |
6317 | faea38e7 | bellard | |
6318 | faea38e7 | bellard | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
|
6319 | faea38e7 | bellard | { |
6320 | faea38e7 | bellard | if (whence == SEEK_SET) {
|
6321 | faea38e7 | bellard | /* nothing to do */
|
6322 | faea38e7 | bellard | } else if (whence == SEEK_CUR) { |
6323 | faea38e7 | bellard | pos += qemu_ftell(f); |
6324 | faea38e7 | bellard | } else {
|
6325 | faea38e7 | bellard | /* SEEK_END not supported */
|
6326 | faea38e7 | bellard | return -1; |
6327 | faea38e7 | bellard | } |
6328 | faea38e7 | bellard | if (f->is_writable) {
|
6329 | faea38e7 | bellard | qemu_fflush(f); |
6330 | faea38e7 | bellard | f->buf_offset = pos; |
6331 | faea38e7 | bellard | } else {
|
6332 | faea38e7 | bellard | f->buf_offset = pos; |
6333 | faea38e7 | bellard | f->buf_index = 0;
|
6334 | faea38e7 | bellard | f->buf_size = 0;
|
6335 | faea38e7 | bellard | } |
6336 | faea38e7 | bellard | return pos;
|
6337 | 8a7ddc38 | bellard | } |
6338 | 8a7ddc38 | bellard | |
6339 | 8a7ddc38 | bellard | void qemu_put_be16(QEMUFile *f, unsigned int v) |
6340 | 8a7ddc38 | bellard | { |
6341 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 8);
|
6342 | 8a7ddc38 | bellard | qemu_put_byte(f, v); |
6343 | 8a7ddc38 | bellard | } |
6344 | 8a7ddc38 | bellard | |
6345 | 8a7ddc38 | bellard | void qemu_put_be32(QEMUFile *f, unsigned int v) |
6346 | 8a7ddc38 | bellard | { |
6347 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 24);
|
6348 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 16);
|
6349 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 8);
|
6350 | 8a7ddc38 | bellard | qemu_put_byte(f, v); |
6351 | 8a7ddc38 | bellard | } |
6352 | 8a7ddc38 | bellard | |
6353 | 8a7ddc38 | bellard | void qemu_put_be64(QEMUFile *f, uint64_t v)
|
6354 | 8a7ddc38 | bellard | { |
6355 | 8a7ddc38 | bellard | qemu_put_be32(f, v >> 32);
|
6356 | 8a7ddc38 | bellard | qemu_put_be32(f, v); |
6357 | 8a7ddc38 | bellard | } |
6358 | 8a7ddc38 | bellard | |
6359 | 8a7ddc38 | bellard | unsigned int qemu_get_be16(QEMUFile *f) |
6360 | 8a7ddc38 | bellard | { |
6361 | 8a7ddc38 | bellard | unsigned int v; |
6362 | 8a7ddc38 | bellard | v = qemu_get_byte(f) << 8;
|
6363 | 8a7ddc38 | bellard | v |= qemu_get_byte(f); |
6364 | 8a7ddc38 | bellard | return v;
|
6365 | 8a7ddc38 | bellard | } |
6366 | 8a7ddc38 | bellard | |
6367 | 8a7ddc38 | bellard | unsigned int qemu_get_be32(QEMUFile *f) |
6368 | 8a7ddc38 | bellard | { |
6369 | 8a7ddc38 | bellard | unsigned int v; |
6370 | 8a7ddc38 | bellard | v = qemu_get_byte(f) << 24;
|
6371 | 8a7ddc38 | bellard | v |= qemu_get_byte(f) << 16;
|
6372 | 8a7ddc38 | bellard | v |= qemu_get_byte(f) << 8;
|
6373 | 8a7ddc38 | bellard | v |= qemu_get_byte(f); |
6374 | 8a7ddc38 | bellard | return v;
|
6375 | 8a7ddc38 | bellard | } |
6376 | 8a7ddc38 | bellard | |
6377 | 8a7ddc38 | bellard | uint64_t qemu_get_be64(QEMUFile *f) |
6378 | 8a7ddc38 | bellard | { |
6379 | 8a7ddc38 | bellard | uint64_t v; |
6380 | 8a7ddc38 | bellard | v = (uint64_t)qemu_get_be32(f) << 32;
|
6381 | 8a7ddc38 | bellard | v |= qemu_get_be32(f); |
6382 | 8a7ddc38 | bellard | return v;
|
6383 | 8a7ddc38 | bellard | } |
6384 | 8a7ddc38 | bellard | |
6385 | 8a7ddc38 | bellard | typedef struct SaveStateEntry { |
6386 | 8a7ddc38 | bellard | char idstr[256]; |
6387 | 8a7ddc38 | bellard | int instance_id;
|
6388 | 8a7ddc38 | bellard | int version_id;
|
6389 | 8a7ddc38 | bellard | SaveStateHandler *save_state; |
6390 | 8a7ddc38 | bellard | LoadStateHandler *load_state; |
6391 | 8a7ddc38 | bellard | void *opaque;
|
6392 | 8a7ddc38 | bellard | struct SaveStateEntry *next;
|
6393 | 8a7ddc38 | bellard | } SaveStateEntry; |
6394 | b4608c04 | bellard | |
6395 | 8a7ddc38 | bellard | static SaveStateEntry *first_se;
|
6396 | 8a7ddc38 | bellard | |
6397 | 18be5187 | pbrook | /* TODO: Individual devices generally have very little idea about the rest
|
6398 | 18fdb1c5 | ths | of the system, so instance_id should be removed/replaced.
|
6399 | 18fdb1c5 | ths | Meanwhile pass -1 as instance_id if you do not already have a clearly
|
6400 | 18fdb1c5 | ths | distinguishing id for all instances of your device class. */
|
6401 | 5fafdf24 | ths | int register_savevm(const char *idstr, |
6402 | 5fafdf24 | ths | int instance_id,
|
6403 | 8a7ddc38 | bellard | int version_id,
|
6404 | 8a7ddc38 | bellard | SaveStateHandler *save_state, |
6405 | 8a7ddc38 | bellard | LoadStateHandler *load_state, |
6406 | 8a7ddc38 | bellard | void *opaque)
|
6407 | 8a7ddc38 | bellard | { |
6408 | 8a7ddc38 | bellard | SaveStateEntry *se, **pse; |
6409 | 8a7ddc38 | bellard | |
6410 | 8a7ddc38 | bellard | se = qemu_malloc(sizeof(SaveStateEntry));
|
6411 | 8a7ddc38 | bellard | if (!se)
|
6412 | 8a7ddc38 | bellard | return -1; |
6413 | 8a7ddc38 | bellard | pstrcpy(se->idstr, sizeof(se->idstr), idstr);
|
6414 | 18be5187 | pbrook | se->instance_id = (instance_id == -1) ? 0 : instance_id; |
6415 | 8a7ddc38 | bellard | se->version_id = version_id; |
6416 | 8a7ddc38 | bellard | se->save_state = save_state; |
6417 | 8a7ddc38 | bellard | se->load_state = load_state; |
6418 | 8a7ddc38 | bellard | se->opaque = opaque; |
6419 | 8a7ddc38 | bellard | se->next = NULL;
|
6420 | 8a7ddc38 | bellard | |
6421 | 8a7ddc38 | bellard | /* add at the end of list */
|
6422 | 8a7ddc38 | bellard | pse = &first_se; |
6423 | 18be5187 | pbrook | while (*pse != NULL) { |
6424 | 18be5187 | pbrook | if (instance_id == -1 |
6425 | 18be5187 | pbrook | && strcmp(se->idstr, (*pse)->idstr) == 0
|
6426 | 18be5187 | pbrook | && se->instance_id <= (*pse)->instance_id) |
6427 | 18be5187 | pbrook | se->instance_id = (*pse)->instance_id + 1;
|
6428 | 8a7ddc38 | bellard | pse = &(*pse)->next; |
6429 | 18be5187 | pbrook | } |
6430 | 8a7ddc38 | bellard | *pse = se; |
6431 | 8a7ddc38 | bellard | return 0; |
6432 | 8a7ddc38 | bellard | } |
6433 | 8a7ddc38 | bellard | |
6434 | 8a7ddc38 | bellard | #define QEMU_VM_FILE_MAGIC 0x5145564d |
6435 | faea38e7 | bellard | #define QEMU_VM_FILE_VERSION 0x00000002 |
6436 | 8a7ddc38 | bellard | |
6437 | 9596ebb7 | pbrook | static int qemu_savevm_state(QEMUFile *f) |
6438 | 8a7ddc38 | bellard | { |
6439 | 8a7ddc38 | bellard | SaveStateEntry *se; |
6440 | faea38e7 | bellard | int len, ret;
|
6441 | faea38e7 | bellard | int64_t cur_pos, len_pos, total_len_pos; |
6442 | 313aa567 | bellard | |
6443 | 8a7ddc38 | bellard | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
6444 | 8a7ddc38 | bellard | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
6445 | faea38e7 | bellard | total_len_pos = qemu_ftell(f); |
6446 | faea38e7 | bellard | qemu_put_be64(f, 0); /* total size */ |
6447 | 8a7ddc38 | bellard | |
6448 | 8a7ddc38 | bellard | for(se = first_se; se != NULL; se = se->next) { |
6449 | d978c02c | aurel32 | if (se->save_state == NULL) |
6450 | d978c02c | aurel32 | /* this one has a loader only, for backwards compatibility */
|
6451 | d978c02c | aurel32 | continue;
|
6452 | d978c02c | aurel32 | |
6453 | 8a7ddc38 | bellard | /* ID string */
|
6454 | 8a7ddc38 | bellard | len = strlen(se->idstr); |
6455 | 8a7ddc38 | bellard | qemu_put_byte(f, len); |
6456 | ffe8ab83 | ths | qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
6457 | 8a7ddc38 | bellard | |
6458 | 8a7ddc38 | bellard | qemu_put_be32(f, se->instance_id); |
6459 | 8a7ddc38 | bellard | qemu_put_be32(f, se->version_id); |
6460 | 8a7ddc38 | bellard | |
6461 | 8a7ddc38 | bellard | /* record size: filled later */
|
6462 | faea38e7 | bellard | len_pos = qemu_ftell(f); |
6463 | 8a7ddc38 | bellard | qemu_put_be32(f, 0);
|
6464 | 8a7ddc38 | bellard | se->save_state(f, se->opaque); |
6465 | 8a7ddc38 | bellard | |
6466 | 8a7ddc38 | bellard | /* fill record size */
|
6467 | faea38e7 | bellard | cur_pos = qemu_ftell(f); |
6468 | faea38e7 | bellard | len = cur_pos - len_pos - 4;
|
6469 | faea38e7 | bellard | qemu_fseek(f, len_pos, SEEK_SET); |
6470 | 8a7ddc38 | bellard | qemu_put_be32(f, len); |
6471 | faea38e7 | bellard | qemu_fseek(f, cur_pos, SEEK_SET); |
6472 | 8a7ddc38 | bellard | } |
6473 | faea38e7 | bellard | cur_pos = qemu_ftell(f); |
6474 | faea38e7 | bellard | qemu_fseek(f, total_len_pos, SEEK_SET); |
6475 | faea38e7 | bellard | qemu_put_be64(f, cur_pos - total_len_pos - 8);
|
6476 | faea38e7 | bellard | qemu_fseek(f, cur_pos, SEEK_SET); |
6477 | 8a7ddc38 | bellard | |
6478 | 8a7ddc38 | bellard | ret = 0;
|
6479 | 8a7ddc38 | bellard | return ret;
|
6480 | 8a7ddc38 | bellard | } |
6481 | 8a7ddc38 | bellard | |
6482 | 8a7ddc38 | bellard | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
6483 | 8a7ddc38 | bellard | { |
6484 | 8a7ddc38 | bellard | SaveStateEntry *se; |
6485 | 8a7ddc38 | bellard | |
6486 | 8a7ddc38 | bellard | for(se = first_se; se != NULL; se = se->next) { |
6487 | 5fafdf24 | ths | if (!strcmp(se->idstr, idstr) &&
|
6488 | 8a7ddc38 | bellard | instance_id == se->instance_id) |
6489 | 8a7ddc38 | bellard | return se;
|
6490 | 8a7ddc38 | bellard | } |
6491 | 8a7ddc38 | bellard | return NULL; |
6492 | 8a7ddc38 | bellard | } |
6493 | 8a7ddc38 | bellard | |
6494 | 9596ebb7 | pbrook | static int qemu_loadvm_state(QEMUFile *f) |
6495 | 8a7ddc38 | bellard | { |
6496 | 8a7ddc38 | bellard | SaveStateEntry *se; |
6497 | faea38e7 | bellard | int len, ret, instance_id, record_len, version_id;
|
6498 | faea38e7 | bellard | int64_t total_len, end_pos, cur_pos; |
6499 | 8a7ddc38 | bellard | unsigned int v; |
6500 | 8a7ddc38 | bellard | char idstr[256]; |
6501 | 3b46e624 | ths | |
6502 | 8a7ddc38 | bellard | v = qemu_get_be32(f); |
6503 | 8a7ddc38 | bellard | if (v != QEMU_VM_FILE_MAGIC)
|
6504 | 8a7ddc38 | bellard | goto fail;
|
6505 | 8a7ddc38 | bellard | v = qemu_get_be32(f); |
6506 | 8a7ddc38 | bellard | if (v != QEMU_VM_FILE_VERSION) {
|
6507 | 8a7ddc38 | bellard | fail:
|
6508 | 8a7ddc38 | bellard | ret = -1;
|
6509 | 8a7ddc38 | bellard | goto the_end;
|
6510 | 8a7ddc38 | bellard | } |
6511 | faea38e7 | bellard | total_len = qemu_get_be64(f); |
6512 | faea38e7 | bellard | end_pos = total_len + qemu_ftell(f); |
6513 | b4608c04 | bellard | for(;;) {
|
6514 | faea38e7 | bellard | if (qemu_ftell(f) >= end_pos)
|
6515 | cd4c3e88 | bellard | break;
|
6516 | faea38e7 | bellard | len = qemu_get_byte(f); |
6517 | ffe8ab83 | ths | qemu_get_buffer(f, (uint8_t *)idstr, len); |
6518 | 8a7ddc38 | bellard | idstr[len] = '\0';
|
6519 | 8a7ddc38 | bellard | instance_id = qemu_get_be32(f); |
6520 | 8a7ddc38 | bellard | version_id = qemu_get_be32(f); |
6521 | 8a7ddc38 | bellard | record_len = qemu_get_be32(f); |
6522 | 8a7ddc38 | bellard | #if 0
|
6523 | 5fafdf24 | ths | printf("idstr=%s instance=0x%x version=%d len=%d\n",
|
6524 | 8a7ddc38 | bellard | idstr, instance_id, version_id, record_len);
|
6525 | 8a7ddc38 | bellard | #endif
|
6526 | faea38e7 | bellard | cur_pos = qemu_ftell(f); |
6527 | 8a7ddc38 | bellard | se = find_se(idstr, instance_id); |
6528 | 8a7ddc38 | bellard | if (!se) {
|
6529 | 5fafdf24 | ths | fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
|
6530 | 8a7ddc38 | bellard | instance_id, idstr); |
6531 | 8a7ddc38 | bellard | } else {
|
6532 | 8a7ddc38 | bellard | ret = se->load_state(f, se->opaque, version_id); |
6533 | 8a7ddc38 | bellard | if (ret < 0) { |
6534 | 5fafdf24 | ths | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
|
6535 | 8a7ddc38 | bellard | instance_id, idstr); |
6536 | 8a7ddc38 | bellard | } |
6537 | 34865134 | bellard | } |
6538 | 8a7ddc38 | bellard | /* always seek to exact end of record */
|
6539 | 8a7ddc38 | bellard | qemu_fseek(f, cur_pos + record_len, SEEK_SET); |
6540 | 8a7ddc38 | bellard | } |
6541 | 8a7ddc38 | bellard | ret = 0;
|
6542 | 8a7ddc38 | bellard | the_end:
|
6543 | faea38e7 | bellard | return ret;
|
6544 | faea38e7 | bellard | } |
6545 | faea38e7 | bellard | |
6546 | faea38e7 | bellard | /* device can contain snapshots */
|
6547 | faea38e7 | bellard | static int bdrv_can_snapshot(BlockDriverState *bs) |
6548 | faea38e7 | bellard | { |
6549 | faea38e7 | bellard | return (bs &&
|
6550 | faea38e7 | bellard | !bdrv_is_removable(bs) && |
6551 | faea38e7 | bellard | !bdrv_is_read_only(bs)); |
6552 | faea38e7 | bellard | } |
6553 | faea38e7 | bellard | |
6554 | faea38e7 | bellard | /* device must be snapshots in order to have a reliable snapshot */
|
6555 | faea38e7 | bellard | static int bdrv_has_snapshot(BlockDriverState *bs) |
6556 | faea38e7 | bellard | { |
6557 | faea38e7 | bellard | return (bs &&
|
6558 | faea38e7 | bellard | !bdrv_is_removable(bs) && |
6559 | faea38e7 | bellard | !bdrv_is_read_only(bs)); |
6560 | faea38e7 | bellard | } |
6561 | faea38e7 | bellard | |
6562 | faea38e7 | bellard | static BlockDriverState *get_bs_snapshots(void) |
6563 | faea38e7 | bellard | { |
6564 | faea38e7 | bellard | BlockDriverState *bs; |
6565 | faea38e7 | bellard | int i;
|
6566 | faea38e7 | bellard | |
6567 | faea38e7 | bellard | if (bs_snapshots)
|
6568 | faea38e7 | bellard | return bs_snapshots;
|
6569 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6570 | e4bcb14c | ths | bs = drives_table[i].bdrv; |
6571 | faea38e7 | bellard | if (bdrv_can_snapshot(bs))
|
6572 | faea38e7 | bellard | goto ok;
|
6573 | faea38e7 | bellard | } |
6574 | faea38e7 | bellard | return NULL; |
6575 | faea38e7 | bellard | ok:
|
6576 | faea38e7 | bellard | bs_snapshots = bs; |
6577 | faea38e7 | bellard | return bs;
|
6578 | faea38e7 | bellard | } |
6579 | faea38e7 | bellard | |
6580 | faea38e7 | bellard | static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
6581 | faea38e7 | bellard | const char *name) |
6582 | faea38e7 | bellard | { |
6583 | faea38e7 | bellard | QEMUSnapshotInfo *sn_tab, *sn; |
6584 | faea38e7 | bellard | int nb_sns, i, ret;
|
6585 | 3b46e624 | ths | |
6586 | faea38e7 | bellard | ret = -ENOENT; |
6587 | faea38e7 | bellard | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
6588 | faea38e7 | bellard | if (nb_sns < 0) |
6589 | faea38e7 | bellard | return ret;
|
6590 | faea38e7 | bellard | for(i = 0; i < nb_sns; i++) { |
6591 | faea38e7 | bellard | sn = &sn_tab[i]; |
6592 | faea38e7 | bellard | if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
|
6593 | faea38e7 | bellard | *sn_info = *sn; |
6594 | faea38e7 | bellard | ret = 0;
|
6595 | faea38e7 | bellard | break;
|
6596 | faea38e7 | bellard | } |
6597 | faea38e7 | bellard | } |
6598 | faea38e7 | bellard | qemu_free(sn_tab); |
6599 | faea38e7 | bellard | return ret;
|
6600 | faea38e7 | bellard | } |
6601 | faea38e7 | bellard | |
6602 | faea38e7 | bellard | void do_savevm(const char *name) |
6603 | faea38e7 | bellard | { |
6604 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6605 | faea38e7 | bellard | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; |
6606 | faea38e7 | bellard | int must_delete, ret, i;
|
6607 | faea38e7 | bellard | BlockDriverInfo bdi1, *bdi = &bdi1; |
6608 | faea38e7 | bellard | QEMUFile *f; |
6609 | faea38e7 | bellard | int saved_vm_running;
|
6610 | 4c279bdf | bellard | #ifdef _WIN32
|
6611 | 4c279bdf | bellard | struct _timeb tb;
|
6612 | 4c279bdf | bellard | #else
|
6613 | faea38e7 | bellard | struct timeval tv;
|
6614 | 4c279bdf | bellard | #endif
|
6615 | faea38e7 | bellard | |
6616 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6617 | faea38e7 | bellard | if (!bs) {
|
6618 | faea38e7 | bellard | term_printf("No block device can accept snapshots\n");
|
6619 | faea38e7 | bellard | return;
|
6620 | faea38e7 | bellard | } |
6621 | faea38e7 | bellard | |
6622 | 6192bc37 | pbrook | /* ??? Should this occur after vm_stop? */
|
6623 | 6192bc37 | pbrook | qemu_aio_flush(); |
6624 | 6192bc37 | pbrook | |
6625 | faea38e7 | bellard | saved_vm_running = vm_running; |
6626 | faea38e7 | bellard | vm_stop(0);
|
6627 | 3b46e624 | ths | |
6628 | faea38e7 | bellard | must_delete = 0;
|
6629 | faea38e7 | bellard | if (name) {
|
6630 | faea38e7 | bellard | ret = bdrv_snapshot_find(bs, old_sn, name); |
6631 | faea38e7 | bellard | if (ret >= 0) { |
6632 | faea38e7 | bellard | must_delete = 1;
|
6633 | faea38e7 | bellard | } |
6634 | faea38e7 | bellard | } |
6635 | faea38e7 | bellard | memset(sn, 0, sizeof(*sn)); |
6636 | faea38e7 | bellard | if (must_delete) {
|
6637 | faea38e7 | bellard | pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
|
6638 | faea38e7 | bellard | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
|
6639 | faea38e7 | bellard | } else {
|
6640 | faea38e7 | bellard | if (name)
|
6641 | faea38e7 | bellard | pstrcpy(sn->name, sizeof(sn->name), name);
|
6642 | faea38e7 | bellard | } |
6643 | faea38e7 | bellard | |
6644 | faea38e7 | bellard | /* fill auxiliary fields */
|
6645 | 4c279bdf | bellard | #ifdef _WIN32
|
6646 | 4c279bdf | bellard | _ftime(&tb); |
6647 | 4c279bdf | bellard | sn->date_sec = tb.time; |
6648 | 4c279bdf | bellard | sn->date_nsec = tb.millitm * 1000000;
|
6649 | 4c279bdf | bellard | #else
|
6650 | faea38e7 | bellard | gettimeofday(&tv, NULL);
|
6651 | faea38e7 | bellard | sn->date_sec = tv.tv_sec; |
6652 | faea38e7 | bellard | sn->date_nsec = tv.tv_usec * 1000;
|
6653 | 4c279bdf | bellard | #endif
|
6654 | faea38e7 | bellard | sn->vm_clock_nsec = qemu_get_clock(vm_clock); |
6655 | 3b46e624 | ths | |
6656 | faea38e7 | bellard | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
6657 | faea38e7 | bellard | term_printf("Device %s does not support VM state snapshots\n",
|
6658 | faea38e7 | bellard | bdrv_get_device_name(bs)); |
6659 | faea38e7 | bellard | goto the_end;
|
6660 | faea38e7 | bellard | } |
6661 | 3b46e624 | ths | |
6662 | faea38e7 | bellard | /* save the VM state */
|
6663 | faea38e7 | bellard | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
|
6664 | faea38e7 | bellard | if (!f) {
|
6665 | faea38e7 | bellard | term_printf("Could not open VM state file\n");
|
6666 | faea38e7 | bellard | goto the_end;
|
6667 | faea38e7 | bellard | } |
6668 | faea38e7 | bellard | ret = qemu_savevm_state(f); |
6669 | faea38e7 | bellard | sn->vm_state_size = qemu_ftell(f); |
6670 | faea38e7 | bellard | qemu_fclose(f); |
6671 | faea38e7 | bellard | if (ret < 0) { |
6672 | faea38e7 | bellard | term_printf("Error %d while writing VM\n", ret);
|
6673 | faea38e7 | bellard | goto the_end;
|
6674 | faea38e7 | bellard | } |
6675 | 3b46e624 | ths | |
6676 | faea38e7 | bellard | /* create the snapshots */
|
6677 | faea38e7 | bellard | |
6678 | e4bcb14c | ths | for(i = 0; i < nb_drives; i++) { |
6679 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6680 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6681 | faea38e7 | bellard | if (must_delete) {
|
6682 | faea38e7 | bellard | ret = bdrv_snapshot_delete(bs1, old_sn->id_str); |
6683 | faea38e7 | bellard | if (ret < 0) { |
6684 | faea38e7 | bellard | term_printf("Error while deleting snapshot on '%s'\n",
|
6685 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6686 | faea38e7 | bellard | } |
6687 | faea38e7 | bellard | } |
6688 | faea38e7 | bellard | ret = bdrv_snapshot_create(bs1, sn); |
6689 | faea38e7 | bellard | if (ret < 0) { |
6690 | faea38e7 | bellard | term_printf("Error while creating snapshot on '%s'\n",
|
6691 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6692 | faea38e7 | bellard | } |
6693 | faea38e7 | bellard | } |
6694 | faea38e7 | bellard | } |
6695 | faea38e7 | bellard | |
6696 | faea38e7 | bellard | the_end:
|
6697 | 8a7ddc38 | bellard | if (saved_vm_running)
|
6698 | 8a7ddc38 | bellard | vm_start(); |
6699 | faea38e7 | bellard | } |
6700 | faea38e7 | bellard | |
6701 | faea38e7 | bellard | void do_loadvm(const char *name) |
6702 | faea38e7 | bellard | { |
6703 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6704 | faea38e7 | bellard | BlockDriverInfo bdi1, *bdi = &bdi1; |
6705 | faea38e7 | bellard | QEMUFile *f; |
6706 | faea38e7 | bellard | int i, ret;
|
6707 | faea38e7 | bellard | int saved_vm_running;
|
6708 | faea38e7 | bellard | |
6709 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6710 | faea38e7 | bellard | if (!bs) {
|
6711 | faea38e7 | bellard | term_printf("No block device supports snapshots\n");
|
6712 | faea38e7 | bellard | return;
|
6713 | faea38e7 | bellard | } |
6714 | 3b46e624 | ths | |
6715 | 6192bc37 | pbrook | /* Flush all IO requests so they don't interfere with the new state. */
|
6716 | 6192bc37 | pbrook | qemu_aio_flush(); |
6717 | 6192bc37 | pbrook | |
6718 | faea38e7 | bellard | saved_vm_running = vm_running; |
6719 | faea38e7 | bellard | vm_stop(0);
|
6720 | faea38e7 | bellard | |
6721 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6722 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6723 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6724 | faea38e7 | bellard | ret = bdrv_snapshot_goto(bs1, name); |
6725 | faea38e7 | bellard | if (ret < 0) { |
6726 | faea38e7 | bellard | if (bs != bs1)
|
6727 | faea38e7 | bellard | term_printf("Warning: ");
|
6728 | faea38e7 | bellard | switch(ret) {
|
6729 | faea38e7 | bellard | case -ENOTSUP:
|
6730 | faea38e7 | bellard | term_printf("Snapshots not supported on device '%s'\n",
|
6731 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6732 | faea38e7 | bellard | break;
|
6733 | faea38e7 | bellard | case -ENOENT:
|
6734 | faea38e7 | bellard | term_printf("Could not find snapshot '%s' on device '%s'\n",
|
6735 | faea38e7 | bellard | name, bdrv_get_device_name(bs1)); |
6736 | faea38e7 | bellard | break;
|
6737 | faea38e7 | bellard | default:
|
6738 | faea38e7 | bellard | term_printf("Error %d while activating snapshot on '%s'\n",
|
6739 | faea38e7 | bellard | ret, bdrv_get_device_name(bs1)); |
6740 | faea38e7 | bellard | break;
|
6741 | faea38e7 | bellard | } |
6742 | faea38e7 | bellard | /* fatal on snapshot block device */
|
6743 | faea38e7 | bellard | if (bs == bs1)
|
6744 | faea38e7 | bellard | goto the_end;
|
6745 | faea38e7 | bellard | } |
6746 | faea38e7 | bellard | } |
6747 | faea38e7 | bellard | } |
6748 | faea38e7 | bellard | |
6749 | faea38e7 | bellard | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
6750 | faea38e7 | bellard | term_printf("Device %s does not support VM state snapshots\n",
|
6751 | faea38e7 | bellard | bdrv_get_device_name(bs)); |
6752 | faea38e7 | bellard | return;
|
6753 | faea38e7 | bellard | } |
6754 | 3b46e624 | ths | |
6755 | faea38e7 | bellard | /* restore the VM state */
|
6756 | faea38e7 | bellard | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
|
6757 | faea38e7 | bellard | if (!f) {
|
6758 | faea38e7 | bellard | term_printf("Could not open VM state file\n");
|
6759 | faea38e7 | bellard | goto the_end;
|
6760 | faea38e7 | bellard | } |
6761 | faea38e7 | bellard | ret = qemu_loadvm_state(f); |
6762 | faea38e7 | bellard | qemu_fclose(f); |
6763 | faea38e7 | bellard | if (ret < 0) { |
6764 | faea38e7 | bellard | term_printf("Error %d while loading VM state\n", ret);
|
6765 | faea38e7 | bellard | } |
6766 | faea38e7 | bellard | the_end:
|
6767 | faea38e7 | bellard | if (saved_vm_running)
|
6768 | faea38e7 | bellard | vm_start(); |
6769 | faea38e7 | bellard | } |
6770 | faea38e7 | bellard | |
6771 | faea38e7 | bellard | void do_delvm(const char *name) |
6772 | faea38e7 | bellard | { |
6773 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6774 | faea38e7 | bellard | int i, ret;
|
6775 | faea38e7 | bellard | |
6776 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6777 | faea38e7 | bellard | if (!bs) {
|
6778 | faea38e7 | bellard | term_printf("No block device supports snapshots\n");
|
6779 | faea38e7 | bellard | return;
|
6780 | faea38e7 | bellard | } |
6781 | 3b46e624 | ths | |
6782 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6783 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6784 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6785 | faea38e7 | bellard | ret = bdrv_snapshot_delete(bs1, name); |
6786 | faea38e7 | bellard | if (ret < 0) { |
6787 | faea38e7 | bellard | if (ret == -ENOTSUP)
|
6788 | faea38e7 | bellard | term_printf("Snapshots not supported on device '%s'\n",
|
6789 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6790 | faea38e7 | bellard | else
|
6791 | faea38e7 | bellard | term_printf("Error %d while deleting snapshot on '%s'\n",
|
6792 | faea38e7 | bellard | ret, bdrv_get_device_name(bs1)); |
6793 | faea38e7 | bellard | } |
6794 | faea38e7 | bellard | } |
6795 | faea38e7 | bellard | } |
6796 | faea38e7 | bellard | } |
6797 | faea38e7 | bellard | |
6798 | faea38e7 | bellard | void do_info_snapshots(void) |
6799 | faea38e7 | bellard | { |
6800 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6801 | faea38e7 | bellard | QEMUSnapshotInfo *sn_tab, *sn; |
6802 | faea38e7 | bellard | int nb_sns, i;
|
6803 | faea38e7 | bellard | char buf[256]; |
6804 | faea38e7 | bellard | |
6805 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6806 | faea38e7 | bellard | if (!bs) {
|
6807 | faea38e7 | bellard | term_printf("No available block device supports snapshots\n");
|
6808 | faea38e7 | bellard | return;
|
6809 | faea38e7 | bellard | } |
6810 | faea38e7 | bellard | term_printf("Snapshot devices:");
|
6811 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6812 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6813 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6814 | faea38e7 | bellard | if (bs == bs1)
|
6815 | faea38e7 | bellard | term_printf(" %s", bdrv_get_device_name(bs1));
|
6816 | faea38e7 | bellard | } |
6817 | faea38e7 | bellard | } |
6818 | faea38e7 | bellard | term_printf("\n");
|
6819 | faea38e7 | bellard | |
6820 | faea38e7 | bellard | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
6821 | faea38e7 | bellard | if (nb_sns < 0) { |
6822 | faea38e7 | bellard | term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
|
6823 | faea38e7 | bellard | return;
|
6824 | faea38e7 | bellard | } |
6825 | faea38e7 | bellard | term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
|
6826 | faea38e7 | bellard | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
6827 | faea38e7 | bellard | for(i = 0; i < nb_sns; i++) { |
6828 | faea38e7 | bellard | sn = &sn_tab[i]; |
6829 | faea38e7 | bellard | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
6830 | faea38e7 | bellard | } |
6831 | faea38e7 | bellard | qemu_free(sn_tab); |
6832 | 8a7ddc38 | bellard | } |
6833 | 8a7ddc38 | bellard | |
6834 | 8a7ddc38 | bellard | /***********************************************************/
|
6835 | 8a7ddc38 | bellard | /* ram save/restore */
|
6836 | 8a7ddc38 | bellard | |
6837 | 8a7ddc38 | bellard | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
6838 | 8a7ddc38 | bellard | { |
6839 | 8a7ddc38 | bellard | int v;
|
6840 | 8a7ddc38 | bellard | |
6841 | 8a7ddc38 | bellard | v = qemu_get_byte(f); |
6842 | 8a7ddc38 | bellard | switch(v) {
|
6843 | 8a7ddc38 | bellard | case 0: |
6844 | 8a7ddc38 | bellard | if (qemu_get_buffer(f, buf, len) != len)
|
6845 | 8a7ddc38 | bellard | return -EIO;
|
6846 | 8a7ddc38 | bellard | break;
|
6847 | 8a7ddc38 | bellard | case 1: |
6848 | 8a7ddc38 | bellard | v = qemu_get_byte(f); |
6849 | 8a7ddc38 | bellard | memset(buf, v, len); |
6850 | 8a7ddc38 | bellard | break;
|
6851 | 8a7ddc38 | bellard | default:
|
6852 | 8a7ddc38 | bellard | return -EINVAL;
|
6853 | 8a7ddc38 | bellard | } |
6854 | 8a7ddc38 | bellard | return 0; |
6855 | 8a7ddc38 | bellard | } |
6856 | 8a7ddc38 | bellard | |
6857 | c88676f8 | bellard | static int ram_load_v1(QEMUFile *f, void *opaque) |
6858 | c88676f8 | bellard | { |
6859 | 00f82b8a | aurel32 | int ret;
|
6860 | 00f82b8a | aurel32 | ram_addr_t i; |
6861 | c88676f8 | bellard | |
6862 | c88676f8 | bellard | if (qemu_get_be32(f) != phys_ram_size)
|
6863 | c88676f8 | bellard | return -EINVAL;
|
6864 | c88676f8 | bellard | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
6865 | c88676f8 | bellard | ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
6866 | c88676f8 | bellard | if (ret)
|
6867 | c88676f8 | bellard | return ret;
|
6868 | c88676f8 | bellard | } |
6869 | c88676f8 | bellard | return 0; |
6870 | c88676f8 | bellard | } |
6871 | c88676f8 | bellard | |
6872 | c88676f8 | bellard | #define BDRV_HASH_BLOCK_SIZE 1024 |
6873 | c88676f8 | bellard | #define IOBUF_SIZE 4096 |
6874 | c88676f8 | bellard | #define RAM_CBLOCK_MAGIC 0xfabe |
6875 | c88676f8 | bellard | |
6876 | c88676f8 | bellard | typedef struct RamCompressState { |
6877 | c88676f8 | bellard | z_stream zstream; |
6878 | c88676f8 | bellard | QEMUFile *f; |
6879 | c88676f8 | bellard | uint8_t buf[IOBUF_SIZE]; |
6880 | c88676f8 | bellard | } RamCompressState; |
6881 | c88676f8 | bellard | |
6882 | c88676f8 | bellard | static int ram_compress_open(RamCompressState *s, QEMUFile *f) |
6883 | c88676f8 | bellard | { |
6884 | c88676f8 | bellard | int ret;
|
6885 | c88676f8 | bellard | memset(s, 0, sizeof(*s)); |
6886 | c88676f8 | bellard | s->f = f; |
6887 | c88676f8 | bellard | ret = deflateInit2(&s->zstream, 1,
|
6888 | 5fafdf24 | ths | Z_DEFLATED, 15,
|
6889 | c88676f8 | bellard | 9, Z_DEFAULT_STRATEGY);
|
6890 | c88676f8 | bellard | if (ret != Z_OK)
|
6891 | c88676f8 | bellard | return -1; |
6892 | c88676f8 | bellard | s->zstream.avail_out = IOBUF_SIZE; |
6893 | c88676f8 | bellard | s->zstream.next_out = s->buf; |
6894 | c88676f8 | bellard | return 0; |
6895 | c88676f8 | bellard | } |
6896 | c88676f8 | bellard | |
6897 | c88676f8 | bellard | static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len) |
6898 | c88676f8 | bellard | { |
6899 | c88676f8 | bellard | qemu_put_be16(s->f, RAM_CBLOCK_MAGIC); |
6900 | c88676f8 | bellard | qemu_put_be16(s->f, len); |
6901 | c88676f8 | bellard | qemu_put_buffer(s->f, buf, len); |
6902 | c88676f8 | bellard | } |
6903 | c88676f8 | bellard | |
6904 | c88676f8 | bellard | static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len) |
6905 | c88676f8 | bellard | { |
6906 | c88676f8 | bellard | int ret;
|
6907 | c88676f8 | bellard | |
6908 | c88676f8 | bellard | s->zstream.avail_in = len; |
6909 | c88676f8 | bellard | s->zstream.next_in = (uint8_t *)buf; |
6910 | c88676f8 | bellard | while (s->zstream.avail_in > 0) { |
6911 | c88676f8 | bellard | ret = deflate(&s->zstream, Z_NO_FLUSH); |
6912 | c88676f8 | bellard | if (ret != Z_OK)
|
6913 | c88676f8 | bellard | return -1; |
6914 | c88676f8 | bellard | if (s->zstream.avail_out == 0) { |
6915 | c88676f8 | bellard | ram_put_cblock(s, s->buf, IOBUF_SIZE); |
6916 | c88676f8 | bellard | s->zstream.avail_out = IOBUF_SIZE; |
6917 | c88676f8 | bellard | s->zstream.next_out = s->buf; |
6918 | c88676f8 | bellard | } |
6919 | c88676f8 | bellard | } |
6920 | c88676f8 | bellard | return 0; |
6921 | c88676f8 | bellard | } |
6922 | c88676f8 | bellard | |
6923 | c88676f8 | bellard | static void ram_compress_close(RamCompressState *s) |
6924 | c88676f8 | bellard | { |
6925 | c88676f8 | bellard | int len, ret;
|
6926 | c88676f8 | bellard | |
6927 | c88676f8 | bellard | /* compress last bytes */
|
6928 | c88676f8 | bellard | for(;;) {
|
6929 | c88676f8 | bellard | ret = deflate(&s->zstream, Z_FINISH); |
6930 | c88676f8 | bellard | if (ret == Z_OK || ret == Z_STREAM_END) {
|
6931 | c88676f8 | bellard | len = IOBUF_SIZE - s->zstream.avail_out; |
6932 | c88676f8 | bellard | if (len > 0) { |
6933 | c88676f8 | bellard | ram_put_cblock(s, s->buf, len); |
6934 | c88676f8 | bellard | } |
6935 | c88676f8 | bellard | s->zstream.avail_out = IOBUF_SIZE; |
6936 | c88676f8 | bellard | s->zstream.next_out = s->buf; |
6937 | c88676f8 | bellard | if (ret == Z_STREAM_END)
|
6938 | c88676f8 | bellard | break;
|
6939 | c88676f8 | bellard | } else {
|
6940 | c88676f8 | bellard | goto fail;
|
6941 | c88676f8 | bellard | } |
6942 | c88676f8 | bellard | } |
6943 | c88676f8 | bellard | fail:
|
6944 | c88676f8 | bellard | deflateEnd(&s->zstream); |
6945 | c88676f8 | bellard | } |
6946 | c88676f8 | bellard | |
6947 | c88676f8 | bellard | typedef struct RamDecompressState { |
6948 | c88676f8 | bellard | z_stream zstream; |
6949 | c88676f8 | bellard | QEMUFile *f; |
6950 | c88676f8 | bellard | uint8_t buf[IOBUF_SIZE]; |
6951 | c88676f8 | bellard | } RamDecompressState; |
6952 | c88676f8 | bellard | |
6953 | c88676f8 | bellard | static int ram_decompress_open(RamDecompressState *s, QEMUFile *f) |
6954 | c88676f8 | bellard | { |
6955 | c88676f8 | bellard | int ret;
|
6956 | c88676f8 | bellard | memset(s, 0, sizeof(*s)); |
6957 | c88676f8 | bellard | s->f = f; |
6958 | c88676f8 | bellard | ret = inflateInit(&s->zstream); |
6959 | c88676f8 | bellard | if (ret != Z_OK)
|
6960 | c88676f8 | bellard | return -1; |
6961 | c88676f8 | bellard | return 0; |
6962 | c88676f8 | bellard | } |
6963 | c88676f8 | bellard | |
6964 | c88676f8 | bellard | static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len) |
6965 | c88676f8 | bellard | { |
6966 | c88676f8 | bellard | int ret, clen;
|
6967 | c88676f8 | bellard | |
6968 | c88676f8 | bellard | s->zstream.avail_out = len; |
6969 | c88676f8 | bellard | s->zstream.next_out = buf; |
6970 | c88676f8 | bellard | while (s->zstream.avail_out > 0) { |
6971 | c88676f8 | bellard | if (s->zstream.avail_in == 0) { |
6972 | c88676f8 | bellard | if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
|
6973 | c88676f8 | bellard | return -1; |
6974 | c88676f8 | bellard | clen = qemu_get_be16(s->f); |
6975 | c88676f8 | bellard | if (clen > IOBUF_SIZE)
|
6976 | c88676f8 | bellard | return -1; |
6977 | c88676f8 | bellard | qemu_get_buffer(s->f, s->buf, clen); |
6978 | c88676f8 | bellard | s->zstream.avail_in = clen; |
6979 | c88676f8 | bellard | s->zstream.next_in = s->buf; |
6980 | c88676f8 | bellard | } |
6981 | c88676f8 | bellard | ret = inflate(&s->zstream, Z_PARTIAL_FLUSH); |
6982 | c88676f8 | bellard | if (ret != Z_OK && ret != Z_STREAM_END) {
|
6983 | c88676f8 | bellard | return -1; |
6984 | c88676f8 | bellard | } |
6985 | c88676f8 | bellard | } |
6986 | c88676f8 | bellard | return 0; |
6987 | c88676f8 | bellard | } |
6988 | c88676f8 | bellard | |
6989 | c88676f8 | bellard | static void ram_decompress_close(RamDecompressState *s) |
6990 | c88676f8 | bellard | { |
6991 | c88676f8 | bellard | inflateEnd(&s->zstream); |
6992 | c88676f8 | bellard | } |
6993 | c88676f8 | bellard | |
6994 | 8a7ddc38 | bellard | static void ram_save(QEMUFile *f, void *opaque) |
6995 | 8a7ddc38 | bellard | { |
6996 | 00f82b8a | aurel32 | ram_addr_t i; |
6997 | c88676f8 | bellard | RamCompressState s1, *s = &s1; |
6998 | c88676f8 | bellard | uint8_t buf[10];
|
6999 | 3b46e624 | ths | |
7000 | 8a7ddc38 | bellard | qemu_put_be32(f, phys_ram_size); |
7001 | c88676f8 | bellard | if (ram_compress_open(s, f) < 0) |
7002 | c88676f8 | bellard | return;
|
7003 | c88676f8 | bellard | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
7004 | c88676f8 | bellard | #if 0
|
7005 | c88676f8 | bellard | if (tight_savevm_enabled) {
|
7006 | c88676f8 | bellard | int64_t sector_num;
|
7007 | c88676f8 | bellard | int j;
|
7008 | c88676f8 | bellard | |
7009 | c88676f8 | bellard | /* find if the memory block is available on a virtual
|
7010 | c88676f8 | bellard | block device */
|
7011 | c88676f8 | bellard | sector_num = -1;
|
7012 | e4bcb14c | ths | for(j = 0; j < nb_drives; j++) {
|
7013 | e4bcb14c | ths | sector_num = bdrv_hash_find(drives_table[j].bdrv,
|
7014 | e4bcb14c | ths | phys_ram_base + i,
|
7015 | e4bcb14c | ths | BDRV_HASH_BLOCK_SIZE);
|
7016 | e4bcb14c | ths | if (sector_num >= 0)
|
7017 | e4bcb14c | ths | break;
|
7018 | c88676f8 | bellard | }
|
7019 | e4bcb14c | ths | if (j == nb_drives)
|
7020 | c88676f8 | bellard | goto normal_compress;
|
7021 | c88676f8 | bellard | buf[0] = 1;
|
7022 | c88676f8 | bellard | buf[1] = j;
|
7023 | c88676f8 | bellard | cpu_to_be64wu((uint64_t *)(buf + 2), sector_num);
|
7024 | c88676f8 | bellard | ram_compress_buf(s, buf, 10);
|
7025 | 5fafdf24 | ths | } else
|
7026 | c88676f8 | bellard | #endif
|
7027 | c88676f8 | bellard | { |
7028 | c88676f8 | bellard | // normal_compress:
|
7029 | c88676f8 | bellard | buf[0] = 0; |
7030 | c88676f8 | bellard | ram_compress_buf(s, buf, 1);
|
7031 | c88676f8 | bellard | ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); |
7032 | c88676f8 | bellard | } |
7033 | 8a7ddc38 | bellard | } |
7034 | c88676f8 | bellard | ram_compress_close(s); |
7035 | 8a7ddc38 | bellard | } |
7036 | 8a7ddc38 | bellard | |
7037 | 8a7ddc38 | bellard | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
7038 | 8a7ddc38 | bellard | { |
7039 | c88676f8 | bellard | RamDecompressState s1, *s = &s1; |
7040 | c88676f8 | bellard | uint8_t buf[10];
|
7041 | 00f82b8a | aurel32 | ram_addr_t i; |
7042 | 8a7ddc38 | bellard | |
7043 | c88676f8 | bellard | if (version_id == 1) |
7044 | c88676f8 | bellard | return ram_load_v1(f, opaque);
|
7045 | c88676f8 | bellard | if (version_id != 2) |
7046 | 8a7ddc38 | bellard | return -EINVAL;
|
7047 | 8a7ddc38 | bellard | if (qemu_get_be32(f) != phys_ram_size)
|
7048 | 8a7ddc38 | bellard | return -EINVAL;
|
7049 | c88676f8 | bellard | if (ram_decompress_open(s, f) < 0) |
7050 | c88676f8 | bellard | return -EINVAL;
|
7051 | c88676f8 | bellard | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
7052 | c88676f8 | bellard | if (ram_decompress_buf(s, buf, 1) < 0) { |
7053 | c88676f8 | bellard | fprintf(stderr, "Error while reading ram block header\n");
|
7054 | c88676f8 | bellard | goto error;
|
7055 | c88676f8 | bellard | } |
7056 | c88676f8 | bellard | if (buf[0] == 0) { |
7057 | c88676f8 | bellard | if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) { |
7058 | 00f82b8a | aurel32 | fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
|
7059 | c88676f8 | bellard | goto error;
|
7060 | c88676f8 | bellard | } |
7061 | 5fafdf24 | ths | } else
|
7062 | c88676f8 | bellard | #if 0
|
7063 | c88676f8 | bellard | if (buf[0] == 1) {
|
7064 | c88676f8 | bellard | int bs_index;
|
7065 | c88676f8 | bellard | int64_t sector_num;
|
7066 | c88676f8 | bellard | |
7067 | c88676f8 | bellard | ram_decompress_buf(s, buf + 1, 9);
|
7068 | c88676f8 | bellard | bs_index = buf[1];
|
7069 | c88676f8 | bellard | sector_num = be64_to_cpupu((const uint64_t *)(buf + 2));
|
7070 | e4bcb14c | ths | if (bs_index >= nb_drives) {
|
7071 | c88676f8 | bellard | fprintf(stderr, "Invalid block device index %d\n", bs_index);
|
7072 | c88676f8 | bellard | goto error;
|
7073 | c88676f8 | bellard | }
|
7074 | e4bcb14c | ths | if (bdrv_read(drives_table[bs_index].bdrv, sector_num,
|
7075 | e4bcb14c | ths | phys_ram_base + i,
|
7076 | c88676f8 | bellard | BDRV_HASH_BLOCK_SIZE / 512) < 0) {
|
7077 | 5fafdf24 | ths | fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n",
|
7078 | c88676f8 | bellard | bs_index, sector_num);
|
7079 | c88676f8 | bellard | goto error;
|
7080 | c88676f8 | bellard | }
|
7081 | 5fafdf24 | ths | } else
|
7082 | c88676f8 | bellard | #endif
|
7083 | c88676f8 | bellard | { |
7084 | c88676f8 | bellard | error:
|
7085 | c88676f8 | bellard | printf("Error block header\n");
|
7086 | c88676f8 | bellard | return -EINVAL;
|
7087 | c88676f8 | bellard | } |
7088 | 8a7ddc38 | bellard | } |
7089 | c88676f8 | bellard | ram_decompress_close(s); |
7090 | 8a7ddc38 | bellard | return 0; |
7091 | 8a7ddc38 | bellard | } |
7092 | 8a7ddc38 | bellard | |
7093 | 8a7ddc38 | bellard | /***********************************************************/
|
7094 | 83f64091 | bellard | /* bottom halves (can be seen as timers which expire ASAP) */
|
7095 | 83f64091 | bellard | |
7096 | 83f64091 | bellard | struct QEMUBH {
|
7097 | 83f64091 | bellard | QEMUBHFunc *cb; |
7098 | 83f64091 | bellard | void *opaque;
|
7099 | 83f64091 | bellard | int scheduled;
|
7100 | 83f64091 | bellard | QEMUBH *next; |
7101 | 83f64091 | bellard | }; |
7102 | 83f64091 | bellard | |
7103 | 83f64091 | bellard | static QEMUBH *first_bh = NULL; |
7104 | 83f64091 | bellard | |
7105 | 83f64091 | bellard | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
|
7106 | 83f64091 | bellard | { |
7107 | 83f64091 | bellard | QEMUBH *bh; |
7108 | 83f64091 | bellard | bh = qemu_mallocz(sizeof(QEMUBH));
|
7109 | 83f64091 | bellard | if (!bh)
|
7110 | 83f64091 | bellard | return NULL; |
7111 | 83f64091 | bellard | bh->cb = cb; |
7112 | 83f64091 | bellard | bh->opaque = opaque; |
7113 | 83f64091 | bellard | return bh;
|
7114 | 83f64091 | bellard | } |
7115 | 83f64091 | bellard | |
7116 | 6eb5733a | bellard | int qemu_bh_poll(void) |
7117 | 83f64091 | bellard | { |
7118 | 83f64091 | bellard | QEMUBH *bh, **pbh; |
7119 | 6eb5733a | bellard | int ret;
|
7120 | 83f64091 | bellard | |
7121 | 6eb5733a | bellard | ret = 0;
|
7122 | 83f64091 | bellard | for(;;) {
|
7123 | 83f64091 | bellard | pbh = &first_bh; |
7124 | 83f64091 | bellard | bh = *pbh; |
7125 | 83f64091 | bellard | if (!bh)
|
7126 | 83f64091 | bellard | break;
|
7127 | 6eb5733a | bellard | ret = 1;
|
7128 | 83f64091 | bellard | *pbh = bh->next; |
7129 | 83f64091 | bellard | bh->scheduled = 0;
|
7130 | 83f64091 | bellard | bh->cb(bh->opaque); |
7131 | 83f64091 | bellard | } |
7132 | 6eb5733a | bellard | return ret;
|
7133 | 83f64091 | bellard | } |
7134 | 83f64091 | bellard | |
7135 | 83f64091 | bellard | void qemu_bh_schedule(QEMUBH *bh)
|
7136 | 83f64091 | bellard | { |
7137 | 83f64091 | bellard | CPUState *env = cpu_single_env; |
7138 | 83f64091 | bellard | if (bh->scheduled)
|
7139 | 83f64091 | bellard | return;
|
7140 | 83f64091 | bellard | bh->scheduled = 1;
|
7141 | 83f64091 | bellard | bh->next = first_bh; |
7142 | 83f64091 | bellard | first_bh = bh; |
7143 | 83f64091 | bellard | |
7144 | 83f64091 | bellard | /* stop the currently executing CPU to execute the BH ASAP */
|
7145 | 83f64091 | bellard | if (env) {
|
7146 | 83f64091 | bellard | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
7147 | 83f64091 | bellard | } |
7148 | 83f64091 | bellard | } |
7149 | 83f64091 | bellard | |
7150 | 83f64091 | bellard | void qemu_bh_cancel(QEMUBH *bh)
|
7151 | 83f64091 | bellard | { |
7152 | 83f64091 | bellard | QEMUBH **pbh; |
7153 | 83f64091 | bellard | if (bh->scheduled) {
|
7154 | 83f64091 | bellard | pbh = &first_bh; |
7155 | 83f64091 | bellard | while (*pbh != bh)
|
7156 | 83f64091 | bellard | pbh = &(*pbh)->next; |
7157 | 83f64091 | bellard | *pbh = bh->next; |
7158 | 83f64091 | bellard | bh->scheduled = 0;
|
7159 | 83f64091 | bellard | } |
7160 | 83f64091 | bellard | } |
7161 | 83f64091 | bellard | |
7162 | 83f64091 | bellard | void qemu_bh_delete(QEMUBH *bh)
|
7163 | 83f64091 | bellard | { |
7164 | 83f64091 | bellard | qemu_bh_cancel(bh); |
7165 | 83f64091 | bellard | qemu_free(bh); |
7166 | 83f64091 | bellard | } |
7167 | 83f64091 | bellard | |
7168 | 83f64091 | bellard | /***********************************************************/
|
7169 | cc1daa40 | bellard | /* machine registration */
|
7170 | cc1daa40 | bellard | |
7171 | cc1daa40 | bellard | QEMUMachine *first_machine = NULL;
|
7172 | cc1daa40 | bellard | |
7173 | cc1daa40 | bellard | int qemu_register_machine(QEMUMachine *m)
|
7174 | cc1daa40 | bellard | { |
7175 | cc1daa40 | bellard | QEMUMachine **pm; |
7176 | cc1daa40 | bellard | pm = &first_machine; |
7177 | cc1daa40 | bellard | while (*pm != NULL) |
7178 | cc1daa40 | bellard | pm = &(*pm)->next; |
7179 | cc1daa40 | bellard | m->next = NULL;
|
7180 | cc1daa40 | bellard | *pm = m; |
7181 | cc1daa40 | bellard | return 0; |
7182 | cc1daa40 | bellard | } |
7183 | cc1daa40 | bellard | |
7184 | 9596ebb7 | pbrook | static QEMUMachine *find_machine(const char *name) |
7185 | cc1daa40 | bellard | { |
7186 | cc1daa40 | bellard | QEMUMachine *m; |
7187 | cc1daa40 | bellard | |
7188 | cc1daa40 | bellard | for(m = first_machine; m != NULL; m = m->next) { |
7189 | cc1daa40 | bellard | if (!strcmp(m->name, name))
|
7190 | cc1daa40 | bellard | return m;
|
7191 | cc1daa40 | bellard | } |
7192 | cc1daa40 | bellard | return NULL; |
7193 | cc1daa40 | bellard | } |
7194 | cc1daa40 | bellard | |
7195 | cc1daa40 | bellard | /***********************************************************/
|
7196 | 8a7ddc38 | bellard | /* main execution loop */
|
7197 | 8a7ddc38 | bellard | |
7198 | 9596ebb7 | pbrook | static void gui_update(void *opaque) |
7199 | 8a7ddc38 | bellard | { |
7200 | 740733bb | ths | DisplayState *ds = opaque; |
7201 | 740733bb | ths | ds->dpy_refresh(ds); |
7202 | f442e08b | aurel32 | qemu_mod_timer(ds->gui_timer, |
7203 | f442e08b | aurel32 | (ds->gui_timer_interval ? |
7204 | f442e08b | aurel32 | ds->gui_timer_interval : |
7205 | f442e08b | aurel32 | GUI_REFRESH_INTERVAL) |
7206 | f442e08b | aurel32 | + qemu_get_clock(rt_clock)); |
7207 | 8a7ddc38 | bellard | } |
7208 | 8a7ddc38 | bellard | |
7209 | 0bd48850 | bellard | struct vm_change_state_entry {
|
7210 | 0bd48850 | bellard | VMChangeStateHandler *cb; |
7211 | 0bd48850 | bellard | void *opaque;
|
7212 | 0bd48850 | bellard | LIST_ENTRY (vm_change_state_entry) entries; |
7213 | 0bd48850 | bellard | }; |
7214 | 0bd48850 | bellard | |
7215 | 0bd48850 | bellard | static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
|
7216 | 0bd48850 | bellard | |
7217 | 0bd48850 | bellard | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
7218 | 0bd48850 | bellard | void *opaque)
|
7219 | 0bd48850 | bellard | { |
7220 | 0bd48850 | bellard | VMChangeStateEntry *e; |
7221 | 0bd48850 | bellard | |
7222 | 0bd48850 | bellard | e = qemu_mallocz(sizeof (*e));
|
7223 | 0bd48850 | bellard | if (!e)
|
7224 | 0bd48850 | bellard | return NULL; |
7225 | 0bd48850 | bellard | |
7226 | 0bd48850 | bellard | e->cb = cb; |
7227 | 0bd48850 | bellard | e->opaque = opaque; |
7228 | 0bd48850 | bellard | LIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
7229 | 0bd48850 | bellard | return e;
|
7230 | 0bd48850 | bellard | } |
7231 | 0bd48850 | bellard | |
7232 | 0bd48850 | bellard | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
|
7233 | 0bd48850 | bellard | { |
7234 | 0bd48850 | bellard | LIST_REMOVE (e, entries); |
7235 | 0bd48850 | bellard | qemu_free (e); |
7236 | 0bd48850 | bellard | } |
7237 | 0bd48850 | bellard | |
7238 | 0bd48850 | bellard | static void vm_state_notify(int running) |
7239 | 0bd48850 | bellard | { |
7240 | 0bd48850 | bellard | VMChangeStateEntry *e; |
7241 | 0bd48850 | bellard | |
7242 | 0bd48850 | bellard | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
|
7243 | 0bd48850 | bellard | e->cb(e->opaque, running); |
7244 | 0bd48850 | bellard | } |
7245 | 0bd48850 | bellard | } |
7246 | 0bd48850 | bellard | |
7247 | 8a7ddc38 | bellard | /* XXX: support several handlers */
|
7248 | 0bd48850 | bellard | static VMStopHandler *vm_stop_cb;
|
7249 | 0bd48850 | bellard | static void *vm_stop_opaque; |
7250 | 8a7ddc38 | bellard | |
7251 | 8a7ddc38 | bellard | int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) |
7252 | 8a7ddc38 | bellard | { |
7253 | 8a7ddc38 | bellard | vm_stop_cb = cb; |
7254 | 8a7ddc38 | bellard | vm_stop_opaque = opaque; |
7255 | 8a7ddc38 | bellard | return 0; |
7256 | 8a7ddc38 | bellard | } |
7257 | 8a7ddc38 | bellard | |
7258 | 8a7ddc38 | bellard | void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) |
7259 | 8a7ddc38 | bellard | { |
7260 | 8a7ddc38 | bellard | vm_stop_cb = NULL;
|
7261 | 8a7ddc38 | bellard | } |
7262 | 8a7ddc38 | bellard | |
7263 | 8a7ddc38 | bellard | void vm_start(void) |
7264 | 8a7ddc38 | bellard | { |
7265 | 8a7ddc38 | bellard | if (!vm_running) {
|
7266 | 8a7ddc38 | bellard | cpu_enable_ticks(); |
7267 | 8a7ddc38 | bellard | vm_running = 1;
|
7268 | 0bd48850 | bellard | vm_state_notify(1);
|
7269 | efe75411 | ths | qemu_rearm_alarm_timer(alarm_timer); |
7270 | 8a7ddc38 | bellard | } |
7271 | 8a7ddc38 | bellard | } |
7272 | 8a7ddc38 | bellard | |
7273 | 5fafdf24 | ths | void vm_stop(int reason) |
7274 | 8a7ddc38 | bellard | { |
7275 | 8a7ddc38 | bellard | if (vm_running) {
|
7276 | 8a7ddc38 | bellard | cpu_disable_ticks(); |
7277 | 8a7ddc38 | bellard | vm_running = 0;
|
7278 | 8a7ddc38 | bellard | if (reason != 0) { |
7279 | 8a7ddc38 | bellard | if (vm_stop_cb) {
|
7280 | 8a7ddc38 | bellard | vm_stop_cb(vm_stop_opaque, reason); |
7281 | 8a7ddc38 | bellard | } |
7282 | 34865134 | bellard | } |
7283 | 0bd48850 | bellard | vm_state_notify(0);
|
7284 | 8a7ddc38 | bellard | } |
7285 | 8a7ddc38 | bellard | } |
7286 | 8a7ddc38 | bellard | |
7287 | bb0c6722 | bellard | /* reset/shutdown handler */
|
7288 | bb0c6722 | bellard | |
7289 | bb0c6722 | bellard | typedef struct QEMUResetEntry { |
7290 | bb0c6722 | bellard | QEMUResetHandler *func; |
7291 | bb0c6722 | bellard | void *opaque;
|
7292 | bb0c6722 | bellard | struct QEMUResetEntry *next;
|
7293 | bb0c6722 | bellard | } QEMUResetEntry; |
7294 | bb0c6722 | bellard | |
7295 | bb0c6722 | bellard | static QEMUResetEntry *first_reset_entry;
|
7296 | bb0c6722 | bellard | static int reset_requested; |
7297 | bb0c6722 | bellard | static int shutdown_requested; |
7298 | 3475187d | bellard | static int powerdown_requested; |
7299 | bb0c6722 | bellard | |
7300 | cf7a2fe2 | aurel32 | int qemu_shutdown_requested(void) |
7301 | cf7a2fe2 | aurel32 | { |
7302 | cf7a2fe2 | aurel32 | int r = shutdown_requested;
|
7303 | cf7a2fe2 | aurel32 | shutdown_requested = 0;
|
7304 | cf7a2fe2 | aurel32 | return r;
|
7305 | cf7a2fe2 | aurel32 | } |
7306 | cf7a2fe2 | aurel32 | |
7307 | cf7a2fe2 | aurel32 | int qemu_reset_requested(void) |
7308 | cf7a2fe2 | aurel32 | { |
7309 | cf7a2fe2 | aurel32 | int r = reset_requested;
|
7310 | cf7a2fe2 | aurel32 | reset_requested = 0;
|
7311 | cf7a2fe2 | aurel32 | return r;
|
7312 | cf7a2fe2 | aurel32 | } |
7313 | cf7a2fe2 | aurel32 | |
7314 | cf7a2fe2 | aurel32 | int qemu_powerdown_requested(void) |
7315 | cf7a2fe2 | aurel32 | { |
7316 | cf7a2fe2 | aurel32 | int r = powerdown_requested;
|
7317 | cf7a2fe2 | aurel32 | powerdown_requested = 0;
|
7318 | cf7a2fe2 | aurel32 | return r;
|
7319 | cf7a2fe2 | aurel32 | } |
7320 | cf7a2fe2 | aurel32 | |
7321 | bb0c6722 | bellard | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
7322 | bb0c6722 | bellard | { |
7323 | bb0c6722 | bellard | QEMUResetEntry **pre, *re; |
7324 | bb0c6722 | bellard | |
7325 | bb0c6722 | bellard | pre = &first_reset_entry; |
7326 | bb0c6722 | bellard | while (*pre != NULL) |
7327 | bb0c6722 | bellard | pre = &(*pre)->next; |
7328 | bb0c6722 | bellard | re = qemu_mallocz(sizeof(QEMUResetEntry));
|
7329 | bb0c6722 | bellard | re->func = func; |
7330 | bb0c6722 | bellard | re->opaque = opaque; |
7331 | bb0c6722 | bellard | re->next = NULL;
|
7332 | bb0c6722 | bellard | *pre = re; |
7333 | bb0c6722 | bellard | } |
7334 | bb0c6722 | bellard | |
7335 | cf7a2fe2 | aurel32 | void qemu_system_reset(void) |
7336 | bb0c6722 | bellard | { |
7337 | bb0c6722 | bellard | QEMUResetEntry *re; |
7338 | bb0c6722 | bellard | |
7339 | bb0c6722 | bellard | /* reset all devices */
|
7340 | bb0c6722 | bellard | for(re = first_reset_entry; re != NULL; re = re->next) { |
7341 | bb0c6722 | bellard | re->func(re->opaque); |
7342 | bb0c6722 | bellard | } |
7343 | bb0c6722 | bellard | } |
7344 | bb0c6722 | bellard | |
7345 | bb0c6722 | bellard | void qemu_system_reset_request(void) |
7346 | bb0c6722 | bellard | { |
7347 | d1beab82 | bellard | if (no_reboot) {
|
7348 | d1beab82 | bellard | shutdown_requested = 1;
|
7349 | d1beab82 | bellard | } else {
|
7350 | d1beab82 | bellard | reset_requested = 1;
|
7351 | d1beab82 | bellard | } |
7352 | 6a00d601 | bellard | if (cpu_single_env)
|
7353 | 6a00d601 | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
7354 | bb0c6722 | bellard | } |
7355 | bb0c6722 | bellard | |
7356 | bb0c6722 | bellard | void qemu_system_shutdown_request(void) |
7357 | bb0c6722 | bellard | { |
7358 | bb0c6722 | bellard | shutdown_requested = 1;
|
7359 | 6a00d601 | bellard | if (cpu_single_env)
|
7360 | 6a00d601 | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
7361 | bb0c6722 | bellard | } |
7362 | bb0c6722 | bellard | |
7363 | 3475187d | bellard | void qemu_system_powerdown_request(void) |
7364 | 3475187d | bellard | { |
7365 | 3475187d | bellard | powerdown_requested = 1;
|
7366 | 6a00d601 | bellard | if (cpu_single_env)
|
7367 | 6a00d601 | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
7368 | bb0c6722 | bellard | } |
7369 | bb0c6722 | bellard | |
7370 | 5905b2e5 | bellard | void main_loop_wait(int timeout) |
7371 | 8a7ddc38 | bellard | { |
7372 | cafffd40 | ths | IOHandlerRecord *ioh; |
7373 | e035649e | bellard | fd_set rfds, wfds, xfds; |
7374 | 877cf882 | ths | int ret, nfds;
|
7375 | 877cf882 | ths | #ifdef _WIN32
|
7376 | 877cf882 | ths | int ret2, i;
|
7377 | 877cf882 | ths | #endif
|
7378 | fd1dff4b | bellard | struct timeval tv;
|
7379 | f331110f | bellard | PollingEntry *pe; |
7380 | f331110f | bellard | |
7381 | c4b1fcc0 | bellard | |
7382 | f331110f | bellard | /* XXX: need to suppress polling by better using win32 events */
|
7383 | f331110f | bellard | ret = 0;
|
7384 | f331110f | bellard | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
7385 | f331110f | bellard | ret |= pe->func(pe->opaque); |
7386 | f331110f | bellard | } |
7387 | 38e205a2 | bellard | #ifdef _WIN32
|
7388 | e6b1e558 | ths | if (ret == 0) { |
7389 | a18e524a | bellard | int err;
|
7390 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
7391 | 3b46e624 | ths | |
7392 | a18e524a | bellard | ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout); |
7393 | a18e524a | bellard | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
7394 | a18e524a | bellard | if (w->func[ret - WAIT_OBJECT_0])
|
7395 | a18e524a | bellard | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
7396 | 3b46e624 | ths | |
7397 | 5fafdf24 | ths | /* Check for additional signaled events */
|
7398 | e6b1e558 | ths | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
7399 | 3b46e624 | ths | |
7400 | e6b1e558 | ths | /* Check if event is signaled */
|
7401 | e6b1e558 | ths | ret2 = WaitForSingleObject(w->events[i], 0);
|
7402 | e6b1e558 | ths | if(ret2 == WAIT_OBJECT_0) {
|
7403 | e6b1e558 | ths | if (w->func[i])
|
7404 | e6b1e558 | ths | w->func[i](w->opaque[i]); |
7405 | e6b1e558 | ths | } else if (ret2 == WAIT_TIMEOUT) { |
7406 | e6b1e558 | ths | } else {
|
7407 | e6b1e558 | ths | err = GetLastError(); |
7408 | e6b1e558 | ths | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
|
7409 | 3b46e624 | ths | } |
7410 | 3b46e624 | ths | } |
7411 | a18e524a | bellard | } else if (ret == WAIT_TIMEOUT) { |
7412 | a18e524a | bellard | } else {
|
7413 | a18e524a | bellard | err = GetLastError(); |
7414 | e6b1e558 | ths | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
|
7415 | a18e524a | bellard | } |
7416 | f331110f | bellard | } |
7417 | fd1dff4b | bellard | #endif
|
7418 | fd1dff4b | bellard | /* poll any events */
|
7419 | fd1dff4b | bellard | /* XXX: separate device handlers from system ones */
|
7420 | fd1dff4b | bellard | nfds = -1;
|
7421 | fd1dff4b | bellard | FD_ZERO(&rfds); |
7422 | fd1dff4b | bellard | FD_ZERO(&wfds); |
7423 | e035649e | bellard | FD_ZERO(&xfds); |
7424 | fd1dff4b | bellard | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
7425 | cafffd40 | ths | if (ioh->deleted)
|
7426 | cafffd40 | ths | continue;
|
7427 | fd1dff4b | bellard | if (ioh->fd_read &&
|
7428 | fd1dff4b | bellard | (!ioh->fd_read_poll || |
7429 | fd1dff4b | bellard | ioh->fd_read_poll(ioh->opaque) != 0)) {
|
7430 | fd1dff4b | bellard | FD_SET(ioh->fd, &rfds); |
7431 | fd1dff4b | bellard | if (ioh->fd > nfds)
|
7432 | fd1dff4b | bellard | nfds = ioh->fd; |
7433 | fd1dff4b | bellard | } |
7434 | fd1dff4b | bellard | if (ioh->fd_write) {
|
7435 | fd1dff4b | bellard | FD_SET(ioh->fd, &wfds); |
7436 | fd1dff4b | bellard | if (ioh->fd > nfds)
|
7437 | fd1dff4b | bellard | nfds = ioh->fd; |
7438 | fd1dff4b | bellard | } |
7439 | fd1dff4b | bellard | } |
7440 | 3b46e624 | ths | |
7441 | fd1dff4b | bellard | tv.tv_sec = 0;
|
7442 | fd1dff4b | bellard | #ifdef _WIN32
|
7443 | fd1dff4b | bellard | tv.tv_usec = 0;
|
7444 | 38e205a2 | bellard | #else
|
7445 | fd1dff4b | bellard | tv.tv_usec = timeout * 1000;
|
7446 | fd1dff4b | bellard | #endif
|
7447 | e035649e | bellard | #if defined(CONFIG_SLIRP)
|
7448 | e035649e | bellard | if (slirp_inited) {
|
7449 | e035649e | bellard | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
7450 | e035649e | bellard | } |
7451 | e035649e | bellard | #endif
|
7452 | e035649e | bellard | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
|
7453 | fd1dff4b | bellard | if (ret > 0) { |
7454 | cafffd40 | ths | IOHandlerRecord **pioh; |
7455 | cafffd40 | ths | |
7456 | cafffd40 | ths | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
7457 | 6ab43fdc | ths | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
|
7458 | fd1dff4b | bellard | ioh->fd_read(ioh->opaque); |
7459 | 7c9d8e07 | bellard | } |
7460 | 6ab43fdc | ths | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
|
7461 | fd1dff4b | bellard | ioh->fd_write(ioh->opaque); |
7462 | c4b1fcc0 | bellard | } |
7463 | b4608c04 | bellard | } |
7464 | cafffd40 | ths | |
7465 | cafffd40 | ths | /* remove deleted IO handlers */
|
7466 | cafffd40 | ths | pioh = &first_io_handler; |
7467 | cafffd40 | ths | while (*pioh) {
|
7468 | cafffd40 | ths | ioh = *pioh; |
7469 | cafffd40 | ths | if (ioh->deleted) {
|
7470 | cafffd40 | ths | *pioh = ioh->next; |
7471 | cafffd40 | ths | qemu_free(ioh); |
7472 | 5fafdf24 | ths | } else
|
7473 | cafffd40 | ths | pioh = &ioh->next; |
7474 | cafffd40 | ths | } |
7475 | fd1dff4b | bellard | } |
7476 | c20709aa | bellard | #if defined(CONFIG_SLIRP)
|
7477 | fd1dff4b | bellard | if (slirp_inited) {
|
7478 | e035649e | bellard | if (ret < 0) { |
7479 | e035649e | bellard | FD_ZERO(&rfds); |
7480 | e035649e | bellard | FD_ZERO(&wfds); |
7481 | e035649e | bellard | FD_ZERO(&xfds); |
7482 | c20709aa | bellard | } |
7483 | e035649e | bellard | slirp_select_poll(&rfds, &wfds, &xfds); |
7484 | fd1dff4b | bellard | } |
7485 | c20709aa | bellard | #endif
|
7486 | 83f64091 | bellard | qemu_aio_poll(); |
7487 | c20709aa | bellard | |
7488 | fd1dff4b | bellard | if (vm_running) {
|
7489 | 21b20814 | edgar_igl | if (likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
|
7490 | 5fafdf24 | ths | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
7491 | fd1dff4b | bellard | qemu_get_clock(vm_clock)); |
7492 | fd1dff4b | bellard | /* run dma transfers, if any */
|
7493 | fd1dff4b | bellard | DMA_run(); |
7494 | fd1dff4b | bellard | } |
7495 | 423f0742 | pbrook | |
7496 | fd1dff4b | bellard | /* real time timers */
|
7497 | 5fafdf24 | ths | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
7498 | fd1dff4b | bellard | qemu_get_clock(rt_clock)); |
7499 | 423f0742 | pbrook | |
7500 | d5d08334 | balrog | if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
|
7501 | d5d08334 | balrog | alarm_timer->flags &= ~(ALARM_FLAG_EXPIRED); |
7502 | d5d08334 | balrog | qemu_rearm_alarm_timer(alarm_timer); |
7503 | d5d08334 | balrog | } |
7504 | b99dc0d1 | balrog | |
7505 | 423f0742 | pbrook | /* Check bottom-halves last in case any of the earlier events triggered
|
7506 | 423f0742 | pbrook | them. */
|
7507 | 423f0742 | pbrook | qemu_bh_poll(); |
7508 | 3b46e624 | ths | |
7509 | 5905b2e5 | bellard | } |
7510 | 5905b2e5 | bellard | |
7511 | 9596ebb7 | pbrook | static int main_loop(void) |
7512 | 5905b2e5 | bellard | { |
7513 | 5905b2e5 | bellard | int ret, timeout;
|
7514 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7515 | 89bfc105 | bellard | int64_t ti; |
7516 | 89bfc105 | bellard | #endif
|
7517 | 6a00d601 | bellard | CPUState *env; |
7518 | 5905b2e5 | bellard | |
7519 | 6a00d601 | bellard | cur_cpu = first_cpu; |
7520 | ee5605e5 | balrog | next_cpu = cur_cpu->next_cpu ?: first_cpu; |
7521 | 5905b2e5 | bellard | for(;;) {
|
7522 | 5905b2e5 | bellard | if (vm_running) {
|
7523 | 15a76449 | bellard | |
7524 | 15a76449 | bellard | for(;;) {
|
7525 | 15a76449 | bellard | /* get next cpu */
|
7526 | ee5605e5 | balrog | env = next_cpu; |
7527 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7528 | 89bfc105 | bellard | ti = profile_getclock(); |
7529 | 89bfc105 | bellard | #endif
|
7530 | 2e70f6ef | pbrook | if (use_icount) {
|
7531 | 2e70f6ef | pbrook | int64_t count; |
7532 | 2e70f6ef | pbrook | int decr;
|
7533 | 2e70f6ef | pbrook | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
7534 | 2e70f6ef | pbrook | env->icount_decr.u16.low = 0;
|
7535 | 2e70f6ef | pbrook | env->icount_extra = 0;
|
7536 | 2e70f6ef | pbrook | count = qemu_next_deadline(); |
7537 | 2e70f6ef | pbrook | count = (count + (1 << icount_time_shift) - 1) |
7538 | 2e70f6ef | pbrook | >> icount_time_shift; |
7539 | 2e70f6ef | pbrook | qemu_icount += count; |
7540 | 2e70f6ef | pbrook | decr = (count > 0xffff) ? 0xffff : count; |
7541 | 2e70f6ef | pbrook | count -= decr; |
7542 | 2e70f6ef | pbrook | env->icount_decr.u16.low = decr; |
7543 | 2e70f6ef | pbrook | env->icount_extra = count; |
7544 | 2e70f6ef | pbrook | } |
7545 | 6a00d601 | bellard | ret = cpu_exec(env); |
7546 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7547 | 89bfc105 | bellard | qemu_time += profile_getclock() - ti; |
7548 | 89bfc105 | bellard | #endif
|
7549 | 2e70f6ef | pbrook | if (use_icount) {
|
7550 | 2e70f6ef | pbrook | /* Fold pending instructions back into the
|
7551 | 2e70f6ef | pbrook | instruction counter, and clear the interrupt flag. */
|
7552 | 2e70f6ef | pbrook | qemu_icount -= (env->icount_decr.u16.low |
7553 | 2e70f6ef | pbrook | + env->icount_extra); |
7554 | 2e70f6ef | pbrook | env->icount_decr.u32 = 0;
|
7555 | 2e70f6ef | pbrook | env->icount_extra = 0;
|
7556 | 2e70f6ef | pbrook | } |
7557 | ee5605e5 | balrog | next_cpu = env->next_cpu ?: first_cpu; |
7558 | 95b01009 | aurel32 | if (event_pending && likely(ret != EXCP_DEBUG)) {
|
7559 | ee5605e5 | balrog | ret = EXCP_INTERRUPT; |
7560 | ee5605e5 | balrog | event_pending = 0;
|
7561 | ee5605e5 | balrog | break;
|
7562 | ee5605e5 | balrog | } |
7563 | bd967e05 | pbrook | if (ret == EXCP_HLT) {
|
7564 | bd967e05 | pbrook | /* Give the next CPU a chance to run. */
|
7565 | bd967e05 | pbrook | cur_cpu = env; |
7566 | bd967e05 | pbrook | continue;
|
7567 | bd967e05 | pbrook | } |
7568 | 15a76449 | bellard | if (ret != EXCP_HALTED)
|
7569 | 15a76449 | bellard | break;
|
7570 | 15a76449 | bellard | /* all CPUs are halted ? */
|
7571 | bd967e05 | pbrook | if (env == cur_cpu)
|
7572 | 15a76449 | bellard | break;
|
7573 | 15a76449 | bellard | } |
7574 | 15a76449 | bellard | cur_cpu = env; |
7575 | 15a76449 | bellard | |
7576 | 5905b2e5 | bellard | if (shutdown_requested) {
|
7577 | 3475187d | bellard | ret = EXCP_INTERRUPT; |
7578 | b2f76161 | aurel32 | if (no_shutdown) {
|
7579 | b2f76161 | aurel32 | vm_stop(0);
|
7580 | b2f76161 | aurel32 | no_shutdown = 0;
|
7581 | b2f76161 | aurel32 | } |
7582 | b2f76161 | aurel32 | else
|
7583 | b2f76161 | aurel32 | break;
|
7584 | 5905b2e5 | bellard | } |
7585 | 5905b2e5 | bellard | if (reset_requested) {
|
7586 | 5905b2e5 | bellard | reset_requested = 0;
|
7587 | 5905b2e5 | bellard | qemu_system_reset(); |
7588 | 3475187d | bellard | ret = EXCP_INTERRUPT; |
7589 | 3475187d | bellard | } |
7590 | 3475187d | bellard | if (powerdown_requested) {
|
7591 | 3475187d | bellard | powerdown_requested = 0;
|
7592 | 3475187d | bellard | qemu_system_powerdown(); |
7593 | 3475187d | bellard | ret = EXCP_INTERRUPT; |
7594 | 5905b2e5 | bellard | } |
7595 | 95b01009 | aurel32 | if (unlikely(ret == EXCP_DEBUG)) {
|
7596 | 5905b2e5 | bellard | vm_stop(EXCP_DEBUG); |
7597 | 5905b2e5 | bellard | } |
7598 | bd967e05 | pbrook | /* If all cpus are halted then wait until the next IRQ */
|
7599 | 5905b2e5 | bellard | /* XXX: use timeout computed from timers */
|
7600 | 2e70f6ef | pbrook | if (ret == EXCP_HALTED) {
|
7601 | 2e70f6ef | pbrook | if (use_icount) {
|
7602 | 2e70f6ef | pbrook | int64_t add; |
7603 | 2e70f6ef | pbrook | int64_t delta; |
7604 | 2e70f6ef | pbrook | /* Advance virtual time to the next event. */
|
7605 | 2e70f6ef | pbrook | if (use_icount == 1) { |
7606 | 2e70f6ef | pbrook | /* When not using an adaptive execution frequency
|
7607 | 2e70f6ef | pbrook | we tend to get badly out of sync with real time,
|
7608 | bf20dc07 | ths | so just delay for a reasonable amount of time. */
|
7609 | 2e70f6ef | pbrook | delta = 0;
|
7610 | 2e70f6ef | pbrook | } else {
|
7611 | 2e70f6ef | pbrook | delta = cpu_get_icount() - cpu_get_clock(); |
7612 | 2e70f6ef | pbrook | } |
7613 | 2e70f6ef | pbrook | if (delta > 0) { |
7614 | 2e70f6ef | pbrook | /* If virtual time is ahead of real time then just
|
7615 | 2e70f6ef | pbrook | wait for IO. */
|
7616 | 2e70f6ef | pbrook | timeout = (delta / 1000000) + 1; |
7617 | 2e70f6ef | pbrook | } else {
|
7618 | 2e70f6ef | pbrook | /* Wait for either IO to occur or the next
|
7619 | 2e70f6ef | pbrook | timer event. */
|
7620 | 2e70f6ef | pbrook | add = qemu_next_deadline(); |
7621 | 2e70f6ef | pbrook | /* We advance the timer before checking for IO.
|
7622 | 2e70f6ef | pbrook | Limit the amount we advance so that early IO
|
7623 | 2e70f6ef | pbrook | activity won't get the guest too far ahead. */
|
7624 | 2e70f6ef | pbrook | if (add > 10000000) |
7625 | 2e70f6ef | pbrook | add = 10000000;
|
7626 | 2e70f6ef | pbrook | delta += add; |
7627 | 2e70f6ef | pbrook | add = (add + (1 << icount_time_shift) - 1) |
7628 | 2e70f6ef | pbrook | >> icount_time_shift; |
7629 | 2e70f6ef | pbrook | qemu_icount += add; |
7630 | 2e70f6ef | pbrook | timeout = delta / 1000000;
|
7631 | 2e70f6ef | pbrook | if (timeout < 0) |
7632 | 2e70f6ef | pbrook | timeout = 0;
|
7633 | 2e70f6ef | pbrook | } |
7634 | 2e70f6ef | pbrook | } else {
|
7635 | 2e70f6ef | pbrook | timeout = 10;
|
7636 | 2e70f6ef | pbrook | } |
7637 | 2e70f6ef | pbrook | } else {
|
7638 | 5905b2e5 | bellard | timeout = 0;
|
7639 | 2e70f6ef | pbrook | } |
7640 | 5905b2e5 | bellard | } else {
|
7641 | 5b08fc10 | aliguori | if (shutdown_requested)
|
7642 | 5b08fc10 | aliguori | break;
|
7643 | 5905b2e5 | bellard | timeout = 10;
|
7644 | 5905b2e5 | bellard | } |
7645 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7646 | 89bfc105 | bellard | ti = profile_getclock(); |
7647 | 89bfc105 | bellard | #endif
|
7648 | 5905b2e5 | bellard | main_loop_wait(timeout); |
7649 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7650 | 89bfc105 | bellard | dev_time += profile_getclock() - ti; |
7651 | 89bfc105 | bellard | #endif
|
7652 | b4608c04 | bellard | } |
7653 | 34865134 | bellard | cpu_disable_ticks(); |
7654 | 34865134 | bellard | return ret;
|
7655 | b4608c04 | bellard | } |
7656 | b4608c04 | bellard | |
7657 | 15f82208 | ths | static void help(int exitcode) |
7658 | 0824d6fc | bellard | { |
7659 | 68d0f70e | bellard | printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n" |
7660 | 0db63474 | bellard | "usage: %s [options] [disk_image]\n"
|
7661 | 0824d6fc | bellard | "\n"
|
7662 | a20dd508 | bellard | "'disk_image' is a raw hard image image for IDE hard disk 0\n"
|
7663 | fc01f7e7 | bellard | "\n"
|
7664 | a20dd508 | bellard | "Standard options:\n"
|
7665 | cc1daa40 | bellard | "-M machine select emulated machine (-M ? for list)\n"
|
7666 | 5adb4839 | pbrook | "-cpu cpu select CPU (-cpu ? for list)\n"
|
7667 | c45886db | bellard | "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
|
7668 | 36b486bb | bellard | "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
|
7669 | 36b486bb | bellard | "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
|
7670 | c4b1fcc0 | bellard | "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
|
7671 | a1620fac | aurel32 | "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
|
7672 | a1620fac | aurel32 | " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
|
7673 | a1620fac | aurel32 | " [,cache=on|off][,format=f]\n"
|
7674 | e4bcb14c | ths | " use 'file' as a drive image\n"
|
7675 | 3e3d5815 | balrog | "-mtdblock file use 'file' as on-board Flash memory image\n"
|
7676 | a1bb27b1 | pbrook | "-sd file use 'file' as SecureDigital card image\n"
|
7677 | 86f55663 | j_mayer | "-pflash file use 'file' as a parallel flash image\n"
|
7678 | eec85c2a | ths | "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
|
7679 | 667accab | ths | "-snapshot write to temporary files instead of disk image files\n"
|
7680 | 667accab | ths | #ifdef CONFIG_SDL
|
7681 | 43523e93 | ths | "-no-frame open SDL window without a frame and window decorations\n"
|
7682 | 3780e197 | ths | "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
|
7683 | 667accab | ths | "-no-quit disable SDL window close capability\n"
|
7684 | 667accab | ths | #endif
|
7685 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7686 | 52ca8d6a | bellard | "-no-fd-bootchk disable boot signature checking for floppy disks\n"
|
7687 | 52ca8d6a | bellard | #endif
|
7688 | a00bad7e | bellard | "-m megs set virtual RAM size to megs MB [default=%d]\n"
|
7689 | 91fc2119 | bellard | "-smp n set the number of CPUs to 'n' [default=1]\n"
|
7690 | c4b1fcc0 | bellard | "-nographic disable graphical output and redirect serial I/Os to console\n"
|
7691 | a171fe39 | balrog | "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
|
7692 | 4ca0074c | bellard | #ifndef _WIN32
|
7693 | 667accab | ths | "-k language use keyboard layout (for example \"fr\" for French)\n"
|
7694 | 4ca0074c | bellard | #endif
|
7695 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7696 | 1d14ffa9 | bellard | "-audio-help print list of audio drivers and their options\n"
|
7697 | c0fe3827 | bellard | "-soundhw c1,... enable audio support\n"
|
7698 | c0fe3827 | bellard | " and only specified sound cards (comma separated list)\n"
|
7699 | c0fe3827 | bellard | " use -soundhw ? to get the list of supported cards\n"
|
7700 | 6a36d84e | bellard | " use -soundhw all to enable all of them\n"
|
7701 | 1d14ffa9 | bellard | #endif
|
7702 | 89980284 | bellard | "-localtime set the real time clock to local time [default=utc]\n"
|
7703 | d63d307f | bellard | "-full-screen start in full screen\n"
|
7704 | a09db21f | bellard | #ifdef TARGET_I386
|
7705 | a09db21f | bellard | "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
|
7706 | a09db21f | bellard | #endif
|
7707 | b389dbfb | bellard | "-usb enable the USB driver (will be the default soon)\n"
|
7708 | b389dbfb | bellard | "-usbdevice name add the host or guest USB device 'name'\n"
|
7709 | 6f7e9aec | bellard | #if defined(TARGET_PPC) || defined(TARGET_SPARC)
|
7710 | 6f7e9aec | bellard | "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
|
7711 | bb0c6722 | bellard | #endif
|
7712 | c35734b2 | ths | "-name string set the name of the guest\n"
|
7713 | c4b1fcc0 | bellard | "\n"
|
7714 | c4b1fcc0 | bellard | "Network options:\n"
|
7715 | a41b2ff2 | pbrook | "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
|
7716 | 7c9d8e07 | bellard | " create a new Network Interface Card and connect it to VLAN 'n'\n"
|
7717 | c20709aa | bellard | #ifdef CONFIG_SLIRP
|
7718 | 115defd1 | pbrook | "-net user[,vlan=n][,hostname=host]\n"
|
7719 | 115defd1 | pbrook | " connect the user mode network stack to VLAN 'n' and send\n"
|
7720 | 115defd1 | pbrook | " hostname 'host' to DHCP clients\n"
|
7721 | 7c9d8e07 | bellard | #endif
|
7722 | 7fb843f8 | bellard | #ifdef _WIN32
|
7723 | 7fb843f8 | bellard | "-net tap[,vlan=n],ifname=name\n"
|
7724 | 7fb843f8 | bellard | " connect the host TAP network interface to VLAN 'n'\n"
|
7725 | 7fb843f8 | bellard | #else
|
7726 | b46a8906 | ths | "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
|
7727 | b46a8906 | ths | " connect the host TAP network interface to VLAN 'n' and use the\n"
|
7728 | b46a8906 | ths | " network scripts 'file' (default=%s)\n"
|
7729 | b46a8906 | ths | " and 'dfile' (default=%s);\n"
|
7730 | b46a8906 | ths | " use '[down]script=no' to disable script execution;\n"
|
7731 | 7c9d8e07 | bellard | " use 'fd=h' to connect to an already opened TAP interface\n"
|
7732 | 7fb843f8 | bellard | #endif
|
7733 | 6a00d601 | bellard | "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
|
7734 | 7c9d8e07 | bellard | " connect the vlan 'n' to another VLAN using a socket connection\n"
|
7735 | 3d830459 | bellard | "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
|
7736 | 3d830459 | bellard | " connect the vlan 'n' to multicast maddr and port\n"
|
7737 | 8a16d273 | ths | #ifdef CONFIG_VDE
|
7738 | 8a16d273 | ths | "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
|
7739 | 8a16d273 | ths | " connect the vlan 'n' to port 'n' of a vde switch running\n"
|
7740 | 8a16d273 | ths | " on host and listening for incoming connections on 'socketpath'.\n"
|
7741 | 8a16d273 | ths | " Use group 'groupname' and mode 'octalmode' to change default\n"
|
7742 | 8a16d273 | ths | " ownership and permissions for communication port.\n"
|
7743 | 8a16d273 | ths | #endif
|
7744 | 7c9d8e07 | bellard | "-net none use it alone to have zero network devices; if no -net option\n"
|
7745 | 7c9d8e07 | bellard | " is provided, the default is '-net nic -net user'\n"
|
7746 | 7c9d8e07 | bellard | "\n"
|
7747 | 7c9d8e07 | bellard | #ifdef CONFIG_SLIRP
|
7748 | 0db1137d | ths | "-tftp dir allow tftp access to files in dir [-net user]\n"
|
7749 | 47d5d01a | ths | "-bootp file advertise file in BOOTP replies\n"
|
7750 | 7c9d8e07 | bellard | #ifndef _WIN32
|
7751 | 7c9d8e07 | bellard | "-smb dir allow SMB access to files in 'dir' [-net user]\n"
|
7752 | c94c8d64 | bellard | #endif
|
7753 | 9bf05444 | bellard | "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
|
7754 | 7c9d8e07 | bellard | " redirect TCP or UDP connections from host to guest [-net user]\n"
|
7755 | c20709aa | bellard | #endif
|
7756 | a20dd508 | bellard | "\n"
|
7757 | c4b1fcc0 | bellard | "Linux boot specific:\n"
|
7758 | a20dd508 | bellard | "-kernel bzImage use 'bzImage' as kernel image\n"
|
7759 | a20dd508 | bellard | "-append cmdline use 'cmdline' as kernel command line\n"
|
7760 | a20dd508 | bellard | "-initrd file use 'file' as initial ram disk\n"
|
7761 | fc01f7e7 | bellard | "\n"
|
7762 | 330d0414 | bellard | "Debug/Expert options:\n"
|
7763 | 82c643ff | bellard | "-monitor dev redirect the monitor to char device 'dev'\n"
|
7764 | 82c643ff | bellard | "-serial dev redirect the serial port to char device 'dev'\n"
|
7765 | 6508fe59 | bellard | "-parallel dev redirect the parallel port to char device 'dev'\n"
|
7766 | f7cce898 | bellard | "-pidfile file Write PID to 'file'\n"
|
7767 | cd6f1169 | bellard | "-S freeze CPU at startup (use 'c' to start execution)\n"
|
7768 | cfc3475a | pbrook | "-s wait gdb connection to port\n"
|
7769 | cfc3475a | pbrook | "-p port set gdb connection port [default=%s]\n"
|
7770 | f193c797 | bellard | "-d item1,... output log to %s (use -d ? for a list of log items)\n"
|
7771 | 46d4767d | bellard | "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
|
7772 | 46d4767d | bellard | " translation (t=none or lba) (usually qemu can guess them)\n"
|
7773 | 87b47350 | bellard | "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
|
7774 | d993e026 | bellard | #ifdef USE_KQEMU
|
7775 | 6515b203 | bellard | "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
|
7776 | d993e026 | bellard | "-no-kqemu disable KQEMU kernel module usage\n"
|
7777 | d993e026 | bellard | #endif
|
7778 | bb0c6722 | bellard | #ifdef TARGET_I386
|
7779 | 1bfe856e | bellard | "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
|
7780 | 1bfe856e | bellard | " (default is CL-GD5446 PCI VGA)\n"
|
7781 | 6515b203 | bellard | "-no-acpi disable ACPI\n"
|
7782 | bb0c6722 | bellard | #endif
|
7783 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
7784 | 4d3b6f6e | balrog | "-curses use a curses/ncurses interface instead of SDL\n"
|
7785 | 4d3b6f6e | balrog | #endif
|
7786 | d1beab82 | bellard | "-no-reboot exit instead of rebooting\n"
|
7787 | b2f76161 | aurel32 | "-no-shutdown stop before shutdown\n"
|
7788 | a8080006 | aurel32 | "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
|
7789 | 24236869 | bellard | "-vnc display start a VNC server on display\n"
|
7790 | 71e3ceb8 | ths | #ifndef _WIN32
|
7791 | 71e3ceb8 | ths | "-daemonize daemonize QEMU after initializing\n"
|
7792 | 71e3ceb8 | ths | #endif
|
7793 | 9ae02555 | ths | "-option-rom rom load a file, rom, into the option ROM space\n"
|
7794 | 66508601 | blueswir1 | #ifdef TARGET_SPARC
|
7795 | 66508601 | blueswir1 | "-prom-env variable=value set OpenBIOS nvram variables\n"
|
7796 | 66508601 | blueswir1 | #endif
|
7797 | f3dcfada | ths | "-clock force the use of the given methods for timer alarm.\n"
|
7798 | 3adda04c | aurel32 | " To see what timers are available use -clock ?\n"
|
7799 | bce61846 | bellard | "-startdate select initial date of the clock\n"
|
7800 | 2e70f6ef | pbrook | "-icount [N|auto]\n"
|
7801 | dd5d6fe9 | pbrook | " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
|
7802 | 0824d6fc | bellard | "\n"
|
7803 | 82c643ff | bellard | "During emulation, the following keys are useful:\n"
|
7804 | 032a8c9e | bellard | "ctrl-alt-f toggle full screen\n"
|
7805 | 032a8c9e | bellard | "ctrl-alt-n switch to virtual console 'n'\n"
|
7806 | 032a8c9e | bellard | "ctrl-alt toggle mouse and keyboard grab\n"
|
7807 | 82c643ff | bellard | "\n"
|
7808 | 82c643ff | bellard | "When using -nographic, press 'ctrl-a h' to get some help.\n"
|
7809 | 82c643ff | bellard | , |
7810 | 0db63474 | bellard | "qemu",
|
7811 | a00bad7e | bellard | DEFAULT_RAM_SIZE, |
7812 | 7c9d8e07 | bellard | #ifndef _WIN32
|
7813 | a00bad7e | bellard | DEFAULT_NETWORK_SCRIPT, |
7814 | b46a8906 | ths | DEFAULT_NETWORK_DOWN_SCRIPT, |
7815 | 7c9d8e07 | bellard | #endif
|
7816 | 6e44ba7f | bellard | DEFAULT_GDBSTUB_PORT, |
7817 | bce61846 | bellard | "/tmp/qemu.log");
|
7818 | 15f82208 | ths | exit(exitcode); |
7819 | 0824d6fc | bellard | } |
7820 | 0824d6fc | bellard | |
7821 | cd6f1169 | bellard | #define HAS_ARG 0x0001 |
7822 | cd6f1169 | bellard | |
7823 | cd6f1169 | bellard | enum {
|
7824 | cd6f1169 | bellard | QEMU_OPTION_h, |
7825 | cd6f1169 | bellard | |
7826 | cc1daa40 | bellard | QEMU_OPTION_M, |
7827 | 94fc95cd | j_mayer | QEMU_OPTION_cpu, |
7828 | cd6f1169 | bellard | QEMU_OPTION_fda, |
7829 | cd6f1169 | bellard | QEMU_OPTION_fdb, |
7830 | cd6f1169 | bellard | QEMU_OPTION_hda, |
7831 | cd6f1169 | bellard | QEMU_OPTION_hdb, |
7832 | cd6f1169 | bellard | QEMU_OPTION_hdc, |
7833 | cd6f1169 | bellard | QEMU_OPTION_hdd, |
7834 | e4bcb14c | ths | QEMU_OPTION_drive, |
7835 | cd6f1169 | bellard | QEMU_OPTION_cdrom, |
7836 | 3e3d5815 | balrog | QEMU_OPTION_mtdblock, |
7837 | a1bb27b1 | pbrook | QEMU_OPTION_sd, |
7838 | 86f55663 | j_mayer | QEMU_OPTION_pflash, |
7839 | cd6f1169 | bellard | QEMU_OPTION_boot, |
7840 | cd6f1169 | bellard | QEMU_OPTION_snapshot, |
7841 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7842 | 52ca8d6a | bellard | QEMU_OPTION_no_fd_bootchk, |
7843 | 52ca8d6a | bellard | #endif
|
7844 | cd6f1169 | bellard | QEMU_OPTION_m, |
7845 | cd6f1169 | bellard | QEMU_OPTION_nographic, |
7846 | a171fe39 | balrog | QEMU_OPTION_portrait, |
7847 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7848 | 1d14ffa9 | bellard | QEMU_OPTION_audio_help, |
7849 | 1d14ffa9 | bellard | QEMU_OPTION_soundhw, |
7850 | 1d14ffa9 | bellard | #endif
|
7851 | cd6f1169 | bellard | |
7852 | 7c9d8e07 | bellard | QEMU_OPTION_net, |
7853 | c7f74643 | bellard | QEMU_OPTION_tftp, |
7854 | 47d5d01a | ths | QEMU_OPTION_bootp, |
7855 | 9d728e8c | bellard | QEMU_OPTION_smb, |
7856 | 9bf05444 | bellard | QEMU_OPTION_redir, |
7857 | cd6f1169 | bellard | |
7858 | cd6f1169 | bellard | QEMU_OPTION_kernel, |
7859 | cd6f1169 | bellard | QEMU_OPTION_append, |
7860 | cd6f1169 | bellard | QEMU_OPTION_initrd, |
7861 | cd6f1169 | bellard | |
7862 | cd6f1169 | bellard | QEMU_OPTION_S, |
7863 | cd6f1169 | bellard | QEMU_OPTION_s, |
7864 | cd6f1169 | bellard | QEMU_OPTION_p, |
7865 | cd6f1169 | bellard | QEMU_OPTION_d, |
7866 | cd6f1169 | bellard | QEMU_OPTION_hdachs, |
7867 | cd6f1169 | bellard | QEMU_OPTION_L, |
7868 | 1192dad8 | j_mayer | QEMU_OPTION_bios, |
7869 | 3d11d0eb | bellard | QEMU_OPTION_k, |
7870 | ee22c2f7 | bellard | QEMU_OPTION_localtime, |
7871 | 1f04275e | bellard | QEMU_OPTION_cirrusvga, |
7872 | d34cab9f | ths | QEMU_OPTION_vmsvga, |
7873 | e9b137c2 | bellard | QEMU_OPTION_g, |
7874 | 1bfe856e | bellard | QEMU_OPTION_std_vga, |
7875 | 20d8a3ed | ths | QEMU_OPTION_echr, |
7876 | 82c643ff | bellard | QEMU_OPTION_monitor, |
7877 | 82c643ff | bellard | QEMU_OPTION_serial, |
7878 | 6508fe59 | bellard | QEMU_OPTION_parallel, |
7879 | d63d307f | bellard | QEMU_OPTION_loadvm, |
7880 | d63d307f | bellard | QEMU_OPTION_full_screen, |
7881 | 43523e93 | ths | QEMU_OPTION_no_frame, |
7882 | 3780e197 | ths | QEMU_OPTION_alt_grab, |
7883 | 667accab | ths | QEMU_OPTION_no_quit, |
7884 | f7cce898 | bellard | QEMU_OPTION_pidfile, |
7885 | d993e026 | bellard | QEMU_OPTION_no_kqemu, |
7886 | 89bfc105 | bellard | QEMU_OPTION_kernel_kqemu, |
7887 | a09db21f | bellard | QEMU_OPTION_win2k_hack, |
7888 | bb36d470 | bellard | QEMU_OPTION_usb, |
7889 | a594cfbf | bellard | QEMU_OPTION_usbdevice, |
7890 | 6a00d601 | bellard | QEMU_OPTION_smp, |
7891 | 24236869 | bellard | QEMU_OPTION_vnc, |
7892 | 6515b203 | bellard | QEMU_OPTION_no_acpi, |
7893 | 4d3b6f6e | balrog | QEMU_OPTION_curses, |
7894 | d1beab82 | bellard | QEMU_OPTION_no_reboot, |
7895 | b2f76161 | aurel32 | QEMU_OPTION_no_shutdown, |
7896 | 9467cd46 | balrog | QEMU_OPTION_show_cursor, |
7897 | 71e3ceb8 | ths | QEMU_OPTION_daemonize, |
7898 | 9ae02555 | ths | QEMU_OPTION_option_rom, |
7899 | c35734b2 | ths | QEMU_OPTION_semihosting, |
7900 | c35734b2 | ths | QEMU_OPTION_name, |
7901 | 66508601 | blueswir1 | QEMU_OPTION_prom_env, |
7902 | 2b8f2d41 | balrog | QEMU_OPTION_old_param, |
7903 | f3dcfada | ths | QEMU_OPTION_clock, |
7904 | 7e0af5d0 | bellard | QEMU_OPTION_startdate, |
7905 | 26a5f13b | bellard | QEMU_OPTION_tb_size, |
7906 | 2e70f6ef | pbrook | QEMU_OPTION_icount, |
7907 | cd6f1169 | bellard | }; |
7908 | cd6f1169 | bellard | |
7909 | cd6f1169 | bellard | typedef struct QEMUOption { |
7910 | cd6f1169 | bellard | const char *name; |
7911 | cd6f1169 | bellard | int flags;
|
7912 | cd6f1169 | bellard | int index;
|
7913 | cd6f1169 | bellard | } QEMUOption; |
7914 | cd6f1169 | bellard | |
7915 | cd6f1169 | bellard | const QEMUOption qemu_options[] = {
|
7916 | cd6f1169 | bellard | { "h", 0, QEMU_OPTION_h }, |
7917 | 64423fb2 | pbrook | { "help", 0, QEMU_OPTION_h }, |
7918 | cd6f1169 | bellard | |
7919 | cc1daa40 | bellard | { "M", HAS_ARG, QEMU_OPTION_M },
|
7920 | 94fc95cd | j_mayer | { "cpu", HAS_ARG, QEMU_OPTION_cpu },
|
7921 | cd6f1169 | bellard | { "fda", HAS_ARG, QEMU_OPTION_fda },
|
7922 | cd6f1169 | bellard | { "fdb", HAS_ARG, QEMU_OPTION_fdb },
|
7923 | cd6f1169 | bellard | { "hda", HAS_ARG, QEMU_OPTION_hda },
|
7924 | cd6f1169 | bellard | { "hdb", HAS_ARG, QEMU_OPTION_hdb },
|
7925 | cd6f1169 | bellard | { "hdc", HAS_ARG, QEMU_OPTION_hdc },
|
7926 | cd6f1169 | bellard | { "hdd", HAS_ARG, QEMU_OPTION_hdd },
|
7927 | e4bcb14c | ths | { "drive", HAS_ARG, QEMU_OPTION_drive },
|
7928 | cd6f1169 | bellard | { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
|
7929 | 3e3d5815 | balrog | { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
|
7930 | a1bb27b1 | pbrook | { "sd", HAS_ARG, QEMU_OPTION_sd },
|
7931 | 86f55663 | j_mayer | { "pflash", HAS_ARG, QEMU_OPTION_pflash },
|
7932 | cd6f1169 | bellard | { "boot", HAS_ARG, QEMU_OPTION_boot },
|
7933 | cd6f1169 | bellard | { "snapshot", 0, QEMU_OPTION_snapshot }, |
7934 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7935 | 52ca8d6a | bellard | { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk }, |
7936 | 52ca8d6a | bellard | #endif
|
7937 | cd6f1169 | bellard | { "m", HAS_ARG, QEMU_OPTION_m },
|
7938 | cd6f1169 | bellard | { "nographic", 0, QEMU_OPTION_nographic }, |
7939 | a171fe39 | balrog | { "portrait", 0, QEMU_OPTION_portrait }, |
7940 | 3d11d0eb | bellard | { "k", HAS_ARG, QEMU_OPTION_k },
|
7941 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7942 | 1d14ffa9 | bellard | { "audio-help", 0, QEMU_OPTION_audio_help }, |
7943 | 1d14ffa9 | bellard | { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
|
7944 | 1d14ffa9 | bellard | #endif
|
7945 | cd6f1169 | bellard | |
7946 | 7c9d8e07 | bellard | { "net", HAS_ARG, QEMU_OPTION_net},
|
7947 | 158156d1 | bellard | #ifdef CONFIG_SLIRP
|
7948 | c7f74643 | bellard | { "tftp", HAS_ARG, QEMU_OPTION_tftp },
|
7949 | 47d5d01a | ths | { "bootp", HAS_ARG, QEMU_OPTION_bootp },
|
7950 | c94c8d64 | bellard | #ifndef _WIN32
|
7951 | 9d728e8c | bellard | { "smb", HAS_ARG, QEMU_OPTION_smb },
|
7952 | c94c8d64 | bellard | #endif
|
7953 | 9bf05444 | bellard | { "redir", HAS_ARG, QEMU_OPTION_redir },
|
7954 | 158156d1 | bellard | #endif
|
7955 | cd6f1169 | bellard | |
7956 | cd6f1169 | bellard | { "kernel", HAS_ARG, QEMU_OPTION_kernel },
|
7957 | cd6f1169 | bellard | { "append", HAS_ARG, QEMU_OPTION_append },
|
7958 | cd6f1169 | bellard | { "initrd", HAS_ARG, QEMU_OPTION_initrd },
|
7959 | cd6f1169 | bellard | |
7960 | cd6f1169 | bellard | { "S", 0, QEMU_OPTION_S }, |
7961 | cd6f1169 | bellard | { "s", 0, QEMU_OPTION_s }, |
7962 | cd6f1169 | bellard | { "p", HAS_ARG, QEMU_OPTION_p },
|
7963 | cd6f1169 | bellard | { "d", HAS_ARG, QEMU_OPTION_d },
|
7964 | cd6f1169 | bellard | { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
|
7965 | cd6f1169 | bellard | { "L", HAS_ARG, QEMU_OPTION_L },
|
7966 | 1192dad8 | j_mayer | { "bios", HAS_ARG, QEMU_OPTION_bios },
|
7967 | d993e026 | bellard | #ifdef USE_KQEMU
|
7968 | d993e026 | bellard | { "no-kqemu", 0, QEMU_OPTION_no_kqemu }, |
7969 | 89bfc105 | bellard | { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu }, |
7970 | d993e026 | bellard | #endif
|
7971 | 6f7e9aec | bellard | #if defined(TARGET_PPC) || defined(TARGET_SPARC)
|
7972 | e9b137c2 | bellard | { "g", 1, QEMU_OPTION_g }, |
7973 | 77d4bc34 | bellard | #endif
|
7974 | ee22c2f7 | bellard | { "localtime", 0, QEMU_OPTION_localtime }, |
7975 | 1bfe856e | bellard | { "std-vga", 0, QEMU_OPTION_std_vga }, |
7976 | 8b6e0729 | balrog | { "echr", HAS_ARG, QEMU_OPTION_echr },
|
7977 | 8b6e0729 | balrog | { "monitor", HAS_ARG, QEMU_OPTION_monitor },
|
7978 | 8b6e0729 | balrog | { "serial", HAS_ARG, QEMU_OPTION_serial },
|
7979 | 8b6e0729 | balrog | { "parallel", HAS_ARG, QEMU_OPTION_parallel },
|
7980 | d63d307f | bellard | { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
|
7981 | d63d307f | bellard | { "full-screen", 0, QEMU_OPTION_full_screen }, |
7982 | 667accab | ths | #ifdef CONFIG_SDL
|
7983 | 43523e93 | ths | { "no-frame", 0, QEMU_OPTION_no_frame }, |
7984 | 3780e197 | ths | { "alt-grab", 0, QEMU_OPTION_alt_grab }, |
7985 | 667accab | ths | { "no-quit", 0, QEMU_OPTION_no_quit }, |
7986 | 667accab | ths | #endif
|
7987 | f7cce898 | bellard | { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
|
7988 | a09db21f | bellard | { "win2k-hack", 0, QEMU_OPTION_win2k_hack }, |
7989 | a594cfbf | bellard | { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
|
7990 | 6a00d601 | bellard | { "smp", HAS_ARG, QEMU_OPTION_smp },
|
7991 | 24236869 | bellard | { "vnc", HAS_ARG, QEMU_OPTION_vnc },
|
7992 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
7993 | 4d3b6f6e | balrog | { "curses", 0, QEMU_OPTION_curses }, |
7994 | 4d3b6f6e | balrog | #endif
|
7995 | 96d30e48 | ths | |
7996 | 1f04275e | bellard | /* temporary options */
|
7997 | a594cfbf | bellard | { "usb", 0, QEMU_OPTION_usb }, |
7998 | 1f04275e | bellard | { "cirrusvga", 0, QEMU_OPTION_cirrusvga }, |
7999 | d34cab9f | ths | { "vmwarevga", 0, QEMU_OPTION_vmsvga }, |
8000 | 6515b203 | bellard | { "no-acpi", 0, QEMU_OPTION_no_acpi }, |
8001 | d1beab82 | bellard | { "no-reboot", 0, QEMU_OPTION_no_reboot }, |
8002 | b2f76161 | aurel32 | { "no-shutdown", 0, QEMU_OPTION_no_shutdown }, |
8003 | 9467cd46 | balrog | { "show-cursor", 0, QEMU_OPTION_show_cursor }, |
8004 | 71e3ceb8 | ths | { "daemonize", 0, QEMU_OPTION_daemonize }, |
8005 | 9ae02555 | ths | { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
|
8006 | a87295e8 | pbrook | #if defined(TARGET_ARM) || defined(TARGET_M68K)
|
8007 | 8e71621f | pbrook | { "semihosting", 0, QEMU_OPTION_semihosting }, |
8008 | 8e71621f | pbrook | #endif
|
8009 | c35734b2 | ths | { "name", HAS_ARG, QEMU_OPTION_name },
|
8010 | 66508601 | blueswir1 | #if defined(TARGET_SPARC)
|
8011 | 66508601 | blueswir1 | { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
|
8012 | 66508601 | blueswir1 | #endif
|
8013 | 2b8f2d41 | balrog | #if defined(TARGET_ARM)
|
8014 | 2b8f2d41 | balrog | { "old-param", 0, QEMU_OPTION_old_param }, |
8015 | 2b8f2d41 | balrog | #endif
|
8016 | f3dcfada | ths | { "clock", HAS_ARG, QEMU_OPTION_clock },
|
8017 | 7e0af5d0 | bellard | { "startdate", HAS_ARG, QEMU_OPTION_startdate },
|
8018 | 26a5f13b | bellard | { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
|
8019 | 2e70f6ef | pbrook | { "icount", HAS_ARG, QEMU_OPTION_icount },
|
8020 | cd6f1169 | bellard | { NULL },
|
8021 | fc01f7e7 | bellard | }; |
8022 | fc01f7e7 | bellard | |
8023 | 5905b2e5 | bellard | /* password input */
|
8024 | 5905b2e5 | bellard | |
8025 | 2bac6019 | balrog | int qemu_key_check(BlockDriverState *bs, const char *name) |
8026 | 2bac6019 | balrog | { |
8027 | 2bac6019 | balrog | char password[256]; |
8028 | 2bac6019 | balrog | int i;
|
8029 | 2bac6019 | balrog | |
8030 | 2bac6019 | balrog | if (!bdrv_is_encrypted(bs))
|
8031 | 2bac6019 | balrog | return 0; |
8032 | 2bac6019 | balrog | |
8033 | 2bac6019 | balrog | term_printf("%s is encrypted.\n", name);
|
8034 | 2bac6019 | balrog | for(i = 0; i < 3; i++) { |
8035 | 2bac6019 | balrog | monitor_readline("Password: ", 1, password, sizeof(password)); |
8036 | 2bac6019 | balrog | if (bdrv_set_key(bs, password) == 0) |
8037 | 2bac6019 | balrog | return 0; |
8038 | 2bac6019 | balrog | term_printf("invalid password\n");
|
8039 | 2bac6019 | balrog | } |
8040 | 2bac6019 | balrog | return -EPERM;
|
8041 | 2bac6019 | balrog | } |
8042 | 2bac6019 | balrog | |
8043 | 83ab7950 | aliguori | static BlockDriverState *get_bdrv(int index) |
8044 | 83ab7950 | aliguori | { |
8045 | 83ab7950 | aliguori | if (index > nb_drives)
|
8046 | 83ab7950 | aliguori | return NULL; |
8047 | 83ab7950 | aliguori | return drives_table[index].bdrv;
|
8048 | 83ab7950 | aliguori | } |
8049 | 83ab7950 | aliguori | |
8050 | 5905b2e5 | bellard | static void read_passwords(void) |
8051 | 5905b2e5 | bellard | { |
8052 | 5905b2e5 | bellard | BlockDriverState *bs; |
8053 | 2bac6019 | balrog | int i;
|
8054 | 5905b2e5 | bellard | |
8055 | 83ab7950 | aliguori | for(i = 0; i < 6; i++) { |
8056 | 83ab7950 | aliguori | bs = get_bdrv(i); |
8057 | 83ab7950 | aliguori | if (bs)
|
8058 | 83ab7950 | aliguori | qemu_key_check(bs, bdrv_get_device_name(bs)); |
8059 | 5905b2e5 | bellard | } |
8060 | 5905b2e5 | bellard | } |
8061 | 5905b2e5 | bellard | |
8062 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
8063 | 6a36d84e | bellard | struct soundhw soundhw[] = {
|
8064 | b00052e4 | balrog | #ifdef HAS_AUDIO_CHOICE
|
8065 | 4ce7ff6e | aurel32 | #if defined(TARGET_I386) || defined(TARGET_MIPS)
|
8066 | fd06c375 | bellard | { |
8067 | fd06c375 | bellard | "pcspk",
|
8068 | fd06c375 | bellard | "PC speaker",
|
8069 | fd06c375 | bellard | 0,
|
8070 | fd06c375 | bellard | 1,
|
8071 | fd06c375 | bellard | { .init_isa = pcspk_audio_init } |
8072 | fd06c375 | bellard | }, |
8073 | fd06c375 | bellard | #endif
|
8074 | 6a36d84e | bellard | { |
8075 | 6a36d84e | bellard | "sb16",
|
8076 | 6a36d84e | bellard | "Creative Sound Blaster 16",
|
8077 | 6a36d84e | bellard | 0,
|
8078 | 6a36d84e | bellard | 1,
|
8079 | 6a36d84e | bellard | { .init_isa = SB16_init } |
8080 | 6a36d84e | bellard | }, |
8081 | 6a36d84e | bellard | |
8082 | cc53d26d | malc | #ifdef CONFIG_CS4231A
|
8083 | cc53d26d | malc | { |
8084 | cc53d26d | malc | "cs4231a",
|
8085 | cc53d26d | malc | "CS4231A",
|
8086 | cc53d26d | malc | 0,
|
8087 | cc53d26d | malc | 1,
|
8088 | cc53d26d | malc | { .init_isa = cs4231a_init } |
8089 | cc53d26d | malc | }, |
8090 | cc53d26d | malc | #endif
|
8091 | cc53d26d | malc | |
8092 | 1d14ffa9 | bellard | #ifdef CONFIG_ADLIB
|
8093 | 6a36d84e | bellard | { |
8094 | 6a36d84e | bellard | "adlib",
|
8095 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
8096 | 6a36d84e | bellard | "Yamaha YMF262 (OPL3)",
|
8097 | 1d14ffa9 | bellard | #else
|
8098 | 6a36d84e | bellard | "Yamaha YM3812 (OPL2)",
|
8099 | 1d14ffa9 | bellard | #endif
|
8100 | 6a36d84e | bellard | 0,
|
8101 | 6a36d84e | bellard | 1,
|
8102 | 6a36d84e | bellard | { .init_isa = Adlib_init } |
8103 | 6a36d84e | bellard | }, |
8104 | 1d14ffa9 | bellard | #endif
|
8105 | 6a36d84e | bellard | |
8106 | 1d14ffa9 | bellard | #ifdef CONFIG_GUS
|
8107 | 6a36d84e | bellard | { |
8108 | 6a36d84e | bellard | "gus",
|
8109 | 6a36d84e | bellard | "Gravis Ultrasound GF1",
|
8110 | 6a36d84e | bellard | 0,
|
8111 | 6a36d84e | bellard | 1,
|
8112 | 6a36d84e | bellard | { .init_isa = GUS_init } |
8113 | 6a36d84e | bellard | }, |
8114 | 1d14ffa9 | bellard | #endif
|
8115 | 6a36d84e | bellard | |
8116 | e5c9a13e | balrog | #ifdef CONFIG_AC97
|
8117 | e5c9a13e | balrog | { |
8118 | e5c9a13e | balrog | "ac97",
|
8119 | e5c9a13e | balrog | "Intel 82801AA AC97 Audio",
|
8120 | e5c9a13e | balrog | 0,
|
8121 | e5c9a13e | balrog | 0,
|
8122 | e5c9a13e | balrog | { .init_pci = ac97_init } |
8123 | e5c9a13e | balrog | }, |
8124 | e5c9a13e | balrog | #endif
|
8125 | e5c9a13e | balrog | |
8126 | 6a36d84e | bellard | { |
8127 | 6a36d84e | bellard | "es1370",
|
8128 | 6a36d84e | bellard | "ENSONIQ AudioPCI ES1370",
|
8129 | 6a36d84e | bellard | 0,
|
8130 | 6a36d84e | bellard | 0,
|
8131 | 6a36d84e | bellard | { .init_pci = es1370_init } |
8132 | 6a36d84e | bellard | }, |
8133 | b00052e4 | balrog | #endif
|
8134 | 6a36d84e | bellard | |
8135 | 6a36d84e | bellard | { NULL, NULL, 0, 0, { NULL } } |
8136 | 6a36d84e | bellard | }; |
8137 | 6a36d84e | bellard | |
8138 | 6a36d84e | bellard | static void select_soundhw (const char *optarg) |
8139 | 6a36d84e | bellard | { |
8140 | 6a36d84e | bellard | struct soundhw *c;
|
8141 | 6a36d84e | bellard | |
8142 | 6a36d84e | bellard | if (*optarg == '?') { |
8143 | 6a36d84e | bellard | show_valid_cards:
|
8144 | 6a36d84e | bellard | |
8145 | 6a36d84e | bellard | printf ("Valid sound card names (comma separated):\n");
|
8146 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
8147 | 6a36d84e | bellard | printf ("%-11s %s\n", c->name, c->descr);
|
8148 | 6a36d84e | bellard | } |
8149 | 6a36d84e | bellard | printf ("\n-soundhw all will enable all of the above\n");
|
8150 | 1d14ffa9 | bellard | exit (*optarg != '?');
|
8151 | 1d14ffa9 | bellard | } |
8152 | 1d14ffa9 | bellard | else {
|
8153 | 6a36d84e | bellard | size_t l; |
8154 | 1d14ffa9 | bellard | const char *p; |
8155 | 1d14ffa9 | bellard | char *e;
|
8156 | 1d14ffa9 | bellard | int bad_card = 0; |
8157 | 1d14ffa9 | bellard | |
8158 | 6a36d84e | bellard | if (!strcmp (optarg, "all")) { |
8159 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
8160 | 6a36d84e | bellard | c->enabled = 1;
|
8161 | 6a36d84e | bellard | } |
8162 | 6a36d84e | bellard | return;
|
8163 | 6a36d84e | bellard | } |
8164 | 1d14ffa9 | bellard | |
8165 | 6a36d84e | bellard | p = optarg; |
8166 | 1d14ffa9 | bellard | while (*p) {
|
8167 | 1d14ffa9 | bellard | e = strchr (p, ',');
|
8168 | 1d14ffa9 | bellard | l = !e ? strlen (p) : (size_t) (e - p); |
8169 | 6a36d84e | bellard | |
8170 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
8171 | 6a36d84e | bellard | if (!strncmp (c->name, p, l)) {
|
8172 | 6a36d84e | bellard | c->enabled = 1;
|
8173 | 1d14ffa9 | bellard | break;
|
8174 | 1d14ffa9 | bellard | } |
8175 | 1d14ffa9 | bellard | } |
8176 | 6a36d84e | bellard | |
8177 | 6a36d84e | bellard | if (!c->name) {
|
8178 | 1d14ffa9 | bellard | if (l > 80) { |
8179 | 1d14ffa9 | bellard | fprintf (stderr, |
8180 | 1d14ffa9 | bellard | "Unknown sound card name (too big to show)\n");
|
8181 | 1d14ffa9 | bellard | } |
8182 | 1d14ffa9 | bellard | else {
|
8183 | 1d14ffa9 | bellard | fprintf (stderr, "Unknown sound card name `%.*s'\n",
|
8184 | 1d14ffa9 | bellard | (int) l, p);
|
8185 | 1d14ffa9 | bellard | } |
8186 | 1d14ffa9 | bellard | bad_card = 1;
|
8187 | 1d14ffa9 | bellard | } |
8188 | 1d14ffa9 | bellard | p += l + (e != NULL);
|
8189 | 1d14ffa9 | bellard | } |
8190 | 1d14ffa9 | bellard | |
8191 | 1d14ffa9 | bellard | if (bad_card)
|
8192 | 1d14ffa9 | bellard | goto show_valid_cards;
|
8193 | 1d14ffa9 | bellard | } |
8194 | 1d14ffa9 | bellard | } |
8195 | 1d14ffa9 | bellard | #endif
|
8196 | 1d14ffa9 | bellard | |
8197 | 3587d7e6 | bellard | #ifdef _WIN32
|
8198 | 3587d7e6 | bellard | static BOOL WINAPI qemu_ctrl_handler(DWORD type)
|
8199 | 3587d7e6 | bellard | { |
8200 | 3587d7e6 | bellard | exit(STATUS_CONTROL_C_EXIT); |
8201 | 3587d7e6 | bellard | return TRUE;
|
8202 | 3587d7e6 | bellard | } |
8203 | 3587d7e6 | bellard | #endif
|
8204 | 3587d7e6 | bellard | |
8205 | 7c9d8e07 | bellard | #define MAX_NET_CLIENTS 32 |
8206 | c20709aa | bellard | |
8207 | 5b08fc10 | aliguori | #ifndef _WIN32
|
8208 | 5b08fc10 | aliguori | |
8209 | 5b08fc10 | aliguori | static void termsig_handler(int signal) |
8210 | 5b08fc10 | aliguori | { |
8211 | 5b08fc10 | aliguori | qemu_system_shutdown_request(); |
8212 | 5b08fc10 | aliguori | } |
8213 | 5b08fc10 | aliguori | |
8214 | 5b08fc10 | aliguori | void termsig_setup(void) |
8215 | 5b08fc10 | aliguori | { |
8216 | 5b08fc10 | aliguori | struct sigaction act;
|
8217 | 5b08fc10 | aliguori | |
8218 | 5b08fc10 | aliguori | memset(&act, 0, sizeof(act)); |
8219 | 5b08fc10 | aliguori | act.sa_handler = termsig_handler; |
8220 | 5b08fc10 | aliguori | sigaction(SIGINT, &act, NULL);
|
8221 | 5b08fc10 | aliguori | sigaction(SIGHUP, &act, NULL);
|
8222 | 5b08fc10 | aliguori | sigaction(SIGTERM, &act, NULL);
|
8223 | 5b08fc10 | aliguori | } |
8224 | 5b08fc10 | aliguori | |
8225 | 5b08fc10 | aliguori | #endif
|
8226 | 5b08fc10 | aliguori | |
8227 | 0824d6fc | bellard | int main(int argc, char **argv) |
8228 | 0824d6fc | bellard | { |
8229 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
8230 | cfc3475a | pbrook | int use_gdbstub;
|
8231 | cfc3475a | pbrook | const char *gdbstub_port; |
8232 | 67b915a5 | bellard | #endif
|
8233 | 28c5af54 | j_mayer | uint32_t boot_devices_bitmap = 0;
|
8234 | e4bcb14c | ths | int i;
|
8235 | 28c5af54 | j_mayer | int snapshot, linux_boot, net_boot;
|
8236 | 7f7f9873 | bellard | const char *initrd_filename; |
8237 | a20dd508 | bellard | const char *kernel_filename, *kernel_cmdline; |
8238 | 28c5af54 | j_mayer | const char *boot_devices = ""; |
8239 | 313aa567 | bellard | DisplayState *ds = &display_state; |
8240 | 46d4767d | bellard | int cyls, heads, secs, translation;
|
8241 | fd5f393a | pbrook | const char *net_clients[MAX_NET_CLIENTS]; |
8242 | 7c9d8e07 | bellard | int nb_net_clients;
|
8243 | e4bcb14c | ths | int hda_index;
|
8244 | cd6f1169 | bellard | int optind;
|
8245 | cd6f1169 | bellard | const char *r, *optarg; |
8246 | 82c643ff | bellard | CharDriverState *monitor_hd; |
8247 | fd5f393a | pbrook | const char *monitor_device; |
8248 | fd5f393a | pbrook | const char *serial_devices[MAX_SERIAL_PORTS]; |
8249 | 8d11df9e | bellard | int serial_device_index;
|
8250 | fd5f393a | pbrook | const char *parallel_devices[MAX_PARALLEL_PORTS]; |
8251 | 6508fe59 | bellard | int parallel_device_index;
|
8252 | d63d307f | bellard | const char *loadvm = NULL; |
8253 | cc1daa40 | bellard | QEMUMachine *machine; |
8254 | 94fc95cd | j_mayer | const char *cpu_model; |
8255 | fd5f393a | pbrook | const char *usb_devices[MAX_USB_CMDLINE]; |
8256 | a594cfbf | bellard | int usb_devices_index;
|
8257 | 71e3ceb8 | ths | int fds[2]; |
8258 | 26a5f13b | bellard | int tb_size;
|
8259 | 93815bc2 | ths | const char *pid_file = NULL; |
8260 | 833c7174 | blueswir1 | VLANState *vlan; |
8261 | 0bd48850 | bellard | |
8262 | 0bd48850 | bellard | LIST_INIT (&vm_change_state_head); |
8263 | be995c27 | bellard | #ifndef _WIN32
|
8264 | be995c27 | bellard | { |
8265 | be995c27 | bellard | struct sigaction act;
|
8266 | be995c27 | bellard | sigfillset(&act.sa_mask); |
8267 | be995c27 | bellard | act.sa_flags = 0;
|
8268 | be995c27 | bellard | act.sa_handler = SIG_IGN; |
8269 | be995c27 | bellard | sigaction(SIGPIPE, &act, NULL);
|
8270 | be995c27 | bellard | } |
8271 | 3587d7e6 | bellard | #else
|
8272 | 3587d7e6 | bellard | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
8273 | a8e5ac33 | bellard | /* Note: cpu_interrupt() is currently not SMP safe, so we force
|
8274 | a8e5ac33 | bellard | QEMU to run on a single CPU */
|
8275 | a8e5ac33 | bellard | { |
8276 | a8e5ac33 | bellard | HANDLE h; |
8277 | a8e5ac33 | bellard | DWORD mask, smask; |
8278 | a8e5ac33 | bellard | int i;
|
8279 | a8e5ac33 | bellard | h = GetCurrentProcess(); |
8280 | a8e5ac33 | bellard | if (GetProcessAffinityMask(h, &mask, &smask)) {
|
8281 | a8e5ac33 | bellard | for(i = 0; i < 32; i++) { |
8282 | a8e5ac33 | bellard | if (mask & (1 << i)) |
8283 | a8e5ac33 | bellard | break;
|
8284 | a8e5ac33 | bellard | } |
8285 | a8e5ac33 | bellard | if (i != 32) { |
8286 | a8e5ac33 | bellard | mask = 1 << i;
|
8287 | a8e5ac33 | bellard | SetProcessAffinityMask(h, mask); |
8288 | a8e5ac33 | bellard | } |
8289 | a8e5ac33 | bellard | } |
8290 | a8e5ac33 | bellard | } |
8291 | 67b915a5 | bellard | #endif
|
8292 | be995c27 | bellard | |
8293 | cc1daa40 | bellard | register_machines(); |
8294 | cc1daa40 | bellard | machine = first_machine; |
8295 | 94fc95cd | j_mayer | cpu_model = NULL;
|
8296 | fc01f7e7 | bellard | initrd_filename = NULL;
|
8297 | 4fc5d071 | aurel32 | ram_size = 0;
|
8298 | 313aa567 | bellard | vga_ram_size = VGA_RAM_SIZE; |
8299 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
8300 | b4608c04 | bellard | use_gdbstub = 0;
|
8301 | c636bb66 | bellard | gdbstub_port = DEFAULT_GDBSTUB_PORT; |
8302 | 67b915a5 | bellard | #endif
|
8303 | 33e3963e | bellard | snapshot = 0;
|
8304 | a20dd508 | bellard | nographic = 0;
|
8305 | 4d3b6f6e | balrog | curses = 0;
|
8306 | a20dd508 | bellard | kernel_filename = NULL;
|
8307 | a20dd508 | bellard | kernel_cmdline = "";
|
8308 | c4b1fcc0 | bellard | cyls = heads = secs = 0;
|
8309 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_AUTO; |
8310 | c60e08d9 | pbrook | monitor_device = "vc";
|
8311 | c4b1fcc0 | bellard | |
8312 | c75a823c | aurel32 | serial_devices[0] = "vc:80Cx24C"; |
8313 | 8d11df9e | bellard | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
8314 | fd5f393a | pbrook | serial_devices[i] = NULL;
|
8315 | 8d11df9e | bellard | serial_device_index = 0;
|
8316 | 3b46e624 | ths | |
8317 | c75a823c | aurel32 | parallel_devices[0] = "vc:640x480"; |
8318 | 6508fe59 | bellard | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
8319 | fd5f393a | pbrook | parallel_devices[i] = NULL;
|
8320 | 6508fe59 | bellard | parallel_device_index = 0;
|
8321 | 3b46e624 | ths | |
8322 | a594cfbf | bellard | usb_devices_index = 0;
|
8323 | 3b46e624 | ths | |
8324 | 7c9d8e07 | bellard | nb_net_clients = 0;
|
8325 | e4bcb14c | ths | nb_drives = 0;
|
8326 | e4bcb14c | ths | nb_drives_opt = 0;
|
8327 | e4bcb14c | ths | hda_index = -1;
|
8328 | 7c9d8e07 | bellard | |
8329 | 7c9d8e07 | bellard | nb_nics = 0;
|
8330 | 3b46e624 | ths | |
8331 | 26a5f13b | bellard | tb_size = 0;
|
8332 | 26a5f13b | bellard | |
8333 | cd6f1169 | bellard | optind = 1;
|
8334 | 0824d6fc | bellard | for(;;) {
|
8335 | cd6f1169 | bellard | if (optind >= argc)
|
8336 | 0824d6fc | bellard | break;
|
8337 | cd6f1169 | bellard | r = argv[optind]; |
8338 | cd6f1169 | bellard | if (r[0] != '-') { |
8339 | 609497ab | balrog | hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
|
8340 | cd6f1169 | bellard | } else {
|
8341 | cd6f1169 | bellard | const QEMUOption *popt;
|
8342 | cd6f1169 | bellard | |
8343 | cd6f1169 | bellard | optind++; |
8344 | dff5efc8 | pbrook | /* Treat --foo the same as -foo. */
|
8345 | dff5efc8 | pbrook | if (r[1] == '-') |
8346 | dff5efc8 | pbrook | r++; |
8347 | cd6f1169 | bellard | popt = qemu_options; |
8348 | cd6f1169 | bellard | for(;;) {
|
8349 | cd6f1169 | bellard | if (!popt->name) {
|
8350 | 5fafdf24 | ths | fprintf(stderr, "%s: invalid option -- '%s'\n",
|
8351 | cd6f1169 | bellard | argv[0], r);
|
8352 | cd6f1169 | bellard | exit(1);
|
8353 | cd6f1169 | bellard | } |
8354 | cd6f1169 | bellard | if (!strcmp(popt->name, r + 1)) |
8355 | cd6f1169 | bellard | break;
|
8356 | cd6f1169 | bellard | popt++; |
8357 | cd6f1169 | bellard | } |
8358 | cd6f1169 | bellard | if (popt->flags & HAS_ARG) {
|
8359 | cd6f1169 | bellard | if (optind >= argc) {
|
8360 | cd6f1169 | bellard | fprintf(stderr, "%s: option '%s' requires an argument\n",
|
8361 | cd6f1169 | bellard | argv[0], r);
|
8362 | cd6f1169 | bellard | exit(1);
|
8363 | cd6f1169 | bellard | } |
8364 | cd6f1169 | bellard | optarg = argv[optind++]; |
8365 | cd6f1169 | bellard | } else {
|
8366 | cd6f1169 | bellard | optarg = NULL;
|
8367 | cd6f1169 | bellard | } |
8368 | cd6f1169 | bellard | |
8369 | cd6f1169 | bellard | switch(popt->index) {
|
8370 | cc1daa40 | bellard | case QEMU_OPTION_M:
|
8371 | cc1daa40 | bellard | machine = find_machine(optarg); |
8372 | cc1daa40 | bellard | if (!machine) {
|
8373 | cc1daa40 | bellard | QEMUMachine *m; |
8374 | cc1daa40 | bellard | printf("Supported machines are:\n");
|
8375 | cc1daa40 | bellard | for(m = first_machine; m != NULL; m = m->next) { |
8376 | cc1daa40 | bellard | printf("%-10s %s%s\n",
|
8377 | 5fafdf24 | ths | m->name, m->desc, |
8378 | cc1daa40 | bellard | m == first_machine ? " (default)" : ""); |
8379 | cc1daa40 | bellard | } |
8380 | 15f82208 | ths | exit(*optarg != '?');
|
8381 | cc1daa40 | bellard | } |
8382 | cc1daa40 | bellard | break;
|
8383 | 94fc95cd | j_mayer | case QEMU_OPTION_cpu:
|
8384 | 94fc95cd | j_mayer | /* hw initialization will check this */
|
8385 | 15f82208 | ths | if (*optarg == '?') { |
8386 | c732abe2 | j_mayer | /* XXX: implement xxx_cpu_list for targets that still miss it */
|
8387 | c732abe2 | j_mayer | #if defined(cpu_list)
|
8388 | c732abe2 | j_mayer | cpu_list(stdout, &fprintf); |
8389 | 94fc95cd | j_mayer | #endif
|
8390 | 15f82208 | ths | exit(0);
|
8391 | 94fc95cd | j_mayer | } else {
|
8392 | 94fc95cd | j_mayer | cpu_model = optarg; |
8393 | 94fc95cd | j_mayer | } |
8394 | 94fc95cd | j_mayer | break;
|
8395 | cd6f1169 | bellard | case QEMU_OPTION_initrd:
|
8396 | fc01f7e7 | bellard | initrd_filename = optarg; |
8397 | fc01f7e7 | bellard | break;
|
8398 | cd6f1169 | bellard | case QEMU_OPTION_hda:
|
8399 | e4bcb14c | ths | if (cyls == 0) |
8400 | 609497ab | balrog | hda_index = drive_add(optarg, HD_ALIAS, 0);
|
8401 | e4bcb14c | ths | else
|
8402 | 609497ab | balrog | hda_index = drive_add(optarg, HD_ALIAS |
8403 | e4bcb14c | ths | ",cyls=%d,heads=%d,secs=%d%s",
|
8404 | 609497ab | balrog | 0, cyls, heads, secs,
|
8405 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_LBA ? |
8406 | e4bcb14c | ths | ",trans=lba" :
|
8407 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_NONE ? |
8408 | e4bcb14c | ths | ",trans=none" : ""); |
8409 | e4bcb14c | ths | break;
|
8410 | cd6f1169 | bellard | case QEMU_OPTION_hdb:
|
8411 | cc1daa40 | bellard | case QEMU_OPTION_hdc:
|
8412 | cc1daa40 | bellard | case QEMU_OPTION_hdd:
|
8413 | 609497ab | balrog | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
8414 | fc01f7e7 | bellard | break;
|
8415 | e4bcb14c | ths | case QEMU_OPTION_drive:
|
8416 | 609497ab | balrog | drive_add(NULL, "%s", optarg); |
8417 | e4bcb14c | ths | break;
|
8418 | 3e3d5815 | balrog | case QEMU_OPTION_mtdblock:
|
8419 | 609497ab | balrog | drive_add(optarg, MTD_ALIAS); |
8420 | 3e3d5815 | balrog | break;
|
8421 | a1bb27b1 | pbrook | case QEMU_OPTION_sd:
|
8422 | 609497ab | balrog | drive_add(optarg, SD_ALIAS); |
8423 | a1bb27b1 | pbrook | break;
|
8424 | 86f55663 | j_mayer | case QEMU_OPTION_pflash:
|
8425 | 609497ab | balrog | drive_add(optarg, PFLASH_ALIAS); |
8426 | 86f55663 | j_mayer | break;
|
8427 | cd6f1169 | bellard | case QEMU_OPTION_snapshot:
|
8428 | 33e3963e | bellard | snapshot = 1;
|
8429 | 33e3963e | bellard | break;
|
8430 | cd6f1169 | bellard | case QEMU_OPTION_hdachs:
|
8431 | 330d0414 | bellard | { |
8432 | 330d0414 | bellard | const char *p; |
8433 | 330d0414 | bellard | p = optarg; |
8434 | 330d0414 | bellard | cyls = strtol(p, (char **)&p, 0); |
8435 | 46d4767d | bellard | if (cyls < 1 || cyls > 16383) |
8436 | 46d4767d | bellard | goto chs_fail;
|
8437 | 330d0414 | bellard | if (*p != ',') |
8438 | 330d0414 | bellard | goto chs_fail;
|
8439 | 330d0414 | bellard | p++; |
8440 | 330d0414 | bellard | heads = strtol(p, (char **)&p, 0); |
8441 | 46d4767d | bellard | if (heads < 1 || heads > 16) |
8442 | 46d4767d | bellard | goto chs_fail;
|
8443 | 330d0414 | bellard | if (*p != ',') |
8444 | 330d0414 | bellard | goto chs_fail;
|
8445 | 330d0414 | bellard | p++; |
8446 | 330d0414 | bellard | secs = strtol(p, (char **)&p, 0); |
8447 | 46d4767d | bellard | if (secs < 1 || secs > 63) |
8448 | 46d4767d | bellard | goto chs_fail;
|
8449 | 46d4767d | bellard | if (*p == ',') { |
8450 | 46d4767d | bellard | p++; |
8451 | 46d4767d | bellard | if (!strcmp(p, "none")) |
8452 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_NONE; |
8453 | 46d4767d | bellard | else if (!strcmp(p, "lba")) |
8454 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_LBA; |
8455 | 46d4767d | bellard | else if (!strcmp(p, "auto")) |
8456 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_AUTO; |
8457 | 46d4767d | bellard | else
|
8458 | 46d4767d | bellard | goto chs_fail;
|
8459 | 46d4767d | bellard | } else if (*p != '\0') { |
8460 | c4b1fcc0 | bellard | chs_fail:
|
8461 | 46d4767d | bellard | fprintf(stderr, "qemu: invalid physical CHS format\n");
|
8462 | 46d4767d | bellard | exit(1);
|
8463 | c4b1fcc0 | bellard | } |
8464 | e4bcb14c | ths | if (hda_index != -1) |
8465 | 609497ab | balrog | snprintf(drives_opt[hda_index].opt, |
8466 | 609497ab | balrog | sizeof(drives_opt[hda_index].opt),
|
8467 | 609497ab | balrog | HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
|
8468 | 609497ab | balrog | 0, cyls, heads, secs,
|
8469 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_LBA ? |
8470 | e4bcb14c | ths | ",trans=lba" :
|
8471 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_NONE ? |
8472 | e4bcb14c | ths | ",trans=none" : ""); |
8473 | 330d0414 | bellard | } |
8474 | 330d0414 | bellard | break;
|
8475 | cd6f1169 | bellard | case QEMU_OPTION_nographic:
|
8476 | a20dd508 | bellard | nographic = 1;
|
8477 | a20dd508 | bellard | break;
|
8478 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
8479 | 4d3b6f6e | balrog | case QEMU_OPTION_curses:
|
8480 | 4d3b6f6e | balrog | curses = 1;
|
8481 | 4d3b6f6e | balrog | break;
|
8482 | 4d3b6f6e | balrog | #endif
|
8483 | a171fe39 | balrog | case QEMU_OPTION_portrait:
|
8484 | a171fe39 | balrog | graphic_rotate = 1;
|
8485 | a171fe39 | balrog | break;
|
8486 | cd6f1169 | bellard | case QEMU_OPTION_kernel:
|
8487 | a20dd508 | bellard | kernel_filename = optarg; |
8488 | a20dd508 | bellard | break;
|
8489 | cd6f1169 | bellard | case QEMU_OPTION_append:
|
8490 | a20dd508 | bellard | kernel_cmdline = optarg; |
8491 | 313aa567 | bellard | break;
|
8492 | cd6f1169 | bellard | case QEMU_OPTION_cdrom:
|
8493 | 609497ab | balrog | drive_add(optarg, CDROM_ALIAS); |
8494 | 36b486bb | bellard | break;
|
8495 | cd6f1169 | bellard | case QEMU_OPTION_boot:
|
8496 | 28c5af54 | j_mayer | boot_devices = optarg; |
8497 | 28c5af54 | j_mayer | /* We just do some generic consistency checks */
|
8498 | 28c5af54 | j_mayer | { |
8499 | 28c5af54 | j_mayer | /* Could easily be extended to 64 devices if needed */
|
8500 | 60fe76f3 | ths | const char *p; |
8501 | 28c5af54 | j_mayer | |
8502 | 28c5af54 | j_mayer | boot_devices_bitmap = 0;
|
8503 | 28c5af54 | j_mayer | for (p = boot_devices; *p != '\0'; p++) { |
8504 | 28c5af54 | j_mayer | /* Allowed boot devices are:
|
8505 | 28c5af54 | j_mayer | * a b : floppy disk drives
|
8506 | 28c5af54 | j_mayer | * c ... f : IDE disk drives
|
8507 | 28c5af54 | j_mayer | * g ... m : machine implementation dependant drives
|
8508 | 28c5af54 | j_mayer | * n ... p : network devices
|
8509 | 28c5af54 | j_mayer | * It's up to each machine implementation to check
|
8510 | 28c5af54 | j_mayer | * if the given boot devices match the actual hardware
|
8511 | 28c5af54 | j_mayer | * implementation and firmware features.
|
8512 | 28c5af54 | j_mayer | */
|
8513 | 28c5af54 | j_mayer | if (*p < 'a' || *p > 'q') { |
8514 | 28c5af54 | j_mayer | fprintf(stderr, "Invalid boot device '%c'\n", *p);
|
8515 | 28c5af54 | j_mayer | exit(1);
|
8516 | 28c5af54 | j_mayer | } |
8517 | 28c5af54 | j_mayer | if (boot_devices_bitmap & (1 << (*p - 'a'))) { |
8518 | 28c5af54 | j_mayer | fprintf(stderr, |
8519 | 28c5af54 | j_mayer | "Boot device '%c' was given twice\n",*p);
|
8520 | 28c5af54 | j_mayer | exit(1);
|
8521 | 28c5af54 | j_mayer | } |
8522 | 28c5af54 | j_mayer | boot_devices_bitmap |= 1 << (*p - 'a'); |
8523 | 28c5af54 | j_mayer | } |
8524 | 36b486bb | bellard | } |
8525 | 36b486bb | bellard | break;
|
8526 | cd6f1169 | bellard | case QEMU_OPTION_fda:
|
8527 | cd6f1169 | bellard | case QEMU_OPTION_fdb:
|
8528 | 609497ab | balrog | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
8529 | c45886db | bellard | break;
|
8530 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
8531 | 52ca8d6a | bellard | case QEMU_OPTION_no_fd_bootchk:
|
8532 | 52ca8d6a | bellard | fd_bootchk = 0;
|
8533 | 52ca8d6a | bellard | break;
|
8534 | 52ca8d6a | bellard | #endif
|
8535 | 7c9d8e07 | bellard | case QEMU_OPTION_net:
|
8536 | 7c9d8e07 | bellard | if (nb_net_clients >= MAX_NET_CLIENTS) {
|
8537 | 7c9d8e07 | bellard | fprintf(stderr, "qemu: too many network clients\n");
|
8538 | c4b1fcc0 | bellard | exit(1);
|
8539 | c4b1fcc0 | bellard | } |
8540 | fd5f393a | pbrook | net_clients[nb_net_clients] = optarg; |
8541 | 7c9d8e07 | bellard | nb_net_clients++; |
8542 | 702c651c | bellard | break;
|
8543 | c7f74643 | bellard | #ifdef CONFIG_SLIRP
|
8544 | c7f74643 | bellard | case QEMU_OPTION_tftp:
|
8545 | c7f74643 | bellard | tftp_prefix = optarg; |
8546 | 9bf05444 | bellard | break;
|
8547 | 47d5d01a | ths | case QEMU_OPTION_bootp:
|
8548 | 47d5d01a | ths | bootp_filename = optarg; |
8549 | 47d5d01a | ths | break;
|
8550 | c94c8d64 | bellard | #ifndef _WIN32
|
8551 | 9d728e8c | bellard | case QEMU_OPTION_smb:
|
8552 | 9d728e8c | bellard | net_slirp_smb(optarg); |
8553 | 9d728e8c | bellard | break;
|
8554 | c94c8d64 | bellard | #endif
|
8555 | 9bf05444 | bellard | case QEMU_OPTION_redir:
|
8556 | 3b46e624 | ths | net_slirp_redir(optarg); |
8557 | 9bf05444 | bellard | break;
|
8558 | c7f74643 | bellard | #endif
|
8559 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
8560 | 1d14ffa9 | bellard | case QEMU_OPTION_audio_help:
|
8561 | 1d14ffa9 | bellard | AUD_help (); |
8562 | 1d14ffa9 | bellard | exit (0);
|
8563 | 1d14ffa9 | bellard | break;
|
8564 | 1d14ffa9 | bellard | case QEMU_OPTION_soundhw:
|
8565 | 1d14ffa9 | bellard | select_soundhw (optarg); |
8566 | 1d14ffa9 | bellard | break;
|
8567 | 1d14ffa9 | bellard | #endif
|
8568 | cd6f1169 | bellard | case QEMU_OPTION_h:
|
8569 | 15f82208 | ths | help(0);
|
8570 | cd6f1169 | bellard | break;
|
8571 | 00f82b8a | aurel32 | case QEMU_OPTION_m: {
|
8572 | 00f82b8a | aurel32 | uint64_t value; |
8573 | 00f82b8a | aurel32 | char *ptr;
|
8574 | 00f82b8a | aurel32 | |
8575 | 00f82b8a | aurel32 | value = strtoul(optarg, &ptr, 10);
|
8576 | 00f82b8a | aurel32 | switch (*ptr) {
|
8577 | 00f82b8a | aurel32 | case 0: case 'M': case 'm': |
8578 | 00f82b8a | aurel32 | value <<= 20;
|
8579 | 00f82b8a | aurel32 | break;
|
8580 | 00f82b8a | aurel32 | case 'G': case 'g': |
8581 | 00f82b8a | aurel32 | value <<= 30;
|
8582 | 00f82b8a | aurel32 | break;
|
8583 | 00f82b8a | aurel32 | default:
|
8584 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
|
8585 | cd6f1169 | bellard | exit(1);
|
8586 | cd6f1169 | bellard | } |
8587 | 00f82b8a | aurel32 | |
8588 | 00f82b8a | aurel32 | /* On 32-bit hosts, QEMU is limited by virtual address space */
|
8589 | 00f82b8a | aurel32 | if (value > (2047 << 20) |
8590 | 00f82b8a | aurel32 | #ifndef USE_KQEMU
|
8591 | 00f82b8a | aurel32 | && HOST_LONG_BITS == 32
|
8592 | 00f82b8a | aurel32 | #endif
|
8593 | 00f82b8a | aurel32 | ) { |
8594 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
|
8595 | 00f82b8a | aurel32 | exit(1);
|
8596 | 00f82b8a | aurel32 | } |
8597 | 00f82b8a | aurel32 | if (value != (uint64_t)(ram_addr_t)value) {
|
8598 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: ram size too large\n");
|
8599 | 00f82b8a | aurel32 | exit(1);
|
8600 | 00f82b8a | aurel32 | } |
8601 | 00f82b8a | aurel32 | ram_size = value; |
8602 | cd6f1169 | bellard | break;
|
8603 | 00f82b8a | aurel32 | } |
8604 | cd6f1169 | bellard | case QEMU_OPTION_d:
|
8605 | cd6f1169 | bellard | { |
8606 | cd6f1169 | bellard | int mask;
|
8607 | cd6f1169 | bellard | CPULogItem *item; |
8608 | 3b46e624 | ths | |
8609 | cd6f1169 | bellard | mask = cpu_str_to_log_mask(optarg); |
8610 | cd6f1169 | bellard | if (!mask) {
|
8611 | cd6f1169 | bellard | printf("Log items (comma separated):\n");
|
8612 | f193c797 | bellard | for(item = cpu_log_items; item->mask != 0; item++) { |
8613 | f193c797 | bellard | printf("%-10s %s\n", item->name, item->help);
|
8614 | f193c797 | bellard | } |
8615 | f193c797 | bellard | exit(1);
|
8616 | cd6f1169 | bellard | } |
8617 | cd6f1169 | bellard | cpu_set_log(mask); |
8618 | f193c797 | bellard | } |
8619 | cd6f1169 | bellard | break;
|
8620 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
8621 | cd6f1169 | bellard | case QEMU_OPTION_s:
|
8622 | cd6f1169 | bellard | use_gdbstub = 1;
|
8623 | cd6f1169 | bellard | break;
|
8624 | cd6f1169 | bellard | case QEMU_OPTION_p:
|
8625 | cfc3475a | pbrook | gdbstub_port = optarg; |
8626 | cd6f1169 | bellard | break;
|
8627 | 67b915a5 | bellard | #endif
|
8628 | cd6f1169 | bellard | case QEMU_OPTION_L:
|
8629 | cd6f1169 | bellard | bios_dir = optarg; |
8630 | cd6f1169 | bellard | break;
|
8631 | 1192dad8 | j_mayer | case QEMU_OPTION_bios:
|
8632 | 1192dad8 | j_mayer | bios_name = optarg; |
8633 | 1192dad8 | j_mayer | break;
|
8634 | cd6f1169 | bellard | case QEMU_OPTION_S:
|
8635 | 3c07f8e8 | pbrook | autostart = 0;
|
8636 | cd6f1169 | bellard | break;
|
8637 | 3d11d0eb | bellard | case QEMU_OPTION_k:
|
8638 | 3d11d0eb | bellard | keyboard_layout = optarg; |
8639 | 3d11d0eb | bellard | break;
|
8640 | ee22c2f7 | bellard | case QEMU_OPTION_localtime:
|
8641 | ee22c2f7 | bellard | rtc_utc = 0;
|
8642 | ee22c2f7 | bellard | break;
|
8643 | 1f04275e | bellard | case QEMU_OPTION_cirrusvga:
|
8644 | 1f04275e | bellard | cirrus_vga_enabled = 1;
|
8645 | d34cab9f | ths | vmsvga_enabled = 0;
|
8646 | d34cab9f | ths | break;
|
8647 | d34cab9f | ths | case QEMU_OPTION_vmsvga:
|
8648 | d34cab9f | ths | cirrus_vga_enabled = 0;
|
8649 | d34cab9f | ths | vmsvga_enabled = 1;
|
8650 | 1f04275e | bellard | break;
|
8651 | 1bfe856e | bellard | case QEMU_OPTION_std_vga:
|
8652 | 1bfe856e | bellard | cirrus_vga_enabled = 0;
|
8653 | d34cab9f | ths | vmsvga_enabled = 0;
|
8654 | 1bfe856e | bellard | break;
|
8655 | e9b137c2 | bellard | case QEMU_OPTION_g:
|
8656 | e9b137c2 | bellard | { |
8657 | e9b137c2 | bellard | const char *p; |
8658 | e9b137c2 | bellard | int w, h, depth;
|
8659 | e9b137c2 | bellard | p = optarg; |
8660 | e9b137c2 | bellard | w = strtol(p, (char **)&p, 10); |
8661 | e9b137c2 | bellard | if (w <= 0) { |
8662 | e9b137c2 | bellard | graphic_error:
|
8663 | e9b137c2 | bellard | fprintf(stderr, "qemu: invalid resolution or depth\n");
|
8664 | e9b137c2 | bellard | exit(1);
|
8665 | e9b137c2 | bellard | } |
8666 | e9b137c2 | bellard | if (*p != 'x') |
8667 | e9b137c2 | bellard | goto graphic_error;
|
8668 | e9b137c2 | bellard | p++; |
8669 | e9b137c2 | bellard | h = strtol(p, (char **)&p, 10); |
8670 | e9b137c2 | bellard | if (h <= 0) |
8671 | e9b137c2 | bellard | goto graphic_error;
|
8672 | e9b137c2 | bellard | if (*p == 'x') { |
8673 | e9b137c2 | bellard | p++; |
8674 | e9b137c2 | bellard | depth = strtol(p, (char **)&p, 10); |
8675 | 5fafdf24 | ths | if (depth != 8 && depth != 15 && depth != 16 && |
8676 | e9b137c2 | bellard | depth != 24 && depth != 32) |
8677 | e9b137c2 | bellard | goto graphic_error;
|
8678 | e9b137c2 | bellard | } else if (*p == '\0') { |
8679 | e9b137c2 | bellard | depth = graphic_depth; |
8680 | e9b137c2 | bellard | } else {
|
8681 | e9b137c2 | bellard | goto graphic_error;
|
8682 | e9b137c2 | bellard | } |
8683 | 3b46e624 | ths | |
8684 | e9b137c2 | bellard | graphic_width = w; |
8685 | e9b137c2 | bellard | graphic_height = h; |
8686 | e9b137c2 | bellard | graphic_depth = depth; |
8687 | e9b137c2 | bellard | } |
8688 | e9b137c2 | bellard | break;
|
8689 | 20d8a3ed | ths | case QEMU_OPTION_echr:
|
8690 | 20d8a3ed | ths | { |
8691 | 20d8a3ed | ths | char *r;
|
8692 | 20d8a3ed | ths | term_escape_char = strtol(optarg, &r, 0);
|
8693 | 20d8a3ed | ths | if (r == optarg)
|
8694 | 20d8a3ed | ths | printf("Bad argument to echr\n");
|
8695 | 20d8a3ed | ths | break;
|
8696 | 20d8a3ed | ths | } |
8697 | 82c643ff | bellard | case QEMU_OPTION_monitor:
|
8698 | fd5f393a | pbrook | monitor_device = optarg; |
8699 | 82c643ff | bellard | break;
|
8700 | 82c643ff | bellard | case QEMU_OPTION_serial:
|
8701 | 8d11df9e | bellard | if (serial_device_index >= MAX_SERIAL_PORTS) {
|
8702 | 8d11df9e | bellard | fprintf(stderr, "qemu: too many serial ports\n");
|
8703 | 8d11df9e | bellard | exit(1);
|
8704 | 8d11df9e | bellard | } |
8705 | fd5f393a | pbrook | serial_devices[serial_device_index] = optarg; |
8706 | 8d11df9e | bellard | serial_device_index++; |
8707 | 82c643ff | bellard | break;
|
8708 | 6508fe59 | bellard | case QEMU_OPTION_parallel:
|
8709 | 6508fe59 | bellard | if (parallel_device_index >= MAX_PARALLEL_PORTS) {
|
8710 | 6508fe59 | bellard | fprintf(stderr, "qemu: too many parallel ports\n");
|
8711 | 6508fe59 | bellard | exit(1);
|
8712 | 6508fe59 | bellard | } |
8713 | fd5f393a | pbrook | parallel_devices[parallel_device_index] = optarg; |
8714 | 6508fe59 | bellard | parallel_device_index++; |
8715 | 6508fe59 | bellard | break;
|
8716 | d63d307f | bellard | case QEMU_OPTION_loadvm:
|
8717 | d63d307f | bellard | loadvm = optarg; |
8718 | d63d307f | bellard | break;
|
8719 | d63d307f | bellard | case QEMU_OPTION_full_screen:
|
8720 | d63d307f | bellard | full_screen = 1;
|
8721 | d63d307f | bellard | break;
|
8722 | 667accab | ths | #ifdef CONFIG_SDL
|
8723 | 43523e93 | ths | case QEMU_OPTION_no_frame:
|
8724 | 43523e93 | ths | no_frame = 1;
|
8725 | 43523e93 | ths | break;
|
8726 | 3780e197 | ths | case QEMU_OPTION_alt_grab:
|
8727 | 3780e197 | ths | alt_grab = 1;
|
8728 | 3780e197 | ths | break;
|
8729 | 667accab | ths | case QEMU_OPTION_no_quit:
|
8730 | 667accab | ths | no_quit = 1;
|
8731 | 667accab | ths | break;
|
8732 | 667accab | ths | #endif
|
8733 | f7cce898 | bellard | case QEMU_OPTION_pidfile:
|
8734 | 93815bc2 | ths | pid_file = optarg; |
8735 | f7cce898 | bellard | break;
|
8736 | a09db21f | bellard | #ifdef TARGET_I386
|
8737 | a09db21f | bellard | case QEMU_OPTION_win2k_hack:
|
8738 | a09db21f | bellard | win2k_install_hack = 1;
|
8739 | a09db21f | bellard | break;
|
8740 | a09db21f | bellard | #endif
|
8741 | d993e026 | bellard | #ifdef USE_KQEMU
|
8742 | d993e026 | bellard | case QEMU_OPTION_no_kqemu:
|
8743 | d993e026 | bellard | kqemu_allowed = 0;
|
8744 | d993e026 | bellard | break;
|
8745 | 89bfc105 | bellard | case QEMU_OPTION_kernel_kqemu:
|
8746 | 89bfc105 | bellard | kqemu_allowed = 2;
|
8747 | 89bfc105 | bellard | break;
|
8748 | d993e026 | bellard | #endif
|
8749 | bb36d470 | bellard | case QEMU_OPTION_usb:
|
8750 | bb36d470 | bellard | usb_enabled = 1;
|
8751 | bb36d470 | bellard | break;
|
8752 | a594cfbf | bellard | case QEMU_OPTION_usbdevice:
|
8753 | a594cfbf | bellard | usb_enabled = 1;
|
8754 | 0d92ed30 | pbrook | if (usb_devices_index >= MAX_USB_CMDLINE) {
|
8755 | a594cfbf | bellard | fprintf(stderr, "Too many USB devices\n");
|
8756 | a594cfbf | bellard | exit(1);
|
8757 | a594cfbf | bellard | } |
8758 | fd5f393a | pbrook | usb_devices[usb_devices_index] = optarg; |
8759 | a594cfbf | bellard | usb_devices_index++; |
8760 | a594cfbf | bellard | break;
|
8761 | 6a00d601 | bellard | case QEMU_OPTION_smp:
|
8762 | 6a00d601 | bellard | smp_cpus = atoi(optarg); |
8763 | ba3c64fb | bellard | if (smp_cpus < 1 || smp_cpus > MAX_CPUS) { |
8764 | 6a00d601 | bellard | fprintf(stderr, "Invalid number of CPUs\n");
|
8765 | 6a00d601 | bellard | exit(1);
|
8766 | 6a00d601 | bellard | } |
8767 | 6a00d601 | bellard | break;
|
8768 | 24236869 | bellard | case QEMU_OPTION_vnc:
|
8769 | 73fc9742 | ths | vnc_display = optarg; |
8770 | 24236869 | bellard | break;
|
8771 | 6515b203 | bellard | case QEMU_OPTION_no_acpi:
|
8772 | 6515b203 | bellard | acpi_enabled = 0;
|
8773 | 6515b203 | bellard | break;
|
8774 | d1beab82 | bellard | case QEMU_OPTION_no_reboot:
|
8775 | d1beab82 | bellard | no_reboot = 1;
|
8776 | d1beab82 | bellard | break;
|
8777 | b2f76161 | aurel32 | case QEMU_OPTION_no_shutdown:
|
8778 | b2f76161 | aurel32 | no_shutdown = 1;
|
8779 | b2f76161 | aurel32 | break;
|
8780 | 9467cd46 | balrog | case QEMU_OPTION_show_cursor:
|
8781 | 9467cd46 | balrog | cursor_hide = 0;
|
8782 | 9467cd46 | balrog | break;
|
8783 | 71e3ceb8 | ths | case QEMU_OPTION_daemonize:
|
8784 | 71e3ceb8 | ths | daemonize = 1;
|
8785 | 71e3ceb8 | ths | break;
|
8786 | 9ae02555 | ths | case QEMU_OPTION_option_rom:
|
8787 | 9ae02555 | ths | if (nb_option_roms >= MAX_OPTION_ROMS) {
|
8788 | 9ae02555 | ths | fprintf(stderr, "Too many option ROMs\n");
|
8789 | 9ae02555 | ths | exit(1);
|
8790 | 9ae02555 | ths | } |
8791 | 9ae02555 | ths | option_rom[nb_option_roms] = optarg; |
8792 | 9ae02555 | ths | nb_option_roms++; |
8793 | 9ae02555 | ths | break;
|
8794 | 8e71621f | pbrook | case QEMU_OPTION_semihosting:
|
8795 | 8e71621f | pbrook | semihosting_enabled = 1;
|
8796 | 8e71621f | pbrook | break;
|
8797 | c35734b2 | ths | case QEMU_OPTION_name:
|
8798 | c35734b2 | ths | qemu_name = optarg; |
8799 | c35734b2 | ths | break;
|
8800 | 66508601 | blueswir1 | #ifdef TARGET_SPARC
|
8801 | 66508601 | blueswir1 | case QEMU_OPTION_prom_env:
|
8802 | 66508601 | blueswir1 | if (nb_prom_envs >= MAX_PROM_ENVS) {
|
8803 | 66508601 | blueswir1 | fprintf(stderr, "Too many prom variables\n");
|
8804 | 66508601 | blueswir1 | exit(1);
|
8805 | 66508601 | blueswir1 | } |
8806 | 66508601 | blueswir1 | prom_envs[nb_prom_envs] = optarg; |
8807 | 66508601 | blueswir1 | nb_prom_envs++; |
8808 | 66508601 | blueswir1 | break;
|
8809 | 66508601 | blueswir1 | #endif
|
8810 | 2b8f2d41 | balrog | #ifdef TARGET_ARM
|
8811 | 2b8f2d41 | balrog | case QEMU_OPTION_old_param:
|
8812 | 2b8f2d41 | balrog | old_param = 1;
|
8813 | 05ebd537 | ths | break;
|
8814 | 2b8f2d41 | balrog | #endif
|
8815 | f3dcfada | ths | case QEMU_OPTION_clock:
|
8816 | f3dcfada | ths | configure_alarms(optarg); |
8817 | f3dcfada | ths | break;
|
8818 | 7e0af5d0 | bellard | case QEMU_OPTION_startdate:
|
8819 | 7e0af5d0 | bellard | { |
8820 | 7e0af5d0 | bellard | struct tm tm;
|
8821 | f6503059 | balrog | time_t rtc_start_date; |
8822 | 7e0af5d0 | bellard | if (!strcmp(optarg, "now")) { |
8823 | f6503059 | balrog | rtc_date_offset = -1;
|
8824 | 7e0af5d0 | bellard | } else {
|
8825 | 7e0af5d0 | bellard | if (sscanf(optarg, "%d-%d-%dT%d:%d:%d", |
8826 | 7e0af5d0 | bellard | &tm.tm_year, |
8827 | 7e0af5d0 | bellard | &tm.tm_mon, |
8828 | 7e0af5d0 | bellard | &tm.tm_mday, |
8829 | 7e0af5d0 | bellard | &tm.tm_hour, |
8830 | 7e0af5d0 | bellard | &tm.tm_min, |
8831 | 7e0af5d0 | bellard | &tm.tm_sec) == 6) {
|
8832 | 7e0af5d0 | bellard | /* OK */
|
8833 | 7e0af5d0 | bellard | } else if (sscanf(optarg, "%d-%d-%d", |
8834 | 7e0af5d0 | bellard | &tm.tm_year, |
8835 | 7e0af5d0 | bellard | &tm.tm_mon, |
8836 | 7e0af5d0 | bellard | &tm.tm_mday) == 3) {
|
8837 | 7e0af5d0 | bellard | tm.tm_hour = 0;
|
8838 | 7e0af5d0 | bellard | tm.tm_min = 0;
|
8839 | 7e0af5d0 | bellard | tm.tm_sec = 0;
|
8840 | 7e0af5d0 | bellard | } else {
|
8841 | 7e0af5d0 | bellard | goto date_fail;
|
8842 | 7e0af5d0 | bellard | } |
8843 | 7e0af5d0 | bellard | tm.tm_year -= 1900;
|
8844 | 7e0af5d0 | bellard | tm.tm_mon--; |
8845 | 3c6b2088 | bellard | rtc_start_date = mktimegm(&tm); |
8846 | 7e0af5d0 | bellard | if (rtc_start_date == -1) { |
8847 | 7e0af5d0 | bellard | date_fail:
|
8848 | 7e0af5d0 | bellard | fprintf(stderr, "Invalid date format. Valid format are:\n"
|
8849 | 7e0af5d0 | bellard | "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
|
8850 | 7e0af5d0 | bellard | exit(1);
|
8851 | 7e0af5d0 | bellard | } |
8852 | f6503059 | balrog | rtc_date_offset = time(NULL) - rtc_start_date;
|
8853 | 7e0af5d0 | bellard | } |
8854 | 7e0af5d0 | bellard | } |
8855 | 7e0af5d0 | bellard | break;
|
8856 | 26a5f13b | bellard | case QEMU_OPTION_tb_size:
|
8857 | 26a5f13b | bellard | tb_size = strtol(optarg, NULL, 0); |
8858 | 26a5f13b | bellard | if (tb_size < 0) |
8859 | 26a5f13b | bellard | tb_size = 0;
|
8860 | 26a5f13b | bellard | break;
|
8861 | 2e70f6ef | pbrook | case QEMU_OPTION_icount:
|
8862 | 2e70f6ef | pbrook | use_icount = 1;
|
8863 | 2e70f6ef | pbrook | if (strcmp(optarg, "auto") == 0) { |
8864 | 2e70f6ef | pbrook | icount_time_shift = -1;
|
8865 | 2e70f6ef | pbrook | } else {
|
8866 | 2e70f6ef | pbrook | icount_time_shift = strtol(optarg, NULL, 0); |
8867 | 2e70f6ef | pbrook | } |
8868 | 2e70f6ef | pbrook | break;
|
8869 | cd6f1169 | bellard | } |
8870 | 0824d6fc | bellard | } |
8871 | 0824d6fc | bellard | } |
8872 | 330d0414 | bellard | |
8873 | bc0129d9 | aliguori | if (nographic) {
|
8874 | bc0129d9 | aliguori | if (serial_device_index == 0) |
8875 | bc0129d9 | aliguori | serial_devices[0] = "stdio"; |
8876 | bc0129d9 | aliguori | if (parallel_device_index == 0) |
8877 | bc0129d9 | aliguori | parallel_devices[0] = "null"; |
8878 | bc0129d9 | aliguori | if (strncmp(monitor_device, "vc", 2) == 0) |
8879 | bc0129d9 | aliguori | monitor_device = "stdio";
|
8880 | bc0129d9 | aliguori | } |
8881 | bc0129d9 | aliguori | |
8882 | 71e3ceb8 | ths | #ifndef _WIN32
|
8883 | 71e3ceb8 | ths | if (daemonize) {
|
8884 | 71e3ceb8 | ths | pid_t pid; |
8885 | 71e3ceb8 | ths | |
8886 | 71e3ceb8 | ths | if (pipe(fds) == -1) |
8887 | 71e3ceb8 | ths | exit(1);
|
8888 | 71e3ceb8 | ths | |
8889 | 71e3ceb8 | ths | pid = fork(); |
8890 | 71e3ceb8 | ths | if (pid > 0) { |
8891 | 71e3ceb8 | ths | uint8_t status; |
8892 | 71e3ceb8 | ths | ssize_t len; |
8893 | 71e3ceb8 | ths | |
8894 | 71e3ceb8 | ths | close(fds[1]);
|
8895 | 71e3ceb8 | ths | |
8896 | 71e3ceb8 | ths | again:
|
8897 | 93815bc2 | ths | len = read(fds[0], &status, 1); |
8898 | 93815bc2 | ths | if (len == -1 && (errno == EINTR)) |
8899 | 93815bc2 | ths | goto again;
|
8900 | 93815bc2 | ths | |
8901 | 93815bc2 | ths | if (len != 1) |
8902 | 93815bc2 | ths | exit(1);
|
8903 | 93815bc2 | ths | else if (status == 1) { |
8904 | 93815bc2 | ths | fprintf(stderr, "Could not acquire pidfile\n");
|
8905 | 93815bc2 | ths | exit(1);
|
8906 | 93815bc2 | ths | } else
|
8907 | 93815bc2 | ths | exit(0);
|
8908 | 71e3ceb8 | ths | } else if (pid < 0) |
8909 | 93815bc2 | ths | exit(1);
|
8910 | 71e3ceb8 | ths | |
8911 | 71e3ceb8 | ths | setsid(); |
8912 | 71e3ceb8 | ths | |
8913 | 71e3ceb8 | ths | pid = fork(); |
8914 | 71e3ceb8 | ths | if (pid > 0) |
8915 | 71e3ceb8 | ths | exit(0);
|
8916 | 71e3ceb8 | ths | else if (pid < 0) |
8917 | 71e3ceb8 | ths | exit(1);
|
8918 | 71e3ceb8 | ths | |
8919 | 71e3ceb8 | ths | umask(027);
|
8920 | 71e3ceb8 | ths | |
8921 | 71e3ceb8 | ths | signal(SIGTSTP, SIG_IGN); |
8922 | 71e3ceb8 | ths | signal(SIGTTOU, SIG_IGN); |
8923 | 71e3ceb8 | ths | signal(SIGTTIN, SIG_IGN); |
8924 | 71e3ceb8 | ths | } |
8925 | 71e3ceb8 | ths | #endif
|
8926 | 71e3ceb8 | ths | |
8927 | aa26bb2d | ths | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
8928 | 93815bc2 | ths | if (daemonize) {
|
8929 | 93815bc2 | ths | uint8_t status = 1;
|
8930 | 93815bc2 | ths | write(fds[1], &status, 1); |
8931 | 93815bc2 | ths | } else
|
8932 | 93815bc2 | ths | fprintf(stderr, "Could not acquire pid file\n");
|
8933 | 93815bc2 | ths | exit(1);
|
8934 | 93815bc2 | ths | } |
8935 | 93815bc2 | ths | |
8936 | ff3fbb30 | bellard | #ifdef USE_KQEMU
|
8937 | ff3fbb30 | bellard | if (smp_cpus > 1) |
8938 | ff3fbb30 | bellard | kqemu_allowed = 0;
|
8939 | ff3fbb30 | bellard | #endif
|
8940 | a20dd508 | bellard | linux_boot = (kernel_filename != NULL);
|
8941 | 7317b8ca | balrog | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
8942 | 6c41b272 | balrog | |
8943 | 28c5af54 | j_mayer | if (!linux_boot && net_boot == 0 && |
8944 | f88e4b91 | blueswir1 | !machine->nodisk_ok && nb_drives_opt == 0)
|
8945 | 15f82208 | ths | help(1);
|
8946 | 0824d6fc | bellard | |
8947 | f8d39c01 | ths | if (!linux_boot && *kernel_cmdline != '\0') { |
8948 | f8d39c01 | ths | fprintf(stderr, "-append only allowed with -kernel option\n");
|
8949 | f8d39c01 | ths | exit(1);
|
8950 | f8d39c01 | ths | } |
8951 | f8d39c01 | ths | |
8952 | f8d39c01 | ths | if (!linux_boot && initrd_filename != NULL) { |
8953 | f8d39c01 | ths | fprintf(stderr, "-initrd only allowed with -kernel option\n");
|
8954 | f8d39c01 | ths | exit(1);
|
8955 | f8d39c01 | ths | } |
8956 | f8d39c01 | ths | |
8957 | 96d30e48 | ths | /* boot to floppy or the default cd if no hard disk defined yet */
|
8958 | 28c5af54 | j_mayer | if (!boot_devices[0]) { |
8959 | e4bcb14c | ths | boot_devices = "cad";
|
8960 | 96d30e48 | ths | } |
8961 | b118d61e | bellard | setvbuf(stdout, NULL, _IOLBF, 0); |
8962 | 3b46e624 | ths | |
8963 | 634fce96 | pbrook | init_timers(); |
8964 | 634fce96 | pbrook | init_timer_alarm(); |
8965 | 83f64091 | bellard | qemu_aio_init(); |
8966 | 2e70f6ef | pbrook | if (use_icount && icount_time_shift < 0) { |
8967 | 2e70f6ef | pbrook | use_icount = 2;
|
8968 | 2e70f6ef | pbrook | /* 125MIPS seems a reasonable initial guess at the guest speed.
|
8969 | 2e70f6ef | pbrook | It will be corrected fairly quickly anyway. */
|
8970 | 2e70f6ef | pbrook | icount_time_shift = 3;
|
8971 | 2e70f6ef | pbrook | init_icount_adjust(); |
8972 | 2e70f6ef | pbrook | } |
8973 | 634fce96 | pbrook | |
8974 | fd1dff4b | bellard | #ifdef _WIN32
|
8975 | fd1dff4b | bellard | socket_init(); |
8976 | fd1dff4b | bellard | #endif
|
8977 | fd1dff4b | bellard | |
8978 | 7c9d8e07 | bellard | /* init network clients */
|
8979 | 7c9d8e07 | bellard | if (nb_net_clients == 0) { |
8980 | 7c9d8e07 | bellard | /* if no clients, we use a default config */
|
8981 | f441b28b | aliguori | net_clients[nb_net_clients++] = "nic";
|
8982 | f441b28b | aliguori | #ifdef CONFIG_SLIRP
|
8983 | f441b28b | aliguori | net_clients[nb_net_clients++] = "user";
|
8984 | f441b28b | aliguori | #endif
|
8985 | c20709aa | bellard | } |
8986 | c20709aa | bellard | |
8987 | 7c9d8e07 | bellard | for(i = 0;i < nb_net_clients; i++) { |
8988 | 9ad97e65 | balrog | if (net_client_parse(net_clients[i]) < 0) |
8989 | 7c9d8e07 | bellard | exit(1);
|
8990 | 702c651c | bellard | } |
8991 | 833c7174 | blueswir1 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
8992 | 833c7174 | blueswir1 | if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) |
8993 | 833c7174 | blueswir1 | continue;
|
8994 | 9ad97e65 | balrog | if (vlan->nb_guest_devs == 0) |
8995 | 9ad97e65 | balrog | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
|
8996 | 833c7174 | blueswir1 | if (vlan->nb_host_devs == 0) |
8997 | 833c7174 | blueswir1 | fprintf(stderr, |
8998 | 833c7174 | blueswir1 | "Warning: vlan %d is not connected to host network\n",
|
8999 | 833c7174 | blueswir1 | vlan->id); |
9000 | 833c7174 | blueswir1 | } |
9001 | f1510b2c | bellard | |
9002 | eec85c2a | ths | #ifdef TARGET_I386
|
9003 | ed494d87 | balrog | /* XXX: this should be moved in the PC machine instantiation code */
|
9004 | 28c5af54 | j_mayer | if (net_boot != 0) { |
9005 | 28c5af54 | j_mayer | int netroms = 0; |
9006 | 28c5af54 | j_mayer | for (i = 0; i < nb_nics && i < 4; i++) { |
9007 | eec85c2a | ths | const char *model = nd_table[i].model; |
9008 | eec85c2a | ths | char buf[1024]; |
9009 | 28c5af54 | j_mayer | if (net_boot & (1 << i)) { |
9010 | 28c5af54 | j_mayer | if (model == NULL) |
9011 | 28c5af54 | j_mayer | model = "ne2k_pci";
|
9012 | 28c5af54 | j_mayer | snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model); |
9013 | 28c5af54 | j_mayer | if (get_image_size(buf) > 0) { |
9014 | 28c5af54 | j_mayer | if (nb_option_roms >= MAX_OPTION_ROMS) {
|
9015 | 28c5af54 | j_mayer | fprintf(stderr, "Too many option ROMs\n");
|
9016 | 28c5af54 | j_mayer | exit(1);
|
9017 | 28c5af54 | j_mayer | } |
9018 | 28c5af54 | j_mayer | option_rom[nb_option_roms] = strdup(buf); |
9019 | 28c5af54 | j_mayer | nb_option_roms++; |
9020 | 28c5af54 | j_mayer | netroms++; |
9021 | 28c5af54 | j_mayer | } |
9022 | 28c5af54 | j_mayer | } |
9023 | eec85c2a | ths | } |
9024 | 28c5af54 | j_mayer | if (netroms == 0) { |
9025 | eec85c2a | ths | fprintf(stderr, "No valid PXE rom found for network device\n");
|
9026 | eec85c2a | ths | exit(1);
|
9027 | eec85c2a | ths | } |
9028 | eec85c2a | ths | } |
9029 | eec85c2a | ths | #endif
|
9030 | eec85c2a | ths | |
9031 | 0824d6fc | bellard | /* init the memory */
|
9032 | 7fb4fdcf | balrog | phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED; |
9033 | 7fb4fdcf | balrog | |
9034 | 7fb4fdcf | balrog | if (machine->ram_require & RAMSIZE_FIXED) {
|
9035 | 7fb4fdcf | balrog | if (ram_size > 0) { |
9036 | 7fb4fdcf | balrog | if (ram_size < phys_ram_size) {
|
9037 | cd940061 | aurel32 | fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
|
9038 | cd940061 | aurel32 | machine->name, (unsigned long long) phys_ram_size); |
9039 | 7fb4fdcf | balrog | exit(-1);
|
9040 | 7fb4fdcf | balrog | } |
9041 | 7fb4fdcf | balrog | |
9042 | 7fb4fdcf | balrog | phys_ram_size = ram_size; |
9043 | 7fb4fdcf | balrog | } else
|
9044 | 7fb4fdcf | balrog | ram_size = phys_ram_size; |
9045 | 7fb4fdcf | balrog | } else {
|
9046 | 4fc5d071 | aurel32 | if (ram_size == 0) |
9047 | 7fb4fdcf | balrog | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
9048 | 7fb4fdcf | balrog | |
9049 | 7fb4fdcf | balrog | phys_ram_size += ram_size; |
9050 | 7fb4fdcf | balrog | } |
9051 | 9ae02555 | ths | |
9052 | d993e026 | bellard | phys_ram_base = qemu_vmalloc(phys_ram_size); |
9053 | 7f7f9873 | bellard | if (!phys_ram_base) {
|
9054 | 7f7f9873 | bellard | fprintf(stderr, "Could not allocate physical memory\n");
|
9055 | 0824d6fc | bellard | exit(1);
|
9056 | 0824d6fc | bellard | } |
9057 | 0824d6fc | bellard | |
9058 | 26a5f13b | bellard | /* init the dynamic translator */
|
9059 | 26a5f13b | bellard | cpu_exec_init_all(tb_size * 1024 * 1024); |
9060 | 26a5f13b | bellard | |
9061 | 5905b2e5 | bellard | bdrv_init(); |
9062 | c4b1fcc0 | bellard | |
9063 | e4bcb14c | ths | /* we always create the cdrom drive, even if no disk is there */
|
9064 | c4b1fcc0 | bellard | |
9065 | e4bcb14c | ths | if (nb_drives_opt < MAX_DRIVES)
|
9066 | 609497ab | balrog | drive_add(NULL, CDROM_ALIAS);
|
9067 | c4b1fcc0 | bellard | |
9068 | 9d413d1d | balrog | /* we always create at least one floppy */
|
9069 | 33e3963e | bellard | |
9070 | e4bcb14c | ths | if (nb_drives_opt < MAX_DRIVES)
|
9071 | 609497ab | balrog | drive_add(NULL, FD_ALIAS, 0); |
9072 | 86f55663 | j_mayer | |
9073 | 9d413d1d | balrog | /* we always create one sd slot, even if no card is in it */
|
9074 | 9d413d1d | balrog | |
9075 | 9d413d1d | balrog | if (nb_drives_opt < MAX_DRIVES)
|
9076 | 609497ab | balrog | drive_add(NULL, SD_ALIAS);
|
9077 | 9d413d1d | balrog | |
9078 | e4bcb14c | ths | /* open the virtual block devices */
|
9079 | e4bcb14c | ths | |
9080 | e4bcb14c | ths | for(i = 0; i < nb_drives_opt; i++) |
9081 | 609497ab | balrog | if (drive_init(&drives_opt[i], snapshot, machine) == -1) |
9082 | e4bcb14c | ths | exit(1);
|
9083 | 3e3d5815 | balrog | |
9084 | c88676f8 | bellard | register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
9085 | c88676f8 | bellard | register_savevm("ram", 0, 2, ram_save, ram_load, NULL); |
9086 | 8a7ddc38 | bellard | |
9087 | 313aa567 | bellard | /* terminal init */
|
9088 | 740733bb | ths | memset(&display_state, 0, sizeof(display_state)); |
9089 | a20dd508 | bellard | if (nographic) {
|
9090 | 4d3b6f6e | balrog | if (curses) {
|
9091 | 4d3b6f6e | balrog | fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
|
9092 | 4d3b6f6e | balrog | exit(1);
|
9093 | 4d3b6f6e | balrog | } |
9094 | 2ff89790 | ths | /* nearly nothing to do */
|
9095 | 2ff89790 | ths | dumb_display_init(ds); |
9096 | 73fc9742 | ths | } else if (vnc_display != NULL) { |
9097 | 71cab5ca | ths | vnc_display_init(ds); |
9098 | 71cab5ca | ths | if (vnc_display_open(ds, vnc_display) < 0) |
9099 | 71cab5ca | ths | exit(1);
|
9100 | 4d3b6f6e | balrog | } else
|
9101 | 4d3b6f6e | balrog | #if defined(CONFIG_CURSES)
|
9102 | 4d3b6f6e | balrog | if (curses) {
|
9103 | 4d3b6f6e | balrog | curses_display_init(ds, full_screen); |
9104 | 4d3b6f6e | balrog | } else
|
9105 | 4d3b6f6e | balrog | #endif
|
9106 | 4d3b6f6e | balrog | { |
9107 | 5b0753e0 | bellard | #if defined(CONFIG_SDL)
|
9108 | 43523e93 | ths | sdl_display_init(ds, full_screen, no_frame); |
9109 | 5b0753e0 | bellard | #elif defined(CONFIG_COCOA)
|
9110 | 5b0753e0 | bellard | cocoa_display_init(ds, full_screen); |
9111 | 67276f53 | pbrook | #else
|
9112 | 67276f53 | pbrook | dumb_display_init(ds); |
9113 | 313aa567 | bellard | #endif
|
9114 | 313aa567 | bellard | } |
9115 | 0824d6fc | bellard | |
9116 | 5b08fc10 | aliguori | #ifndef _WIN32
|
9117 | 5b08fc10 | aliguori | /* must be after terminal init, SDL library changes signal handlers */
|
9118 | 5b08fc10 | aliguori | termsig_setup(); |
9119 | 5b08fc10 | aliguori | #endif
|
9120 | 5b08fc10 | aliguori | |
9121 | 20d8a3ed | ths | /* Maintain compatibility with multiple stdio monitors */
|
9122 | 20d8a3ed | ths | if (!strcmp(monitor_device,"stdio")) { |
9123 | 20d8a3ed | ths | for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
9124 | fd5f393a | pbrook | const char *devname = serial_devices[i]; |
9125 | fd5f393a | pbrook | if (devname && !strcmp(devname,"mon:stdio")) { |
9126 | fd5f393a | pbrook | monitor_device = NULL;
|
9127 | 20d8a3ed | ths | break;
|
9128 | fd5f393a | pbrook | } else if (devname && !strcmp(devname,"stdio")) { |
9129 | fd5f393a | pbrook | monitor_device = NULL;
|
9130 | fd5f393a | pbrook | serial_devices[i] = "mon:stdio";
|
9131 | 20d8a3ed | ths | break;
|
9132 | 20d8a3ed | ths | } |
9133 | 20d8a3ed | ths | } |
9134 | 20d8a3ed | ths | } |
9135 | fd5f393a | pbrook | if (monitor_device) {
|
9136 | 20d8a3ed | ths | monitor_hd = qemu_chr_open(monitor_device); |
9137 | 20d8a3ed | ths | if (!monitor_hd) {
|
9138 | 20d8a3ed | ths | fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
|
9139 | 20d8a3ed | ths | exit(1);
|
9140 | 20d8a3ed | ths | } |
9141 | 20d8a3ed | ths | monitor_init(monitor_hd, !nographic); |
9142 | 82c643ff | bellard | } |
9143 | 82c643ff | bellard | |
9144 | 8d11df9e | bellard | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
9145 | c03b0f0f | bellard | const char *devname = serial_devices[i]; |
9146 | fd5f393a | pbrook | if (devname && strcmp(devname, "none")) { |
9147 | c03b0f0f | bellard | serial_hds[i] = qemu_chr_open(devname); |
9148 | 8d11df9e | bellard | if (!serial_hds[i]) {
|
9149 | 5fafdf24 | ths | fprintf(stderr, "qemu: could not open serial device '%s'\n",
|
9150 | c03b0f0f | bellard | devname); |
9151 | 8d11df9e | bellard | exit(1);
|
9152 | 8d11df9e | bellard | } |
9153 | af3a9031 | ths | if (strstart(devname, "vc", 0)) |
9154 | 7ba1260a | bellard | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
|
9155 | 8d11df9e | bellard | } |
9156 | 82c643ff | bellard | } |
9157 | 82c643ff | bellard | |
9158 | 6508fe59 | bellard | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
9159 | c03b0f0f | bellard | const char *devname = parallel_devices[i]; |
9160 | fd5f393a | pbrook | if (devname && strcmp(devname, "none")) { |
9161 | c03b0f0f | bellard | parallel_hds[i] = qemu_chr_open(devname); |
9162 | 6508fe59 | bellard | if (!parallel_hds[i]) {
|
9163 | 5fafdf24 | ths | fprintf(stderr, "qemu: could not open parallel device '%s'\n",
|
9164 | c03b0f0f | bellard | devname); |
9165 | 6508fe59 | bellard | exit(1);
|
9166 | 6508fe59 | bellard | } |
9167 | af3a9031 | ths | if (strstart(devname, "vc", 0)) |
9168 | 7ba1260a | bellard | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
|
9169 | 6508fe59 | bellard | } |
9170 | 6508fe59 | bellard | } |
9171 | 6508fe59 | bellard | |
9172 | b881c2c6 | blueswir1 | machine->init(ram_size, vga_ram_size, boot_devices, ds, |
9173 | 94fc95cd | j_mayer | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
9174 | 73332e5c | bellard | |
9175 | 0d92ed30 | pbrook | /* init USB devices */
|
9176 | 0d92ed30 | pbrook | if (usb_enabled) {
|
9177 | 0d92ed30 | pbrook | for(i = 0; i < usb_devices_index; i++) { |
9178 | 0d92ed30 | pbrook | if (usb_device_add(usb_devices[i]) < 0) { |
9179 | 0d92ed30 | pbrook | fprintf(stderr, "Warning: could not add USB device %s\n",
|
9180 | 0d92ed30 | pbrook | usb_devices[i]); |
9181 | 0d92ed30 | pbrook | } |
9182 | 0d92ed30 | pbrook | } |
9183 | 0d92ed30 | pbrook | } |
9184 | 0d92ed30 | pbrook | |
9185 | 740733bb | ths | if (display_state.dpy_refresh) {
|
9186 | 740733bb | ths | display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state); |
9187 | 740733bb | ths | qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock)); |
9188 | 740733bb | ths | } |
9189 | 7f7f9873 | bellard | |
9190 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
9191 | b4608c04 | bellard | if (use_gdbstub) {
|
9192 | c636bb66 | bellard | /* XXX: use standard host:port notation and modify options
|
9193 | c636bb66 | bellard | accordingly. */
|
9194 | cfc3475a | pbrook | if (gdbserver_start(gdbstub_port) < 0) { |
9195 | cfc3475a | pbrook | fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
|
9196 | c636bb66 | bellard | gdbstub_port); |
9197 | 8a7ddc38 | bellard | exit(1);
|
9198 | 8a7ddc38 | bellard | } |
9199 | 45669e00 | balrog | } |
9200 | 67b915a5 | bellard | #endif
|
9201 | 45669e00 | balrog | |
9202 | d63d307f | bellard | if (loadvm)
|
9203 | faea38e7 | bellard | do_loadvm(loadvm); |
9204 | d63d307f | bellard | |
9205 | 67b915a5 | bellard | { |
9206 | 5905b2e5 | bellard | /* XXX: simplify init */
|
9207 | 83ab7950 | aliguori | read_passwords(); |
9208 | 3c07f8e8 | pbrook | if (autostart) {
|
9209 | 5905b2e5 | bellard | vm_start(); |
9210 | 5905b2e5 | bellard | } |
9211 | 0824d6fc | bellard | } |
9212 | ffd843bc | ths | |
9213 | 71e3ceb8 | ths | if (daemonize) {
|
9214 | 71e3ceb8 | ths | uint8_t status = 0;
|
9215 | 71e3ceb8 | ths | ssize_t len; |
9216 | 71e3ceb8 | ths | int fd;
|
9217 | 71e3ceb8 | ths | |
9218 | 71e3ceb8 | ths | again1:
|
9219 | 71e3ceb8 | ths | len = write(fds[1], &status, 1); |
9220 | 71e3ceb8 | ths | if (len == -1 && (errno == EINTR)) |
9221 | 71e3ceb8 | ths | goto again1;
|
9222 | 71e3ceb8 | ths | |
9223 | 71e3ceb8 | ths | if (len != 1) |
9224 | 71e3ceb8 | ths | exit(1);
|
9225 | 71e3ceb8 | ths | |
9226 | bd54b863 | aliguori | chdir("/");
|
9227 | aeb30be6 | balrog | TFR(fd = open("/dev/null", O_RDWR));
|
9228 | 71e3ceb8 | ths | if (fd == -1) |
9229 | 71e3ceb8 | ths | exit(1);
|
9230 | 71e3ceb8 | ths | |
9231 | 71e3ceb8 | ths | dup2(fd, 0);
|
9232 | 71e3ceb8 | ths | dup2(fd, 1);
|
9233 | 71e3ceb8 | ths | dup2(fd, 2);
|
9234 | 71e3ceb8 | ths | |
9235 | 71e3ceb8 | ths | close(fd); |
9236 | 71e3ceb8 | ths | } |
9237 | 71e3ceb8 | ths | |
9238 | 8a7ddc38 | bellard | main_loop(); |
9239 | 40c3bac3 | bellard | quit_timers(); |
9240 | b46a8906 | ths | |
9241 | 7d294b61 | ths | #if !defined(_WIN32)
|
9242 | b46a8906 | ths | /* close network clients */
|
9243 | b46a8906 | ths | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
9244 | b46a8906 | ths | VLANClientState *vc; |
9245 | b46a8906 | ths | |
9246 | 7d294b61 | ths | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
9247 | b46a8906 | ths | if (vc->fd_read == tap_receive) {
|
9248 | b46a8906 | ths | char ifname[64]; |
9249 | b46a8906 | ths | TAPState *s = vc->opaque; |
9250 | b46a8906 | ths | |
9251 | b46a8906 | ths | if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 && |
9252 | b46a8906 | ths | s->down_script[0])
|
9253 | b46a8906 | ths | launch_script(s->down_script, ifname, s->fd); |
9254 | b46a8906 | ths | } |
9255 | 8a16d273 | ths | #if defined(CONFIG_VDE)
|
9256 | 8a16d273 | ths | if (vc->fd_read == vde_from_qemu) {
|
9257 | 8a16d273 | ths | VDEState *s = vc->opaque; |
9258 | 8a16d273 | ths | vde_close(s->vde); |
9259 | 8a16d273 | ths | } |
9260 | 8a16d273 | ths | #endif
|
9261 | 4fddf62a | ths | } |
9262 | 7d294b61 | ths | } |
9263 | 7d294b61 | ths | #endif
|
9264 | 0824d6fc | bellard | return 0; |
9265 | 0824d6fc | bellard | } |