root / vl.c @ f97572e5
History | View | Annotate | Download (238.9 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 | 8a16d273 | ths | #if defined(CONFIG_VDE)
|
113 | 8a16d273 | ths | #include <libvdeplug.h> |
114 | 8a16d273 | ths | #endif
|
115 | 8a16d273 | ths | |
116 | 67b915a5 | bellard | #ifdef _WIN32
|
117 | 7d3505c5 | bellard | #include <malloc.h> |
118 | 67b915a5 | bellard | #include <sys/timeb.h> |
119 | 4fddf62a | ths | #include <mmsystem.h> |
120 | 67b915a5 | bellard | #define getopt_long_only getopt_long
|
121 | 67b915a5 | bellard | #define memalign(align, size) malloc(size)
|
122 | 67b915a5 | bellard | #endif
|
123 | 67b915a5 | bellard | |
124 | 6ca957f0 | bellard | #include "qemu_socket.h" |
125 | 6ca957f0 | bellard | |
126 | 73332e5c | bellard | #ifdef CONFIG_SDL
|
127 | 96bcd4f8 | bellard | #ifdef __APPLE__
|
128 | 83fb7adf | bellard | #include <SDL/SDL.h> |
129 | 96bcd4f8 | bellard | #endif
|
130 | 73332e5c | bellard | #endif /* CONFIG_SDL */ |
131 | 0824d6fc | bellard | |
132 | 5b0753e0 | bellard | #ifdef CONFIG_COCOA
|
133 | 5b0753e0 | bellard | #undef main
|
134 | 5b0753e0 | bellard | #define main qemu_main
|
135 | 5b0753e0 | bellard | #endif /* CONFIG_COCOA */ |
136 | 5b0753e0 | bellard | |
137 | 0824d6fc | bellard | #include "disas.h" |
138 | fc01f7e7 | bellard | |
139 | 8a7ddc38 | bellard | #include "exec-all.h" |
140 | 0824d6fc | bellard | |
141 | 5a67135a | bellard | #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
142 | b46a8906 | ths | #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown" |
143 | a14d6c8c | pbrook | #ifdef __sun__
|
144 | a14d6c8c | pbrook | #define SMBD_COMMAND "/usr/sfw/sbin/smbd" |
145 | a14d6c8c | pbrook | #else
|
146 | a14d6c8c | pbrook | #define SMBD_COMMAND "/usr/sbin/smbd" |
147 | a14d6c8c | pbrook | #endif
|
148 | f1510b2c | bellard | |
149 | 0824d6fc | bellard | //#define DEBUG_UNUSED_IOPORT
|
150 | fd872598 | bellard | //#define DEBUG_IOPORT
|
151 | 330d0414 | bellard | |
152 | 77d4bc34 | bellard | #ifdef TARGET_PPC
|
153 | 77d4bc34 | bellard | #define DEFAULT_RAM_SIZE 144 |
154 | 77d4bc34 | bellard | #else
|
155 | 1bfe856e | bellard | #define DEFAULT_RAM_SIZE 128 |
156 | 77d4bc34 | bellard | #endif
|
157 | 313aa567 | bellard | |
158 | 0d92ed30 | pbrook | /* Max number of USB devices that can be specified on the commandline. */
|
159 | 0d92ed30 | pbrook | #define MAX_USB_CMDLINE 8 |
160 | 0d92ed30 | pbrook | |
161 | 7dea1da4 | bellard | /* XXX: use a two level table to limit memory usage */
|
162 | 7dea1da4 | bellard | #define MAX_IOPORTS 65536 |
163 | 0824d6fc | bellard | |
164 | 80cabfad | bellard | const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
165 | 1192dad8 | j_mayer | const char *bios_name = NULL; |
166 | c4b1fcc0 | bellard | void *ioport_opaque[MAX_IOPORTS];
|
167 | fc01f7e7 | bellard | IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
|
168 | fc01f7e7 | bellard | IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
|
169 | e4bcb14c | ths | /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
|
170 | faea38e7 | bellard | to store the VM snapshots */
|
171 | e4bcb14c | ths | DriveInfo drives_table[MAX_DRIVES+1];
|
172 | e4bcb14c | ths | int nb_drives;
|
173 | faea38e7 | bellard | /* point to the block driver where the snapshots are managed */
|
174 | faea38e7 | bellard | BlockDriverState *bs_snapshots; |
175 | 313aa567 | bellard | int vga_ram_size;
|
176 | 313aa567 | bellard | static DisplayState display_state;
|
177 | a20dd508 | bellard | int nographic;
|
178 | 4d3b6f6e | balrog | int curses;
|
179 | 3d11d0eb | bellard | const char* keyboard_layout = NULL; |
180 | 313aa567 | bellard | int64_t ticks_per_sec; |
181 | 00f82b8a | aurel32 | ram_addr_t ram_size; |
182 | 80cabfad | bellard | int pit_min_timer_count = 0; |
183 | c4b1fcc0 | bellard | int nb_nics;
|
184 | 7c9d8e07 | bellard | NICInfo nd_table[MAX_NICS]; |
185 | 8a7ddc38 | bellard | int vm_running;
|
186 | f6503059 | balrog | static int rtc_utc = 1; |
187 | f6503059 | balrog | static int rtc_date_offset = -1; /* -1 means no change */ |
188 | 1bfe856e | bellard | int cirrus_vga_enabled = 1; |
189 | d34cab9f | ths | int vmsvga_enabled = 0; |
190 | d827220b | bellard | #ifdef TARGET_SPARC
|
191 | d827220b | bellard | int graphic_width = 1024; |
192 | d827220b | bellard | int graphic_height = 768; |
193 | eee0b836 | blueswir1 | int graphic_depth = 8; |
194 | d827220b | bellard | #else
|
195 | 1bfe856e | bellard | int graphic_width = 800; |
196 | 1bfe856e | bellard | int graphic_height = 600; |
197 | e9b137c2 | bellard | int graphic_depth = 15; |
198 | eee0b836 | blueswir1 | #endif
|
199 | d63d307f | bellard | int full_screen = 0; |
200 | 43523e93 | ths | int no_frame = 0; |
201 | 667accab | ths | int no_quit = 0; |
202 | 8d11df9e | bellard | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
203 | 6508fe59 | bellard | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
204 | a09db21f | bellard | #ifdef TARGET_I386
|
205 | a09db21f | bellard | int win2k_install_hack = 0; |
206 | a09db21f | bellard | #endif
|
207 | bb36d470 | bellard | int usb_enabled = 0; |
208 | 7c9d8e07 | bellard | static VLANState *first_vlan;
|
209 | 6a00d601 | bellard | int smp_cpus = 1; |
210 | 73fc9742 | ths | const char *vnc_display; |
211 | d3e9db93 | bellard | #if defined(TARGET_SPARC)
|
212 | ba3c64fb | bellard | #define MAX_CPUS 16 |
213 | d3e9db93 | bellard | #elif defined(TARGET_I386)
|
214 | d3e9db93 | bellard | #define MAX_CPUS 255 |
215 | ba3c64fb | bellard | #else
|
216 | d3e9db93 | bellard | #define MAX_CPUS 1 |
217 | ba3c64fb | bellard | #endif
|
218 | 6515b203 | bellard | int acpi_enabled = 1; |
219 | 52ca8d6a | bellard | int fd_bootchk = 1; |
220 | d1beab82 | bellard | int no_reboot = 0; |
221 | b2f76161 | aurel32 | int no_shutdown = 0; |
222 | 9467cd46 | balrog | int cursor_hide = 1; |
223 | a171fe39 | balrog | int graphic_rotate = 0; |
224 | 71e3ceb8 | ths | int daemonize = 0; |
225 | 9ae02555 | ths | const char *option_rom[MAX_OPTION_ROMS]; |
226 | 9ae02555 | ths | int nb_option_roms;
|
227 | 8e71621f | pbrook | int semihosting_enabled = 0; |
228 | 3c07f8e8 | pbrook | int autostart = 1; |
229 | 2b8f2d41 | balrog | #ifdef TARGET_ARM
|
230 | 2b8f2d41 | balrog | int old_param = 0; |
231 | 2b8f2d41 | balrog | #endif
|
232 | c35734b2 | ths | const char *qemu_name; |
233 | 3780e197 | ths | int alt_grab = 0; |
234 | 66508601 | blueswir1 | #ifdef TARGET_SPARC
|
235 | 66508601 | blueswir1 | unsigned int nb_prom_envs = 0; |
236 | 66508601 | blueswir1 | const char *prom_envs[MAX_PROM_ENVS]; |
237 | 66508601 | blueswir1 | #endif
|
238 | e4bcb14c | ths | int nb_drives_opt;
|
239 | 609497ab | balrog | struct drive_opt {
|
240 | 609497ab | balrog | const char *file; |
241 | 609497ab | balrog | char opt[1024]; |
242 | 609497ab | balrog | } drives_opt[MAX_DRIVES]; |
243 | 0824d6fc | bellard | |
244 | ee5605e5 | balrog | static CPUState *cur_cpu;
|
245 | ee5605e5 | balrog | static CPUState *next_cpu;
|
246 | 76ea08f9 | balrog | static int event_pending = 1; |
247 | bf20dc07 | ths | /* Conversion factor from emulated instructions to virtual clock ticks. */
|
248 | 2e70f6ef | pbrook | static int icount_time_shift; |
249 | bf20dc07 | ths | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
|
250 | 2e70f6ef | pbrook | #define MAX_ICOUNT_SHIFT 10 |
251 | 2e70f6ef | pbrook | /* Compensate for varying guest execution speed. */
|
252 | 2e70f6ef | pbrook | static int64_t qemu_icount_bias;
|
253 | 2e70f6ef | pbrook | QEMUTimer *icount_rt_timer; |
254 | 2e70f6ef | pbrook | QEMUTimer *icount_vm_timer; |
255 | ee5605e5 | balrog | |
256 | aeb30be6 | balrog | #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR) |
257 | aeb30be6 | balrog | |
258 | 0824d6fc | bellard | /***********************************************************/
|
259 | 26aa7d72 | bellard | /* x86 ISA bus support */
|
260 | 26aa7d72 | bellard | |
261 | 26aa7d72 | bellard | target_phys_addr_t isa_mem_base = 0;
|
262 | 3de388f6 | bellard | PicState2 *isa_pic; |
263 | 0824d6fc | bellard | |
264 | 477e3edf | aliguori | static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
|
265 | 477e3edf | aliguori | static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
|
266 | 477e3edf | aliguori | |
267 | 477e3edf | aliguori | static uint32_t ioport_read(int index, uint32_t address) |
268 | 477e3edf | aliguori | { |
269 | 477e3edf | aliguori | static IOPortReadFunc *default_func[3] = { |
270 | 477e3edf | aliguori | default_ioport_readb, |
271 | 477e3edf | aliguori | default_ioport_readw, |
272 | 477e3edf | aliguori | default_ioport_readl |
273 | 477e3edf | aliguori | }; |
274 | 477e3edf | aliguori | IOPortReadFunc *func = ioport_read_table[index][address]; |
275 | 477e3edf | aliguori | if (!func)
|
276 | 477e3edf | aliguori | func = default_func[index]; |
277 | 477e3edf | aliguori | return func(ioport_opaque[address], address);
|
278 | 477e3edf | aliguori | } |
279 | 477e3edf | aliguori | |
280 | 477e3edf | aliguori | static void ioport_write(int index, uint32_t address, uint32_t data) |
281 | 477e3edf | aliguori | { |
282 | 477e3edf | aliguori | static IOPortWriteFunc *default_func[3] = { |
283 | 477e3edf | aliguori | default_ioport_writeb, |
284 | 477e3edf | aliguori | default_ioport_writew, |
285 | 477e3edf | aliguori | default_ioport_writel |
286 | 477e3edf | aliguori | }; |
287 | 477e3edf | aliguori | IOPortWriteFunc *func = ioport_write_table[index][address]; |
288 | 477e3edf | aliguori | if (!func)
|
289 | 477e3edf | aliguori | func = default_func[index]; |
290 | 477e3edf | aliguori | func(ioport_opaque[address], address, data); |
291 | 477e3edf | aliguori | } |
292 | 477e3edf | aliguori | |
293 | 9596ebb7 | pbrook | static uint32_t default_ioport_readb(void *opaque, uint32_t address) |
294 | 0824d6fc | bellard | { |
295 | 0824d6fc | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
296 | 1196be37 | ths | fprintf(stderr, "unused inb: port=0x%04x\n", address);
|
297 | 0824d6fc | bellard | #endif
|
298 | fc01f7e7 | bellard | return 0xff; |
299 | 0824d6fc | bellard | } |
300 | 0824d6fc | bellard | |
301 | 9596ebb7 | pbrook | static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
302 | 0824d6fc | bellard | { |
303 | 0824d6fc | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
304 | 1196be37 | ths | fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
|
305 | 0824d6fc | bellard | #endif
|
306 | 0824d6fc | bellard | } |
307 | 0824d6fc | bellard | |
308 | 0824d6fc | bellard | /* default is to make two byte accesses */
|
309 | 9596ebb7 | pbrook | static uint32_t default_ioport_readw(void *opaque, uint32_t address) |
310 | 0824d6fc | bellard | { |
311 | 0824d6fc | bellard | uint32_t data; |
312 | 477e3edf | aliguori | data = ioport_read(0, address);
|
313 | db45c29a | bellard | address = (address + 1) & (MAX_IOPORTS - 1); |
314 | 477e3edf | aliguori | data |= ioport_read(0, address) << 8; |
315 | 0824d6fc | bellard | return data;
|
316 | 0824d6fc | bellard | } |
317 | 0824d6fc | bellard | |
318 | 9596ebb7 | pbrook | static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
319 | 0824d6fc | bellard | { |
320 | 477e3edf | aliguori | ioport_write(0, address, data & 0xff); |
321 | db45c29a | bellard | address = (address + 1) & (MAX_IOPORTS - 1); |
322 | 477e3edf | aliguori | ioport_write(0, address, (data >> 8) & 0xff); |
323 | 0824d6fc | bellard | } |
324 | 0824d6fc | bellard | |
325 | 9596ebb7 | pbrook | static uint32_t default_ioport_readl(void *opaque, uint32_t address) |
326 | 0824d6fc | bellard | { |
327 | fc01f7e7 | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
328 | 1196be37 | ths | fprintf(stderr, "unused inl: port=0x%04x\n", address);
|
329 | fc01f7e7 | bellard | #endif
|
330 | fc01f7e7 | bellard | return 0xffffffff; |
331 | 0824d6fc | bellard | } |
332 | 0824d6fc | bellard | |
333 | 9596ebb7 | pbrook | static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
334 | 0824d6fc | bellard | { |
335 | fc01f7e7 | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
336 | 1196be37 | ths | fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
|
337 | fc01f7e7 | bellard | #endif
|
338 | 0824d6fc | bellard | } |
339 | 0824d6fc | bellard | |
340 | fc01f7e7 | bellard | /* size is the word size in byte */
|
341 | 5fafdf24 | ths | int register_ioport_read(int start, int length, int size, |
342 | c4b1fcc0 | bellard | IOPortReadFunc *func, void *opaque)
|
343 | f1510b2c | bellard | { |
344 | fc01f7e7 | bellard | int i, bsize;
|
345 | f1510b2c | bellard | |
346 | c4b1fcc0 | bellard | if (size == 1) { |
347 | fc01f7e7 | bellard | bsize = 0;
|
348 | c4b1fcc0 | bellard | } else if (size == 2) { |
349 | fc01f7e7 | bellard | bsize = 1;
|
350 | c4b1fcc0 | bellard | } else if (size == 4) { |
351 | fc01f7e7 | bellard | bsize = 2;
|
352 | c4b1fcc0 | bellard | } else {
|
353 | 88fdf56f | balrog | hw_error("register_ioport_read: invalid size");
|
354 | fc01f7e7 | bellard | return -1; |
355 | c4b1fcc0 | bellard | } |
356 | c4b1fcc0 | bellard | for(i = start; i < start + length; i += size) {
|
357 | fc01f7e7 | bellard | ioport_read_table[bsize][i] = func; |
358 | c4b1fcc0 | bellard | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
359 | 88fdf56f | balrog | hw_error("register_ioport_read: invalid opaque");
|
360 | c4b1fcc0 | bellard | ioport_opaque[i] = opaque; |
361 | c4b1fcc0 | bellard | } |
362 | f1510b2c | bellard | return 0; |
363 | f1510b2c | bellard | } |
364 | f1510b2c | bellard | |
365 | fc01f7e7 | bellard | /* size is the word size in byte */
|
366 | 5fafdf24 | ths | int register_ioport_write(int start, int length, int size, |
367 | c4b1fcc0 | bellard | IOPortWriteFunc *func, void *opaque)
|
368 | f1510b2c | bellard | { |
369 | fc01f7e7 | bellard | int i, bsize;
|
370 | f1510b2c | bellard | |
371 | c4b1fcc0 | bellard | if (size == 1) { |
372 | fc01f7e7 | bellard | bsize = 0;
|
373 | c4b1fcc0 | bellard | } else if (size == 2) { |
374 | fc01f7e7 | bellard | bsize = 1;
|
375 | c4b1fcc0 | bellard | } else if (size == 4) { |
376 | fc01f7e7 | bellard | bsize = 2;
|
377 | c4b1fcc0 | bellard | } else {
|
378 | 88fdf56f | balrog | hw_error("register_ioport_write: invalid size");
|
379 | fc01f7e7 | bellard | return -1; |
380 | c4b1fcc0 | bellard | } |
381 | c4b1fcc0 | bellard | for(i = start; i < start + length; i += size) {
|
382 | fc01f7e7 | bellard | ioport_write_table[bsize][i] = func; |
383 | 88fdf56f | balrog | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
384 | 88fdf56f | balrog | hw_error("register_ioport_write: invalid opaque");
|
385 | c4b1fcc0 | bellard | ioport_opaque[i] = opaque; |
386 | c4b1fcc0 | bellard | } |
387 | f1510b2c | bellard | return 0; |
388 | f1510b2c | bellard | } |
389 | f1510b2c | bellard | |
390 | 69b91039 | bellard | void isa_unassign_ioport(int start, int length) |
391 | 69b91039 | bellard | { |
392 | 69b91039 | bellard | int i;
|
393 | 69b91039 | bellard | |
394 | 69b91039 | bellard | for(i = start; i < start + length; i++) {
|
395 | 69b91039 | bellard | ioport_read_table[0][i] = default_ioport_readb;
|
396 | 69b91039 | bellard | ioport_read_table[1][i] = default_ioport_readw;
|
397 | 69b91039 | bellard | ioport_read_table[2][i] = default_ioport_readl;
|
398 | 69b91039 | bellard | |
399 | 69b91039 | bellard | ioport_write_table[0][i] = default_ioport_writeb;
|
400 | 69b91039 | bellard | ioport_write_table[1][i] = default_ioport_writew;
|
401 | 69b91039 | bellard | ioport_write_table[2][i] = default_ioport_writel;
|
402 | 69b91039 | bellard | } |
403 | 69b91039 | bellard | } |
404 | 69b91039 | bellard | |
405 | 20f32282 | bellard | /***********************************************************/
|
406 | 20f32282 | bellard | |
407 | c45886db | bellard | void cpu_outb(CPUState *env, int addr, int val) |
408 | 0824d6fc | bellard | { |
409 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
410 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
411 | fd872598 | bellard | fprintf(logfile, "outb: %04x %02x\n", addr, val);
|
412 | 3b46e624 | ths | #endif
|
413 | 477e3edf | aliguori | ioport_write(0, addr, val);
|
414 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
415 | 89bfc105 | bellard | if (env)
|
416 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
417 | 89bfc105 | bellard | #endif
|
418 | 0824d6fc | bellard | } |
419 | 0824d6fc | bellard | |
420 | c45886db | bellard | void cpu_outw(CPUState *env, int addr, int val) |
421 | 0824d6fc | bellard | { |
422 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
423 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
424 | fd872598 | bellard | fprintf(logfile, "outw: %04x %04x\n", addr, val);
|
425 | 3b46e624 | ths | #endif
|
426 | 477e3edf | aliguori | ioport_write(1, addr, val);
|
427 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
428 | 89bfc105 | bellard | if (env)
|
429 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
430 | 89bfc105 | bellard | #endif
|
431 | 0824d6fc | bellard | } |
432 | 0824d6fc | bellard | |
433 | c45886db | bellard | void cpu_outl(CPUState *env, int addr, int val) |
434 | 0824d6fc | bellard | { |
435 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
436 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
437 | fd872598 | bellard | fprintf(logfile, "outl: %04x %08x\n", addr, val);
|
438 | fd872598 | bellard | #endif
|
439 | 477e3edf | aliguori | ioport_write(2, addr, val);
|
440 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
441 | 89bfc105 | bellard | if (env)
|
442 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
443 | 89bfc105 | bellard | #endif
|
444 | 0824d6fc | bellard | } |
445 | 0824d6fc | bellard | |
446 | c45886db | bellard | int cpu_inb(CPUState *env, int addr) |
447 | 0824d6fc | bellard | { |
448 | fd872598 | bellard | int val;
|
449 | 477e3edf | aliguori | val = ioport_read(0, addr);
|
450 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
451 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
452 | fd872598 | bellard | fprintf(logfile, "inb : %04x %02x\n", addr, val);
|
453 | fd872598 | bellard | #endif
|
454 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
455 | 89bfc105 | bellard | if (env)
|
456 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
457 | 89bfc105 | bellard | #endif
|
458 | fd872598 | bellard | return val;
|
459 | 0824d6fc | bellard | } |
460 | 0824d6fc | bellard | |
461 | c45886db | bellard | int cpu_inw(CPUState *env, int addr) |
462 | 0824d6fc | bellard | { |
463 | fd872598 | bellard | int val;
|
464 | 477e3edf | aliguori | val = ioport_read(1, addr);
|
465 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
466 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
467 | fd872598 | bellard | fprintf(logfile, "inw : %04x %04x\n", addr, val);
|
468 | fd872598 | bellard | #endif
|
469 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
470 | 89bfc105 | bellard | if (env)
|
471 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
472 | 89bfc105 | bellard | #endif
|
473 | fd872598 | bellard | return val;
|
474 | 0824d6fc | bellard | } |
475 | 0824d6fc | bellard | |
476 | c45886db | bellard | int cpu_inl(CPUState *env, int addr) |
477 | 0824d6fc | bellard | { |
478 | fd872598 | bellard | int val;
|
479 | 477e3edf | aliguori | val = ioport_read(2, addr);
|
480 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
481 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
482 | fd872598 | bellard | fprintf(logfile, "inl : %04x %08x\n", addr, val);
|
483 | fd872598 | bellard | #endif
|
484 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
485 | 89bfc105 | bellard | if (env)
|
486 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
487 | 89bfc105 | bellard | #endif
|
488 | fd872598 | bellard | return val;
|
489 | 0824d6fc | bellard | } |
490 | 0824d6fc | bellard | |
491 | 0824d6fc | bellard | /***********************************************************/
|
492 | 0824d6fc | bellard | void hw_error(const char *fmt, ...) |
493 | 0824d6fc | bellard | { |
494 | 0824d6fc | bellard | va_list ap; |
495 | 6a00d601 | bellard | CPUState *env; |
496 | 0824d6fc | bellard | |
497 | 0824d6fc | bellard | va_start(ap, fmt); |
498 | 0824d6fc | bellard | fprintf(stderr, "qemu: hardware error: ");
|
499 | 0824d6fc | bellard | vfprintf(stderr, fmt, ap); |
500 | 0824d6fc | bellard | fprintf(stderr, "\n");
|
501 | 6a00d601 | bellard | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
502 | 6a00d601 | bellard | fprintf(stderr, "CPU #%d:\n", env->cpu_index);
|
503 | 0824d6fc | bellard | #ifdef TARGET_I386
|
504 | 6a00d601 | bellard | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
505 | c45886db | bellard | #else
|
506 | 6a00d601 | bellard | cpu_dump_state(env, stderr, fprintf, 0);
|
507 | 0824d6fc | bellard | #endif
|
508 | 6a00d601 | bellard | } |
509 | 0824d6fc | bellard | va_end(ap); |
510 | 0824d6fc | bellard | abort(); |
511 | 0824d6fc | bellard | } |
512 | 0824d6fc | bellard | |
513 | 8a7ddc38 | bellard | /***********************************************************/
|
514 | 63066f4f | bellard | /* keyboard/mouse */
|
515 | 63066f4f | bellard | |
516 | 63066f4f | bellard | static QEMUPutKBDEvent *qemu_put_kbd_event;
|
517 | 63066f4f | bellard | static void *qemu_put_kbd_event_opaque; |
518 | 455204eb | ths | static QEMUPutMouseEntry *qemu_put_mouse_event_head;
|
519 | 455204eb | ths | static QEMUPutMouseEntry *qemu_put_mouse_event_current;
|
520 | 63066f4f | bellard | |
521 | 63066f4f | bellard | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
522 | 63066f4f | bellard | { |
523 | 63066f4f | bellard | qemu_put_kbd_event_opaque = opaque; |
524 | 63066f4f | bellard | qemu_put_kbd_event = func; |
525 | 63066f4f | bellard | } |
526 | 63066f4f | bellard | |
527 | 455204eb | ths | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
528 | 455204eb | ths | void *opaque, int absolute, |
529 | 455204eb | ths | const char *name) |
530 | 63066f4f | bellard | { |
531 | 455204eb | ths | QEMUPutMouseEntry *s, *cursor; |
532 | 455204eb | ths | |
533 | 455204eb | ths | s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
|
534 | 455204eb | ths | if (!s)
|
535 | 455204eb | ths | return NULL; |
536 | 455204eb | ths | |
537 | 455204eb | ths | s->qemu_put_mouse_event = func; |
538 | 455204eb | ths | s->qemu_put_mouse_event_opaque = opaque; |
539 | 455204eb | ths | s->qemu_put_mouse_event_absolute = absolute; |
540 | 455204eb | ths | s->qemu_put_mouse_event_name = qemu_strdup(name); |
541 | 455204eb | ths | s->next = NULL;
|
542 | 455204eb | ths | |
543 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
544 | 455204eb | ths | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
545 | 455204eb | ths | return s;
|
546 | 455204eb | ths | } |
547 | 455204eb | ths | |
548 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
549 | 455204eb | ths | while (cursor->next != NULL) |
550 | 455204eb | ths | cursor = cursor->next; |
551 | 455204eb | ths | |
552 | 455204eb | ths | cursor->next = s; |
553 | 455204eb | ths | qemu_put_mouse_event_current = s; |
554 | 455204eb | ths | |
555 | 455204eb | ths | return s;
|
556 | 455204eb | ths | } |
557 | 455204eb | ths | |
558 | 455204eb | ths | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
|
559 | 455204eb | ths | { |
560 | 455204eb | ths | QEMUPutMouseEntry *prev = NULL, *cursor;
|
561 | 455204eb | ths | |
562 | 455204eb | ths | if (!qemu_put_mouse_event_head || entry == NULL) |
563 | 455204eb | ths | return;
|
564 | 455204eb | ths | |
565 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
566 | 455204eb | ths | while (cursor != NULL && cursor != entry) { |
567 | 455204eb | ths | prev = cursor; |
568 | 455204eb | ths | cursor = cursor->next; |
569 | 455204eb | ths | } |
570 | 455204eb | ths | |
571 | 455204eb | ths | if (cursor == NULL) // does not exist or list empty |
572 | 455204eb | ths | return;
|
573 | 455204eb | ths | else if (prev == NULL) { // entry is head |
574 | 455204eb | ths | qemu_put_mouse_event_head = cursor->next; |
575 | 455204eb | ths | if (qemu_put_mouse_event_current == entry)
|
576 | 455204eb | ths | qemu_put_mouse_event_current = cursor->next; |
577 | 455204eb | ths | qemu_free(entry->qemu_put_mouse_event_name); |
578 | 455204eb | ths | qemu_free(entry); |
579 | 455204eb | ths | return;
|
580 | 455204eb | ths | } |
581 | 455204eb | ths | |
582 | 455204eb | ths | prev->next = entry->next; |
583 | 455204eb | ths | |
584 | 455204eb | ths | if (qemu_put_mouse_event_current == entry)
|
585 | 455204eb | ths | qemu_put_mouse_event_current = prev; |
586 | 455204eb | ths | |
587 | 455204eb | ths | qemu_free(entry->qemu_put_mouse_event_name); |
588 | 455204eb | ths | qemu_free(entry); |
589 | 63066f4f | bellard | } |
590 | 63066f4f | bellard | |
591 | 63066f4f | bellard | void kbd_put_keycode(int keycode) |
592 | 63066f4f | bellard | { |
593 | 63066f4f | bellard | if (qemu_put_kbd_event) {
|
594 | 63066f4f | bellard | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
595 | 63066f4f | bellard | } |
596 | 63066f4f | bellard | } |
597 | 63066f4f | bellard | |
598 | 63066f4f | bellard | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
599 | 63066f4f | bellard | { |
600 | 455204eb | ths | QEMUPutMouseEvent *mouse_event; |
601 | 455204eb | ths | void *mouse_event_opaque;
|
602 | a171fe39 | balrog | int width;
|
603 | 455204eb | ths | |
604 | 455204eb | ths | if (!qemu_put_mouse_event_current) {
|
605 | 455204eb | ths | return;
|
606 | 455204eb | ths | } |
607 | 455204eb | ths | |
608 | 455204eb | ths | mouse_event = |
609 | 455204eb | ths | qemu_put_mouse_event_current->qemu_put_mouse_event; |
610 | 455204eb | ths | mouse_event_opaque = |
611 | 455204eb | ths | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
612 | 455204eb | ths | |
613 | 455204eb | ths | if (mouse_event) {
|
614 | a171fe39 | balrog | if (graphic_rotate) {
|
615 | a171fe39 | balrog | if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
|
616 | a171fe39 | balrog | width = 0x7fff;
|
617 | a171fe39 | balrog | else
|
618 | b94ed577 | aurel32 | width = graphic_width - 1;
|
619 | a171fe39 | balrog | mouse_event(mouse_event_opaque, |
620 | a171fe39 | balrog | width - dy, dx, dz, buttons_state); |
621 | a171fe39 | balrog | } else
|
622 | a171fe39 | balrog | mouse_event(mouse_event_opaque, |
623 | a171fe39 | balrog | dx, dy, dz, buttons_state); |
624 | 63066f4f | bellard | } |
625 | 63066f4f | bellard | } |
626 | 63066f4f | bellard | |
627 | 09b26c5e | bellard | int kbd_mouse_is_absolute(void) |
628 | 09b26c5e | bellard | { |
629 | 455204eb | ths | if (!qemu_put_mouse_event_current)
|
630 | 455204eb | ths | return 0; |
631 | 455204eb | ths | |
632 | 455204eb | ths | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
|
633 | 455204eb | ths | } |
634 | 455204eb | ths | |
635 | 455204eb | ths | void do_info_mice(void) |
636 | 455204eb | ths | { |
637 | 455204eb | ths | QEMUPutMouseEntry *cursor; |
638 | 455204eb | ths | int index = 0; |
639 | 455204eb | ths | |
640 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
641 | 455204eb | ths | term_printf("No mouse devices connected\n");
|
642 | 455204eb | ths | return;
|
643 | 455204eb | ths | } |
644 | 455204eb | ths | |
645 | 455204eb | ths | term_printf("Mouse devices available:\n");
|
646 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
647 | 455204eb | ths | while (cursor != NULL) { |
648 | 455204eb | ths | term_printf("%c Mouse #%d: %s\n",
|
649 | 455204eb | ths | (cursor == qemu_put_mouse_event_current ? '*' : ' '), |
650 | 455204eb | ths | index, cursor->qemu_put_mouse_event_name); |
651 | 455204eb | ths | index++; |
652 | 455204eb | ths | cursor = cursor->next; |
653 | 455204eb | ths | } |
654 | 455204eb | ths | } |
655 | 455204eb | ths | |
656 | 455204eb | ths | void do_mouse_set(int index) |
657 | 455204eb | ths | { |
658 | 455204eb | ths | QEMUPutMouseEntry *cursor; |
659 | 455204eb | ths | int i = 0; |
660 | 455204eb | ths | |
661 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
662 | 455204eb | ths | term_printf("No mouse devices connected\n");
|
663 | 455204eb | ths | return;
|
664 | 455204eb | ths | } |
665 | 455204eb | ths | |
666 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
667 | 455204eb | ths | while (cursor != NULL && index != i) { |
668 | 455204eb | ths | i++; |
669 | 455204eb | ths | cursor = cursor->next; |
670 | 455204eb | ths | } |
671 | 455204eb | ths | |
672 | 455204eb | ths | if (cursor != NULL) |
673 | 455204eb | ths | qemu_put_mouse_event_current = cursor; |
674 | 455204eb | ths | else
|
675 | 455204eb | ths | term_printf("Mouse at given index not found\n");
|
676 | 09b26c5e | bellard | } |
677 | 09b26c5e | bellard | |
678 | 1dce7c3c | bellard | /* compute with 96 bit intermediate result: (a*b)/c */
|
679 | 1dce7c3c | bellard | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
680 | 0824d6fc | bellard | { |
681 | 1dce7c3c | bellard | union {
|
682 | 1dce7c3c | bellard | uint64_t ll; |
683 | 1dce7c3c | bellard | struct {
|
684 | 1dce7c3c | bellard | #ifdef WORDS_BIGENDIAN
|
685 | 1dce7c3c | bellard | uint32_t high, low; |
686 | 1dce7c3c | bellard | #else
|
687 | 1dce7c3c | bellard | uint32_t low, high; |
688 | 3b46e624 | ths | #endif
|
689 | 1dce7c3c | bellard | } l; |
690 | 1dce7c3c | bellard | } u, res; |
691 | 1dce7c3c | bellard | uint64_t rl, rh; |
692 | 0824d6fc | bellard | |
693 | 1dce7c3c | bellard | u.ll = a; |
694 | 1dce7c3c | bellard | rl = (uint64_t)u.l.low * (uint64_t)b; |
695 | 1dce7c3c | bellard | rh = (uint64_t)u.l.high * (uint64_t)b; |
696 | 1dce7c3c | bellard | rh += (rl >> 32);
|
697 | 1dce7c3c | bellard | res.l.high = rh / c; |
698 | 1dce7c3c | bellard | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
699 | 1dce7c3c | bellard | return res.ll;
|
700 | 34865134 | bellard | } |
701 | 34865134 | bellard | |
702 | 1dce7c3c | bellard | /***********************************************************/
|
703 | 1dce7c3c | bellard | /* real time host monotonic timer */
|
704 | 34865134 | bellard | |
705 | 1dce7c3c | bellard | #define QEMU_TIMER_BASE 1000000000LL |
706 | 34865134 | bellard | |
707 | 1dce7c3c | bellard | #ifdef WIN32
|
708 | 0824d6fc | bellard | |
709 | 1dce7c3c | bellard | static int64_t clock_freq;
|
710 | 1115dde7 | bellard | |
711 | 1dce7c3c | bellard | static void init_get_clock(void) |
712 | 1115dde7 | bellard | { |
713 | a8e5ac33 | bellard | LARGE_INTEGER freq; |
714 | a8e5ac33 | bellard | int ret;
|
715 | 1dce7c3c | bellard | ret = QueryPerformanceFrequency(&freq); |
716 | 1dce7c3c | bellard | if (ret == 0) { |
717 | 1dce7c3c | bellard | fprintf(stderr, "Could not calibrate ticks\n");
|
718 | 1dce7c3c | bellard | exit(1);
|
719 | 1dce7c3c | bellard | } |
720 | 1dce7c3c | bellard | clock_freq = freq.QuadPart; |
721 | 1115dde7 | bellard | } |
722 | 1115dde7 | bellard | |
723 | 1dce7c3c | bellard | static int64_t get_clock(void) |
724 | b8076a74 | bellard | { |
725 | 1dce7c3c | bellard | LARGE_INTEGER ti; |
726 | 1dce7c3c | bellard | QueryPerformanceCounter(&ti); |
727 | 1dce7c3c | bellard | return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
|
728 | b8076a74 | bellard | } |
729 | b8076a74 | bellard | |
730 | 1dce7c3c | bellard | #else
|
731 | 90cb9493 | bellard | |
732 | 1dce7c3c | bellard | static int use_rt_clock; |
733 | 1dce7c3c | bellard | |
734 | 1dce7c3c | bellard | static void init_get_clock(void) |
735 | 90cb9493 | bellard | { |
736 | 1dce7c3c | bellard | use_rt_clock = 0;
|
737 | 1dce7c3c | bellard | #if defined(__linux__)
|
738 | 1dce7c3c | bellard | { |
739 | 1dce7c3c | bellard | struct timespec ts;
|
740 | 1dce7c3c | bellard | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
741 | 1dce7c3c | bellard | use_rt_clock = 1;
|
742 | 1dce7c3c | bellard | } |
743 | 1dce7c3c | bellard | } |
744 | 1dce7c3c | bellard | #endif
|
745 | 90cb9493 | bellard | } |
746 | 90cb9493 | bellard | |
747 | 1dce7c3c | bellard | static int64_t get_clock(void) |
748 | fdbb4691 | bellard | { |
749 | 1dce7c3c | bellard | #if defined(__linux__)
|
750 | 1dce7c3c | bellard | if (use_rt_clock) {
|
751 | 1dce7c3c | bellard | struct timespec ts;
|
752 | 1dce7c3c | bellard | clock_gettime(CLOCK_MONOTONIC, &ts); |
753 | 1dce7c3c | bellard | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
754 | 5fafdf24 | ths | } else
|
755 | fdbb4691 | bellard | #endif
|
756 | 1dce7c3c | bellard | { |
757 | 1dce7c3c | bellard | /* XXX: using gettimeofday leads to problems if the date
|
758 | 1dce7c3c | bellard | changes, so it should be avoided. */
|
759 | 1dce7c3c | bellard | struct timeval tv;
|
760 | 1dce7c3c | bellard | gettimeofday(&tv, NULL);
|
761 | 1dce7c3c | bellard | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
762 | 1dce7c3c | bellard | } |
763 | fdbb4691 | bellard | } |
764 | 34865134 | bellard | #endif
|
765 | 34865134 | bellard | |
766 | 2e70f6ef | pbrook | /* Return the virtual CPU time, based on the instruction counter. */
|
767 | 2e70f6ef | pbrook | static int64_t cpu_get_icount(void) |
768 | 2e70f6ef | pbrook | { |
769 | 2e70f6ef | pbrook | int64_t icount; |
770 | 2e70f6ef | pbrook | CPUState *env = cpu_single_env;; |
771 | 2e70f6ef | pbrook | icount = qemu_icount; |
772 | 2e70f6ef | pbrook | if (env) {
|
773 | 2e70f6ef | pbrook | if (!can_do_io(env))
|
774 | 2e70f6ef | pbrook | fprintf(stderr, "Bad clock read\n");
|
775 | 2e70f6ef | pbrook | icount -= (env->icount_decr.u16.low + env->icount_extra); |
776 | 2e70f6ef | pbrook | } |
777 | 2e70f6ef | pbrook | return qemu_icount_bias + (icount << icount_time_shift);
|
778 | 2e70f6ef | pbrook | } |
779 | 2e70f6ef | pbrook | |
780 | 1dce7c3c | bellard | /***********************************************************/
|
781 | 1dce7c3c | bellard | /* guest cycle counter */
|
782 | 1dce7c3c | bellard | |
783 | eade0f19 | bellard | static int64_t cpu_ticks_prev;
|
784 | 34865134 | bellard | static int64_t cpu_ticks_offset;
|
785 | 1dce7c3c | bellard | static int64_t cpu_clock_offset;
|
786 | 8a7ddc38 | bellard | static int cpu_ticks_enabled; |
787 | 34865134 | bellard | |
788 | 1dce7c3c | bellard | /* return the host CPU cycle counter and handle stop/restart */
|
789 | 1dce7c3c | bellard | int64_t cpu_get_ticks(void)
|
790 | 34865134 | bellard | { |
791 | 2e70f6ef | pbrook | if (use_icount) {
|
792 | 2e70f6ef | pbrook | return cpu_get_icount();
|
793 | 2e70f6ef | pbrook | } |
794 | 8a7ddc38 | bellard | if (!cpu_ticks_enabled) {
|
795 | 8a7ddc38 | bellard | return cpu_ticks_offset;
|
796 | 8a7ddc38 | bellard | } else {
|
797 | eade0f19 | bellard | int64_t ticks; |
798 | eade0f19 | bellard | ticks = cpu_get_real_ticks(); |
799 | eade0f19 | bellard | if (cpu_ticks_prev > ticks) {
|
800 | eade0f19 | bellard | /* Note: non increasing ticks may happen if the host uses
|
801 | eade0f19 | bellard | software suspend */
|
802 | eade0f19 | bellard | cpu_ticks_offset += cpu_ticks_prev - ticks; |
803 | eade0f19 | bellard | } |
804 | eade0f19 | bellard | cpu_ticks_prev = ticks; |
805 | eade0f19 | bellard | return ticks + cpu_ticks_offset;
|
806 | 8a7ddc38 | bellard | } |
807 | 34865134 | bellard | } |
808 | 34865134 | bellard | |
809 | 1dce7c3c | bellard | /* return the host CPU monotonic timer and handle stop/restart */
|
810 | 1dce7c3c | bellard | static int64_t cpu_get_clock(void) |
811 | 1dce7c3c | bellard | { |
812 | 1dce7c3c | bellard | int64_t ti; |
813 | 1dce7c3c | bellard | if (!cpu_ticks_enabled) {
|
814 | 1dce7c3c | bellard | return cpu_clock_offset;
|
815 | 1dce7c3c | bellard | } else {
|
816 | 1dce7c3c | bellard | ti = get_clock(); |
817 | 1dce7c3c | bellard | return ti + cpu_clock_offset;
|
818 | 1dce7c3c | bellard | } |
819 | 1dce7c3c | bellard | } |
820 | 1dce7c3c | bellard | |
821 | 34865134 | bellard | /* enable cpu_get_ticks() */
|
822 | 34865134 | bellard | void cpu_enable_ticks(void) |
823 | 34865134 | bellard | { |
824 | 8a7ddc38 | bellard | if (!cpu_ticks_enabled) {
|
825 | 8a7ddc38 | bellard | cpu_ticks_offset -= cpu_get_real_ticks(); |
826 | 1dce7c3c | bellard | cpu_clock_offset -= get_clock(); |
827 | 8a7ddc38 | bellard | cpu_ticks_enabled = 1;
|
828 | 8a7ddc38 | bellard | } |
829 | 34865134 | bellard | } |
830 | 34865134 | bellard | |
831 | 34865134 | bellard | /* disable cpu_get_ticks() : the clock is stopped. You must not call
|
832 | 34865134 | bellard | cpu_get_ticks() after that. */
|
833 | 34865134 | bellard | void cpu_disable_ticks(void) |
834 | 34865134 | bellard | { |
835 | 8a7ddc38 | bellard | if (cpu_ticks_enabled) {
|
836 | 8a7ddc38 | bellard | cpu_ticks_offset = cpu_get_ticks(); |
837 | 1dce7c3c | bellard | cpu_clock_offset = cpu_get_clock(); |
838 | 8a7ddc38 | bellard | cpu_ticks_enabled = 0;
|
839 | 8a7ddc38 | bellard | } |
840 | 34865134 | bellard | } |
841 | 34865134 | bellard | |
842 | 1dce7c3c | bellard | /***********************************************************/
|
843 | 1dce7c3c | bellard | /* timers */
|
844 | 5fafdf24 | ths | |
845 | 8a7ddc38 | bellard | #define QEMU_TIMER_REALTIME 0 |
846 | 8a7ddc38 | bellard | #define QEMU_TIMER_VIRTUAL 1 |
847 | 8a7ddc38 | bellard | |
848 | 8a7ddc38 | bellard | struct QEMUClock {
|
849 | 8a7ddc38 | bellard | int type;
|
850 | 8a7ddc38 | bellard | /* XXX: add frequency */
|
851 | 8a7ddc38 | bellard | }; |
852 | 8a7ddc38 | bellard | |
853 | 8a7ddc38 | bellard | struct QEMUTimer {
|
854 | 8a7ddc38 | bellard | QEMUClock *clock; |
855 | 8a7ddc38 | bellard | int64_t expire_time; |
856 | 8a7ddc38 | bellard | QEMUTimerCB *cb; |
857 | 8a7ddc38 | bellard | void *opaque;
|
858 | 8a7ddc38 | bellard | struct QEMUTimer *next;
|
859 | 8a7ddc38 | bellard | }; |
860 | 8a7ddc38 | bellard | |
861 | c8994013 | ths | struct qemu_alarm_timer {
|
862 | c8994013 | ths | char const *name; |
863 | efe75411 | ths | unsigned int flags; |
864 | c8994013 | ths | |
865 | c8994013 | ths | int (*start)(struct qemu_alarm_timer *t); |
866 | c8994013 | ths | void (*stop)(struct qemu_alarm_timer *t); |
867 | efe75411 | ths | void (*rearm)(struct qemu_alarm_timer *t); |
868 | c8994013 | ths | void *priv;
|
869 | c8994013 | ths | }; |
870 | c8994013 | ths | |
871 | efe75411 | ths | #define ALARM_FLAG_DYNTICKS 0x1 |
872 | d5d08334 | balrog | #define ALARM_FLAG_EXPIRED 0x2 |
873 | efe75411 | ths | |
874 | efe75411 | ths | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
875 | efe75411 | ths | { |
876 | efe75411 | ths | return t->flags & ALARM_FLAG_DYNTICKS;
|
877 | efe75411 | ths | } |
878 | efe75411 | ths | |
879 | efe75411 | ths | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
880 | efe75411 | ths | { |
881 | efe75411 | ths | if (!alarm_has_dynticks(t))
|
882 | efe75411 | ths | return;
|
883 | efe75411 | ths | |
884 | efe75411 | ths | t->rearm(t); |
885 | efe75411 | ths | } |
886 | efe75411 | ths | |
887 | efe75411 | ths | /* TODO: MIN_TIMER_REARM_US should be optimized */
|
888 | efe75411 | ths | #define MIN_TIMER_REARM_US 250 |
889 | efe75411 | ths | |
890 | c8994013 | ths | static struct qemu_alarm_timer *alarm_timer; |
891 | 8a7ddc38 | bellard | |
892 | 40c3bac3 | bellard | #ifdef _WIN32
|
893 | c8994013 | ths | |
894 | c8994013 | ths | struct qemu_alarm_win32 {
|
895 | c8994013 | ths | MMRESULT timerId; |
896 | c8994013 | ths | HANDLE host_alarm; |
897 | c8994013 | ths | unsigned int period; |
898 | c8994013 | ths | } alarm_win32_data = {0, NULL, -1}; |
899 | c8994013 | ths | |
900 | c8994013 | ths | static int win32_start_timer(struct qemu_alarm_timer *t); |
901 | c8994013 | ths | static void win32_stop_timer(struct qemu_alarm_timer *t); |
902 | efe75411 | ths | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
903 | c8994013 | ths | |
904 | 40c3bac3 | bellard | #else
|
905 | c8994013 | ths | |
906 | c8994013 | ths | static int unix_start_timer(struct qemu_alarm_timer *t); |
907 | c8994013 | ths | static void unix_stop_timer(struct qemu_alarm_timer *t); |
908 | c8994013 | ths | |
909 | 231c6586 | ths | #ifdef __linux__
|
910 | 231c6586 | ths | |
911 | efe75411 | ths | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
912 | efe75411 | ths | static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
913 | efe75411 | ths | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
914 | efe75411 | ths | |
915 | c40ec5a9 | ths | static int hpet_start_timer(struct qemu_alarm_timer *t); |
916 | c40ec5a9 | ths | static void hpet_stop_timer(struct qemu_alarm_timer *t); |
917 | c40ec5a9 | ths | |
918 | c8994013 | ths | static int rtc_start_timer(struct qemu_alarm_timer *t); |
919 | c8994013 | ths | static void rtc_stop_timer(struct qemu_alarm_timer *t); |
920 | c8994013 | ths | |
921 | efe75411 | ths | #endif /* __linux__ */ |
922 | 8a7ddc38 | bellard | |
923 | c8994013 | ths | #endif /* _WIN32 */ |
924 | c8994013 | ths | |
925 | 2e70f6ef | pbrook | /* Correlation between real and virtual time is always going to be
|
926 | bf20dc07 | ths | fairly approximate, so ignore small variation.
|
927 | 2e70f6ef | pbrook | When the guest is idle real and virtual time will be aligned in
|
928 | 2e70f6ef | pbrook | the IO wait loop. */
|
929 | 2e70f6ef | pbrook | #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10) |
930 | 2e70f6ef | pbrook | |
931 | 2e70f6ef | pbrook | static void icount_adjust(void) |
932 | 2e70f6ef | pbrook | { |
933 | 2e70f6ef | pbrook | int64_t cur_time; |
934 | 2e70f6ef | pbrook | int64_t cur_icount; |
935 | 2e70f6ef | pbrook | int64_t delta; |
936 | 2e70f6ef | pbrook | static int64_t last_delta;
|
937 | 2e70f6ef | pbrook | /* If the VM is not running, then do nothing. */
|
938 | 2e70f6ef | pbrook | if (!vm_running)
|
939 | 2e70f6ef | pbrook | return;
|
940 | 2e70f6ef | pbrook | |
941 | 2e70f6ef | pbrook | cur_time = cpu_get_clock(); |
942 | 2e70f6ef | pbrook | cur_icount = qemu_get_clock(vm_clock); |
943 | 2e70f6ef | pbrook | delta = cur_icount - cur_time; |
944 | 2e70f6ef | pbrook | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
|
945 | 2e70f6ef | pbrook | if (delta > 0 |
946 | 2e70f6ef | pbrook | && last_delta + ICOUNT_WOBBLE < delta * 2
|
947 | 2e70f6ef | pbrook | && icount_time_shift > 0) {
|
948 | 2e70f6ef | pbrook | /* The guest is getting too far ahead. Slow time down. */
|
949 | 2e70f6ef | pbrook | icount_time_shift--; |
950 | 2e70f6ef | pbrook | } |
951 | 2e70f6ef | pbrook | if (delta < 0 |
952 | 2e70f6ef | pbrook | && last_delta - ICOUNT_WOBBLE > delta * 2
|
953 | 2e70f6ef | pbrook | && icount_time_shift < MAX_ICOUNT_SHIFT) { |
954 | 2e70f6ef | pbrook | /* The guest is getting too far behind. Speed time up. */
|
955 | 2e70f6ef | pbrook | icount_time_shift++; |
956 | 2e70f6ef | pbrook | } |
957 | 2e70f6ef | pbrook | last_delta = delta; |
958 | 2e70f6ef | pbrook | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
959 | 2e70f6ef | pbrook | } |
960 | 2e70f6ef | pbrook | |
961 | 2e70f6ef | pbrook | static void icount_adjust_rt(void * opaque) |
962 | 2e70f6ef | pbrook | { |
963 | 2e70f6ef | pbrook | qemu_mod_timer(icount_rt_timer, |
964 | 2e70f6ef | pbrook | qemu_get_clock(rt_clock) + 1000);
|
965 | 2e70f6ef | pbrook | icount_adjust(); |
966 | 2e70f6ef | pbrook | } |
967 | 2e70f6ef | pbrook | |
968 | 2e70f6ef | pbrook | static void icount_adjust_vm(void * opaque) |
969 | 2e70f6ef | pbrook | { |
970 | 2e70f6ef | pbrook | qemu_mod_timer(icount_vm_timer, |
971 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
|
972 | 2e70f6ef | pbrook | icount_adjust(); |
973 | 2e70f6ef | pbrook | } |
974 | 2e70f6ef | pbrook | |
975 | 2e70f6ef | pbrook | static void init_icount_adjust(void) |
976 | 2e70f6ef | pbrook | { |
977 | 2e70f6ef | pbrook | /* Have both realtime and virtual time triggers for speed adjustment.
|
978 | 2e70f6ef | pbrook | The realtime trigger catches emulated time passing too slowly,
|
979 | 2e70f6ef | pbrook | the virtual time trigger catches emulated time passing too fast.
|
980 | 2e70f6ef | pbrook | Realtime triggers occur even when idle, so use them less frequently
|
981 | 2e70f6ef | pbrook | than VM triggers. */
|
982 | 2e70f6ef | pbrook | icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
|
983 | 2e70f6ef | pbrook | qemu_mod_timer(icount_rt_timer, |
984 | 2e70f6ef | pbrook | qemu_get_clock(rt_clock) + 1000);
|
985 | 2e70f6ef | pbrook | icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
|
986 | 2e70f6ef | pbrook | qemu_mod_timer(icount_vm_timer, |
987 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
|
988 | 2e70f6ef | pbrook | } |
989 | 2e70f6ef | pbrook | |
990 | c8994013 | ths | static struct qemu_alarm_timer alarm_timers[] = { |
991 | efe75411 | ths | #ifndef _WIN32
|
992 | 231c6586 | ths | #ifdef __linux__
|
993 | efe75411 | ths | {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
|
994 | efe75411 | ths | dynticks_stop_timer, dynticks_rearm_timer, NULL},
|
995 | c40ec5a9 | ths | /* HPET - if available - is preferred */
|
996 | efe75411 | ths | {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
997 | c40ec5a9 | ths | /* ...otherwise try RTC */
|
998 | efe75411 | ths | {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
999 | c8994013 | ths | #endif
|
1000 | efe75411 | ths | {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
1001 | c8994013 | ths | #else
|
1002 | efe75411 | ths | {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
|
1003 | efe75411 | ths | win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
1004 | efe75411 | ths | {"win32", 0, win32_start_timer, |
1005 | efe75411 | ths | win32_stop_timer, NULL, &alarm_win32_data},
|
1006 | c8994013 | ths | #endif
|
1007 | c8994013 | ths | {NULL, }
|
1008 | c8994013 | ths | }; |
1009 | c8994013 | ths | |
1010 | 3f47aa8c | blueswir1 | static void show_available_alarms(void) |
1011 | f3dcfada | ths | { |
1012 | f3dcfada | ths | int i;
|
1013 | f3dcfada | ths | |
1014 | f3dcfada | ths | printf("Available alarm timers, in order of precedence:\n");
|
1015 | f3dcfada | ths | for (i = 0; alarm_timers[i].name; i++) |
1016 | f3dcfada | ths | printf("%s\n", alarm_timers[i].name);
|
1017 | f3dcfada | ths | } |
1018 | f3dcfada | ths | |
1019 | f3dcfada | ths | static void configure_alarms(char const *opt) |
1020 | f3dcfada | ths | { |
1021 | f3dcfada | ths | int i;
|
1022 | f3dcfada | ths | int cur = 0; |
1023 | f3dcfada | ths | int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1; |
1024 | f3dcfada | ths | char *arg;
|
1025 | f3dcfada | ths | char *name;
|
1026 | 2e70f6ef | pbrook | struct qemu_alarm_timer tmp;
|
1027 | f3dcfada | ths | |
1028 | 3adda04c | aurel32 | if (!strcmp(opt, "?")) { |
1029 | f3dcfada | ths | show_available_alarms(); |
1030 | f3dcfada | ths | exit(0);
|
1031 | f3dcfada | ths | } |
1032 | f3dcfada | ths | |
1033 | f3dcfada | ths | arg = strdup(opt); |
1034 | f3dcfada | ths | |
1035 | f3dcfada | ths | /* Reorder the array */
|
1036 | f3dcfada | ths | name = strtok(arg, ",");
|
1037 | f3dcfada | ths | while (name) {
|
1038 | e2b577e5 | balrog | for (i = 0; i < count && alarm_timers[i].name; i++) { |
1039 | f3dcfada | ths | if (!strcmp(alarm_timers[i].name, name))
|
1040 | f3dcfada | ths | break;
|
1041 | f3dcfada | ths | } |
1042 | f3dcfada | ths | |
1043 | f3dcfada | ths | if (i == count) {
|
1044 | f3dcfada | ths | fprintf(stderr, "Unknown clock %s\n", name);
|
1045 | f3dcfada | ths | goto next;
|
1046 | f3dcfada | ths | } |
1047 | f3dcfada | ths | |
1048 | f3dcfada | ths | if (i < cur)
|
1049 | f3dcfada | ths | /* Ignore */
|
1050 | f3dcfada | ths | goto next;
|
1051 | f3dcfada | ths | |
1052 | f3dcfada | ths | /* Swap */
|
1053 | f3dcfada | ths | tmp = alarm_timers[i]; |
1054 | f3dcfada | ths | alarm_timers[i] = alarm_timers[cur]; |
1055 | f3dcfada | ths | alarm_timers[cur] = tmp; |
1056 | f3dcfada | ths | |
1057 | f3dcfada | ths | cur++; |
1058 | f3dcfada | ths | next:
|
1059 | f3dcfada | ths | name = strtok(NULL, ","); |
1060 | f3dcfada | ths | } |
1061 | f3dcfada | ths | |
1062 | f3dcfada | ths | free(arg); |
1063 | f3dcfada | ths | |
1064 | f3dcfada | ths | if (cur) {
|
1065 | 2e70f6ef | pbrook | /* Disable remaining timers */
|
1066 | f3dcfada | ths | for (i = cur; i < count; i++)
|
1067 | f3dcfada | ths | alarm_timers[i].name = NULL;
|
1068 | 3adda04c | aurel32 | } else {
|
1069 | 3adda04c | aurel32 | show_available_alarms(); |
1070 | 3adda04c | aurel32 | exit(1);
|
1071 | f3dcfada | ths | } |
1072 | f3dcfada | ths | } |
1073 | f3dcfada | ths | |
1074 | c8994013 | ths | QEMUClock *rt_clock; |
1075 | c8994013 | ths | QEMUClock *vm_clock; |
1076 | c8994013 | ths | |
1077 | c8994013 | ths | static QEMUTimer *active_timers[2]; |
1078 | c8994013 | ths | |
1079 | 9596ebb7 | pbrook | static QEMUClock *qemu_new_clock(int type) |
1080 | 8a7ddc38 | bellard | { |
1081 | 8a7ddc38 | bellard | QEMUClock *clock; |
1082 | 8a7ddc38 | bellard | clock = qemu_mallocz(sizeof(QEMUClock));
|
1083 | 8a7ddc38 | bellard | if (!clock)
|
1084 | 8a7ddc38 | bellard | return NULL; |
1085 | 8a7ddc38 | bellard | clock->type = type; |
1086 | 8a7ddc38 | bellard | return clock;
|
1087 | 8a7ddc38 | bellard | } |
1088 | 8a7ddc38 | bellard | |
1089 | 8a7ddc38 | bellard | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
|
1090 | 8a7ddc38 | bellard | { |
1091 | 8a7ddc38 | bellard | QEMUTimer *ts; |
1092 | 8a7ddc38 | bellard | |
1093 | 8a7ddc38 | bellard | ts = qemu_mallocz(sizeof(QEMUTimer));
|
1094 | 8a7ddc38 | bellard | ts->clock = clock; |
1095 | 8a7ddc38 | bellard | ts->cb = cb; |
1096 | 8a7ddc38 | bellard | ts->opaque = opaque; |
1097 | 8a7ddc38 | bellard | return ts;
|
1098 | 8a7ddc38 | bellard | } |
1099 | 8a7ddc38 | bellard | |
1100 | 8a7ddc38 | bellard | void qemu_free_timer(QEMUTimer *ts)
|
1101 | 8a7ddc38 | bellard | { |
1102 | 8a7ddc38 | bellard | qemu_free(ts); |
1103 | 8a7ddc38 | bellard | } |
1104 | 8a7ddc38 | bellard | |
1105 | 8a7ddc38 | bellard | /* stop a timer, but do not dealloc it */
|
1106 | 8a7ddc38 | bellard | void qemu_del_timer(QEMUTimer *ts)
|
1107 | 8a7ddc38 | bellard | { |
1108 | 8a7ddc38 | bellard | QEMUTimer **pt, *t; |
1109 | 8a7ddc38 | bellard | |
1110 | 8a7ddc38 | bellard | /* NOTE: this code must be signal safe because
|
1111 | 8a7ddc38 | bellard | qemu_timer_expired() can be called from a signal. */
|
1112 | 8a7ddc38 | bellard | pt = &active_timers[ts->clock->type]; |
1113 | 8a7ddc38 | bellard | for(;;) {
|
1114 | 8a7ddc38 | bellard | t = *pt; |
1115 | 8a7ddc38 | bellard | if (!t)
|
1116 | 8a7ddc38 | bellard | break;
|
1117 | 8a7ddc38 | bellard | if (t == ts) {
|
1118 | 8a7ddc38 | bellard | *pt = t->next; |
1119 | 8a7ddc38 | bellard | break;
|
1120 | 8a7ddc38 | bellard | } |
1121 | 8a7ddc38 | bellard | pt = &t->next; |
1122 | 8a7ddc38 | bellard | } |
1123 | 8a7ddc38 | bellard | } |
1124 | 8a7ddc38 | bellard | |
1125 | 8a7ddc38 | bellard | /* modify the current timer so that it will be fired when current_time
|
1126 | 8a7ddc38 | bellard | >= expire_time. The corresponding callback will be called. */
|
1127 | 8a7ddc38 | bellard | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
|
1128 | 8a7ddc38 | bellard | { |
1129 | 8a7ddc38 | bellard | QEMUTimer **pt, *t; |
1130 | 8a7ddc38 | bellard | |
1131 | 8a7ddc38 | bellard | qemu_del_timer(ts); |
1132 | 8a7ddc38 | bellard | |
1133 | 8a7ddc38 | bellard | /* add the timer in the sorted list */
|
1134 | 8a7ddc38 | bellard | /* NOTE: this code must be signal safe because
|
1135 | 8a7ddc38 | bellard | qemu_timer_expired() can be called from a signal. */
|
1136 | 8a7ddc38 | bellard | pt = &active_timers[ts->clock->type]; |
1137 | 8a7ddc38 | bellard | for(;;) {
|
1138 | 8a7ddc38 | bellard | t = *pt; |
1139 | 8a7ddc38 | bellard | if (!t)
|
1140 | 8a7ddc38 | bellard | break;
|
1141 | 5fafdf24 | ths | if (t->expire_time > expire_time)
|
1142 | 8a7ddc38 | bellard | break;
|
1143 | 8a7ddc38 | bellard | pt = &t->next; |
1144 | 8a7ddc38 | bellard | } |
1145 | 8a7ddc38 | bellard | ts->expire_time = expire_time; |
1146 | 8a7ddc38 | bellard | ts->next = *pt; |
1147 | 8a7ddc38 | bellard | *pt = ts; |
1148 | d5d08334 | balrog | |
1149 | d5d08334 | balrog | /* Rearm if necessary */
|
1150 | 2e70f6ef | pbrook | if (pt == &active_timers[ts->clock->type]) {
|
1151 | 2e70f6ef | pbrook | if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { |
1152 | 2e70f6ef | pbrook | qemu_rearm_alarm_timer(alarm_timer); |
1153 | 2e70f6ef | pbrook | } |
1154 | 2e70f6ef | pbrook | /* Interrupt execution to force deadline recalculation. */
|
1155 | 2e70f6ef | pbrook | if (use_icount && cpu_single_env) {
|
1156 | 2e70f6ef | pbrook | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
1157 | 2e70f6ef | pbrook | } |
1158 | 2e70f6ef | pbrook | } |
1159 | 8a7ddc38 | bellard | } |
1160 | 8a7ddc38 | bellard | |
1161 | 8a7ddc38 | bellard | int qemu_timer_pending(QEMUTimer *ts)
|
1162 | 8a7ddc38 | bellard | { |
1163 | 8a7ddc38 | bellard | QEMUTimer *t; |
1164 | 8a7ddc38 | bellard | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
1165 | 8a7ddc38 | bellard | if (t == ts)
|
1166 | 8a7ddc38 | bellard | return 1; |
1167 | 8a7ddc38 | bellard | } |
1168 | 8a7ddc38 | bellard | return 0; |
1169 | 8a7ddc38 | bellard | } |
1170 | 8a7ddc38 | bellard | |
1171 | 8a7ddc38 | bellard | static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
1172 | 8a7ddc38 | bellard | { |
1173 | 8a7ddc38 | bellard | if (!timer_head)
|
1174 | 8a7ddc38 | bellard | return 0; |
1175 | 8a7ddc38 | bellard | return (timer_head->expire_time <= current_time);
|
1176 | 8a7ddc38 | bellard | } |
1177 | 8a7ddc38 | bellard | |
1178 | 8a7ddc38 | bellard | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
1179 | 8a7ddc38 | bellard | { |
1180 | 8a7ddc38 | bellard | QEMUTimer *ts; |
1181 | 3b46e624 | ths | |
1182 | 8a7ddc38 | bellard | for(;;) {
|
1183 | 8a7ddc38 | bellard | ts = *ptimer_head; |
1184 | e95c8d51 | bellard | if (!ts || ts->expire_time > current_time)
|
1185 | 8a7ddc38 | bellard | break;
|
1186 | 8a7ddc38 | bellard | /* remove timer from the list before calling the callback */
|
1187 | 8a7ddc38 | bellard | *ptimer_head = ts->next; |
1188 | 8a7ddc38 | bellard | ts->next = NULL;
|
1189 | 3b46e624 | ths | |
1190 | 8a7ddc38 | bellard | /* run the callback (the timer list can be modified) */
|
1191 | 8a7ddc38 | bellard | ts->cb(ts->opaque); |
1192 | 8a7ddc38 | bellard | } |
1193 | 8a7ddc38 | bellard | } |
1194 | 8a7ddc38 | bellard | |
1195 | 8a7ddc38 | bellard | int64_t qemu_get_clock(QEMUClock *clock) |
1196 | 8a7ddc38 | bellard | { |
1197 | 8a7ddc38 | bellard | switch(clock->type) {
|
1198 | 8a7ddc38 | bellard | case QEMU_TIMER_REALTIME:
|
1199 | 1dce7c3c | bellard | return get_clock() / 1000000; |
1200 | 8a7ddc38 | bellard | default:
|
1201 | 8a7ddc38 | bellard | case QEMU_TIMER_VIRTUAL:
|
1202 | 2e70f6ef | pbrook | if (use_icount) {
|
1203 | 2e70f6ef | pbrook | return cpu_get_icount();
|
1204 | 2e70f6ef | pbrook | } else {
|
1205 | 2e70f6ef | pbrook | return cpu_get_clock();
|
1206 | 2e70f6ef | pbrook | } |
1207 | 8a7ddc38 | bellard | } |
1208 | 8a7ddc38 | bellard | } |
1209 | 8a7ddc38 | bellard | |
1210 | 1dce7c3c | bellard | static void init_timers(void) |
1211 | 1dce7c3c | bellard | { |
1212 | 1dce7c3c | bellard | init_get_clock(); |
1213 | 1dce7c3c | bellard | ticks_per_sec = QEMU_TIMER_BASE; |
1214 | 1dce7c3c | bellard | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
1215 | 1dce7c3c | bellard | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
1216 | 1dce7c3c | bellard | } |
1217 | 1dce7c3c | bellard | |
1218 | 8a7ddc38 | bellard | /* save a timer */
|
1219 | 8a7ddc38 | bellard | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
|
1220 | 8a7ddc38 | bellard | { |
1221 | 8a7ddc38 | bellard | uint64_t expire_time; |
1222 | 8a7ddc38 | bellard | |
1223 | 8a7ddc38 | bellard | if (qemu_timer_pending(ts)) {
|
1224 | 8a7ddc38 | bellard | expire_time = ts->expire_time; |
1225 | 8a7ddc38 | bellard | } else {
|
1226 | 8a7ddc38 | bellard | expire_time = -1;
|
1227 | 8a7ddc38 | bellard | } |
1228 | 8a7ddc38 | bellard | qemu_put_be64(f, expire_time); |
1229 | 8a7ddc38 | bellard | } |
1230 | 8a7ddc38 | bellard | |
1231 | 8a7ddc38 | bellard | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
|
1232 | 8a7ddc38 | bellard | { |
1233 | 8a7ddc38 | bellard | uint64_t expire_time; |
1234 | 8a7ddc38 | bellard | |
1235 | 8a7ddc38 | bellard | expire_time = qemu_get_be64(f); |
1236 | 8a7ddc38 | bellard | if (expire_time != -1) { |
1237 | 8a7ddc38 | bellard | qemu_mod_timer(ts, expire_time); |
1238 | 8a7ddc38 | bellard | } else {
|
1239 | 8a7ddc38 | bellard | qemu_del_timer(ts); |
1240 | 8a7ddc38 | bellard | } |
1241 | 8a7ddc38 | bellard | } |
1242 | 8a7ddc38 | bellard | |
1243 | 8a7ddc38 | bellard | static void timer_save(QEMUFile *f, void *opaque) |
1244 | 8a7ddc38 | bellard | { |
1245 | 8a7ddc38 | bellard | if (cpu_ticks_enabled) {
|
1246 | 8a7ddc38 | bellard | hw_error("cannot save state if virtual timers are running");
|
1247 | 8a7ddc38 | bellard | } |
1248 | bee8d684 | ths | qemu_put_be64(f, cpu_ticks_offset); |
1249 | bee8d684 | ths | qemu_put_be64(f, ticks_per_sec); |
1250 | bee8d684 | ths | qemu_put_be64(f, cpu_clock_offset); |
1251 | 8a7ddc38 | bellard | } |
1252 | 8a7ddc38 | bellard | |
1253 | 8a7ddc38 | bellard | static int timer_load(QEMUFile *f, void *opaque, int version_id) |
1254 | 8a7ddc38 | bellard | { |
1255 | c88676f8 | bellard | if (version_id != 1 && version_id != 2) |
1256 | 8a7ddc38 | bellard | return -EINVAL;
|
1257 | 8a7ddc38 | bellard | if (cpu_ticks_enabled) {
|
1258 | 8a7ddc38 | bellard | return -EINVAL;
|
1259 | 8a7ddc38 | bellard | } |
1260 | bee8d684 | ths | cpu_ticks_offset=qemu_get_be64(f); |
1261 | bee8d684 | ths | ticks_per_sec=qemu_get_be64(f); |
1262 | c88676f8 | bellard | if (version_id == 2) { |
1263 | bee8d684 | ths | cpu_clock_offset=qemu_get_be64(f); |
1264 | c88676f8 | bellard | } |
1265 | 8a7ddc38 | bellard | return 0; |
1266 | 8a7ddc38 | bellard | } |
1267 | 8a7ddc38 | bellard | |
1268 | 67b915a5 | bellard | #ifdef _WIN32
|
1269 | 5fafdf24 | ths | void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
|
1270 | 67b915a5 | bellard | DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
1271 | 67b915a5 | bellard | #else
|
1272 | 8a7ddc38 | bellard | static void host_alarm_handler(int host_signum) |
1273 | 67b915a5 | bellard | #endif
|
1274 | 8a7ddc38 | bellard | { |
1275 | 02ba45c5 | bellard | #if 0
|
1276 | 02ba45c5 | bellard | #define DISP_FREQ 1000
|
1277 | 02ba45c5 | bellard | {
|
1278 | 02ba45c5 | bellard | static int64_t delta_min = INT64_MAX;
|
1279 | 02ba45c5 | bellard | static int64_t delta_max, delta_cum, last_clock, delta, ti;
|
1280 | 02ba45c5 | bellard | static int count;
|
1281 | 02ba45c5 | bellard | ti = qemu_get_clock(vm_clock);
|
1282 | 02ba45c5 | bellard | if (last_clock != 0) {
|
1283 | 02ba45c5 | bellard | delta = ti - last_clock;
|
1284 | 02ba45c5 | bellard | if (delta < delta_min)
|
1285 | 02ba45c5 | bellard | delta_min = delta;
|
1286 | 02ba45c5 | bellard | if (delta > delta_max)
|
1287 | 02ba45c5 | bellard | delta_max = delta;
|
1288 | 02ba45c5 | bellard | delta_cum += delta;
|
1289 | 02ba45c5 | bellard | if (++count == DISP_FREQ) {
|
1290 | 26a76461 | bellard | printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
|
1291 | 02ba45c5 | bellard | muldiv64(delta_min, 1000000, ticks_per_sec),
|
1292 | 02ba45c5 | bellard | muldiv64(delta_max, 1000000, ticks_per_sec),
|
1293 | 02ba45c5 | bellard | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
|
1294 | 02ba45c5 | bellard | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
|
1295 | 02ba45c5 | bellard | count = 0;
|
1296 | 02ba45c5 | bellard | delta_min = INT64_MAX;
|
1297 | 02ba45c5 | bellard | delta_max = 0;
|
1298 | 02ba45c5 | bellard | delta_cum = 0;
|
1299 | 02ba45c5 | bellard | }
|
1300 | 02ba45c5 | bellard | }
|
1301 | 02ba45c5 | bellard | last_clock = ti;
|
1302 | 02ba45c5 | bellard | }
|
1303 | 02ba45c5 | bellard | #endif
|
1304 | efe75411 | ths | if (alarm_has_dynticks(alarm_timer) ||
|
1305 | 2e70f6ef | pbrook | (!use_icount && |
1306 | 2e70f6ef | pbrook | qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
1307 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock))) || |
1308 | 8a7ddc38 | bellard | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
1309 | 8a7ddc38 | bellard | qemu_get_clock(rt_clock))) { |
1310 | 06d9f2f7 | bellard | #ifdef _WIN32
|
1311 | c8994013 | ths | struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv; |
1312 | c8994013 | ths | SetEvent(data->host_alarm); |
1313 | 06d9f2f7 | bellard | #endif
|
1314 | ee5605e5 | balrog | CPUState *env = next_cpu; |
1315 | ee5605e5 | balrog | |
1316 | d5d08334 | balrog | alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
1317 | d5d08334 | balrog | |
1318 | 4f8eb8da | balrog | if (env) {
|
1319 | 4f8eb8da | balrog | /* stop the currently executing cpu because a timer occured */
|
1320 | 4f8eb8da | balrog | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
1321 | a332e112 | bellard | #ifdef USE_KQEMU
|
1322 | 4f8eb8da | balrog | if (env->kqemu_enabled) {
|
1323 | 4f8eb8da | balrog | kqemu_cpu_interrupt(env); |
1324 | 4f8eb8da | balrog | } |
1325 | ee5605e5 | balrog | #endif
|
1326 | 4f8eb8da | balrog | } |
1327 | ee5605e5 | balrog | event_pending = 1;
|
1328 | 8a7ddc38 | bellard | } |
1329 | 8a7ddc38 | bellard | } |
1330 | 8a7ddc38 | bellard | |
1331 | 2e70f6ef | pbrook | static int64_t qemu_next_deadline(void) |
1332 | efe75411 | ths | { |
1333 | 2e70f6ef | pbrook | int64_t delta; |
1334 | efe75411 | ths | |
1335 | efe75411 | ths | if (active_timers[QEMU_TIMER_VIRTUAL]) {
|
1336 | 2e70f6ef | pbrook | delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time - |
1337 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock); |
1338 | 2e70f6ef | pbrook | } else {
|
1339 | 2e70f6ef | pbrook | /* To avoid problems with overflow limit this to 2^32. */
|
1340 | 2e70f6ef | pbrook | delta = INT32_MAX; |
1341 | efe75411 | ths | } |
1342 | efe75411 | ths | |
1343 | 2e70f6ef | pbrook | if (delta < 0) |
1344 | 2e70f6ef | pbrook | delta = 0;
|
1345 | efe75411 | ths | |
1346 | 2e70f6ef | pbrook | return delta;
|
1347 | 2e70f6ef | pbrook | } |
1348 | 2e70f6ef | pbrook | |
1349 | 2e70f6ef | pbrook | static uint64_t qemu_next_deadline_dyntick(void) |
1350 | 2e70f6ef | pbrook | { |
1351 | 2e70f6ef | pbrook | int64_t delta; |
1352 | 2e70f6ef | pbrook | int64_t rtdelta; |
1353 | 2e70f6ef | pbrook | |
1354 | 2e70f6ef | pbrook | if (use_icount)
|
1355 | 2e70f6ef | pbrook | delta = INT32_MAX; |
1356 | 2e70f6ef | pbrook | else
|
1357 | 2e70f6ef | pbrook | delta = (qemu_next_deadline() + 999) / 1000; |
1358 | 2e70f6ef | pbrook | |
1359 | 2e70f6ef | pbrook | if (active_timers[QEMU_TIMER_REALTIME]) {
|
1360 | 2e70f6ef | pbrook | rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time - |
1361 | 2e70f6ef | pbrook | qemu_get_clock(rt_clock))*1000;
|
1362 | 2e70f6ef | pbrook | if (rtdelta < delta)
|
1363 | 2e70f6ef | pbrook | delta = rtdelta; |
1364 | 2e70f6ef | pbrook | } |
1365 | 2e70f6ef | pbrook | |
1366 | 2e70f6ef | pbrook | if (delta < MIN_TIMER_REARM_US)
|
1367 | 2e70f6ef | pbrook | delta = MIN_TIMER_REARM_US; |
1368 | 2e70f6ef | pbrook | |
1369 | 2e70f6ef | pbrook | return delta;
|
1370 | efe75411 | ths | } |
1371 | efe75411 | ths | |
1372 | fd872598 | bellard | #ifndef _WIN32
|
1373 | fd872598 | bellard | |
1374 | 829309c7 | bellard | #if defined(__linux__)
|
1375 | 829309c7 | bellard | |
1376 | fd872598 | bellard | #define RTC_FREQ 1024 |
1377 | fd872598 | bellard | |
1378 | c8994013 | ths | static void enable_sigio_timer(int fd) |
1379 | c8994013 | ths | { |
1380 | c8994013 | ths | struct sigaction act;
|
1381 | c8994013 | ths | |
1382 | c8994013 | ths | /* timer signal */
|
1383 | c8994013 | ths | sigfillset(&act.sa_mask); |
1384 | c8994013 | ths | act.sa_flags = 0;
|
1385 | c8994013 | ths | act.sa_handler = host_alarm_handler; |
1386 | c8994013 | ths | |
1387 | c8994013 | ths | sigaction(SIGIO, &act, NULL);
|
1388 | c8994013 | ths | fcntl(fd, F_SETFL, O_ASYNC); |
1389 | c8994013 | ths | fcntl(fd, F_SETOWN, getpid()); |
1390 | c8994013 | ths | } |
1391 | 829309c7 | bellard | |
1392 | c40ec5a9 | ths | static int hpet_start_timer(struct qemu_alarm_timer *t) |
1393 | c40ec5a9 | ths | { |
1394 | c40ec5a9 | ths | struct hpet_info info;
|
1395 | c40ec5a9 | ths | int r, fd;
|
1396 | c40ec5a9 | ths | |
1397 | c40ec5a9 | ths | fd = open("/dev/hpet", O_RDONLY);
|
1398 | c40ec5a9 | ths | if (fd < 0) |
1399 | c40ec5a9 | ths | return -1; |
1400 | c40ec5a9 | ths | |
1401 | c40ec5a9 | ths | /* Set frequency */
|
1402 | c40ec5a9 | ths | r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
1403 | c40ec5a9 | ths | if (r < 0) { |
1404 | c40ec5a9 | ths | fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
|
1405 | c40ec5a9 | ths | "error, but for better emulation accuracy type:\n"
|
1406 | c40ec5a9 | ths | "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
|
1407 | c40ec5a9 | ths | goto fail;
|
1408 | c40ec5a9 | ths | } |
1409 | c40ec5a9 | ths | |
1410 | c40ec5a9 | ths | /* Check capabilities */
|
1411 | c40ec5a9 | ths | r = ioctl(fd, HPET_INFO, &info); |
1412 | c40ec5a9 | ths | if (r < 0) |
1413 | c40ec5a9 | ths | goto fail;
|
1414 | c40ec5a9 | ths | |
1415 | c40ec5a9 | ths | /* Enable periodic mode */
|
1416 | c40ec5a9 | ths | r = ioctl(fd, HPET_EPI, 0);
|
1417 | c40ec5a9 | ths | if (info.hi_flags && (r < 0)) |
1418 | c40ec5a9 | ths | goto fail;
|
1419 | c40ec5a9 | ths | |
1420 | c40ec5a9 | ths | /* Enable interrupt */
|
1421 | c40ec5a9 | ths | r = ioctl(fd, HPET_IE_ON, 0);
|
1422 | c40ec5a9 | ths | if (r < 0) |
1423 | c40ec5a9 | ths | goto fail;
|
1424 | c40ec5a9 | ths | |
1425 | c40ec5a9 | ths | enable_sigio_timer(fd); |
1426 | fcdc2129 | pbrook | t->priv = (void *)(long)fd; |
1427 | c40ec5a9 | ths | |
1428 | c40ec5a9 | ths | return 0; |
1429 | c40ec5a9 | ths | fail:
|
1430 | c40ec5a9 | ths | close(fd); |
1431 | c40ec5a9 | ths | return -1; |
1432 | c40ec5a9 | ths | } |
1433 | c40ec5a9 | ths | |
1434 | c40ec5a9 | ths | static void hpet_stop_timer(struct qemu_alarm_timer *t) |
1435 | c40ec5a9 | ths | { |
1436 | fcdc2129 | pbrook | int fd = (long)t->priv; |
1437 | c40ec5a9 | ths | |
1438 | c40ec5a9 | ths | close(fd); |
1439 | c40ec5a9 | ths | } |
1440 | c40ec5a9 | ths | |
1441 | c8994013 | ths | static int rtc_start_timer(struct qemu_alarm_timer *t) |
1442 | fd872598 | bellard | { |
1443 | c8994013 | ths | int rtc_fd;
|
1444 | b5a23ad4 | balrog | unsigned long current_rtc_freq = 0; |
1445 | c8994013 | ths | |
1446 | aeb30be6 | balrog | TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
|
1447 | fd872598 | bellard | if (rtc_fd < 0) |
1448 | fd872598 | bellard | return -1; |
1449 | b5a23ad4 | balrog | ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
1450 | b5a23ad4 | balrog | if (current_rtc_freq != RTC_FREQ &&
|
1451 | b5a23ad4 | balrog | ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
|
1452 | fd872598 | bellard | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
|
1453 | fd872598 | bellard | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
|
1454 | fd872598 | bellard | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
|
1455 | fd872598 | bellard | goto fail;
|
1456 | fd872598 | bellard | } |
1457 | fd872598 | bellard | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
1458 | fd872598 | bellard | fail:
|
1459 | fd872598 | bellard | close(rtc_fd); |
1460 | fd872598 | bellard | return -1; |
1461 | fd872598 | bellard | } |
1462 | c8994013 | ths | |
1463 | c8994013 | ths | enable_sigio_timer(rtc_fd); |
1464 | c8994013 | ths | |
1465 | fcdc2129 | pbrook | t->priv = (void *)(long)rtc_fd; |
1466 | c8994013 | ths | |
1467 | fd872598 | bellard | return 0; |
1468 | fd872598 | bellard | } |
1469 | fd872598 | bellard | |
1470 | c8994013 | ths | static void rtc_stop_timer(struct qemu_alarm_timer *t) |
1471 | 829309c7 | bellard | { |
1472 | fcdc2129 | pbrook | int rtc_fd = (long)t->priv; |
1473 | c8994013 | ths | |
1474 | c8994013 | ths | close(rtc_fd); |
1475 | 829309c7 | bellard | } |
1476 | 829309c7 | bellard | |
1477 | efe75411 | ths | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
1478 | efe75411 | ths | { |
1479 | efe75411 | ths | struct sigevent ev;
|
1480 | efe75411 | ths | timer_t host_timer; |
1481 | efe75411 | ths | struct sigaction act;
|
1482 | efe75411 | ths | |
1483 | efe75411 | ths | sigfillset(&act.sa_mask); |
1484 | efe75411 | ths | act.sa_flags = 0;
|
1485 | efe75411 | ths | act.sa_handler = host_alarm_handler; |
1486 | efe75411 | ths | |
1487 | efe75411 | ths | sigaction(SIGALRM, &act, NULL);
|
1488 | efe75411 | ths | |
1489 | efe75411 | ths | ev.sigev_value.sival_int = 0;
|
1490 | efe75411 | ths | ev.sigev_notify = SIGEV_SIGNAL; |
1491 | efe75411 | ths | ev.sigev_signo = SIGALRM; |
1492 | efe75411 | ths | |
1493 | efe75411 | ths | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
|
1494 | efe75411 | ths | perror("timer_create");
|
1495 | efe75411 | ths | |
1496 | efe75411 | ths | /* disable dynticks */
|
1497 | efe75411 | ths | fprintf(stderr, "Dynamic Ticks disabled\n");
|
1498 | efe75411 | ths | |
1499 | efe75411 | ths | return -1; |
1500 | efe75411 | ths | } |
1501 | efe75411 | ths | |
1502 | efe75411 | ths | t->priv = (void *)host_timer;
|
1503 | efe75411 | ths | |
1504 | efe75411 | ths | return 0; |
1505 | efe75411 | ths | } |
1506 | efe75411 | ths | |
1507 | efe75411 | ths | static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
1508 | efe75411 | ths | { |
1509 | efe75411 | ths | timer_t host_timer = (timer_t)t->priv; |
1510 | efe75411 | ths | |
1511 | efe75411 | ths | timer_delete(host_timer); |
1512 | efe75411 | ths | } |
1513 | efe75411 | ths | |
1514 | efe75411 | ths | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
1515 | efe75411 | ths | { |
1516 | efe75411 | ths | timer_t host_timer = (timer_t)t->priv; |
1517 | efe75411 | ths | struct itimerspec timeout;
|
1518 | efe75411 | ths | int64_t nearest_delta_us = INT64_MAX; |
1519 | efe75411 | ths | int64_t current_us; |
1520 | efe75411 | ths | |
1521 | efe75411 | ths | if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1522 | efe75411 | ths | !active_timers[QEMU_TIMER_VIRTUAL]) |
1523 | d5d08334 | balrog | return;
|
1524 | efe75411 | ths | |
1525 | 2e70f6ef | pbrook | nearest_delta_us = qemu_next_deadline_dyntick(); |
1526 | efe75411 | ths | |
1527 | efe75411 | ths | /* check whether a timer is already running */
|
1528 | efe75411 | ths | if (timer_gettime(host_timer, &timeout)) {
|
1529 | efe75411 | ths | perror("gettime");
|
1530 | efe75411 | ths | fprintf(stderr, "Internal timer error: aborting\n");
|
1531 | efe75411 | ths | exit(1);
|
1532 | efe75411 | ths | } |
1533 | efe75411 | ths | current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
1534 | efe75411 | ths | if (current_us && current_us <= nearest_delta_us)
|
1535 | efe75411 | ths | return;
|
1536 | efe75411 | ths | |
1537 | efe75411 | ths | timeout.it_interval.tv_sec = 0;
|
1538 | efe75411 | ths | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
1539 | efe75411 | ths | timeout.it_value.tv_sec = nearest_delta_us / 1000000;
|
1540 | efe75411 | ths | timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
1541 | efe75411 | ths | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
1542 | efe75411 | ths | perror("settime");
|
1543 | efe75411 | ths | fprintf(stderr, "Internal timer error: aborting\n");
|
1544 | efe75411 | ths | exit(1);
|
1545 | efe75411 | ths | } |
1546 | efe75411 | ths | } |
1547 | efe75411 | ths | |
1548 | 70744b3a | ths | #endif /* defined(__linux__) */ |
1549 | 231c6586 | ths | |
1550 | c8994013 | ths | static int unix_start_timer(struct qemu_alarm_timer *t) |
1551 | c8994013 | ths | { |
1552 | c8994013 | ths | struct sigaction act;
|
1553 | c8994013 | ths | struct itimerval itv;
|
1554 | c8994013 | ths | int err;
|
1555 | c8994013 | ths | |
1556 | c8994013 | ths | /* timer signal */
|
1557 | c8994013 | ths | sigfillset(&act.sa_mask); |
1558 | c8994013 | ths | act.sa_flags = 0;
|
1559 | c8994013 | ths | act.sa_handler = host_alarm_handler; |
1560 | c8994013 | ths | |
1561 | c8994013 | ths | sigaction(SIGALRM, &act, NULL);
|
1562 | c8994013 | ths | |
1563 | c8994013 | ths | itv.it_interval.tv_sec = 0;
|
1564 | c8994013 | ths | /* for i386 kernel 2.6 to get 1 ms */
|
1565 | c8994013 | ths | itv.it_interval.tv_usec = 999;
|
1566 | c8994013 | ths | itv.it_value.tv_sec = 0;
|
1567 | c8994013 | ths | itv.it_value.tv_usec = 10 * 1000; |
1568 | c8994013 | ths | |
1569 | c8994013 | ths | err = setitimer(ITIMER_REAL, &itv, NULL);
|
1570 | c8994013 | ths | if (err)
|
1571 | c8994013 | ths | return -1; |
1572 | c8994013 | ths | |
1573 | c8994013 | ths | return 0; |
1574 | c8994013 | ths | } |
1575 | c8994013 | ths | |
1576 | c8994013 | ths | static void unix_stop_timer(struct qemu_alarm_timer *t) |
1577 | c8994013 | ths | { |
1578 | c8994013 | ths | struct itimerval itv;
|
1579 | c8994013 | ths | |
1580 | c8994013 | ths | memset(&itv, 0, sizeof(itv)); |
1581 | c8994013 | ths | setitimer(ITIMER_REAL, &itv, NULL);
|
1582 | c8994013 | ths | } |
1583 | c8994013 | ths | |
1584 | 829309c7 | bellard | #endif /* !defined(_WIN32) */ |
1585 | fd872598 | bellard | |
1586 | c8994013 | ths | #ifdef _WIN32
|
1587 | c8994013 | ths | |
1588 | c8994013 | ths | static int win32_start_timer(struct qemu_alarm_timer *t) |
1589 | c8994013 | ths | { |
1590 | c8994013 | ths | TIMECAPS tc; |
1591 | c8994013 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1592 | efe75411 | ths | UINT flags; |
1593 | c8994013 | ths | |
1594 | c8994013 | ths | data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL); |
1595 | c8994013 | ths | if (!data->host_alarm) {
|
1596 | c8994013 | ths | perror("Failed CreateEvent");
|
1597 | c396a7f0 | ths | return -1; |
1598 | c8994013 | ths | } |
1599 | c8994013 | ths | |
1600 | c8994013 | ths | memset(&tc, 0, sizeof(tc)); |
1601 | c8994013 | ths | timeGetDevCaps(&tc, sizeof(tc));
|
1602 | c8994013 | ths | |
1603 | c8994013 | ths | if (data->period < tc.wPeriodMin)
|
1604 | c8994013 | ths | data->period = tc.wPeriodMin; |
1605 | c8994013 | ths | |
1606 | c8994013 | ths | timeBeginPeriod(data->period); |
1607 | c8994013 | ths | |
1608 | efe75411 | ths | flags = TIME_CALLBACK_FUNCTION; |
1609 | efe75411 | ths | if (alarm_has_dynticks(t))
|
1610 | efe75411 | ths | flags |= TIME_ONESHOT; |
1611 | efe75411 | ths | else
|
1612 | efe75411 | ths | flags |= TIME_PERIODIC; |
1613 | efe75411 | ths | |
1614 | c8994013 | ths | data->timerId = timeSetEvent(1, // interval (ms) |
1615 | c8994013 | ths | data->period, // resolution
|
1616 | c8994013 | ths | host_alarm_handler, // function
|
1617 | c8994013 | ths | (DWORD)t, // parameter
|
1618 | efe75411 | ths | flags); |
1619 | c8994013 | ths | |
1620 | c8994013 | ths | if (!data->timerId) {
|
1621 | c8994013 | ths | perror("Failed to initialize win32 alarm timer");
|
1622 | c8994013 | ths | |
1623 | c8994013 | ths | timeEndPeriod(data->period); |
1624 | c8994013 | ths | CloseHandle(data->host_alarm); |
1625 | c8994013 | ths | return -1; |
1626 | c8994013 | ths | } |
1627 | c8994013 | ths | |
1628 | c8994013 | ths | qemu_add_wait_object(data->host_alarm, NULL, NULL); |
1629 | c8994013 | ths | |
1630 | c8994013 | ths | return 0; |
1631 | c8994013 | ths | } |
1632 | c8994013 | ths | |
1633 | c8994013 | ths | static void win32_stop_timer(struct qemu_alarm_timer *t) |
1634 | c8994013 | ths | { |
1635 | c8994013 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1636 | c8994013 | ths | |
1637 | c8994013 | ths | timeKillEvent(data->timerId); |
1638 | c8994013 | ths | timeEndPeriod(data->period); |
1639 | c8994013 | ths | |
1640 | c8994013 | ths | CloseHandle(data->host_alarm); |
1641 | c8994013 | ths | } |
1642 | c8994013 | ths | |
1643 | efe75411 | ths | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
1644 | efe75411 | ths | { |
1645 | efe75411 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1646 | efe75411 | ths | uint64_t nearest_delta_us; |
1647 | efe75411 | ths | |
1648 | efe75411 | ths | if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1649 | efe75411 | ths | !active_timers[QEMU_TIMER_VIRTUAL]) |
1650 | d5d08334 | balrog | return;
|
1651 | efe75411 | ths | |
1652 | 2e70f6ef | pbrook | nearest_delta_us = qemu_next_deadline_dyntick(); |
1653 | efe75411 | ths | nearest_delta_us /= 1000;
|
1654 | efe75411 | ths | |
1655 | efe75411 | ths | timeKillEvent(data->timerId); |
1656 | efe75411 | ths | |
1657 | efe75411 | ths | data->timerId = timeSetEvent(1,
|
1658 | efe75411 | ths | data->period, |
1659 | efe75411 | ths | host_alarm_handler, |
1660 | efe75411 | ths | (DWORD)t, |
1661 | efe75411 | ths | TIME_ONESHOT | TIME_PERIODIC); |
1662 | efe75411 | ths | |
1663 | efe75411 | ths | if (!data->timerId) {
|
1664 | efe75411 | ths | perror("Failed to re-arm win32 alarm timer");
|
1665 | efe75411 | ths | |
1666 | efe75411 | ths | timeEndPeriod(data->period); |
1667 | efe75411 | ths | CloseHandle(data->host_alarm); |
1668 | efe75411 | ths | exit(1);
|
1669 | efe75411 | ths | } |
1670 | efe75411 | ths | } |
1671 | efe75411 | ths | |
1672 | c8994013 | ths | #endif /* _WIN32 */ |
1673 | c8994013 | ths | |
1674 | 1dce7c3c | bellard | static void init_timer_alarm(void) |
1675 | 8a7ddc38 | bellard | { |
1676 | c8994013 | ths | struct qemu_alarm_timer *t;
|
1677 | c8994013 | ths | int i, err = -1; |
1678 | c8994013 | ths | |
1679 | c8994013 | ths | for (i = 0; alarm_timers[i].name; i++) { |
1680 | c8994013 | ths | t = &alarm_timers[i]; |
1681 | c8994013 | ths | |
1682 | c8994013 | ths | err = t->start(t); |
1683 | c8994013 | ths | if (!err)
|
1684 | c8994013 | ths | break;
|
1685 | 67b915a5 | bellard | } |
1686 | fd872598 | bellard | |
1687 | c8994013 | ths | if (err) {
|
1688 | c8994013 | ths | fprintf(stderr, "Unable to find any suitable alarm timer.\n");
|
1689 | c8994013 | ths | fprintf(stderr, "Terminating\n");
|
1690 | c8994013 | ths | exit(1);
|
1691 | 67b915a5 | bellard | } |
1692 | c8994013 | ths | |
1693 | c8994013 | ths | alarm_timer = t; |
1694 | 8a7ddc38 | bellard | } |
1695 | 8a7ddc38 | bellard | |
1696 | 9596ebb7 | pbrook | static void quit_timers(void) |
1697 | 40c3bac3 | bellard | { |
1698 | c8994013 | ths | alarm_timer->stop(alarm_timer); |
1699 | c8994013 | ths | alarm_timer = NULL;
|
1700 | 40c3bac3 | bellard | } |
1701 | 40c3bac3 | bellard | |
1702 | c4b1fcc0 | bellard | /***********************************************************/
|
1703 | f6503059 | balrog | /* host time/date access */
|
1704 | f6503059 | balrog | void qemu_get_timedate(struct tm *tm, int offset) |
1705 | f6503059 | balrog | { |
1706 | f6503059 | balrog | time_t ti; |
1707 | f6503059 | balrog | struct tm *ret;
|
1708 | f6503059 | balrog | |
1709 | f6503059 | balrog | time(&ti); |
1710 | f6503059 | balrog | ti += offset; |
1711 | f6503059 | balrog | if (rtc_date_offset == -1) { |
1712 | f6503059 | balrog | if (rtc_utc)
|
1713 | f6503059 | balrog | ret = gmtime(&ti); |
1714 | f6503059 | balrog | else
|
1715 | f6503059 | balrog | ret = localtime(&ti); |
1716 | f6503059 | balrog | } else {
|
1717 | f6503059 | balrog | ti -= rtc_date_offset; |
1718 | f6503059 | balrog | ret = gmtime(&ti); |
1719 | f6503059 | balrog | } |
1720 | f6503059 | balrog | |
1721 | f6503059 | balrog | memcpy(tm, ret, sizeof(struct tm)); |
1722 | f6503059 | balrog | } |
1723 | f6503059 | balrog | |
1724 | f6503059 | balrog | int qemu_timedate_diff(struct tm *tm) |
1725 | f6503059 | balrog | { |
1726 | f6503059 | balrog | time_t seconds; |
1727 | f6503059 | balrog | |
1728 | f6503059 | balrog | if (rtc_date_offset == -1) |
1729 | f6503059 | balrog | if (rtc_utc)
|
1730 | f6503059 | balrog | seconds = mktimegm(tm); |
1731 | f6503059 | balrog | else
|
1732 | f6503059 | balrog | seconds = mktime(tm); |
1733 | f6503059 | balrog | else
|
1734 | f6503059 | balrog | seconds = mktimegm(tm) + rtc_date_offset; |
1735 | f6503059 | balrog | |
1736 | f6503059 | balrog | return seconds - time(NULL); |
1737 | f6503059 | balrog | } |
1738 | f6503059 | balrog | |
1739 | f6503059 | balrog | /***********************************************************/
|
1740 | 82c643ff | bellard | /* character device */
|
1741 | 313aa567 | bellard | |
1742 | e5b0bc44 | pbrook | static void qemu_chr_event(CharDriverState *s, int event) |
1743 | e5b0bc44 | pbrook | { |
1744 | e5b0bc44 | pbrook | if (!s->chr_event)
|
1745 | e5b0bc44 | pbrook | return;
|
1746 | e5b0bc44 | pbrook | s->chr_event(s->handler_opaque, event); |
1747 | e5b0bc44 | pbrook | } |
1748 | e5b0bc44 | pbrook | |
1749 | 86e94dea | ths | static void qemu_chr_reset_bh(void *opaque) |
1750 | 86e94dea | ths | { |
1751 | 86e94dea | ths | CharDriverState *s = opaque; |
1752 | e5b0bc44 | pbrook | qemu_chr_event(s, CHR_EVENT_RESET); |
1753 | 86e94dea | ths | qemu_bh_delete(s->bh); |
1754 | 86e94dea | ths | s->bh = NULL;
|
1755 | 86e94dea | ths | } |
1756 | 86e94dea | ths | |
1757 | 86e94dea | ths | void qemu_chr_reset(CharDriverState *s)
|
1758 | 86e94dea | ths | { |
1759 | 86e94dea | ths | if (s->bh == NULL) { |
1760 | 86e94dea | ths | s->bh = qemu_bh_new(qemu_chr_reset_bh, s); |
1761 | 86e94dea | ths | qemu_bh_schedule(s->bh); |
1762 | 86e94dea | ths | } |
1763 | 86e94dea | ths | } |
1764 | 86e94dea | ths | |
1765 | 82c643ff | bellard | int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
1766 | 82c643ff | bellard | { |
1767 | 82c643ff | bellard | return s->chr_write(s, buf, len);
|
1768 | 82c643ff | bellard | } |
1769 | 67b915a5 | bellard | |
1770 | e57a8c0e | bellard | int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg) |
1771 | f8d179e3 | bellard | { |
1772 | e57a8c0e | bellard | if (!s->chr_ioctl)
|
1773 | e57a8c0e | bellard | return -ENOTSUP;
|
1774 | e57a8c0e | bellard | return s->chr_ioctl(s, cmd, arg);
|
1775 | f8d179e3 | bellard | } |
1776 | f8d179e3 | bellard | |
1777 | e5b0bc44 | pbrook | int qemu_chr_can_read(CharDriverState *s)
|
1778 | e5b0bc44 | pbrook | { |
1779 | e5b0bc44 | pbrook | if (!s->chr_can_read)
|
1780 | e5b0bc44 | pbrook | return 0; |
1781 | e5b0bc44 | pbrook | return s->chr_can_read(s->handler_opaque);
|
1782 | e5b0bc44 | pbrook | } |
1783 | e5b0bc44 | pbrook | |
1784 | e5b0bc44 | pbrook | void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len) |
1785 | e5b0bc44 | pbrook | { |
1786 | e5b0bc44 | pbrook | s->chr_read(s->handler_opaque, buf, len); |
1787 | e5b0bc44 | pbrook | } |
1788 | e5b0bc44 | pbrook | |
1789 | bd9bdce6 | balrog | void qemu_chr_accept_input(CharDriverState *s)
|
1790 | bd9bdce6 | balrog | { |
1791 | bd9bdce6 | balrog | if (s->chr_accept_input)
|
1792 | bd9bdce6 | balrog | s->chr_accept_input(s); |
1793 | bd9bdce6 | balrog | } |
1794 | e5b0bc44 | pbrook | |
1795 | 82c643ff | bellard | void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
1796 | 67b915a5 | bellard | { |
1797 | 82c643ff | bellard | char buf[4096]; |
1798 | 82c643ff | bellard | va_list ap; |
1799 | 82c643ff | bellard | va_start(ap, fmt); |
1800 | 82c643ff | bellard | vsnprintf(buf, sizeof(buf), fmt, ap);
|
1801 | ffe8ab83 | ths | qemu_chr_write(s, (uint8_t *)buf, strlen(buf)); |
1802 | 82c643ff | bellard | va_end(ap); |
1803 | 67b915a5 | bellard | } |
1804 | 67b915a5 | bellard | |
1805 | 5905b2e5 | bellard | void qemu_chr_send_event(CharDriverState *s, int event) |
1806 | 5905b2e5 | bellard | { |
1807 | 5905b2e5 | bellard | if (s->chr_send_event)
|
1808 | 5905b2e5 | bellard | s->chr_send_event(s, event); |
1809 | 5905b2e5 | bellard | } |
1810 | 5905b2e5 | bellard | |
1811 | 5fafdf24 | ths | void qemu_chr_add_handlers(CharDriverState *s,
|
1812 | 5fafdf24 | ths | IOCanRWHandler *fd_can_read, |
1813 | e5b0bc44 | pbrook | IOReadHandler *fd_read, |
1814 | e5b0bc44 | pbrook | IOEventHandler *fd_event, |
1815 | e5b0bc44 | pbrook | void *opaque)
|
1816 | 82c643ff | bellard | { |
1817 | e5b0bc44 | pbrook | s->chr_can_read = fd_can_read; |
1818 | e5b0bc44 | pbrook | s->chr_read = fd_read; |
1819 | e5b0bc44 | pbrook | s->chr_event = fd_event; |
1820 | e5b0bc44 | pbrook | s->handler_opaque = opaque; |
1821 | e5b0bc44 | pbrook | if (s->chr_update_read_handler)
|
1822 | e5b0bc44 | pbrook | s->chr_update_read_handler(s); |
1823 | 82c643ff | bellard | } |
1824 | 3b46e624 | ths | |
1825 | 82c643ff | bellard | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1826 | 313aa567 | bellard | { |
1827 | 82c643ff | bellard | return len;
|
1828 | 82c643ff | bellard | } |
1829 | 82c643ff | bellard | |
1830 | 52f61fde | ths | static CharDriverState *qemu_chr_open_null(void) |
1831 | 82c643ff | bellard | { |
1832 | 82c643ff | bellard | CharDriverState *chr; |
1833 | 82c643ff | bellard | |
1834 | 82c643ff | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
1835 | 82c643ff | bellard | if (!chr)
|
1836 | 82c643ff | bellard | return NULL; |
1837 | 82c643ff | bellard | chr->chr_write = null_chr_write; |
1838 | 82c643ff | bellard | return chr;
|
1839 | 82c643ff | bellard | } |
1840 | 82c643ff | bellard | |
1841 | 20d8a3ed | ths | /* MUX driver for serial I/O splitting */
|
1842 | 20d8a3ed | ths | static int term_timestamps; |
1843 | 20d8a3ed | ths | static int64_t term_timestamps_start;
|
1844 | 9c1de612 | ths | #define MAX_MUX 4 |
1845 | bd9bdce6 | balrog | #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ |
1846 | bd9bdce6 | balrog | #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) |
1847 | 20d8a3ed | ths | typedef struct { |
1848 | 20d8a3ed | ths | IOCanRWHandler *chr_can_read[MAX_MUX]; |
1849 | 20d8a3ed | ths | IOReadHandler *chr_read[MAX_MUX]; |
1850 | 20d8a3ed | ths | IOEventHandler *chr_event[MAX_MUX]; |
1851 | 20d8a3ed | ths | void *ext_opaque[MAX_MUX];
|
1852 | 20d8a3ed | ths | CharDriverState *drv; |
1853 | bd9bdce6 | balrog | unsigned char buffer[MUX_BUFFER_SIZE]; |
1854 | bd9bdce6 | balrog | int prod;
|
1855 | bd9bdce6 | balrog | int cons;
|
1856 | 20d8a3ed | ths | int mux_cnt;
|
1857 | 20d8a3ed | ths | int term_got_escape;
|
1858 | 20d8a3ed | ths | int max_size;
|
1859 | 20d8a3ed | ths | } MuxDriver; |
1860 | 20d8a3ed | ths | |
1861 | 20d8a3ed | ths | |
1862 | 20d8a3ed | ths | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1863 | 20d8a3ed | ths | { |
1864 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
1865 | 20d8a3ed | ths | int ret;
|
1866 | 20d8a3ed | ths | if (!term_timestamps) {
|
1867 | 20d8a3ed | ths | ret = d->drv->chr_write(d->drv, buf, len); |
1868 | 20d8a3ed | ths | } else {
|
1869 | 20d8a3ed | ths | int i;
|
1870 | 20d8a3ed | ths | |
1871 | 20d8a3ed | ths | ret = 0;
|
1872 | 20d8a3ed | ths | for(i = 0; i < len; i++) { |
1873 | 20d8a3ed | ths | ret += d->drv->chr_write(d->drv, buf+i, 1);
|
1874 | 20d8a3ed | ths | if (buf[i] == '\n') { |
1875 | 20d8a3ed | ths | char buf1[64]; |
1876 | 20d8a3ed | ths | int64_t ti; |
1877 | 20d8a3ed | ths | int secs;
|
1878 | 20d8a3ed | ths | |
1879 | 20d8a3ed | ths | ti = get_clock(); |
1880 | 20d8a3ed | ths | if (term_timestamps_start == -1) |
1881 | 20d8a3ed | ths | term_timestamps_start = ti; |
1882 | 20d8a3ed | ths | ti -= term_timestamps_start; |
1883 | 20d8a3ed | ths | secs = ti / 1000000000;
|
1884 | 20d8a3ed | ths | snprintf(buf1, sizeof(buf1),
|
1885 | 20d8a3ed | ths | "[%02d:%02d:%02d.%03d] ",
|
1886 | 20d8a3ed | ths | secs / 3600,
|
1887 | 20d8a3ed | ths | (secs / 60) % 60, |
1888 | 20d8a3ed | ths | secs % 60,
|
1889 | 20d8a3ed | ths | (int)((ti / 1000000) % 1000)); |
1890 | ffe8ab83 | ths | d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
1891 | 20d8a3ed | ths | } |
1892 | 20d8a3ed | ths | } |
1893 | 20d8a3ed | ths | } |
1894 | 20d8a3ed | ths | return ret;
|
1895 | 20d8a3ed | ths | } |
1896 | 20d8a3ed | ths | |
1897 | 20d8a3ed | ths | static char *mux_help[] = { |
1898 | 20d8a3ed | ths | "% h print this help\n\r",
|
1899 | 20d8a3ed | ths | "% x exit emulator\n\r",
|
1900 | 20d8a3ed | ths | "% s save disk data back to file (if -snapshot)\n\r",
|
1901 | 20d8a3ed | ths | "% t toggle console timestamps\n\r"
|
1902 | 20d8a3ed | ths | "% b send break (magic sysrq)\n\r",
|
1903 | 20d8a3ed | ths | "% c switch between console and monitor\n\r",
|
1904 | 20d8a3ed | ths | "% % sends %\n\r",
|
1905 | 20d8a3ed | ths | NULL
|
1906 | 20d8a3ed | ths | }; |
1907 | 20d8a3ed | ths | |
1908 | 20d8a3ed | ths | static int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
1909 | 20d8a3ed | ths | static void mux_print_help(CharDriverState *chr) |
1910 | 20d8a3ed | ths | { |
1911 | 20d8a3ed | ths | int i, j;
|
1912 | 20d8a3ed | ths | char ebuf[15] = "Escape-Char"; |
1913 | 20d8a3ed | ths | char cbuf[50] = "\n\r"; |
1914 | 20d8a3ed | ths | |
1915 | 20d8a3ed | ths | if (term_escape_char > 0 && term_escape_char < 26) { |
1916 | 363a37d5 | blueswir1 | snprintf(cbuf, sizeof(cbuf), "\n\r"); |
1917 | 363a37d5 | blueswir1 | snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a'); |
1918 | 20d8a3ed | ths | } else {
|
1919 | 363a37d5 | blueswir1 | snprintf(cbuf, sizeof(cbuf),
|
1920 | 363a37d5 | blueswir1 | "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
|
1921 | 363a37d5 | blueswir1 | term_escape_char); |
1922 | 20d8a3ed | ths | } |
1923 | ffe8ab83 | ths | chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf)); |
1924 | 20d8a3ed | ths | for (i = 0; mux_help[i] != NULL; i++) { |
1925 | 20d8a3ed | ths | for (j=0; mux_help[i][j] != '\0'; j++) { |
1926 | 20d8a3ed | ths | if (mux_help[i][j] == '%') |
1927 | ffe8ab83 | ths | chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf)); |
1928 | 20d8a3ed | ths | else
|
1929 | ffe8ab83 | ths | chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
|
1930 | 20d8a3ed | ths | } |
1931 | 20d8a3ed | ths | } |
1932 | 20d8a3ed | ths | } |
1933 | 20d8a3ed | ths | |
1934 | 20d8a3ed | ths | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
1935 | 20d8a3ed | ths | { |
1936 | 20d8a3ed | ths | if (d->term_got_escape) {
|
1937 | 20d8a3ed | ths | d->term_got_escape = 0;
|
1938 | 20d8a3ed | ths | if (ch == term_escape_char)
|
1939 | 20d8a3ed | ths | goto send_char;
|
1940 | 20d8a3ed | ths | switch(ch) {
|
1941 | 20d8a3ed | ths | case '?': |
1942 | 20d8a3ed | ths | case 'h': |
1943 | 20d8a3ed | ths | mux_print_help(chr); |
1944 | 20d8a3ed | ths | break;
|
1945 | 20d8a3ed | ths | case 'x': |
1946 | 20d8a3ed | ths | { |
1947 | 20d8a3ed | ths | char *term = "QEMU: Terminated\n\r"; |
1948 | ffe8ab83 | ths | chr->chr_write(chr,(uint8_t *)term,strlen(term)); |
1949 | 20d8a3ed | ths | exit(0);
|
1950 | 20d8a3ed | ths | break;
|
1951 | 20d8a3ed | ths | } |
1952 | 20d8a3ed | ths | case 's': |
1953 | 20d8a3ed | ths | { |
1954 | 20d8a3ed | ths | int i;
|
1955 | e4bcb14c | ths | for (i = 0; i < nb_drives; i++) { |
1956 | e4bcb14c | ths | bdrv_commit(drives_table[i].bdrv); |
1957 | 20d8a3ed | ths | } |
1958 | 20d8a3ed | ths | } |
1959 | 20d8a3ed | ths | break;
|
1960 | 20d8a3ed | ths | case 'b': |
1961 | 36ddb83b | balrog | qemu_chr_event(chr, CHR_EVENT_BREAK); |
1962 | 20d8a3ed | ths | break;
|
1963 | 20d8a3ed | ths | case 'c': |
1964 | 20d8a3ed | ths | /* Switch to the next registered device */
|
1965 | 20d8a3ed | ths | chr->focus++; |
1966 | 20d8a3ed | ths | if (chr->focus >= d->mux_cnt)
|
1967 | 20d8a3ed | ths | chr->focus = 0;
|
1968 | 20d8a3ed | ths | break;
|
1969 | 20d8a3ed | ths | case 't': |
1970 | 20d8a3ed | ths | term_timestamps = !term_timestamps; |
1971 | 20d8a3ed | ths | term_timestamps_start = -1;
|
1972 | 20d8a3ed | ths | break;
|
1973 | 20d8a3ed | ths | } |
1974 | 20d8a3ed | ths | } else if (ch == term_escape_char) { |
1975 | 20d8a3ed | ths | d->term_got_escape = 1;
|
1976 | 20d8a3ed | ths | } else {
|
1977 | 20d8a3ed | ths | send_char:
|
1978 | 20d8a3ed | ths | return 1; |
1979 | 20d8a3ed | ths | } |
1980 | 20d8a3ed | ths | return 0; |
1981 | 20d8a3ed | ths | } |
1982 | 20d8a3ed | ths | |
1983 | bd9bdce6 | balrog | static void mux_chr_accept_input(CharDriverState *chr) |
1984 | bd9bdce6 | balrog | { |
1985 | bd9bdce6 | balrog | int m = chr->focus;
|
1986 | bd9bdce6 | balrog | MuxDriver *d = chr->opaque; |
1987 | bd9bdce6 | balrog | |
1988 | bd9bdce6 | balrog | while (d->prod != d->cons &&
|
1989 | bd9bdce6 | balrog | d->chr_can_read[m] && |
1990 | bd9bdce6 | balrog | d->chr_can_read[m](d->ext_opaque[m])) { |
1991 | bd9bdce6 | balrog | d->chr_read[m](d->ext_opaque[m], |
1992 | bd9bdce6 | balrog | &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
|
1993 | bd9bdce6 | balrog | } |
1994 | bd9bdce6 | balrog | } |
1995 | bd9bdce6 | balrog | |
1996 | 20d8a3ed | ths | static int mux_chr_can_read(void *opaque) |
1997 | 20d8a3ed | ths | { |
1998 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
1999 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
2000 | bd9bdce6 | balrog | |
2001 | bd9bdce6 | balrog | if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
|
2002 | bd9bdce6 | balrog | return 1; |
2003 | 20d8a3ed | ths | if (d->chr_can_read[chr->focus])
|
2004 | bd9bdce6 | balrog | return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
|
2005 | 20d8a3ed | ths | return 0; |
2006 | 20d8a3ed | ths | } |
2007 | 20d8a3ed | ths | |
2008 | 20d8a3ed | ths | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
2009 | 20d8a3ed | ths | { |
2010 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2011 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
2012 | bd9bdce6 | balrog | int m = chr->focus;
|
2013 | 20d8a3ed | ths | int i;
|
2014 | bd9bdce6 | balrog | |
2015 | bd9bdce6 | balrog | mux_chr_accept_input (opaque); |
2016 | bd9bdce6 | balrog | |
2017 | 20d8a3ed | ths | for(i = 0; i < size; i++) |
2018 | bd9bdce6 | balrog | if (mux_proc_byte(chr, d, buf[i])) {
|
2019 | bd9bdce6 | balrog | if (d->prod == d->cons &&
|
2020 | bd9bdce6 | balrog | d->chr_can_read[m] && |
2021 | bd9bdce6 | balrog | d->chr_can_read[m](d->ext_opaque[m])) |
2022 | bd9bdce6 | balrog | d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
|
2023 | bd9bdce6 | balrog | else
|
2024 | bd9bdce6 | balrog | d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i]; |
2025 | bd9bdce6 | balrog | } |
2026 | 20d8a3ed | ths | } |
2027 | 20d8a3ed | ths | |
2028 | 20d8a3ed | ths | static void mux_chr_event(void *opaque, int event) |
2029 | 20d8a3ed | ths | { |
2030 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2031 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
2032 | 20d8a3ed | ths | int i;
|
2033 | 20d8a3ed | ths | |
2034 | 20d8a3ed | ths | /* Send the event to all registered listeners */
|
2035 | 20d8a3ed | ths | for (i = 0; i < d->mux_cnt; i++) |
2036 | 20d8a3ed | ths | if (d->chr_event[i])
|
2037 | 20d8a3ed | ths | d->chr_event[i](d->ext_opaque[i], event); |
2038 | 20d8a3ed | ths | } |
2039 | 20d8a3ed | ths | |
2040 | 20d8a3ed | ths | static void mux_chr_update_read_handler(CharDriverState *chr) |
2041 | 20d8a3ed | ths | { |
2042 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
2043 | 20d8a3ed | ths | |
2044 | 20d8a3ed | ths | if (d->mux_cnt >= MAX_MUX) {
|
2045 | 20d8a3ed | ths | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
|
2046 | 20d8a3ed | ths | return;
|
2047 | 20d8a3ed | ths | } |
2048 | 20d8a3ed | ths | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
2049 | 20d8a3ed | ths | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
2050 | 20d8a3ed | ths | d->chr_read[d->mux_cnt] = chr->chr_read; |
2051 | 20d8a3ed | ths | d->chr_event[d->mux_cnt] = chr->chr_event; |
2052 | 20d8a3ed | ths | /* Fix up the real driver with mux routines */
|
2053 | 20d8a3ed | ths | if (d->mux_cnt == 0) { |
2054 | 20d8a3ed | ths | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
2055 | 20d8a3ed | ths | mux_chr_event, chr); |
2056 | 20d8a3ed | ths | } |
2057 | 20d8a3ed | ths | chr->focus = d->mux_cnt; |
2058 | 20d8a3ed | ths | d->mux_cnt++; |
2059 | 20d8a3ed | ths | } |
2060 | 20d8a3ed | ths | |
2061 | 9596ebb7 | pbrook | static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
|
2062 | 20d8a3ed | ths | { |
2063 | 20d8a3ed | ths | CharDriverState *chr; |
2064 | 20d8a3ed | ths | MuxDriver *d; |
2065 | 20d8a3ed | ths | |
2066 | 20d8a3ed | ths | chr = qemu_mallocz(sizeof(CharDriverState));
|
2067 | 20d8a3ed | ths | if (!chr)
|
2068 | 20d8a3ed | ths | return NULL; |
2069 | 20d8a3ed | ths | d = qemu_mallocz(sizeof(MuxDriver));
|
2070 | 20d8a3ed | ths | if (!d) {
|
2071 | 20d8a3ed | ths | free(chr); |
2072 | 20d8a3ed | ths | return NULL; |
2073 | 20d8a3ed | ths | } |
2074 | 20d8a3ed | ths | |
2075 | 20d8a3ed | ths | chr->opaque = d; |
2076 | 20d8a3ed | ths | d->drv = drv; |
2077 | 20d8a3ed | ths | chr->focus = -1;
|
2078 | 20d8a3ed | ths | chr->chr_write = mux_chr_write; |
2079 | 20d8a3ed | ths | chr->chr_update_read_handler = mux_chr_update_read_handler; |
2080 | bd9bdce6 | balrog | chr->chr_accept_input = mux_chr_accept_input; |
2081 | 20d8a3ed | ths | return chr;
|
2082 | 20d8a3ed | ths | } |
2083 | 20d8a3ed | ths | |
2084 | 20d8a3ed | ths | |
2085 | fd1dff4b | bellard | #ifdef _WIN32
|
2086 | 82c643ff | bellard | |
2087 | fd1dff4b | bellard | static void socket_cleanup(void) |
2088 | fd1dff4b | bellard | { |
2089 | fd1dff4b | bellard | WSACleanup(); |
2090 | fd1dff4b | bellard | } |
2091 | 82c643ff | bellard | |
2092 | fd1dff4b | bellard | static int socket_init(void) |
2093 | fd1dff4b | bellard | { |
2094 | fd1dff4b | bellard | WSADATA Data; |
2095 | fd1dff4b | bellard | int ret, err;
|
2096 | fd1dff4b | bellard | |
2097 | fd1dff4b | bellard | ret = WSAStartup(MAKEWORD(2,2), &Data); |
2098 | fd1dff4b | bellard | if (ret != 0) { |
2099 | fd1dff4b | bellard | err = WSAGetLastError(); |
2100 | fd1dff4b | bellard | fprintf(stderr, "WSAStartup: %d\n", err);
|
2101 | fd1dff4b | bellard | return -1; |
2102 | fd1dff4b | bellard | } |
2103 | fd1dff4b | bellard | atexit(socket_cleanup); |
2104 | fd1dff4b | bellard | return 0; |
2105 | fd1dff4b | bellard | } |
2106 | fd1dff4b | bellard | |
2107 | fd1dff4b | bellard | static int send_all(int fd, const uint8_t *buf, int len1) |
2108 | fd1dff4b | bellard | { |
2109 | fd1dff4b | bellard | int ret, len;
|
2110 | 3b46e624 | ths | |
2111 | fd1dff4b | bellard | len = len1; |
2112 | fd1dff4b | bellard | while (len > 0) { |
2113 | fd1dff4b | bellard | ret = send(fd, buf, len, 0);
|
2114 | fd1dff4b | bellard | if (ret < 0) { |
2115 | fd1dff4b | bellard | int errno;
|
2116 | fd1dff4b | bellard | errno = WSAGetLastError(); |
2117 | fd1dff4b | bellard | if (errno != WSAEWOULDBLOCK) {
|
2118 | fd1dff4b | bellard | return -1; |
2119 | fd1dff4b | bellard | } |
2120 | fd1dff4b | bellard | } else if (ret == 0) { |
2121 | fd1dff4b | bellard | break;
|
2122 | fd1dff4b | bellard | } else {
|
2123 | fd1dff4b | bellard | buf += ret; |
2124 | fd1dff4b | bellard | len -= ret; |
2125 | fd1dff4b | bellard | } |
2126 | fd1dff4b | bellard | } |
2127 | fd1dff4b | bellard | return len1 - len;
|
2128 | fd1dff4b | bellard | } |
2129 | fd1dff4b | bellard | |
2130 | fd1dff4b | bellard | void socket_set_nonblock(int fd) |
2131 | fd1dff4b | bellard | { |
2132 | fd1dff4b | bellard | unsigned long opt = 1; |
2133 | fd1dff4b | bellard | ioctlsocket(fd, FIONBIO, &opt); |
2134 | fd1dff4b | bellard | } |
2135 | fd1dff4b | bellard | |
2136 | fd1dff4b | bellard | #else
|
2137 | fd1dff4b | bellard | |
2138 | 1d96905d | bellard | static int unix_write(int fd, const uint8_t *buf, int len1) |
2139 | 1d96905d | bellard | { |
2140 | 1d96905d | bellard | int ret, len;
|
2141 | 1d96905d | bellard | |
2142 | 1d96905d | bellard | len = len1; |
2143 | 1d96905d | bellard | while (len > 0) { |
2144 | 1d96905d | bellard | ret = write(fd, buf, len); |
2145 | 1d96905d | bellard | if (ret < 0) { |
2146 | 1d96905d | bellard | if (errno != EINTR && errno != EAGAIN)
|
2147 | 1d96905d | bellard | return -1; |
2148 | 1d96905d | bellard | } else if (ret == 0) { |
2149 | 1d96905d | bellard | break;
|
2150 | 1d96905d | bellard | } else {
|
2151 | 1d96905d | bellard | buf += ret; |
2152 | 1d96905d | bellard | len -= ret; |
2153 | 1d96905d | bellard | } |
2154 | 1d96905d | bellard | } |
2155 | 1d96905d | bellard | return len1 - len;
|
2156 | 1d96905d | bellard | } |
2157 | 1d96905d | bellard | |
2158 | fd1dff4b | bellard | static inline int send_all(int fd, const uint8_t *buf, int len1) |
2159 | fd1dff4b | bellard | { |
2160 | fd1dff4b | bellard | return unix_write(fd, buf, len1);
|
2161 | fd1dff4b | bellard | } |
2162 | fd1dff4b | bellard | |
2163 | fd1dff4b | bellard | void socket_set_nonblock(int fd) |
2164 | fd1dff4b | bellard | { |
2165 | fd58ff9d | ths | int f;
|
2166 | fd58ff9d | ths | f = fcntl(fd, F_GETFL); |
2167 | fd58ff9d | ths | fcntl(fd, F_SETFL, f | O_NONBLOCK); |
2168 | fd1dff4b | bellard | } |
2169 | fd1dff4b | bellard | #endif /* !_WIN32 */ |
2170 | fd1dff4b | bellard | |
2171 | fd1dff4b | bellard | #ifndef _WIN32
|
2172 | fd1dff4b | bellard | |
2173 | fd1dff4b | bellard | typedef struct { |
2174 | fd1dff4b | bellard | int fd_in, fd_out;
|
2175 | fd1dff4b | bellard | int max_size;
|
2176 | fd1dff4b | bellard | } FDCharDriver; |
2177 | fd1dff4b | bellard | |
2178 | 20d8a3ed | ths | #define STDIO_MAX_CLIENTS 1 |
2179 | 20d8a3ed | ths | static int stdio_nb_clients = 0; |
2180 | fd1dff4b | bellard | |
2181 | 82c643ff | bellard | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
2182 | 82c643ff | bellard | { |
2183 | 82c643ff | bellard | FDCharDriver *s = chr->opaque; |
2184 | 1d96905d | bellard | return unix_write(s->fd_out, buf, len);
|
2185 | 82c643ff | bellard | } |
2186 | 82c643ff | bellard | |
2187 | 7c9d8e07 | bellard | static int fd_chr_read_poll(void *opaque) |
2188 | 7c9d8e07 | bellard | { |
2189 | 7c9d8e07 | bellard | CharDriverState *chr = opaque; |
2190 | 7c9d8e07 | bellard | FDCharDriver *s = chr->opaque; |
2191 | 7c9d8e07 | bellard | |
2192 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
2193 | 7c9d8e07 | bellard | return s->max_size;
|
2194 | 7c9d8e07 | bellard | } |
2195 | 7c9d8e07 | bellard | |
2196 | 7c9d8e07 | bellard | static void fd_chr_read(void *opaque) |
2197 | 7c9d8e07 | bellard | { |
2198 | 7c9d8e07 | bellard | CharDriverState *chr = opaque; |
2199 | 7c9d8e07 | bellard | FDCharDriver *s = chr->opaque; |
2200 | 7c9d8e07 | bellard | int size, len;
|
2201 | 7c9d8e07 | bellard | uint8_t buf[1024];
|
2202 | 3b46e624 | ths | |
2203 | 7c9d8e07 | bellard | len = sizeof(buf);
|
2204 | 7c9d8e07 | bellard | if (len > s->max_size)
|
2205 | 7c9d8e07 | bellard | len = s->max_size; |
2206 | 7c9d8e07 | bellard | if (len == 0) |
2207 | 7c9d8e07 | bellard | return;
|
2208 | 7c9d8e07 | bellard | size = read(s->fd_in, buf, len); |
2209 | 188157fe | pbrook | if (size == 0) { |
2210 | 188157fe | pbrook | /* FD has been closed. Remove it from the active list. */
|
2211 | 188157fe | pbrook | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
2212 | 188157fe | pbrook | return;
|
2213 | 188157fe | pbrook | } |
2214 | 7c9d8e07 | bellard | if (size > 0) { |
2215 | e5b0bc44 | pbrook | qemu_chr_read(chr, buf, size); |
2216 | 7c9d8e07 | bellard | } |
2217 | 7c9d8e07 | bellard | } |
2218 | 7c9d8e07 | bellard | |
2219 | e5b0bc44 | pbrook | static void fd_chr_update_read_handler(CharDriverState *chr) |
2220 | 82c643ff | bellard | { |
2221 | 82c643ff | bellard | FDCharDriver *s = chr->opaque; |
2222 | 82c643ff | bellard | |
2223 | f8d179e3 | bellard | if (s->fd_in >= 0) { |
2224 | f8d179e3 | bellard | if (nographic && s->fd_in == 0) { |
2225 | f8d179e3 | bellard | } else {
|
2226 | 5fafdf24 | ths | qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, |
2227 | 7c9d8e07 | bellard | fd_chr_read, NULL, chr);
|
2228 | f8d179e3 | bellard | } |
2229 | 82c643ff | bellard | } |
2230 | 82c643ff | bellard | } |
2231 | 82c643ff | bellard | |
2232 | a11d070e | balrog | static void fd_chr_close(struct CharDriverState *chr) |
2233 | a11d070e | balrog | { |
2234 | a11d070e | balrog | FDCharDriver *s = chr->opaque; |
2235 | a11d070e | balrog | |
2236 | a11d070e | balrog | if (s->fd_in >= 0) { |
2237 | a11d070e | balrog | if (nographic && s->fd_in == 0) { |
2238 | a11d070e | balrog | } else {
|
2239 | a11d070e | balrog | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
2240 | a11d070e | balrog | } |
2241 | a11d070e | balrog | } |
2242 | a11d070e | balrog | |
2243 | a11d070e | balrog | qemu_free(s); |
2244 | a11d070e | balrog | } |
2245 | a11d070e | balrog | |
2246 | 82c643ff | bellard | /* open a character device to a unix fd */
|
2247 | 52f61fde | ths | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
2248 | 82c643ff | bellard | { |
2249 | 82c643ff | bellard | CharDriverState *chr; |
2250 | 82c643ff | bellard | FDCharDriver *s; |
2251 | 82c643ff | bellard | |
2252 | 82c643ff | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
2253 | 82c643ff | bellard | if (!chr)
|
2254 | 82c643ff | bellard | return NULL; |
2255 | 82c643ff | bellard | s = qemu_mallocz(sizeof(FDCharDriver));
|
2256 | 82c643ff | bellard | if (!s) {
|
2257 | 82c643ff | bellard | free(chr); |
2258 | 82c643ff | bellard | return NULL; |
2259 | 82c643ff | bellard | } |
2260 | 82c643ff | bellard | s->fd_in = fd_in; |
2261 | 82c643ff | bellard | s->fd_out = fd_out; |
2262 | 82c643ff | bellard | chr->opaque = s; |
2263 | 82c643ff | bellard | chr->chr_write = fd_chr_write; |
2264 | e5b0bc44 | pbrook | chr->chr_update_read_handler = fd_chr_update_read_handler; |
2265 | a11d070e | balrog | chr->chr_close = fd_chr_close; |
2266 | 86e94dea | ths | |
2267 | 86e94dea | ths | qemu_chr_reset(chr); |
2268 | 86e94dea | ths | |
2269 | 82c643ff | bellard | return chr;
|
2270 | 82c643ff | bellard | } |
2271 | 82c643ff | bellard | |
2272 | 52f61fde | ths | static CharDriverState *qemu_chr_open_file_out(const char *file_out) |
2273 | f8d179e3 | bellard | { |
2274 | f8d179e3 | bellard | int fd_out;
|
2275 | f8d179e3 | bellard | |
2276 | aeb30be6 | balrog | TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
|
2277 | f8d179e3 | bellard | if (fd_out < 0) |
2278 | f8d179e3 | bellard | return NULL; |
2279 | f8d179e3 | bellard | return qemu_chr_open_fd(-1, fd_out); |
2280 | f8d179e3 | bellard | } |
2281 | f8d179e3 | bellard | |
2282 | 52f61fde | ths | static CharDriverState *qemu_chr_open_pipe(const char *filename) |
2283 | f8d179e3 | bellard | { |
2284 | c26c1c4b | ths | int fd_in, fd_out;
|
2285 | c26c1c4b | ths | char filename_in[256], filename_out[256]; |
2286 | c26c1c4b | ths | |
2287 | c26c1c4b | ths | snprintf(filename_in, 256, "%s.in", filename); |
2288 | c26c1c4b | ths | snprintf(filename_out, 256, "%s.out", filename); |
2289 | aeb30be6 | balrog | TFR(fd_in = open(filename_in, O_RDWR | O_BINARY)); |
2290 | aeb30be6 | balrog | TFR(fd_out = open(filename_out, O_RDWR | O_BINARY)); |
2291 | c26c1c4b | ths | if (fd_in < 0 || fd_out < 0) { |
2292 | c26c1c4b | ths | if (fd_in >= 0) |
2293 | c26c1c4b | ths | close(fd_in); |
2294 | c26c1c4b | ths | if (fd_out >= 0) |
2295 | c26c1c4b | ths | close(fd_out); |
2296 | aeb30be6 | balrog | TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY)); |
2297 | c26c1c4b | ths | if (fd_in < 0) |
2298 | c26c1c4b | ths | return NULL; |
2299 | c26c1c4b | ths | } |
2300 | c26c1c4b | ths | return qemu_chr_open_fd(fd_in, fd_out);
|
2301 | f8d179e3 | bellard | } |
2302 | f8d179e3 | bellard | |
2303 | f8d179e3 | bellard | |
2304 | 82c643ff | bellard | /* for STDIO, we handle the case where several clients use it
|
2305 | 82c643ff | bellard | (nographic mode) */
|
2306 | 82c643ff | bellard | |
2307 | aa0bc6b6 | bellard | #define TERM_FIFO_MAX_SIZE 1 |
2308 | aa0bc6b6 | bellard | |
2309 | aa0bc6b6 | bellard | static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
|
2310 | 1dce7c3c | bellard | static int term_fifo_size; |
2311 | 330d0414 | bellard | |
2312 | 7c9d8e07 | bellard | static int stdio_read_poll(void *opaque) |
2313 | 82c643ff | bellard | { |
2314 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2315 | aa0bc6b6 | bellard | |
2316 | 20d8a3ed | ths | /* try to flush the queue if needed */
|
2317 | 20d8a3ed | ths | if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) { |
2318 | 20d8a3ed | ths | qemu_chr_read(chr, term_fifo, 1);
|
2319 | 20d8a3ed | ths | term_fifo_size = 0;
|
2320 | aa0bc6b6 | bellard | } |
2321 | 20d8a3ed | ths | /* see if we can absorb more chars */
|
2322 | 20d8a3ed | ths | if (term_fifo_size == 0) |
2323 | 20d8a3ed | ths | return 1; |
2324 | 20d8a3ed | ths | else
|
2325 | 20d8a3ed | ths | return 0; |
2326 | 82c643ff | bellard | } |
2327 | 82c643ff | bellard | |
2328 | 7c9d8e07 | bellard | static void stdio_read(void *opaque) |
2329 | 82c643ff | bellard | { |
2330 | 7c9d8e07 | bellard | int size;
|
2331 | 7c9d8e07 | bellard | uint8_t buf[1];
|
2332 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2333 | 20d8a3ed | ths | |
2334 | 7c9d8e07 | bellard | size = read(0, buf, 1); |
2335 | 519945df | pbrook | if (size == 0) { |
2336 | 519945df | pbrook | /* stdin has been closed. Remove it from the active list. */
|
2337 | 519945df | pbrook | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
2338 | 519945df | pbrook | return;
|
2339 | 519945df | pbrook | } |
2340 | 20d8a3ed | ths | if (size > 0) { |
2341 | 20d8a3ed | ths | if (qemu_chr_can_read(chr) > 0) { |
2342 | 20d8a3ed | ths | qemu_chr_read(chr, buf, 1);
|
2343 | 20d8a3ed | ths | } else if (term_fifo_size == 0) { |
2344 | 20d8a3ed | ths | term_fifo[term_fifo_size++] = buf[0];
|
2345 | 1dce7c3c | bellard | } |
2346 | 1dce7c3c | bellard | } |
2347 | 1dce7c3c | bellard | } |
2348 | 1dce7c3c | bellard | |
2349 | 8d11df9e | bellard | /* init terminal so that we can grab keys */
|
2350 | 8d11df9e | bellard | static struct termios oldtty; |
2351 | 8d11df9e | bellard | static int old_fd0_flags; |
2352 | a11d070e | balrog | static int term_atexit_done; |
2353 | 8d11df9e | bellard | |
2354 | 8d11df9e | bellard | static void term_exit(void) |
2355 | 8d11df9e | bellard | { |
2356 | 8d11df9e | bellard | tcsetattr (0, TCSANOW, &oldtty);
|
2357 | 8d11df9e | bellard | fcntl(0, F_SETFL, old_fd0_flags);
|
2358 | 8d11df9e | bellard | } |
2359 | 8d11df9e | bellard | |
2360 | 8d11df9e | bellard | static void term_init(void) |
2361 | 8d11df9e | bellard | { |
2362 | 8d11df9e | bellard | struct termios tty;
|
2363 | 8d11df9e | bellard | |
2364 | 8d11df9e | bellard | tcgetattr (0, &tty);
|
2365 | 8d11df9e | bellard | oldtty = tty; |
2366 | 8d11df9e | bellard | old_fd0_flags = fcntl(0, F_GETFL);
|
2367 | 8d11df9e | bellard | |
2368 | 8d11df9e | bellard | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
2369 | 8d11df9e | bellard | |INLCR|IGNCR|ICRNL|IXON); |
2370 | 8d11df9e | bellard | tty.c_oflag |= OPOST; |
2371 | 8d11df9e | bellard | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
2372 | 8d11df9e | bellard | /* if graphical mode, we allow Ctrl-C handling */
|
2373 | 8d11df9e | bellard | if (nographic)
|
2374 | 8d11df9e | bellard | tty.c_lflag &= ~ISIG; |
2375 | 8d11df9e | bellard | tty.c_cflag &= ~(CSIZE|PARENB); |
2376 | 8d11df9e | bellard | tty.c_cflag |= CS8; |
2377 | 8d11df9e | bellard | tty.c_cc[VMIN] = 1;
|
2378 | 8d11df9e | bellard | tty.c_cc[VTIME] = 0;
|
2379 | 3b46e624 | ths | |
2380 | 8d11df9e | bellard | tcsetattr (0, TCSANOW, &tty);
|
2381 | 8d11df9e | bellard | |
2382 | a11d070e | balrog | if (!term_atexit_done++)
|
2383 | a11d070e | balrog | atexit(term_exit); |
2384 | 8d11df9e | bellard | |
2385 | 8d11df9e | bellard | fcntl(0, F_SETFL, O_NONBLOCK);
|
2386 | 8d11df9e | bellard | } |
2387 | 8d11df9e | bellard | |
2388 | a11d070e | balrog | static void qemu_chr_close_stdio(struct CharDriverState *chr) |
2389 | a11d070e | balrog | { |
2390 | a11d070e | balrog | term_exit(); |
2391 | a11d070e | balrog | stdio_nb_clients--; |
2392 | a11d070e | balrog | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
2393 | a11d070e | balrog | fd_chr_close(chr); |
2394 | a11d070e | balrog | } |
2395 | a11d070e | balrog | |
2396 | 52f61fde | ths | static CharDriverState *qemu_chr_open_stdio(void) |
2397 | 82c643ff | bellard | { |
2398 | 82c643ff | bellard | CharDriverState *chr; |
2399 | 82c643ff | bellard | |
2400 | 20d8a3ed | ths | if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
|
2401 | 20d8a3ed | ths | return NULL; |
2402 | 20d8a3ed | ths | chr = qemu_chr_open_fd(0, 1); |
2403 | a11d070e | balrog | chr->chr_close = qemu_chr_close_stdio; |
2404 | 20d8a3ed | ths | qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr); |
2405 | 20d8a3ed | ths | stdio_nb_clients++; |
2406 | 20d8a3ed | ths | term_init(); |
2407 | 20d8a3ed | ths | |
2408 | 82c643ff | bellard | return chr;
|
2409 | 82c643ff | bellard | } |
2410 | 82c643ff | bellard | |
2411 | 64b7b733 | aurel32 | #ifdef __sun__
|
2412 | 64b7b733 | aurel32 | /* Once Solaris has openpty(), this is going to be removed. */
|
2413 | 64b7b733 | aurel32 | int openpty(int *amaster, int *aslave, char *name, |
2414 | 64b7b733 | aurel32 | struct termios *termp, struct winsize *winp) |
2415 | 64b7b733 | aurel32 | { |
2416 | 64b7b733 | aurel32 | const char *slave; |
2417 | 64b7b733 | aurel32 | int mfd = -1, sfd = -1; |
2418 | 64b7b733 | aurel32 | |
2419 | 64b7b733 | aurel32 | *amaster = *aslave = -1;
|
2420 | 64b7b733 | aurel32 | |
2421 | 64b7b733 | aurel32 | mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
2422 | 64b7b733 | aurel32 | if (mfd < 0) |
2423 | 64b7b733 | aurel32 | goto err;
|
2424 | 64b7b733 | aurel32 | |
2425 | 64b7b733 | aurel32 | if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) |
2426 | 64b7b733 | aurel32 | goto err;
|
2427 | 64b7b733 | aurel32 | |
2428 | 64b7b733 | aurel32 | if ((slave = ptsname(mfd)) == NULL) |
2429 | 64b7b733 | aurel32 | goto err;
|
2430 | 64b7b733 | aurel32 | |
2431 | 64b7b733 | aurel32 | if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) |
2432 | 64b7b733 | aurel32 | goto err;
|
2433 | 64b7b733 | aurel32 | |
2434 | 64b7b733 | aurel32 | if (ioctl(sfd, I_PUSH, "ptem") == -1 || |
2435 | 64b7b733 | aurel32 | (termp != NULL && tcgetattr(sfd, termp) < 0)) |
2436 | 64b7b733 | aurel32 | goto err;
|
2437 | 64b7b733 | aurel32 | |
2438 | 64b7b733 | aurel32 | if (amaster)
|
2439 | 64b7b733 | aurel32 | *amaster = mfd; |
2440 | 64b7b733 | aurel32 | if (aslave)
|
2441 | 64b7b733 | aurel32 | *aslave = sfd; |
2442 | 64b7b733 | aurel32 | if (winp)
|
2443 | 64b7b733 | aurel32 | ioctl(sfd, TIOCSWINSZ, winp); |
2444 | 64b7b733 | aurel32 | |
2445 | 64b7b733 | aurel32 | return 0; |
2446 | 64b7b733 | aurel32 | |
2447 | 64b7b733 | aurel32 | err:
|
2448 | 64b7b733 | aurel32 | if (sfd != -1) |
2449 | 64b7b733 | aurel32 | close(sfd); |
2450 | 64b7b733 | aurel32 | close(mfd); |
2451 | 64b7b733 | aurel32 | return -1; |
2452 | 64b7b733 | aurel32 | } |
2453 | 64b7b733 | aurel32 | |
2454 | 64b7b733 | aurel32 | void cfmakeraw (struct termios *termios_p) |
2455 | 64b7b733 | aurel32 | { |
2456 | 64b7b733 | aurel32 | termios_p->c_iflag &= |
2457 | 64b7b733 | aurel32 | ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); |
2458 | 64b7b733 | aurel32 | termios_p->c_oflag &= ~OPOST; |
2459 | 64b7b733 | aurel32 | termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); |
2460 | 64b7b733 | aurel32 | termios_p->c_cflag &= ~(CSIZE|PARENB); |
2461 | 64b7b733 | aurel32 | termios_p->c_cflag |= CS8; |
2462 | 64b7b733 | aurel32 | |
2463 | 64b7b733 | aurel32 | termios_p->c_cc[VMIN] = 0;
|
2464 | 64b7b733 | aurel32 | termios_p->c_cc[VTIME] = 0;
|
2465 | 64b7b733 | aurel32 | } |
2466 | 64b7b733 | aurel32 | #endif
|
2467 | 64b7b733 | aurel32 | |
2468 | aec62507 | ths | #if defined(__linux__) || defined(__sun__)
|
2469 | 279e694b | aliguori | |
2470 | 279e694b | aliguori | typedef struct { |
2471 | 279e694b | aliguori | int fd;
|
2472 | 279e694b | aliguori | int connected;
|
2473 | 279e694b | aliguori | int polling;
|
2474 | 279e694b | aliguori | int read_bytes;
|
2475 | 279e694b | aliguori | QEMUTimer *timer; |
2476 | 279e694b | aliguori | } PtyCharDriver; |
2477 | 279e694b | aliguori | |
2478 | 279e694b | aliguori | static void pty_chr_update_read_handler(CharDriverState *chr); |
2479 | 279e694b | aliguori | static void pty_chr_state(CharDriverState *chr, int connected); |
2480 | 279e694b | aliguori | |
2481 | 279e694b | aliguori | static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
2482 | 279e694b | aliguori | { |
2483 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2484 | 279e694b | aliguori | |
2485 | 279e694b | aliguori | if (!s->connected) {
|
2486 | 279e694b | aliguori | /* guest sends data, check for (re-)connect */
|
2487 | 279e694b | aliguori | pty_chr_update_read_handler(chr); |
2488 | 279e694b | aliguori | return 0; |
2489 | 279e694b | aliguori | } |
2490 | 279e694b | aliguori | return unix_write(s->fd, buf, len);
|
2491 | 279e694b | aliguori | } |
2492 | 279e694b | aliguori | |
2493 | 279e694b | aliguori | static int pty_chr_read_poll(void *opaque) |
2494 | 279e694b | aliguori | { |
2495 | 279e694b | aliguori | CharDriverState *chr = opaque; |
2496 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2497 | 279e694b | aliguori | |
2498 | 279e694b | aliguori | s->read_bytes = qemu_chr_can_read(chr); |
2499 | 279e694b | aliguori | return s->read_bytes;
|
2500 | 279e694b | aliguori | } |
2501 | 279e694b | aliguori | |
2502 | 279e694b | aliguori | static void pty_chr_read(void *opaque) |
2503 | 279e694b | aliguori | { |
2504 | 279e694b | aliguori | CharDriverState *chr = opaque; |
2505 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2506 | 279e694b | aliguori | int size, len;
|
2507 | 279e694b | aliguori | uint8_t buf[1024];
|
2508 | 279e694b | aliguori | |
2509 | 279e694b | aliguori | len = sizeof(buf);
|
2510 | 279e694b | aliguori | if (len > s->read_bytes)
|
2511 | 279e694b | aliguori | len = s->read_bytes; |
2512 | 279e694b | aliguori | if (len == 0) |
2513 | 279e694b | aliguori | return;
|
2514 | 279e694b | aliguori | size = read(s->fd, buf, len); |
2515 | 279e694b | aliguori | if ((size == -1 && errno == EIO) || |
2516 | 279e694b | aliguori | (size == 0)) {
|
2517 | 279e694b | aliguori | pty_chr_state(chr, 0);
|
2518 | 279e694b | aliguori | return;
|
2519 | 279e694b | aliguori | } |
2520 | 279e694b | aliguori | if (size > 0) { |
2521 | 279e694b | aliguori | pty_chr_state(chr, 1);
|
2522 | 279e694b | aliguori | qemu_chr_read(chr, buf, size); |
2523 | 279e694b | aliguori | } |
2524 | 279e694b | aliguori | } |
2525 | 279e694b | aliguori | |
2526 | 279e694b | aliguori | static void pty_chr_update_read_handler(CharDriverState *chr) |
2527 | 279e694b | aliguori | { |
2528 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2529 | 279e694b | aliguori | |
2530 | 279e694b | aliguori | qemu_set_fd_handler2(s->fd, pty_chr_read_poll, |
2531 | 279e694b | aliguori | pty_chr_read, NULL, chr);
|
2532 | 279e694b | aliguori | s->polling = 1;
|
2533 | 279e694b | aliguori | /*
|
2534 | 279e694b | aliguori | * Short timeout here: just need wait long enougth that qemu makes
|
2535 | 279e694b | aliguori | * it through the poll loop once. When reconnected we want a
|
2536 | 279e694b | aliguori | * short timeout so we notice it almost instantly. Otherwise
|
2537 | 279e694b | aliguori | * read() gives us -EIO instantly, making pty_chr_state() reset the
|
2538 | 279e694b | aliguori | * timeout to the normal (much longer) poll interval before the
|
2539 | 279e694b | aliguori | * timer triggers.
|
2540 | 279e694b | aliguori | */
|
2541 | 279e694b | aliguori | qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
|
2542 | 279e694b | aliguori | } |
2543 | 279e694b | aliguori | |
2544 | 279e694b | aliguori | static void pty_chr_state(CharDriverState *chr, int connected) |
2545 | 279e694b | aliguori | { |
2546 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2547 | 279e694b | aliguori | |
2548 | 279e694b | aliguori | if (!connected) {
|
2549 | 279e694b | aliguori | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
2550 | 279e694b | aliguori | s->connected = 0;
|
2551 | 279e694b | aliguori | s->polling = 0;
|
2552 | 279e694b | aliguori | /* (re-)connect poll interval for idle guests: once per second.
|
2553 | 279e694b | aliguori | * We check more frequently in case the guests sends data to
|
2554 | 279e694b | aliguori | * the virtual device linked to our pty. */
|
2555 | 279e694b | aliguori | qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
|
2556 | 279e694b | aliguori | } else {
|
2557 | 279e694b | aliguori | if (!s->connected)
|
2558 | 279e694b | aliguori | qemu_chr_reset(chr); |
2559 | 279e694b | aliguori | s->connected = 1;
|
2560 | 279e694b | aliguori | } |
2561 | 279e694b | aliguori | } |
2562 | 279e694b | aliguori | |
2563 | 8fcd3692 | blueswir1 | static void pty_chr_timer(void *opaque) |
2564 | 279e694b | aliguori | { |
2565 | 279e694b | aliguori | struct CharDriverState *chr = opaque;
|
2566 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2567 | 279e694b | aliguori | |
2568 | 279e694b | aliguori | if (s->connected)
|
2569 | 279e694b | aliguori | return;
|
2570 | 279e694b | aliguori | if (s->polling) {
|
2571 | 279e694b | aliguori | /* If we arrive here without polling being cleared due
|
2572 | 279e694b | aliguori | * read returning -EIO, then we are (re-)connected */
|
2573 | 279e694b | aliguori | pty_chr_state(chr, 1);
|
2574 | 279e694b | aliguori | return;
|
2575 | 279e694b | aliguori | } |
2576 | 279e694b | aliguori | |
2577 | 279e694b | aliguori | /* Next poll ... */
|
2578 | 279e694b | aliguori | pty_chr_update_read_handler(chr); |
2579 | 279e694b | aliguori | } |
2580 | 279e694b | aliguori | |
2581 | 279e694b | aliguori | static void pty_chr_close(struct CharDriverState *chr) |
2582 | 279e694b | aliguori | { |
2583 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2584 | 279e694b | aliguori | |
2585 | 279e694b | aliguori | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
2586 | 279e694b | aliguori | close(s->fd); |
2587 | 279e694b | aliguori | qemu_free(s); |
2588 | 279e694b | aliguori | } |
2589 | 279e694b | aliguori | |
2590 | 52f61fde | ths | static CharDriverState *qemu_chr_open_pty(void) |
2591 | 82c643ff | bellard | { |
2592 | 279e694b | aliguori | CharDriverState *chr; |
2593 | 279e694b | aliguori | PtyCharDriver *s; |
2594 | 91fc2119 | bellard | struct termios tty;
|
2595 | 279e694b | aliguori | int slave_fd;
|
2596 | 279e694b | aliguori | |
2597 | 279e694b | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
2598 | 279e694b | aliguori | if (!chr)
|
2599 | 279e694b | aliguori | return NULL; |
2600 | 279e694b | aliguori | s = qemu_mallocz(sizeof(PtyCharDriver));
|
2601 | 279e694b | aliguori | if (!s) {
|
2602 | 279e694b | aliguori | qemu_free(chr); |
2603 | 279e694b | aliguori | return NULL; |
2604 | 279e694b | aliguori | } |
2605 | 3b46e624 | ths | |
2606 | 279e694b | aliguori | if (openpty(&s->fd, &slave_fd, NULL, NULL, NULL) < 0) { |
2607 | 82c643ff | bellard | return NULL; |
2608 | 82c643ff | bellard | } |
2609 | 3b46e624 | ths | |
2610 | 64b7b733 | aurel32 | /* Set raw attributes on the pty. */
|
2611 | 64b7b733 | aurel32 | cfmakeraw(&tty); |
2612 | 64b7b733 | aurel32 | tcsetattr(slave_fd, TCSAFLUSH, &tty); |
2613 | 279e694b | aliguori | close(slave_fd); |
2614 | 279e694b | aliguori | |
2615 | 279e694b | aliguori | fprintf(stderr, "char device redirected to %s\n", ptsname(s->fd));
|
2616 | 91fc2119 | bellard | |
2617 | 279e694b | aliguori | chr->opaque = s; |
2618 | 279e694b | aliguori | chr->chr_write = pty_chr_write; |
2619 | 279e694b | aliguori | chr->chr_update_read_handler = pty_chr_update_read_handler; |
2620 | 279e694b | aliguori | chr->chr_close = pty_chr_close; |
2621 | 279e694b | aliguori | |
2622 | 279e694b | aliguori | s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr); |
2623 | 279e694b | aliguori | |
2624 | 279e694b | aliguori | return chr;
|
2625 | 82c643ff | bellard | } |
2626 | f8d179e3 | bellard | |
2627 | 5fafdf24 | ths | static void tty_serial_init(int fd, int speed, |
2628 | f8d179e3 | bellard | int parity, int data_bits, int stop_bits) |
2629 | f8d179e3 | bellard | { |
2630 | f8d179e3 | bellard | struct termios tty;
|
2631 | f8d179e3 | bellard | speed_t spd; |
2632 | f8d179e3 | bellard | |
2633 | e57a8c0e | bellard | #if 0
|
2634 | 5fafdf24 | ths | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
|
2635 | e57a8c0e | bellard | speed, parity, data_bits, stop_bits);
|
2636 | e57a8c0e | bellard | #endif
|
2637 | e57a8c0e | bellard | tcgetattr (fd, &tty); |
2638 | f8d179e3 | bellard | |
2639 | a7954218 | balrog | #define MARGIN 1.1 |
2640 | a7954218 | balrog | if (speed <= 50 * MARGIN) |
2641 | f8d179e3 | bellard | spd = B50; |
2642 | a7954218 | balrog | else if (speed <= 75 * MARGIN) |
2643 | f8d179e3 | bellard | spd = B75; |
2644 | a7954218 | balrog | else if (speed <= 300 * MARGIN) |
2645 | f8d179e3 | bellard | spd = B300; |
2646 | a7954218 | balrog | else if (speed <= 600 * MARGIN) |
2647 | f8d179e3 | bellard | spd = B600; |
2648 | a7954218 | balrog | else if (speed <= 1200 * MARGIN) |
2649 | f8d179e3 | bellard | spd = B1200; |
2650 | a7954218 | balrog | else if (speed <= 2400 * MARGIN) |
2651 | f8d179e3 | bellard | spd = B2400; |
2652 | a7954218 | balrog | else if (speed <= 4800 * MARGIN) |
2653 | f8d179e3 | bellard | spd = B4800; |
2654 | a7954218 | balrog | else if (speed <= 9600 * MARGIN) |
2655 | f8d179e3 | bellard | spd = B9600; |
2656 | a7954218 | balrog | else if (speed <= 19200 * MARGIN) |
2657 | f8d179e3 | bellard | spd = B19200; |
2658 | a7954218 | balrog | else if (speed <= 38400 * MARGIN) |
2659 | f8d179e3 | bellard | spd = B38400; |
2660 | a7954218 | balrog | else if (speed <= 57600 * MARGIN) |
2661 | f8d179e3 | bellard | spd = B57600; |
2662 | a7954218 | balrog | else if (speed <= 115200 * MARGIN) |
2663 | a7954218 | balrog | spd = B115200; |
2664 | a7954218 | balrog | else
|
2665 | f8d179e3 | bellard | spd = B115200; |
2666 | f8d179e3 | bellard | |
2667 | f8d179e3 | bellard | cfsetispeed(&tty, spd); |
2668 | f8d179e3 | bellard | cfsetospeed(&tty, spd); |
2669 | f8d179e3 | bellard | |
2670 | f8d179e3 | bellard | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
2671 | f8d179e3 | bellard | |INLCR|IGNCR|ICRNL|IXON); |
2672 | f8d179e3 | bellard | tty.c_oflag |= OPOST; |
2673 | f8d179e3 | bellard | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
2674 | 094eed6c | bellard | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
2675 | f8d179e3 | bellard | switch(data_bits) {
|
2676 | f8d179e3 | bellard | default:
|
2677 | f8d179e3 | bellard | case 8: |
2678 | f8d179e3 | bellard | tty.c_cflag |= CS8; |
2679 | f8d179e3 | bellard | break;
|
2680 | f8d179e3 | bellard | case 7: |
2681 | f8d179e3 | bellard | tty.c_cflag |= CS7; |
2682 | f8d179e3 | bellard | break;
|
2683 | f8d179e3 | bellard | case 6: |
2684 | f8d179e3 | bellard | tty.c_cflag |= CS6; |
2685 | f8d179e3 | bellard | break;
|
2686 | f8d179e3 | bellard | case 5: |
2687 | f8d179e3 | bellard | tty.c_cflag |= CS5; |
2688 | f8d179e3 | bellard | break;
|
2689 | f8d179e3 | bellard | } |
2690 | f8d179e3 | bellard | switch(parity) {
|
2691 | f8d179e3 | bellard | default:
|
2692 | f8d179e3 | bellard | case 'N': |
2693 | f8d179e3 | bellard | break;
|
2694 | f8d179e3 | bellard | case 'E': |
2695 | f8d179e3 | bellard | tty.c_cflag |= PARENB; |
2696 | f8d179e3 | bellard | break;
|
2697 | f8d179e3 | bellard | case 'O': |
2698 | f8d179e3 | bellard | tty.c_cflag |= PARENB | PARODD; |
2699 | f8d179e3 | bellard | break;
|
2700 | f8d179e3 | bellard | } |
2701 | 094eed6c | bellard | if (stop_bits == 2) |
2702 | 094eed6c | bellard | tty.c_cflag |= CSTOPB; |
2703 | 3b46e624 | ths | |
2704 | f8d179e3 | bellard | tcsetattr (fd, TCSANOW, &tty); |
2705 | f8d179e3 | bellard | } |
2706 | f8d179e3 | bellard | |
2707 | e57a8c0e | bellard | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
2708 | f8d179e3 | bellard | { |
2709 | f8d179e3 | bellard | FDCharDriver *s = chr->opaque; |
2710 | 3b46e624 | ths | |
2711 | e57a8c0e | bellard | switch(cmd) {
|
2712 | e57a8c0e | bellard | case CHR_IOCTL_SERIAL_SET_PARAMS:
|
2713 | e57a8c0e | bellard | { |
2714 | e57a8c0e | bellard | QEMUSerialSetParams *ssp = arg; |
2715 | 5fafdf24 | ths | tty_serial_init(s->fd_in, ssp->speed, ssp->parity, |
2716 | e57a8c0e | bellard | ssp->data_bits, ssp->stop_bits); |
2717 | e57a8c0e | bellard | } |
2718 | e57a8c0e | bellard | break;
|
2719 | e57a8c0e | bellard | case CHR_IOCTL_SERIAL_SET_BREAK:
|
2720 | e57a8c0e | bellard | { |
2721 | e57a8c0e | bellard | int enable = *(int *)arg; |
2722 | e57a8c0e | bellard | if (enable)
|
2723 | e57a8c0e | bellard | tcsendbreak(s->fd_in, 1);
|
2724 | e57a8c0e | bellard | } |
2725 | e57a8c0e | bellard | break;
|
2726 | 81174dae | aliguori | case CHR_IOCTL_SERIAL_GET_TIOCM:
|
2727 | 81174dae | aliguori | { |
2728 | 81174dae | aliguori | int sarg = 0; |
2729 | 81174dae | aliguori | int *targ = (int *)arg; |
2730 | 81174dae | aliguori | ioctl(s->fd_in, TIOCMGET, &sarg); |
2731 | 81174dae | aliguori | *targ = 0;
|
2732 | 81174dae | aliguori | if (sarg | TIOCM_CTS)
|
2733 | 81174dae | aliguori | *targ |= CHR_TIOCM_CTS; |
2734 | 81174dae | aliguori | if (sarg | TIOCM_CAR)
|
2735 | 81174dae | aliguori | *targ |= CHR_TIOCM_CAR; |
2736 | 81174dae | aliguori | if (sarg | TIOCM_DSR)
|
2737 | 81174dae | aliguori | *targ |= CHR_TIOCM_DSR; |
2738 | 81174dae | aliguori | if (sarg | TIOCM_RI)
|
2739 | 81174dae | aliguori | *targ |= CHR_TIOCM_RI; |
2740 | 81174dae | aliguori | if (sarg | TIOCM_DTR)
|
2741 | 81174dae | aliguori | *targ |= CHR_TIOCM_DTR; |
2742 | 81174dae | aliguori | if (sarg | TIOCM_RTS)
|
2743 | 81174dae | aliguori | *targ |= CHR_TIOCM_RTS; |
2744 | 81174dae | aliguori | } |
2745 | 81174dae | aliguori | break;
|
2746 | 81174dae | aliguori | case CHR_IOCTL_SERIAL_SET_TIOCM:
|
2747 | 81174dae | aliguori | { |
2748 | 81174dae | aliguori | int sarg = *(int *)arg; |
2749 | 81174dae | aliguori | int targ = 0; |
2750 | 81174dae | aliguori | if (sarg | CHR_TIOCM_DTR)
|
2751 | 81174dae | aliguori | targ |= TIOCM_DTR; |
2752 | 81174dae | aliguori | if (sarg | CHR_TIOCM_RTS)
|
2753 | 81174dae | aliguori | targ |= TIOCM_RTS; |
2754 | 81174dae | aliguori | ioctl(s->fd_in, TIOCMSET, &targ); |
2755 | 81174dae | aliguori | } |
2756 | 81174dae | aliguori | break;
|
2757 | e57a8c0e | bellard | default:
|
2758 | e57a8c0e | bellard | return -ENOTSUP;
|
2759 | e57a8c0e | bellard | } |
2760 | e57a8c0e | bellard | return 0; |
2761 | f8d179e3 | bellard | } |
2762 | f8d179e3 | bellard | |
2763 | 52f61fde | ths | static CharDriverState *qemu_chr_open_tty(const char *filename) |
2764 | f8d179e3 | bellard | { |
2765 | f8d179e3 | bellard | CharDriverState *chr; |
2766 | f8d179e3 | bellard | int fd;
|
2767 | f8d179e3 | bellard | |
2768 | aeb30be6 | balrog | TFR(fd = open(filename, O_RDWR | O_NONBLOCK)); |
2769 | f8d179e3 | bellard | tty_serial_init(fd, 115200, 'N', 8, 1); |
2770 | f8d179e3 | bellard | chr = qemu_chr_open_fd(fd, fd); |
2771 | aeb30be6 | balrog | if (!chr) {
|
2772 | aeb30be6 | balrog | close(fd); |
2773 | f8d179e3 | bellard | return NULL; |
2774 | aeb30be6 | balrog | } |
2775 | e57a8c0e | bellard | chr->chr_ioctl = tty_serial_ioctl; |
2776 | 86e94dea | ths | qemu_chr_reset(chr); |
2777 | e57a8c0e | bellard | return chr;
|
2778 | e57a8c0e | bellard | } |
2779 | aec62507 | ths | #else /* ! __linux__ && ! __sun__ */ |
2780 | aec62507 | ths | static CharDriverState *qemu_chr_open_pty(void) |
2781 | aec62507 | ths | { |
2782 | aec62507 | ths | return NULL; |
2783 | aec62507 | ths | } |
2784 | aec62507 | ths | #endif /* __linux__ || __sun__ */ |
2785 | e57a8c0e | bellard | |
2786 | aec62507 | ths | #if defined(__linux__)
|
2787 | 5867c88a | ths | typedef struct { |
2788 | 5867c88a | ths | int fd;
|
2789 | 5867c88a | ths | int mode;
|
2790 | 5867c88a | ths | } ParallelCharDriver; |
2791 | 5867c88a | ths | |
2792 | 5867c88a | ths | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
2793 | 5867c88a | ths | { |
2794 | 5867c88a | ths | if (s->mode != mode) {
|
2795 | 5867c88a | ths | int m = mode;
|
2796 | 5867c88a | ths | if (ioctl(s->fd, PPSETMODE, &m) < 0) |
2797 | 5867c88a | ths | return 0; |
2798 | 5867c88a | ths | s->mode = mode; |
2799 | 5867c88a | ths | } |
2800 | 5867c88a | ths | return 1; |
2801 | 5867c88a | ths | } |
2802 | 5867c88a | ths | |
2803 | e57a8c0e | bellard | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
2804 | e57a8c0e | bellard | { |
2805 | 5867c88a | ths | ParallelCharDriver *drv = chr->opaque; |
2806 | 5867c88a | ths | int fd = drv->fd;
|
2807 | e57a8c0e | bellard | uint8_t b; |
2808 | e57a8c0e | bellard | |
2809 | e57a8c0e | bellard | switch(cmd) {
|
2810 | e57a8c0e | bellard | case CHR_IOCTL_PP_READ_DATA:
|
2811 | e57a8c0e | bellard | if (ioctl(fd, PPRDATA, &b) < 0) |
2812 | e57a8c0e | bellard | return -ENOTSUP;
|
2813 | e57a8c0e | bellard | *(uint8_t *)arg = b; |
2814 | e57a8c0e | bellard | break;
|
2815 | e57a8c0e | bellard | case CHR_IOCTL_PP_WRITE_DATA:
|
2816 | e57a8c0e | bellard | b = *(uint8_t *)arg; |
2817 | e57a8c0e | bellard | if (ioctl(fd, PPWDATA, &b) < 0) |
2818 | e57a8c0e | bellard | return -ENOTSUP;
|
2819 | e57a8c0e | bellard | break;
|
2820 | e57a8c0e | bellard | case CHR_IOCTL_PP_READ_CONTROL:
|
2821 | e57a8c0e | bellard | if (ioctl(fd, PPRCONTROL, &b) < 0) |
2822 | e57a8c0e | bellard | return -ENOTSUP;
|
2823 | 5867c88a | ths | /* Linux gives only the lowest bits, and no way to know data
|
2824 | 5867c88a | ths | direction! For better compatibility set the fixed upper
|
2825 | 5867c88a | ths | bits. */
|
2826 | 5867c88a | ths | *(uint8_t *)arg = b | 0xc0;
|
2827 | e57a8c0e | bellard | break;
|
2828 | e57a8c0e | bellard | case CHR_IOCTL_PP_WRITE_CONTROL:
|
2829 | e57a8c0e | bellard | b = *(uint8_t *)arg; |
2830 | e57a8c0e | bellard | if (ioctl(fd, PPWCONTROL, &b) < 0) |
2831 | e57a8c0e | bellard | return -ENOTSUP;
|
2832 | e57a8c0e | bellard | break;
|
2833 | e57a8c0e | bellard | case CHR_IOCTL_PP_READ_STATUS:
|
2834 | e57a8c0e | bellard | if (ioctl(fd, PPRSTATUS, &b) < 0) |
2835 | e57a8c0e | bellard | return -ENOTSUP;
|
2836 | e57a8c0e | bellard | *(uint8_t *)arg = b; |
2837 | e57a8c0e | bellard | break;
|
2838 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_READ_ADDR:
|
2839 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
2840 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2841 | 5867c88a | ths | int n = read(fd, parg->buffer, parg->count);
|
2842 | 5867c88a | ths | if (n != parg->count) {
|
2843 | 5867c88a | ths | return -EIO;
|
2844 | 5867c88a | ths | } |
2845 | 5867c88a | ths | } |
2846 | 5867c88a | ths | break;
|
2847 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_READ:
|
2848 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
2849 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2850 | 5867c88a | ths | int n = read(fd, parg->buffer, parg->count);
|
2851 | 5867c88a | ths | if (n != parg->count) {
|
2852 | 5867c88a | ths | return -EIO;
|
2853 | 5867c88a | ths | } |
2854 | 5867c88a | ths | } |
2855 | 5867c88a | ths | break;
|
2856 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_WRITE_ADDR:
|
2857 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
2858 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2859 | 5867c88a | ths | int n = write(fd, parg->buffer, parg->count);
|
2860 | 5867c88a | ths | if (n != parg->count) {
|
2861 | 5867c88a | ths | return -EIO;
|
2862 | 5867c88a | ths | } |
2863 | 5867c88a | ths | } |
2864 | 5867c88a | ths | break;
|
2865 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_WRITE:
|
2866 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
2867 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2868 | 5867c88a | ths | int n = write(fd, parg->buffer, parg->count);
|
2869 | 5867c88a | ths | if (n != parg->count) {
|
2870 | 5867c88a | ths | return -EIO;
|
2871 | 5867c88a | ths | } |
2872 | 5867c88a | ths | } |
2873 | 5867c88a | ths | break;
|
2874 | e57a8c0e | bellard | default:
|
2875 | e57a8c0e | bellard | return -ENOTSUP;
|
2876 | e57a8c0e | bellard | } |
2877 | e57a8c0e | bellard | return 0; |
2878 | e57a8c0e | bellard | } |
2879 | e57a8c0e | bellard | |
2880 | 5867c88a | ths | static void pp_close(CharDriverState *chr) |
2881 | 5867c88a | ths | { |
2882 | 5867c88a | ths | ParallelCharDriver *drv = chr->opaque; |
2883 | 5867c88a | ths | int fd = drv->fd;
|
2884 | 5867c88a | ths | |
2885 | 5867c88a | ths | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
2886 | 5867c88a | ths | ioctl(fd, PPRELEASE); |
2887 | 5867c88a | ths | close(fd); |
2888 | 5867c88a | ths | qemu_free(drv); |
2889 | 5867c88a | ths | } |
2890 | 5867c88a | ths | |
2891 | 52f61fde | ths | static CharDriverState *qemu_chr_open_pp(const char *filename) |
2892 | e57a8c0e | bellard | { |
2893 | e57a8c0e | bellard | CharDriverState *chr; |
2894 | 5867c88a | ths | ParallelCharDriver *drv; |
2895 | e57a8c0e | bellard | int fd;
|
2896 | e57a8c0e | bellard | |
2897 | aeb30be6 | balrog | TFR(fd = open(filename, O_RDWR)); |
2898 | e57a8c0e | bellard | if (fd < 0) |
2899 | e57a8c0e | bellard | return NULL; |
2900 | e57a8c0e | bellard | |
2901 | e57a8c0e | bellard | if (ioctl(fd, PPCLAIM) < 0) { |
2902 | e57a8c0e | bellard | close(fd); |
2903 | e57a8c0e | bellard | return NULL; |
2904 | e57a8c0e | bellard | } |
2905 | e57a8c0e | bellard | |
2906 | 5867c88a | ths | drv = qemu_mallocz(sizeof(ParallelCharDriver));
|
2907 | 5867c88a | ths | if (!drv) {
|
2908 | 5867c88a | ths | close(fd); |
2909 | 5867c88a | ths | return NULL; |
2910 | 5867c88a | ths | } |
2911 | 5867c88a | ths | drv->fd = fd; |
2912 | 5867c88a | ths | drv->mode = IEEE1284_MODE_COMPAT; |
2913 | 5867c88a | ths | |
2914 | e57a8c0e | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
2915 | e57a8c0e | bellard | if (!chr) {
|
2916 | 5867c88a | ths | qemu_free(drv); |
2917 | e57a8c0e | bellard | close(fd); |
2918 | e57a8c0e | bellard | return NULL; |
2919 | e57a8c0e | bellard | } |
2920 | e57a8c0e | bellard | chr->chr_write = null_chr_write; |
2921 | e57a8c0e | bellard | chr->chr_ioctl = pp_ioctl; |
2922 | 5867c88a | ths | chr->chr_close = pp_close; |
2923 | 5867c88a | ths | chr->opaque = drv; |
2924 | 86e94dea | ths | |
2925 | 86e94dea | ths | qemu_chr_reset(chr); |
2926 | 86e94dea | ths | |
2927 | f8d179e3 | bellard | return chr;
|
2928 | f8d179e3 | bellard | } |
2929 | aec62507 | ths | #endif /* __linux__ */ |
2930 | f8d179e3 | bellard | |
2931 | aec62507 | ths | #else /* _WIN32 */ |
2932 | 82c643ff | bellard | |
2933 | f331110f | bellard | typedef struct { |
2934 | f331110f | bellard | int max_size;
|
2935 | f331110f | bellard | HANDLE hcom, hrecv, hsend; |
2936 | f331110f | bellard | OVERLAPPED orecv, osend; |
2937 | f331110f | bellard | BOOL fpipe; |
2938 | f331110f | bellard | DWORD len; |
2939 | f331110f | bellard | } WinCharState; |
2940 | f331110f | bellard | |
2941 | f331110f | bellard | #define NSENDBUF 2048 |
2942 | f331110f | bellard | #define NRECVBUF 2048 |
2943 | f331110f | bellard | #define MAXCONNECT 1 |
2944 | f331110f | bellard | #define NTIMEOUT 5000 |
2945 | f331110f | bellard | |
2946 | f331110f | bellard | static int win_chr_poll(void *opaque); |
2947 | f331110f | bellard | static int win_chr_pipe_poll(void *opaque); |
2948 | f331110f | bellard | |
2949 | 087f4ae0 | ths | static void win_chr_close(CharDriverState *chr) |
2950 | f331110f | bellard | { |
2951 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2952 | 087f4ae0 | ths | |
2953 | f331110f | bellard | if (s->hsend) {
|
2954 | f331110f | bellard | CloseHandle(s->hsend); |
2955 | f331110f | bellard | s->hsend = NULL;
|
2956 | f331110f | bellard | } |
2957 | f331110f | bellard | if (s->hrecv) {
|
2958 | f331110f | bellard | CloseHandle(s->hrecv); |
2959 | f331110f | bellard | s->hrecv = NULL;
|
2960 | f331110f | bellard | } |
2961 | f331110f | bellard | if (s->hcom) {
|
2962 | f331110f | bellard | CloseHandle(s->hcom); |
2963 | f331110f | bellard | s->hcom = NULL;
|
2964 | f331110f | bellard | } |
2965 | f331110f | bellard | if (s->fpipe)
|
2966 | 087f4ae0 | ths | qemu_del_polling_cb(win_chr_pipe_poll, chr); |
2967 | f331110f | bellard | else
|
2968 | 087f4ae0 | ths | qemu_del_polling_cb(win_chr_poll, chr); |
2969 | f331110f | bellard | } |
2970 | f331110f | bellard | |
2971 | 087f4ae0 | ths | static int win_chr_init(CharDriverState *chr, const char *filename) |
2972 | f331110f | bellard | { |
2973 | f331110f | bellard | WinCharState *s = chr->opaque; |
2974 | f331110f | bellard | COMMCONFIG comcfg; |
2975 | f331110f | bellard | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
2976 | f331110f | bellard | COMSTAT comstat; |
2977 | f331110f | bellard | DWORD size; |
2978 | f331110f | bellard | DWORD err; |
2979 | 3b46e624 | ths | |
2980 | f331110f | bellard | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
2981 | f331110f | bellard | if (!s->hsend) {
|
2982 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
2983 | f331110f | bellard | goto fail;
|
2984 | f331110f | bellard | } |
2985 | f331110f | bellard | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
2986 | f331110f | bellard | if (!s->hrecv) {
|
2987 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
2988 | f331110f | bellard | goto fail;
|
2989 | f331110f | bellard | } |
2990 | f331110f | bellard | |
2991 | f331110f | bellard | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
2992 | f331110f | bellard | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
|
2993 | f331110f | bellard | if (s->hcom == INVALID_HANDLE_VALUE) {
|
2994 | f331110f | bellard | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
|
2995 | f331110f | bellard | s->hcom = NULL;
|
2996 | f331110f | bellard | goto fail;
|
2997 | f331110f | bellard | } |
2998 | 3b46e624 | ths | |
2999 | f331110f | bellard | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
|
3000 | f331110f | bellard | fprintf(stderr, "Failed SetupComm\n");
|
3001 | f331110f | bellard | goto fail;
|
3002 | f331110f | bellard | } |
3003 | 3b46e624 | ths | |
3004 | f331110f | bellard | ZeroMemory(&comcfg, sizeof(COMMCONFIG));
|
3005 | f331110f | bellard | size = sizeof(COMMCONFIG);
|
3006 | f331110f | bellard | GetDefaultCommConfig(filename, &comcfg, &size); |
3007 | f331110f | bellard | comcfg.dcb.DCBlength = sizeof(DCB);
|
3008 | f331110f | bellard | CommConfigDialog(filename, NULL, &comcfg);
|
3009 | f331110f | bellard | |
3010 | f331110f | bellard | if (!SetCommState(s->hcom, &comcfg.dcb)) {
|
3011 | f331110f | bellard | fprintf(stderr, "Failed SetCommState\n");
|
3012 | f331110f | bellard | goto fail;
|
3013 | f331110f | bellard | } |
3014 | f331110f | bellard | |
3015 | f331110f | bellard | if (!SetCommMask(s->hcom, EV_ERR)) {
|
3016 | f331110f | bellard | fprintf(stderr, "Failed SetCommMask\n");
|
3017 | f331110f | bellard | goto fail;
|
3018 | f331110f | bellard | } |
3019 | f331110f | bellard | |
3020 | f331110f | bellard | cto.ReadIntervalTimeout = MAXDWORD; |
3021 | f331110f | bellard | if (!SetCommTimeouts(s->hcom, &cto)) {
|
3022 | f331110f | bellard | fprintf(stderr, "Failed SetCommTimeouts\n");
|
3023 | f331110f | bellard | goto fail;
|
3024 | f331110f | bellard | } |
3025 | 3b46e624 | ths | |
3026 | f331110f | bellard | if (!ClearCommError(s->hcom, &err, &comstat)) {
|
3027 | f331110f | bellard | fprintf(stderr, "Failed ClearCommError\n");
|
3028 | f331110f | bellard | goto fail;
|
3029 | f331110f | bellard | } |
3030 | 087f4ae0 | ths | qemu_add_polling_cb(win_chr_poll, chr); |
3031 | f331110f | bellard | return 0; |
3032 | f331110f | bellard | |
3033 | f331110f | bellard | fail:
|
3034 | 087f4ae0 | ths | win_chr_close(chr); |
3035 | f331110f | bellard | return -1; |
3036 | f331110f | bellard | } |
3037 | f331110f | bellard | |
3038 | f331110f | bellard | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
3039 | f331110f | bellard | { |
3040 | f331110f | bellard | WinCharState *s = chr->opaque; |
3041 | f331110f | bellard | DWORD len, ret, size, err; |
3042 | f331110f | bellard | |
3043 | f331110f | bellard | len = len1; |
3044 | f331110f | bellard | ZeroMemory(&s->osend, sizeof(s->osend));
|
3045 | f331110f | bellard | s->osend.hEvent = s->hsend; |
3046 | f331110f | bellard | while (len > 0) { |
3047 | f331110f | bellard | if (s->hsend)
|
3048 | f331110f | bellard | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
3049 | f331110f | bellard | else
|
3050 | f331110f | bellard | ret = WriteFile(s->hcom, buf, len, &size, NULL);
|
3051 | f331110f | bellard | if (!ret) {
|
3052 | f331110f | bellard | err = GetLastError(); |
3053 | f331110f | bellard | if (err == ERROR_IO_PENDING) {
|
3054 | f331110f | bellard | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
3055 | f331110f | bellard | if (ret) {
|
3056 | f331110f | bellard | buf += size; |
3057 | f331110f | bellard | len -= size; |
3058 | f331110f | bellard | } else {
|
3059 | f331110f | bellard | break;
|
3060 | f331110f | bellard | } |
3061 | f331110f | bellard | } else {
|
3062 | f331110f | bellard | break;
|
3063 | f331110f | bellard | } |
3064 | f331110f | bellard | } else {
|
3065 | f331110f | bellard | buf += size; |
3066 | f331110f | bellard | len -= size; |
3067 | f331110f | bellard | } |
3068 | f331110f | bellard | } |
3069 | f331110f | bellard | return len1 - len;
|
3070 | f331110f | bellard | } |
3071 | f331110f | bellard | |
3072 | 087f4ae0 | ths | static int win_chr_read_poll(CharDriverState *chr) |
3073 | f331110f | bellard | { |
3074 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3075 | 087f4ae0 | ths | |
3076 | 087f4ae0 | ths | s->max_size = qemu_chr_can_read(chr); |
3077 | f331110f | bellard | return s->max_size;
|
3078 | f331110f | bellard | } |
3079 | e5b0bc44 | pbrook | |
3080 | 087f4ae0 | ths | static void win_chr_readfile(CharDriverState *chr) |
3081 | f331110f | bellard | { |
3082 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3083 | f331110f | bellard | int ret, err;
|
3084 | f331110f | bellard | uint8_t buf[1024];
|
3085 | f331110f | bellard | DWORD size; |
3086 | 3b46e624 | ths | |
3087 | f331110f | bellard | ZeroMemory(&s->orecv, sizeof(s->orecv));
|
3088 | f331110f | bellard | s->orecv.hEvent = s->hrecv; |
3089 | f331110f | bellard | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
3090 | f331110f | bellard | if (!ret) {
|
3091 | f331110f | bellard | err = GetLastError(); |
3092 | f331110f | bellard | if (err == ERROR_IO_PENDING) {
|
3093 | f331110f | bellard | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
3094 | f331110f | bellard | } |
3095 | f331110f | bellard | } |
3096 | f331110f | bellard | |
3097 | f331110f | bellard | if (size > 0) { |
3098 | 087f4ae0 | ths | qemu_chr_read(chr, buf, size); |
3099 | f331110f | bellard | } |
3100 | f331110f | bellard | } |
3101 | f331110f | bellard | |
3102 | 087f4ae0 | ths | static void win_chr_read(CharDriverState *chr) |
3103 | f331110f | bellard | { |
3104 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3105 | 087f4ae0 | ths | |
3106 | f331110f | bellard | if (s->len > s->max_size)
|
3107 | f331110f | bellard | s->len = s->max_size; |
3108 | f331110f | bellard | if (s->len == 0) |
3109 | f331110f | bellard | return;
|
3110 | 3b46e624 | ths | |
3111 | 087f4ae0 | ths | win_chr_readfile(chr); |
3112 | f331110f | bellard | } |
3113 | f331110f | bellard | |
3114 | f331110f | bellard | static int win_chr_poll(void *opaque) |
3115 | f331110f | bellard | { |
3116 | 087f4ae0 | ths | CharDriverState *chr = opaque; |
3117 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3118 | f331110f | bellard | COMSTAT status; |
3119 | f331110f | bellard | DWORD comerr; |
3120 | 3b46e624 | ths | |
3121 | f331110f | bellard | ClearCommError(s->hcom, &comerr, &status); |
3122 | f331110f | bellard | if (status.cbInQue > 0) { |
3123 | f331110f | bellard | s->len = status.cbInQue; |
3124 | 087f4ae0 | ths | win_chr_read_poll(chr); |
3125 | 087f4ae0 | ths | win_chr_read(chr); |
3126 | f331110f | bellard | return 1; |
3127 | f331110f | bellard | } |
3128 | f331110f | bellard | return 0; |
3129 | f331110f | bellard | } |
3130 | f331110f | bellard | |
3131 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win(const char *filename) |
3132 | f331110f | bellard | { |
3133 | f331110f | bellard | CharDriverState *chr; |
3134 | f331110f | bellard | WinCharState *s; |
3135 | 3b46e624 | ths | |
3136 | f331110f | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3137 | f331110f | bellard | if (!chr)
|
3138 | f331110f | bellard | return NULL; |
3139 | f331110f | bellard | s = qemu_mallocz(sizeof(WinCharState));
|
3140 | f331110f | bellard | if (!s) {
|
3141 | f331110f | bellard | free(chr); |
3142 | f331110f | bellard | return NULL; |
3143 | f331110f | bellard | } |
3144 | f331110f | bellard | chr->opaque = s; |
3145 | f331110f | bellard | chr->chr_write = win_chr_write; |
3146 | f331110f | bellard | chr->chr_close = win_chr_close; |
3147 | f331110f | bellard | |
3148 | 087f4ae0 | ths | if (win_chr_init(chr, filename) < 0) { |
3149 | f331110f | bellard | free(s); |
3150 | f331110f | bellard | free(chr); |
3151 | f331110f | bellard | return NULL; |
3152 | f331110f | bellard | } |
3153 | 86e94dea | ths | qemu_chr_reset(chr); |
3154 | f331110f | bellard | return chr;
|
3155 | f331110f | bellard | } |
3156 | f331110f | bellard | |
3157 | f331110f | bellard | static int win_chr_pipe_poll(void *opaque) |
3158 | f331110f | bellard | { |
3159 | 087f4ae0 | ths | CharDriverState *chr = opaque; |
3160 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3161 | f331110f | bellard | DWORD size; |
3162 | f331110f | bellard | |
3163 | f331110f | bellard | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
3164 | f331110f | bellard | if (size > 0) { |
3165 | f331110f | bellard | s->len = size; |
3166 | 087f4ae0 | ths | win_chr_read_poll(chr); |
3167 | 087f4ae0 | ths | win_chr_read(chr); |
3168 | f331110f | bellard | return 1; |
3169 | f331110f | bellard | } |
3170 | f331110f | bellard | return 0; |
3171 | f331110f | bellard | } |
3172 | f331110f | bellard | |
3173 | 087f4ae0 | ths | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) |
3174 | f331110f | bellard | { |
3175 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3176 | f331110f | bellard | OVERLAPPED ov; |
3177 | f331110f | bellard | int ret;
|
3178 | f331110f | bellard | DWORD size; |
3179 | f331110f | bellard | char openname[256]; |
3180 | 3b46e624 | ths | |
3181 | f331110f | bellard | s->fpipe = TRUE; |
3182 | f331110f | bellard | |
3183 | f331110f | bellard | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
3184 | f331110f | bellard | if (!s->hsend) {
|
3185 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
3186 | f331110f | bellard | goto fail;
|
3187 | f331110f | bellard | } |
3188 | f331110f | bellard | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
3189 | f331110f | bellard | if (!s->hrecv) {
|
3190 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
3191 | f331110f | bellard | goto fail;
|
3192 | f331110f | bellard | } |
3193 | 3b46e624 | ths | |
3194 | f331110f | bellard | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
3195 | f331110f | bellard | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
3196 | f331110f | bellard | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
3197 | f331110f | bellard | PIPE_WAIT, |
3198 | f331110f | bellard | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
|
3199 | f331110f | bellard | if (s->hcom == INVALID_HANDLE_VALUE) {
|
3200 | f331110f | bellard | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
|
3201 | f331110f | bellard | s->hcom = NULL;
|
3202 | f331110f | bellard | goto fail;
|
3203 | f331110f | bellard | } |
3204 | f331110f | bellard | |
3205 | f331110f | bellard | ZeroMemory(&ov, sizeof(ov));
|
3206 | f331110f | bellard | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
3207 | f331110f | bellard | ret = ConnectNamedPipe(s->hcom, &ov); |
3208 | f331110f | bellard | if (ret) {
|
3209 | f331110f | bellard | fprintf(stderr, "Failed ConnectNamedPipe\n");
|
3210 | f331110f | bellard | goto fail;
|
3211 | f331110f | bellard | } |
3212 | f331110f | bellard | |
3213 | f331110f | bellard | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
3214 | f331110f | bellard | if (!ret) {
|
3215 | f331110f | bellard | fprintf(stderr, "Failed GetOverlappedResult\n");
|
3216 | f331110f | bellard | if (ov.hEvent) {
|
3217 | f331110f | bellard | CloseHandle(ov.hEvent); |
3218 | f331110f | bellard | ov.hEvent = NULL;
|
3219 | f331110f | bellard | } |
3220 | f331110f | bellard | goto fail;
|
3221 | f331110f | bellard | } |
3222 | f331110f | bellard | |
3223 | f331110f | bellard | if (ov.hEvent) {
|
3224 | f331110f | bellard | CloseHandle(ov.hEvent); |
3225 | f331110f | bellard | ov.hEvent = NULL;
|
3226 | f331110f | bellard | } |
3227 | 087f4ae0 | ths | qemu_add_polling_cb(win_chr_pipe_poll, chr); |
3228 | f331110f | bellard | return 0; |
3229 | f331110f | bellard | |
3230 | f331110f | bellard | fail:
|
3231 | 087f4ae0 | ths | win_chr_close(chr); |
3232 | f331110f | bellard | return -1; |
3233 | f331110f | bellard | } |
3234 | f331110f | bellard | |
3235 | f331110f | bellard | |
3236 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win_pipe(const char *filename) |
3237 | f331110f | bellard | { |
3238 | f331110f | bellard | CharDriverState *chr; |
3239 | f331110f | bellard | WinCharState *s; |
3240 | f331110f | bellard | |
3241 | f331110f | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3242 | f331110f | bellard | if (!chr)
|
3243 | f331110f | bellard | return NULL; |
3244 | f331110f | bellard | s = qemu_mallocz(sizeof(WinCharState));
|
3245 | f331110f | bellard | if (!s) {
|
3246 | f331110f | bellard | free(chr); |
3247 | f331110f | bellard | return NULL; |
3248 | f331110f | bellard | } |
3249 | f331110f | bellard | chr->opaque = s; |
3250 | f331110f | bellard | chr->chr_write = win_chr_write; |
3251 | f331110f | bellard | chr->chr_close = win_chr_close; |
3252 | 3b46e624 | ths | |
3253 | 087f4ae0 | ths | if (win_chr_pipe_init(chr, filename) < 0) { |
3254 | f331110f | bellard | free(s); |
3255 | f331110f | bellard | free(chr); |
3256 | f331110f | bellard | return NULL; |
3257 | f331110f | bellard | } |
3258 | 86e94dea | ths | qemu_chr_reset(chr); |
3259 | f331110f | bellard | return chr;
|
3260 | f331110f | bellard | } |
3261 | f331110f | bellard | |
3262 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
|
3263 | f331110f | bellard | { |
3264 | f331110f | bellard | CharDriverState *chr; |
3265 | f331110f | bellard | WinCharState *s; |
3266 | f331110f | bellard | |
3267 | f331110f | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3268 | f331110f | bellard | if (!chr)
|
3269 | f331110f | bellard | return NULL; |
3270 | f331110f | bellard | s = qemu_mallocz(sizeof(WinCharState));
|
3271 | f331110f | bellard | if (!s) {
|
3272 | f331110f | bellard | free(chr); |
3273 | f331110f | bellard | return NULL; |
3274 | f331110f | bellard | } |
3275 | f331110f | bellard | s->hcom = fd_out; |
3276 | f331110f | bellard | chr->opaque = s; |
3277 | f331110f | bellard | chr->chr_write = win_chr_write; |
3278 | 86e94dea | ths | qemu_chr_reset(chr); |
3279 | f331110f | bellard | return chr;
|
3280 | f331110f | bellard | } |
3281 | 72d46479 | ths | |
3282 | 72d46479 | ths | static CharDriverState *qemu_chr_open_win_con(const char *filename) |
3283 | 72d46479 | ths | { |
3284 | 72d46479 | ths | return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
|
3285 | 72d46479 | ths | } |
3286 | 72d46479 | ths | |
3287 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win_file_out(const char *file_out) |
3288 | f331110f | bellard | { |
3289 | f331110f | bellard | HANDLE fd_out; |
3290 | 3b46e624 | ths | |
3291 | f331110f | bellard | fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
|
3292 | f331110f | bellard | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
3293 | f331110f | bellard | if (fd_out == INVALID_HANDLE_VALUE)
|
3294 | f331110f | bellard | return NULL; |
3295 | f331110f | bellard | |
3296 | f331110f | bellard | return qemu_chr_open_win_file(fd_out);
|
3297 | f331110f | bellard | } |
3298 | aec62507 | ths | #endif /* !_WIN32 */ |
3299 | f331110f | bellard | |
3300 | 0bab00f3 | bellard | /***********************************************************/
|
3301 | 0bab00f3 | bellard | /* UDP Net console */
|
3302 | 0bab00f3 | bellard | |
3303 | 0bab00f3 | bellard | typedef struct { |
3304 | 0bab00f3 | bellard | int fd;
|
3305 | 0bab00f3 | bellard | struct sockaddr_in daddr;
|
3306 | 60fe76f3 | ths | uint8_t buf[1024];
|
3307 | 0bab00f3 | bellard | int bufcnt;
|
3308 | 0bab00f3 | bellard | int bufptr;
|
3309 | 0bab00f3 | bellard | int max_size;
|
3310 | 0bab00f3 | bellard | } NetCharDriver; |
3311 | 0bab00f3 | bellard | |
3312 | 0bab00f3 | bellard | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
3313 | 0bab00f3 | bellard | { |
3314 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3315 | 0bab00f3 | bellard | |
3316 | 0bab00f3 | bellard | return sendto(s->fd, buf, len, 0, |
3317 | 0bab00f3 | bellard | (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in)); |
3318 | 0bab00f3 | bellard | } |
3319 | 0bab00f3 | bellard | |
3320 | 0bab00f3 | bellard | static int udp_chr_read_poll(void *opaque) |
3321 | 0bab00f3 | bellard | { |
3322 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3323 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3324 | 0bab00f3 | bellard | |
3325 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3326 | 0bab00f3 | bellard | |
3327 | 0bab00f3 | bellard | /* If there were any stray characters in the queue process them
|
3328 | 0bab00f3 | bellard | * first
|
3329 | 0bab00f3 | bellard | */
|
3330 | 0bab00f3 | bellard | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
3331 | e5b0bc44 | pbrook | qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
3332 | 0bab00f3 | bellard | s->bufptr++; |
3333 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3334 | 0bab00f3 | bellard | } |
3335 | 0bab00f3 | bellard | return s->max_size;
|
3336 | 0bab00f3 | bellard | } |
3337 | 0bab00f3 | bellard | |
3338 | 0bab00f3 | bellard | static void udp_chr_read(void *opaque) |
3339 | 0bab00f3 | bellard | { |
3340 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3341 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3342 | 0bab00f3 | bellard | |
3343 | 0bab00f3 | bellard | if (s->max_size == 0) |
3344 | 0bab00f3 | bellard | return;
|
3345 | 0bab00f3 | bellard | s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0); |
3346 | 0bab00f3 | bellard | s->bufptr = s->bufcnt; |
3347 | 0bab00f3 | bellard | if (s->bufcnt <= 0) |
3348 | 0bab00f3 | bellard | return;
|
3349 | 0bab00f3 | bellard | |
3350 | 0bab00f3 | bellard | s->bufptr = 0;
|
3351 | 0bab00f3 | bellard | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
3352 | e5b0bc44 | pbrook | qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
3353 | 0bab00f3 | bellard | s->bufptr++; |
3354 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3355 | 0bab00f3 | bellard | } |
3356 | 0bab00f3 | bellard | } |
3357 | 0bab00f3 | bellard | |
3358 | e5b0bc44 | pbrook | static void udp_chr_update_read_handler(CharDriverState *chr) |
3359 | 0bab00f3 | bellard | { |
3360 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3361 | 0bab00f3 | bellard | |
3362 | 0bab00f3 | bellard | if (s->fd >= 0) { |
3363 | 0bab00f3 | bellard | qemu_set_fd_handler2(s->fd, udp_chr_read_poll, |
3364 | 0bab00f3 | bellard | udp_chr_read, NULL, chr);
|
3365 | 0bab00f3 | bellard | } |
3366 | 0bab00f3 | bellard | } |
3367 | 0bab00f3 | bellard | |
3368 | 0bab00f3 | bellard | int parse_host_port(struct sockaddr_in *saddr, const char *str); |
3369 | 52f61fde | ths | #ifndef _WIN32
|
3370 | 52f61fde | ths | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str); |
3371 | 52f61fde | ths | #endif
|
3372 | 951f1351 | bellard | int parse_host_src_port(struct sockaddr_in *haddr, |
3373 | 951f1351 | bellard | struct sockaddr_in *saddr,
|
3374 | 951f1351 | bellard | const char *str); |
3375 | 0bab00f3 | bellard | |
3376 | 52f61fde | ths | static CharDriverState *qemu_chr_open_udp(const char *def) |
3377 | 0bab00f3 | bellard | { |
3378 | 0bab00f3 | bellard | CharDriverState *chr = NULL;
|
3379 | 0bab00f3 | bellard | NetCharDriver *s = NULL;
|
3380 | 0bab00f3 | bellard | int fd = -1; |
3381 | 951f1351 | bellard | struct sockaddr_in saddr;
|
3382 | 0bab00f3 | bellard | |
3383 | 0bab00f3 | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3384 | 0bab00f3 | bellard | if (!chr)
|
3385 | 0bab00f3 | bellard | goto return_err;
|
3386 | 0bab00f3 | bellard | s = qemu_mallocz(sizeof(NetCharDriver));
|
3387 | 0bab00f3 | bellard | if (!s)
|
3388 | 0bab00f3 | bellard | goto return_err;
|
3389 | 0bab00f3 | bellard | |
3390 | 0bab00f3 | bellard | fd = socket(PF_INET, SOCK_DGRAM, 0);
|
3391 | 0bab00f3 | bellard | if (fd < 0) { |
3392 | 0bab00f3 | bellard | perror("socket(PF_INET, SOCK_DGRAM)");
|
3393 | 0bab00f3 | bellard | goto return_err;
|
3394 | 0bab00f3 | bellard | } |
3395 | 0bab00f3 | bellard | |
3396 | 951f1351 | bellard | if (parse_host_src_port(&s->daddr, &saddr, def) < 0) { |
3397 | 951f1351 | bellard | printf("Could not parse: %s\n", def);
|
3398 | 951f1351 | bellard | goto return_err;
|
3399 | 0bab00f3 | bellard | } |
3400 | 0bab00f3 | bellard | |
3401 | 951f1351 | bellard | if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) |
3402 | 0bab00f3 | bellard | { |
3403 | 0bab00f3 | bellard | perror("bind");
|
3404 | 0bab00f3 | bellard | goto return_err;
|
3405 | 0bab00f3 | bellard | } |
3406 | 0bab00f3 | bellard | |
3407 | 0bab00f3 | bellard | s->fd = fd; |
3408 | 0bab00f3 | bellard | s->bufcnt = 0;
|
3409 | 0bab00f3 | bellard | s->bufptr = 0;
|
3410 | 0bab00f3 | bellard | chr->opaque = s; |
3411 | 0bab00f3 | bellard | chr->chr_write = udp_chr_write; |
3412 | e5b0bc44 | pbrook | chr->chr_update_read_handler = udp_chr_update_read_handler; |
3413 | 0bab00f3 | bellard | return chr;
|
3414 | 0bab00f3 | bellard | |
3415 | 0bab00f3 | bellard | return_err:
|
3416 | 0bab00f3 | bellard | if (chr)
|
3417 | 0bab00f3 | bellard | free(chr); |
3418 | 0bab00f3 | bellard | if (s)
|
3419 | 0bab00f3 | bellard | free(s); |
3420 | 0bab00f3 | bellard | if (fd >= 0) |
3421 | 0bab00f3 | bellard | closesocket(fd); |
3422 | 0bab00f3 | bellard | return NULL; |
3423 | 0bab00f3 | bellard | } |
3424 | 0bab00f3 | bellard | |
3425 | 0bab00f3 | bellard | /***********************************************************/
|
3426 | 0bab00f3 | bellard | /* TCP Net console */
|
3427 | 0bab00f3 | bellard | |
3428 | 0bab00f3 | bellard | typedef struct { |
3429 | 0bab00f3 | bellard | int fd, listen_fd;
|
3430 | 0bab00f3 | bellard | int connected;
|
3431 | 0bab00f3 | bellard | int max_size;
|
3432 | 951f1351 | bellard | int do_telnetopt;
|
3433 | e5b0bc44 | pbrook | int do_nodelay;
|
3434 | ffd843bc | ths | int is_unix;
|
3435 | 0bab00f3 | bellard | } TCPCharDriver; |
3436 | 0bab00f3 | bellard | |
3437 | 0bab00f3 | bellard | static void tcp_chr_accept(void *opaque); |
3438 | 0bab00f3 | bellard | |
3439 | 0bab00f3 | bellard | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
3440 | 0bab00f3 | bellard | { |
3441 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3442 | 0bab00f3 | bellard | if (s->connected) {
|
3443 | 0bab00f3 | bellard | return send_all(s->fd, buf, len);
|
3444 | 0bab00f3 | bellard | } else {
|
3445 | 0bab00f3 | bellard | /* XXX: indicate an error ? */
|
3446 | 0bab00f3 | bellard | return len;
|
3447 | 0bab00f3 | bellard | } |
3448 | 0bab00f3 | bellard | } |
3449 | 0bab00f3 | bellard | |
3450 | 0bab00f3 | bellard | static int tcp_chr_read_poll(void *opaque) |
3451 | 0bab00f3 | bellard | { |
3452 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3453 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3454 | 0bab00f3 | bellard | if (!s->connected)
|
3455 | 0bab00f3 | bellard | return 0; |
3456 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3457 | 0bab00f3 | bellard | return s->max_size;
|
3458 | 0bab00f3 | bellard | } |
3459 | 0bab00f3 | bellard | |
3460 | 951f1351 | bellard | #define IAC 255 |
3461 | 951f1351 | bellard | #define IAC_BREAK 243 |
3462 | 951f1351 | bellard | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
3463 | 951f1351 | bellard | TCPCharDriver *s, |
3464 | 60fe76f3 | ths | uint8_t *buf, int *size)
|
3465 | 951f1351 | bellard | { |
3466 | 951f1351 | bellard | /* Handle any telnet client's basic IAC options to satisfy char by
|
3467 | 951f1351 | bellard | * char mode with no echo. All IAC options will be removed from
|
3468 | 951f1351 | bellard | * the buf and the do_telnetopt variable will be used to track the
|
3469 | 951f1351 | bellard | * state of the width of the IAC information.
|
3470 | 951f1351 | bellard | *
|
3471 | 951f1351 | bellard | * IAC commands come in sets of 3 bytes with the exception of the
|
3472 | 951f1351 | bellard | * "IAC BREAK" command and the double IAC.
|
3473 | 951f1351 | bellard | */
|
3474 | 951f1351 | bellard | |
3475 | 951f1351 | bellard | int i;
|
3476 | 951f1351 | bellard | int j = 0; |
3477 | 951f1351 | bellard | |
3478 | 951f1351 | bellard | for (i = 0; i < *size; i++) { |
3479 | 951f1351 | bellard | if (s->do_telnetopt > 1) { |
3480 | 951f1351 | bellard | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
3481 | 951f1351 | bellard | /* Double IAC means send an IAC */
|
3482 | 951f1351 | bellard | if (j != i)
|
3483 | 951f1351 | bellard | buf[j] = buf[i]; |
3484 | 951f1351 | bellard | j++; |
3485 | 951f1351 | bellard | s->do_telnetopt = 1;
|
3486 | 951f1351 | bellard | } else {
|
3487 | 951f1351 | bellard | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
3488 | 951f1351 | bellard | /* Handle IAC break commands by sending a serial break */
|
3489 | e5b0bc44 | pbrook | qemu_chr_event(chr, CHR_EVENT_BREAK); |
3490 | 951f1351 | bellard | s->do_telnetopt++; |
3491 | 951f1351 | bellard | } |
3492 | 951f1351 | bellard | s->do_telnetopt++; |
3493 | 951f1351 | bellard | } |
3494 | 951f1351 | bellard | if (s->do_telnetopt >= 4) { |
3495 | 951f1351 | bellard | s->do_telnetopt = 1;
|
3496 | 951f1351 | bellard | } |
3497 | 951f1351 | bellard | } else {
|
3498 | 951f1351 | bellard | if ((unsigned char)buf[i] == IAC) { |
3499 | 951f1351 | bellard | s->do_telnetopt = 2;
|
3500 | 951f1351 | bellard | } else {
|
3501 | 951f1351 | bellard | if (j != i)
|
3502 | 951f1351 | bellard | buf[j] = buf[i]; |
3503 | 951f1351 | bellard | j++; |
3504 | 951f1351 | bellard | } |
3505 | 951f1351 | bellard | } |
3506 | 951f1351 | bellard | } |
3507 | 951f1351 | bellard | *size = j; |
3508 | 951f1351 | bellard | } |
3509 | 951f1351 | bellard | |
3510 | 0bab00f3 | bellard | static void tcp_chr_read(void *opaque) |
3511 | 0bab00f3 | bellard | { |
3512 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3513 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3514 | 0bab00f3 | bellard | uint8_t buf[1024];
|
3515 | 0bab00f3 | bellard | int len, size;
|
3516 | 0bab00f3 | bellard | |
3517 | 0bab00f3 | bellard | if (!s->connected || s->max_size <= 0) |
3518 | 0bab00f3 | bellard | return;
|
3519 | 0bab00f3 | bellard | len = sizeof(buf);
|
3520 | 0bab00f3 | bellard | if (len > s->max_size)
|
3521 | 0bab00f3 | bellard | len = s->max_size; |
3522 | 0bab00f3 | bellard | size = recv(s->fd, buf, len, 0);
|
3523 | 0bab00f3 | bellard | if (size == 0) { |
3524 | 0bab00f3 | bellard | /* connection closed */
|
3525 | 0bab00f3 | bellard | s->connected = 0;
|
3526 | 0bab00f3 | bellard | if (s->listen_fd >= 0) { |
3527 | 0bab00f3 | bellard | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
3528 | 0bab00f3 | bellard | } |
3529 | 0bab00f3 | bellard | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
3530 | 0bab00f3 | bellard | closesocket(s->fd); |
3531 | 0bab00f3 | bellard | s->fd = -1;
|
3532 | 0bab00f3 | bellard | } else if (size > 0) { |
3533 | 951f1351 | bellard | if (s->do_telnetopt)
|
3534 | 951f1351 | bellard | tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
3535 | 951f1351 | bellard | if (size > 0) |
3536 | e5b0bc44 | pbrook | qemu_chr_read(chr, buf, size); |
3537 | 0bab00f3 | bellard | } |
3538 | 0bab00f3 | bellard | } |
3539 | 0bab00f3 | bellard | |
3540 | 0bab00f3 | bellard | static void tcp_chr_connect(void *opaque) |
3541 | 0bab00f3 | bellard | { |
3542 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3543 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3544 | 0bab00f3 | bellard | |
3545 | 0bab00f3 | bellard | s->connected = 1;
|
3546 | 0bab00f3 | bellard | qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, |
3547 | 0bab00f3 | bellard | tcp_chr_read, NULL, chr);
|
3548 | 86e94dea | ths | qemu_chr_reset(chr); |
3549 | 0bab00f3 | bellard | } |
3550 | 0bab00f3 | bellard | |
3551 | 951f1351 | bellard | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
3552 | 951f1351 | bellard | static void tcp_chr_telnet_init(int fd) |
3553 | 951f1351 | bellard | { |
3554 | 951f1351 | bellard | char buf[3]; |
3555 | 951f1351 | bellard | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
|
3556 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
3557 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3558 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
3559 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3560 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
3561 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3562 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
3563 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3564 | 951f1351 | bellard | } |
3565 | 951f1351 | bellard | |
3566 | f7499989 | pbrook | static void socket_set_nodelay(int fd) |
3567 | f7499989 | pbrook | { |
3568 | f7499989 | pbrook | int val = 1; |
3569 | f7499989 | pbrook | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
3570 | f7499989 | pbrook | } |
3571 | f7499989 | pbrook | |
3572 | 0bab00f3 | bellard | static void tcp_chr_accept(void *opaque) |
3573 | 0bab00f3 | bellard | { |
3574 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3575 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3576 | 0bab00f3 | bellard | struct sockaddr_in saddr;
|
3577 | ffd843bc | ths | #ifndef _WIN32
|
3578 | ffd843bc | ths | struct sockaddr_un uaddr;
|
3579 | ffd843bc | ths | #endif
|
3580 | ffd843bc | ths | struct sockaddr *addr;
|
3581 | 0bab00f3 | bellard | socklen_t len; |
3582 | 0bab00f3 | bellard | int fd;
|
3583 | 0bab00f3 | bellard | |
3584 | 0bab00f3 | bellard | for(;;) {
|
3585 | ffd843bc | ths | #ifndef _WIN32
|
3586 | ffd843bc | ths | if (s->is_unix) {
|
3587 | ffd843bc | ths | len = sizeof(uaddr);
|
3588 | ffd843bc | ths | addr = (struct sockaddr *)&uaddr;
|
3589 | ffd843bc | ths | } else
|
3590 | ffd843bc | ths | #endif
|
3591 | ffd843bc | ths | { |
3592 | ffd843bc | ths | len = sizeof(saddr);
|
3593 | ffd843bc | ths | addr = (struct sockaddr *)&saddr;
|
3594 | ffd843bc | ths | } |
3595 | ffd843bc | ths | fd = accept(s->listen_fd, addr, &len); |
3596 | 0bab00f3 | bellard | if (fd < 0 && errno != EINTR) { |
3597 | 0bab00f3 | bellard | return;
|
3598 | 0bab00f3 | bellard | } else if (fd >= 0) { |
3599 | 951f1351 | bellard | if (s->do_telnetopt)
|
3600 | 951f1351 | bellard | tcp_chr_telnet_init(fd); |
3601 | 0bab00f3 | bellard | break;
|
3602 | 0bab00f3 | bellard | } |
3603 | 0bab00f3 | bellard | } |
3604 | 0bab00f3 | bellard | socket_set_nonblock(fd); |
3605 | f7499989 | pbrook | if (s->do_nodelay)
|
3606 | f7499989 | pbrook | socket_set_nodelay(fd); |
3607 | 0bab00f3 | bellard | s->fd = fd; |
3608 | 0bab00f3 | bellard | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
3609 | 0bab00f3 | bellard | tcp_chr_connect(chr); |
3610 | 0bab00f3 | bellard | } |
3611 | 0bab00f3 | bellard | |
3612 | 0bab00f3 | bellard | static void tcp_chr_close(CharDriverState *chr) |
3613 | 0bab00f3 | bellard | { |
3614 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3615 | 0bab00f3 | bellard | if (s->fd >= 0) |
3616 | 0bab00f3 | bellard | closesocket(s->fd); |
3617 | 0bab00f3 | bellard | if (s->listen_fd >= 0) |
3618 | 0bab00f3 | bellard | closesocket(s->listen_fd); |
3619 | 0bab00f3 | bellard | qemu_free(s); |
3620 | 0bab00f3 | bellard | } |
3621 | 0bab00f3 | bellard | |
3622 | 5fafdf24 | ths | static CharDriverState *qemu_chr_open_tcp(const char *host_str, |
3623 | ffd843bc | ths | int is_telnet,
|
3624 | ffd843bc | ths | int is_unix)
|
3625 | 0bab00f3 | bellard | { |
3626 | 0bab00f3 | bellard | CharDriverState *chr = NULL;
|
3627 | 0bab00f3 | bellard | TCPCharDriver *s = NULL;
|
3628 | 0bab00f3 | bellard | int fd = -1, ret, err, val; |
3629 | 951f1351 | bellard | int is_listen = 0; |
3630 | 951f1351 | bellard | int is_waitconnect = 1; |
3631 | f7499989 | pbrook | int do_nodelay = 0; |
3632 | 951f1351 | bellard | const char *ptr; |
3633 | 0bab00f3 | bellard | struct sockaddr_in saddr;
|
3634 | ffd843bc | ths | #ifndef _WIN32
|
3635 | ffd843bc | ths | struct sockaddr_un uaddr;
|
3636 | ffd843bc | ths | #endif
|
3637 | ffd843bc | ths | struct sockaddr *addr;
|
3638 | ffd843bc | ths | socklen_t addrlen; |
3639 | 0bab00f3 | bellard | |
3640 | ffd843bc | ths | #ifndef _WIN32
|
3641 | ffd843bc | ths | if (is_unix) {
|
3642 | ffd843bc | ths | addr = (struct sockaddr *)&uaddr;
|
3643 | ffd843bc | ths | addrlen = sizeof(uaddr);
|
3644 | ffd843bc | ths | if (parse_unix_path(&uaddr, host_str) < 0) |
3645 | ffd843bc | ths | goto fail;
|
3646 | ffd843bc | ths | } else
|
3647 | ffd843bc | ths | #endif
|
3648 | ffd843bc | ths | { |
3649 | ffd843bc | ths | addr = (struct sockaddr *)&saddr;
|
3650 | ffd843bc | ths | addrlen = sizeof(saddr);
|
3651 | ffd843bc | ths | if (parse_host_port(&saddr, host_str) < 0) |
3652 | ffd843bc | ths | goto fail;
|
3653 | ffd843bc | ths | } |
3654 | 0bab00f3 | bellard | |
3655 | 951f1351 | bellard | ptr = host_str; |
3656 | 951f1351 | bellard | while((ptr = strchr(ptr,','))) { |
3657 | 951f1351 | bellard | ptr++; |
3658 | 951f1351 | bellard | if (!strncmp(ptr,"server",6)) { |
3659 | 951f1351 | bellard | is_listen = 1;
|
3660 | 951f1351 | bellard | } else if (!strncmp(ptr,"nowait",6)) { |
3661 | 951f1351 | bellard | is_waitconnect = 0;
|
3662 | f7499989 | pbrook | } else if (!strncmp(ptr,"nodelay",6)) { |
3663 | f7499989 | pbrook | do_nodelay = 1;
|
3664 | 951f1351 | bellard | } else {
|
3665 | 951f1351 | bellard | printf("Unknown option: %s\n", ptr);
|
3666 | 951f1351 | bellard | goto fail;
|
3667 | 951f1351 | bellard | } |
3668 | 951f1351 | bellard | } |
3669 | 951f1351 | bellard | if (!is_listen)
|
3670 | 951f1351 | bellard | is_waitconnect = 0;
|
3671 | 951f1351 | bellard | |
3672 | 0bab00f3 | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3673 | 0bab00f3 | bellard | if (!chr)
|
3674 | 0bab00f3 | bellard | goto fail;
|
3675 | 0bab00f3 | bellard | s = qemu_mallocz(sizeof(TCPCharDriver));
|
3676 | 0bab00f3 | bellard | if (!s)
|
3677 | 0bab00f3 | bellard | goto fail;
|
3678 | ffd843bc | ths | |
3679 | ffd843bc | ths | #ifndef _WIN32
|
3680 | ffd843bc | ths | if (is_unix)
|
3681 | ffd843bc | ths | fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
3682 | ffd843bc | ths | else
|
3683 | ffd843bc | ths | #endif
|
3684 | ffd843bc | ths | fd = socket(PF_INET, SOCK_STREAM, 0);
|
3685 | 5fafdf24 | ths | |
3686 | 5fafdf24 | ths | if (fd < 0) |
3687 | 0bab00f3 | bellard | goto fail;
|
3688 | 951f1351 | bellard | |
3689 | 951f1351 | bellard | if (!is_waitconnect)
|
3690 | 951f1351 | bellard | socket_set_nonblock(fd); |
3691 | 0bab00f3 | bellard | |
3692 | 0bab00f3 | bellard | s->connected = 0;
|
3693 | 0bab00f3 | bellard | s->fd = -1;
|
3694 | 0bab00f3 | bellard | s->listen_fd = -1;
|
3695 | ffd843bc | ths | s->is_unix = is_unix; |
3696 | f7499989 | pbrook | s->do_nodelay = do_nodelay && !is_unix; |
3697 | ffd843bc | ths | |
3698 | ffd843bc | ths | chr->opaque = s; |
3699 | ffd843bc | ths | chr->chr_write = tcp_chr_write; |
3700 | ffd843bc | ths | chr->chr_close = tcp_chr_close; |
3701 | ffd843bc | ths | |
3702 | 0bab00f3 | bellard | if (is_listen) {
|
3703 | 0bab00f3 | bellard | /* allow fast reuse */
|
3704 | ffd843bc | ths | #ifndef _WIN32
|
3705 | ffd843bc | ths | if (is_unix) {
|
3706 | ffd843bc | ths | char path[109]; |
3707 | ae45d369 | bellard | pstrcpy(path, sizeof(path), uaddr.sun_path);
|
3708 | ffd843bc | ths | unlink(path); |
3709 | ffd843bc | ths | } else
|
3710 | ffd843bc | ths | #endif
|
3711 | ffd843bc | ths | { |
3712 | ffd843bc | ths | val = 1;
|
3713 | ffd843bc | ths | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
3714 | ffd843bc | ths | } |
3715 | 3b46e624 | ths | |
3716 | ffd843bc | ths | ret = bind(fd, addr, addrlen); |
3717 | ffd843bc | ths | if (ret < 0) |
3718 | 0bab00f3 | bellard | goto fail;
|
3719 | ffd843bc | ths | |
3720 | 0bab00f3 | bellard | ret = listen(fd, 0);
|
3721 | 0bab00f3 | bellard | if (ret < 0) |
3722 | 0bab00f3 | bellard | goto fail;
|
3723 | ffd843bc | ths | |
3724 | 0bab00f3 | bellard | s->listen_fd = fd; |
3725 | 0bab00f3 | bellard | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
3726 | 951f1351 | bellard | if (is_telnet)
|
3727 | 951f1351 | bellard | s->do_telnetopt = 1;
|
3728 | 0bab00f3 | bellard | } else {
|
3729 | 0bab00f3 | bellard | for(;;) {
|
3730 | ffd843bc | ths | ret = connect(fd, addr, addrlen); |
3731 | 0bab00f3 | bellard | if (ret < 0) { |
3732 | 0bab00f3 | bellard | err = socket_error(); |
3733 | 0bab00f3 | bellard | if (err == EINTR || err == EWOULDBLOCK) {
|
3734 | 0bab00f3 | bellard | } else if (err == EINPROGRESS) { |
3735 | 0bab00f3 | bellard | break;
|
3736 | f5b12268 | ths | #ifdef _WIN32
|
3737 | f5b12268 | ths | } else if (err == WSAEALREADY) { |
3738 | f5b12268 | ths | break;
|
3739 | f5b12268 | ths | #endif
|
3740 | 0bab00f3 | bellard | } else {
|
3741 | 0bab00f3 | bellard | goto fail;
|
3742 | 0bab00f3 | bellard | } |
3743 | 0bab00f3 | bellard | } else {
|
3744 | 0bab00f3 | bellard | s->connected = 1;
|
3745 | 0bab00f3 | bellard | break;
|
3746 | 0bab00f3 | bellard | } |
3747 | 0bab00f3 | bellard | } |
3748 | 0bab00f3 | bellard | s->fd = fd; |
3749 | f7499989 | pbrook | socket_set_nodelay(fd); |
3750 | 0bab00f3 | bellard | if (s->connected)
|
3751 | 0bab00f3 | bellard | tcp_chr_connect(chr); |
3752 | 0bab00f3 | bellard | else
|
3753 | 0bab00f3 | bellard | qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
|
3754 | 0bab00f3 | bellard | } |
3755 | 3b46e624 | ths | |
3756 | 951f1351 | bellard | if (is_listen && is_waitconnect) {
|
3757 | 951f1351 | bellard | printf("QEMU waiting for connection on: %s\n", host_str);
|
3758 | 951f1351 | bellard | tcp_chr_accept(chr); |
3759 | 951f1351 | bellard | socket_set_nonblock(s->listen_fd); |
3760 | 951f1351 | bellard | } |
3761 | 951f1351 | bellard | |
3762 | 0bab00f3 | bellard | return chr;
|
3763 | 0bab00f3 | bellard | fail:
|
3764 | 0bab00f3 | bellard | if (fd >= 0) |
3765 | 0bab00f3 | bellard | closesocket(fd); |
3766 | 0bab00f3 | bellard | qemu_free(s); |
3767 | 0bab00f3 | bellard | qemu_free(chr); |
3768 | 0bab00f3 | bellard | return NULL; |
3769 | 0bab00f3 | bellard | } |
3770 | 0bab00f3 | bellard | |
3771 | 82c643ff | bellard | CharDriverState *qemu_chr_open(const char *filename) |
3772 | 82c643ff | bellard | { |
3773 | f8d179e3 | bellard | const char *p; |
3774 | fd1dff4b | bellard | |
3775 | 82c643ff | bellard | if (!strcmp(filename, "vc")) { |
3776 | af3a9031 | ths | return text_console_init(&display_state, 0); |
3777 | af3a9031 | ths | } else if (strstart(filename, "vc:", &p)) { |
3778 | af3a9031 | ths | return text_console_init(&display_state, p);
|
3779 | 82c643ff | bellard | } else if (!strcmp(filename, "null")) { |
3780 | 82c643ff | bellard | return qemu_chr_open_null();
|
3781 | 5fafdf24 | ths | } else
|
3782 | 0bab00f3 | bellard | if (strstart(filename, "tcp:", &p)) { |
3783 | ffd843bc | ths | return qemu_chr_open_tcp(p, 0, 0); |
3784 | 0bab00f3 | bellard | } else
|
3785 | 951f1351 | bellard | if (strstart(filename, "telnet:", &p)) { |
3786 | ffd843bc | ths | return qemu_chr_open_tcp(p, 1, 0); |
3787 | 0bab00f3 | bellard | } else
|
3788 | 0bab00f3 | bellard | if (strstart(filename, "udp:", &p)) { |
3789 | 0bab00f3 | bellard | return qemu_chr_open_udp(p);
|
3790 | 0bab00f3 | bellard | } else
|
3791 | 20d8a3ed | ths | if (strstart(filename, "mon:", &p)) { |
3792 | 20d8a3ed | ths | CharDriverState *drv = qemu_chr_open(p); |
3793 | 20d8a3ed | ths | if (drv) {
|
3794 | 20d8a3ed | ths | drv = qemu_chr_open_mux(drv); |
3795 | 20d8a3ed | ths | monitor_init(drv, !nographic); |
3796 | 20d8a3ed | ths | return drv;
|
3797 | 20d8a3ed | ths | } |
3798 | 20d8a3ed | ths | printf("Unable to open driver: %s\n", p);
|
3799 | 20d8a3ed | ths | return 0; |
3800 | 20d8a3ed | ths | } else
|
3801 | 7664728b | bellard | #ifndef _WIN32
|
3802 | ffd843bc | ths | if (strstart(filename, "unix:", &p)) { |
3803 | ffd843bc | ths | return qemu_chr_open_tcp(p, 0, 1); |
3804 | ffd843bc | ths | } else if (strstart(filename, "file:", &p)) { |
3805 | f8d179e3 | bellard | return qemu_chr_open_file_out(p);
|
3806 | f8d179e3 | bellard | } else if (strstart(filename, "pipe:", &p)) { |
3807 | f8d179e3 | bellard | return qemu_chr_open_pipe(p);
|
3808 | 7664728b | bellard | } else if (!strcmp(filename, "pty")) { |
3809 | 82c643ff | bellard | return qemu_chr_open_pty();
|
3810 | 82c643ff | bellard | } else if (!strcmp(filename, "stdio")) { |
3811 | 82c643ff | bellard | return qemu_chr_open_stdio();
|
3812 | 5fafdf24 | ths | } else
|
3813 | f8d179e3 | bellard | #if defined(__linux__)
|
3814 | e57a8c0e | bellard | if (strstart(filename, "/dev/parport", NULL)) { |
3815 | e57a8c0e | bellard | return qemu_chr_open_pp(filename);
|
3816 | 5fafdf24 | ths | } else
|
3817 | aec62507 | ths | #endif
|
3818 | 3fda388a | ths | #if defined(__linux__) || defined(__sun__)
|
3819 | f8d179e3 | bellard | if (strstart(filename, "/dev/", NULL)) { |
3820 | f8d179e3 | bellard | return qemu_chr_open_tty(filename);
|
3821 | 3fda388a | ths | } else
|
3822 | 3fda388a | ths | #endif
|
3823 | aec62507 | ths | #else /* !_WIN32 */ |
3824 | f331110f | bellard | if (strstart(filename, "COM", NULL)) { |
3825 | f331110f | bellard | return qemu_chr_open_win(filename);
|
3826 | f331110f | bellard | } else
|
3827 | f331110f | bellard | if (strstart(filename, "pipe:", &p)) { |
3828 | f331110f | bellard | return qemu_chr_open_win_pipe(p);
|
3829 | f331110f | bellard | } else
|
3830 | 72d46479 | ths | if (strstart(filename, "con:", NULL)) { |
3831 | 72d46479 | ths | return qemu_chr_open_win_con(filename);
|
3832 | 72d46479 | ths | } else
|
3833 | f331110f | bellard | if (strstart(filename, "file:", &p)) { |
3834 | f331110f | bellard | return qemu_chr_open_win_file_out(p);
|
3835 | 2e4d9fb1 | aurel32 | } else
|
3836 | 2e4d9fb1 | aurel32 | #endif
|
3837 | 2e4d9fb1 | aurel32 | #ifdef CONFIG_BRLAPI
|
3838 | 2e4d9fb1 | aurel32 | if (!strcmp(filename, "braille")) { |
3839 | 2e4d9fb1 | aurel32 | return chr_baum_init();
|
3840 | 2e4d9fb1 | aurel32 | } else
|
3841 | f331110f | bellard | #endif
|
3842 | 82c643ff | bellard | { |
3843 | 82c643ff | bellard | return NULL; |
3844 | 82c643ff | bellard | } |
3845 | 82c643ff | bellard | } |
3846 | 82c643ff | bellard | |
3847 | f331110f | bellard | void qemu_chr_close(CharDriverState *chr)
|
3848 | f331110f | bellard | { |
3849 | f331110f | bellard | if (chr->chr_close)
|
3850 | f331110f | bellard | chr->chr_close(chr); |
3851 | a11d070e | balrog | qemu_free(chr); |
3852 | f331110f | bellard | } |
3853 | f331110f | bellard | |
3854 | 80cabfad | bellard | /***********************************************************/
|
3855 | 7c9d8e07 | bellard | /* network device redirectors */
|
3856 | 330d0414 | bellard | |
3857 | 3f4afa14 | j_mayer | __attribute__ (( unused )) |
3858 | 9596ebb7 | pbrook | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
3859 | c20709aa | bellard | { |
3860 | c20709aa | bellard | int len, i, j, c;
|
3861 | c20709aa | bellard | |
3862 | c20709aa | bellard | for(i=0;i<size;i+=16) { |
3863 | c20709aa | bellard | len = size - i; |
3864 | c20709aa | bellard | if (len > 16) |
3865 | c20709aa | bellard | len = 16;
|
3866 | c20709aa | bellard | fprintf(f, "%08x ", i);
|
3867 | c20709aa | bellard | for(j=0;j<16;j++) { |
3868 | c20709aa | bellard | if (j < len)
|
3869 | c20709aa | bellard | fprintf(f, " %02x", buf[i+j]);
|
3870 | c20709aa | bellard | else
|
3871 | c20709aa | bellard | fprintf(f, " ");
|
3872 | c20709aa | bellard | } |
3873 | c20709aa | bellard | fprintf(f, " ");
|
3874 | c20709aa | bellard | for(j=0;j<len;j++) { |
3875 | c20709aa | bellard | c = buf[i+j]; |
3876 | c20709aa | bellard | if (c < ' ' || c > '~') |
3877 | c20709aa | bellard | c = '.';
|
3878 | c20709aa | bellard | fprintf(f, "%c", c);
|
3879 | c20709aa | bellard | } |
3880 | c20709aa | bellard | fprintf(f, "\n");
|
3881 | c20709aa | bellard | } |
3882 | c20709aa | bellard | } |
3883 | c20709aa | bellard | |
3884 | 7c9d8e07 | bellard | static int parse_macaddr(uint8_t *macaddr, const char *p) |
3885 | c20709aa | bellard | { |
3886 | 7c9d8e07 | bellard | int i;
|
3887 | 76ea08f9 | balrog | char *last_char;
|
3888 | 76ea08f9 | balrog | long int offset; |
3889 | 76ea08f9 | balrog | |
3890 | 76ea08f9 | balrog | errno = 0;
|
3891 | 76ea08f9 | balrog | offset = strtol(p, &last_char, 0);
|
3892 | 76ea08f9 | balrog | if (0 == errno && '\0' == *last_char && |
3893 | 76ea08f9 | balrog | offset >= 0 && offset <= 0xFFFFFF) { |
3894 | 76ea08f9 | balrog | macaddr[3] = (offset & 0xFF0000) >> 16; |
3895 | 76ea08f9 | balrog | macaddr[4] = (offset & 0xFF00) >> 8; |
3896 | 76ea08f9 | balrog | macaddr[5] = offset & 0xFF; |
3897 | 76ea08f9 | balrog | return 0; |
3898 | 76ea08f9 | balrog | } else {
|
3899 | 76ea08f9 | balrog | for(i = 0; i < 6; i++) { |
3900 | 76ea08f9 | balrog | macaddr[i] = strtol(p, (char **)&p, 16); |
3901 | 76ea08f9 | balrog | if (i == 5) { |
3902 | 76ea08f9 | balrog | if (*p != '\0') |
3903 | 76ea08f9 | balrog | return -1; |
3904 | 76ea08f9 | balrog | } else {
|
3905 | 76ea08f9 | balrog | if (*p != ':' && *p != '-') |
3906 | 76ea08f9 | balrog | return -1; |
3907 | 76ea08f9 | balrog | p++; |
3908 | 76ea08f9 | balrog | } |
3909 | 7c9d8e07 | bellard | } |
3910 | 76ea08f9 | balrog | return 0; |
3911 | 7c9d8e07 | bellard | } |
3912 | 76ea08f9 | balrog | |
3913 | 76ea08f9 | balrog | return -1; |
3914 | c20709aa | bellard | } |
3915 | 67b915a5 | bellard | |
3916 | 7c9d8e07 | bellard | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
3917 | 67b915a5 | bellard | { |
3918 | 7c9d8e07 | bellard | const char *p, *p1; |
3919 | 7c9d8e07 | bellard | int len;
|
3920 | 7c9d8e07 | bellard | p = *pp; |
3921 | 7c9d8e07 | bellard | p1 = strchr(p, sep); |
3922 | 7c9d8e07 | bellard | if (!p1)
|
3923 | 7c9d8e07 | bellard | return -1; |
3924 | 7c9d8e07 | bellard | len = p1 - p; |
3925 | 7c9d8e07 | bellard | p1++; |
3926 | 7c9d8e07 | bellard | if (buf_size > 0) { |
3927 | 7c9d8e07 | bellard | if (len > buf_size - 1) |
3928 | 7c9d8e07 | bellard | len = buf_size - 1;
|
3929 | 7c9d8e07 | bellard | memcpy(buf, p, len); |
3930 | 7c9d8e07 | bellard | buf[len] = '\0';
|
3931 | 7c9d8e07 | bellard | } |
3932 | 7c9d8e07 | bellard | *pp = p1; |
3933 | 7c9d8e07 | bellard | return 0; |
3934 | c20709aa | bellard | } |
3935 | c20709aa | bellard | |
3936 | 951f1351 | bellard | int parse_host_src_port(struct sockaddr_in *haddr, |
3937 | 951f1351 | bellard | struct sockaddr_in *saddr,
|
3938 | 951f1351 | bellard | const char *input_str) |
3939 | 951f1351 | bellard | { |
3940 | 951f1351 | bellard | char *str = strdup(input_str);
|
3941 | 951f1351 | bellard | char *host_str = str;
|
3942 | 951f1351 | bellard | char *src_str;
|
3943 | 951f1351 | bellard | char *ptr;
|
3944 | 951f1351 | bellard | |
3945 | 951f1351 | bellard | /*
|
3946 | 951f1351 | bellard | * Chop off any extra arguments at the end of the string which
|
3947 | 951f1351 | bellard | * would start with a comma, then fill in the src port information
|
3948 | 951f1351 | bellard | * if it was provided else use the "any address" and "any port".
|
3949 | 951f1351 | bellard | */
|
3950 | 951f1351 | bellard | if ((ptr = strchr(str,','))) |
3951 | 951f1351 | bellard | *ptr = '\0';
|
3952 | 951f1351 | bellard | |
3953 | 951f1351 | bellard | if ((src_str = strchr(input_str,'@'))) { |
3954 | 951f1351 | bellard | *src_str = '\0';
|
3955 | 951f1351 | bellard | src_str++; |
3956 | 951f1351 | bellard | } |
3957 | 951f1351 | bellard | |
3958 | 951f1351 | bellard | if (parse_host_port(haddr, host_str) < 0) |
3959 | 951f1351 | bellard | goto fail;
|
3960 | 951f1351 | bellard | |
3961 | 951f1351 | bellard | if (!src_str || *src_str == '\0') |
3962 | 951f1351 | bellard | src_str = ":0";
|
3963 | 951f1351 | bellard | |
3964 | 951f1351 | bellard | if (parse_host_port(saddr, src_str) < 0) |
3965 | 951f1351 | bellard | goto fail;
|
3966 | 951f1351 | bellard | |
3967 | 951f1351 | bellard | free(str); |
3968 | 951f1351 | bellard | return(0); |
3969 | 951f1351 | bellard | |
3970 | 951f1351 | bellard | fail:
|
3971 | 951f1351 | bellard | free(str); |
3972 | 951f1351 | bellard | return -1; |
3973 | 951f1351 | bellard | } |
3974 | 951f1351 | bellard | |
3975 | 7c9d8e07 | bellard | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
3976 | 7c9d8e07 | bellard | { |
3977 | 7c9d8e07 | bellard | char buf[512]; |
3978 | 7c9d8e07 | bellard | struct hostent *he;
|
3979 | 7c9d8e07 | bellard | const char *p, *r; |
3980 | 7c9d8e07 | bellard | int port;
|
3981 | 7c9d8e07 | bellard | |
3982 | 7c9d8e07 | bellard | p = str; |
3983 | 7c9d8e07 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
3984 | 7c9d8e07 | bellard | return -1; |
3985 | 7c9d8e07 | bellard | saddr->sin_family = AF_INET; |
3986 | 7c9d8e07 | bellard | if (buf[0] == '\0') { |
3987 | 7c9d8e07 | bellard | saddr->sin_addr.s_addr = 0;
|
3988 | 7c9d8e07 | bellard | } else {
|
3989 | 7c9d8e07 | bellard | if (isdigit(buf[0])) { |
3990 | 7c9d8e07 | bellard | if (!inet_aton(buf, &saddr->sin_addr))
|
3991 | 7c9d8e07 | bellard | return -1; |
3992 | 7c9d8e07 | bellard | } else {
|
3993 | 7c9d8e07 | bellard | if ((he = gethostbyname(buf)) == NULL) |
3994 | 7c9d8e07 | bellard | return - 1; |
3995 | 7c9d8e07 | bellard | saddr->sin_addr = *(struct in_addr *)he->h_addr;
|
3996 | 7c9d8e07 | bellard | } |
3997 | 7c9d8e07 | bellard | } |
3998 | 7c9d8e07 | bellard | port = strtol(p, (char **)&r, 0); |
3999 | 7c9d8e07 | bellard | if (r == p)
|
4000 | 7c9d8e07 | bellard | return -1; |
4001 | 7c9d8e07 | bellard | saddr->sin_port = htons(port); |
4002 | 7c9d8e07 | bellard | return 0; |
4003 | 7c9d8e07 | bellard | } |
4004 | c20709aa | bellard | |
4005 | 52f61fde | ths | #ifndef _WIN32
|
4006 | 52f61fde | ths | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) |
4007 | ffd843bc | ths | { |
4008 | ffd843bc | ths | const char *p; |
4009 | ffd843bc | ths | int len;
|
4010 | ffd843bc | ths | |
4011 | ffd843bc | ths | len = MIN(108, strlen(str));
|
4012 | ffd843bc | ths | p = strchr(str, ',');
|
4013 | ffd843bc | ths | if (p)
|
4014 | ffd843bc | ths | len = MIN(len, p - str); |
4015 | ffd843bc | ths | |
4016 | ffd843bc | ths | memset(uaddr, 0, sizeof(*uaddr)); |
4017 | ffd843bc | ths | |
4018 | ffd843bc | ths | uaddr->sun_family = AF_UNIX; |
4019 | ffd843bc | ths | memcpy(uaddr->sun_path, str, len); |
4020 | ffd843bc | ths | |
4021 | ffd843bc | ths | return 0; |
4022 | ffd843bc | ths | } |
4023 | 52f61fde | ths | #endif
|
4024 | ffd843bc | ths | |
4025 | 7c9d8e07 | bellard | /* find or alloc a new VLAN */
|
4026 | 7c9d8e07 | bellard | VLANState *qemu_find_vlan(int id)
|
4027 | c20709aa | bellard | { |
4028 | 7c9d8e07 | bellard | VLANState **pvlan, *vlan; |
4029 | 7c9d8e07 | bellard | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
4030 | 7c9d8e07 | bellard | if (vlan->id == id)
|
4031 | 7c9d8e07 | bellard | return vlan;
|
4032 | 7c9d8e07 | bellard | } |
4033 | 7c9d8e07 | bellard | vlan = qemu_mallocz(sizeof(VLANState));
|
4034 | 7c9d8e07 | bellard | if (!vlan)
|
4035 | 7c9d8e07 | bellard | return NULL; |
4036 | 7c9d8e07 | bellard | vlan->id = id; |
4037 | 7c9d8e07 | bellard | vlan->next = NULL;
|
4038 | 7c9d8e07 | bellard | pvlan = &first_vlan; |
4039 | 7c9d8e07 | bellard | while (*pvlan != NULL) |
4040 | 7c9d8e07 | bellard | pvlan = &(*pvlan)->next; |
4041 | 7c9d8e07 | bellard | *pvlan = vlan; |
4042 | 7c9d8e07 | bellard | return vlan;
|
4043 | c20709aa | bellard | } |
4044 | c20709aa | bellard | |
4045 | 7c9d8e07 | bellard | VLANClientState *qemu_new_vlan_client(VLANState *vlan, |
4046 | d861b05e | pbrook | IOReadHandler *fd_read, |
4047 | d861b05e | pbrook | IOCanRWHandler *fd_can_read, |
4048 | d861b05e | pbrook | void *opaque)
|
4049 | c20709aa | bellard | { |
4050 | 7c9d8e07 | bellard | VLANClientState *vc, **pvc; |
4051 | 7c9d8e07 | bellard | vc = qemu_mallocz(sizeof(VLANClientState));
|
4052 | 7c9d8e07 | bellard | if (!vc)
|
4053 | 7c9d8e07 | bellard | return NULL; |
4054 | 7c9d8e07 | bellard | vc->fd_read = fd_read; |
4055 | d861b05e | pbrook | vc->fd_can_read = fd_can_read; |
4056 | 7c9d8e07 | bellard | vc->opaque = opaque; |
4057 | 7c9d8e07 | bellard | vc->vlan = vlan; |
4058 | 7c9d8e07 | bellard | |
4059 | 7c9d8e07 | bellard | vc->next = NULL;
|
4060 | 7c9d8e07 | bellard | pvc = &vlan->first_client; |
4061 | 7c9d8e07 | bellard | while (*pvc != NULL) |
4062 | 7c9d8e07 | bellard | pvc = &(*pvc)->next; |
4063 | 7c9d8e07 | bellard | *pvc = vc; |
4064 | 7c9d8e07 | bellard | return vc;
|
4065 | c20709aa | bellard | } |
4066 | c20709aa | bellard | |
4067 | dcf414d6 | balrog | void qemu_del_vlan_client(VLANClientState *vc)
|
4068 | dcf414d6 | balrog | { |
4069 | dcf414d6 | balrog | VLANClientState **pvc = &vc->vlan->first_client; |
4070 | dcf414d6 | balrog | |
4071 | dcf414d6 | balrog | while (*pvc != NULL) |
4072 | dcf414d6 | balrog | if (*pvc == vc) {
|
4073 | dcf414d6 | balrog | *pvc = vc->next; |
4074 | dcf414d6 | balrog | free(vc); |
4075 | dcf414d6 | balrog | break;
|
4076 | dcf414d6 | balrog | } else
|
4077 | dcf414d6 | balrog | pvc = &(*pvc)->next; |
4078 | dcf414d6 | balrog | } |
4079 | dcf414d6 | balrog | |
4080 | d861b05e | pbrook | int qemu_can_send_packet(VLANClientState *vc1)
|
4081 | d861b05e | pbrook | { |
4082 | d861b05e | pbrook | VLANState *vlan = vc1->vlan; |
4083 | d861b05e | pbrook | VLANClientState *vc; |
4084 | d861b05e | pbrook | |
4085 | d861b05e | pbrook | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
4086 | d861b05e | pbrook | if (vc != vc1) {
|
4087 | fbd1711d | balrog | if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
|
4088 | fbd1711d | balrog | return 1; |
4089 | d861b05e | pbrook | } |
4090 | d861b05e | pbrook | } |
4091 | fbd1711d | balrog | return 0; |
4092 | d861b05e | pbrook | } |
4093 | d861b05e | pbrook | |
4094 | 7c9d8e07 | bellard | void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size) |
4095 | c20709aa | bellard | { |
4096 | 7c9d8e07 | bellard | VLANState *vlan = vc1->vlan; |
4097 | 7c9d8e07 | bellard | VLANClientState *vc; |
4098 | 7c9d8e07 | bellard | |
4099 | 7c9d8e07 | bellard | #if 0
|
4100 | 7c9d8e07 | bellard | printf("vlan %d send:\n", vlan->id);
|
4101 | 7c9d8e07 | bellard | hex_dump(stdout, buf, size);
|
4102 | 7c9d8e07 | bellard | #endif
|
4103 | 7c9d8e07 | bellard | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
4104 | 7c9d8e07 | bellard | if (vc != vc1) {
|
4105 | 7c9d8e07 | bellard | vc->fd_read(vc->opaque, buf, size); |
4106 | 7c9d8e07 | bellard | } |
4107 | 7c9d8e07 | bellard | } |
4108 | 67b915a5 | bellard | } |
4109 | 67b915a5 | bellard | |
4110 | c20709aa | bellard | #if defined(CONFIG_SLIRP)
|
4111 | c20709aa | bellard | |
4112 | c20709aa | bellard | /* slirp network adapter */
|
4113 | c20709aa | bellard | |
4114 | c20709aa | bellard | static int slirp_inited; |
4115 | 7c9d8e07 | bellard | static VLANClientState *slirp_vc;
|
4116 | c20709aa | bellard | |
4117 | c20709aa | bellard | int slirp_can_output(void) |
4118 | c20709aa | bellard | { |
4119 | 3b7f5d47 | pbrook | return !slirp_vc || qemu_can_send_packet(slirp_vc);
|
4120 | c20709aa | bellard | } |
4121 | c20709aa | bellard | |
4122 | c20709aa | bellard | void slirp_output(const uint8_t *pkt, int pkt_len) |
4123 | 67b915a5 | bellard | { |
4124 | c20709aa | bellard | #if 0
|
4125 | 7c9d8e07 | bellard | printf("slirp output:\n");
|
4126 | c20709aa | bellard | hex_dump(stdout, pkt, pkt_len);
|
4127 | c20709aa | bellard | #endif
|
4128 | 3b7f5d47 | pbrook | if (!slirp_vc)
|
4129 | 3b7f5d47 | pbrook | return;
|
4130 | 7c9d8e07 | bellard | qemu_send_packet(slirp_vc, pkt, pkt_len); |
4131 | 67b915a5 | bellard | } |
4132 | 67b915a5 | bellard | |
4133 | 7c9d8e07 | bellard | static void slirp_receive(void *opaque, const uint8_t *buf, int size) |
4134 | c20709aa | bellard | { |
4135 | c20709aa | bellard | #if 0
|
4136 | 7c9d8e07 | bellard | printf("slirp input:\n");
|
4137 | c20709aa | bellard | hex_dump(stdout, buf, size);
|
4138 | c20709aa | bellard | #endif
|
4139 | c20709aa | bellard | slirp_input(buf, size); |
4140 | c20709aa | bellard | } |
4141 | c20709aa | bellard | |
4142 | 7c9d8e07 | bellard | static int net_slirp_init(VLANState *vlan) |
4143 | c20709aa | bellard | { |
4144 | c20709aa | bellard | if (!slirp_inited) {
|
4145 | c20709aa | bellard | slirp_inited = 1;
|
4146 | c20709aa | bellard | slirp_init(); |
4147 | c20709aa | bellard | } |
4148 | 5fafdf24 | ths | slirp_vc = qemu_new_vlan_client(vlan, |
4149 | d861b05e | pbrook | slirp_receive, NULL, NULL); |
4150 | 7c9d8e07 | bellard | snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector"); |
4151 | 9bf05444 | bellard | return 0; |
4152 | 9bf05444 | bellard | } |
4153 | 9bf05444 | bellard | |
4154 | 9bf05444 | bellard | static void net_slirp_redir(const char *redir_str) |
4155 | 9bf05444 | bellard | { |
4156 | 9bf05444 | bellard | int is_udp;
|
4157 | 9bf05444 | bellard | char buf[256], *r; |
4158 | 9bf05444 | bellard | const char *p; |
4159 | 9bf05444 | bellard | struct in_addr guest_addr;
|
4160 | 9bf05444 | bellard | int host_port, guest_port;
|
4161 | 3b46e624 | ths | |
4162 | 9bf05444 | bellard | if (!slirp_inited) {
|
4163 | 9bf05444 | bellard | slirp_inited = 1;
|
4164 | 9bf05444 | bellard | slirp_init(); |
4165 | 9bf05444 | bellard | } |
4166 | 9bf05444 | bellard | |
4167 | 9bf05444 | bellard | p = redir_str; |
4168 | 9bf05444 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
4169 | 9bf05444 | bellard | goto fail;
|
4170 | 9bf05444 | bellard | if (!strcmp(buf, "tcp")) { |
4171 | 9bf05444 | bellard | is_udp = 0;
|
4172 | 9bf05444 | bellard | } else if (!strcmp(buf, "udp")) { |
4173 | 9bf05444 | bellard | is_udp = 1;
|
4174 | 9bf05444 | bellard | } else {
|
4175 | 9bf05444 | bellard | goto fail;
|
4176 | 9bf05444 | bellard | } |
4177 | 9bf05444 | bellard | |
4178 | 9bf05444 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
4179 | 9bf05444 | bellard | goto fail;
|
4180 | 9bf05444 | bellard | host_port = strtol(buf, &r, 0);
|
4181 | 9bf05444 | bellard | if (r == buf)
|
4182 | 9bf05444 | bellard | goto fail;
|
4183 | 9bf05444 | bellard | |
4184 | 9bf05444 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
4185 | 9bf05444 | bellard | goto fail;
|
4186 | 9bf05444 | bellard | if (buf[0] == '\0') { |
4187 | 9bf05444 | bellard | pstrcpy(buf, sizeof(buf), "10.0.2.15"); |
4188 | 9bf05444 | bellard | } |
4189 | 9bf05444 | bellard | if (!inet_aton(buf, &guest_addr))
|
4190 | 9bf05444 | bellard | goto fail;
|
4191 | 3b46e624 | ths | |
4192 | 9bf05444 | bellard | guest_port = strtol(p, &r, 0);
|
4193 | 9bf05444 | bellard | if (r == p)
|
4194 | 9bf05444 | bellard | goto fail;
|
4195 | 3b46e624 | ths | |
4196 | 9bf05444 | bellard | if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { |
4197 | 9bf05444 | bellard | fprintf(stderr, "qemu: could not set up redirection\n");
|
4198 | 9bf05444 | bellard | exit(1);
|
4199 | 9bf05444 | bellard | } |
4200 | 9bf05444 | bellard | return;
|
4201 | 9bf05444 | bellard | fail:
|
4202 | 9bf05444 | bellard | fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
|
4203 | 9bf05444 | bellard | exit(1);
|
4204 | 9bf05444 | bellard | } |
4205 | 3b46e624 | ths | |
4206 | c94c8d64 | bellard | #ifndef _WIN32
|
4207 | c94c8d64 | bellard | |
4208 | 9d728e8c | bellard | char smb_dir[1024]; |
4209 | 9d728e8c | bellard | |
4210 | 044fae83 | balrog | static void erase_dir(char *dir_name) |
4211 | 9d728e8c | bellard | { |
4212 | 9d728e8c | bellard | DIR *d; |
4213 | 9d728e8c | bellard | struct dirent *de;
|
4214 | 9d728e8c | bellard | char filename[1024]; |
4215 | 9d728e8c | bellard | |
4216 | 9d728e8c | bellard | /* erase all the files in the directory */
|
4217 | 044fae83 | balrog | if ((d = opendir(dir_name)) != 0) { |
4218 | 044fae83 | balrog | for(;;) {
|
4219 | 044fae83 | balrog | de = readdir(d); |
4220 | 044fae83 | balrog | if (!de)
|
4221 | 044fae83 | balrog | break;
|
4222 | 044fae83 | balrog | if (strcmp(de->d_name, ".") != 0 && |
4223 | 044fae83 | balrog | strcmp(de->d_name, "..") != 0) { |
4224 | 044fae83 | balrog | snprintf(filename, sizeof(filename), "%s/%s", |
4225 | 044fae83 | balrog | smb_dir, de->d_name); |
4226 | 044fae83 | balrog | if (unlink(filename) != 0) /* is it a directory? */ |
4227 | 044fae83 | balrog | erase_dir(filename); |
4228 | 044fae83 | balrog | } |
4229 | 9d728e8c | bellard | } |
4230 | 044fae83 | balrog | closedir(d); |
4231 | 044fae83 | balrog | rmdir(dir_name); |
4232 | 9d728e8c | bellard | } |
4233 | 044fae83 | balrog | } |
4234 | 044fae83 | balrog | |
4235 | 044fae83 | balrog | /* automatic user mode samba server configuration */
|
4236 | 044fae83 | balrog | static void smb_exit(void) |
4237 | 044fae83 | balrog | { |
4238 | 044fae83 | balrog | erase_dir(smb_dir); |
4239 | 9d728e8c | bellard | } |
4240 | 9d728e8c | bellard | |
4241 | 9d728e8c | bellard | /* automatic user mode samba server configuration */
|
4242 | 9596ebb7 | pbrook | static void net_slirp_smb(const char *exported_dir) |
4243 | 9d728e8c | bellard | { |
4244 | 9d728e8c | bellard | char smb_conf[1024]; |
4245 | 9d728e8c | bellard | char smb_cmdline[1024]; |
4246 | 9d728e8c | bellard | FILE *f; |
4247 | 9d728e8c | bellard | |
4248 | 9d728e8c | bellard | if (!slirp_inited) {
|
4249 | 9d728e8c | bellard | slirp_inited = 1;
|
4250 | 9d728e8c | bellard | slirp_init(); |
4251 | 9d728e8c | bellard | } |
4252 | 9d728e8c | bellard | |
4253 | 9d728e8c | bellard | /* XXX: better tmp dir construction */
|
4254 | 9d728e8c | bellard | snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); |
4255 | 9d728e8c | bellard | if (mkdir(smb_dir, 0700) < 0) { |
4256 | 9d728e8c | bellard | fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
|
4257 | 9d728e8c | bellard | exit(1);
|
4258 | 9d728e8c | bellard | } |
4259 | 9d728e8c | bellard | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); |
4260 | 3b46e624 | ths | |
4261 | 9d728e8c | bellard | f = fopen(smb_conf, "w");
|
4262 | 9d728e8c | bellard | if (!f) {
|
4263 | 9d728e8c | bellard | fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
|
4264 | 9d728e8c | bellard | exit(1);
|
4265 | 9d728e8c | bellard | } |
4266 | 5fafdf24 | ths | fprintf(f, |
4267 | 9d728e8c | bellard | "[global]\n"
|
4268 | 157777ef | bellard | "private dir=%s\n"
|
4269 | 157777ef | bellard | "smb ports=0\n"
|
4270 | 157777ef | bellard | "socket address=127.0.0.1\n"
|
4271 | 9d728e8c | bellard | "pid directory=%s\n"
|
4272 | 9d728e8c | bellard | "lock directory=%s\n"
|
4273 | 9d728e8c | bellard | "log file=%s/log.smbd\n"
|
4274 | 9d728e8c | bellard | "smb passwd file=%s/smbpasswd\n"
|
4275 | 03ffbb69 | bellard | "security = share\n"
|
4276 | 9d728e8c | bellard | "[qemu]\n"
|
4277 | 9d728e8c | bellard | "path=%s\n"
|
4278 | 9d728e8c | bellard | "read only=no\n"
|
4279 | 9d728e8c | bellard | "guest ok=yes\n",
|
4280 | 9d728e8c | bellard | smb_dir, |
4281 | 157777ef | bellard | smb_dir, |
4282 | 9d728e8c | bellard | smb_dir, |
4283 | 9d728e8c | bellard | smb_dir, |
4284 | 9d728e8c | bellard | smb_dir, |
4285 | 9d728e8c | bellard | exported_dir |
4286 | 9d728e8c | bellard | ); |
4287 | 9d728e8c | bellard | fclose(f); |
4288 | 9d728e8c | bellard | atexit(smb_exit); |
4289 | 9d728e8c | bellard | |
4290 | a14d6c8c | pbrook | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
4291 | a14d6c8c | pbrook | SMBD_COMMAND, smb_conf); |
4292 | 3b46e624 | ths | |
4293 | 9d728e8c | bellard | slirp_add_exec(0, smb_cmdline, 4, 139); |
4294 | 9d728e8c | bellard | } |
4295 | 9bf05444 | bellard | |
4296 | c94c8d64 | bellard | #endif /* !defined(_WIN32) */ |
4297 | 31a60e22 | blueswir1 | void do_info_slirp(void) |
4298 | 31a60e22 | blueswir1 | { |
4299 | 31a60e22 | blueswir1 | slirp_stats(); |
4300 | 31a60e22 | blueswir1 | } |
4301 | c94c8d64 | bellard | |
4302 | c20709aa | bellard | #endif /* CONFIG_SLIRP */ |
4303 | c20709aa | bellard | |
4304 | c20709aa | bellard | #if !defined(_WIN32)
|
4305 | 7c9d8e07 | bellard | |
4306 | 7c9d8e07 | bellard | typedef struct TAPState { |
4307 | 7c9d8e07 | bellard | VLANClientState *vc; |
4308 | 7c9d8e07 | bellard | int fd;
|
4309 | b46a8906 | ths | char down_script[1024]; |
4310 | 7c9d8e07 | bellard | } TAPState; |
4311 | 7c9d8e07 | bellard | |
4312 | 7c9d8e07 | bellard | static void tap_receive(void *opaque, const uint8_t *buf, int size) |
4313 | 7c9d8e07 | bellard | { |
4314 | 7c9d8e07 | bellard | TAPState *s = opaque; |
4315 | 7c9d8e07 | bellard | int ret;
|
4316 | 7c9d8e07 | bellard | for(;;) {
|
4317 | 7c9d8e07 | bellard | ret = write(s->fd, buf, size); |
4318 | 7c9d8e07 | bellard | if (ret < 0 && (errno == EINTR || errno == EAGAIN)) { |
4319 | 7c9d8e07 | bellard | } else {
|
4320 | 7c9d8e07 | bellard | break;
|
4321 | 7c9d8e07 | bellard | } |
4322 | 7c9d8e07 | bellard | } |
4323 | 7c9d8e07 | bellard | } |
4324 | 7c9d8e07 | bellard | |
4325 | 7c9d8e07 | bellard | static void tap_send(void *opaque) |
4326 | 7c9d8e07 | bellard | { |
4327 | 7c9d8e07 | bellard | TAPState *s = opaque; |
4328 | 7c9d8e07 | bellard | uint8_t buf[4096];
|
4329 | 7c9d8e07 | bellard | int size;
|
4330 | 7c9d8e07 | bellard | |
4331 | d5d10bc3 | ths | #ifdef __sun__
|
4332 | d5d10bc3 | ths | struct strbuf sbuf;
|
4333 | d5d10bc3 | ths | int f = 0; |
4334 | d5d10bc3 | ths | sbuf.maxlen = sizeof(buf);
|
4335 | d5d10bc3 | ths | sbuf.buf = buf; |
4336 | d5d10bc3 | ths | size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1; |
4337 | d5d10bc3 | ths | #else
|
4338 | 7c9d8e07 | bellard | size = read(s->fd, buf, sizeof(buf));
|
4339 | d5d10bc3 | ths | #endif
|
4340 | 7c9d8e07 | bellard | if (size > 0) { |
4341 | 7c9d8e07 | bellard | qemu_send_packet(s->vc, buf, size); |
4342 | 7c9d8e07 | bellard | } |
4343 | 7c9d8e07 | bellard | } |
4344 | 7c9d8e07 | bellard | |
4345 | 7c9d8e07 | bellard | /* fd support */
|
4346 | 7c9d8e07 | bellard | |
4347 | 7c9d8e07 | bellard | static TAPState *net_tap_fd_init(VLANState *vlan, int fd) |
4348 | 7c9d8e07 | bellard | { |
4349 | 7c9d8e07 | bellard | TAPState *s; |
4350 | 7c9d8e07 | bellard | |
4351 | 7c9d8e07 | bellard | s = qemu_mallocz(sizeof(TAPState));
|
4352 | 7c9d8e07 | bellard | if (!s)
|
4353 | 7c9d8e07 | bellard | return NULL; |
4354 | 7c9d8e07 | bellard | s->fd = fd; |
4355 | d861b05e | pbrook | s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
|
4356 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, tap_send, NULL, s);
|
4357 | 7c9d8e07 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd); |
4358 | 7c9d8e07 | bellard | return s;
|
4359 | 7c9d8e07 | bellard | } |
4360 | 7c9d8e07 | bellard | |
4361 | 5c40d2bd | ths | #if defined (_BSD) || defined (__FreeBSD_kernel__)
|
4362 | 7c9d8e07 | bellard | static int tap_open(char *ifname, int ifname_size) |
4363 | 7d3505c5 | bellard | { |
4364 | 7d3505c5 | bellard | int fd;
|
4365 | 7d3505c5 | bellard | char *dev;
|
4366 | 7d3505c5 | bellard | struct stat s;
|
4367 | 67b915a5 | bellard | |
4368 | aeb30be6 | balrog | TFR(fd = open("/dev/tap", O_RDWR));
|
4369 | 7d3505c5 | bellard | if (fd < 0) { |
4370 | 7d3505c5 | bellard | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
|
4371 | 7d3505c5 | bellard | return -1; |
4372 | 7d3505c5 | bellard | } |
4373 | 7d3505c5 | bellard | |
4374 | 7d3505c5 | bellard | fstat(fd, &s); |
4375 | 7d3505c5 | bellard | dev = devname(s.st_rdev, S_IFCHR); |
4376 | 7d3505c5 | bellard | pstrcpy(ifname, ifname_size, dev); |
4377 | 7d3505c5 | bellard | |
4378 | 7d3505c5 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
4379 | 7d3505c5 | bellard | return fd;
|
4380 | 7d3505c5 | bellard | } |
4381 | ec530c81 | bellard | #elif defined(__sun__)
|
4382 | d5d10bc3 | ths | #define TUNNEWPPA (('T'<<16) | 0x0001) |
4383 | 5fafdf24 | ths | /*
|
4384 | 5fafdf24 | ths | * Allocate TAP device, returns opened fd.
|
4385 | d5d10bc3 | ths | * Stores dev name in the first arg(must be large enough).
|
4386 | 3b46e624 | ths | */
|
4387 | 363a37d5 | blueswir1 | int tap_alloc(char *dev, size_t dev_size) |
4388 | d5d10bc3 | ths | { |
4389 | d5d10bc3 | ths | int tap_fd, if_fd, ppa = -1; |
4390 | d5d10bc3 | ths | static int ip_fd = 0; |
4391 | d5d10bc3 | ths | char *ptr;
|
4392 | d5d10bc3 | ths | |
4393 | d5d10bc3 | ths | static int arp_fd = 0; |
4394 | d5d10bc3 | ths | int ip_muxid, arp_muxid;
|
4395 | d5d10bc3 | ths | struct strioctl strioc_if, strioc_ppa;
|
4396 | d5d10bc3 | ths | int link_type = I_PLINK;;
|
4397 | d5d10bc3 | ths | struct lifreq ifr;
|
4398 | d5d10bc3 | ths | char actual_name[32] = ""; |
4399 | d5d10bc3 | ths | |
4400 | d5d10bc3 | ths | memset(&ifr, 0x0, sizeof(ifr)); |
4401 | d5d10bc3 | ths | |
4402 | d5d10bc3 | ths | if( *dev ){
|
4403 | 5fafdf24 | ths | ptr = dev; |
4404 | 5fafdf24 | ths | while( *ptr && !isdigit((int)*ptr) ) ptr++; |
4405 | d5d10bc3 | ths | ppa = atoi(ptr); |
4406 | d5d10bc3 | ths | } |
4407 | d5d10bc3 | ths | |
4408 | d5d10bc3 | ths | /* Check if IP device was opened */
|
4409 | d5d10bc3 | ths | if( ip_fd )
|
4410 | d5d10bc3 | ths | close(ip_fd); |
4411 | d5d10bc3 | ths | |
4412 | aeb30be6 | balrog | TFR(ip_fd = open("/dev/udp", O_RDWR, 0)); |
4413 | aeb30be6 | balrog | if (ip_fd < 0) { |
4414 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
|
4415 | d5d10bc3 | ths | return -1; |
4416 | d5d10bc3 | ths | } |
4417 | d5d10bc3 | ths | |
4418 | aeb30be6 | balrog | TFR(tap_fd = open("/dev/tap", O_RDWR, 0)); |
4419 | aeb30be6 | balrog | if (tap_fd < 0) { |
4420 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't open /dev/tap");
|
4421 | d5d10bc3 | ths | return -1; |
4422 | d5d10bc3 | ths | } |
4423 | d5d10bc3 | ths | |
4424 | d5d10bc3 | ths | /* Assign a new PPA and get its unit number. */
|
4425 | d5d10bc3 | ths | strioc_ppa.ic_cmd = TUNNEWPPA; |
4426 | d5d10bc3 | ths | strioc_ppa.ic_timout = 0;
|
4427 | d5d10bc3 | ths | strioc_ppa.ic_len = sizeof(ppa);
|
4428 | d5d10bc3 | ths | strioc_ppa.ic_dp = (char *)&ppa;
|
4429 | d5d10bc3 | ths | if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) |
4430 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't assign new interface");
|
4431 | d5d10bc3 | ths | |
4432 | aeb30be6 | balrog | TFR(if_fd = open("/dev/tap", O_RDWR, 0)); |
4433 | aeb30be6 | balrog | if (if_fd < 0) { |
4434 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't open /dev/tap (2)");
|
4435 | d5d10bc3 | ths | return -1; |
4436 | d5d10bc3 | ths | } |
4437 | d5d10bc3 | ths | if(ioctl(if_fd, I_PUSH, "ip") < 0){ |
4438 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't push IP module");
|
4439 | d5d10bc3 | ths | return -1; |
4440 | d5d10bc3 | ths | } |
4441 | d5d10bc3 | ths | |
4442 | d5d10bc3 | ths | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) |
4443 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't get flags\n");
|
4444 | d5d10bc3 | ths | |
4445 | d5d10bc3 | ths | snprintf (actual_name, 32, "tap%d", ppa); |
4446 | d5d10bc3 | ths | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
|
4447 | d5d10bc3 | ths | |
4448 | d5d10bc3 | ths | ifr.lifr_ppa = ppa; |
4449 | d5d10bc3 | ths | /* Assign ppa according to the unit number returned by tun device */
|
4450 | d5d10bc3 | ths | |
4451 | d5d10bc3 | ths | if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) |
4452 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't set PPA %d", ppa);
|
4453 | d5d10bc3 | ths | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) |
4454 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't get flags\n");
|
4455 | d5d10bc3 | ths | /* Push arp module to if_fd */
|
4456 | d5d10bc3 | ths | if (ioctl (if_fd, I_PUSH, "arp") < 0) |
4457 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't push ARP module (2)");
|
4458 | d5d10bc3 | ths | |
4459 | d5d10bc3 | ths | /* Push arp module to ip_fd */
|
4460 | d5d10bc3 | ths | if (ioctl (ip_fd, I_POP, NULL) < 0) |
4461 | d5d10bc3 | ths | syslog (LOG_ERR, "I_POP failed\n");
|
4462 | d5d10bc3 | ths | if (ioctl (ip_fd, I_PUSH, "arp") < 0) |
4463 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't push ARP module (3)\n");
|
4464 | d5d10bc3 | ths | /* Open arp_fd */
|
4465 | aeb30be6 | balrog | TFR(arp_fd = open ("/dev/tap", O_RDWR, 0)); |
4466 | aeb30be6 | balrog | if (arp_fd < 0) |
4467 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't open %s\n", "/dev/tap"); |
4468 | d5d10bc3 | ths | |
4469 | d5d10bc3 | ths | /* Set ifname to arp */
|
4470 | d5d10bc3 | ths | strioc_if.ic_cmd = SIOCSLIFNAME; |
4471 | d5d10bc3 | ths | strioc_if.ic_timout = 0;
|
4472 | d5d10bc3 | ths | strioc_if.ic_len = sizeof(ifr);
|
4473 | d5d10bc3 | ths | strioc_if.ic_dp = (char *)𝔦
|
4474 | d5d10bc3 | ths | if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ |
4475 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't set ifname to arp\n");
|
4476 | d5d10bc3 | ths | } |
4477 | d5d10bc3 | ths | |
4478 | d5d10bc3 | ths | if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ |
4479 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't link TAP device to IP");
|
4480 | d5d10bc3 | ths | return -1; |
4481 | d5d10bc3 | ths | } |
4482 | d5d10bc3 | ths | |
4483 | d5d10bc3 | ths | if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) |
4484 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't link TAP device to ARP");
|
4485 | d5d10bc3 | ths | |
4486 | d5d10bc3 | ths | close (if_fd); |
4487 | d5d10bc3 | ths | |
4488 | d5d10bc3 | ths | memset(&ifr, 0x0, sizeof(ifr)); |
4489 | d5d10bc3 | ths | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
|
4490 | d5d10bc3 | ths | ifr.lifr_ip_muxid = ip_muxid; |
4491 | d5d10bc3 | ths | ifr.lifr_arp_muxid = arp_muxid; |
4492 | d5d10bc3 | ths | |
4493 | d5d10bc3 | ths | if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) |
4494 | d5d10bc3 | ths | { |
4495 | d5d10bc3 | ths | ioctl (ip_fd, I_PUNLINK , arp_muxid); |
4496 | d5d10bc3 | ths | ioctl (ip_fd, I_PUNLINK, ip_muxid); |
4497 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't set multiplexor id");
|
4498 | d5d10bc3 | ths | } |
4499 | d5d10bc3 | ths | |
4500 | 363a37d5 | blueswir1 | snprintf(dev, dev_size, "tap%d", ppa);
|
4501 | d5d10bc3 | ths | return tap_fd;
|
4502 | d5d10bc3 | ths | } |
4503 | d5d10bc3 | ths | |
4504 | ec530c81 | bellard | static int tap_open(char *ifname, int ifname_size) |
4505 | ec530c81 | bellard | { |
4506 | d5d10bc3 | ths | char dev[10]=""; |
4507 | d5d10bc3 | ths | int fd;
|
4508 | 363a37d5 | blueswir1 | if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){ |
4509 | d5d10bc3 | ths | fprintf(stderr, "Cannot allocate TAP device\n");
|
4510 | d5d10bc3 | ths | return -1; |
4511 | d5d10bc3 | ths | } |
4512 | d5d10bc3 | ths | pstrcpy(ifname, ifname_size, dev); |
4513 | d5d10bc3 | ths | fcntl(fd, F_SETFL, O_NONBLOCK); |
4514 | d5d10bc3 | ths | return fd;
|
4515 | ec530c81 | bellard | } |
4516 | 7d3505c5 | bellard | #else
|
4517 | 7c9d8e07 | bellard | static int tap_open(char *ifname, int ifname_size) |
4518 | 330d0414 | bellard | { |
4519 | 80cabfad | bellard | struct ifreq ifr;
|
4520 | c4b1fcc0 | bellard | int fd, ret;
|
4521 | 3b46e624 | ths | |
4522 | aeb30be6 | balrog | TFR(fd = open("/dev/net/tun", O_RDWR));
|
4523 | 80cabfad | bellard | if (fd < 0) { |
4524 | 80cabfad | bellard | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
|
4525 | 80cabfad | bellard | return -1; |
4526 | 330d0414 | bellard | } |
4527 | 80cabfad | bellard | memset(&ifr, 0, sizeof(ifr)); |
4528 | 80cabfad | bellard | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
4529 | 7c9d8e07 | bellard | if (ifname[0] != '\0') |
4530 | 7c9d8e07 | bellard | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
4531 | 7c9d8e07 | bellard | else
|
4532 | 7c9d8e07 | bellard | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
|
4533 | 80cabfad | bellard | ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
|
4534 | 80cabfad | bellard | if (ret != 0) { |
4535 | 80cabfad | bellard | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
|
4536 | 80cabfad | bellard | close(fd); |
4537 | 80cabfad | bellard | return -1; |
4538 | 80cabfad | bellard | } |
4539 | c4b1fcc0 | bellard | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
4540 | 80cabfad | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
4541 | c4b1fcc0 | bellard | return fd;
|
4542 | c4b1fcc0 | bellard | } |
4543 | 7d3505c5 | bellard | #endif
|
4544 | 330d0414 | bellard | |
4545 | b46a8906 | ths | static int launch_script(const char *setup_script, const char *ifname, int fd) |
4546 | 7c9d8e07 | bellard | { |
4547 | b46a8906 | ths | int pid, status;
|
4548 | 7c9d8e07 | bellard | char *args[3]; |
4549 | 7c9d8e07 | bellard | char **parg;
|
4550 | 7c9d8e07 | bellard | |
4551 | b46a8906 | ths | /* try to launch network script */
|
4552 | 7c9d8e07 | bellard | pid = fork(); |
4553 | 7c9d8e07 | bellard | if (pid >= 0) { |
4554 | 7c9d8e07 | bellard | if (pid == 0) { |
4555 | 50d3eeae | ths | int open_max = sysconf (_SC_OPEN_MAX), i;
|
4556 | 50d3eeae | ths | for (i = 0; i < open_max; i++) |
4557 | 50d3eeae | ths | if (i != STDIN_FILENO &&
|
4558 | 50d3eeae | ths | i != STDOUT_FILENO && |
4559 | 50d3eeae | ths | i != STDERR_FILENO && |
4560 | 50d3eeae | ths | i != fd) |
4561 | 50d3eeae | ths | close(i); |
4562 | 50d3eeae | ths | |
4563 | 7c9d8e07 | bellard | parg = args; |
4564 | 7c9d8e07 | bellard | *parg++ = (char *)setup_script;
|
4565 | b46a8906 | ths | *parg++ = (char *)ifname;
|
4566 | 7c9d8e07 | bellard | *parg++ = NULL;
|
4567 | 7c9d8e07 | bellard | execv(setup_script, args); |
4568 | 4a38940d | bellard | _exit(1);
|
4569 | 7c9d8e07 | bellard | } |
4570 | 7c9d8e07 | bellard | while (waitpid(pid, &status, 0) != pid); |
4571 | 7c9d8e07 | bellard | if (!WIFEXITED(status) ||
|
4572 | 7c9d8e07 | bellard | WEXITSTATUS(status) != 0) {
|
4573 | 7c9d8e07 | bellard | fprintf(stderr, "%s: could not launch network script\n",
|
4574 | 7c9d8e07 | bellard | setup_script); |
4575 | 7c9d8e07 | bellard | return -1; |
4576 | 7c9d8e07 | bellard | } |
4577 | 7c9d8e07 | bellard | } |
4578 | b46a8906 | ths | return 0; |
4579 | b46a8906 | ths | } |
4580 | b46a8906 | ths | |
4581 | b46a8906 | ths | static int net_tap_init(VLANState *vlan, const char *ifname1, |
4582 | b46a8906 | ths | const char *setup_script, const char *down_script) |
4583 | b46a8906 | ths | { |
4584 | b46a8906 | ths | TAPState *s; |
4585 | b46a8906 | ths | int fd;
|
4586 | b46a8906 | ths | char ifname[128]; |
4587 | b46a8906 | ths | |
4588 | b46a8906 | ths | if (ifname1 != NULL) |
4589 | b46a8906 | ths | pstrcpy(ifname, sizeof(ifname), ifname1);
|
4590 | b46a8906 | ths | else
|
4591 | b46a8906 | ths | ifname[0] = '\0'; |
4592 | b46a8906 | ths | TFR(fd = tap_open(ifname, sizeof(ifname)));
|
4593 | b46a8906 | ths | if (fd < 0) |
4594 | b46a8906 | ths | return -1; |
4595 | b46a8906 | ths | |
4596 | b46a8906 | ths | if (!setup_script || !strcmp(setup_script, "no")) |
4597 | b46a8906 | ths | setup_script = "";
|
4598 | b46a8906 | ths | if (setup_script[0] != '\0') { |
4599 | b46a8906 | ths | if (launch_script(setup_script, ifname, fd))
|
4600 | b46a8906 | ths | return -1; |
4601 | 7c9d8e07 | bellard | } |
4602 | 7c9d8e07 | bellard | s = net_tap_fd_init(vlan, fd); |
4603 | 7c9d8e07 | bellard | if (!s)
|
4604 | 7c9d8e07 | bellard | return -1; |
4605 | 5fafdf24 | ths | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4606 | 7c9d8e07 | bellard | "tap: ifname=%s setup_script=%s", ifname, setup_script);
|
4607 | b46a8906 | ths | if (down_script && strcmp(down_script, "no")) |
4608 | b46a8906 | ths | snprintf(s->down_script, sizeof(s->down_script), "%s", down_script); |
4609 | 7c9d8e07 | bellard | return 0; |
4610 | 7c9d8e07 | bellard | } |
4611 | 7c9d8e07 | bellard | |
4612 | fd1dff4b | bellard | #endif /* !_WIN32 */ |
4613 | fd1dff4b | bellard | |
4614 | 8a16d273 | ths | #if defined(CONFIG_VDE)
|
4615 | 8a16d273 | ths | typedef struct VDEState { |
4616 | 8a16d273 | ths | VLANClientState *vc; |
4617 | 8a16d273 | ths | VDECONN *vde; |
4618 | 8a16d273 | ths | } VDEState; |
4619 | 8a16d273 | ths | |
4620 | 8a16d273 | ths | static void vde_to_qemu(void *opaque) |
4621 | 8a16d273 | ths | { |
4622 | 8a16d273 | ths | VDEState *s = opaque; |
4623 | 8a16d273 | ths | uint8_t buf[4096];
|
4624 | 8a16d273 | ths | int size;
|
4625 | 8a16d273 | ths | |
4626 | 8a16d273 | ths | size = vde_recv(s->vde, buf, sizeof(buf), 0); |
4627 | 8a16d273 | ths | if (size > 0) { |
4628 | 8a16d273 | ths | qemu_send_packet(s->vc, buf, size); |
4629 | 8a16d273 | ths | } |
4630 | 8a16d273 | ths | } |
4631 | 8a16d273 | ths | |
4632 | 8a16d273 | ths | static void vde_from_qemu(void *opaque, const uint8_t *buf, int size) |
4633 | 8a16d273 | ths | { |
4634 | 8a16d273 | ths | VDEState *s = opaque; |
4635 | 8a16d273 | ths | int ret;
|
4636 | 8a16d273 | ths | for(;;) {
|
4637 | 8a16d273 | ths | ret = vde_send(s->vde, buf, size, 0);
|
4638 | 8a16d273 | ths | if (ret < 0 && errno == EINTR) { |
4639 | 8a16d273 | ths | } else {
|
4640 | 8a16d273 | ths | break;
|
4641 | 8a16d273 | ths | } |
4642 | 8a16d273 | ths | } |
4643 | 8a16d273 | ths | } |
4644 | 8a16d273 | ths | |
4645 | 8a16d273 | ths | static int net_vde_init(VLANState *vlan, const char *sock, int port, |
4646 | 8a16d273 | ths | const char *group, int mode) |
4647 | 8a16d273 | ths | { |
4648 | 8a16d273 | ths | VDEState *s; |
4649 | 8a16d273 | ths | char *init_group = strlen(group) ? (char *)group : NULL; |
4650 | 8a16d273 | ths | char *init_sock = strlen(sock) ? (char *)sock : NULL; |
4651 | 8a16d273 | ths | |
4652 | 8a16d273 | ths | struct vde_open_args args = {
|
4653 | 8a16d273 | ths | .port = port, |
4654 | 8a16d273 | ths | .group = init_group, |
4655 | 8a16d273 | ths | .mode = mode, |
4656 | 8a16d273 | ths | }; |
4657 | 8a16d273 | ths | |
4658 | 8a16d273 | ths | s = qemu_mallocz(sizeof(VDEState));
|
4659 | 8a16d273 | ths | if (!s)
|
4660 | 8a16d273 | ths | return -1; |
4661 | 8a16d273 | ths | s->vde = vde_open(init_sock, "QEMU", &args);
|
4662 | 8a16d273 | ths | if (!s->vde){
|
4663 | 8a16d273 | ths | free(s); |
4664 | 8a16d273 | ths | return -1; |
4665 | 8a16d273 | ths | } |
4666 | 8a16d273 | ths | s->vc = qemu_new_vlan_client(vlan, vde_from_qemu, NULL, s);
|
4667 | 8a16d273 | ths | qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
|
4668 | 8a16d273 | ths | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "vde: sock=%s fd=%d", |
4669 | 8a16d273 | ths | sock, vde_datafd(s->vde)); |
4670 | 8a16d273 | ths | return 0; |
4671 | 8a16d273 | ths | } |
4672 | 8a16d273 | ths | #endif
|
4673 | 8a16d273 | ths | |
4674 | 7c9d8e07 | bellard | /* network connection */
|
4675 | 7c9d8e07 | bellard | typedef struct NetSocketState { |
4676 | 7c9d8e07 | bellard | VLANClientState *vc; |
4677 | 7c9d8e07 | bellard | int fd;
|
4678 | 7c9d8e07 | bellard | int state; /* 0 = getting length, 1 = getting data */ |
4679 | 7c9d8e07 | bellard | int index;
|
4680 | 7c9d8e07 | bellard | int packet_len;
|
4681 | 7c9d8e07 | bellard | uint8_t buf[4096];
|
4682 | 3d830459 | bellard | struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
4683 | 7c9d8e07 | bellard | } NetSocketState; |
4684 | 7c9d8e07 | bellard | |
4685 | 7c9d8e07 | bellard | typedef struct NetSocketListenState { |
4686 | 7c9d8e07 | bellard | VLANState *vlan; |
4687 | 7c9d8e07 | bellard | int fd;
|
4688 | 7c9d8e07 | bellard | } NetSocketListenState; |
4689 | 7c9d8e07 | bellard | |
4690 | 7c9d8e07 | bellard | /* XXX: we consider we can send the whole packet without blocking */
|
4691 | 7c9d8e07 | bellard | static void net_socket_receive(void *opaque, const uint8_t *buf, int size) |
4692 | c20709aa | bellard | { |
4693 | 7c9d8e07 | bellard | NetSocketState *s = opaque; |
4694 | 7c9d8e07 | bellard | uint32_t len; |
4695 | 7c9d8e07 | bellard | len = htonl(size); |
4696 | 7c9d8e07 | bellard | |
4697 | fd1dff4b | bellard | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
4698 | fd1dff4b | bellard | send_all(s->fd, buf, size); |
4699 | c20709aa | bellard | } |
4700 | c20709aa | bellard | |
4701 | 3d830459 | bellard | static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size) |
4702 | 3d830459 | bellard | { |
4703 | 3d830459 | bellard | NetSocketState *s = opaque; |
4704 | 5fafdf24 | ths | sendto(s->fd, buf, size, 0,
|
4705 | 3d830459 | bellard | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
4706 | 3d830459 | bellard | } |
4707 | 3d830459 | bellard | |
4708 | 7c9d8e07 | bellard | static void net_socket_send(void *opaque) |
4709 | c4b1fcc0 | bellard | { |
4710 | 7c9d8e07 | bellard | NetSocketState *s = opaque; |
4711 | fd1dff4b | bellard | int l, size, err;
|
4712 | 7c9d8e07 | bellard | uint8_t buf1[4096];
|
4713 | 7c9d8e07 | bellard | const uint8_t *buf;
|
4714 | 7c9d8e07 | bellard | |
4715 | fd1dff4b | bellard | size = recv(s->fd, buf1, sizeof(buf1), 0); |
4716 | fd1dff4b | bellard | if (size < 0) { |
4717 | fd1dff4b | bellard | err = socket_error(); |
4718 | 5fafdf24 | ths | if (err != EWOULDBLOCK)
|
4719 | fd1dff4b | bellard | goto eoc;
|
4720 | fd1dff4b | bellard | } else if (size == 0) { |
4721 | 7c9d8e07 | bellard | /* end of connection */
|
4722 | fd1dff4b | bellard | eoc:
|
4723 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
4724 | fd1dff4b | bellard | closesocket(s->fd); |
4725 | 7c9d8e07 | bellard | return;
|
4726 | 7c9d8e07 | bellard | } |
4727 | 7c9d8e07 | bellard | buf = buf1; |
4728 | 7c9d8e07 | bellard | while (size > 0) { |
4729 | 7c9d8e07 | bellard | /* reassemble a packet from the network */
|
4730 | 7c9d8e07 | bellard | switch(s->state) {
|
4731 | 7c9d8e07 | bellard | case 0: |
4732 | 7c9d8e07 | bellard | l = 4 - s->index;
|
4733 | 7c9d8e07 | bellard | if (l > size)
|
4734 | 7c9d8e07 | bellard | l = size; |
4735 | 7c9d8e07 | bellard | memcpy(s->buf + s->index, buf, l); |
4736 | 7c9d8e07 | bellard | buf += l; |
4737 | 7c9d8e07 | bellard | size -= l; |
4738 | 7c9d8e07 | bellard | s->index += l; |
4739 | 7c9d8e07 | bellard | if (s->index == 4) { |
4740 | 7c9d8e07 | bellard | /* got length */
|
4741 | 7c9d8e07 | bellard | s->packet_len = ntohl(*(uint32_t *)s->buf); |
4742 | 7c9d8e07 | bellard | s->index = 0;
|
4743 | 7c9d8e07 | bellard | s->state = 1;
|
4744 | 7c9d8e07 | bellard | } |
4745 | 7c9d8e07 | bellard | break;
|
4746 | 7c9d8e07 | bellard | case 1: |
4747 | 7c9d8e07 | bellard | l = s->packet_len - s->index; |
4748 | 7c9d8e07 | bellard | if (l > size)
|
4749 | 7c9d8e07 | bellard | l = size; |
4750 | 7c9d8e07 | bellard | memcpy(s->buf + s->index, buf, l); |
4751 | 7c9d8e07 | bellard | s->index += l; |
4752 | 7c9d8e07 | bellard | buf += l; |
4753 | 7c9d8e07 | bellard | size -= l; |
4754 | 7c9d8e07 | bellard | if (s->index >= s->packet_len) {
|
4755 | 7c9d8e07 | bellard | qemu_send_packet(s->vc, s->buf, s->packet_len); |
4756 | 7c9d8e07 | bellard | s->index = 0;
|
4757 | 7c9d8e07 | bellard | s->state = 0;
|
4758 | 7c9d8e07 | bellard | } |
4759 | 7c9d8e07 | bellard | break;
|
4760 | 7c9d8e07 | bellard | } |
4761 | 7c9d8e07 | bellard | } |
4762 | c20709aa | bellard | } |
4763 | c20709aa | bellard | |
4764 | 3d830459 | bellard | static void net_socket_send_dgram(void *opaque) |
4765 | 3d830459 | bellard | { |
4766 | 3d830459 | bellard | NetSocketState *s = opaque; |
4767 | 3d830459 | bellard | int size;
|
4768 | 3d830459 | bellard | |
4769 | 3d830459 | bellard | size = recv(s->fd, s->buf, sizeof(s->buf), 0); |
4770 | 5fafdf24 | ths | if (size < 0) |
4771 | 3d830459 | bellard | return;
|
4772 | 3d830459 | bellard | if (size == 0) { |
4773 | 3d830459 | bellard | /* end of connection */
|
4774 | 3d830459 | bellard | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
4775 | 3d830459 | bellard | return;
|
4776 | 3d830459 | bellard | } |
4777 | 3d830459 | bellard | qemu_send_packet(s->vc, s->buf, size); |
4778 | 3d830459 | bellard | } |
4779 | 3d830459 | bellard | |
4780 | 3d830459 | bellard | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
4781 | 3d830459 | bellard | { |
4782 | 3d830459 | bellard | struct ip_mreq imr;
|
4783 | 3d830459 | bellard | int fd;
|
4784 | 3d830459 | bellard | int val, ret;
|
4785 | 3d830459 | bellard | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
|
4786 | 3d830459 | bellard | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
|
4787 | 5fafdf24 | ths | inet_ntoa(mcastaddr->sin_addr), |
4788 | fd1dff4b | bellard | (int)ntohl(mcastaddr->sin_addr.s_addr));
|
4789 | 3d830459 | bellard | return -1; |
4790 | 3d830459 | bellard | |
4791 | 3d830459 | bellard | } |
4792 | 3d830459 | bellard | fd = socket(PF_INET, SOCK_DGRAM, 0);
|
4793 | 3d830459 | bellard | if (fd < 0) { |
4794 | 3d830459 | bellard | perror("socket(PF_INET, SOCK_DGRAM)");
|
4795 | 3d830459 | bellard | return -1; |
4796 | 3d830459 | bellard | } |
4797 | 3d830459 | bellard | |
4798 | fd1dff4b | bellard | val = 1;
|
4799 | 5fafdf24 | ths | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
4800 | fd1dff4b | bellard | (const char *)&val, sizeof(val)); |
4801 | fd1dff4b | bellard | if (ret < 0) { |
4802 | fd1dff4b | bellard | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
|
4803 | fd1dff4b | bellard | goto fail;
|
4804 | fd1dff4b | bellard | } |
4805 | fd1dff4b | bellard | |
4806 | fd1dff4b | bellard | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
4807 | fd1dff4b | bellard | if (ret < 0) { |
4808 | fd1dff4b | bellard | perror("bind");
|
4809 | fd1dff4b | bellard | goto fail;
|
4810 | fd1dff4b | bellard | } |
4811 | 3b46e624 | ths | |
4812 | 3d830459 | bellard | /* Add host to multicast group */
|
4813 | 3d830459 | bellard | imr.imr_multiaddr = mcastaddr->sin_addr; |
4814 | 3d830459 | bellard | imr.imr_interface.s_addr = htonl(INADDR_ANY); |
4815 | 3d830459 | bellard | |
4816 | 5fafdf24 | ths | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
4817 | fd1dff4b | bellard | (const char *)&imr, sizeof(struct ip_mreq)); |
4818 | 3d830459 | bellard | if (ret < 0) { |
4819 | 3d830459 | bellard | perror("setsockopt(IP_ADD_MEMBERSHIP)");
|
4820 | 3d830459 | bellard | goto fail;
|
4821 | 3d830459 | bellard | } |
4822 | 3d830459 | bellard | |
4823 | 3d830459 | bellard | /* Force mcast msgs to loopback (eg. several QEMUs in same host */
|
4824 | 3d830459 | bellard | val = 1;
|
4825 | 5fafdf24 | ths | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
4826 | fd1dff4b | bellard | (const char *)&val, sizeof(val)); |
4827 | 3d830459 | bellard | if (ret < 0) { |
4828 | 3d830459 | bellard | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
|
4829 | 3d830459 | bellard | goto fail;
|
4830 | 3d830459 | bellard | } |
4831 | 3d830459 | bellard | |
4832 | fd1dff4b | bellard | socket_set_nonblock(fd); |
4833 | 3d830459 | bellard | return fd;
|
4834 | 3d830459 | bellard | fail:
|
4835 | 5fafdf24 | ths | if (fd >= 0) |
4836 | 0bab00f3 | bellard | closesocket(fd); |
4837 | 3d830459 | bellard | return -1; |
4838 | 3d830459 | bellard | } |
4839 | 3d830459 | bellard | |
4840 | 5fafdf24 | ths | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, |
4841 | 3d830459 | bellard | int is_connected)
|
4842 | 3d830459 | bellard | { |
4843 | 3d830459 | bellard | struct sockaddr_in saddr;
|
4844 | 3d830459 | bellard | int newfd;
|
4845 | 3d830459 | bellard | socklen_t saddr_len; |
4846 | 3d830459 | bellard | NetSocketState *s; |
4847 | 3d830459 | bellard | |
4848 | 3d830459 | bellard | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
|
4849 | 5fafdf24 | ths | * Because this may be "shared" socket from a "master" process, datagrams would be recv()
|
4850 | 3d830459 | bellard | * by ONLY ONE process: we must "clone" this dgram socket --jjo
|
4851 | 3d830459 | bellard | */
|
4852 | 3d830459 | bellard | |
4853 | 3d830459 | bellard | if (is_connected) {
|
4854 | 3d830459 | bellard | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
4855 | 3d830459 | bellard | /* must be bound */
|
4856 | 3d830459 | bellard | if (saddr.sin_addr.s_addr==0) { |
4857 | 3d830459 | bellard | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
|
4858 | 3d830459 | bellard | fd); |
4859 | 3d830459 | bellard | return NULL; |
4860 | 3d830459 | bellard | } |
4861 | 3d830459 | bellard | /* clone dgram socket */
|
4862 | 3d830459 | bellard | newfd = net_socket_mcast_create(&saddr); |
4863 | 3d830459 | bellard | if (newfd < 0) { |
4864 | 3d830459 | bellard | /* error already reported by net_socket_mcast_create() */
|
4865 | 3d830459 | bellard | close(fd); |
4866 | 3d830459 | bellard | return NULL; |
4867 | 3d830459 | bellard | } |
4868 | 3d830459 | bellard | /* clone newfd to fd, close newfd */
|
4869 | 3d830459 | bellard | dup2(newfd, fd); |
4870 | 3d830459 | bellard | close(newfd); |
4871 | 5fafdf24 | ths | |
4872 | 3d830459 | bellard | } else {
|
4873 | 3d830459 | bellard | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
|
4874 | 3d830459 | bellard | fd, strerror(errno)); |
4875 | 3d830459 | bellard | return NULL; |
4876 | 3d830459 | bellard | } |
4877 | 3d830459 | bellard | } |
4878 | 3d830459 | bellard | |
4879 | 3d830459 | bellard | s = qemu_mallocz(sizeof(NetSocketState));
|
4880 | 3d830459 | bellard | if (!s)
|
4881 | 3d830459 | bellard | return NULL; |
4882 | 3d830459 | bellard | s->fd = fd; |
4883 | 3d830459 | bellard | |
4884 | d861b05e | pbrook | s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
|
4885 | 3d830459 | bellard | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
|
4886 | 3d830459 | bellard | |
4887 | 3d830459 | bellard | /* mcast: save bound address as dst */
|
4888 | 3d830459 | bellard | if (is_connected) s->dgram_dst=saddr;
|
4889 | 3d830459 | bellard | |
4890 | 3d830459 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4891 | 5fafdf24 | ths | "socket: fd=%d (%s mcast=%s:%d)",
|
4892 | 3d830459 | bellard | fd, is_connected? "cloned" : "", |
4893 | 3d830459 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4894 | 3d830459 | bellard | return s;
|
4895 | 3d830459 | bellard | } |
4896 | 3d830459 | bellard | |
4897 | 7c9d8e07 | bellard | static void net_socket_connect(void *opaque) |
4898 | c20709aa | bellard | { |
4899 | 7c9d8e07 | bellard | NetSocketState *s = opaque; |
4900 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
|
4901 | 7c9d8e07 | bellard | } |
4902 | c4b1fcc0 | bellard | |
4903 | 5fafdf24 | ths | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, |
4904 | 7c9d8e07 | bellard | int is_connected)
|
4905 | 7c9d8e07 | bellard | { |
4906 | 7c9d8e07 | bellard | NetSocketState *s; |
4907 | 7c9d8e07 | bellard | s = qemu_mallocz(sizeof(NetSocketState));
|
4908 | 7c9d8e07 | bellard | if (!s)
|
4909 | 7c9d8e07 | bellard | return NULL; |
4910 | 7c9d8e07 | bellard | s->fd = fd; |
4911 | 5fafdf24 | ths | s->vc = qemu_new_vlan_client(vlan, |
4912 | d861b05e | pbrook | net_socket_receive, NULL, s);
|
4913 | 7c9d8e07 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4914 | 7c9d8e07 | bellard | "socket: fd=%d", fd);
|
4915 | 7c9d8e07 | bellard | if (is_connected) {
|
4916 | 7c9d8e07 | bellard | net_socket_connect(s); |
4917 | 7c9d8e07 | bellard | } else {
|
4918 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
|
4919 | 7c9d8e07 | bellard | } |
4920 | 7c9d8e07 | bellard | return s;
|
4921 | 7c9d8e07 | bellard | } |
4922 | c4b1fcc0 | bellard | |
4923 | 5fafdf24 | ths | static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, |
4924 | 3d830459 | bellard | int is_connected)
|
4925 | 3d830459 | bellard | { |
4926 | 3d830459 | bellard | int so_type=-1, optlen=sizeof(so_type); |
4927 | 3d830459 | bellard | |
4928 | 69b34976 | ths | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, |
4929 | 69b34976 | ths | (socklen_t *)&optlen)< 0) {
|
4930 | 931f03ee | ths | fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
|
4931 | 3d830459 | bellard | return NULL; |
4932 | 3d830459 | bellard | } |
4933 | 3d830459 | bellard | switch(so_type) {
|
4934 | 3d830459 | bellard | case SOCK_DGRAM:
|
4935 | 3d830459 | bellard | return net_socket_fd_init_dgram(vlan, fd, is_connected);
|
4936 | 3d830459 | bellard | case SOCK_STREAM:
|
4937 | 3d830459 | bellard | return net_socket_fd_init_stream(vlan, fd, is_connected);
|
4938 | 3d830459 | bellard | default:
|
4939 | 3d830459 | bellard | /* who knows ... this could be a eg. a pty, do warn and continue as stream */
|
4940 | 3d830459 | bellard | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
|
4941 | 3d830459 | bellard | return net_socket_fd_init_stream(vlan, fd, is_connected);
|
4942 | 3d830459 | bellard | } |
4943 | 3d830459 | bellard | return NULL; |
4944 | 3d830459 | bellard | } |
4945 | 3d830459 | bellard | |
4946 | 7c9d8e07 | bellard | static void net_socket_accept(void *opaque) |
4947 | 7c9d8e07 | bellard | { |
4948 | 3b46e624 | ths | NetSocketListenState *s = opaque; |
4949 | 7c9d8e07 | bellard | NetSocketState *s1; |
4950 | 7c9d8e07 | bellard | struct sockaddr_in saddr;
|
4951 | 7c9d8e07 | bellard | socklen_t len; |
4952 | 7c9d8e07 | bellard | int fd;
|
4953 | 7c9d8e07 | bellard | |
4954 | 7c9d8e07 | bellard | for(;;) {
|
4955 | 7c9d8e07 | bellard | len = sizeof(saddr);
|
4956 | 7c9d8e07 | bellard | fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
|
4957 | 7c9d8e07 | bellard | if (fd < 0 && errno != EINTR) { |
4958 | 7c9d8e07 | bellard | return;
|
4959 | 7c9d8e07 | bellard | } else if (fd >= 0) { |
4960 | 7c9d8e07 | bellard | break;
|
4961 | 80cabfad | bellard | } |
4962 | 330d0414 | bellard | } |
4963 | 5fafdf24 | ths | s1 = net_socket_fd_init(s->vlan, fd, 1);
|
4964 | 7c9d8e07 | bellard | if (!s1) {
|
4965 | 0bab00f3 | bellard | closesocket(fd); |
4966 | 7c9d8e07 | bellard | } else {
|
4967 | 7c9d8e07 | bellard | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
|
4968 | 5fafdf24 | ths | "socket: connection from %s:%d",
|
4969 | 7c9d8e07 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4970 | 7c9d8e07 | bellard | } |
4971 | 7c9d8e07 | bellard | } |
4972 | 7c9d8e07 | bellard | |
4973 | 7c9d8e07 | bellard | static int net_socket_listen_init(VLANState *vlan, const char *host_str) |
4974 | 7c9d8e07 | bellard | { |
4975 | 7c9d8e07 | bellard | NetSocketListenState *s; |
4976 | 7c9d8e07 | bellard | int fd, val, ret;
|
4977 | 7c9d8e07 | bellard | struct sockaddr_in saddr;
|
4978 | 7c9d8e07 | bellard | |
4979 | 7c9d8e07 | bellard | if (parse_host_port(&saddr, host_str) < 0) |
4980 | 7c9d8e07 | bellard | return -1; |
4981 | 3b46e624 | ths | |
4982 | 7c9d8e07 | bellard | s = qemu_mallocz(sizeof(NetSocketListenState));
|
4983 | 7c9d8e07 | bellard | if (!s)
|
4984 | 7c9d8e07 | bellard | return -1; |
4985 | 7c9d8e07 | bellard | |
4986 | 7c9d8e07 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
4987 | 7c9d8e07 | bellard | if (fd < 0) { |
4988 | 7c9d8e07 | bellard | perror("socket");
|
4989 | 7c9d8e07 | bellard | return -1; |
4990 | 7c9d8e07 | bellard | } |
4991 | fd1dff4b | bellard | socket_set_nonblock(fd); |
4992 | 7c9d8e07 | bellard | |
4993 | 7c9d8e07 | bellard | /* allow fast reuse */
|
4994 | 7c9d8e07 | bellard | val = 1;
|
4995 | fd1dff4b | bellard | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
4996 | 3b46e624 | ths | |
4997 | 7c9d8e07 | bellard | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
4998 | 7c9d8e07 | bellard | if (ret < 0) { |
4999 | 7c9d8e07 | bellard | perror("bind");
|
5000 | 7c9d8e07 | bellard | return -1; |
5001 | 7c9d8e07 | bellard | } |
5002 | 7c9d8e07 | bellard | ret = listen(fd, 0);
|
5003 | 7c9d8e07 | bellard | if (ret < 0) { |
5004 | 7c9d8e07 | bellard | perror("listen");
|
5005 | 7c9d8e07 | bellard | return -1; |
5006 | 7c9d8e07 | bellard | } |
5007 | 7c9d8e07 | bellard | s->vlan = vlan; |
5008 | 7c9d8e07 | bellard | s->fd = fd; |
5009 | 7c9d8e07 | bellard | qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
|
5010 | 80cabfad | bellard | return 0; |
5011 | 330d0414 | bellard | } |
5012 | 330d0414 | bellard | |
5013 | 7c9d8e07 | bellard | static int net_socket_connect_init(VLANState *vlan, const char *host_str) |
5014 | 330d0414 | bellard | { |
5015 | 7c9d8e07 | bellard | NetSocketState *s; |
5016 | fd1dff4b | bellard | int fd, connected, ret, err;
|
5017 | 7c9d8e07 | bellard | struct sockaddr_in saddr;
|
5018 | 7c9d8e07 | bellard | |
5019 | 7c9d8e07 | bellard | if (parse_host_port(&saddr, host_str) < 0) |
5020 | 7c9d8e07 | bellard | return -1; |
5021 | 7c9d8e07 | bellard | |
5022 | 7c9d8e07 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
5023 | 7c9d8e07 | bellard | if (fd < 0) { |
5024 | 7c9d8e07 | bellard | perror("socket");
|
5025 | 7c9d8e07 | bellard | return -1; |
5026 | 7c9d8e07 | bellard | } |
5027 | fd1dff4b | bellard | socket_set_nonblock(fd); |
5028 | 7c9d8e07 | bellard | |
5029 | 7c9d8e07 | bellard | connected = 0;
|
5030 | 7c9d8e07 | bellard | for(;;) {
|
5031 | 7c9d8e07 | bellard | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
5032 | 7c9d8e07 | bellard | if (ret < 0) { |
5033 | fd1dff4b | bellard | err = socket_error(); |
5034 | fd1dff4b | bellard | if (err == EINTR || err == EWOULDBLOCK) {
|
5035 | fd1dff4b | bellard | } else if (err == EINPROGRESS) { |
5036 | 7c9d8e07 | bellard | break;
|
5037 | f5b12268 | ths | #ifdef _WIN32
|
5038 | f5b12268 | ths | } else if (err == WSAEALREADY) { |
5039 | f5b12268 | ths | break;
|
5040 | f5b12268 | ths | #endif
|
5041 | 7c9d8e07 | bellard | } else {
|
5042 | 7c9d8e07 | bellard | perror("connect");
|
5043 | fd1dff4b | bellard | closesocket(fd); |
5044 | 7c9d8e07 | bellard | return -1; |
5045 | 7c9d8e07 | bellard | } |
5046 | 7c9d8e07 | bellard | } else {
|
5047 | 7c9d8e07 | bellard | connected = 1;
|
5048 | 7c9d8e07 | bellard | break;
|
5049 | 7c9d8e07 | bellard | } |
5050 | 7c9d8e07 | bellard | } |
5051 | 7c9d8e07 | bellard | s = net_socket_fd_init(vlan, fd, connected); |
5052 | 7c9d8e07 | bellard | if (!s)
|
5053 | 7c9d8e07 | bellard | return -1; |
5054 | 7c9d8e07 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
5055 | 5fafdf24 | ths | "socket: connect to %s:%d",
|
5056 | 7c9d8e07 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
5057 | c20709aa | bellard | return 0; |
5058 | 80cabfad | bellard | } |
5059 | 330d0414 | bellard | |
5060 | 3d830459 | bellard | static int net_socket_mcast_init(VLANState *vlan, const char *host_str) |
5061 | 3d830459 | bellard | { |
5062 | 3d830459 | bellard | NetSocketState *s; |
5063 | 3d830459 | bellard | int fd;
|
5064 | 3d830459 | bellard | struct sockaddr_in saddr;
|
5065 | 3d830459 | bellard | |
5066 | 3d830459 | bellard | if (parse_host_port(&saddr, host_str) < 0) |
5067 | 3d830459 | bellard | return -1; |
5068 | 3d830459 | bellard | |
5069 | 3d830459 | bellard | |
5070 | 3d830459 | bellard | fd = net_socket_mcast_create(&saddr); |
5071 | 3d830459 | bellard | if (fd < 0) |
5072 | 3d830459 | bellard | return -1; |
5073 | 3d830459 | bellard | |
5074 | 3d830459 | bellard | s = net_socket_fd_init(vlan, fd, 0);
|
5075 | 3d830459 | bellard | if (!s)
|
5076 | 3d830459 | bellard | return -1; |
5077 | 3d830459 | bellard | |
5078 | 3d830459 | bellard | s->dgram_dst = saddr; |
5079 | 3b46e624 | ths | |
5080 | 3d830459 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
5081 | 5fafdf24 | ths | "socket: mcast=%s:%d",
|
5082 | 3d830459 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
5083 | 3d830459 | bellard | return 0; |
5084 | 3d830459 | bellard | |
5085 | 3d830459 | bellard | } |
5086 | 3d830459 | bellard | |
5087 | 609497ab | balrog | static const char *get_opt_name(char *buf, int buf_size, const char *p) |
5088 | 609497ab | balrog | { |
5089 | 609497ab | balrog | char *q;
|
5090 | 609497ab | balrog | |
5091 | 609497ab | balrog | q = buf; |
5092 | 609497ab | balrog | while (*p != '\0' && *p != '=') { |
5093 | 609497ab | balrog | if (q && (q - buf) < buf_size - 1) |
5094 | 609497ab | balrog | *q++ = *p; |
5095 | 609497ab | balrog | p++; |
5096 | 609497ab | balrog | } |
5097 | 609497ab | balrog | if (q)
|
5098 | 609497ab | balrog | *q = '\0';
|
5099 | 609497ab | balrog | |
5100 | 609497ab | balrog | return p;
|
5101 | 609497ab | balrog | } |
5102 | 609497ab | balrog | |
5103 | 609497ab | balrog | static const char *get_opt_value(char *buf, int buf_size, const char *p) |
5104 | e4bcb14c | ths | { |
5105 | e4bcb14c | ths | char *q;
|
5106 | e4bcb14c | ths | |
5107 | e4bcb14c | ths | q = buf; |
5108 | e4bcb14c | ths | while (*p != '\0') { |
5109 | 609497ab | balrog | if (*p == ',') { |
5110 | 609497ab | balrog | if (*(p + 1) != ',') |
5111 | e4bcb14c | ths | break;
|
5112 | e4bcb14c | ths | p++; |
5113 | 609497ab | balrog | } |
5114 | e4bcb14c | ths | if (q && (q - buf) < buf_size - 1) |
5115 | e4bcb14c | ths | *q++ = *p; |
5116 | e4bcb14c | ths | p++; |
5117 | e4bcb14c | ths | } |
5118 | e4bcb14c | ths | if (q)
|
5119 | e4bcb14c | ths | *q = '\0';
|
5120 | e4bcb14c | ths | |
5121 | e4bcb14c | ths | return p;
|
5122 | e4bcb14c | ths | } |
5123 | e4bcb14c | ths | |
5124 | 7c9d8e07 | bellard | static int get_param_value(char *buf, int buf_size, |
5125 | 7c9d8e07 | bellard | const char *tag, const char *str) |
5126 | 7c9d8e07 | bellard | { |
5127 | 7c9d8e07 | bellard | const char *p; |
5128 | 7c9d8e07 | bellard | char option[128]; |
5129 | 7c9d8e07 | bellard | |
5130 | 7c9d8e07 | bellard | p = str; |
5131 | 7c9d8e07 | bellard | for(;;) {
|
5132 | 609497ab | balrog | p = get_opt_name(option, sizeof(option), p);
|
5133 | 7c9d8e07 | bellard | if (*p != '=') |
5134 | 7c9d8e07 | bellard | break;
|
5135 | 7c9d8e07 | bellard | p++; |
5136 | 7c9d8e07 | bellard | if (!strcmp(tag, option)) {
|
5137 | 609497ab | balrog | (void)get_opt_value(buf, buf_size, p);
|
5138 | e4bcb14c | ths | return strlen(buf);
|
5139 | 7c9d8e07 | bellard | } else {
|
5140 | 609497ab | balrog | p = get_opt_value(NULL, 0, p); |
5141 | 7c9d8e07 | bellard | } |
5142 | 7c9d8e07 | bellard | if (*p != ',') |
5143 | 7c9d8e07 | bellard | break;
|
5144 | 7c9d8e07 | bellard | p++; |
5145 | 7c9d8e07 | bellard | } |
5146 | 7c9d8e07 | bellard | return 0; |
5147 | 7c9d8e07 | bellard | } |
5148 | 7c9d8e07 | bellard | |
5149 | e4bcb14c | ths | static int check_params(char *buf, int buf_size, |
5150 | e4bcb14c | ths | char **params, const char *str) |
5151 | e4bcb14c | ths | { |
5152 | e4bcb14c | ths | const char *p; |
5153 | e4bcb14c | ths | int i;
|
5154 | e4bcb14c | ths | |
5155 | e4bcb14c | ths | p = str; |
5156 | e4bcb14c | ths | for(;;) {
|
5157 | 609497ab | balrog | p = get_opt_name(buf, buf_size, p); |
5158 | e4bcb14c | ths | if (*p != '=') |
5159 | e4bcb14c | ths | return -1; |
5160 | e4bcb14c | ths | p++; |
5161 | e4bcb14c | ths | for(i = 0; params[i] != NULL; i++) |
5162 | e4bcb14c | ths | if (!strcmp(params[i], buf))
|
5163 | e4bcb14c | ths | break;
|
5164 | e4bcb14c | ths | if (params[i] == NULL) |
5165 | e4bcb14c | ths | return -1; |
5166 | 609497ab | balrog | p = get_opt_value(NULL, 0, p); |
5167 | e4bcb14c | ths | if (*p != ',') |
5168 | e4bcb14c | ths | break;
|
5169 | e4bcb14c | ths | p++; |
5170 | e4bcb14c | ths | } |
5171 | e4bcb14c | ths | return 0; |
5172 | e4bcb14c | ths | } |
5173 | e4bcb14c | ths | |
5174 | 9ad97e65 | balrog | static int net_client_init(const char *device, const char *p) |
5175 | 7c9d8e07 | bellard | { |
5176 | 7c9d8e07 | bellard | char buf[1024]; |
5177 | 7c9d8e07 | bellard | int vlan_id, ret;
|
5178 | 7c9d8e07 | bellard | VLANState *vlan; |
5179 | 7c9d8e07 | bellard | |
5180 | 7c9d8e07 | bellard | vlan_id = 0;
|
5181 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "vlan", p)) { |
5182 | 7c9d8e07 | bellard | vlan_id = strtol(buf, NULL, 0); |
5183 | 7c9d8e07 | bellard | } |
5184 | 7c9d8e07 | bellard | vlan = qemu_find_vlan(vlan_id); |
5185 | 7c9d8e07 | bellard | if (!vlan) {
|
5186 | 7c9d8e07 | bellard | fprintf(stderr, "Could not create vlan %d\n", vlan_id);
|
5187 | 7c9d8e07 | bellard | return -1; |
5188 | 7c9d8e07 | bellard | } |
5189 | 7c9d8e07 | bellard | if (!strcmp(device, "nic")) { |
5190 | 7c9d8e07 | bellard | NICInfo *nd; |
5191 | 7c9d8e07 | bellard | uint8_t *macaddr; |
5192 | 7c9d8e07 | bellard | |
5193 | 7c9d8e07 | bellard | if (nb_nics >= MAX_NICS) {
|
5194 | 7c9d8e07 | bellard | fprintf(stderr, "Too Many NICs\n");
|
5195 | 7c9d8e07 | bellard | return -1; |
5196 | 7c9d8e07 | bellard | } |
5197 | 7c9d8e07 | bellard | nd = &nd_table[nb_nics]; |
5198 | 7c9d8e07 | bellard | macaddr = nd->macaddr; |
5199 | 7c9d8e07 | bellard | macaddr[0] = 0x52; |
5200 | 7c9d8e07 | bellard | macaddr[1] = 0x54; |
5201 | 7c9d8e07 | bellard | macaddr[2] = 0x00; |
5202 | 7c9d8e07 | bellard | macaddr[3] = 0x12; |
5203 | 7c9d8e07 | bellard | macaddr[4] = 0x34; |
5204 | 7c9d8e07 | bellard | macaddr[5] = 0x56 + nb_nics; |
5205 | 7c9d8e07 | bellard | |
5206 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "macaddr", p)) { |
5207 | 7c9d8e07 | bellard | if (parse_macaddr(macaddr, buf) < 0) { |
5208 | 7c9d8e07 | bellard | fprintf(stderr, "invalid syntax for ethernet address\n");
|
5209 | 7c9d8e07 | bellard | return -1; |
5210 | 7c9d8e07 | bellard | } |
5211 | 7c9d8e07 | bellard | } |
5212 | a41b2ff2 | pbrook | if (get_param_value(buf, sizeof(buf), "model", p)) { |
5213 | a41b2ff2 | pbrook | nd->model = strdup(buf); |
5214 | a41b2ff2 | pbrook | } |
5215 | 7c9d8e07 | bellard | nd->vlan = vlan; |
5216 | 7c9d8e07 | bellard | nb_nics++; |
5217 | 833c7174 | blueswir1 | vlan->nb_guest_devs++; |
5218 | 7c9d8e07 | bellard | ret = 0;
|
5219 | 7c9d8e07 | bellard | } else
|
5220 | 7c9d8e07 | bellard | if (!strcmp(device, "none")) { |
5221 | 7c9d8e07 | bellard | /* does nothing. It is needed to signal that no network cards
|
5222 | 7c9d8e07 | bellard | are wanted */
|
5223 | 7c9d8e07 | bellard | ret = 0;
|
5224 | 7c9d8e07 | bellard | } else
|
5225 | 7c9d8e07 | bellard | #ifdef CONFIG_SLIRP
|
5226 | 7c9d8e07 | bellard | if (!strcmp(device, "user")) { |
5227 | 115defd1 | pbrook | if (get_param_value(buf, sizeof(buf), "hostname", p)) { |
5228 | 3f423c9c | bellard | pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
|
5229 | 115defd1 | pbrook | } |
5230 | 833c7174 | blueswir1 | vlan->nb_host_devs++; |
5231 | 7c9d8e07 | bellard | ret = net_slirp_init(vlan); |
5232 | 7c9d8e07 | bellard | } else
|
5233 | 7c9d8e07 | bellard | #endif
|
5234 | 7fb843f8 | bellard | #ifdef _WIN32
|
5235 | 7fb843f8 | bellard | if (!strcmp(device, "tap")) { |
5236 | 7fb843f8 | bellard | char ifname[64]; |
5237 | 7fb843f8 | bellard | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
5238 | 7fb843f8 | bellard | fprintf(stderr, "tap: no interface name\n");
|
5239 | 7fb843f8 | bellard | return -1; |
5240 | 7fb843f8 | bellard | } |
5241 | 833c7174 | blueswir1 | vlan->nb_host_devs++; |
5242 | 7fb843f8 | bellard | ret = tap_win32_init(vlan, ifname); |
5243 | 7fb843f8 | bellard | } else
|
5244 | 7fb843f8 | bellard | #else
|
5245 | 7c9d8e07 | bellard | if (!strcmp(device, "tap")) { |
5246 | 7c9d8e07 | bellard | char ifname[64]; |
5247 | b46a8906 | ths | char setup_script[1024], down_script[1024]; |
5248 | 7c9d8e07 | bellard | int fd;
|
5249 | 4f01035f | pbrook | vlan->nb_host_devs++; |
5250 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
5251 | 7c9d8e07 | bellard | fd = strtol(buf, NULL, 0); |
5252 | 64538cdf | pbrook | fcntl(fd, F_SETFL, O_NONBLOCK); |
5253 | 7c9d8e07 | bellard | ret = -1;
|
5254 | 7c9d8e07 | bellard | if (net_tap_fd_init(vlan, fd))
|
5255 | 7c9d8e07 | bellard | ret = 0;
|
5256 | 7c9d8e07 | bellard | } else {
|
5257 | bf8c5342 | bellard | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
5258 | bf8c5342 | bellard | ifname[0] = '\0'; |
5259 | bf8c5342 | bellard | } |
5260 | 7c9d8e07 | bellard | if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) { |
5261 | 7c9d8e07 | bellard | pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
|
5262 | 7c9d8e07 | bellard | } |
5263 | b46a8906 | ths | if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) { |
5264 | b46a8906 | ths | pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
|
5265 | b46a8906 | ths | } |
5266 | b46a8906 | ths | ret = net_tap_init(vlan, ifname, setup_script, down_script); |
5267 | 7c9d8e07 | bellard | } |
5268 | 7c9d8e07 | bellard | } else
|
5269 | fd1dff4b | bellard | #endif
|
5270 | 7c9d8e07 | bellard | if (!strcmp(device, "socket")) { |
5271 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
5272 | 7c9d8e07 | bellard | int fd;
|
5273 | 7c9d8e07 | bellard | fd = strtol(buf, NULL, 0); |
5274 | 7c9d8e07 | bellard | ret = -1;
|
5275 | 7c9d8e07 | bellard | if (net_socket_fd_init(vlan, fd, 1)) |
5276 | 7c9d8e07 | bellard | ret = 0;
|
5277 | 7c9d8e07 | bellard | } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) { |
5278 | 7c9d8e07 | bellard | ret = net_socket_listen_init(vlan, buf); |
5279 | 7c9d8e07 | bellard | } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { |
5280 | 7c9d8e07 | bellard | ret = net_socket_connect_init(vlan, buf); |
5281 | 3d830459 | bellard | } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { |
5282 | 3d830459 | bellard | ret = net_socket_mcast_init(vlan, buf); |
5283 | 7c9d8e07 | bellard | } else {
|
5284 | 7c9d8e07 | bellard | fprintf(stderr, "Unknown socket options: %s\n", p);
|
5285 | 7c9d8e07 | bellard | return -1; |
5286 | 7c9d8e07 | bellard | } |
5287 | 833c7174 | blueswir1 | vlan->nb_host_devs++; |
5288 | 7c9d8e07 | bellard | } else
|
5289 | 8a16d273 | ths | #ifdef CONFIG_VDE
|
5290 | 8a16d273 | ths | if (!strcmp(device, "vde")) { |
5291 | 8a16d273 | ths | char vde_sock[1024], vde_group[512]; |
5292 | 8a16d273 | ths | int vde_port, vde_mode;
|
5293 | 8a16d273 | ths | vlan->nb_host_devs++; |
5294 | 8a16d273 | ths | if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) { |
5295 | 8a16d273 | ths | vde_sock[0] = '\0'; |
5296 | 8a16d273 | ths | } |
5297 | 8a16d273 | ths | if (get_param_value(buf, sizeof(buf), "port", p) > 0) { |
5298 | 8a16d273 | ths | vde_port = strtol(buf, NULL, 10); |
5299 | 8a16d273 | ths | } else {
|
5300 | 8a16d273 | ths | vde_port = 0;
|
5301 | 8a16d273 | ths | } |
5302 | 8a16d273 | ths | if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) { |
5303 | 8a16d273 | ths | vde_group[0] = '\0'; |
5304 | 8a16d273 | ths | } |
5305 | 8a16d273 | ths | if (get_param_value(buf, sizeof(buf), "mode", p) > 0) { |
5306 | 8a16d273 | ths | vde_mode = strtol(buf, NULL, 8); |
5307 | 8a16d273 | ths | } else {
|
5308 | 8a16d273 | ths | vde_mode = 0700;
|
5309 | 8a16d273 | ths | } |
5310 | 8a16d273 | ths | ret = net_vde_init(vlan, vde_sock, vde_port, vde_group, vde_mode); |
5311 | 8a16d273 | ths | } else
|
5312 | 8a16d273 | ths | #endif
|
5313 | 7c9d8e07 | bellard | { |
5314 | 7c9d8e07 | bellard | fprintf(stderr, "Unknown network device: %s\n", device);
|
5315 | 7c9d8e07 | bellard | return -1; |
5316 | 7c9d8e07 | bellard | } |
5317 | 7c9d8e07 | bellard | if (ret < 0) { |
5318 | 7c9d8e07 | bellard | fprintf(stderr, "Could not initialize device '%s'\n", device);
|
5319 | 7c9d8e07 | bellard | } |
5320 | 3b46e624 | ths | |
5321 | 7c9d8e07 | bellard | return ret;
|
5322 | 7c9d8e07 | bellard | } |
5323 | 7c9d8e07 | bellard | |
5324 | 9ad97e65 | balrog | static int net_client_parse(const char *str) |
5325 | 9ad97e65 | balrog | { |
5326 | 9ad97e65 | balrog | const char *p; |
5327 | 9ad97e65 | balrog | char *q;
|
5328 | 9ad97e65 | balrog | char device[64]; |
5329 | 9ad97e65 | balrog | |
5330 | 9ad97e65 | balrog | p = str; |
5331 | 9ad97e65 | balrog | q = device; |
5332 | 9ad97e65 | balrog | while (*p != '\0' && *p != ',') { |
5333 | 9ad97e65 | balrog | if ((q - device) < sizeof(device) - 1) |
5334 | 9ad97e65 | balrog | *q++ = *p; |
5335 | 9ad97e65 | balrog | p++; |
5336 | 9ad97e65 | balrog | } |
5337 | 9ad97e65 | balrog | *q = '\0';
|
5338 | 9ad97e65 | balrog | if (*p == ',') |
5339 | 9ad97e65 | balrog | p++; |
5340 | 9ad97e65 | balrog | |
5341 | 9ad97e65 | balrog | return net_client_init(device, p);
|
5342 | 9ad97e65 | balrog | } |
5343 | 9ad97e65 | balrog | |
5344 | 7c9d8e07 | bellard | void do_info_network(void) |
5345 | 7c9d8e07 | bellard | { |
5346 | 7c9d8e07 | bellard | VLANState *vlan; |
5347 | 7c9d8e07 | bellard | VLANClientState *vc; |
5348 | 7c9d8e07 | bellard | |
5349 | 7c9d8e07 | bellard | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
5350 | 7c9d8e07 | bellard | term_printf("VLAN %d devices:\n", vlan->id);
|
5351 | 7c9d8e07 | bellard | for(vc = vlan->first_client; vc != NULL; vc = vc->next) |
5352 | 7c9d8e07 | bellard | term_printf(" %s\n", vc->info_str);
|
5353 | 7c9d8e07 | bellard | } |
5354 | 7c9d8e07 | bellard | } |
5355 | 42550fde | ths | |
5356 | 609497ab | balrog | #define HD_ALIAS "index=%d,media=disk" |
5357 | e4bcb14c | ths | #ifdef TARGET_PPC
|
5358 | e4bcb14c | ths | #define CDROM_ALIAS "index=1,media=cdrom" |
5359 | e4bcb14c | ths | #else
|
5360 | e4bcb14c | ths | #define CDROM_ALIAS "index=2,media=cdrom" |
5361 | e4bcb14c | ths | #endif
|
5362 | e4bcb14c | ths | #define FD_ALIAS "index=%d,if=floppy" |
5363 | 609497ab | balrog | #define PFLASH_ALIAS "if=pflash" |
5364 | 609497ab | balrog | #define MTD_ALIAS "if=mtd" |
5365 | 9d413d1d | balrog | #define SD_ALIAS "index=0,if=sd" |
5366 | e4bcb14c | ths | |
5367 | 609497ab | balrog | static int drive_add(const char *file, const char *fmt, ...) |
5368 | e4bcb14c | ths | { |
5369 | e4bcb14c | ths | va_list ap; |
5370 | e4bcb14c | ths | |
5371 | e4bcb14c | ths | if (nb_drives_opt >= MAX_DRIVES) {
|
5372 | e4bcb14c | ths | fprintf(stderr, "qemu: too many drives\n");
|
5373 | e4bcb14c | ths | exit(1);
|
5374 | e4bcb14c | ths | } |
5375 | e4bcb14c | ths | |
5376 | 609497ab | balrog | drives_opt[nb_drives_opt].file = file; |
5377 | e4bcb14c | ths | va_start(ap, fmt); |
5378 | 609497ab | balrog | vsnprintf(drives_opt[nb_drives_opt].opt, |
5379 | 609497ab | balrog | sizeof(drives_opt[0].opt), fmt, ap); |
5380 | e4bcb14c | ths | va_end(ap); |
5381 | e4bcb14c | ths | |
5382 | e4bcb14c | ths | return nb_drives_opt++;
|
5383 | e4bcb14c | ths | } |
5384 | e4bcb14c | ths | |
5385 | f60d39bc | ths | int drive_get_index(BlockInterfaceType type, int bus, int unit) |
5386 | e4bcb14c | ths | { |
5387 | e4bcb14c | ths | int index;
|
5388 | e4bcb14c | ths | |
5389 | e4bcb14c | ths | /* seek interface, bus and unit */
|
5390 | e4bcb14c | ths | |
5391 | e4bcb14c | ths | for (index = 0; index < nb_drives; index++) |
5392 | f60d39bc | ths | if (drives_table[index].type == type &&
|
5393 | e4bcb14c | ths | drives_table[index].bus == bus && |
5394 | e4bcb14c | ths | drives_table[index].unit == unit) |
5395 | e4bcb14c | ths | return index;
|
5396 | e4bcb14c | ths | |
5397 | e4bcb14c | ths | return -1; |
5398 | e4bcb14c | ths | } |
5399 | e4bcb14c | ths | |
5400 | f60d39bc | ths | int drive_get_max_bus(BlockInterfaceType type)
|
5401 | e4bcb14c | ths | { |
5402 | e4bcb14c | ths | int max_bus;
|
5403 | e4bcb14c | ths | int index;
|
5404 | e4bcb14c | ths | |
5405 | e4bcb14c | ths | max_bus = -1;
|
5406 | e4bcb14c | ths | for (index = 0; index < nb_drives; index++) { |
5407 | f60d39bc | ths | if(drives_table[index].type == type &&
|
5408 | e4bcb14c | ths | drives_table[index].bus > max_bus) |
5409 | e4bcb14c | ths | max_bus = drives_table[index].bus; |
5410 | e4bcb14c | ths | } |
5411 | e4bcb14c | ths | return max_bus;
|
5412 | e4bcb14c | ths | } |
5413 | e4bcb14c | ths | |
5414 | a1620fac | aurel32 | static void bdrv_format_print(void *opaque, const char *name) |
5415 | a1620fac | aurel32 | { |
5416 | a1620fac | aurel32 | fprintf(stderr, " %s", name);
|
5417 | a1620fac | aurel32 | } |
5418 | a1620fac | aurel32 | |
5419 | 609497ab | balrog | static int drive_init(struct drive_opt *arg, int snapshot, |
5420 | 609497ab | balrog | QEMUMachine *machine) |
5421 | e4bcb14c | ths | { |
5422 | e4bcb14c | ths | char buf[128]; |
5423 | e4bcb14c | ths | char file[1024]; |
5424 | c8522bdf | balrog | char devname[128]; |
5425 | c8522bdf | balrog | const char *mediastr = ""; |
5426 | f60d39bc | ths | BlockInterfaceType type; |
5427 | e4bcb14c | ths | enum { MEDIA_DISK, MEDIA_CDROM } media;
|
5428 | e4bcb14c | ths | int bus_id, unit_id;
|
5429 | e4bcb14c | ths | int cyls, heads, secs, translation;
|
5430 | e4bcb14c | ths | BlockDriverState *bdrv; |
5431 | 1e72d3b7 | aurel32 | BlockDriver *drv = NULL;
|
5432 | e4bcb14c | ths | int max_devs;
|
5433 | e4bcb14c | ths | int index;
|
5434 | 33f00271 | balrog | int cache;
|
5435 | 33f00271 | balrog | int bdrv_flags;
|
5436 | 609497ab | balrog | char *str = arg->opt;
|
5437 | e4bcb14c | ths | char *params[] = { "bus", "unit", "if", "index", "cyls", "heads", |
5438 | 33f00271 | balrog | "secs", "trans", "media", "snapshot", "file", |
5439 | 1e72d3b7 | aurel32 | "cache", "format", NULL }; |
5440 | e4bcb14c | ths | |
5441 | e4bcb14c | ths | if (check_params(buf, sizeof(buf), params, str) < 0) { |
5442 | ff993638 | balrog | fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
|
5443 | e4bcb14c | ths | buf, str); |
5444 | e4bcb14c | ths | return -1; |
5445 | e4bcb14c | ths | } |
5446 | e4bcb14c | ths | |
5447 | e4bcb14c | ths | file[0] = 0; |
5448 | e4bcb14c | ths | cyls = heads = secs = 0;
|
5449 | e4bcb14c | ths | bus_id = 0;
|
5450 | e4bcb14c | ths | unit_id = -1;
|
5451 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_AUTO; |
5452 | e4bcb14c | ths | index = -1;
|
5453 | 33f00271 | balrog | cache = 1;
|
5454 | e4bcb14c | ths | |
5455 | e4bcb14c | ths | if (!strcmp(machine->name, "realview") || |
5456 | e4bcb14c | ths | !strcmp(machine->name, "SS-5") ||
|
5457 | e4bcb14c | ths | !strcmp(machine->name, "SS-10") ||
|
5458 | e4bcb14c | ths | !strcmp(machine->name, "SS-600MP") ||
|
5459 | e4bcb14c | ths | !strcmp(machine->name, "versatilepb") ||
|
5460 | e4bcb14c | ths | !strcmp(machine->name, "versatileab")) {
|
5461 | f60d39bc | ths | type = IF_SCSI; |
5462 | e4bcb14c | ths | max_devs = MAX_SCSI_DEVS; |
5463 | 363a37d5 | blueswir1 | pstrcpy(devname, sizeof(devname), "scsi"); |
5464 | e4bcb14c | ths | } else {
|
5465 | f60d39bc | ths | type = IF_IDE; |
5466 | e4bcb14c | ths | max_devs = MAX_IDE_DEVS; |
5467 | 363a37d5 | blueswir1 | pstrcpy(devname, sizeof(devname), "ide"); |
5468 | e4bcb14c | ths | } |
5469 | e4bcb14c | ths | media = MEDIA_DISK; |
5470 | e4bcb14c | ths | |
5471 | e4bcb14c | ths | /* extract parameters */
|
5472 | e4bcb14c | ths | |
5473 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "bus", str)) { |
5474 | e4bcb14c | ths | bus_id = strtol(buf, NULL, 0); |
5475 | e4bcb14c | ths | if (bus_id < 0) { |
5476 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
|
5477 | e4bcb14c | ths | return -1; |
5478 | e4bcb14c | ths | } |
5479 | e4bcb14c | ths | } |
5480 | e4bcb14c | ths | |
5481 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "unit", str)) { |
5482 | e4bcb14c | ths | unit_id = strtol(buf, NULL, 0); |
5483 | e4bcb14c | ths | if (unit_id < 0) { |
5484 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
|
5485 | e4bcb14c | ths | return -1; |
5486 | e4bcb14c | ths | } |
5487 | e4bcb14c | ths | } |
5488 | e4bcb14c | ths | |
5489 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "if", str)) { |
5490 | ae45d369 | bellard | pstrcpy(devname, sizeof(devname), buf);
|
5491 | e4bcb14c | ths | if (!strcmp(buf, "ide")) { |
5492 | f60d39bc | ths | type = IF_IDE; |
5493 | e4bcb14c | ths | max_devs = MAX_IDE_DEVS; |
5494 | e4bcb14c | ths | } else if (!strcmp(buf, "scsi")) { |
5495 | f60d39bc | ths | type = IF_SCSI; |
5496 | e4bcb14c | ths | max_devs = MAX_SCSI_DEVS; |
5497 | e4bcb14c | ths | } else if (!strcmp(buf, "floppy")) { |
5498 | f60d39bc | ths | type = IF_FLOPPY; |
5499 | e4bcb14c | ths | max_devs = 0;
|
5500 | e4bcb14c | ths | } else if (!strcmp(buf, "pflash")) { |
5501 | f60d39bc | ths | type = IF_PFLASH; |
5502 | e4bcb14c | ths | max_devs = 0;
|
5503 | e4bcb14c | ths | } else if (!strcmp(buf, "mtd")) { |
5504 | f60d39bc | ths | type = IF_MTD; |
5505 | e4bcb14c | ths | max_devs = 0;
|
5506 | e4bcb14c | ths | } else if (!strcmp(buf, "sd")) { |
5507 | f60d39bc | ths | type = IF_SD; |
5508 | e4bcb14c | ths | max_devs = 0;
|
5509 | e4bcb14c | ths | } else {
|
5510 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
|
5511 | e4bcb14c | ths | return -1; |
5512 | e4bcb14c | ths | } |
5513 | e4bcb14c | ths | } |
5514 | e4bcb14c | ths | |
5515 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "index", str)) { |
5516 | e4bcb14c | ths | index = strtol(buf, NULL, 0); |
5517 | e4bcb14c | ths | if (index < 0) { |
5518 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid index\n", str);
|
5519 | e4bcb14c | ths | return -1; |
5520 | e4bcb14c | ths | } |
5521 | e4bcb14c | ths | } |
5522 | e4bcb14c | ths | |
5523 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "cyls", str)) { |
5524 | e4bcb14c | ths | cyls = strtol(buf, NULL, 0); |
5525 | e4bcb14c | ths | } |
5526 | e4bcb14c | ths | |
5527 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "heads", str)) { |
5528 | e4bcb14c | ths | heads = strtol(buf, NULL, 0); |
5529 | e4bcb14c | ths | } |
5530 | e4bcb14c | ths | |
5531 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "secs", str)) { |
5532 | e4bcb14c | ths | secs = strtol(buf, NULL, 0); |
5533 | e4bcb14c | ths | } |
5534 | e4bcb14c | ths | |
5535 | e4bcb14c | ths | if (cyls || heads || secs) {
|
5536 | e4bcb14c | ths | if (cyls < 1 || cyls > 16383) { |
5537 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
|
5538 | e4bcb14c | ths | return -1; |
5539 | e4bcb14c | ths | } |
5540 | e4bcb14c | ths | if (heads < 1 || heads > 16) { |
5541 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
|
5542 | e4bcb14c | ths | return -1; |
5543 | e4bcb14c | ths | } |
5544 | e4bcb14c | ths | if (secs < 1 || secs > 63) { |
5545 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
|
5546 | e4bcb14c | ths | return -1; |
5547 | e4bcb14c | ths | } |
5548 | e4bcb14c | ths | } |
5549 | e4bcb14c | ths | |
5550 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "trans", str)) { |
5551 | e4bcb14c | ths | if (!cyls) {
|
5552 | e4bcb14c | ths | fprintf(stderr, |
5553 | e4bcb14c | ths | "qemu: '%s' trans must be used with cyls,heads and secs\n",
|
5554 | e4bcb14c | ths | str); |
5555 | e4bcb14c | ths | return -1; |
5556 | e4bcb14c | ths | } |
5557 | e4bcb14c | ths | if (!strcmp(buf, "none")) |
5558 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_NONE; |
5559 | e4bcb14c | ths | else if (!strcmp(buf, "lba")) |
5560 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_LBA; |
5561 | e4bcb14c | ths | else if (!strcmp(buf, "auto")) |
5562 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_AUTO; |
5563 | e4bcb14c | ths | else {
|
5564 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
|
5565 | e4bcb14c | ths | return -1; |
5566 | e4bcb14c | ths | } |
5567 | e4bcb14c | ths | } |
5568 | e4bcb14c | ths | |
5569 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "media", str)) { |
5570 | e4bcb14c | ths | if (!strcmp(buf, "disk")) { |
5571 | e4bcb14c | ths | media = MEDIA_DISK; |
5572 | e4bcb14c | ths | } else if (!strcmp(buf, "cdrom")) { |
5573 | e4bcb14c | ths | if (cyls || secs || heads) {
|
5574 | e4bcb14c | ths | fprintf(stderr, |
5575 | e4bcb14c | ths | "qemu: '%s' invalid physical CHS format\n", str);
|
5576 | e4bcb14c | ths | return -1; |
5577 | e4bcb14c | ths | } |
5578 | e4bcb14c | ths | media = MEDIA_CDROM; |
5579 | e4bcb14c | ths | } else {
|
5580 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid media\n", str);
|
5581 | e4bcb14c | ths | return -1; |
5582 | e4bcb14c | ths | } |
5583 | e4bcb14c | ths | } |
5584 | e4bcb14c | ths | |
5585 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "snapshot", str)) { |
5586 | e4bcb14c | ths | if (!strcmp(buf, "on")) |
5587 | e4bcb14c | ths | snapshot = 1;
|
5588 | e4bcb14c | ths | else if (!strcmp(buf, "off")) |
5589 | e4bcb14c | ths | snapshot = 0;
|
5590 | e4bcb14c | ths | else {
|
5591 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
|
5592 | e4bcb14c | ths | return -1; |
5593 | e4bcb14c | ths | } |
5594 | e4bcb14c | ths | } |
5595 | e4bcb14c | ths | |
5596 | 33f00271 | balrog | if (get_param_value(buf, sizeof(buf), "cache", str)) { |
5597 | 33f00271 | balrog | if (!strcmp(buf, "off")) |
5598 | 33f00271 | balrog | cache = 0;
|
5599 | 33f00271 | balrog | else if (!strcmp(buf, "on")) |
5600 | 33f00271 | balrog | cache = 1;
|
5601 | 33f00271 | balrog | else {
|
5602 | 33f00271 | balrog | fprintf(stderr, "qemu: invalid cache option\n");
|
5603 | 33f00271 | balrog | return -1; |
5604 | 33f00271 | balrog | } |
5605 | 33f00271 | balrog | } |
5606 | 33f00271 | balrog | |
5607 | 1e72d3b7 | aurel32 | if (get_param_value(buf, sizeof(buf), "format", str)) { |
5608 | a1620fac | aurel32 | if (strcmp(buf, "?") == 0) { |
5609 | a1620fac | aurel32 | fprintf(stderr, "qemu: Supported formats:");
|
5610 | a1620fac | aurel32 | bdrv_iterate_format(bdrv_format_print, NULL);
|
5611 | a1620fac | aurel32 | fprintf(stderr, "\n");
|
5612 | a1620fac | aurel32 | return -1; |
5613 | a1620fac | aurel32 | } |
5614 | 1e72d3b7 | aurel32 | drv = bdrv_find_format(buf); |
5615 | 1e72d3b7 | aurel32 | if (!drv) {
|
5616 | 1e72d3b7 | aurel32 | fprintf(stderr, "qemu: '%s' invalid format\n", buf);
|
5617 | 1e72d3b7 | aurel32 | return -1; |
5618 | 1e72d3b7 | aurel32 | } |
5619 | 1e72d3b7 | aurel32 | } |
5620 | 1e72d3b7 | aurel32 | |
5621 | 609497ab | balrog | if (arg->file == NULL) |
5622 | 609497ab | balrog | get_param_value(file, sizeof(file), "file", str); |
5623 | 609497ab | balrog | else
|
5624 | 609497ab | balrog | pstrcpy(file, sizeof(file), arg->file);
|
5625 | e4bcb14c | ths | |
5626 | e4bcb14c | ths | /* compute bus and unit according index */
|
5627 | e4bcb14c | ths | |
5628 | e4bcb14c | ths | if (index != -1) { |
5629 | e4bcb14c | ths | if (bus_id != 0 || unit_id != -1) { |
5630 | e4bcb14c | ths | fprintf(stderr, |
5631 | e4bcb14c | ths | "qemu: '%s' index cannot be used with bus and unit\n", str);
|
5632 | e4bcb14c | ths | return -1; |
5633 | e4bcb14c | ths | } |
5634 | e4bcb14c | ths | if (max_devs == 0) |
5635 | e4bcb14c | ths | { |
5636 | e4bcb14c | ths | unit_id = index; |
5637 | e4bcb14c | ths | bus_id = 0;
|
5638 | e4bcb14c | ths | } else {
|
5639 | e4bcb14c | ths | unit_id = index % max_devs; |
5640 | e4bcb14c | ths | bus_id = index / max_devs; |
5641 | e4bcb14c | ths | } |
5642 | e4bcb14c | ths | } |
5643 | e4bcb14c | ths | |
5644 | e4bcb14c | ths | /* if user doesn't specify a unit_id,
|
5645 | e4bcb14c | ths | * try to find the first free
|
5646 | e4bcb14c | ths | */
|
5647 | e4bcb14c | ths | |
5648 | e4bcb14c | ths | if (unit_id == -1) { |
5649 | e4bcb14c | ths | unit_id = 0;
|
5650 | f60d39bc | ths | while (drive_get_index(type, bus_id, unit_id) != -1) { |
5651 | e4bcb14c | ths | unit_id++; |
5652 | e4bcb14c | ths | if (max_devs && unit_id >= max_devs) {
|
5653 | e4bcb14c | ths | unit_id -= max_devs; |
5654 | e4bcb14c | ths | bus_id++; |
5655 | e4bcb14c | ths | } |
5656 | e4bcb14c | ths | } |
5657 | e4bcb14c | ths | } |
5658 | e4bcb14c | ths | |
5659 | e4bcb14c | ths | /* check unit id */
|
5660 | e4bcb14c | ths | |
5661 | e4bcb14c | ths | if (max_devs && unit_id >= max_devs) {
|
5662 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
|
5663 | e4bcb14c | ths | str, unit_id, max_devs - 1);
|
5664 | e4bcb14c | ths | return -1; |
5665 | e4bcb14c | ths | } |
5666 | e4bcb14c | ths | |
5667 | e4bcb14c | ths | /*
|
5668 | e4bcb14c | ths | * ignore multiple definitions
|
5669 | e4bcb14c | ths | */
|
5670 | e4bcb14c | ths | |
5671 | f60d39bc | ths | if (drive_get_index(type, bus_id, unit_id) != -1) |
5672 | e4bcb14c | ths | return 0; |
5673 | e4bcb14c | ths | |
5674 | e4bcb14c | ths | /* init */
|
5675 | e4bcb14c | ths | |
5676 | f60d39bc | ths | if (type == IF_IDE || type == IF_SCSI)
|
5677 | c8522bdf | balrog | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
5678 | e6198a70 | balrog | if (max_devs)
|
5679 | e6198a70 | balrog | snprintf(buf, sizeof(buf), "%s%i%s%i", |
5680 | e6198a70 | balrog | devname, bus_id, mediastr, unit_id); |
5681 | e6198a70 | balrog | else
|
5682 | e6198a70 | balrog | snprintf(buf, sizeof(buf), "%s%s%i", |
5683 | e6198a70 | balrog | devname, mediastr, unit_id); |
5684 | e4bcb14c | ths | bdrv = bdrv_new(buf); |
5685 | e4bcb14c | ths | drives_table[nb_drives].bdrv = bdrv; |
5686 | f60d39bc | ths | drives_table[nb_drives].type = type; |
5687 | e4bcb14c | ths | drives_table[nb_drives].bus = bus_id; |
5688 | e4bcb14c | ths | drives_table[nb_drives].unit = unit_id; |
5689 | e4bcb14c | ths | nb_drives++; |
5690 | e4bcb14c | ths | |
5691 | f60d39bc | ths | switch(type) {
|
5692 | e4bcb14c | ths | case IF_IDE:
|
5693 | e4bcb14c | ths | case IF_SCSI:
|
5694 | e4bcb14c | ths | switch(media) {
|
5695 | e4bcb14c | ths | case MEDIA_DISK:
|
5696 | e4bcb14c | ths | if (cyls != 0) { |
5697 | e4bcb14c | ths | bdrv_set_geometry_hint(bdrv, cyls, heads, secs); |
5698 | e4bcb14c | ths | bdrv_set_translation_hint(bdrv, translation); |
5699 | e4bcb14c | ths | } |
5700 | e4bcb14c | ths | break;
|
5701 | e4bcb14c | ths | case MEDIA_CDROM:
|
5702 | e4bcb14c | ths | bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM); |
5703 | e4bcb14c | ths | break;
|
5704 | e4bcb14c | ths | } |
5705 | e4bcb14c | ths | break;
|
5706 | e4bcb14c | ths | case IF_SD:
|
5707 | e4bcb14c | ths | /* FIXME: This isn't really a floppy, but it's a reasonable
|
5708 | e4bcb14c | ths | approximation. */
|
5709 | e4bcb14c | ths | case IF_FLOPPY:
|
5710 | e4bcb14c | ths | bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY); |
5711 | e4bcb14c | ths | break;
|
5712 | e4bcb14c | ths | case IF_PFLASH:
|
5713 | e4bcb14c | ths | case IF_MTD:
|
5714 | e4bcb14c | ths | break;
|
5715 | e4bcb14c | ths | } |
5716 | e4bcb14c | ths | if (!file[0]) |
5717 | e4bcb14c | ths | return 0; |
5718 | 33f00271 | balrog | bdrv_flags = 0;
|
5719 | 33f00271 | balrog | if (snapshot)
|
5720 | 33f00271 | balrog | bdrv_flags |= BDRV_O_SNAPSHOT; |
5721 | 33f00271 | balrog | if (!cache)
|
5722 | 33f00271 | balrog | bdrv_flags |= BDRV_O_DIRECT; |
5723 | 83ab7950 | aliguori | if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) { |
5724 | e4bcb14c | ths | fprintf(stderr, "qemu: could not open disk image %s\n",
|
5725 | e4bcb14c | ths | file); |
5726 | e4bcb14c | ths | return -1; |
5727 | e4bcb14c | ths | } |
5728 | e4bcb14c | ths | return 0; |
5729 | e4bcb14c | ths | } |
5730 | e4bcb14c | ths | |
5731 | 330d0414 | bellard | /***********************************************************/
|
5732 | a594cfbf | bellard | /* USB devices */
|
5733 | a594cfbf | bellard | |
5734 | 0d92ed30 | pbrook | static USBPort *used_usb_ports;
|
5735 | 0d92ed30 | pbrook | static USBPort *free_usb_ports;
|
5736 | 0d92ed30 | pbrook | |
5737 | 0d92ed30 | pbrook | /* ??? Maybe change this to register a hub to keep track of the topology. */
|
5738 | 0d92ed30 | pbrook | void qemu_register_usb_port(USBPort *port, void *opaque, int index, |
5739 | 0d92ed30 | pbrook | usb_attachfn attach) |
5740 | 0d92ed30 | pbrook | { |
5741 | 0d92ed30 | pbrook | port->opaque = opaque; |
5742 | 0d92ed30 | pbrook | port->index = index; |
5743 | 0d92ed30 | pbrook | port->attach = attach; |
5744 | 0d92ed30 | pbrook | port->next = free_usb_ports; |
5745 | 0d92ed30 | pbrook | free_usb_ports = port; |
5746 | 0d92ed30 | pbrook | } |
5747 | 0d92ed30 | pbrook | |
5748 | 4b096fc9 | aliguori | int usb_device_add_dev(USBDevice *dev)
|
5749 | 4b096fc9 | aliguori | { |
5750 | 4b096fc9 | aliguori | USBPort *port; |
5751 | 4b096fc9 | aliguori | |
5752 | 4b096fc9 | aliguori | /* Find a USB port to add the device to. */
|
5753 | 4b096fc9 | aliguori | port = free_usb_ports; |
5754 | 4b096fc9 | aliguori | if (!port->next) {
|
5755 | 4b096fc9 | aliguori | USBDevice *hub; |
5756 | 4b096fc9 | aliguori | |
5757 | 4b096fc9 | aliguori | /* Create a new hub and chain it on. */
|
5758 | 4b096fc9 | aliguori | free_usb_ports = NULL;
|
5759 | 4b096fc9 | aliguori | port->next = used_usb_ports; |
5760 | 4b096fc9 | aliguori | used_usb_ports = port; |
5761 | 4b096fc9 | aliguori | |
5762 | 4b096fc9 | aliguori | hub = usb_hub_init(VM_USB_HUB_SIZE); |
5763 | 4b096fc9 | aliguori | usb_attach(port, hub); |
5764 | 4b096fc9 | aliguori | port = free_usb_ports; |
5765 | 4b096fc9 | aliguori | } |
5766 | 4b096fc9 | aliguori | |
5767 | 4b096fc9 | aliguori | free_usb_ports = port->next; |
5768 | 4b096fc9 | aliguori | port->next = used_usb_ports; |
5769 | 4b096fc9 | aliguori | used_usb_ports = port; |
5770 | 4b096fc9 | aliguori | usb_attach(port, dev); |
5771 | 4b096fc9 | aliguori | return 0; |
5772 | 4b096fc9 | aliguori | } |
5773 | 4b096fc9 | aliguori | |
5774 | a594cfbf | bellard | static int usb_device_add(const char *devname) |
5775 | a594cfbf | bellard | { |
5776 | a594cfbf | bellard | const char *p; |
5777 | a594cfbf | bellard | USBDevice *dev; |
5778 | 0d92ed30 | pbrook | USBPort *port; |
5779 | a594cfbf | bellard | |
5780 | 0d92ed30 | pbrook | if (!free_usb_ports)
|
5781 | a594cfbf | bellard | return -1; |
5782 | a594cfbf | bellard | |
5783 | a594cfbf | bellard | if (strstart(devname, "host:", &p)) { |
5784 | a594cfbf | bellard | dev = usb_host_device_open(p); |
5785 | a594cfbf | bellard | } else if (!strcmp(devname, "mouse")) { |
5786 | a594cfbf | bellard | dev = usb_mouse_init(); |
5787 | 09b26c5e | bellard | } else if (!strcmp(devname, "tablet")) { |
5788 | 47b2d338 | balrog | dev = usb_tablet_init(); |
5789 | 47b2d338 | balrog | } else if (!strcmp(devname, "keyboard")) { |
5790 | 47b2d338 | balrog | dev = usb_keyboard_init(); |
5791 | 2e5d83bb | pbrook | } else if (strstart(devname, "disk:", &p)) { |
5792 | 2e5d83bb | pbrook | dev = usb_msd_init(p); |
5793 | f6d2a316 | balrog | } else if (!strcmp(devname, "wacom-tablet")) { |
5794 | f6d2a316 | balrog | dev = usb_wacom_init(); |
5795 | a7954218 | balrog | } else if (strstart(devname, "serial:", &p)) { |
5796 | a7954218 | balrog | dev = usb_serial_init(p); |
5797 | 2e4d9fb1 | aurel32 | #ifdef CONFIG_BRLAPI
|
5798 | 2e4d9fb1 | aurel32 | } else if (!strcmp(devname, "braille")) { |
5799 | 2e4d9fb1 | aurel32 | dev = usb_baum_init(); |
5800 | 2e4d9fb1 | aurel32 | #endif
|
5801 | 6c9f886c | balrog | } else if (strstart(devname, "net:", &p)) { |
5802 | 9ad97e65 | balrog | int nic = nb_nics;
|
5803 | 6c9f886c | balrog | |
5804 | 9ad97e65 | balrog | if (net_client_init("nic", p) < 0) |
5805 | 6c9f886c | balrog | return -1; |
5806 | 9ad97e65 | balrog | nd_table[nic].model = "usb";
|
5807 | 9ad97e65 | balrog | dev = usb_net_init(&nd_table[nic]); |
5808 | a594cfbf | bellard | } else {
|
5809 | a594cfbf | bellard | return -1; |
5810 | a594cfbf | bellard | } |
5811 | 0d92ed30 | pbrook | if (!dev)
|
5812 | 0d92ed30 | pbrook | return -1; |
5813 | 0d92ed30 | pbrook | |
5814 | 4b096fc9 | aliguori | return usb_device_add_dev(dev);
|
5815 | a594cfbf | bellard | } |
5816 | a594cfbf | bellard | |
5817 | 1f3870ab | aliguori | int usb_device_del_addr(int bus_num, int addr) |
5818 | a594cfbf | bellard | { |
5819 | 0d92ed30 | pbrook | USBPort *port; |
5820 | 0d92ed30 | pbrook | USBPort **lastp; |
5821 | 059809e4 | bellard | USBDevice *dev; |
5822 | a594cfbf | bellard | |
5823 | 0d92ed30 | pbrook | if (!used_usb_ports)
|
5824 | a594cfbf | bellard | return -1; |
5825 | a594cfbf | bellard | |
5826 | a594cfbf | bellard | if (bus_num != 0) |
5827 | a594cfbf | bellard | return -1; |
5828 | 0d92ed30 | pbrook | |
5829 | 0d92ed30 | pbrook | lastp = &used_usb_ports; |
5830 | 0d92ed30 | pbrook | port = used_usb_ports; |
5831 | 0d92ed30 | pbrook | while (port && port->dev->addr != addr) {
|
5832 | 0d92ed30 | pbrook | lastp = &port->next; |
5833 | 0d92ed30 | pbrook | port = port->next; |
5834 | a594cfbf | bellard | } |
5835 | 0d92ed30 | pbrook | |
5836 | 0d92ed30 | pbrook | if (!port)
|
5837 | a594cfbf | bellard | return -1; |
5838 | 0d92ed30 | pbrook | |
5839 | 059809e4 | bellard | dev = port->dev; |
5840 | 0d92ed30 | pbrook | *lastp = port->next; |
5841 | 0d92ed30 | pbrook | usb_attach(port, NULL);
|
5842 | 059809e4 | bellard | dev->handle_destroy(dev); |
5843 | 0d92ed30 | pbrook | port->next = free_usb_ports; |
5844 | 0d92ed30 | pbrook | free_usb_ports = port; |
5845 | a594cfbf | bellard | return 0; |
5846 | a594cfbf | bellard | } |
5847 | a594cfbf | bellard | |
5848 | 1f3870ab | aliguori | static int usb_device_del(const char *devname) |
5849 | 1f3870ab | aliguori | { |
5850 | 1f3870ab | aliguori | int bus_num, addr;
|
5851 | 1f3870ab | aliguori | const char *p; |
5852 | 1f3870ab | aliguori | |
5853 | 1f3870ab | aliguori | if (!used_usb_ports)
|
5854 | 1f3870ab | aliguori | return -1; |
5855 | 1f3870ab | aliguori | |
5856 | 1f3870ab | aliguori | p = strchr(devname, '.');
|
5857 | 1f3870ab | aliguori | if (!p)
|
5858 | 1f3870ab | aliguori | return -1; |
5859 | 1f3870ab | aliguori | bus_num = strtoul(devname, NULL, 0); |
5860 | 1f3870ab | aliguori | addr = strtoul(p + 1, NULL, 0); |
5861 | 1f3870ab | aliguori | |
5862 | 1f3870ab | aliguori | return usb_device_del_addr(bus_num, addr);
|
5863 | 1f3870ab | aliguori | } |
5864 | 1f3870ab | aliguori | |
5865 | a594cfbf | bellard | void do_usb_add(const char *devname) |
5866 | a594cfbf | bellard | { |
5867 | 4b096fc9 | aliguori | usb_device_add(devname); |
5868 | a594cfbf | bellard | } |
5869 | a594cfbf | bellard | |
5870 | a594cfbf | bellard | void do_usb_del(const char *devname) |
5871 | a594cfbf | bellard | { |
5872 | 4b096fc9 | aliguori | usb_device_del(devname); |
5873 | a594cfbf | bellard | } |
5874 | a594cfbf | bellard | |
5875 | a594cfbf | bellard | void usb_info(void) |
5876 | a594cfbf | bellard | { |
5877 | a594cfbf | bellard | USBDevice *dev; |
5878 | 0d92ed30 | pbrook | USBPort *port; |
5879 | a594cfbf | bellard | const char *speed_str; |
5880 | a594cfbf | bellard | |
5881 | 0d92ed30 | pbrook | if (!usb_enabled) {
|
5882 | a594cfbf | bellard | term_printf("USB support not enabled\n");
|
5883 | a594cfbf | bellard | return;
|
5884 | a594cfbf | bellard | } |
5885 | a594cfbf | bellard | |
5886 | 0d92ed30 | pbrook | for (port = used_usb_ports; port; port = port->next) {
|
5887 | 0d92ed30 | pbrook | dev = port->dev; |
5888 | 0d92ed30 | pbrook | if (!dev)
|
5889 | 0d92ed30 | pbrook | continue;
|
5890 | 0d92ed30 | pbrook | switch(dev->speed) {
|
5891 | 5fafdf24 | ths | case USB_SPEED_LOW:
|
5892 | 5fafdf24 | ths | speed_str = "1.5";
|
5893 | 0d92ed30 | pbrook | break;
|
5894 | 5fafdf24 | ths | case USB_SPEED_FULL:
|
5895 | 5fafdf24 | ths | speed_str = "12";
|
5896 | 0d92ed30 | pbrook | break;
|
5897 | 5fafdf24 | ths | case USB_SPEED_HIGH:
|
5898 | 5fafdf24 | ths | speed_str = "480";
|
5899 | 0d92ed30 | pbrook | break;
|
5900 | 0d92ed30 | pbrook | default:
|
5901 | 5fafdf24 | ths | speed_str = "?";
|
5902 | 0d92ed30 | pbrook | break;
|
5903 | a594cfbf | bellard | } |
5904 | 5fafdf24 | ths | term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
|
5905 | 1f6e24e7 | bellard | 0, dev->addr, speed_str, dev->devname);
|
5906 | a594cfbf | bellard | } |
5907 | a594cfbf | bellard | } |
5908 | a594cfbf | bellard | |
5909 | f7cce898 | bellard | /***********************************************************/
|
5910 | 201a51fc | balrog | /* PCMCIA/Cardbus */
|
5911 | 201a51fc | balrog | |
5912 | 201a51fc | balrog | static struct pcmcia_socket_entry_s { |
5913 | 201a51fc | balrog | struct pcmcia_socket_s *socket;
|
5914 | 201a51fc | balrog | struct pcmcia_socket_entry_s *next;
|
5915 | 201a51fc | balrog | } *pcmcia_sockets = 0;
|
5916 | 201a51fc | balrog | |
5917 | 201a51fc | balrog | void pcmcia_socket_register(struct pcmcia_socket_s *socket) |
5918 | 201a51fc | balrog | { |
5919 | 201a51fc | balrog | struct pcmcia_socket_entry_s *entry;
|
5920 | 201a51fc | balrog | |
5921 | 201a51fc | balrog | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); |
5922 | 201a51fc | balrog | entry->socket = socket; |
5923 | 201a51fc | balrog | entry->next = pcmcia_sockets; |
5924 | 201a51fc | balrog | pcmcia_sockets = entry; |
5925 | 201a51fc | balrog | } |
5926 | 201a51fc | balrog | |
5927 | 201a51fc | balrog | void pcmcia_socket_unregister(struct pcmcia_socket_s *socket) |
5928 | 201a51fc | balrog | { |
5929 | 201a51fc | balrog | struct pcmcia_socket_entry_s *entry, **ptr;
|
5930 | 201a51fc | balrog | |
5931 | 201a51fc | balrog | ptr = &pcmcia_sockets; |
5932 | 201a51fc | balrog | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
|
5933 | 201a51fc | balrog | if (entry->socket == socket) {
|
5934 | 201a51fc | balrog | *ptr = entry->next; |
5935 | 201a51fc | balrog | qemu_free(entry); |
5936 | 201a51fc | balrog | } |
5937 | 201a51fc | balrog | } |
5938 | 201a51fc | balrog | |
5939 | 201a51fc | balrog | void pcmcia_info(void) |
5940 | 201a51fc | balrog | { |
5941 | 201a51fc | balrog | struct pcmcia_socket_entry_s *iter;
|
5942 | 201a51fc | balrog | if (!pcmcia_sockets)
|
5943 | 201a51fc | balrog | term_printf("No PCMCIA sockets\n");
|
5944 | 201a51fc | balrog | |
5945 | 201a51fc | balrog | for (iter = pcmcia_sockets; iter; iter = iter->next)
|
5946 | 201a51fc | balrog | term_printf("%s: %s\n", iter->socket->slot_string,
|
5947 | 201a51fc | balrog | iter->socket->attached ? iter->socket->card_string : |
5948 | 201a51fc | balrog | "Empty");
|
5949 | 201a51fc | balrog | } |
5950 | 201a51fc | balrog | |
5951 | 201a51fc | balrog | /***********************************************************/
|
5952 | 2ff89790 | ths | /* dumb display */
|
5953 | 2ff89790 | ths | |
5954 | 2ff89790 | ths | static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
5955 | 2ff89790 | ths | { |
5956 | 2ff89790 | ths | } |
5957 | 2ff89790 | ths | |
5958 | 2ff89790 | ths | static void dumb_resize(DisplayState *ds, int w, int h) |
5959 | 2ff89790 | ths | { |
5960 | 2ff89790 | ths | } |
5961 | 2ff89790 | ths | |
5962 | 2ff89790 | ths | static void dumb_refresh(DisplayState *ds) |
5963 | 2ff89790 | ths | { |
5964 | 2ff89790 | ths | #if defined(CONFIG_SDL)
|
5965 | 2ff89790 | ths | vga_hw_update(); |
5966 | 2ff89790 | ths | #endif
|
5967 | 2ff89790 | ths | } |
5968 | 2ff89790 | ths | |
5969 | 2ff89790 | ths | static void dumb_display_init(DisplayState *ds) |
5970 | 2ff89790 | ths | { |
5971 | 2ff89790 | ths | ds->data = NULL;
|
5972 | 2ff89790 | ths | ds->linesize = 0;
|
5973 | 2ff89790 | ths | ds->depth = 0;
|
5974 | 2ff89790 | ths | ds->dpy_update = dumb_update; |
5975 | 2ff89790 | ths | ds->dpy_resize = dumb_resize; |
5976 | 2ff89790 | ths | ds->dpy_refresh = dumb_refresh; |
5977 | bcfad70f | aliguori | ds->gui_timer_interval = 500;
|
5978 | bcfad70f | aliguori | ds->idle = 1;
|
5979 | 2ff89790 | ths | } |
5980 | 2ff89790 | ths | |
5981 | 2ff89790 | ths | /***********************************************************/
|
5982 | 8a7ddc38 | bellard | /* I/O handling */
|
5983 | 0824d6fc | bellard | |
5984 | c4b1fcc0 | bellard | #define MAX_IO_HANDLERS 64 |
5985 | c4b1fcc0 | bellard | |
5986 | c4b1fcc0 | bellard | typedef struct IOHandlerRecord { |
5987 | c4b1fcc0 | bellard | int fd;
|
5988 | 7c9d8e07 | bellard | IOCanRWHandler *fd_read_poll; |
5989 | 7c9d8e07 | bellard | IOHandler *fd_read; |
5990 | 7c9d8e07 | bellard | IOHandler *fd_write; |
5991 | cafffd40 | ths | int deleted;
|
5992 | c4b1fcc0 | bellard | void *opaque;
|
5993 | c4b1fcc0 | bellard | /* temporary data */
|
5994 | c4b1fcc0 | bellard | struct pollfd *ufd;
|
5995 | 8a7ddc38 | bellard | struct IOHandlerRecord *next;
|
5996 | c4b1fcc0 | bellard | } IOHandlerRecord; |
5997 | c4b1fcc0 | bellard | |
5998 | 8a7ddc38 | bellard | static IOHandlerRecord *first_io_handler;
|
5999 | c4b1fcc0 | bellard | |
6000 | 7c9d8e07 | bellard | /* XXX: fd_read_poll should be suppressed, but an API change is
|
6001 | 7c9d8e07 | bellard | necessary in the character devices to suppress fd_can_read(). */
|
6002 | 5fafdf24 | ths | int qemu_set_fd_handler2(int fd, |
6003 | 5fafdf24 | ths | IOCanRWHandler *fd_read_poll, |
6004 | 5fafdf24 | ths | IOHandler *fd_read, |
6005 | 5fafdf24 | ths | IOHandler *fd_write, |
6006 | 7c9d8e07 | bellard | void *opaque)
|
6007 | c4b1fcc0 | bellard | { |
6008 | 7c9d8e07 | bellard | IOHandlerRecord **pioh, *ioh; |
6009 | c4b1fcc0 | bellard | |
6010 | 7c9d8e07 | bellard | if (!fd_read && !fd_write) {
|
6011 | 7c9d8e07 | bellard | pioh = &first_io_handler; |
6012 | 7c9d8e07 | bellard | for(;;) {
|
6013 | 7c9d8e07 | bellard | ioh = *pioh; |
6014 | 7c9d8e07 | bellard | if (ioh == NULL) |
6015 | 7c9d8e07 | bellard | break;
|
6016 | 7c9d8e07 | bellard | if (ioh->fd == fd) {
|
6017 | cafffd40 | ths | ioh->deleted = 1;
|
6018 | 7c9d8e07 | bellard | break;
|
6019 | 7c9d8e07 | bellard | } |
6020 | 7c9d8e07 | bellard | pioh = &ioh->next; |
6021 | 7c9d8e07 | bellard | } |
6022 | 7c9d8e07 | bellard | } else {
|
6023 | 7c9d8e07 | bellard | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
6024 | 7c9d8e07 | bellard | if (ioh->fd == fd)
|
6025 | 7c9d8e07 | bellard | goto found;
|
6026 | 7c9d8e07 | bellard | } |
6027 | 7c9d8e07 | bellard | ioh = qemu_mallocz(sizeof(IOHandlerRecord));
|
6028 | 7c9d8e07 | bellard | if (!ioh)
|
6029 | 7c9d8e07 | bellard | return -1; |
6030 | 7c9d8e07 | bellard | ioh->next = first_io_handler; |
6031 | 7c9d8e07 | bellard | first_io_handler = ioh; |
6032 | 7c9d8e07 | bellard | found:
|
6033 | 7c9d8e07 | bellard | ioh->fd = fd; |
6034 | 7c9d8e07 | bellard | ioh->fd_read_poll = fd_read_poll; |
6035 | 7c9d8e07 | bellard | ioh->fd_read = fd_read; |
6036 | 7c9d8e07 | bellard | ioh->fd_write = fd_write; |
6037 | 7c9d8e07 | bellard | ioh->opaque = opaque; |
6038 | cafffd40 | ths | ioh->deleted = 0;
|
6039 | 7c9d8e07 | bellard | } |
6040 | c4b1fcc0 | bellard | return 0; |
6041 | c4b1fcc0 | bellard | } |
6042 | c4b1fcc0 | bellard | |
6043 | 5fafdf24 | ths | int qemu_set_fd_handler(int fd, |
6044 | 5fafdf24 | ths | IOHandler *fd_read, |
6045 | 5fafdf24 | ths | IOHandler *fd_write, |
6046 | 7c9d8e07 | bellard | void *opaque)
|
6047 | 8a7ddc38 | bellard | { |
6048 | 7c9d8e07 | bellard | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
6049 | 8a7ddc38 | bellard | } |
6050 | 8a7ddc38 | bellard | |
6051 | 8a7ddc38 | bellard | /***********************************************************/
|
6052 | f331110f | bellard | /* Polling handling */
|
6053 | f331110f | bellard | |
6054 | f331110f | bellard | typedef struct PollingEntry { |
6055 | f331110f | bellard | PollingFunc *func; |
6056 | f331110f | bellard | void *opaque;
|
6057 | f331110f | bellard | struct PollingEntry *next;
|
6058 | f331110f | bellard | } PollingEntry; |
6059 | f331110f | bellard | |
6060 | f331110f | bellard | static PollingEntry *first_polling_entry;
|
6061 | f331110f | bellard | |
6062 | f331110f | bellard | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
6063 | f331110f | bellard | { |
6064 | f331110f | bellard | PollingEntry **ppe, *pe; |
6065 | f331110f | bellard | pe = qemu_mallocz(sizeof(PollingEntry));
|
6066 | f331110f | bellard | if (!pe)
|
6067 | f331110f | bellard | return -1; |
6068 | f331110f | bellard | pe->func = func; |
6069 | f331110f | bellard | pe->opaque = opaque; |
6070 | f331110f | bellard | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
6071 | f331110f | bellard | *ppe = pe; |
6072 | f331110f | bellard | return 0; |
6073 | f331110f | bellard | } |
6074 | f331110f | bellard | |
6075 | f331110f | bellard | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
6076 | f331110f | bellard | { |
6077 | f331110f | bellard | PollingEntry **ppe, *pe; |
6078 | f331110f | bellard | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
6079 | f331110f | bellard | pe = *ppe; |
6080 | f331110f | bellard | if (pe->func == func && pe->opaque == opaque) {
|
6081 | f331110f | bellard | *ppe = pe->next; |
6082 | f331110f | bellard | qemu_free(pe); |
6083 | f331110f | bellard | break;
|
6084 | f331110f | bellard | } |
6085 | f331110f | bellard | } |
6086 | f331110f | bellard | } |
6087 | f331110f | bellard | |
6088 | a18e524a | bellard | #ifdef _WIN32
|
6089 | a18e524a | bellard | /***********************************************************/
|
6090 | a18e524a | bellard | /* Wait objects support */
|
6091 | a18e524a | bellard | typedef struct WaitObjects { |
6092 | a18e524a | bellard | int num;
|
6093 | a18e524a | bellard | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
|
6094 | a18e524a | bellard | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
|
6095 | a18e524a | bellard | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
6096 | a18e524a | bellard | } WaitObjects; |
6097 | a18e524a | bellard | |
6098 | a18e524a | bellard | static WaitObjects wait_objects = {0}; |
6099 | 3b46e624 | ths | |
6100 | a18e524a | bellard | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
6101 | a18e524a | bellard | { |
6102 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
6103 | a18e524a | bellard | |
6104 | a18e524a | bellard | if (w->num >= MAXIMUM_WAIT_OBJECTS)
|
6105 | a18e524a | bellard | return -1; |
6106 | a18e524a | bellard | w->events[w->num] = handle; |
6107 | a18e524a | bellard | w->func[w->num] = func; |
6108 | a18e524a | bellard | w->opaque[w->num] = opaque; |
6109 | a18e524a | bellard | w->num++; |
6110 | a18e524a | bellard | return 0; |
6111 | a18e524a | bellard | } |
6112 | a18e524a | bellard | |
6113 | a18e524a | bellard | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
6114 | a18e524a | bellard | { |
6115 | a18e524a | bellard | int i, found;
|
6116 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
6117 | a18e524a | bellard | |
6118 | a18e524a | bellard | found = 0;
|
6119 | a18e524a | bellard | for (i = 0; i < w->num; i++) { |
6120 | a18e524a | bellard | if (w->events[i] == handle)
|
6121 | a18e524a | bellard | found = 1;
|
6122 | a18e524a | bellard | if (found) {
|
6123 | a18e524a | bellard | w->events[i] = w->events[i + 1];
|
6124 | a18e524a | bellard | w->func[i] = w->func[i + 1];
|
6125 | a18e524a | bellard | w->opaque[i] = w->opaque[i + 1];
|
6126 | 3b46e624 | ths | } |
6127 | a18e524a | bellard | } |
6128 | a18e524a | bellard | if (found)
|
6129 | a18e524a | bellard | w->num--; |
6130 | a18e524a | bellard | } |
6131 | a18e524a | bellard | #endif
|
6132 | a18e524a | bellard | |
6133 | f331110f | bellard | /***********************************************************/
|
6134 | 8a7ddc38 | bellard | /* savevm/loadvm support */
|
6135 | 8a7ddc38 | bellard | |
6136 | faea38e7 | bellard | #define IO_BUF_SIZE 32768 |
6137 | faea38e7 | bellard | |
6138 | faea38e7 | bellard | struct QEMUFile {
|
6139 | faea38e7 | bellard | FILE *outfile; |
6140 | faea38e7 | bellard | BlockDriverState *bs; |
6141 | faea38e7 | bellard | int is_file;
|
6142 | faea38e7 | bellard | int is_writable;
|
6143 | faea38e7 | bellard | int64_t base_offset; |
6144 | faea38e7 | bellard | int64_t buf_offset; /* start of buffer when writing, end of buffer
|
6145 | faea38e7 | bellard | when reading */
|
6146 | faea38e7 | bellard | int buf_index;
|
6147 | faea38e7 | bellard | int buf_size; /* 0 when writing */ |
6148 | faea38e7 | bellard | uint8_t buf[IO_BUF_SIZE]; |
6149 | faea38e7 | bellard | }; |
6150 | faea38e7 | bellard | |
6151 | faea38e7 | bellard | QEMUFile *qemu_fopen(const char *filename, const char *mode) |
6152 | faea38e7 | bellard | { |
6153 | faea38e7 | bellard | QEMUFile *f; |
6154 | faea38e7 | bellard | |
6155 | faea38e7 | bellard | f = qemu_mallocz(sizeof(QEMUFile));
|
6156 | faea38e7 | bellard | if (!f)
|
6157 | faea38e7 | bellard | return NULL; |
6158 | faea38e7 | bellard | if (!strcmp(mode, "wb")) { |
6159 | faea38e7 | bellard | f->is_writable = 1;
|
6160 | faea38e7 | bellard | } else if (!strcmp(mode, "rb")) { |
6161 | faea38e7 | bellard | f->is_writable = 0;
|
6162 | faea38e7 | bellard | } else {
|
6163 | faea38e7 | bellard | goto fail;
|
6164 | faea38e7 | bellard | } |
6165 | faea38e7 | bellard | f->outfile = fopen(filename, mode); |
6166 | faea38e7 | bellard | if (!f->outfile)
|
6167 | faea38e7 | bellard | goto fail;
|
6168 | faea38e7 | bellard | f->is_file = 1;
|
6169 | faea38e7 | bellard | return f;
|
6170 | faea38e7 | bellard | fail:
|
6171 | faea38e7 | bellard | if (f->outfile)
|
6172 | faea38e7 | bellard | fclose(f->outfile); |
6173 | faea38e7 | bellard | qemu_free(f); |
6174 | faea38e7 | bellard | return NULL; |
6175 | faea38e7 | bellard | } |
6176 | faea38e7 | bellard | |
6177 | 9596ebb7 | pbrook | static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable) |
6178 | faea38e7 | bellard | { |
6179 | faea38e7 | bellard | QEMUFile *f; |
6180 | faea38e7 | bellard | |
6181 | faea38e7 | bellard | f = qemu_mallocz(sizeof(QEMUFile));
|
6182 | faea38e7 | bellard | if (!f)
|
6183 | faea38e7 | bellard | return NULL; |
6184 | faea38e7 | bellard | f->is_file = 0;
|
6185 | faea38e7 | bellard | f->bs = bs; |
6186 | faea38e7 | bellard | f->is_writable = is_writable; |
6187 | faea38e7 | bellard | f->base_offset = offset; |
6188 | faea38e7 | bellard | return f;
|
6189 | faea38e7 | bellard | } |
6190 | faea38e7 | bellard | |
6191 | faea38e7 | bellard | void qemu_fflush(QEMUFile *f)
|
6192 | faea38e7 | bellard | { |
6193 | faea38e7 | bellard | if (!f->is_writable)
|
6194 | faea38e7 | bellard | return;
|
6195 | faea38e7 | bellard | if (f->buf_index > 0) { |
6196 | faea38e7 | bellard | if (f->is_file) {
|
6197 | faea38e7 | bellard | fseek(f->outfile, f->buf_offset, SEEK_SET); |
6198 | faea38e7 | bellard | fwrite(f->buf, 1, f->buf_index, f->outfile);
|
6199 | faea38e7 | bellard | } else {
|
6200 | 5fafdf24 | ths | bdrv_pwrite(f->bs, f->base_offset + f->buf_offset, |
6201 | faea38e7 | bellard | f->buf, f->buf_index); |
6202 | faea38e7 | bellard | } |
6203 | faea38e7 | bellard | f->buf_offset += f->buf_index; |
6204 | faea38e7 | bellard | f->buf_index = 0;
|
6205 | faea38e7 | bellard | } |
6206 | faea38e7 | bellard | } |
6207 | faea38e7 | bellard | |
6208 | faea38e7 | bellard | static void qemu_fill_buffer(QEMUFile *f) |
6209 | faea38e7 | bellard | { |
6210 | faea38e7 | bellard | int len;
|
6211 | faea38e7 | bellard | |
6212 | faea38e7 | bellard | if (f->is_writable)
|
6213 | faea38e7 | bellard | return;
|
6214 | faea38e7 | bellard | if (f->is_file) {
|
6215 | faea38e7 | bellard | fseek(f->outfile, f->buf_offset, SEEK_SET); |
6216 | faea38e7 | bellard | len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile);
|
6217 | faea38e7 | bellard | if (len < 0) |
6218 | faea38e7 | bellard | len = 0;
|
6219 | faea38e7 | bellard | } else {
|
6220 | 5fafdf24 | ths | len = bdrv_pread(f->bs, f->base_offset + f->buf_offset, |
6221 | faea38e7 | bellard | f->buf, IO_BUF_SIZE); |
6222 | faea38e7 | bellard | if (len < 0) |
6223 | faea38e7 | bellard | len = 0;
|
6224 | faea38e7 | bellard | } |
6225 | faea38e7 | bellard | f->buf_index = 0;
|
6226 | faea38e7 | bellard | f->buf_size = len; |
6227 | faea38e7 | bellard | f->buf_offset += len; |
6228 | faea38e7 | bellard | } |
6229 | faea38e7 | bellard | |
6230 | faea38e7 | bellard | void qemu_fclose(QEMUFile *f)
|
6231 | faea38e7 | bellard | { |
6232 | faea38e7 | bellard | if (f->is_writable)
|
6233 | faea38e7 | bellard | qemu_fflush(f); |
6234 | faea38e7 | bellard | if (f->is_file) {
|
6235 | faea38e7 | bellard | fclose(f->outfile); |
6236 | faea38e7 | bellard | } |
6237 | faea38e7 | bellard | qemu_free(f); |
6238 | faea38e7 | bellard | } |
6239 | faea38e7 | bellard | |
6240 | 8a7ddc38 | bellard | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
6241 | b4608c04 | bellard | { |
6242 | faea38e7 | bellard | int l;
|
6243 | faea38e7 | bellard | while (size > 0) { |
6244 | faea38e7 | bellard | l = IO_BUF_SIZE - f->buf_index; |
6245 | faea38e7 | bellard | if (l > size)
|
6246 | faea38e7 | bellard | l = size; |
6247 | faea38e7 | bellard | memcpy(f->buf + f->buf_index, buf, l); |
6248 | faea38e7 | bellard | f->buf_index += l; |
6249 | faea38e7 | bellard | buf += l; |
6250 | faea38e7 | bellard | size -= l; |
6251 | faea38e7 | bellard | if (f->buf_index >= IO_BUF_SIZE)
|
6252 | faea38e7 | bellard | qemu_fflush(f); |
6253 | faea38e7 | bellard | } |
6254 | b4608c04 | bellard | } |
6255 | b4608c04 | bellard | |
6256 | 8a7ddc38 | bellard | void qemu_put_byte(QEMUFile *f, int v) |
6257 | b4608c04 | bellard | { |
6258 | faea38e7 | bellard | f->buf[f->buf_index++] = v; |
6259 | faea38e7 | bellard | if (f->buf_index >= IO_BUF_SIZE)
|
6260 | faea38e7 | bellard | qemu_fflush(f); |
6261 | faea38e7 | bellard | } |
6262 | faea38e7 | bellard | |
6263 | faea38e7 | bellard | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1) |
6264 | faea38e7 | bellard | { |
6265 | faea38e7 | bellard | int size, l;
|
6266 | faea38e7 | bellard | |
6267 | faea38e7 | bellard | size = size1; |
6268 | faea38e7 | bellard | while (size > 0) { |
6269 | faea38e7 | bellard | l = f->buf_size - f->buf_index; |
6270 | faea38e7 | bellard | if (l == 0) { |
6271 | faea38e7 | bellard | qemu_fill_buffer(f); |
6272 | faea38e7 | bellard | l = f->buf_size - f->buf_index; |
6273 | faea38e7 | bellard | if (l == 0) |
6274 | faea38e7 | bellard | break;
|
6275 | faea38e7 | bellard | } |
6276 | faea38e7 | bellard | if (l > size)
|
6277 | faea38e7 | bellard | l = size; |
6278 | faea38e7 | bellard | memcpy(buf, f->buf + f->buf_index, l); |
6279 | faea38e7 | bellard | f->buf_index += l; |
6280 | faea38e7 | bellard | buf += l; |
6281 | faea38e7 | bellard | size -= l; |
6282 | faea38e7 | bellard | } |
6283 | faea38e7 | bellard | return size1 - size;
|
6284 | faea38e7 | bellard | } |
6285 | faea38e7 | bellard | |
6286 | faea38e7 | bellard | int qemu_get_byte(QEMUFile *f)
|
6287 | faea38e7 | bellard | { |
6288 | faea38e7 | bellard | if (f->buf_index >= f->buf_size) {
|
6289 | faea38e7 | bellard | qemu_fill_buffer(f); |
6290 | faea38e7 | bellard | if (f->buf_index >= f->buf_size)
|
6291 | faea38e7 | bellard | return 0; |
6292 | faea38e7 | bellard | } |
6293 | faea38e7 | bellard | return f->buf[f->buf_index++];
|
6294 | faea38e7 | bellard | } |
6295 | faea38e7 | bellard | |
6296 | faea38e7 | bellard | int64_t qemu_ftell(QEMUFile *f) |
6297 | faea38e7 | bellard | { |
6298 | faea38e7 | bellard | return f->buf_offset - f->buf_size + f->buf_index;
|
6299 | faea38e7 | bellard | } |
6300 | faea38e7 | bellard | |
6301 | faea38e7 | bellard | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
|
6302 | faea38e7 | bellard | { |
6303 | faea38e7 | bellard | if (whence == SEEK_SET) {
|
6304 | faea38e7 | bellard | /* nothing to do */
|
6305 | faea38e7 | bellard | } else if (whence == SEEK_CUR) { |
6306 | faea38e7 | bellard | pos += qemu_ftell(f); |
6307 | faea38e7 | bellard | } else {
|
6308 | faea38e7 | bellard | /* SEEK_END not supported */
|
6309 | faea38e7 | bellard | return -1; |
6310 | faea38e7 | bellard | } |
6311 | faea38e7 | bellard | if (f->is_writable) {
|
6312 | faea38e7 | bellard | qemu_fflush(f); |
6313 | faea38e7 | bellard | f->buf_offset = pos; |
6314 | faea38e7 | bellard | } else {
|
6315 | faea38e7 | bellard | f->buf_offset = pos; |
6316 | faea38e7 | bellard | f->buf_index = 0;
|
6317 | faea38e7 | bellard | f->buf_size = 0;
|
6318 | faea38e7 | bellard | } |
6319 | faea38e7 | bellard | return pos;
|
6320 | 8a7ddc38 | bellard | } |
6321 | 8a7ddc38 | bellard | |
6322 | 8a7ddc38 | bellard | void qemu_put_be16(QEMUFile *f, unsigned int v) |
6323 | 8a7ddc38 | bellard | { |
6324 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 8);
|
6325 | 8a7ddc38 | bellard | qemu_put_byte(f, v); |
6326 | 8a7ddc38 | bellard | } |
6327 | 8a7ddc38 | bellard | |
6328 | 8a7ddc38 | bellard | void qemu_put_be32(QEMUFile *f, unsigned int v) |
6329 | 8a7ddc38 | bellard | { |
6330 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 24);
|
6331 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 16);
|
6332 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 8);
|
6333 | 8a7ddc38 | bellard | qemu_put_byte(f, v); |
6334 | 8a7ddc38 | bellard | } |
6335 | 8a7ddc38 | bellard | |
6336 | 8a7ddc38 | bellard | void qemu_put_be64(QEMUFile *f, uint64_t v)
|
6337 | 8a7ddc38 | bellard | { |
6338 | 8a7ddc38 | bellard | qemu_put_be32(f, v >> 32);
|
6339 | 8a7ddc38 | bellard | qemu_put_be32(f, v); |
6340 | 8a7ddc38 | bellard | } |
6341 | 8a7ddc38 | bellard | |
6342 | 8a7ddc38 | bellard | unsigned int qemu_get_be16(QEMUFile *f) |
6343 | 8a7ddc38 | bellard | { |
6344 | 8a7ddc38 | bellard | unsigned int v; |
6345 | 8a7ddc38 | bellard | v = qemu_get_byte(f) << 8;
|
6346 | 8a7ddc38 | bellard | v |= qemu_get_byte(f); |
6347 | 8a7ddc38 | bellard | return v;
|
6348 | 8a7ddc38 | bellard | } |
6349 | 8a7ddc38 | bellard | |
6350 | 8a7ddc38 | bellard | unsigned int qemu_get_be32(QEMUFile *f) |
6351 | 8a7ddc38 | bellard | { |
6352 | 8a7ddc38 | bellard | unsigned int v; |
6353 | 8a7ddc38 | bellard | v = qemu_get_byte(f) << 24;
|
6354 | 8a7ddc38 | bellard | v |= qemu_get_byte(f) << 16;
|
6355 | 8a7ddc38 | bellard | v |= qemu_get_byte(f) << 8;
|
6356 | 8a7ddc38 | bellard | v |= qemu_get_byte(f); |
6357 | 8a7ddc38 | bellard | return v;
|
6358 | 8a7ddc38 | bellard | } |
6359 | 8a7ddc38 | bellard | |
6360 | 8a7ddc38 | bellard | uint64_t qemu_get_be64(QEMUFile *f) |
6361 | 8a7ddc38 | bellard | { |
6362 | 8a7ddc38 | bellard | uint64_t v; |
6363 | 8a7ddc38 | bellard | v = (uint64_t)qemu_get_be32(f) << 32;
|
6364 | 8a7ddc38 | bellard | v |= qemu_get_be32(f); |
6365 | 8a7ddc38 | bellard | return v;
|
6366 | 8a7ddc38 | bellard | } |
6367 | 8a7ddc38 | bellard | |
6368 | 8a7ddc38 | bellard | typedef struct SaveStateEntry { |
6369 | 8a7ddc38 | bellard | char idstr[256]; |
6370 | 8a7ddc38 | bellard | int instance_id;
|
6371 | 8a7ddc38 | bellard | int version_id;
|
6372 | 8a7ddc38 | bellard | SaveStateHandler *save_state; |
6373 | 8a7ddc38 | bellard | LoadStateHandler *load_state; |
6374 | 8a7ddc38 | bellard | void *opaque;
|
6375 | 8a7ddc38 | bellard | struct SaveStateEntry *next;
|
6376 | 8a7ddc38 | bellard | } SaveStateEntry; |
6377 | b4608c04 | bellard | |
6378 | 8a7ddc38 | bellard | static SaveStateEntry *first_se;
|
6379 | 8a7ddc38 | bellard | |
6380 | 18be5187 | pbrook | /* TODO: Individual devices generally have very little idea about the rest
|
6381 | 18fdb1c5 | ths | of the system, so instance_id should be removed/replaced.
|
6382 | 18fdb1c5 | ths | Meanwhile pass -1 as instance_id if you do not already have a clearly
|
6383 | 18fdb1c5 | ths | distinguishing id for all instances of your device class. */
|
6384 | 5fafdf24 | ths | int register_savevm(const char *idstr, |
6385 | 5fafdf24 | ths | int instance_id,
|
6386 | 8a7ddc38 | bellard | int version_id,
|
6387 | 8a7ddc38 | bellard | SaveStateHandler *save_state, |
6388 | 8a7ddc38 | bellard | LoadStateHandler *load_state, |
6389 | 8a7ddc38 | bellard | void *opaque)
|
6390 | 8a7ddc38 | bellard | { |
6391 | 8a7ddc38 | bellard | SaveStateEntry *se, **pse; |
6392 | 8a7ddc38 | bellard | |
6393 | 8a7ddc38 | bellard | se = qemu_malloc(sizeof(SaveStateEntry));
|
6394 | 8a7ddc38 | bellard | if (!se)
|
6395 | 8a7ddc38 | bellard | return -1; |
6396 | 8a7ddc38 | bellard | pstrcpy(se->idstr, sizeof(se->idstr), idstr);
|
6397 | 18be5187 | pbrook | se->instance_id = (instance_id == -1) ? 0 : instance_id; |
6398 | 8a7ddc38 | bellard | se->version_id = version_id; |
6399 | 8a7ddc38 | bellard | se->save_state = save_state; |
6400 | 8a7ddc38 | bellard | se->load_state = load_state; |
6401 | 8a7ddc38 | bellard | se->opaque = opaque; |
6402 | 8a7ddc38 | bellard | se->next = NULL;
|
6403 | 8a7ddc38 | bellard | |
6404 | 8a7ddc38 | bellard | /* add at the end of list */
|
6405 | 8a7ddc38 | bellard | pse = &first_se; |
6406 | 18be5187 | pbrook | while (*pse != NULL) { |
6407 | 18be5187 | pbrook | if (instance_id == -1 |
6408 | 18be5187 | pbrook | && strcmp(se->idstr, (*pse)->idstr) == 0
|
6409 | 18be5187 | pbrook | && se->instance_id <= (*pse)->instance_id) |
6410 | 18be5187 | pbrook | se->instance_id = (*pse)->instance_id + 1;
|
6411 | 8a7ddc38 | bellard | pse = &(*pse)->next; |
6412 | 18be5187 | pbrook | } |
6413 | 8a7ddc38 | bellard | *pse = se; |
6414 | 8a7ddc38 | bellard | return 0; |
6415 | 8a7ddc38 | bellard | } |
6416 | 8a7ddc38 | bellard | |
6417 | 8a7ddc38 | bellard | #define QEMU_VM_FILE_MAGIC 0x5145564d |
6418 | faea38e7 | bellard | #define QEMU_VM_FILE_VERSION 0x00000002 |
6419 | 8a7ddc38 | bellard | |
6420 | 9596ebb7 | pbrook | static int qemu_savevm_state(QEMUFile *f) |
6421 | 8a7ddc38 | bellard | { |
6422 | 8a7ddc38 | bellard | SaveStateEntry *se; |
6423 | faea38e7 | bellard | int len, ret;
|
6424 | faea38e7 | bellard | int64_t cur_pos, len_pos, total_len_pos; |
6425 | 313aa567 | bellard | |
6426 | 8a7ddc38 | bellard | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
6427 | 8a7ddc38 | bellard | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
6428 | faea38e7 | bellard | total_len_pos = qemu_ftell(f); |
6429 | faea38e7 | bellard | qemu_put_be64(f, 0); /* total size */ |
6430 | 8a7ddc38 | bellard | |
6431 | 8a7ddc38 | bellard | for(se = first_se; se != NULL; se = se->next) { |
6432 | d978c02c | aurel32 | if (se->save_state == NULL) |
6433 | d978c02c | aurel32 | /* this one has a loader only, for backwards compatibility */
|
6434 | d978c02c | aurel32 | continue;
|
6435 | d978c02c | aurel32 | |
6436 | 8a7ddc38 | bellard | /* ID string */
|
6437 | 8a7ddc38 | bellard | len = strlen(se->idstr); |
6438 | 8a7ddc38 | bellard | qemu_put_byte(f, len); |
6439 | ffe8ab83 | ths | qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
6440 | 8a7ddc38 | bellard | |
6441 | 8a7ddc38 | bellard | qemu_put_be32(f, se->instance_id); |
6442 | 8a7ddc38 | bellard | qemu_put_be32(f, se->version_id); |
6443 | 8a7ddc38 | bellard | |
6444 | 8a7ddc38 | bellard | /* record size: filled later */
|
6445 | faea38e7 | bellard | len_pos = qemu_ftell(f); |
6446 | 8a7ddc38 | bellard | qemu_put_be32(f, 0);
|
6447 | 8a7ddc38 | bellard | se->save_state(f, se->opaque); |
6448 | 8a7ddc38 | bellard | |
6449 | 8a7ddc38 | bellard | /* fill record size */
|
6450 | faea38e7 | bellard | cur_pos = qemu_ftell(f); |
6451 | faea38e7 | bellard | len = cur_pos - len_pos - 4;
|
6452 | faea38e7 | bellard | qemu_fseek(f, len_pos, SEEK_SET); |
6453 | 8a7ddc38 | bellard | qemu_put_be32(f, len); |
6454 | faea38e7 | bellard | qemu_fseek(f, cur_pos, SEEK_SET); |
6455 | 8a7ddc38 | bellard | } |
6456 | faea38e7 | bellard | cur_pos = qemu_ftell(f); |
6457 | faea38e7 | bellard | qemu_fseek(f, total_len_pos, SEEK_SET); |
6458 | faea38e7 | bellard | qemu_put_be64(f, cur_pos - total_len_pos - 8);
|
6459 | faea38e7 | bellard | qemu_fseek(f, cur_pos, SEEK_SET); |
6460 | 8a7ddc38 | bellard | |
6461 | 8a7ddc38 | bellard | ret = 0;
|
6462 | 8a7ddc38 | bellard | return ret;
|
6463 | 8a7ddc38 | bellard | } |
6464 | 8a7ddc38 | bellard | |
6465 | 8a7ddc38 | bellard | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
6466 | 8a7ddc38 | bellard | { |
6467 | 8a7ddc38 | bellard | SaveStateEntry *se; |
6468 | 8a7ddc38 | bellard | |
6469 | 8a7ddc38 | bellard | for(se = first_se; se != NULL; se = se->next) { |
6470 | 5fafdf24 | ths | if (!strcmp(se->idstr, idstr) &&
|
6471 | 8a7ddc38 | bellard | instance_id == se->instance_id) |
6472 | 8a7ddc38 | bellard | return se;
|
6473 | 8a7ddc38 | bellard | } |
6474 | 8a7ddc38 | bellard | return NULL; |
6475 | 8a7ddc38 | bellard | } |
6476 | 8a7ddc38 | bellard | |
6477 | 9596ebb7 | pbrook | static int qemu_loadvm_state(QEMUFile *f) |
6478 | 8a7ddc38 | bellard | { |
6479 | 8a7ddc38 | bellard | SaveStateEntry *se; |
6480 | faea38e7 | bellard | int len, ret, instance_id, record_len, version_id;
|
6481 | faea38e7 | bellard | int64_t total_len, end_pos, cur_pos; |
6482 | 8a7ddc38 | bellard | unsigned int v; |
6483 | 8a7ddc38 | bellard | char idstr[256]; |
6484 | 3b46e624 | ths | |
6485 | 8a7ddc38 | bellard | v = qemu_get_be32(f); |
6486 | 8a7ddc38 | bellard | if (v != QEMU_VM_FILE_MAGIC)
|
6487 | 8a7ddc38 | bellard | goto fail;
|
6488 | 8a7ddc38 | bellard | v = qemu_get_be32(f); |
6489 | 8a7ddc38 | bellard | if (v != QEMU_VM_FILE_VERSION) {
|
6490 | 8a7ddc38 | bellard | fail:
|
6491 | 8a7ddc38 | bellard | ret = -1;
|
6492 | 8a7ddc38 | bellard | goto the_end;
|
6493 | 8a7ddc38 | bellard | } |
6494 | faea38e7 | bellard | total_len = qemu_get_be64(f); |
6495 | faea38e7 | bellard | end_pos = total_len + qemu_ftell(f); |
6496 | b4608c04 | bellard | for(;;) {
|
6497 | faea38e7 | bellard | if (qemu_ftell(f) >= end_pos)
|
6498 | cd4c3e88 | bellard | break;
|
6499 | faea38e7 | bellard | len = qemu_get_byte(f); |
6500 | ffe8ab83 | ths | qemu_get_buffer(f, (uint8_t *)idstr, len); |
6501 | 8a7ddc38 | bellard | idstr[len] = '\0';
|
6502 | 8a7ddc38 | bellard | instance_id = qemu_get_be32(f); |
6503 | 8a7ddc38 | bellard | version_id = qemu_get_be32(f); |
6504 | 8a7ddc38 | bellard | record_len = qemu_get_be32(f); |
6505 | 8a7ddc38 | bellard | #if 0
|
6506 | 5fafdf24 | ths | printf("idstr=%s instance=0x%x version=%d len=%d\n",
|
6507 | 8a7ddc38 | bellard | idstr, instance_id, version_id, record_len);
|
6508 | 8a7ddc38 | bellard | #endif
|
6509 | faea38e7 | bellard | cur_pos = qemu_ftell(f); |
6510 | 8a7ddc38 | bellard | se = find_se(idstr, instance_id); |
6511 | 8a7ddc38 | bellard | if (!se) {
|
6512 | 5fafdf24 | ths | fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
|
6513 | 8a7ddc38 | bellard | instance_id, idstr); |
6514 | 8a7ddc38 | bellard | } else {
|
6515 | 8a7ddc38 | bellard | ret = se->load_state(f, se->opaque, version_id); |
6516 | 8a7ddc38 | bellard | if (ret < 0) { |
6517 | 5fafdf24 | ths | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
|
6518 | 8a7ddc38 | bellard | instance_id, idstr); |
6519 | 8a7ddc38 | bellard | } |
6520 | 34865134 | bellard | } |
6521 | 8a7ddc38 | bellard | /* always seek to exact end of record */
|
6522 | 8a7ddc38 | bellard | qemu_fseek(f, cur_pos + record_len, SEEK_SET); |
6523 | 8a7ddc38 | bellard | } |
6524 | 8a7ddc38 | bellard | ret = 0;
|
6525 | 8a7ddc38 | bellard | the_end:
|
6526 | faea38e7 | bellard | return ret;
|
6527 | faea38e7 | bellard | } |
6528 | faea38e7 | bellard | |
6529 | faea38e7 | bellard | /* device can contain snapshots */
|
6530 | faea38e7 | bellard | static int bdrv_can_snapshot(BlockDriverState *bs) |
6531 | faea38e7 | bellard | { |
6532 | faea38e7 | bellard | return (bs &&
|
6533 | faea38e7 | bellard | !bdrv_is_removable(bs) && |
6534 | faea38e7 | bellard | !bdrv_is_read_only(bs)); |
6535 | faea38e7 | bellard | } |
6536 | faea38e7 | bellard | |
6537 | faea38e7 | bellard | /* device must be snapshots in order to have a reliable snapshot */
|
6538 | faea38e7 | bellard | static int bdrv_has_snapshot(BlockDriverState *bs) |
6539 | faea38e7 | bellard | { |
6540 | faea38e7 | bellard | return (bs &&
|
6541 | faea38e7 | bellard | !bdrv_is_removable(bs) && |
6542 | faea38e7 | bellard | !bdrv_is_read_only(bs)); |
6543 | faea38e7 | bellard | } |
6544 | faea38e7 | bellard | |
6545 | faea38e7 | bellard | static BlockDriverState *get_bs_snapshots(void) |
6546 | faea38e7 | bellard | { |
6547 | faea38e7 | bellard | BlockDriverState *bs; |
6548 | faea38e7 | bellard | int i;
|
6549 | faea38e7 | bellard | |
6550 | faea38e7 | bellard | if (bs_snapshots)
|
6551 | faea38e7 | bellard | return bs_snapshots;
|
6552 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6553 | e4bcb14c | ths | bs = drives_table[i].bdrv; |
6554 | faea38e7 | bellard | if (bdrv_can_snapshot(bs))
|
6555 | faea38e7 | bellard | goto ok;
|
6556 | faea38e7 | bellard | } |
6557 | faea38e7 | bellard | return NULL; |
6558 | faea38e7 | bellard | ok:
|
6559 | faea38e7 | bellard | bs_snapshots = bs; |
6560 | faea38e7 | bellard | return bs;
|
6561 | faea38e7 | bellard | } |
6562 | faea38e7 | bellard | |
6563 | faea38e7 | bellard | static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
6564 | faea38e7 | bellard | const char *name) |
6565 | faea38e7 | bellard | { |
6566 | faea38e7 | bellard | QEMUSnapshotInfo *sn_tab, *sn; |
6567 | faea38e7 | bellard | int nb_sns, i, ret;
|
6568 | 3b46e624 | ths | |
6569 | faea38e7 | bellard | ret = -ENOENT; |
6570 | faea38e7 | bellard | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
6571 | faea38e7 | bellard | if (nb_sns < 0) |
6572 | faea38e7 | bellard | return ret;
|
6573 | faea38e7 | bellard | for(i = 0; i < nb_sns; i++) { |
6574 | faea38e7 | bellard | sn = &sn_tab[i]; |
6575 | faea38e7 | bellard | if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
|
6576 | faea38e7 | bellard | *sn_info = *sn; |
6577 | faea38e7 | bellard | ret = 0;
|
6578 | faea38e7 | bellard | break;
|
6579 | faea38e7 | bellard | } |
6580 | faea38e7 | bellard | } |
6581 | faea38e7 | bellard | qemu_free(sn_tab); |
6582 | faea38e7 | bellard | return ret;
|
6583 | faea38e7 | bellard | } |
6584 | faea38e7 | bellard | |
6585 | faea38e7 | bellard | void do_savevm(const char *name) |
6586 | faea38e7 | bellard | { |
6587 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6588 | faea38e7 | bellard | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; |
6589 | faea38e7 | bellard | int must_delete, ret, i;
|
6590 | faea38e7 | bellard | BlockDriverInfo bdi1, *bdi = &bdi1; |
6591 | faea38e7 | bellard | QEMUFile *f; |
6592 | faea38e7 | bellard | int saved_vm_running;
|
6593 | 4c279bdf | bellard | #ifdef _WIN32
|
6594 | 4c279bdf | bellard | struct _timeb tb;
|
6595 | 4c279bdf | bellard | #else
|
6596 | faea38e7 | bellard | struct timeval tv;
|
6597 | 4c279bdf | bellard | #endif
|
6598 | faea38e7 | bellard | |
6599 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6600 | faea38e7 | bellard | if (!bs) {
|
6601 | faea38e7 | bellard | term_printf("No block device can accept snapshots\n");
|
6602 | faea38e7 | bellard | return;
|
6603 | faea38e7 | bellard | } |
6604 | faea38e7 | bellard | |
6605 | 6192bc37 | pbrook | /* ??? Should this occur after vm_stop? */
|
6606 | 6192bc37 | pbrook | qemu_aio_flush(); |
6607 | 6192bc37 | pbrook | |
6608 | faea38e7 | bellard | saved_vm_running = vm_running; |
6609 | faea38e7 | bellard | vm_stop(0);
|
6610 | 3b46e624 | ths | |
6611 | faea38e7 | bellard | must_delete = 0;
|
6612 | faea38e7 | bellard | if (name) {
|
6613 | faea38e7 | bellard | ret = bdrv_snapshot_find(bs, old_sn, name); |
6614 | faea38e7 | bellard | if (ret >= 0) { |
6615 | faea38e7 | bellard | must_delete = 1;
|
6616 | faea38e7 | bellard | } |
6617 | faea38e7 | bellard | } |
6618 | faea38e7 | bellard | memset(sn, 0, sizeof(*sn)); |
6619 | faea38e7 | bellard | if (must_delete) {
|
6620 | faea38e7 | bellard | pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
|
6621 | faea38e7 | bellard | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
|
6622 | faea38e7 | bellard | } else {
|
6623 | faea38e7 | bellard | if (name)
|
6624 | faea38e7 | bellard | pstrcpy(sn->name, sizeof(sn->name), name);
|
6625 | faea38e7 | bellard | } |
6626 | faea38e7 | bellard | |
6627 | faea38e7 | bellard | /* fill auxiliary fields */
|
6628 | 4c279bdf | bellard | #ifdef _WIN32
|
6629 | 4c279bdf | bellard | _ftime(&tb); |
6630 | 4c279bdf | bellard | sn->date_sec = tb.time; |
6631 | 4c279bdf | bellard | sn->date_nsec = tb.millitm * 1000000;
|
6632 | 4c279bdf | bellard | #else
|
6633 | faea38e7 | bellard | gettimeofday(&tv, NULL);
|
6634 | faea38e7 | bellard | sn->date_sec = tv.tv_sec; |
6635 | faea38e7 | bellard | sn->date_nsec = tv.tv_usec * 1000;
|
6636 | 4c279bdf | bellard | #endif
|
6637 | faea38e7 | bellard | sn->vm_clock_nsec = qemu_get_clock(vm_clock); |
6638 | 3b46e624 | ths | |
6639 | faea38e7 | bellard | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
6640 | faea38e7 | bellard | term_printf("Device %s does not support VM state snapshots\n",
|
6641 | faea38e7 | bellard | bdrv_get_device_name(bs)); |
6642 | faea38e7 | bellard | goto the_end;
|
6643 | faea38e7 | bellard | } |
6644 | 3b46e624 | ths | |
6645 | faea38e7 | bellard | /* save the VM state */
|
6646 | faea38e7 | bellard | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
|
6647 | faea38e7 | bellard | if (!f) {
|
6648 | faea38e7 | bellard | term_printf("Could not open VM state file\n");
|
6649 | faea38e7 | bellard | goto the_end;
|
6650 | faea38e7 | bellard | } |
6651 | faea38e7 | bellard | ret = qemu_savevm_state(f); |
6652 | faea38e7 | bellard | sn->vm_state_size = qemu_ftell(f); |
6653 | faea38e7 | bellard | qemu_fclose(f); |
6654 | faea38e7 | bellard | if (ret < 0) { |
6655 | faea38e7 | bellard | term_printf("Error %d while writing VM\n", ret);
|
6656 | faea38e7 | bellard | goto the_end;
|
6657 | faea38e7 | bellard | } |
6658 | 3b46e624 | ths | |
6659 | faea38e7 | bellard | /* create the snapshots */
|
6660 | faea38e7 | bellard | |
6661 | e4bcb14c | ths | for(i = 0; i < nb_drives; i++) { |
6662 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6663 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6664 | faea38e7 | bellard | if (must_delete) {
|
6665 | faea38e7 | bellard | ret = bdrv_snapshot_delete(bs1, old_sn->id_str); |
6666 | faea38e7 | bellard | if (ret < 0) { |
6667 | faea38e7 | bellard | term_printf("Error while deleting snapshot on '%s'\n",
|
6668 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6669 | faea38e7 | bellard | } |
6670 | faea38e7 | bellard | } |
6671 | faea38e7 | bellard | ret = bdrv_snapshot_create(bs1, sn); |
6672 | faea38e7 | bellard | if (ret < 0) { |
6673 | faea38e7 | bellard | term_printf("Error while creating snapshot on '%s'\n",
|
6674 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6675 | faea38e7 | bellard | } |
6676 | faea38e7 | bellard | } |
6677 | faea38e7 | bellard | } |
6678 | faea38e7 | bellard | |
6679 | faea38e7 | bellard | the_end:
|
6680 | 8a7ddc38 | bellard | if (saved_vm_running)
|
6681 | 8a7ddc38 | bellard | vm_start(); |
6682 | faea38e7 | bellard | } |
6683 | faea38e7 | bellard | |
6684 | faea38e7 | bellard | void do_loadvm(const char *name) |
6685 | faea38e7 | bellard | { |
6686 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6687 | faea38e7 | bellard | BlockDriverInfo bdi1, *bdi = &bdi1; |
6688 | faea38e7 | bellard | QEMUFile *f; |
6689 | faea38e7 | bellard | int i, ret;
|
6690 | faea38e7 | bellard | int saved_vm_running;
|
6691 | faea38e7 | bellard | |
6692 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6693 | faea38e7 | bellard | if (!bs) {
|
6694 | faea38e7 | bellard | term_printf("No block device supports snapshots\n");
|
6695 | faea38e7 | bellard | return;
|
6696 | faea38e7 | bellard | } |
6697 | 3b46e624 | ths | |
6698 | 6192bc37 | pbrook | /* Flush all IO requests so they don't interfere with the new state. */
|
6699 | 6192bc37 | pbrook | qemu_aio_flush(); |
6700 | 6192bc37 | pbrook | |
6701 | faea38e7 | bellard | saved_vm_running = vm_running; |
6702 | faea38e7 | bellard | vm_stop(0);
|
6703 | faea38e7 | bellard | |
6704 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6705 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6706 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6707 | faea38e7 | bellard | ret = bdrv_snapshot_goto(bs1, name); |
6708 | faea38e7 | bellard | if (ret < 0) { |
6709 | faea38e7 | bellard | if (bs != bs1)
|
6710 | faea38e7 | bellard | term_printf("Warning: ");
|
6711 | faea38e7 | bellard | switch(ret) {
|
6712 | faea38e7 | bellard | case -ENOTSUP:
|
6713 | faea38e7 | bellard | term_printf("Snapshots not supported on device '%s'\n",
|
6714 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6715 | faea38e7 | bellard | break;
|
6716 | faea38e7 | bellard | case -ENOENT:
|
6717 | faea38e7 | bellard | term_printf("Could not find snapshot '%s' on device '%s'\n",
|
6718 | faea38e7 | bellard | name, bdrv_get_device_name(bs1)); |
6719 | faea38e7 | bellard | break;
|
6720 | faea38e7 | bellard | default:
|
6721 | faea38e7 | bellard | term_printf("Error %d while activating snapshot on '%s'\n",
|
6722 | faea38e7 | bellard | ret, bdrv_get_device_name(bs1)); |
6723 | faea38e7 | bellard | break;
|
6724 | faea38e7 | bellard | } |
6725 | faea38e7 | bellard | /* fatal on snapshot block device */
|
6726 | faea38e7 | bellard | if (bs == bs1)
|
6727 | faea38e7 | bellard | goto the_end;
|
6728 | faea38e7 | bellard | } |
6729 | faea38e7 | bellard | } |
6730 | faea38e7 | bellard | } |
6731 | faea38e7 | bellard | |
6732 | faea38e7 | bellard | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
6733 | faea38e7 | bellard | term_printf("Device %s does not support VM state snapshots\n",
|
6734 | faea38e7 | bellard | bdrv_get_device_name(bs)); |
6735 | faea38e7 | bellard | return;
|
6736 | faea38e7 | bellard | } |
6737 | 3b46e624 | ths | |
6738 | faea38e7 | bellard | /* restore the VM state */
|
6739 | faea38e7 | bellard | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
|
6740 | faea38e7 | bellard | if (!f) {
|
6741 | faea38e7 | bellard | term_printf("Could not open VM state file\n");
|
6742 | faea38e7 | bellard | goto the_end;
|
6743 | faea38e7 | bellard | } |
6744 | faea38e7 | bellard | ret = qemu_loadvm_state(f); |
6745 | faea38e7 | bellard | qemu_fclose(f); |
6746 | faea38e7 | bellard | if (ret < 0) { |
6747 | faea38e7 | bellard | term_printf("Error %d while loading VM state\n", ret);
|
6748 | faea38e7 | bellard | } |
6749 | faea38e7 | bellard | the_end:
|
6750 | faea38e7 | bellard | if (saved_vm_running)
|
6751 | faea38e7 | bellard | vm_start(); |
6752 | faea38e7 | bellard | } |
6753 | faea38e7 | bellard | |
6754 | faea38e7 | bellard | void do_delvm(const char *name) |
6755 | faea38e7 | bellard | { |
6756 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6757 | faea38e7 | bellard | int i, ret;
|
6758 | faea38e7 | bellard | |
6759 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6760 | faea38e7 | bellard | if (!bs) {
|
6761 | faea38e7 | bellard | term_printf("No block device supports snapshots\n");
|
6762 | faea38e7 | bellard | return;
|
6763 | faea38e7 | bellard | } |
6764 | 3b46e624 | ths | |
6765 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6766 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6767 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6768 | faea38e7 | bellard | ret = bdrv_snapshot_delete(bs1, name); |
6769 | faea38e7 | bellard | if (ret < 0) { |
6770 | faea38e7 | bellard | if (ret == -ENOTSUP)
|
6771 | faea38e7 | bellard | term_printf("Snapshots not supported on device '%s'\n",
|
6772 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6773 | faea38e7 | bellard | else
|
6774 | faea38e7 | bellard | term_printf("Error %d while deleting snapshot on '%s'\n",
|
6775 | faea38e7 | bellard | ret, bdrv_get_device_name(bs1)); |
6776 | faea38e7 | bellard | } |
6777 | faea38e7 | bellard | } |
6778 | faea38e7 | bellard | } |
6779 | faea38e7 | bellard | } |
6780 | faea38e7 | bellard | |
6781 | faea38e7 | bellard | void do_info_snapshots(void) |
6782 | faea38e7 | bellard | { |
6783 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6784 | faea38e7 | bellard | QEMUSnapshotInfo *sn_tab, *sn; |
6785 | faea38e7 | bellard | int nb_sns, i;
|
6786 | faea38e7 | bellard | char buf[256]; |
6787 | faea38e7 | bellard | |
6788 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6789 | faea38e7 | bellard | if (!bs) {
|
6790 | faea38e7 | bellard | term_printf("No available block device supports snapshots\n");
|
6791 | faea38e7 | bellard | return;
|
6792 | faea38e7 | bellard | } |
6793 | faea38e7 | bellard | term_printf("Snapshot devices:");
|
6794 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6795 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6796 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6797 | faea38e7 | bellard | if (bs == bs1)
|
6798 | faea38e7 | bellard | term_printf(" %s", bdrv_get_device_name(bs1));
|
6799 | faea38e7 | bellard | } |
6800 | faea38e7 | bellard | } |
6801 | faea38e7 | bellard | term_printf("\n");
|
6802 | faea38e7 | bellard | |
6803 | faea38e7 | bellard | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
6804 | faea38e7 | bellard | if (nb_sns < 0) { |
6805 | faea38e7 | bellard | term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
|
6806 | faea38e7 | bellard | return;
|
6807 | faea38e7 | bellard | } |
6808 | faea38e7 | bellard | term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
|
6809 | faea38e7 | bellard | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
6810 | faea38e7 | bellard | for(i = 0; i < nb_sns; i++) { |
6811 | faea38e7 | bellard | sn = &sn_tab[i]; |
6812 | faea38e7 | bellard | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
6813 | faea38e7 | bellard | } |
6814 | faea38e7 | bellard | qemu_free(sn_tab); |
6815 | 8a7ddc38 | bellard | } |
6816 | 8a7ddc38 | bellard | |
6817 | 8a7ddc38 | bellard | /***********************************************************/
|
6818 | 8a7ddc38 | bellard | /* ram save/restore */
|
6819 | 8a7ddc38 | bellard | |
6820 | 8a7ddc38 | bellard | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
6821 | 8a7ddc38 | bellard | { |
6822 | 8a7ddc38 | bellard | int v;
|
6823 | 8a7ddc38 | bellard | |
6824 | 8a7ddc38 | bellard | v = qemu_get_byte(f); |
6825 | 8a7ddc38 | bellard | switch(v) {
|
6826 | 8a7ddc38 | bellard | case 0: |
6827 | 8a7ddc38 | bellard | if (qemu_get_buffer(f, buf, len) != len)
|
6828 | 8a7ddc38 | bellard | return -EIO;
|
6829 | 8a7ddc38 | bellard | break;
|
6830 | 8a7ddc38 | bellard | case 1: |
6831 | 8a7ddc38 | bellard | v = qemu_get_byte(f); |
6832 | 8a7ddc38 | bellard | memset(buf, v, len); |
6833 | 8a7ddc38 | bellard | break;
|
6834 | 8a7ddc38 | bellard | default:
|
6835 | 8a7ddc38 | bellard | return -EINVAL;
|
6836 | 8a7ddc38 | bellard | } |
6837 | 8a7ddc38 | bellard | return 0; |
6838 | 8a7ddc38 | bellard | } |
6839 | 8a7ddc38 | bellard | |
6840 | c88676f8 | bellard | static int ram_load_v1(QEMUFile *f, void *opaque) |
6841 | c88676f8 | bellard | { |
6842 | 00f82b8a | aurel32 | int ret;
|
6843 | 00f82b8a | aurel32 | ram_addr_t i; |
6844 | c88676f8 | bellard | |
6845 | c88676f8 | bellard | if (qemu_get_be32(f) != phys_ram_size)
|
6846 | c88676f8 | bellard | return -EINVAL;
|
6847 | c88676f8 | bellard | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
6848 | c88676f8 | bellard | ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
6849 | c88676f8 | bellard | if (ret)
|
6850 | c88676f8 | bellard | return ret;
|
6851 | c88676f8 | bellard | } |
6852 | c88676f8 | bellard | return 0; |
6853 | c88676f8 | bellard | } |
6854 | c88676f8 | bellard | |
6855 | c88676f8 | bellard | #define BDRV_HASH_BLOCK_SIZE 1024 |
6856 | c88676f8 | bellard | #define IOBUF_SIZE 4096 |
6857 | c88676f8 | bellard | #define RAM_CBLOCK_MAGIC 0xfabe |
6858 | c88676f8 | bellard | |
6859 | c88676f8 | bellard | typedef struct RamCompressState { |
6860 | c88676f8 | bellard | z_stream zstream; |
6861 | c88676f8 | bellard | QEMUFile *f; |
6862 | c88676f8 | bellard | uint8_t buf[IOBUF_SIZE]; |
6863 | c88676f8 | bellard | } RamCompressState; |
6864 | c88676f8 | bellard | |
6865 | c88676f8 | bellard | static int ram_compress_open(RamCompressState *s, QEMUFile *f) |
6866 | c88676f8 | bellard | { |
6867 | c88676f8 | bellard | int ret;
|
6868 | c88676f8 | bellard | memset(s, 0, sizeof(*s)); |
6869 | c88676f8 | bellard | s->f = f; |
6870 | c88676f8 | bellard | ret = deflateInit2(&s->zstream, 1,
|
6871 | 5fafdf24 | ths | Z_DEFLATED, 15,
|
6872 | c88676f8 | bellard | 9, Z_DEFAULT_STRATEGY);
|
6873 | c88676f8 | bellard | if (ret != Z_OK)
|
6874 | c88676f8 | bellard | return -1; |
6875 | c88676f8 | bellard | s->zstream.avail_out = IOBUF_SIZE; |
6876 | c88676f8 | bellard | s->zstream.next_out = s->buf; |
6877 | c88676f8 | bellard | return 0; |
6878 | c88676f8 | bellard | } |
6879 | c88676f8 | bellard | |
6880 | c88676f8 | bellard | static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len) |
6881 | c88676f8 | bellard | { |
6882 | c88676f8 | bellard | qemu_put_be16(s->f, RAM_CBLOCK_MAGIC); |
6883 | c88676f8 | bellard | qemu_put_be16(s->f, len); |
6884 | c88676f8 | bellard | qemu_put_buffer(s->f, buf, len); |
6885 | c88676f8 | bellard | } |
6886 | c88676f8 | bellard | |
6887 | c88676f8 | bellard | static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len) |
6888 | c88676f8 | bellard | { |
6889 | c88676f8 | bellard | int ret;
|
6890 | c88676f8 | bellard | |
6891 | c88676f8 | bellard | s->zstream.avail_in = len; |
6892 | c88676f8 | bellard | s->zstream.next_in = (uint8_t *)buf; |
6893 | c88676f8 | bellard | while (s->zstream.avail_in > 0) { |
6894 | c88676f8 | bellard | ret = deflate(&s->zstream, Z_NO_FLUSH); |
6895 | c88676f8 | bellard | if (ret != Z_OK)
|
6896 | c88676f8 | bellard | return -1; |
6897 | c88676f8 | bellard | if (s->zstream.avail_out == 0) { |
6898 | c88676f8 | bellard | ram_put_cblock(s, s->buf, IOBUF_SIZE); |
6899 | c88676f8 | bellard | s->zstream.avail_out = IOBUF_SIZE; |
6900 | c88676f8 | bellard | s->zstream.next_out = s->buf; |
6901 | c88676f8 | bellard | } |
6902 | c88676f8 | bellard | } |
6903 | c88676f8 | bellard | return 0; |
6904 | c88676f8 | bellard | } |
6905 | c88676f8 | bellard | |
6906 | c88676f8 | bellard | static void ram_compress_close(RamCompressState *s) |
6907 | c88676f8 | bellard | { |
6908 | c88676f8 | bellard | int len, ret;
|
6909 | c88676f8 | bellard | |
6910 | c88676f8 | bellard | /* compress last bytes */
|
6911 | c88676f8 | bellard | for(;;) {
|
6912 | c88676f8 | bellard | ret = deflate(&s->zstream, Z_FINISH); |
6913 | c88676f8 | bellard | if (ret == Z_OK || ret == Z_STREAM_END) {
|
6914 | c88676f8 | bellard | len = IOBUF_SIZE - s->zstream.avail_out; |
6915 | c88676f8 | bellard | if (len > 0) { |
6916 | c88676f8 | bellard | ram_put_cblock(s, s->buf, len); |
6917 | c88676f8 | bellard | } |
6918 | c88676f8 | bellard | s->zstream.avail_out = IOBUF_SIZE; |
6919 | c88676f8 | bellard | s->zstream.next_out = s->buf; |
6920 | c88676f8 | bellard | if (ret == Z_STREAM_END)
|
6921 | c88676f8 | bellard | break;
|
6922 | c88676f8 | bellard | } else {
|
6923 | c88676f8 | bellard | goto fail;
|
6924 | c88676f8 | bellard | } |
6925 | c88676f8 | bellard | } |
6926 | c88676f8 | bellard | fail:
|
6927 | c88676f8 | bellard | deflateEnd(&s->zstream); |
6928 | c88676f8 | bellard | } |
6929 | c88676f8 | bellard | |
6930 | c88676f8 | bellard | typedef struct RamDecompressState { |
6931 | c88676f8 | bellard | z_stream zstream; |
6932 | c88676f8 | bellard | QEMUFile *f; |
6933 | c88676f8 | bellard | uint8_t buf[IOBUF_SIZE]; |
6934 | c88676f8 | bellard | } RamDecompressState; |
6935 | c88676f8 | bellard | |
6936 | c88676f8 | bellard | static int ram_decompress_open(RamDecompressState *s, QEMUFile *f) |
6937 | c88676f8 | bellard | { |
6938 | c88676f8 | bellard | int ret;
|
6939 | c88676f8 | bellard | memset(s, 0, sizeof(*s)); |
6940 | c88676f8 | bellard | s->f = f; |
6941 | c88676f8 | bellard | ret = inflateInit(&s->zstream); |
6942 | c88676f8 | bellard | if (ret != Z_OK)
|
6943 | c88676f8 | bellard | return -1; |
6944 | c88676f8 | bellard | return 0; |
6945 | c88676f8 | bellard | } |
6946 | c88676f8 | bellard | |
6947 | c88676f8 | bellard | static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len) |
6948 | c88676f8 | bellard | { |
6949 | c88676f8 | bellard | int ret, clen;
|
6950 | c88676f8 | bellard | |
6951 | c88676f8 | bellard | s->zstream.avail_out = len; |
6952 | c88676f8 | bellard | s->zstream.next_out = buf; |
6953 | c88676f8 | bellard | while (s->zstream.avail_out > 0) { |
6954 | c88676f8 | bellard | if (s->zstream.avail_in == 0) { |
6955 | c88676f8 | bellard | if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
|
6956 | c88676f8 | bellard | return -1; |
6957 | c88676f8 | bellard | clen = qemu_get_be16(s->f); |
6958 | c88676f8 | bellard | if (clen > IOBUF_SIZE)
|
6959 | c88676f8 | bellard | return -1; |
6960 | c88676f8 | bellard | qemu_get_buffer(s->f, s->buf, clen); |
6961 | c88676f8 | bellard | s->zstream.avail_in = clen; |
6962 | c88676f8 | bellard | s->zstream.next_in = s->buf; |
6963 | c88676f8 | bellard | } |
6964 | c88676f8 | bellard | ret = inflate(&s->zstream, Z_PARTIAL_FLUSH); |
6965 | c88676f8 | bellard | if (ret != Z_OK && ret != Z_STREAM_END) {
|
6966 | c88676f8 | bellard | return -1; |
6967 | c88676f8 | bellard | } |
6968 | c88676f8 | bellard | } |
6969 | c88676f8 | bellard | return 0; |
6970 | c88676f8 | bellard | } |
6971 | c88676f8 | bellard | |
6972 | c88676f8 | bellard | static void ram_decompress_close(RamDecompressState *s) |
6973 | c88676f8 | bellard | { |
6974 | c88676f8 | bellard | inflateEnd(&s->zstream); |
6975 | c88676f8 | bellard | } |
6976 | c88676f8 | bellard | |
6977 | 8a7ddc38 | bellard | static void ram_save(QEMUFile *f, void *opaque) |
6978 | 8a7ddc38 | bellard | { |
6979 | 00f82b8a | aurel32 | ram_addr_t i; |
6980 | c88676f8 | bellard | RamCompressState s1, *s = &s1; |
6981 | c88676f8 | bellard | uint8_t buf[10];
|
6982 | 3b46e624 | ths | |
6983 | 8a7ddc38 | bellard | qemu_put_be32(f, phys_ram_size); |
6984 | c88676f8 | bellard | if (ram_compress_open(s, f) < 0) |
6985 | c88676f8 | bellard | return;
|
6986 | c88676f8 | bellard | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
6987 | c88676f8 | bellard | #if 0
|
6988 | c88676f8 | bellard | if (tight_savevm_enabled) {
|
6989 | c88676f8 | bellard | int64_t sector_num;
|
6990 | c88676f8 | bellard | int j;
|
6991 | c88676f8 | bellard | |
6992 | c88676f8 | bellard | /* find if the memory block is available on a virtual
|
6993 | c88676f8 | bellard | block device */
|
6994 | c88676f8 | bellard | sector_num = -1;
|
6995 | e4bcb14c | ths | for(j = 0; j < nb_drives; j++) {
|
6996 | e4bcb14c | ths | sector_num = bdrv_hash_find(drives_table[j].bdrv,
|
6997 | e4bcb14c | ths | phys_ram_base + i,
|
6998 | e4bcb14c | ths | BDRV_HASH_BLOCK_SIZE);
|
6999 | e4bcb14c | ths | if (sector_num >= 0)
|
7000 | e4bcb14c | ths | break;
|
7001 | c88676f8 | bellard | }
|
7002 | e4bcb14c | ths | if (j == nb_drives)
|
7003 | c88676f8 | bellard | goto normal_compress;
|
7004 | c88676f8 | bellard | buf[0] = 1;
|
7005 | c88676f8 | bellard | buf[1] = j;
|
7006 | c88676f8 | bellard | cpu_to_be64wu((uint64_t *)(buf + 2), sector_num);
|
7007 | c88676f8 | bellard | ram_compress_buf(s, buf, 10);
|
7008 | 5fafdf24 | ths | } else
|
7009 | c88676f8 | bellard | #endif
|
7010 | c88676f8 | bellard | { |
7011 | c88676f8 | bellard | // normal_compress:
|
7012 | c88676f8 | bellard | buf[0] = 0; |
7013 | c88676f8 | bellard | ram_compress_buf(s, buf, 1);
|
7014 | c88676f8 | bellard | ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); |
7015 | c88676f8 | bellard | } |
7016 | 8a7ddc38 | bellard | } |
7017 | c88676f8 | bellard | ram_compress_close(s); |
7018 | 8a7ddc38 | bellard | } |
7019 | 8a7ddc38 | bellard | |
7020 | 8a7ddc38 | bellard | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
7021 | 8a7ddc38 | bellard | { |
7022 | c88676f8 | bellard | RamDecompressState s1, *s = &s1; |
7023 | c88676f8 | bellard | uint8_t buf[10];
|
7024 | 00f82b8a | aurel32 | ram_addr_t i; |
7025 | 8a7ddc38 | bellard | |
7026 | c88676f8 | bellard | if (version_id == 1) |
7027 | c88676f8 | bellard | return ram_load_v1(f, opaque);
|
7028 | c88676f8 | bellard | if (version_id != 2) |
7029 | 8a7ddc38 | bellard | return -EINVAL;
|
7030 | 8a7ddc38 | bellard | if (qemu_get_be32(f) != phys_ram_size)
|
7031 | 8a7ddc38 | bellard | return -EINVAL;
|
7032 | c88676f8 | bellard | if (ram_decompress_open(s, f) < 0) |
7033 | c88676f8 | bellard | return -EINVAL;
|
7034 | c88676f8 | bellard | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
7035 | c88676f8 | bellard | if (ram_decompress_buf(s, buf, 1) < 0) { |
7036 | c88676f8 | bellard | fprintf(stderr, "Error while reading ram block header\n");
|
7037 | c88676f8 | bellard | goto error;
|
7038 | c88676f8 | bellard | } |
7039 | c88676f8 | bellard | if (buf[0] == 0) { |
7040 | c88676f8 | bellard | if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) { |
7041 | 00f82b8a | aurel32 | fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
|
7042 | c88676f8 | bellard | goto error;
|
7043 | c88676f8 | bellard | } |
7044 | 5fafdf24 | ths | } else
|
7045 | c88676f8 | bellard | #if 0
|
7046 | c88676f8 | bellard | if (buf[0] == 1) {
|
7047 | c88676f8 | bellard | int bs_index;
|
7048 | c88676f8 | bellard | int64_t sector_num;
|
7049 | c88676f8 | bellard | |
7050 | c88676f8 | bellard | ram_decompress_buf(s, buf + 1, 9);
|
7051 | c88676f8 | bellard | bs_index = buf[1];
|
7052 | c88676f8 | bellard | sector_num = be64_to_cpupu((const uint64_t *)(buf + 2));
|
7053 | e4bcb14c | ths | if (bs_index >= nb_drives) {
|
7054 | c88676f8 | bellard | fprintf(stderr, "Invalid block device index %d\n", bs_index);
|
7055 | c88676f8 | bellard | goto error;
|
7056 | c88676f8 | bellard | }
|
7057 | e4bcb14c | ths | if (bdrv_read(drives_table[bs_index].bdrv, sector_num,
|
7058 | e4bcb14c | ths | phys_ram_base + i,
|
7059 | c88676f8 | bellard | BDRV_HASH_BLOCK_SIZE / 512) < 0) {
|
7060 | 5fafdf24 | ths | fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n",
|
7061 | c88676f8 | bellard | bs_index, sector_num);
|
7062 | c88676f8 | bellard | goto error;
|
7063 | c88676f8 | bellard | }
|
7064 | 5fafdf24 | ths | } else
|
7065 | c88676f8 | bellard | #endif
|
7066 | c88676f8 | bellard | { |
7067 | c88676f8 | bellard | error:
|
7068 | c88676f8 | bellard | printf("Error block header\n");
|
7069 | c88676f8 | bellard | return -EINVAL;
|
7070 | c88676f8 | bellard | } |
7071 | 8a7ddc38 | bellard | } |
7072 | c88676f8 | bellard | ram_decompress_close(s); |
7073 | 8a7ddc38 | bellard | return 0; |
7074 | 8a7ddc38 | bellard | } |
7075 | 8a7ddc38 | bellard | |
7076 | 8a7ddc38 | bellard | /***********************************************************/
|
7077 | 83f64091 | bellard | /* bottom halves (can be seen as timers which expire ASAP) */
|
7078 | 83f64091 | bellard | |
7079 | 83f64091 | bellard | struct QEMUBH {
|
7080 | 83f64091 | bellard | QEMUBHFunc *cb; |
7081 | 83f64091 | bellard | void *opaque;
|
7082 | 83f64091 | bellard | int scheduled;
|
7083 | 83f64091 | bellard | QEMUBH *next; |
7084 | 83f64091 | bellard | }; |
7085 | 83f64091 | bellard | |
7086 | 83f64091 | bellard | static QEMUBH *first_bh = NULL; |
7087 | 83f64091 | bellard | |
7088 | 83f64091 | bellard | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
|
7089 | 83f64091 | bellard | { |
7090 | 83f64091 | bellard | QEMUBH *bh; |
7091 | 83f64091 | bellard | bh = qemu_mallocz(sizeof(QEMUBH));
|
7092 | 83f64091 | bellard | if (!bh)
|
7093 | 83f64091 | bellard | return NULL; |
7094 | 83f64091 | bellard | bh->cb = cb; |
7095 | 83f64091 | bellard | bh->opaque = opaque; |
7096 | 83f64091 | bellard | return bh;
|
7097 | 83f64091 | bellard | } |
7098 | 83f64091 | bellard | |
7099 | 6eb5733a | bellard | int qemu_bh_poll(void) |
7100 | 83f64091 | bellard | { |
7101 | 83f64091 | bellard | QEMUBH *bh, **pbh; |
7102 | 6eb5733a | bellard | int ret;
|
7103 | 83f64091 | bellard | |
7104 | 6eb5733a | bellard | ret = 0;
|
7105 | 83f64091 | bellard | for(;;) {
|
7106 | 83f64091 | bellard | pbh = &first_bh; |
7107 | 83f64091 | bellard | bh = *pbh; |
7108 | 83f64091 | bellard | if (!bh)
|
7109 | 83f64091 | bellard | break;
|
7110 | 6eb5733a | bellard | ret = 1;
|
7111 | 83f64091 | bellard | *pbh = bh->next; |
7112 | 83f64091 | bellard | bh->scheduled = 0;
|
7113 | 83f64091 | bellard | bh->cb(bh->opaque); |
7114 | 83f64091 | bellard | } |
7115 | 6eb5733a | bellard | return ret;
|
7116 | 83f64091 | bellard | } |
7117 | 83f64091 | bellard | |
7118 | 83f64091 | bellard | void qemu_bh_schedule(QEMUBH *bh)
|
7119 | 83f64091 | bellard | { |
7120 | 83f64091 | bellard | CPUState *env = cpu_single_env; |
7121 | 83f64091 | bellard | if (bh->scheduled)
|
7122 | 83f64091 | bellard | return;
|
7123 | 83f64091 | bellard | bh->scheduled = 1;
|
7124 | 83f64091 | bellard | bh->next = first_bh; |
7125 | 83f64091 | bellard | first_bh = bh; |
7126 | 83f64091 | bellard | |
7127 | 83f64091 | bellard | /* stop the currently executing CPU to execute the BH ASAP */
|
7128 | 83f64091 | bellard | if (env) {
|
7129 | 83f64091 | bellard | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
7130 | 83f64091 | bellard | } |
7131 | 83f64091 | bellard | } |
7132 | 83f64091 | bellard | |
7133 | 83f64091 | bellard | void qemu_bh_cancel(QEMUBH *bh)
|
7134 | 83f64091 | bellard | { |
7135 | 83f64091 | bellard | QEMUBH **pbh; |
7136 | 83f64091 | bellard | if (bh->scheduled) {
|
7137 | 83f64091 | bellard | pbh = &first_bh; |
7138 | 83f64091 | bellard | while (*pbh != bh)
|
7139 | 83f64091 | bellard | pbh = &(*pbh)->next; |
7140 | 83f64091 | bellard | *pbh = bh->next; |
7141 | 83f64091 | bellard | bh->scheduled = 0;
|
7142 | 83f64091 | bellard | } |
7143 | 83f64091 | bellard | } |
7144 | 83f64091 | bellard | |
7145 | 83f64091 | bellard | void qemu_bh_delete(QEMUBH *bh)
|
7146 | 83f64091 | bellard | { |
7147 | 83f64091 | bellard | qemu_bh_cancel(bh); |
7148 | 83f64091 | bellard | qemu_free(bh); |
7149 | 83f64091 | bellard | } |
7150 | 83f64091 | bellard | |
7151 | 83f64091 | bellard | /***********************************************************/
|
7152 | cc1daa40 | bellard | /* machine registration */
|
7153 | cc1daa40 | bellard | |
7154 | cc1daa40 | bellard | QEMUMachine *first_machine = NULL;
|
7155 | cc1daa40 | bellard | |
7156 | cc1daa40 | bellard | int qemu_register_machine(QEMUMachine *m)
|
7157 | cc1daa40 | bellard | { |
7158 | cc1daa40 | bellard | QEMUMachine **pm; |
7159 | cc1daa40 | bellard | pm = &first_machine; |
7160 | cc1daa40 | bellard | while (*pm != NULL) |
7161 | cc1daa40 | bellard | pm = &(*pm)->next; |
7162 | cc1daa40 | bellard | m->next = NULL;
|
7163 | cc1daa40 | bellard | *pm = m; |
7164 | cc1daa40 | bellard | return 0; |
7165 | cc1daa40 | bellard | } |
7166 | cc1daa40 | bellard | |
7167 | 9596ebb7 | pbrook | static QEMUMachine *find_machine(const char *name) |
7168 | cc1daa40 | bellard | { |
7169 | cc1daa40 | bellard | QEMUMachine *m; |
7170 | cc1daa40 | bellard | |
7171 | cc1daa40 | bellard | for(m = first_machine; m != NULL; m = m->next) { |
7172 | cc1daa40 | bellard | if (!strcmp(m->name, name))
|
7173 | cc1daa40 | bellard | return m;
|
7174 | cc1daa40 | bellard | } |
7175 | cc1daa40 | bellard | return NULL; |
7176 | cc1daa40 | bellard | } |
7177 | cc1daa40 | bellard | |
7178 | cc1daa40 | bellard | /***********************************************************/
|
7179 | 8a7ddc38 | bellard | /* main execution loop */
|
7180 | 8a7ddc38 | bellard | |
7181 | 9596ebb7 | pbrook | static void gui_update(void *opaque) |
7182 | 8a7ddc38 | bellard | { |
7183 | 740733bb | ths | DisplayState *ds = opaque; |
7184 | 740733bb | ths | ds->dpy_refresh(ds); |
7185 | f442e08b | aurel32 | qemu_mod_timer(ds->gui_timer, |
7186 | f442e08b | aurel32 | (ds->gui_timer_interval ? |
7187 | f442e08b | aurel32 | ds->gui_timer_interval : |
7188 | f442e08b | aurel32 | GUI_REFRESH_INTERVAL) |
7189 | f442e08b | aurel32 | + qemu_get_clock(rt_clock)); |
7190 | 8a7ddc38 | bellard | } |
7191 | 8a7ddc38 | bellard | |
7192 | 0bd48850 | bellard | struct vm_change_state_entry {
|
7193 | 0bd48850 | bellard | VMChangeStateHandler *cb; |
7194 | 0bd48850 | bellard | void *opaque;
|
7195 | 0bd48850 | bellard | LIST_ENTRY (vm_change_state_entry) entries; |
7196 | 0bd48850 | bellard | }; |
7197 | 0bd48850 | bellard | |
7198 | 0bd48850 | bellard | static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
|
7199 | 0bd48850 | bellard | |
7200 | 0bd48850 | bellard | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
7201 | 0bd48850 | bellard | void *opaque)
|
7202 | 0bd48850 | bellard | { |
7203 | 0bd48850 | bellard | VMChangeStateEntry *e; |
7204 | 0bd48850 | bellard | |
7205 | 0bd48850 | bellard | e = qemu_mallocz(sizeof (*e));
|
7206 | 0bd48850 | bellard | if (!e)
|
7207 | 0bd48850 | bellard | return NULL; |
7208 | 0bd48850 | bellard | |
7209 | 0bd48850 | bellard | e->cb = cb; |
7210 | 0bd48850 | bellard | e->opaque = opaque; |
7211 | 0bd48850 | bellard | LIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
7212 | 0bd48850 | bellard | return e;
|
7213 | 0bd48850 | bellard | } |
7214 | 0bd48850 | bellard | |
7215 | 0bd48850 | bellard | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
|
7216 | 0bd48850 | bellard | { |
7217 | 0bd48850 | bellard | LIST_REMOVE (e, entries); |
7218 | 0bd48850 | bellard | qemu_free (e); |
7219 | 0bd48850 | bellard | } |
7220 | 0bd48850 | bellard | |
7221 | 0bd48850 | bellard | static void vm_state_notify(int running) |
7222 | 0bd48850 | bellard | { |
7223 | 0bd48850 | bellard | VMChangeStateEntry *e; |
7224 | 0bd48850 | bellard | |
7225 | 0bd48850 | bellard | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
|
7226 | 0bd48850 | bellard | e->cb(e->opaque, running); |
7227 | 0bd48850 | bellard | } |
7228 | 0bd48850 | bellard | } |
7229 | 0bd48850 | bellard | |
7230 | 8a7ddc38 | bellard | /* XXX: support several handlers */
|
7231 | 0bd48850 | bellard | static VMStopHandler *vm_stop_cb;
|
7232 | 0bd48850 | bellard | static void *vm_stop_opaque; |
7233 | 8a7ddc38 | bellard | |
7234 | 8a7ddc38 | bellard | int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) |
7235 | 8a7ddc38 | bellard | { |
7236 | 8a7ddc38 | bellard | vm_stop_cb = cb; |
7237 | 8a7ddc38 | bellard | vm_stop_opaque = opaque; |
7238 | 8a7ddc38 | bellard | return 0; |
7239 | 8a7ddc38 | bellard | } |
7240 | 8a7ddc38 | bellard | |
7241 | 8a7ddc38 | bellard | void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) |
7242 | 8a7ddc38 | bellard | { |
7243 | 8a7ddc38 | bellard | vm_stop_cb = NULL;
|
7244 | 8a7ddc38 | bellard | } |
7245 | 8a7ddc38 | bellard | |
7246 | 8a7ddc38 | bellard | void vm_start(void) |
7247 | 8a7ddc38 | bellard | { |
7248 | 8a7ddc38 | bellard | if (!vm_running) {
|
7249 | 8a7ddc38 | bellard | cpu_enable_ticks(); |
7250 | 8a7ddc38 | bellard | vm_running = 1;
|
7251 | 0bd48850 | bellard | vm_state_notify(1);
|
7252 | efe75411 | ths | qemu_rearm_alarm_timer(alarm_timer); |
7253 | 8a7ddc38 | bellard | } |
7254 | 8a7ddc38 | bellard | } |
7255 | 8a7ddc38 | bellard | |
7256 | 5fafdf24 | ths | void vm_stop(int reason) |
7257 | 8a7ddc38 | bellard | { |
7258 | 8a7ddc38 | bellard | if (vm_running) {
|
7259 | 8a7ddc38 | bellard | cpu_disable_ticks(); |
7260 | 8a7ddc38 | bellard | vm_running = 0;
|
7261 | 8a7ddc38 | bellard | if (reason != 0) { |
7262 | 8a7ddc38 | bellard | if (vm_stop_cb) {
|
7263 | 8a7ddc38 | bellard | vm_stop_cb(vm_stop_opaque, reason); |
7264 | 8a7ddc38 | bellard | } |
7265 | 34865134 | bellard | } |
7266 | 0bd48850 | bellard | vm_state_notify(0);
|
7267 | 8a7ddc38 | bellard | } |
7268 | 8a7ddc38 | bellard | } |
7269 | 8a7ddc38 | bellard | |
7270 | bb0c6722 | bellard | /* reset/shutdown handler */
|
7271 | bb0c6722 | bellard | |
7272 | bb0c6722 | bellard | typedef struct QEMUResetEntry { |
7273 | bb0c6722 | bellard | QEMUResetHandler *func; |
7274 | bb0c6722 | bellard | void *opaque;
|
7275 | bb0c6722 | bellard | struct QEMUResetEntry *next;
|
7276 | bb0c6722 | bellard | } QEMUResetEntry; |
7277 | bb0c6722 | bellard | |
7278 | bb0c6722 | bellard | static QEMUResetEntry *first_reset_entry;
|
7279 | bb0c6722 | bellard | static int reset_requested; |
7280 | bb0c6722 | bellard | static int shutdown_requested; |
7281 | 3475187d | bellard | static int powerdown_requested; |
7282 | bb0c6722 | bellard | |
7283 | cf7a2fe2 | aurel32 | int qemu_shutdown_requested(void) |
7284 | cf7a2fe2 | aurel32 | { |
7285 | cf7a2fe2 | aurel32 | int r = shutdown_requested;
|
7286 | cf7a2fe2 | aurel32 | shutdown_requested = 0;
|
7287 | cf7a2fe2 | aurel32 | return r;
|
7288 | cf7a2fe2 | aurel32 | } |
7289 | cf7a2fe2 | aurel32 | |
7290 | cf7a2fe2 | aurel32 | int qemu_reset_requested(void) |
7291 | cf7a2fe2 | aurel32 | { |
7292 | cf7a2fe2 | aurel32 | int r = reset_requested;
|
7293 | cf7a2fe2 | aurel32 | reset_requested = 0;
|
7294 | cf7a2fe2 | aurel32 | return r;
|
7295 | cf7a2fe2 | aurel32 | } |
7296 | cf7a2fe2 | aurel32 | |
7297 | cf7a2fe2 | aurel32 | int qemu_powerdown_requested(void) |
7298 | cf7a2fe2 | aurel32 | { |
7299 | cf7a2fe2 | aurel32 | int r = powerdown_requested;
|
7300 | cf7a2fe2 | aurel32 | powerdown_requested = 0;
|
7301 | cf7a2fe2 | aurel32 | return r;
|
7302 | cf7a2fe2 | aurel32 | } |
7303 | cf7a2fe2 | aurel32 | |
7304 | bb0c6722 | bellard | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
7305 | bb0c6722 | bellard | { |
7306 | bb0c6722 | bellard | QEMUResetEntry **pre, *re; |
7307 | bb0c6722 | bellard | |
7308 | bb0c6722 | bellard | pre = &first_reset_entry; |
7309 | bb0c6722 | bellard | while (*pre != NULL) |
7310 | bb0c6722 | bellard | pre = &(*pre)->next; |
7311 | bb0c6722 | bellard | re = qemu_mallocz(sizeof(QEMUResetEntry));
|
7312 | bb0c6722 | bellard | re->func = func; |
7313 | bb0c6722 | bellard | re->opaque = opaque; |
7314 | bb0c6722 | bellard | re->next = NULL;
|
7315 | bb0c6722 | bellard | *pre = re; |
7316 | bb0c6722 | bellard | } |
7317 | bb0c6722 | bellard | |
7318 | cf7a2fe2 | aurel32 | void qemu_system_reset(void) |
7319 | bb0c6722 | bellard | { |
7320 | bb0c6722 | bellard | QEMUResetEntry *re; |
7321 | bb0c6722 | bellard | |
7322 | bb0c6722 | bellard | /* reset all devices */
|
7323 | bb0c6722 | bellard | for(re = first_reset_entry; re != NULL; re = re->next) { |
7324 | bb0c6722 | bellard | re->func(re->opaque); |
7325 | bb0c6722 | bellard | } |
7326 | bb0c6722 | bellard | } |
7327 | bb0c6722 | bellard | |
7328 | bb0c6722 | bellard | void qemu_system_reset_request(void) |
7329 | bb0c6722 | bellard | { |
7330 | d1beab82 | bellard | if (no_reboot) {
|
7331 | d1beab82 | bellard | shutdown_requested = 1;
|
7332 | d1beab82 | bellard | } else {
|
7333 | d1beab82 | bellard | reset_requested = 1;
|
7334 | d1beab82 | bellard | } |
7335 | 6a00d601 | bellard | if (cpu_single_env)
|
7336 | 6a00d601 | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
7337 | bb0c6722 | bellard | } |
7338 | bb0c6722 | bellard | |
7339 | bb0c6722 | bellard | void qemu_system_shutdown_request(void) |
7340 | bb0c6722 | bellard | { |
7341 | bb0c6722 | bellard | shutdown_requested = 1;
|
7342 | 6a00d601 | bellard | if (cpu_single_env)
|
7343 | 6a00d601 | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
7344 | bb0c6722 | bellard | } |
7345 | bb0c6722 | bellard | |
7346 | 3475187d | bellard | void qemu_system_powerdown_request(void) |
7347 | 3475187d | bellard | { |
7348 | 3475187d | bellard | powerdown_requested = 1;
|
7349 | 6a00d601 | bellard | if (cpu_single_env)
|
7350 | 6a00d601 | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
7351 | bb0c6722 | bellard | } |
7352 | bb0c6722 | bellard | |
7353 | 5905b2e5 | bellard | void main_loop_wait(int timeout) |
7354 | 8a7ddc38 | bellard | { |
7355 | cafffd40 | ths | IOHandlerRecord *ioh; |
7356 | e035649e | bellard | fd_set rfds, wfds, xfds; |
7357 | 877cf882 | ths | int ret, nfds;
|
7358 | 877cf882 | ths | #ifdef _WIN32
|
7359 | 877cf882 | ths | int ret2, i;
|
7360 | 877cf882 | ths | #endif
|
7361 | fd1dff4b | bellard | struct timeval tv;
|
7362 | f331110f | bellard | PollingEntry *pe; |
7363 | f331110f | bellard | |
7364 | c4b1fcc0 | bellard | |
7365 | f331110f | bellard | /* XXX: need to suppress polling by better using win32 events */
|
7366 | f331110f | bellard | ret = 0;
|
7367 | f331110f | bellard | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
7368 | f331110f | bellard | ret |= pe->func(pe->opaque); |
7369 | f331110f | bellard | } |
7370 | 38e205a2 | bellard | #ifdef _WIN32
|
7371 | e6b1e558 | ths | if (ret == 0) { |
7372 | a18e524a | bellard | int err;
|
7373 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
7374 | 3b46e624 | ths | |
7375 | a18e524a | bellard | ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout); |
7376 | a18e524a | bellard | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
7377 | a18e524a | bellard | if (w->func[ret - WAIT_OBJECT_0])
|
7378 | a18e524a | bellard | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
7379 | 3b46e624 | ths | |
7380 | 5fafdf24 | ths | /* Check for additional signaled events */
|
7381 | e6b1e558 | ths | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
7382 | 3b46e624 | ths | |
7383 | e6b1e558 | ths | /* Check if event is signaled */
|
7384 | e6b1e558 | ths | ret2 = WaitForSingleObject(w->events[i], 0);
|
7385 | e6b1e558 | ths | if(ret2 == WAIT_OBJECT_0) {
|
7386 | e6b1e558 | ths | if (w->func[i])
|
7387 | e6b1e558 | ths | w->func[i](w->opaque[i]); |
7388 | e6b1e558 | ths | } else if (ret2 == WAIT_TIMEOUT) { |
7389 | e6b1e558 | ths | } else {
|
7390 | e6b1e558 | ths | err = GetLastError(); |
7391 | e6b1e558 | ths | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
|
7392 | 3b46e624 | ths | } |
7393 | 3b46e624 | ths | } |
7394 | a18e524a | bellard | } else if (ret == WAIT_TIMEOUT) { |
7395 | a18e524a | bellard | } else {
|
7396 | a18e524a | bellard | err = GetLastError(); |
7397 | e6b1e558 | ths | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
|
7398 | a18e524a | bellard | } |
7399 | f331110f | bellard | } |
7400 | fd1dff4b | bellard | #endif
|
7401 | fd1dff4b | bellard | /* poll any events */
|
7402 | fd1dff4b | bellard | /* XXX: separate device handlers from system ones */
|
7403 | fd1dff4b | bellard | nfds = -1;
|
7404 | fd1dff4b | bellard | FD_ZERO(&rfds); |
7405 | fd1dff4b | bellard | FD_ZERO(&wfds); |
7406 | e035649e | bellard | FD_ZERO(&xfds); |
7407 | fd1dff4b | bellard | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
7408 | cafffd40 | ths | if (ioh->deleted)
|
7409 | cafffd40 | ths | continue;
|
7410 | fd1dff4b | bellard | if (ioh->fd_read &&
|
7411 | fd1dff4b | bellard | (!ioh->fd_read_poll || |
7412 | fd1dff4b | bellard | ioh->fd_read_poll(ioh->opaque) != 0)) {
|
7413 | fd1dff4b | bellard | FD_SET(ioh->fd, &rfds); |
7414 | fd1dff4b | bellard | if (ioh->fd > nfds)
|
7415 | fd1dff4b | bellard | nfds = ioh->fd; |
7416 | fd1dff4b | bellard | } |
7417 | fd1dff4b | bellard | if (ioh->fd_write) {
|
7418 | fd1dff4b | bellard | FD_SET(ioh->fd, &wfds); |
7419 | fd1dff4b | bellard | if (ioh->fd > nfds)
|
7420 | fd1dff4b | bellard | nfds = ioh->fd; |
7421 | fd1dff4b | bellard | } |
7422 | fd1dff4b | bellard | } |
7423 | 3b46e624 | ths | |
7424 | fd1dff4b | bellard | tv.tv_sec = 0;
|
7425 | fd1dff4b | bellard | #ifdef _WIN32
|
7426 | fd1dff4b | bellard | tv.tv_usec = 0;
|
7427 | 38e205a2 | bellard | #else
|
7428 | fd1dff4b | bellard | tv.tv_usec = timeout * 1000;
|
7429 | fd1dff4b | bellard | #endif
|
7430 | e035649e | bellard | #if defined(CONFIG_SLIRP)
|
7431 | e035649e | bellard | if (slirp_inited) {
|
7432 | e035649e | bellard | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
7433 | e035649e | bellard | } |
7434 | e035649e | bellard | #endif
|
7435 | e035649e | bellard | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
|
7436 | fd1dff4b | bellard | if (ret > 0) { |
7437 | cafffd40 | ths | IOHandlerRecord **pioh; |
7438 | cafffd40 | ths | |
7439 | cafffd40 | ths | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
7440 | 6ab43fdc | ths | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
|
7441 | fd1dff4b | bellard | ioh->fd_read(ioh->opaque); |
7442 | 7c9d8e07 | bellard | } |
7443 | 6ab43fdc | ths | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
|
7444 | fd1dff4b | bellard | ioh->fd_write(ioh->opaque); |
7445 | c4b1fcc0 | bellard | } |
7446 | b4608c04 | bellard | } |
7447 | cafffd40 | ths | |
7448 | cafffd40 | ths | /* remove deleted IO handlers */
|
7449 | cafffd40 | ths | pioh = &first_io_handler; |
7450 | cafffd40 | ths | while (*pioh) {
|
7451 | cafffd40 | ths | ioh = *pioh; |
7452 | cafffd40 | ths | if (ioh->deleted) {
|
7453 | cafffd40 | ths | *pioh = ioh->next; |
7454 | cafffd40 | ths | qemu_free(ioh); |
7455 | 5fafdf24 | ths | } else
|
7456 | cafffd40 | ths | pioh = &ioh->next; |
7457 | cafffd40 | ths | } |
7458 | fd1dff4b | bellard | } |
7459 | c20709aa | bellard | #if defined(CONFIG_SLIRP)
|
7460 | fd1dff4b | bellard | if (slirp_inited) {
|
7461 | e035649e | bellard | if (ret < 0) { |
7462 | e035649e | bellard | FD_ZERO(&rfds); |
7463 | e035649e | bellard | FD_ZERO(&wfds); |
7464 | e035649e | bellard | FD_ZERO(&xfds); |
7465 | c20709aa | bellard | } |
7466 | e035649e | bellard | slirp_select_poll(&rfds, &wfds, &xfds); |
7467 | fd1dff4b | bellard | } |
7468 | c20709aa | bellard | #endif
|
7469 | 83f64091 | bellard | qemu_aio_poll(); |
7470 | c20709aa | bellard | |
7471 | fd1dff4b | bellard | if (vm_running) {
|
7472 | 21b20814 | edgar_igl | if (likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
|
7473 | 5fafdf24 | ths | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
7474 | fd1dff4b | bellard | qemu_get_clock(vm_clock)); |
7475 | fd1dff4b | bellard | /* run dma transfers, if any */
|
7476 | fd1dff4b | bellard | DMA_run(); |
7477 | fd1dff4b | bellard | } |
7478 | 423f0742 | pbrook | |
7479 | fd1dff4b | bellard | /* real time timers */
|
7480 | 5fafdf24 | ths | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
7481 | fd1dff4b | bellard | qemu_get_clock(rt_clock)); |
7482 | 423f0742 | pbrook | |
7483 | d5d08334 | balrog | if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
|
7484 | d5d08334 | balrog | alarm_timer->flags &= ~(ALARM_FLAG_EXPIRED); |
7485 | d5d08334 | balrog | qemu_rearm_alarm_timer(alarm_timer); |
7486 | d5d08334 | balrog | } |
7487 | b99dc0d1 | balrog | |
7488 | 423f0742 | pbrook | /* Check bottom-halves last in case any of the earlier events triggered
|
7489 | 423f0742 | pbrook | them. */
|
7490 | 423f0742 | pbrook | qemu_bh_poll(); |
7491 | 3b46e624 | ths | |
7492 | 5905b2e5 | bellard | } |
7493 | 5905b2e5 | bellard | |
7494 | 9596ebb7 | pbrook | static int main_loop(void) |
7495 | 5905b2e5 | bellard | { |
7496 | 5905b2e5 | bellard | int ret, timeout;
|
7497 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7498 | 89bfc105 | bellard | int64_t ti; |
7499 | 89bfc105 | bellard | #endif
|
7500 | 6a00d601 | bellard | CPUState *env; |
7501 | 5905b2e5 | bellard | |
7502 | 6a00d601 | bellard | cur_cpu = first_cpu; |
7503 | ee5605e5 | balrog | next_cpu = cur_cpu->next_cpu ?: first_cpu; |
7504 | 5905b2e5 | bellard | for(;;) {
|
7505 | 5905b2e5 | bellard | if (vm_running) {
|
7506 | 15a76449 | bellard | |
7507 | 15a76449 | bellard | for(;;) {
|
7508 | 15a76449 | bellard | /* get next cpu */
|
7509 | ee5605e5 | balrog | env = next_cpu; |
7510 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7511 | 89bfc105 | bellard | ti = profile_getclock(); |
7512 | 89bfc105 | bellard | #endif
|
7513 | 2e70f6ef | pbrook | if (use_icount) {
|
7514 | 2e70f6ef | pbrook | int64_t count; |
7515 | 2e70f6ef | pbrook | int decr;
|
7516 | 2e70f6ef | pbrook | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
7517 | 2e70f6ef | pbrook | env->icount_decr.u16.low = 0;
|
7518 | 2e70f6ef | pbrook | env->icount_extra = 0;
|
7519 | 2e70f6ef | pbrook | count = qemu_next_deadline(); |
7520 | 2e70f6ef | pbrook | count = (count + (1 << icount_time_shift) - 1) |
7521 | 2e70f6ef | pbrook | >> icount_time_shift; |
7522 | 2e70f6ef | pbrook | qemu_icount += count; |
7523 | 2e70f6ef | pbrook | decr = (count > 0xffff) ? 0xffff : count; |
7524 | 2e70f6ef | pbrook | count -= decr; |
7525 | 2e70f6ef | pbrook | env->icount_decr.u16.low = decr; |
7526 | 2e70f6ef | pbrook | env->icount_extra = count; |
7527 | 2e70f6ef | pbrook | } |
7528 | 6a00d601 | bellard | ret = cpu_exec(env); |
7529 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7530 | 89bfc105 | bellard | qemu_time += profile_getclock() - ti; |
7531 | 89bfc105 | bellard | #endif
|
7532 | 2e70f6ef | pbrook | if (use_icount) {
|
7533 | 2e70f6ef | pbrook | /* Fold pending instructions back into the
|
7534 | 2e70f6ef | pbrook | instruction counter, and clear the interrupt flag. */
|
7535 | 2e70f6ef | pbrook | qemu_icount -= (env->icount_decr.u16.low |
7536 | 2e70f6ef | pbrook | + env->icount_extra); |
7537 | 2e70f6ef | pbrook | env->icount_decr.u32 = 0;
|
7538 | 2e70f6ef | pbrook | env->icount_extra = 0;
|
7539 | 2e70f6ef | pbrook | } |
7540 | ee5605e5 | balrog | next_cpu = env->next_cpu ?: first_cpu; |
7541 | 95b01009 | aurel32 | if (event_pending && likely(ret != EXCP_DEBUG)) {
|
7542 | ee5605e5 | balrog | ret = EXCP_INTERRUPT; |
7543 | ee5605e5 | balrog | event_pending = 0;
|
7544 | ee5605e5 | balrog | break;
|
7545 | ee5605e5 | balrog | } |
7546 | bd967e05 | pbrook | if (ret == EXCP_HLT) {
|
7547 | bd967e05 | pbrook | /* Give the next CPU a chance to run. */
|
7548 | bd967e05 | pbrook | cur_cpu = env; |
7549 | bd967e05 | pbrook | continue;
|
7550 | bd967e05 | pbrook | } |
7551 | 15a76449 | bellard | if (ret != EXCP_HALTED)
|
7552 | 15a76449 | bellard | break;
|
7553 | 15a76449 | bellard | /* all CPUs are halted ? */
|
7554 | bd967e05 | pbrook | if (env == cur_cpu)
|
7555 | 15a76449 | bellard | break;
|
7556 | 15a76449 | bellard | } |
7557 | 15a76449 | bellard | cur_cpu = env; |
7558 | 15a76449 | bellard | |
7559 | 5905b2e5 | bellard | if (shutdown_requested) {
|
7560 | 3475187d | bellard | ret = EXCP_INTERRUPT; |
7561 | b2f76161 | aurel32 | if (no_shutdown) {
|
7562 | b2f76161 | aurel32 | vm_stop(0);
|
7563 | b2f76161 | aurel32 | no_shutdown = 0;
|
7564 | b2f76161 | aurel32 | } |
7565 | b2f76161 | aurel32 | else
|
7566 | b2f76161 | aurel32 | break;
|
7567 | 5905b2e5 | bellard | } |
7568 | 5905b2e5 | bellard | if (reset_requested) {
|
7569 | 5905b2e5 | bellard | reset_requested = 0;
|
7570 | 5905b2e5 | bellard | qemu_system_reset(); |
7571 | 3475187d | bellard | ret = EXCP_INTERRUPT; |
7572 | 3475187d | bellard | } |
7573 | 3475187d | bellard | if (powerdown_requested) {
|
7574 | 3475187d | bellard | powerdown_requested = 0;
|
7575 | 3475187d | bellard | qemu_system_powerdown(); |
7576 | 3475187d | bellard | ret = EXCP_INTERRUPT; |
7577 | 5905b2e5 | bellard | } |
7578 | 95b01009 | aurel32 | if (unlikely(ret == EXCP_DEBUG)) {
|
7579 | 5905b2e5 | bellard | vm_stop(EXCP_DEBUG); |
7580 | 5905b2e5 | bellard | } |
7581 | bd967e05 | pbrook | /* If all cpus are halted then wait until the next IRQ */
|
7582 | 5905b2e5 | bellard | /* XXX: use timeout computed from timers */
|
7583 | 2e70f6ef | pbrook | if (ret == EXCP_HALTED) {
|
7584 | 2e70f6ef | pbrook | if (use_icount) {
|
7585 | 2e70f6ef | pbrook | int64_t add; |
7586 | 2e70f6ef | pbrook | int64_t delta; |
7587 | 2e70f6ef | pbrook | /* Advance virtual time to the next event. */
|
7588 | 2e70f6ef | pbrook | if (use_icount == 1) { |
7589 | 2e70f6ef | pbrook | /* When not using an adaptive execution frequency
|
7590 | 2e70f6ef | pbrook | we tend to get badly out of sync with real time,
|
7591 | bf20dc07 | ths | so just delay for a reasonable amount of time. */
|
7592 | 2e70f6ef | pbrook | delta = 0;
|
7593 | 2e70f6ef | pbrook | } else {
|
7594 | 2e70f6ef | pbrook | delta = cpu_get_icount() - cpu_get_clock(); |
7595 | 2e70f6ef | pbrook | } |
7596 | 2e70f6ef | pbrook | if (delta > 0) { |
7597 | 2e70f6ef | pbrook | /* If virtual time is ahead of real time then just
|
7598 | 2e70f6ef | pbrook | wait for IO. */
|
7599 | 2e70f6ef | pbrook | timeout = (delta / 1000000) + 1; |
7600 | 2e70f6ef | pbrook | } else {
|
7601 | 2e70f6ef | pbrook | /* Wait for either IO to occur or the next
|
7602 | 2e70f6ef | pbrook | timer event. */
|
7603 | 2e70f6ef | pbrook | add = qemu_next_deadline(); |
7604 | 2e70f6ef | pbrook | /* We advance the timer before checking for IO.
|
7605 | 2e70f6ef | pbrook | Limit the amount we advance so that early IO
|
7606 | 2e70f6ef | pbrook | activity won't get the guest too far ahead. */
|
7607 | 2e70f6ef | pbrook | if (add > 10000000) |
7608 | 2e70f6ef | pbrook | add = 10000000;
|
7609 | 2e70f6ef | pbrook | delta += add; |
7610 | 2e70f6ef | pbrook | add = (add + (1 << icount_time_shift) - 1) |
7611 | 2e70f6ef | pbrook | >> icount_time_shift; |
7612 | 2e70f6ef | pbrook | qemu_icount += add; |
7613 | 2e70f6ef | pbrook | timeout = delta / 1000000;
|
7614 | 2e70f6ef | pbrook | if (timeout < 0) |
7615 | 2e70f6ef | pbrook | timeout = 0;
|
7616 | 2e70f6ef | pbrook | } |
7617 | 2e70f6ef | pbrook | } else {
|
7618 | 2e70f6ef | pbrook | timeout = 10;
|
7619 | 2e70f6ef | pbrook | } |
7620 | 2e70f6ef | pbrook | } else {
|
7621 | 5905b2e5 | bellard | timeout = 0;
|
7622 | 2e70f6ef | pbrook | } |
7623 | 5905b2e5 | bellard | } else {
|
7624 | 5b08fc10 | aliguori | if (shutdown_requested)
|
7625 | 5b08fc10 | aliguori | break;
|
7626 | 5905b2e5 | bellard | timeout = 10;
|
7627 | 5905b2e5 | bellard | } |
7628 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7629 | 89bfc105 | bellard | ti = profile_getclock(); |
7630 | 89bfc105 | bellard | #endif
|
7631 | 5905b2e5 | bellard | main_loop_wait(timeout); |
7632 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7633 | 89bfc105 | bellard | dev_time += profile_getclock() - ti; |
7634 | 89bfc105 | bellard | #endif
|
7635 | b4608c04 | bellard | } |
7636 | 34865134 | bellard | cpu_disable_ticks(); |
7637 | 34865134 | bellard | return ret;
|
7638 | b4608c04 | bellard | } |
7639 | b4608c04 | bellard | |
7640 | 15f82208 | ths | static void help(int exitcode) |
7641 | 0824d6fc | bellard | { |
7642 | 68d0f70e | bellard | printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n" |
7643 | 0db63474 | bellard | "usage: %s [options] [disk_image]\n"
|
7644 | 0824d6fc | bellard | "\n"
|
7645 | a20dd508 | bellard | "'disk_image' is a raw hard image image for IDE hard disk 0\n"
|
7646 | fc01f7e7 | bellard | "\n"
|
7647 | a20dd508 | bellard | "Standard options:\n"
|
7648 | cc1daa40 | bellard | "-M machine select emulated machine (-M ? for list)\n"
|
7649 | 5adb4839 | pbrook | "-cpu cpu select CPU (-cpu ? for list)\n"
|
7650 | c45886db | bellard | "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
|
7651 | 36b486bb | bellard | "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
|
7652 | 36b486bb | bellard | "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
|
7653 | c4b1fcc0 | bellard | "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
|
7654 | a1620fac | aurel32 | "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
|
7655 | a1620fac | aurel32 | " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
|
7656 | a1620fac | aurel32 | " [,cache=on|off][,format=f]\n"
|
7657 | e4bcb14c | ths | " use 'file' as a drive image\n"
|
7658 | 3e3d5815 | balrog | "-mtdblock file use 'file' as on-board Flash memory image\n"
|
7659 | a1bb27b1 | pbrook | "-sd file use 'file' as SecureDigital card image\n"
|
7660 | 86f55663 | j_mayer | "-pflash file use 'file' as a parallel flash image\n"
|
7661 | eec85c2a | ths | "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
|
7662 | 667accab | ths | "-snapshot write to temporary files instead of disk image files\n"
|
7663 | 667accab | ths | #ifdef CONFIG_SDL
|
7664 | 43523e93 | ths | "-no-frame open SDL window without a frame and window decorations\n"
|
7665 | 3780e197 | ths | "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
|
7666 | 667accab | ths | "-no-quit disable SDL window close capability\n"
|
7667 | 667accab | ths | #endif
|
7668 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7669 | 52ca8d6a | bellard | "-no-fd-bootchk disable boot signature checking for floppy disks\n"
|
7670 | 52ca8d6a | bellard | #endif
|
7671 | a00bad7e | bellard | "-m megs set virtual RAM size to megs MB [default=%d]\n"
|
7672 | 91fc2119 | bellard | "-smp n set the number of CPUs to 'n' [default=1]\n"
|
7673 | c4b1fcc0 | bellard | "-nographic disable graphical output and redirect serial I/Os to console\n"
|
7674 | a171fe39 | balrog | "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
|
7675 | 4ca0074c | bellard | #ifndef _WIN32
|
7676 | 667accab | ths | "-k language use keyboard layout (for example \"fr\" for French)\n"
|
7677 | 4ca0074c | bellard | #endif
|
7678 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7679 | 1d14ffa9 | bellard | "-audio-help print list of audio drivers and their options\n"
|
7680 | c0fe3827 | bellard | "-soundhw c1,... enable audio support\n"
|
7681 | c0fe3827 | bellard | " and only specified sound cards (comma separated list)\n"
|
7682 | c0fe3827 | bellard | " use -soundhw ? to get the list of supported cards\n"
|
7683 | 6a36d84e | bellard | " use -soundhw all to enable all of them\n"
|
7684 | 1d14ffa9 | bellard | #endif
|
7685 | 89980284 | bellard | "-localtime set the real time clock to local time [default=utc]\n"
|
7686 | d63d307f | bellard | "-full-screen start in full screen\n"
|
7687 | a09db21f | bellard | #ifdef TARGET_I386
|
7688 | a09db21f | bellard | "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
|
7689 | a09db21f | bellard | #endif
|
7690 | b389dbfb | bellard | "-usb enable the USB driver (will be the default soon)\n"
|
7691 | b389dbfb | bellard | "-usbdevice name add the host or guest USB device 'name'\n"
|
7692 | 6f7e9aec | bellard | #if defined(TARGET_PPC) || defined(TARGET_SPARC)
|
7693 | 6f7e9aec | bellard | "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
|
7694 | bb0c6722 | bellard | #endif
|
7695 | c35734b2 | ths | "-name string set the name of the guest\n"
|
7696 | c4b1fcc0 | bellard | "\n"
|
7697 | c4b1fcc0 | bellard | "Network options:\n"
|
7698 | a41b2ff2 | pbrook | "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
|
7699 | 7c9d8e07 | bellard | " create a new Network Interface Card and connect it to VLAN 'n'\n"
|
7700 | c20709aa | bellard | #ifdef CONFIG_SLIRP
|
7701 | 115defd1 | pbrook | "-net user[,vlan=n][,hostname=host]\n"
|
7702 | 115defd1 | pbrook | " connect the user mode network stack to VLAN 'n' and send\n"
|
7703 | 115defd1 | pbrook | " hostname 'host' to DHCP clients\n"
|
7704 | 7c9d8e07 | bellard | #endif
|
7705 | 7fb843f8 | bellard | #ifdef _WIN32
|
7706 | 7fb843f8 | bellard | "-net tap[,vlan=n],ifname=name\n"
|
7707 | 7fb843f8 | bellard | " connect the host TAP network interface to VLAN 'n'\n"
|
7708 | 7fb843f8 | bellard | #else
|
7709 | b46a8906 | ths | "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
|
7710 | b46a8906 | ths | " connect the host TAP network interface to VLAN 'n' and use the\n"
|
7711 | b46a8906 | ths | " network scripts 'file' (default=%s)\n"
|
7712 | b46a8906 | ths | " and 'dfile' (default=%s);\n"
|
7713 | b46a8906 | ths | " use '[down]script=no' to disable script execution;\n"
|
7714 | 7c9d8e07 | bellard | " use 'fd=h' to connect to an already opened TAP interface\n"
|
7715 | 7fb843f8 | bellard | #endif
|
7716 | 6a00d601 | bellard | "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
|
7717 | 7c9d8e07 | bellard | " connect the vlan 'n' to another VLAN using a socket connection\n"
|
7718 | 3d830459 | bellard | "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
|
7719 | 3d830459 | bellard | " connect the vlan 'n' to multicast maddr and port\n"
|
7720 | 8a16d273 | ths | #ifdef CONFIG_VDE
|
7721 | 8a16d273 | ths | "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
|
7722 | 8a16d273 | ths | " connect the vlan 'n' to port 'n' of a vde switch running\n"
|
7723 | 8a16d273 | ths | " on host and listening for incoming connections on 'socketpath'.\n"
|
7724 | 8a16d273 | ths | " Use group 'groupname' and mode 'octalmode' to change default\n"
|
7725 | 8a16d273 | ths | " ownership and permissions for communication port.\n"
|
7726 | 8a16d273 | ths | #endif
|
7727 | 7c9d8e07 | bellard | "-net none use it alone to have zero network devices; if no -net option\n"
|
7728 | 7c9d8e07 | bellard | " is provided, the default is '-net nic -net user'\n"
|
7729 | 7c9d8e07 | bellard | "\n"
|
7730 | 7c9d8e07 | bellard | #ifdef CONFIG_SLIRP
|
7731 | 0db1137d | ths | "-tftp dir allow tftp access to files in dir [-net user]\n"
|
7732 | 47d5d01a | ths | "-bootp file advertise file in BOOTP replies\n"
|
7733 | 7c9d8e07 | bellard | #ifndef _WIN32
|
7734 | 7c9d8e07 | bellard | "-smb dir allow SMB access to files in 'dir' [-net user]\n"
|
7735 | c94c8d64 | bellard | #endif
|
7736 | 9bf05444 | bellard | "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
|
7737 | 7c9d8e07 | bellard | " redirect TCP or UDP connections from host to guest [-net user]\n"
|
7738 | c20709aa | bellard | #endif
|
7739 | a20dd508 | bellard | "\n"
|
7740 | c4b1fcc0 | bellard | "Linux boot specific:\n"
|
7741 | a20dd508 | bellard | "-kernel bzImage use 'bzImage' as kernel image\n"
|
7742 | a20dd508 | bellard | "-append cmdline use 'cmdline' as kernel command line\n"
|
7743 | a20dd508 | bellard | "-initrd file use 'file' as initial ram disk\n"
|
7744 | fc01f7e7 | bellard | "\n"
|
7745 | 330d0414 | bellard | "Debug/Expert options:\n"
|
7746 | 82c643ff | bellard | "-monitor dev redirect the monitor to char device 'dev'\n"
|
7747 | 82c643ff | bellard | "-serial dev redirect the serial port to char device 'dev'\n"
|
7748 | 6508fe59 | bellard | "-parallel dev redirect the parallel port to char device 'dev'\n"
|
7749 | f7cce898 | bellard | "-pidfile file Write PID to 'file'\n"
|
7750 | cd6f1169 | bellard | "-S freeze CPU at startup (use 'c' to start execution)\n"
|
7751 | cfc3475a | pbrook | "-s wait gdb connection to port\n"
|
7752 | cfc3475a | pbrook | "-p port set gdb connection port [default=%s]\n"
|
7753 | f193c797 | bellard | "-d item1,... output log to %s (use -d ? for a list of log items)\n"
|
7754 | 46d4767d | bellard | "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
|
7755 | 46d4767d | bellard | " translation (t=none or lba) (usually qemu can guess them)\n"
|
7756 | 87b47350 | bellard | "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
|
7757 | d993e026 | bellard | #ifdef USE_KQEMU
|
7758 | 6515b203 | bellard | "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
|
7759 | d993e026 | bellard | "-no-kqemu disable KQEMU kernel module usage\n"
|
7760 | d993e026 | bellard | #endif
|
7761 | bb0c6722 | bellard | #ifdef TARGET_I386
|
7762 | 1bfe856e | bellard | "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
|
7763 | 1bfe856e | bellard | " (default is CL-GD5446 PCI VGA)\n"
|
7764 | 6515b203 | bellard | "-no-acpi disable ACPI\n"
|
7765 | bb0c6722 | bellard | #endif
|
7766 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
7767 | 4d3b6f6e | balrog | "-curses use a curses/ncurses interface instead of SDL\n"
|
7768 | 4d3b6f6e | balrog | #endif
|
7769 | d1beab82 | bellard | "-no-reboot exit instead of rebooting\n"
|
7770 | b2f76161 | aurel32 | "-no-shutdown stop before shutdown\n"
|
7771 | a8080006 | aurel32 | "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
|
7772 | 24236869 | bellard | "-vnc display start a VNC server on display\n"
|
7773 | 71e3ceb8 | ths | #ifndef _WIN32
|
7774 | 71e3ceb8 | ths | "-daemonize daemonize QEMU after initializing\n"
|
7775 | 71e3ceb8 | ths | #endif
|
7776 | 9ae02555 | ths | "-option-rom rom load a file, rom, into the option ROM space\n"
|
7777 | 66508601 | blueswir1 | #ifdef TARGET_SPARC
|
7778 | 66508601 | blueswir1 | "-prom-env variable=value set OpenBIOS nvram variables\n"
|
7779 | 66508601 | blueswir1 | #endif
|
7780 | f3dcfada | ths | "-clock force the use of the given methods for timer alarm.\n"
|
7781 | 3adda04c | aurel32 | " To see what timers are available use -clock ?\n"
|
7782 | bce61846 | bellard | "-startdate select initial date of the clock\n"
|
7783 | 2e70f6ef | pbrook | "-icount [N|auto]\n"
|
7784 | dd5d6fe9 | pbrook | " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
|
7785 | 0824d6fc | bellard | "\n"
|
7786 | 82c643ff | bellard | "During emulation, the following keys are useful:\n"
|
7787 | 032a8c9e | bellard | "ctrl-alt-f toggle full screen\n"
|
7788 | 032a8c9e | bellard | "ctrl-alt-n switch to virtual console 'n'\n"
|
7789 | 032a8c9e | bellard | "ctrl-alt toggle mouse and keyboard grab\n"
|
7790 | 82c643ff | bellard | "\n"
|
7791 | 82c643ff | bellard | "When using -nographic, press 'ctrl-a h' to get some help.\n"
|
7792 | 82c643ff | bellard | , |
7793 | 0db63474 | bellard | "qemu",
|
7794 | a00bad7e | bellard | DEFAULT_RAM_SIZE, |
7795 | 7c9d8e07 | bellard | #ifndef _WIN32
|
7796 | a00bad7e | bellard | DEFAULT_NETWORK_SCRIPT, |
7797 | b46a8906 | ths | DEFAULT_NETWORK_DOWN_SCRIPT, |
7798 | 7c9d8e07 | bellard | #endif
|
7799 | 6e44ba7f | bellard | DEFAULT_GDBSTUB_PORT, |
7800 | bce61846 | bellard | "/tmp/qemu.log");
|
7801 | 15f82208 | ths | exit(exitcode); |
7802 | 0824d6fc | bellard | } |
7803 | 0824d6fc | bellard | |
7804 | cd6f1169 | bellard | #define HAS_ARG 0x0001 |
7805 | cd6f1169 | bellard | |
7806 | cd6f1169 | bellard | enum {
|
7807 | cd6f1169 | bellard | QEMU_OPTION_h, |
7808 | cd6f1169 | bellard | |
7809 | cc1daa40 | bellard | QEMU_OPTION_M, |
7810 | 94fc95cd | j_mayer | QEMU_OPTION_cpu, |
7811 | cd6f1169 | bellard | QEMU_OPTION_fda, |
7812 | cd6f1169 | bellard | QEMU_OPTION_fdb, |
7813 | cd6f1169 | bellard | QEMU_OPTION_hda, |
7814 | cd6f1169 | bellard | QEMU_OPTION_hdb, |
7815 | cd6f1169 | bellard | QEMU_OPTION_hdc, |
7816 | cd6f1169 | bellard | QEMU_OPTION_hdd, |
7817 | e4bcb14c | ths | QEMU_OPTION_drive, |
7818 | cd6f1169 | bellard | QEMU_OPTION_cdrom, |
7819 | 3e3d5815 | balrog | QEMU_OPTION_mtdblock, |
7820 | a1bb27b1 | pbrook | QEMU_OPTION_sd, |
7821 | 86f55663 | j_mayer | QEMU_OPTION_pflash, |
7822 | cd6f1169 | bellard | QEMU_OPTION_boot, |
7823 | cd6f1169 | bellard | QEMU_OPTION_snapshot, |
7824 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7825 | 52ca8d6a | bellard | QEMU_OPTION_no_fd_bootchk, |
7826 | 52ca8d6a | bellard | #endif
|
7827 | cd6f1169 | bellard | QEMU_OPTION_m, |
7828 | cd6f1169 | bellard | QEMU_OPTION_nographic, |
7829 | a171fe39 | balrog | QEMU_OPTION_portrait, |
7830 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7831 | 1d14ffa9 | bellard | QEMU_OPTION_audio_help, |
7832 | 1d14ffa9 | bellard | QEMU_OPTION_soundhw, |
7833 | 1d14ffa9 | bellard | #endif
|
7834 | cd6f1169 | bellard | |
7835 | 7c9d8e07 | bellard | QEMU_OPTION_net, |
7836 | c7f74643 | bellard | QEMU_OPTION_tftp, |
7837 | 47d5d01a | ths | QEMU_OPTION_bootp, |
7838 | 9d728e8c | bellard | QEMU_OPTION_smb, |
7839 | 9bf05444 | bellard | QEMU_OPTION_redir, |
7840 | cd6f1169 | bellard | |
7841 | cd6f1169 | bellard | QEMU_OPTION_kernel, |
7842 | cd6f1169 | bellard | QEMU_OPTION_append, |
7843 | cd6f1169 | bellard | QEMU_OPTION_initrd, |
7844 | cd6f1169 | bellard | |
7845 | cd6f1169 | bellard | QEMU_OPTION_S, |
7846 | cd6f1169 | bellard | QEMU_OPTION_s, |
7847 | cd6f1169 | bellard | QEMU_OPTION_p, |
7848 | cd6f1169 | bellard | QEMU_OPTION_d, |
7849 | cd6f1169 | bellard | QEMU_OPTION_hdachs, |
7850 | cd6f1169 | bellard | QEMU_OPTION_L, |
7851 | 1192dad8 | j_mayer | QEMU_OPTION_bios, |
7852 | 3d11d0eb | bellard | QEMU_OPTION_k, |
7853 | ee22c2f7 | bellard | QEMU_OPTION_localtime, |
7854 | 1f04275e | bellard | QEMU_OPTION_cirrusvga, |
7855 | d34cab9f | ths | QEMU_OPTION_vmsvga, |
7856 | e9b137c2 | bellard | QEMU_OPTION_g, |
7857 | 1bfe856e | bellard | QEMU_OPTION_std_vga, |
7858 | 20d8a3ed | ths | QEMU_OPTION_echr, |
7859 | 82c643ff | bellard | QEMU_OPTION_monitor, |
7860 | 82c643ff | bellard | QEMU_OPTION_serial, |
7861 | 6508fe59 | bellard | QEMU_OPTION_parallel, |
7862 | d63d307f | bellard | QEMU_OPTION_loadvm, |
7863 | d63d307f | bellard | QEMU_OPTION_full_screen, |
7864 | 43523e93 | ths | QEMU_OPTION_no_frame, |
7865 | 3780e197 | ths | QEMU_OPTION_alt_grab, |
7866 | 667accab | ths | QEMU_OPTION_no_quit, |
7867 | f7cce898 | bellard | QEMU_OPTION_pidfile, |
7868 | d993e026 | bellard | QEMU_OPTION_no_kqemu, |
7869 | 89bfc105 | bellard | QEMU_OPTION_kernel_kqemu, |
7870 | a09db21f | bellard | QEMU_OPTION_win2k_hack, |
7871 | bb36d470 | bellard | QEMU_OPTION_usb, |
7872 | a594cfbf | bellard | QEMU_OPTION_usbdevice, |
7873 | 6a00d601 | bellard | QEMU_OPTION_smp, |
7874 | 24236869 | bellard | QEMU_OPTION_vnc, |
7875 | 6515b203 | bellard | QEMU_OPTION_no_acpi, |
7876 | 4d3b6f6e | balrog | QEMU_OPTION_curses, |
7877 | d1beab82 | bellard | QEMU_OPTION_no_reboot, |
7878 | b2f76161 | aurel32 | QEMU_OPTION_no_shutdown, |
7879 | 9467cd46 | balrog | QEMU_OPTION_show_cursor, |
7880 | 71e3ceb8 | ths | QEMU_OPTION_daemonize, |
7881 | 9ae02555 | ths | QEMU_OPTION_option_rom, |
7882 | c35734b2 | ths | QEMU_OPTION_semihosting, |
7883 | c35734b2 | ths | QEMU_OPTION_name, |
7884 | 66508601 | blueswir1 | QEMU_OPTION_prom_env, |
7885 | 2b8f2d41 | balrog | QEMU_OPTION_old_param, |
7886 | f3dcfada | ths | QEMU_OPTION_clock, |
7887 | 7e0af5d0 | bellard | QEMU_OPTION_startdate, |
7888 | 26a5f13b | bellard | QEMU_OPTION_tb_size, |
7889 | 2e70f6ef | pbrook | QEMU_OPTION_icount, |
7890 | cd6f1169 | bellard | }; |
7891 | cd6f1169 | bellard | |
7892 | cd6f1169 | bellard | typedef struct QEMUOption { |
7893 | cd6f1169 | bellard | const char *name; |
7894 | cd6f1169 | bellard | int flags;
|
7895 | cd6f1169 | bellard | int index;
|
7896 | cd6f1169 | bellard | } QEMUOption; |
7897 | cd6f1169 | bellard | |
7898 | cd6f1169 | bellard | const QEMUOption qemu_options[] = {
|
7899 | cd6f1169 | bellard | { "h", 0, QEMU_OPTION_h }, |
7900 | 64423fb2 | pbrook | { "help", 0, QEMU_OPTION_h }, |
7901 | cd6f1169 | bellard | |
7902 | cc1daa40 | bellard | { "M", HAS_ARG, QEMU_OPTION_M },
|
7903 | 94fc95cd | j_mayer | { "cpu", HAS_ARG, QEMU_OPTION_cpu },
|
7904 | cd6f1169 | bellard | { "fda", HAS_ARG, QEMU_OPTION_fda },
|
7905 | cd6f1169 | bellard | { "fdb", HAS_ARG, QEMU_OPTION_fdb },
|
7906 | cd6f1169 | bellard | { "hda", HAS_ARG, QEMU_OPTION_hda },
|
7907 | cd6f1169 | bellard | { "hdb", HAS_ARG, QEMU_OPTION_hdb },
|
7908 | cd6f1169 | bellard | { "hdc", HAS_ARG, QEMU_OPTION_hdc },
|
7909 | cd6f1169 | bellard | { "hdd", HAS_ARG, QEMU_OPTION_hdd },
|
7910 | e4bcb14c | ths | { "drive", HAS_ARG, QEMU_OPTION_drive },
|
7911 | cd6f1169 | bellard | { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
|
7912 | 3e3d5815 | balrog | { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
|
7913 | a1bb27b1 | pbrook | { "sd", HAS_ARG, QEMU_OPTION_sd },
|
7914 | 86f55663 | j_mayer | { "pflash", HAS_ARG, QEMU_OPTION_pflash },
|
7915 | cd6f1169 | bellard | { "boot", HAS_ARG, QEMU_OPTION_boot },
|
7916 | cd6f1169 | bellard | { "snapshot", 0, QEMU_OPTION_snapshot }, |
7917 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7918 | 52ca8d6a | bellard | { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk }, |
7919 | 52ca8d6a | bellard | #endif
|
7920 | cd6f1169 | bellard | { "m", HAS_ARG, QEMU_OPTION_m },
|
7921 | cd6f1169 | bellard | { "nographic", 0, QEMU_OPTION_nographic }, |
7922 | a171fe39 | balrog | { "portrait", 0, QEMU_OPTION_portrait }, |
7923 | 3d11d0eb | bellard | { "k", HAS_ARG, QEMU_OPTION_k },
|
7924 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7925 | 1d14ffa9 | bellard | { "audio-help", 0, QEMU_OPTION_audio_help }, |
7926 | 1d14ffa9 | bellard | { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
|
7927 | 1d14ffa9 | bellard | #endif
|
7928 | cd6f1169 | bellard | |
7929 | 7c9d8e07 | bellard | { "net", HAS_ARG, QEMU_OPTION_net},
|
7930 | 158156d1 | bellard | #ifdef CONFIG_SLIRP
|
7931 | c7f74643 | bellard | { "tftp", HAS_ARG, QEMU_OPTION_tftp },
|
7932 | 47d5d01a | ths | { "bootp", HAS_ARG, QEMU_OPTION_bootp },
|
7933 | c94c8d64 | bellard | #ifndef _WIN32
|
7934 | 9d728e8c | bellard | { "smb", HAS_ARG, QEMU_OPTION_smb },
|
7935 | c94c8d64 | bellard | #endif
|
7936 | 9bf05444 | bellard | { "redir", HAS_ARG, QEMU_OPTION_redir },
|
7937 | 158156d1 | bellard | #endif
|
7938 | cd6f1169 | bellard | |
7939 | cd6f1169 | bellard | { "kernel", HAS_ARG, QEMU_OPTION_kernel },
|
7940 | cd6f1169 | bellard | { "append", HAS_ARG, QEMU_OPTION_append },
|
7941 | cd6f1169 | bellard | { "initrd", HAS_ARG, QEMU_OPTION_initrd },
|
7942 | cd6f1169 | bellard | |
7943 | cd6f1169 | bellard | { "S", 0, QEMU_OPTION_S }, |
7944 | cd6f1169 | bellard | { "s", 0, QEMU_OPTION_s }, |
7945 | cd6f1169 | bellard | { "p", HAS_ARG, QEMU_OPTION_p },
|
7946 | cd6f1169 | bellard | { "d", HAS_ARG, QEMU_OPTION_d },
|
7947 | cd6f1169 | bellard | { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
|
7948 | cd6f1169 | bellard | { "L", HAS_ARG, QEMU_OPTION_L },
|
7949 | 1192dad8 | j_mayer | { "bios", HAS_ARG, QEMU_OPTION_bios },
|
7950 | d993e026 | bellard | #ifdef USE_KQEMU
|
7951 | d993e026 | bellard | { "no-kqemu", 0, QEMU_OPTION_no_kqemu }, |
7952 | 89bfc105 | bellard | { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu }, |
7953 | d993e026 | bellard | #endif
|
7954 | 6f7e9aec | bellard | #if defined(TARGET_PPC) || defined(TARGET_SPARC)
|
7955 | e9b137c2 | bellard | { "g", 1, QEMU_OPTION_g }, |
7956 | 77d4bc34 | bellard | #endif
|
7957 | ee22c2f7 | bellard | { "localtime", 0, QEMU_OPTION_localtime }, |
7958 | 1bfe856e | bellard | { "std-vga", 0, QEMU_OPTION_std_vga }, |
7959 | 8b6e0729 | balrog | { "echr", HAS_ARG, QEMU_OPTION_echr },
|
7960 | 8b6e0729 | balrog | { "monitor", HAS_ARG, QEMU_OPTION_monitor },
|
7961 | 8b6e0729 | balrog | { "serial", HAS_ARG, QEMU_OPTION_serial },
|
7962 | 8b6e0729 | balrog | { "parallel", HAS_ARG, QEMU_OPTION_parallel },
|
7963 | d63d307f | bellard | { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
|
7964 | d63d307f | bellard | { "full-screen", 0, QEMU_OPTION_full_screen }, |
7965 | 667accab | ths | #ifdef CONFIG_SDL
|
7966 | 43523e93 | ths | { "no-frame", 0, QEMU_OPTION_no_frame }, |
7967 | 3780e197 | ths | { "alt-grab", 0, QEMU_OPTION_alt_grab }, |
7968 | 667accab | ths | { "no-quit", 0, QEMU_OPTION_no_quit }, |
7969 | 667accab | ths | #endif
|
7970 | f7cce898 | bellard | { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
|
7971 | a09db21f | bellard | { "win2k-hack", 0, QEMU_OPTION_win2k_hack }, |
7972 | a594cfbf | bellard | { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
|
7973 | 6a00d601 | bellard | { "smp", HAS_ARG, QEMU_OPTION_smp },
|
7974 | 24236869 | bellard | { "vnc", HAS_ARG, QEMU_OPTION_vnc },
|
7975 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
7976 | 4d3b6f6e | balrog | { "curses", 0, QEMU_OPTION_curses }, |
7977 | 4d3b6f6e | balrog | #endif
|
7978 | 96d30e48 | ths | |
7979 | 1f04275e | bellard | /* temporary options */
|
7980 | a594cfbf | bellard | { "usb", 0, QEMU_OPTION_usb }, |
7981 | 1f04275e | bellard | { "cirrusvga", 0, QEMU_OPTION_cirrusvga }, |
7982 | d34cab9f | ths | { "vmwarevga", 0, QEMU_OPTION_vmsvga }, |
7983 | 6515b203 | bellard | { "no-acpi", 0, QEMU_OPTION_no_acpi }, |
7984 | d1beab82 | bellard | { "no-reboot", 0, QEMU_OPTION_no_reboot }, |
7985 | b2f76161 | aurel32 | { "no-shutdown", 0, QEMU_OPTION_no_shutdown }, |
7986 | 9467cd46 | balrog | { "show-cursor", 0, QEMU_OPTION_show_cursor }, |
7987 | 71e3ceb8 | ths | { "daemonize", 0, QEMU_OPTION_daemonize }, |
7988 | 9ae02555 | ths | { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
|
7989 | a87295e8 | pbrook | #if defined(TARGET_ARM) || defined(TARGET_M68K)
|
7990 | 8e71621f | pbrook | { "semihosting", 0, QEMU_OPTION_semihosting }, |
7991 | 8e71621f | pbrook | #endif
|
7992 | c35734b2 | ths | { "name", HAS_ARG, QEMU_OPTION_name },
|
7993 | 66508601 | blueswir1 | #if defined(TARGET_SPARC)
|
7994 | 66508601 | blueswir1 | { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
|
7995 | 66508601 | blueswir1 | #endif
|
7996 | 2b8f2d41 | balrog | #if defined(TARGET_ARM)
|
7997 | 2b8f2d41 | balrog | { "old-param", 0, QEMU_OPTION_old_param }, |
7998 | 2b8f2d41 | balrog | #endif
|
7999 | f3dcfada | ths | { "clock", HAS_ARG, QEMU_OPTION_clock },
|
8000 | 7e0af5d0 | bellard | { "startdate", HAS_ARG, QEMU_OPTION_startdate },
|
8001 | 26a5f13b | bellard | { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
|
8002 | 2e70f6ef | pbrook | { "icount", HAS_ARG, QEMU_OPTION_icount },
|
8003 | cd6f1169 | bellard | { NULL },
|
8004 | fc01f7e7 | bellard | }; |
8005 | fc01f7e7 | bellard | |
8006 | 5905b2e5 | bellard | /* password input */
|
8007 | 5905b2e5 | bellard | |
8008 | 2bac6019 | balrog | int qemu_key_check(BlockDriverState *bs, const char *name) |
8009 | 2bac6019 | balrog | { |
8010 | 2bac6019 | balrog | char password[256]; |
8011 | 2bac6019 | balrog | int i;
|
8012 | 2bac6019 | balrog | |
8013 | 2bac6019 | balrog | if (!bdrv_is_encrypted(bs))
|
8014 | 2bac6019 | balrog | return 0; |
8015 | 2bac6019 | balrog | |
8016 | 2bac6019 | balrog | term_printf("%s is encrypted.\n", name);
|
8017 | 2bac6019 | balrog | for(i = 0; i < 3; i++) { |
8018 | 2bac6019 | balrog | monitor_readline("Password: ", 1, password, sizeof(password)); |
8019 | 2bac6019 | balrog | if (bdrv_set_key(bs, password) == 0) |
8020 | 2bac6019 | balrog | return 0; |
8021 | 2bac6019 | balrog | term_printf("invalid password\n");
|
8022 | 2bac6019 | balrog | } |
8023 | 2bac6019 | balrog | return -EPERM;
|
8024 | 2bac6019 | balrog | } |
8025 | 2bac6019 | balrog | |
8026 | 83ab7950 | aliguori | static BlockDriverState *get_bdrv(int index) |
8027 | 83ab7950 | aliguori | { |
8028 | 83ab7950 | aliguori | if (index > nb_drives)
|
8029 | 83ab7950 | aliguori | return NULL; |
8030 | 83ab7950 | aliguori | return drives_table[index].bdrv;
|
8031 | 83ab7950 | aliguori | } |
8032 | 83ab7950 | aliguori | |
8033 | 5905b2e5 | bellard | static void read_passwords(void) |
8034 | 5905b2e5 | bellard | { |
8035 | 5905b2e5 | bellard | BlockDriverState *bs; |
8036 | 2bac6019 | balrog | int i;
|
8037 | 5905b2e5 | bellard | |
8038 | 83ab7950 | aliguori | for(i = 0; i < 6; i++) { |
8039 | 83ab7950 | aliguori | bs = get_bdrv(i); |
8040 | 83ab7950 | aliguori | if (bs)
|
8041 | 83ab7950 | aliguori | qemu_key_check(bs, bdrv_get_device_name(bs)); |
8042 | 5905b2e5 | bellard | } |
8043 | 5905b2e5 | bellard | } |
8044 | 5905b2e5 | bellard | |
8045 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
8046 | 6a36d84e | bellard | struct soundhw soundhw[] = {
|
8047 | b00052e4 | balrog | #ifdef HAS_AUDIO_CHOICE
|
8048 | 4ce7ff6e | aurel32 | #if defined(TARGET_I386) || defined(TARGET_MIPS)
|
8049 | fd06c375 | bellard | { |
8050 | fd06c375 | bellard | "pcspk",
|
8051 | fd06c375 | bellard | "PC speaker",
|
8052 | fd06c375 | bellard | 0,
|
8053 | fd06c375 | bellard | 1,
|
8054 | fd06c375 | bellard | { .init_isa = pcspk_audio_init } |
8055 | fd06c375 | bellard | }, |
8056 | fd06c375 | bellard | #endif
|
8057 | 6a36d84e | bellard | { |
8058 | 6a36d84e | bellard | "sb16",
|
8059 | 6a36d84e | bellard | "Creative Sound Blaster 16",
|
8060 | 6a36d84e | bellard | 0,
|
8061 | 6a36d84e | bellard | 1,
|
8062 | 6a36d84e | bellard | { .init_isa = SB16_init } |
8063 | 6a36d84e | bellard | }, |
8064 | 6a36d84e | bellard | |
8065 | cc53d26d | malc | #ifdef CONFIG_CS4231A
|
8066 | cc53d26d | malc | { |
8067 | cc53d26d | malc | "cs4231a",
|
8068 | cc53d26d | malc | "CS4231A",
|
8069 | cc53d26d | malc | 0,
|
8070 | cc53d26d | malc | 1,
|
8071 | cc53d26d | malc | { .init_isa = cs4231a_init } |
8072 | cc53d26d | malc | }, |
8073 | cc53d26d | malc | #endif
|
8074 | cc53d26d | malc | |
8075 | 1d14ffa9 | bellard | #ifdef CONFIG_ADLIB
|
8076 | 6a36d84e | bellard | { |
8077 | 6a36d84e | bellard | "adlib",
|
8078 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
8079 | 6a36d84e | bellard | "Yamaha YMF262 (OPL3)",
|
8080 | 1d14ffa9 | bellard | #else
|
8081 | 6a36d84e | bellard | "Yamaha YM3812 (OPL2)",
|
8082 | 1d14ffa9 | bellard | #endif
|
8083 | 6a36d84e | bellard | 0,
|
8084 | 6a36d84e | bellard | 1,
|
8085 | 6a36d84e | bellard | { .init_isa = Adlib_init } |
8086 | 6a36d84e | bellard | }, |
8087 | 1d14ffa9 | bellard | #endif
|
8088 | 6a36d84e | bellard | |
8089 | 1d14ffa9 | bellard | #ifdef CONFIG_GUS
|
8090 | 6a36d84e | bellard | { |
8091 | 6a36d84e | bellard | "gus",
|
8092 | 6a36d84e | bellard | "Gravis Ultrasound GF1",
|
8093 | 6a36d84e | bellard | 0,
|
8094 | 6a36d84e | bellard | 1,
|
8095 | 6a36d84e | bellard | { .init_isa = GUS_init } |
8096 | 6a36d84e | bellard | }, |
8097 | 1d14ffa9 | bellard | #endif
|
8098 | 6a36d84e | bellard | |
8099 | e5c9a13e | balrog | #ifdef CONFIG_AC97
|
8100 | e5c9a13e | balrog | { |
8101 | e5c9a13e | balrog | "ac97",
|
8102 | e5c9a13e | balrog | "Intel 82801AA AC97 Audio",
|
8103 | e5c9a13e | balrog | 0,
|
8104 | e5c9a13e | balrog | 0,
|
8105 | e5c9a13e | balrog | { .init_pci = ac97_init } |
8106 | e5c9a13e | balrog | }, |
8107 | e5c9a13e | balrog | #endif
|
8108 | e5c9a13e | balrog | |
8109 | 6a36d84e | bellard | { |
8110 | 6a36d84e | bellard | "es1370",
|
8111 | 6a36d84e | bellard | "ENSONIQ AudioPCI ES1370",
|
8112 | 6a36d84e | bellard | 0,
|
8113 | 6a36d84e | bellard | 0,
|
8114 | 6a36d84e | bellard | { .init_pci = es1370_init } |
8115 | 6a36d84e | bellard | }, |
8116 | b00052e4 | balrog | #endif
|
8117 | 6a36d84e | bellard | |
8118 | 6a36d84e | bellard | { NULL, NULL, 0, 0, { NULL } } |
8119 | 6a36d84e | bellard | }; |
8120 | 6a36d84e | bellard | |
8121 | 6a36d84e | bellard | static void select_soundhw (const char *optarg) |
8122 | 6a36d84e | bellard | { |
8123 | 6a36d84e | bellard | struct soundhw *c;
|
8124 | 6a36d84e | bellard | |
8125 | 6a36d84e | bellard | if (*optarg == '?') { |
8126 | 6a36d84e | bellard | show_valid_cards:
|
8127 | 6a36d84e | bellard | |
8128 | 6a36d84e | bellard | printf ("Valid sound card names (comma separated):\n");
|
8129 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
8130 | 6a36d84e | bellard | printf ("%-11s %s\n", c->name, c->descr);
|
8131 | 6a36d84e | bellard | } |
8132 | 6a36d84e | bellard | printf ("\n-soundhw all will enable all of the above\n");
|
8133 | 1d14ffa9 | bellard | exit (*optarg != '?');
|
8134 | 1d14ffa9 | bellard | } |
8135 | 1d14ffa9 | bellard | else {
|
8136 | 6a36d84e | bellard | size_t l; |
8137 | 1d14ffa9 | bellard | const char *p; |
8138 | 1d14ffa9 | bellard | char *e;
|
8139 | 1d14ffa9 | bellard | int bad_card = 0; |
8140 | 1d14ffa9 | bellard | |
8141 | 6a36d84e | bellard | if (!strcmp (optarg, "all")) { |
8142 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
8143 | 6a36d84e | bellard | c->enabled = 1;
|
8144 | 6a36d84e | bellard | } |
8145 | 6a36d84e | bellard | return;
|
8146 | 6a36d84e | bellard | } |
8147 | 1d14ffa9 | bellard | |
8148 | 6a36d84e | bellard | p = optarg; |
8149 | 1d14ffa9 | bellard | while (*p) {
|
8150 | 1d14ffa9 | bellard | e = strchr (p, ',');
|
8151 | 1d14ffa9 | bellard | l = !e ? strlen (p) : (size_t) (e - p); |
8152 | 6a36d84e | bellard | |
8153 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
8154 | 6a36d84e | bellard | if (!strncmp (c->name, p, l)) {
|
8155 | 6a36d84e | bellard | c->enabled = 1;
|
8156 | 1d14ffa9 | bellard | break;
|
8157 | 1d14ffa9 | bellard | } |
8158 | 1d14ffa9 | bellard | } |
8159 | 6a36d84e | bellard | |
8160 | 6a36d84e | bellard | if (!c->name) {
|
8161 | 1d14ffa9 | bellard | if (l > 80) { |
8162 | 1d14ffa9 | bellard | fprintf (stderr, |
8163 | 1d14ffa9 | bellard | "Unknown sound card name (too big to show)\n");
|
8164 | 1d14ffa9 | bellard | } |
8165 | 1d14ffa9 | bellard | else {
|
8166 | 1d14ffa9 | bellard | fprintf (stderr, "Unknown sound card name `%.*s'\n",
|
8167 | 1d14ffa9 | bellard | (int) l, p);
|
8168 | 1d14ffa9 | bellard | } |
8169 | 1d14ffa9 | bellard | bad_card = 1;
|
8170 | 1d14ffa9 | bellard | } |
8171 | 1d14ffa9 | bellard | p += l + (e != NULL);
|
8172 | 1d14ffa9 | bellard | } |
8173 | 1d14ffa9 | bellard | |
8174 | 1d14ffa9 | bellard | if (bad_card)
|
8175 | 1d14ffa9 | bellard | goto show_valid_cards;
|
8176 | 1d14ffa9 | bellard | } |
8177 | 1d14ffa9 | bellard | } |
8178 | 1d14ffa9 | bellard | #endif
|
8179 | 1d14ffa9 | bellard | |
8180 | 3587d7e6 | bellard | #ifdef _WIN32
|
8181 | 3587d7e6 | bellard | static BOOL WINAPI qemu_ctrl_handler(DWORD type)
|
8182 | 3587d7e6 | bellard | { |
8183 | 3587d7e6 | bellard | exit(STATUS_CONTROL_C_EXIT); |
8184 | 3587d7e6 | bellard | return TRUE;
|
8185 | 3587d7e6 | bellard | } |
8186 | 3587d7e6 | bellard | #endif
|
8187 | 3587d7e6 | bellard | |
8188 | 7c9d8e07 | bellard | #define MAX_NET_CLIENTS 32 |
8189 | c20709aa | bellard | |
8190 | 5b08fc10 | aliguori | #ifndef _WIN32
|
8191 | 5b08fc10 | aliguori | |
8192 | 5b08fc10 | aliguori | static void termsig_handler(int signal) |
8193 | 5b08fc10 | aliguori | { |
8194 | 5b08fc10 | aliguori | qemu_system_shutdown_request(); |
8195 | 5b08fc10 | aliguori | } |
8196 | 5b08fc10 | aliguori | |
8197 | 5b08fc10 | aliguori | void termsig_setup(void) |
8198 | 5b08fc10 | aliguori | { |
8199 | 5b08fc10 | aliguori | struct sigaction act;
|
8200 | 5b08fc10 | aliguori | |
8201 | 5b08fc10 | aliguori | memset(&act, 0, sizeof(act)); |
8202 | 5b08fc10 | aliguori | act.sa_handler = termsig_handler; |
8203 | 5b08fc10 | aliguori | sigaction(SIGINT, &act, NULL);
|
8204 | 5b08fc10 | aliguori | sigaction(SIGHUP, &act, NULL);
|
8205 | 5b08fc10 | aliguori | sigaction(SIGTERM, &act, NULL);
|
8206 | 5b08fc10 | aliguori | } |
8207 | 5b08fc10 | aliguori | |
8208 | 5b08fc10 | aliguori | #endif
|
8209 | 5b08fc10 | aliguori | |
8210 | 0824d6fc | bellard | int main(int argc, char **argv) |
8211 | 0824d6fc | bellard | { |
8212 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
8213 | cfc3475a | pbrook | int use_gdbstub;
|
8214 | cfc3475a | pbrook | const char *gdbstub_port; |
8215 | 67b915a5 | bellard | #endif
|
8216 | 28c5af54 | j_mayer | uint32_t boot_devices_bitmap = 0;
|
8217 | e4bcb14c | ths | int i;
|
8218 | 28c5af54 | j_mayer | int snapshot, linux_boot, net_boot;
|
8219 | 7f7f9873 | bellard | const char *initrd_filename; |
8220 | a20dd508 | bellard | const char *kernel_filename, *kernel_cmdline; |
8221 | 28c5af54 | j_mayer | const char *boot_devices = ""; |
8222 | 313aa567 | bellard | DisplayState *ds = &display_state; |
8223 | 46d4767d | bellard | int cyls, heads, secs, translation;
|
8224 | fd5f393a | pbrook | const char *net_clients[MAX_NET_CLIENTS]; |
8225 | 7c9d8e07 | bellard | int nb_net_clients;
|
8226 | e4bcb14c | ths | int hda_index;
|
8227 | cd6f1169 | bellard | int optind;
|
8228 | cd6f1169 | bellard | const char *r, *optarg; |
8229 | 82c643ff | bellard | CharDriverState *monitor_hd; |
8230 | fd5f393a | pbrook | const char *monitor_device; |
8231 | fd5f393a | pbrook | const char *serial_devices[MAX_SERIAL_PORTS]; |
8232 | 8d11df9e | bellard | int serial_device_index;
|
8233 | fd5f393a | pbrook | const char *parallel_devices[MAX_PARALLEL_PORTS]; |
8234 | 6508fe59 | bellard | int parallel_device_index;
|
8235 | d63d307f | bellard | const char *loadvm = NULL; |
8236 | cc1daa40 | bellard | QEMUMachine *machine; |
8237 | 94fc95cd | j_mayer | const char *cpu_model; |
8238 | fd5f393a | pbrook | const char *usb_devices[MAX_USB_CMDLINE]; |
8239 | a594cfbf | bellard | int usb_devices_index;
|
8240 | 71e3ceb8 | ths | int fds[2]; |
8241 | 26a5f13b | bellard | int tb_size;
|
8242 | 93815bc2 | ths | const char *pid_file = NULL; |
8243 | 833c7174 | blueswir1 | VLANState *vlan; |
8244 | 0bd48850 | bellard | |
8245 | 0bd48850 | bellard | LIST_INIT (&vm_change_state_head); |
8246 | be995c27 | bellard | #ifndef _WIN32
|
8247 | be995c27 | bellard | { |
8248 | be995c27 | bellard | struct sigaction act;
|
8249 | be995c27 | bellard | sigfillset(&act.sa_mask); |
8250 | be995c27 | bellard | act.sa_flags = 0;
|
8251 | be995c27 | bellard | act.sa_handler = SIG_IGN; |
8252 | be995c27 | bellard | sigaction(SIGPIPE, &act, NULL);
|
8253 | be995c27 | bellard | } |
8254 | 3587d7e6 | bellard | #else
|
8255 | 3587d7e6 | bellard | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
8256 | a8e5ac33 | bellard | /* Note: cpu_interrupt() is currently not SMP safe, so we force
|
8257 | a8e5ac33 | bellard | QEMU to run on a single CPU */
|
8258 | a8e5ac33 | bellard | { |
8259 | a8e5ac33 | bellard | HANDLE h; |
8260 | a8e5ac33 | bellard | DWORD mask, smask; |
8261 | a8e5ac33 | bellard | int i;
|
8262 | a8e5ac33 | bellard | h = GetCurrentProcess(); |
8263 | a8e5ac33 | bellard | if (GetProcessAffinityMask(h, &mask, &smask)) {
|
8264 | a8e5ac33 | bellard | for(i = 0; i < 32; i++) { |
8265 | a8e5ac33 | bellard | if (mask & (1 << i)) |
8266 | a8e5ac33 | bellard | break;
|
8267 | a8e5ac33 | bellard | } |
8268 | a8e5ac33 | bellard | if (i != 32) { |
8269 | a8e5ac33 | bellard | mask = 1 << i;
|
8270 | a8e5ac33 | bellard | SetProcessAffinityMask(h, mask); |
8271 | a8e5ac33 | bellard | } |
8272 | a8e5ac33 | bellard | } |
8273 | a8e5ac33 | bellard | } |
8274 | 67b915a5 | bellard | #endif
|
8275 | be995c27 | bellard | |
8276 | cc1daa40 | bellard | register_machines(); |
8277 | cc1daa40 | bellard | machine = first_machine; |
8278 | 94fc95cd | j_mayer | cpu_model = NULL;
|
8279 | fc01f7e7 | bellard | initrd_filename = NULL;
|
8280 | 4fc5d071 | aurel32 | ram_size = 0;
|
8281 | 313aa567 | bellard | vga_ram_size = VGA_RAM_SIZE; |
8282 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
8283 | b4608c04 | bellard | use_gdbstub = 0;
|
8284 | c636bb66 | bellard | gdbstub_port = DEFAULT_GDBSTUB_PORT; |
8285 | 67b915a5 | bellard | #endif
|
8286 | 33e3963e | bellard | snapshot = 0;
|
8287 | a20dd508 | bellard | nographic = 0;
|
8288 | 4d3b6f6e | balrog | curses = 0;
|
8289 | a20dd508 | bellard | kernel_filename = NULL;
|
8290 | a20dd508 | bellard | kernel_cmdline = "";
|
8291 | c4b1fcc0 | bellard | cyls = heads = secs = 0;
|
8292 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_AUTO; |
8293 | c60e08d9 | pbrook | monitor_device = "vc";
|
8294 | c4b1fcc0 | bellard | |
8295 | c75a823c | aurel32 | serial_devices[0] = "vc:80Cx24C"; |
8296 | 8d11df9e | bellard | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
8297 | fd5f393a | pbrook | serial_devices[i] = NULL;
|
8298 | 8d11df9e | bellard | serial_device_index = 0;
|
8299 | 3b46e624 | ths | |
8300 | c75a823c | aurel32 | parallel_devices[0] = "vc:640x480"; |
8301 | 6508fe59 | bellard | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
8302 | fd5f393a | pbrook | parallel_devices[i] = NULL;
|
8303 | 6508fe59 | bellard | parallel_device_index = 0;
|
8304 | 3b46e624 | ths | |
8305 | a594cfbf | bellard | usb_devices_index = 0;
|
8306 | 3b46e624 | ths | |
8307 | 7c9d8e07 | bellard | nb_net_clients = 0;
|
8308 | e4bcb14c | ths | nb_drives = 0;
|
8309 | e4bcb14c | ths | nb_drives_opt = 0;
|
8310 | e4bcb14c | ths | hda_index = -1;
|
8311 | 7c9d8e07 | bellard | |
8312 | 7c9d8e07 | bellard | nb_nics = 0;
|
8313 | 3b46e624 | ths | |
8314 | 26a5f13b | bellard | tb_size = 0;
|
8315 | 26a5f13b | bellard | |
8316 | cd6f1169 | bellard | optind = 1;
|
8317 | 0824d6fc | bellard | for(;;) {
|
8318 | cd6f1169 | bellard | if (optind >= argc)
|
8319 | 0824d6fc | bellard | break;
|
8320 | cd6f1169 | bellard | r = argv[optind]; |
8321 | cd6f1169 | bellard | if (r[0] != '-') { |
8322 | 609497ab | balrog | hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
|
8323 | cd6f1169 | bellard | } else {
|
8324 | cd6f1169 | bellard | const QEMUOption *popt;
|
8325 | cd6f1169 | bellard | |
8326 | cd6f1169 | bellard | optind++; |
8327 | dff5efc8 | pbrook | /* Treat --foo the same as -foo. */
|
8328 | dff5efc8 | pbrook | if (r[1] == '-') |
8329 | dff5efc8 | pbrook | r++; |
8330 | cd6f1169 | bellard | popt = qemu_options; |
8331 | cd6f1169 | bellard | for(;;) {
|
8332 | cd6f1169 | bellard | if (!popt->name) {
|
8333 | 5fafdf24 | ths | fprintf(stderr, "%s: invalid option -- '%s'\n",
|
8334 | cd6f1169 | bellard | argv[0], r);
|
8335 | cd6f1169 | bellard | exit(1);
|
8336 | cd6f1169 | bellard | } |
8337 | cd6f1169 | bellard | if (!strcmp(popt->name, r + 1)) |
8338 | cd6f1169 | bellard | break;
|
8339 | cd6f1169 | bellard | popt++; |
8340 | cd6f1169 | bellard | } |
8341 | cd6f1169 | bellard | if (popt->flags & HAS_ARG) {
|
8342 | cd6f1169 | bellard | if (optind >= argc) {
|
8343 | cd6f1169 | bellard | fprintf(stderr, "%s: option '%s' requires an argument\n",
|
8344 | cd6f1169 | bellard | argv[0], r);
|
8345 | cd6f1169 | bellard | exit(1);
|
8346 | cd6f1169 | bellard | } |
8347 | cd6f1169 | bellard | optarg = argv[optind++]; |
8348 | cd6f1169 | bellard | } else {
|
8349 | cd6f1169 | bellard | optarg = NULL;
|
8350 | cd6f1169 | bellard | } |
8351 | cd6f1169 | bellard | |
8352 | cd6f1169 | bellard | switch(popt->index) {
|
8353 | cc1daa40 | bellard | case QEMU_OPTION_M:
|
8354 | cc1daa40 | bellard | machine = find_machine(optarg); |
8355 | cc1daa40 | bellard | if (!machine) {
|
8356 | cc1daa40 | bellard | QEMUMachine *m; |
8357 | cc1daa40 | bellard | printf("Supported machines are:\n");
|
8358 | cc1daa40 | bellard | for(m = first_machine; m != NULL; m = m->next) { |
8359 | cc1daa40 | bellard | printf("%-10s %s%s\n",
|
8360 | 5fafdf24 | ths | m->name, m->desc, |
8361 | cc1daa40 | bellard | m == first_machine ? " (default)" : ""); |
8362 | cc1daa40 | bellard | } |
8363 | 15f82208 | ths | exit(*optarg != '?');
|
8364 | cc1daa40 | bellard | } |
8365 | cc1daa40 | bellard | break;
|
8366 | 94fc95cd | j_mayer | case QEMU_OPTION_cpu:
|
8367 | 94fc95cd | j_mayer | /* hw initialization will check this */
|
8368 | 15f82208 | ths | if (*optarg == '?') { |
8369 | c732abe2 | j_mayer | /* XXX: implement xxx_cpu_list for targets that still miss it */
|
8370 | c732abe2 | j_mayer | #if defined(cpu_list)
|
8371 | c732abe2 | j_mayer | cpu_list(stdout, &fprintf); |
8372 | 94fc95cd | j_mayer | #endif
|
8373 | 15f82208 | ths | exit(0);
|
8374 | 94fc95cd | j_mayer | } else {
|
8375 | 94fc95cd | j_mayer | cpu_model = optarg; |
8376 | 94fc95cd | j_mayer | } |
8377 | 94fc95cd | j_mayer | break;
|
8378 | cd6f1169 | bellard | case QEMU_OPTION_initrd:
|
8379 | fc01f7e7 | bellard | initrd_filename = optarg; |
8380 | fc01f7e7 | bellard | break;
|
8381 | cd6f1169 | bellard | case QEMU_OPTION_hda:
|
8382 | e4bcb14c | ths | if (cyls == 0) |
8383 | 609497ab | balrog | hda_index = drive_add(optarg, HD_ALIAS, 0);
|
8384 | e4bcb14c | ths | else
|
8385 | 609497ab | balrog | hda_index = drive_add(optarg, HD_ALIAS |
8386 | e4bcb14c | ths | ",cyls=%d,heads=%d,secs=%d%s",
|
8387 | 609497ab | balrog | 0, cyls, heads, secs,
|
8388 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_LBA ? |
8389 | e4bcb14c | ths | ",trans=lba" :
|
8390 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_NONE ? |
8391 | e4bcb14c | ths | ",trans=none" : ""); |
8392 | e4bcb14c | ths | break;
|
8393 | cd6f1169 | bellard | case QEMU_OPTION_hdb:
|
8394 | cc1daa40 | bellard | case QEMU_OPTION_hdc:
|
8395 | cc1daa40 | bellard | case QEMU_OPTION_hdd:
|
8396 | 609497ab | balrog | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
8397 | fc01f7e7 | bellard | break;
|
8398 | e4bcb14c | ths | case QEMU_OPTION_drive:
|
8399 | 609497ab | balrog | drive_add(NULL, "%s", optarg); |
8400 | e4bcb14c | ths | break;
|
8401 | 3e3d5815 | balrog | case QEMU_OPTION_mtdblock:
|
8402 | 609497ab | balrog | drive_add(optarg, MTD_ALIAS); |
8403 | 3e3d5815 | balrog | break;
|
8404 | a1bb27b1 | pbrook | case QEMU_OPTION_sd:
|
8405 | 609497ab | balrog | drive_add(optarg, SD_ALIAS); |
8406 | a1bb27b1 | pbrook | break;
|
8407 | 86f55663 | j_mayer | case QEMU_OPTION_pflash:
|
8408 | 609497ab | balrog | drive_add(optarg, PFLASH_ALIAS); |
8409 | 86f55663 | j_mayer | break;
|
8410 | cd6f1169 | bellard | case QEMU_OPTION_snapshot:
|
8411 | 33e3963e | bellard | snapshot = 1;
|
8412 | 33e3963e | bellard | break;
|
8413 | cd6f1169 | bellard | case QEMU_OPTION_hdachs:
|
8414 | 330d0414 | bellard | { |
8415 | 330d0414 | bellard | const char *p; |
8416 | 330d0414 | bellard | p = optarg; |
8417 | 330d0414 | bellard | cyls = strtol(p, (char **)&p, 0); |
8418 | 46d4767d | bellard | if (cyls < 1 || cyls > 16383) |
8419 | 46d4767d | bellard | goto chs_fail;
|
8420 | 330d0414 | bellard | if (*p != ',') |
8421 | 330d0414 | bellard | goto chs_fail;
|
8422 | 330d0414 | bellard | p++; |
8423 | 330d0414 | bellard | heads = strtol(p, (char **)&p, 0); |
8424 | 46d4767d | bellard | if (heads < 1 || heads > 16) |
8425 | 46d4767d | bellard | goto chs_fail;
|
8426 | 330d0414 | bellard | if (*p != ',') |
8427 | 330d0414 | bellard | goto chs_fail;
|
8428 | 330d0414 | bellard | p++; |
8429 | 330d0414 | bellard | secs = strtol(p, (char **)&p, 0); |
8430 | 46d4767d | bellard | if (secs < 1 || secs > 63) |
8431 | 46d4767d | bellard | goto chs_fail;
|
8432 | 46d4767d | bellard | if (*p == ',') { |
8433 | 46d4767d | bellard | p++; |
8434 | 46d4767d | bellard | if (!strcmp(p, "none")) |
8435 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_NONE; |
8436 | 46d4767d | bellard | else if (!strcmp(p, "lba")) |
8437 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_LBA; |
8438 | 46d4767d | bellard | else if (!strcmp(p, "auto")) |
8439 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_AUTO; |
8440 | 46d4767d | bellard | else
|
8441 | 46d4767d | bellard | goto chs_fail;
|
8442 | 46d4767d | bellard | } else if (*p != '\0') { |
8443 | c4b1fcc0 | bellard | chs_fail:
|
8444 | 46d4767d | bellard | fprintf(stderr, "qemu: invalid physical CHS format\n");
|
8445 | 46d4767d | bellard | exit(1);
|
8446 | c4b1fcc0 | bellard | } |
8447 | e4bcb14c | ths | if (hda_index != -1) |
8448 | 609497ab | balrog | snprintf(drives_opt[hda_index].opt, |
8449 | 609497ab | balrog | sizeof(drives_opt[hda_index].opt),
|
8450 | 609497ab | balrog | HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
|
8451 | 609497ab | balrog | 0, cyls, heads, secs,
|
8452 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_LBA ? |
8453 | e4bcb14c | ths | ",trans=lba" :
|
8454 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_NONE ? |
8455 | e4bcb14c | ths | ",trans=none" : ""); |
8456 | 330d0414 | bellard | } |
8457 | 330d0414 | bellard | break;
|
8458 | cd6f1169 | bellard | case QEMU_OPTION_nographic:
|
8459 | a20dd508 | bellard | nographic = 1;
|
8460 | a20dd508 | bellard | break;
|
8461 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
8462 | 4d3b6f6e | balrog | case QEMU_OPTION_curses:
|
8463 | 4d3b6f6e | balrog | curses = 1;
|
8464 | 4d3b6f6e | balrog | break;
|
8465 | 4d3b6f6e | balrog | #endif
|
8466 | a171fe39 | balrog | case QEMU_OPTION_portrait:
|
8467 | a171fe39 | balrog | graphic_rotate = 1;
|
8468 | a171fe39 | balrog | break;
|
8469 | cd6f1169 | bellard | case QEMU_OPTION_kernel:
|
8470 | a20dd508 | bellard | kernel_filename = optarg; |
8471 | a20dd508 | bellard | break;
|
8472 | cd6f1169 | bellard | case QEMU_OPTION_append:
|
8473 | a20dd508 | bellard | kernel_cmdline = optarg; |
8474 | 313aa567 | bellard | break;
|
8475 | cd6f1169 | bellard | case QEMU_OPTION_cdrom:
|
8476 | 609497ab | balrog | drive_add(optarg, CDROM_ALIAS); |
8477 | 36b486bb | bellard | break;
|
8478 | cd6f1169 | bellard | case QEMU_OPTION_boot:
|
8479 | 28c5af54 | j_mayer | boot_devices = optarg; |
8480 | 28c5af54 | j_mayer | /* We just do some generic consistency checks */
|
8481 | 28c5af54 | j_mayer | { |
8482 | 28c5af54 | j_mayer | /* Could easily be extended to 64 devices if needed */
|
8483 | 60fe76f3 | ths | const char *p; |
8484 | 28c5af54 | j_mayer | |
8485 | 28c5af54 | j_mayer | boot_devices_bitmap = 0;
|
8486 | 28c5af54 | j_mayer | for (p = boot_devices; *p != '\0'; p++) { |
8487 | 28c5af54 | j_mayer | /* Allowed boot devices are:
|
8488 | 28c5af54 | j_mayer | * a b : floppy disk drives
|
8489 | 28c5af54 | j_mayer | * c ... f : IDE disk drives
|
8490 | 28c5af54 | j_mayer | * g ... m : machine implementation dependant drives
|
8491 | 28c5af54 | j_mayer | * n ... p : network devices
|
8492 | 28c5af54 | j_mayer | * It's up to each machine implementation to check
|
8493 | 28c5af54 | j_mayer | * if the given boot devices match the actual hardware
|
8494 | 28c5af54 | j_mayer | * implementation and firmware features.
|
8495 | 28c5af54 | j_mayer | */
|
8496 | 28c5af54 | j_mayer | if (*p < 'a' || *p > 'q') { |
8497 | 28c5af54 | j_mayer | fprintf(stderr, "Invalid boot device '%c'\n", *p);
|
8498 | 28c5af54 | j_mayer | exit(1);
|
8499 | 28c5af54 | j_mayer | } |
8500 | 28c5af54 | j_mayer | if (boot_devices_bitmap & (1 << (*p - 'a'))) { |
8501 | 28c5af54 | j_mayer | fprintf(stderr, |
8502 | 28c5af54 | j_mayer | "Boot device '%c' was given twice\n",*p);
|
8503 | 28c5af54 | j_mayer | exit(1);
|
8504 | 28c5af54 | j_mayer | } |
8505 | 28c5af54 | j_mayer | boot_devices_bitmap |= 1 << (*p - 'a'); |
8506 | 28c5af54 | j_mayer | } |
8507 | 36b486bb | bellard | } |
8508 | 36b486bb | bellard | break;
|
8509 | cd6f1169 | bellard | case QEMU_OPTION_fda:
|
8510 | cd6f1169 | bellard | case QEMU_OPTION_fdb:
|
8511 | 609497ab | balrog | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
8512 | c45886db | bellard | break;
|
8513 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
8514 | 52ca8d6a | bellard | case QEMU_OPTION_no_fd_bootchk:
|
8515 | 52ca8d6a | bellard | fd_bootchk = 0;
|
8516 | 52ca8d6a | bellard | break;
|
8517 | 52ca8d6a | bellard | #endif
|
8518 | 7c9d8e07 | bellard | case QEMU_OPTION_net:
|
8519 | 7c9d8e07 | bellard | if (nb_net_clients >= MAX_NET_CLIENTS) {
|
8520 | 7c9d8e07 | bellard | fprintf(stderr, "qemu: too many network clients\n");
|
8521 | c4b1fcc0 | bellard | exit(1);
|
8522 | c4b1fcc0 | bellard | } |
8523 | fd5f393a | pbrook | net_clients[nb_net_clients] = optarg; |
8524 | 7c9d8e07 | bellard | nb_net_clients++; |
8525 | 702c651c | bellard | break;
|
8526 | c7f74643 | bellard | #ifdef CONFIG_SLIRP
|
8527 | c7f74643 | bellard | case QEMU_OPTION_tftp:
|
8528 | c7f74643 | bellard | tftp_prefix = optarg; |
8529 | 9bf05444 | bellard | break;
|
8530 | 47d5d01a | ths | case QEMU_OPTION_bootp:
|
8531 | 47d5d01a | ths | bootp_filename = optarg; |
8532 | 47d5d01a | ths | break;
|
8533 | c94c8d64 | bellard | #ifndef _WIN32
|
8534 | 9d728e8c | bellard | case QEMU_OPTION_smb:
|
8535 | 9d728e8c | bellard | net_slirp_smb(optarg); |
8536 | 9d728e8c | bellard | break;
|
8537 | c94c8d64 | bellard | #endif
|
8538 | 9bf05444 | bellard | case QEMU_OPTION_redir:
|
8539 | 3b46e624 | ths | net_slirp_redir(optarg); |
8540 | 9bf05444 | bellard | break;
|
8541 | c7f74643 | bellard | #endif
|
8542 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
8543 | 1d14ffa9 | bellard | case QEMU_OPTION_audio_help:
|
8544 | 1d14ffa9 | bellard | AUD_help (); |
8545 | 1d14ffa9 | bellard | exit (0);
|
8546 | 1d14ffa9 | bellard | break;
|
8547 | 1d14ffa9 | bellard | case QEMU_OPTION_soundhw:
|
8548 | 1d14ffa9 | bellard | select_soundhw (optarg); |
8549 | 1d14ffa9 | bellard | break;
|
8550 | 1d14ffa9 | bellard | #endif
|
8551 | cd6f1169 | bellard | case QEMU_OPTION_h:
|
8552 | 15f82208 | ths | help(0);
|
8553 | cd6f1169 | bellard | break;
|
8554 | 00f82b8a | aurel32 | case QEMU_OPTION_m: {
|
8555 | 00f82b8a | aurel32 | uint64_t value; |
8556 | 00f82b8a | aurel32 | char *ptr;
|
8557 | 00f82b8a | aurel32 | |
8558 | 00f82b8a | aurel32 | value = strtoul(optarg, &ptr, 10);
|
8559 | 00f82b8a | aurel32 | switch (*ptr) {
|
8560 | 00f82b8a | aurel32 | case 0: case 'M': case 'm': |
8561 | 00f82b8a | aurel32 | value <<= 20;
|
8562 | 00f82b8a | aurel32 | break;
|
8563 | 00f82b8a | aurel32 | case 'G': case 'g': |
8564 | 00f82b8a | aurel32 | value <<= 30;
|
8565 | 00f82b8a | aurel32 | break;
|
8566 | 00f82b8a | aurel32 | default:
|
8567 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
|
8568 | cd6f1169 | bellard | exit(1);
|
8569 | cd6f1169 | bellard | } |
8570 | 00f82b8a | aurel32 | |
8571 | 00f82b8a | aurel32 | /* On 32-bit hosts, QEMU is limited by virtual address space */
|
8572 | 00f82b8a | aurel32 | if (value > (2047 << 20) |
8573 | 00f82b8a | aurel32 | #ifndef USE_KQEMU
|
8574 | 00f82b8a | aurel32 | && HOST_LONG_BITS == 32
|
8575 | 00f82b8a | aurel32 | #endif
|
8576 | 00f82b8a | aurel32 | ) { |
8577 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
|
8578 | 00f82b8a | aurel32 | exit(1);
|
8579 | 00f82b8a | aurel32 | } |
8580 | 00f82b8a | aurel32 | if (value != (uint64_t)(ram_addr_t)value) {
|
8581 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: ram size too large\n");
|
8582 | 00f82b8a | aurel32 | exit(1);
|
8583 | 00f82b8a | aurel32 | } |
8584 | 00f82b8a | aurel32 | ram_size = value; |
8585 | cd6f1169 | bellard | break;
|
8586 | 00f82b8a | aurel32 | } |
8587 | cd6f1169 | bellard | case QEMU_OPTION_d:
|
8588 | cd6f1169 | bellard | { |
8589 | cd6f1169 | bellard | int mask;
|
8590 | cd6f1169 | bellard | CPULogItem *item; |
8591 | 3b46e624 | ths | |
8592 | cd6f1169 | bellard | mask = cpu_str_to_log_mask(optarg); |
8593 | cd6f1169 | bellard | if (!mask) {
|
8594 | cd6f1169 | bellard | printf("Log items (comma separated):\n");
|
8595 | f193c797 | bellard | for(item = cpu_log_items; item->mask != 0; item++) { |
8596 | f193c797 | bellard | printf("%-10s %s\n", item->name, item->help);
|
8597 | f193c797 | bellard | } |
8598 | f193c797 | bellard | exit(1);
|
8599 | cd6f1169 | bellard | } |
8600 | cd6f1169 | bellard | cpu_set_log(mask); |
8601 | f193c797 | bellard | } |
8602 | cd6f1169 | bellard | break;
|
8603 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
8604 | cd6f1169 | bellard | case QEMU_OPTION_s:
|
8605 | cd6f1169 | bellard | use_gdbstub = 1;
|
8606 | cd6f1169 | bellard | break;
|
8607 | cd6f1169 | bellard | case QEMU_OPTION_p:
|
8608 | cfc3475a | pbrook | gdbstub_port = optarg; |
8609 | cd6f1169 | bellard | break;
|
8610 | 67b915a5 | bellard | #endif
|
8611 | cd6f1169 | bellard | case QEMU_OPTION_L:
|
8612 | cd6f1169 | bellard | bios_dir = optarg; |
8613 | cd6f1169 | bellard | break;
|
8614 | 1192dad8 | j_mayer | case QEMU_OPTION_bios:
|
8615 | 1192dad8 | j_mayer | bios_name = optarg; |
8616 | 1192dad8 | j_mayer | break;
|
8617 | cd6f1169 | bellard | case QEMU_OPTION_S:
|
8618 | 3c07f8e8 | pbrook | autostart = 0;
|
8619 | cd6f1169 | bellard | break;
|
8620 | 3d11d0eb | bellard | case QEMU_OPTION_k:
|
8621 | 3d11d0eb | bellard | keyboard_layout = optarg; |
8622 | 3d11d0eb | bellard | break;
|
8623 | ee22c2f7 | bellard | case QEMU_OPTION_localtime:
|
8624 | ee22c2f7 | bellard | rtc_utc = 0;
|
8625 | ee22c2f7 | bellard | break;
|
8626 | 1f04275e | bellard | case QEMU_OPTION_cirrusvga:
|
8627 | 1f04275e | bellard | cirrus_vga_enabled = 1;
|
8628 | d34cab9f | ths | vmsvga_enabled = 0;
|
8629 | d34cab9f | ths | break;
|
8630 | d34cab9f | ths | case QEMU_OPTION_vmsvga:
|
8631 | d34cab9f | ths | cirrus_vga_enabled = 0;
|
8632 | d34cab9f | ths | vmsvga_enabled = 1;
|
8633 | 1f04275e | bellard | break;
|
8634 | 1bfe856e | bellard | case QEMU_OPTION_std_vga:
|
8635 | 1bfe856e | bellard | cirrus_vga_enabled = 0;
|
8636 | d34cab9f | ths | vmsvga_enabled = 0;
|
8637 | 1bfe856e | bellard | break;
|
8638 | e9b137c2 | bellard | case QEMU_OPTION_g:
|
8639 | e9b137c2 | bellard | { |
8640 | e9b137c2 | bellard | const char *p; |
8641 | e9b137c2 | bellard | int w, h, depth;
|
8642 | e9b137c2 | bellard | p = optarg; |
8643 | e9b137c2 | bellard | w = strtol(p, (char **)&p, 10); |
8644 | e9b137c2 | bellard | if (w <= 0) { |
8645 | e9b137c2 | bellard | graphic_error:
|
8646 | e9b137c2 | bellard | fprintf(stderr, "qemu: invalid resolution or depth\n");
|
8647 | e9b137c2 | bellard | exit(1);
|
8648 | e9b137c2 | bellard | } |
8649 | e9b137c2 | bellard | if (*p != 'x') |
8650 | e9b137c2 | bellard | goto graphic_error;
|
8651 | e9b137c2 | bellard | p++; |
8652 | e9b137c2 | bellard | h = strtol(p, (char **)&p, 10); |
8653 | e9b137c2 | bellard | if (h <= 0) |
8654 | e9b137c2 | bellard | goto graphic_error;
|
8655 | e9b137c2 | bellard | if (*p == 'x') { |
8656 | e9b137c2 | bellard | p++; |
8657 | e9b137c2 | bellard | depth = strtol(p, (char **)&p, 10); |
8658 | 5fafdf24 | ths | if (depth != 8 && depth != 15 && depth != 16 && |
8659 | e9b137c2 | bellard | depth != 24 && depth != 32) |
8660 | e9b137c2 | bellard | goto graphic_error;
|
8661 | e9b137c2 | bellard | } else if (*p == '\0') { |
8662 | e9b137c2 | bellard | depth = graphic_depth; |
8663 | e9b137c2 | bellard | } else {
|
8664 | e9b137c2 | bellard | goto graphic_error;
|
8665 | e9b137c2 | bellard | } |
8666 | 3b46e624 | ths | |
8667 | e9b137c2 | bellard | graphic_width = w; |
8668 | e9b137c2 | bellard | graphic_height = h; |
8669 | e9b137c2 | bellard | graphic_depth = depth; |
8670 | e9b137c2 | bellard | } |
8671 | e9b137c2 | bellard | break;
|
8672 | 20d8a3ed | ths | case QEMU_OPTION_echr:
|
8673 | 20d8a3ed | ths | { |
8674 | 20d8a3ed | ths | char *r;
|
8675 | 20d8a3ed | ths | term_escape_char = strtol(optarg, &r, 0);
|
8676 | 20d8a3ed | ths | if (r == optarg)
|
8677 | 20d8a3ed | ths | printf("Bad argument to echr\n");
|
8678 | 20d8a3ed | ths | break;
|
8679 | 20d8a3ed | ths | } |
8680 | 82c643ff | bellard | case QEMU_OPTION_monitor:
|
8681 | fd5f393a | pbrook | monitor_device = optarg; |
8682 | 82c643ff | bellard | break;
|
8683 | 82c643ff | bellard | case QEMU_OPTION_serial:
|
8684 | 8d11df9e | bellard | if (serial_device_index >= MAX_SERIAL_PORTS) {
|
8685 | 8d11df9e | bellard | fprintf(stderr, "qemu: too many serial ports\n");
|
8686 | 8d11df9e | bellard | exit(1);
|
8687 | 8d11df9e | bellard | } |
8688 | fd5f393a | pbrook | serial_devices[serial_device_index] = optarg; |
8689 | 8d11df9e | bellard | serial_device_index++; |
8690 | 82c643ff | bellard | break;
|
8691 | 6508fe59 | bellard | case QEMU_OPTION_parallel:
|
8692 | 6508fe59 | bellard | if (parallel_device_index >= MAX_PARALLEL_PORTS) {
|
8693 | 6508fe59 | bellard | fprintf(stderr, "qemu: too many parallel ports\n");
|
8694 | 6508fe59 | bellard | exit(1);
|
8695 | 6508fe59 | bellard | } |
8696 | fd5f393a | pbrook | parallel_devices[parallel_device_index] = optarg; |
8697 | 6508fe59 | bellard | parallel_device_index++; |
8698 | 6508fe59 | bellard | break;
|
8699 | d63d307f | bellard | case QEMU_OPTION_loadvm:
|
8700 | d63d307f | bellard | loadvm = optarg; |
8701 | d63d307f | bellard | break;
|
8702 | d63d307f | bellard | case QEMU_OPTION_full_screen:
|
8703 | d63d307f | bellard | full_screen = 1;
|
8704 | d63d307f | bellard | break;
|
8705 | 667accab | ths | #ifdef CONFIG_SDL
|
8706 | 43523e93 | ths | case QEMU_OPTION_no_frame:
|
8707 | 43523e93 | ths | no_frame = 1;
|
8708 | 43523e93 | ths | break;
|
8709 | 3780e197 | ths | case QEMU_OPTION_alt_grab:
|
8710 | 3780e197 | ths | alt_grab = 1;
|
8711 | 3780e197 | ths | break;
|
8712 | 667accab | ths | case QEMU_OPTION_no_quit:
|
8713 | 667accab | ths | no_quit = 1;
|
8714 | 667accab | ths | break;
|
8715 | 667accab | ths | #endif
|
8716 | f7cce898 | bellard | case QEMU_OPTION_pidfile:
|
8717 | 93815bc2 | ths | pid_file = optarg; |
8718 | f7cce898 | bellard | break;
|
8719 | a09db21f | bellard | #ifdef TARGET_I386
|
8720 | a09db21f | bellard | case QEMU_OPTION_win2k_hack:
|
8721 | a09db21f | bellard | win2k_install_hack = 1;
|
8722 | a09db21f | bellard | break;
|
8723 | a09db21f | bellard | #endif
|
8724 | d993e026 | bellard | #ifdef USE_KQEMU
|
8725 | d993e026 | bellard | case QEMU_OPTION_no_kqemu:
|
8726 | d993e026 | bellard | kqemu_allowed = 0;
|
8727 | d993e026 | bellard | break;
|
8728 | 89bfc105 | bellard | case QEMU_OPTION_kernel_kqemu:
|
8729 | 89bfc105 | bellard | kqemu_allowed = 2;
|
8730 | 89bfc105 | bellard | break;
|
8731 | d993e026 | bellard | #endif
|
8732 | bb36d470 | bellard | case QEMU_OPTION_usb:
|
8733 | bb36d470 | bellard | usb_enabled = 1;
|
8734 | bb36d470 | bellard | break;
|
8735 | a594cfbf | bellard | case QEMU_OPTION_usbdevice:
|
8736 | a594cfbf | bellard | usb_enabled = 1;
|
8737 | 0d92ed30 | pbrook | if (usb_devices_index >= MAX_USB_CMDLINE) {
|
8738 | a594cfbf | bellard | fprintf(stderr, "Too many USB devices\n");
|
8739 | a594cfbf | bellard | exit(1);
|
8740 | a594cfbf | bellard | } |
8741 | fd5f393a | pbrook | usb_devices[usb_devices_index] = optarg; |
8742 | a594cfbf | bellard | usb_devices_index++; |
8743 | a594cfbf | bellard | break;
|
8744 | 6a00d601 | bellard | case QEMU_OPTION_smp:
|
8745 | 6a00d601 | bellard | smp_cpus = atoi(optarg); |
8746 | ba3c64fb | bellard | if (smp_cpus < 1 || smp_cpus > MAX_CPUS) { |
8747 | 6a00d601 | bellard | fprintf(stderr, "Invalid number of CPUs\n");
|
8748 | 6a00d601 | bellard | exit(1);
|
8749 | 6a00d601 | bellard | } |
8750 | 6a00d601 | bellard | break;
|
8751 | 24236869 | bellard | case QEMU_OPTION_vnc:
|
8752 | 73fc9742 | ths | vnc_display = optarg; |
8753 | 24236869 | bellard | break;
|
8754 | 6515b203 | bellard | case QEMU_OPTION_no_acpi:
|
8755 | 6515b203 | bellard | acpi_enabled = 0;
|
8756 | 6515b203 | bellard | break;
|
8757 | d1beab82 | bellard | case QEMU_OPTION_no_reboot:
|
8758 | d1beab82 | bellard | no_reboot = 1;
|
8759 | d1beab82 | bellard | break;
|
8760 | b2f76161 | aurel32 | case QEMU_OPTION_no_shutdown:
|
8761 | b2f76161 | aurel32 | no_shutdown = 1;
|
8762 | b2f76161 | aurel32 | break;
|
8763 | 9467cd46 | balrog | case QEMU_OPTION_show_cursor:
|
8764 | 9467cd46 | balrog | cursor_hide = 0;
|
8765 | 9467cd46 | balrog | break;
|
8766 | 71e3ceb8 | ths | case QEMU_OPTION_daemonize:
|
8767 | 71e3ceb8 | ths | daemonize = 1;
|
8768 | 71e3ceb8 | ths | break;
|
8769 | 9ae02555 | ths | case QEMU_OPTION_option_rom:
|
8770 | 9ae02555 | ths | if (nb_option_roms >= MAX_OPTION_ROMS) {
|
8771 | 9ae02555 | ths | fprintf(stderr, "Too many option ROMs\n");
|
8772 | 9ae02555 | ths | exit(1);
|
8773 | 9ae02555 | ths | } |
8774 | 9ae02555 | ths | option_rom[nb_option_roms] = optarg; |
8775 | 9ae02555 | ths | nb_option_roms++; |
8776 | 9ae02555 | ths | break;
|
8777 | 8e71621f | pbrook | case QEMU_OPTION_semihosting:
|
8778 | 8e71621f | pbrook | semihosting_enabled = 1;
|
8779 | 8e71621f | pbrook | break;
|
8780 | c35734b2 | ths | case QEMU_OPTION_name:
|
8781 | c35734b2 | ths | qemu_name = optarg; |
8782 | c35734b2 | ths | break;
|
8783 | 66508601 | blueswir1 | #ifdef TARGET_SPARC
|
8784 | 66508601 | blueswir1 | case QEMU_OPTION_prom_env:
|
8785 | 66508601 | blueswir1 | if (nb_prom_envs >= MAX_PROM_ENVS) {
|
8786 | 66508601 | blueswir1 | fprintf(stderr, "Too many prom variables\n");
|
8787 | 66508601 | blueswir1 | exit(1);
|
8788 | 66508601 | blueswir1 | } |
8789 | 66508601 | blueswir1 | prom_envs[nb_prom_envs] = optarg; |
8790 | 66508601 | blueswir1 | nb_prom_envs++; |
8791 | 66508601 | blueswir1 | break;
|
8792 | 66508601 | blueswir1 | #endif
|
8793 | 2b8f2d41 | balrog | #ifdef TARGET_ARM
|
8794 | 2b8f2d41 | balrog | case QEMU_OPTION_old_param:
|
8795 | 2b8f2d41 | balrog | old_param = 1;
|
8796 | 05ebd537 | ths | break;
|
8797 | 2b8f2d41 | balrog | #endif
|
8798 | f3dcfada | ths | case QEMU_OPTION_clock:
|
8799 | f3dcfada | ths | configure_alarms(optarg); |
8800 | f3dcfada | ths | break;
|
8801 | 7e0af5d0 | bellard | case QEMU_OPTION_startdate:
|
8802 | 7e0af5d0 | bellard | { |
8803 | 7e0af5d0 | bellard | struct tm tm;
|
8804 | f6503059 | balrog | time_t rtc_start_date; |
8805 | 7e0af5d0 | bellard | if (!strcmp(optarg, "now")) { |
8806 | f6503059 | balrog | rtc_date_offset = -1;
|
8807 | 7e0af5d0 | bellard | } else {
|
8808 | 7e0af5d0 | bellard | if (sscanf(optarg, "%d-%d-%dT%d:%d:%d", |
8809 | 7e0af5d0 | bellard | &tm.tm_year, |
8810 | 7e0af5d0 | bellard | &tm.tm_mon, |
8811 | 7e0af5d0 | bellard | &tm.tm_mday, |
8812 | 7e0af5d0 | bellard | &tm.tm_hour, |
8813 | 7e0af5d0 | bellard | &tm.tm_min, |
8814 | 7e0af5d0 | bellard | &tm.tm_sec) == 6) {
|
8815 | 7e0af5d0 | bellard | /* OK */
|
8816 | 7e0af5d0 | bellard | } else if (sscanf(optarg, "%d-%d-%d", |
8817 | 7e0af5d0 | bellard | &tm.tm_year, |
8818 | 7e0af5d0 | bellard | &tm.tm_mon, |
8819 | 7e0af5d0 | bellard | &tm.tm_mday) == 3) {
|
8820 | 7e0af5d0 | bellard | tm.tm_hour = 0;
|
8821 | 7e0af5d0 | bellard | tm.tm_min = 0;
|
8822 | 7e0af5d0 | bellard | tm.tm_sec = 0;
|
8823 | 7e0af5d0 | bellard | } else {
|
8824 | 7e0af5d0 | bellard | goto date_fail;
|
8825 | 7e0af5d0 | bellard | } |
8826 | 7e0af5d0 | bellard | tm.tm_year -= 1900;
|
8827 | 7e0af5d0 | bellard | tm.tm_mon--; |
8828 | 3c6b2088 | bellard | rtc_start_date = mktimegm(&tm); |
8829 | 7e0af5d0 | bellard | if (rtc_start_date == -1) { |
8830 | 7e0af5d0 | bellard | date_fail:
|
8831 | 7e0af5d0 | bellard | fprintf(stderr, "Invalid date format. Valid format are:\n"
|
8832 | 7e0af5d0 | bellard | "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
|
8833 | 7e0af5d0 | bellard | exit(1);
|
8834 | 7e0af5d0 | bellard | } |
8835 | f6503059 | balrog | rtc_date_offset = time(NULL) - rtc_start_date;
|
8836 | 7e0af5d0 | bellard | } |
8837 | 7e0af5d0 | bellard | } |
8838 | 7e0af5d0 | bellard | break;
|
8839 | 26a5f13b | bellard | case QEMU_OPTION_tb_size:
|
8840 | 26a5f13b | bellard | tb_size = strtol(optarg, NULL, 0); |
8841 | 26a5f13b | bellard | if (tb_size < 0) |
8842 | 26a5f13b | bellard | tb_size = 0;
|
8843 | 26a5f13b | bellard | break;
|
8844 | 2e70f6ef | pbrook | case QEMU_OPTION_icount:
|
8845 | 2e70f6ef | pbrook | use_icount = 1;
|
8846 | 2e70f6ef | pbrook | if (strcmp(optarg, "auto") == 0) { |
8847 | 2e70f6ef | pbrook | icount_time_shift = -1;
|
8848 | 2e70f6ef | pbrook | } else {
|
8849 | 2e70f6ef | pbrook | icount_time_shift = strtol(optarg, NULL, 0); |
8850 | 2e70f6ef | pbrook | } |
8851 | 2e70f6ef | pbrook | break;
|
8852 | cd6f1169 | bellard | } |
8853 | 0824d6fc | bellard | } |
8854 | 0824d6fc | bellard | } |
8855 | 330d0414 | bellard | |
8856 | bc0129d9 | aliguori | if (nographic) {
|
8857 | bc0129d9 | aliguori | if (serial_device_index == 0) |
8858 | bc0129d9 | aliguori | serial_devices[0] = "stdio"; |
8859 | bc0129d9 | aliguori | if (parallel_device_index == 0) |
8860 | bc0129d9 | aliguori | parallel_devices[0] = "null"; |
8861 | bc0129d9 | aliguori | if (strncmp(monitor_device, "vc", 2) == 0) |
8862 | bc0129d9 | aliguori | monitor_device = "stdio";
|
8863 | bc0129d9 | aliguori | } |
8864 | bc0129d9 | aliguori | |
8865 | 71e3ceb8 | ths | #ifndef _WIN32
|
8866 | 71e3ceb8 | ths | if (daemonize) {
|
8867 | 71e3ceb8 | ths | pid_t pid; |
8868 | 71e3ceb8 | ths | |
8869 | 71e3ceb8 | ths | if (pipe(fds) == -1) |
8870 | 71e3ceb8 | ths | exit(1);
|
8871 | 71e3ceb8 | ths | |
8872 | 71e3ceb8 | ths | pid = fork(); |
8873 | 71e3ceb8 | ths | if (pid > 0) { |
8874 | 71e3ceb8 | ths | uint8_t status; |
8875 | 71e3ceb8 | ths | ssize_t len; |
8876 | 71e3ceb8 | ths | |
8877 | 71e3ceb8 | ths | close(fds[1]);
|
8878 | 71e3ceb8 | ths | |
8879 | 71e3ceb8 | ths | again:
|
8880 | 93815bc2 | ths | len = read(fds[0], &status, 1); |
8881 | 93815bc2 | ths | if (len == -1 && (errno == EINTR)) |
8882 | 93815bc2 | ths | goto again;
|
8883 | 93815bc2 | ths | |
8884 | 93815bc2 | ths | if (len != 1) |
8885 | 93815bc2 | ths | exit(1);
|
8886 | 93815bc2 | ths | else if (status == 1) { |
8887 | 93815bc2 | ths | fprintf(stderr, "Could not acquire pidfile\n");
|
8888 | 93815bc2 | ths | exit(1);
|
8889 | 93815bc2 | ths | } else
|
8890 | 93815bc2 | ths | exit(0);
|
8891 | 71e3ceb8 | ths | } else if (pid < 0) |
8892 | 93815bc2 | ths | exit(1);
|
8893 | 71e3ceb8 | ths | |
8894 | 71e3ceb8 | ths | setsid(); |
8895 | 71e3ceb8 | ths | |
8896 | 71e3ceb8 | ths | pid = fork(); |
8897 | 71e3ceb8 | ths | if (pid > 0) |
8898 | 71e3ceb8 | ths | exit(0);
|
8899 | 71e3ceb8 | ths | else if (pid < 0) |
8900 | 71e3ceb8 | ths | exit(1);
|
8901 | 71e3ceb8 | ths | |
8902 | 71e3ceb8 | ths | umask(027);
|
8903 | 71e3ceb8 | ths | |
8904 | 71e3ceb8 | ths | signal(SIGTSTP, SIG_IGN); |
8905 | 71e3ceb8 | ths | signal(SIGTTOU, SIG_IGN); |
8906 | 71e3ceb8 | ths | signal(SIGTTIN, SIG_IGN); |
8907 | 71e3ceb8 | ths | } |
8908 | 71e3ceb8 | ths | #endif
|
8909 | 71e3ceb8 | ths | |
8910 | aa26bb2d | ths | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
8911 | 93815bc2 | ths | if (daemonize) {
|
8912 | 93815bc2 | ths | uint8_t status = 1;
|
8913 | 93815bc2 | ths | write(fds[1], &status, 1); |
8914 | 93815bc2 | ths | } else
|
8915 | 93815bc2 | ths | fprintf(stderr, "Could not acquire pid file\n");
|
8916 | 93815bc2 | ths | exit(1);
|
8917 | 93815bc2 | ths | } |
8918 | 93815bc2 | ths | |
8919 | ff3fbb30 | bellard | #ifdef USE_KQEMU
|
8920 | ff3fbb30 | bellard | if (smp_cpus > 1) |
8921 | ff3fbb30 | bellard | kqemu_allowed = 0;
|
8922 | ff3fbb30 | bellard | #endif
|
8923 | a20dd508 | bellard | linux_boot = (kernel_filename != NULL);
|
8924 | 7317b8ca | balrog | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
8925 | 6c41b272 | balrog | |
8926 | 28c5af54 | j_mayer | if (!linux_boot && net_boot == 0 && |
8927 | f88e4b91 | blueswir1 | !machine->nodisk_ok && nb_drives_opt == 0)
|
8928 | 15f82208 | ths | help(1);
|
8929 | 0824d6fc | bellard | |
8930 | f8d39c01 | ths | if (!linux_boot && *kernel_cmdline != '\0') { |
8931 | f8d39c01 | ths | fprintf(stderr, "-append only allowed with -kernel option\n");
|
8932 | f8d39c01 | ths | exit(1);
|
8933 | f8d39c01 | ths | } |
8934 | f8d39c01 | ths | |
8935 | f8d39c01 | ths | if (!linux_boot && initrd_filename != NULL) { |
8936 | f8d39c01 | ths | fprintf(stderr, "-initrd only allowed with -kernel option\n");
|
8937 | f8d39c01 | ths | exit(1);
|
8938 | f8d39c01 | ths | } |
8939 | f8d39c01 | ths | |
8940 | 96d30e48 | ths | /* boot to floppy or the default cd if no hard disk defined yet */
|
8941 | 28c5af54 | j_mayer | if (!boot_devices[0]) { |
8942 | e4bcb14c | ths | boot_devices = "cad";
|
8943 | 96d30e48 | ths | } |
8944 | b118d61e | bellard | setvbuf(stdout, NULL, _IOLBF, 0); |
8945 | 3b46e624 | ths | |
8946 | 634fce96 | pbrook | init_timers(); |
8947 | 634fce96 | pbrook | init_timer_alarm(); |
8948 | 83f64091 | bellard | qemu_aio_init(); |
8949 | 2e70f6ef | pbrook | if (use_icount && icount_time_shift < 0) { |
8950 | 2e70f6ef | pbrook | use_icount = 2;
|
8951 | 2e70f6ef | pbrook | /* 125MIPS seems a reasonable initial guess at the guest speed.
|
8952 | 2e70f6ef | pbrook | It will be corrected fairly quickly anyway. */
|
8953 | 2e70f6ef | pbrook | icount_time_shift = 3;
|
8954 | 2e70f6ef | pbrook | init_icount_adjust(); |
8955 | 2e70f6ef | pbrook | } |
8956 | 634fce96 | pbrook | |
8957 | fd1dff4b | bellard | #ifdef _WIN32
|
8958 | fd1dff4b | bellard | socket_init(); |
8959 | fd1dff4b | bellard | #endif
|
8960 | fd1dff4b | bellard | |
8961 | 7c9d8e07 | bellard | /* init network clients */
|
8962 | 7c9d8e07 | bellard | if (nb_net_clients == 0) { |
8963 | 7c9d8e07 | bellard | /* if no clients, we use a default config */
|
8964 | fd5f393a | pbrook | net_clients[0] = "nic"; |
8965 | fd5f393a | pbrook | net_clients[1] = "user"; |
8966 | 7c9d8e07 | bellard | nb_net_clients = 2;
|
8967 | c20709aa | bellard | } |
8968 | c20709aa | bellard | |
8969 | 7c9d8e07 | bellard | for(i = 0;i < nb_net_clients; i++) { |
8970 | 9ad97e65 | balrog | if (net_client_parse(net_clients[i]) < 0) |
8971 | 7c9d8e07 | bellard | exit(1);
|
8972 | 702c651c | bellard | } |
8973 | 833c7174 | blueswir1 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
8974 | 833c7174 | blueswir1 | if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) |
8975 | 833c7174 | blueswir1 | continue;
|
8976 | 9ad97e65 | balrog | if (vlan->nb_guest_devs == 0) |
8977 | 9ad97e65 | balrog | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
|
8978 | 833c7174 | blueswir1 | if (vlan->nb_host_devs == 0) |
8979 | 833c7174 | blueswir1 | fprintf(stderr, |
8980 | 833c7174 | blueswir1 | "Warning: vlan %d is not connected to host network\n",
|
8981 | 833c7174 | blueswir1 | vlan->id); |
8982 | 833c7174 | blueswir1 | } |
8983 | f1510b2c | bellard | |
8984 | eec85c2a | ths | #ifdef TARGET_I386
|
8985 | ed494d87 | balrog | /* XXX: this should be moved in the PC machine instantiation code */
|
8986 | 28c5af54 | j_mayer | if (net_boot != 0) { |
8987 | 28c5af54 | j_mayer | int netroms = 0; |
8988 | 28c5af54 | j_mayer | for (i = 0; i < nb_nics && i < 4; i++) { |
8989 | eec85c2a | ths | const char *model = nd_table[i].model; |
8990 | eec85c2a | ths | char buf[1024]; |
8991 | 28c5af54 | j_mayer | if (net_boot & (1 << i)) { |
8992 | 28c5af54 | j_mayer | if (model == NULL) |
8993 | 28c5af54 | j_mayer | model = "ne2k_pci";
|
8994 | 28c5af54 | j_mayer | snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model); |
8995 | 28c5af54 | j_mayer | if (get_image_size(buf) > 0) { |
8996 | 28c5af54 | j_mayer | if (nb_option_roms >= MAX_OPTION_ROMS) {
|
8997 | 28c5af54 | j_mayer | fprintf(stderr, "Too many option ROMs\n");
|
8998 | 28c5af54 | j_mayer | exit(1);
|
8999 | 28c5af54 | j_mayer | } |
9000 | 28c5af54 | j_mayer | option_rom[nb_option_roms] = strdup(buf); |
9001 | 28c5af54 | j_mayer | nb_option_roms++; |
9002 | 28c5af54 | j_mayer | netroms++; |
9003 | 28c5af54 | j_mayer | } |
9004 | 28c5af54 | j_mayer | } |
9005 | eec85c2a | ths | } |
9006 | 28c5af54 | j_mayer | if (netroms == 0) { |
9007 | eec85c2a | ths | fprintf(stderr, "No valid PXE rom found for network device\n");
|
9008 | eec85c2a | ths | exit(1);
|
9009 | eec85c2a | ths | } |
9010 | eec85c2a | ths | } |
9011 | eec85c2a | ths | #endif
|
9012 | eec85c2a | ths | |
9013 | 0824d6fc | bellard | /* init the memory */
|
9014 | 7fb4fdcf | balrog | phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED; |
9015 | 7fb4fdcf | balrog | |
9016 | 7fb4fdcf | balrog | if (machine->ram_require & RAMSIZE_FIXED) {
|
9017 | 7fb4fdcf | balrog | if (ram_size > 0) { |
9018 | 7fb4fdcf | balrog | if (ram_size < phys_ram_size) {
|
9019 | cd940061 | aurel32 | fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
|
9020 | cd940061 | aurel32 | machine->name, (unsigned long long) phys_ram_size); |
9021 | 7fb4fdcf | balrog | exit(-1);
|
9022 | 7fb4fdcf | balrog | } |
9023 | 7fb4fdcf | balrog | |
9024 | 7fb4fdcf | balrog | phys_ram_size = ram_size; |
9025 | 7fb4fdcf | balrog | } else
|
9026 | 7fb4fdcf | balrog | ram_size = phys_ram_size; |
9027 | 7fb4fdcf | balrog | } else {
|
9028 | 4fc5d071 | aurel32 | if (ram_size == 0) |
9029 | 7fb4fdcf | balrog | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
9030 | 7fb4fdcf | balrog | |
9031 | 7fb4fdcf | balrog | phys_ram_size += ram_size; |
9032 | 7fb4fdcf | balrog | } |
9033 | 9ae02555 | ths | |
9034 | d993e026 | bellard | phys_ram_base = qemu_vmalloc(phys_ram_size); |
9035 | 7f7f9873 | bellard | if (!phys_ram_base) {
|
9036 | 7f7f9873 | bellard | fprintf(stderr, "Could not allocate physical memory\n");
|
9037 | 0824d6fc | bellard | exit(1);
|
9038 | 0824d6fc | bellard | } |
9039 | 0824d6fc | bellard | |
9040 | 26a5f13b | bellard | /* init the dynamic translator */
|
9041 | 26a5f13b | bellard | cpu_exec_init_all(tb_size * 1024 * 1024); |
9042 | 26a5f13b | bellard | |
9043 | 5905b2e5 | bellard | bdrv_init(); |
9044 | c4b1fcc0 | bellard | |
9045 | e4bcb14c | ths | /* we always create the cdrom drive, even if no disk is there */
|
9046 | c4b1fcc0 | bellard | |
9047 | e4bcb14c | ths | if (nb_drives_opt < MAX_DRIVES)
|
9048 | 609497ab | balrog | drive_add(NULL, CDROM_ALIAS);
|
9049 | c4b1fcc0 | bellard | |
9050 | 9d413d1d | balrog | /* we always create at least one floppy */
|
9051 | 33e3963e | bellard | |
9052 | e4bcb14c | ths | if (nb_drives_opt < MAX_DRIVES)
|
9053 | 609497ab | balrog | drive_add(NULL, FD_ALIAS, 0); |
9054 | 86f55663 | j_mayer | |
9055 | 9d413d1d | balrog | /* we always create one sd slot, even if no card is in it */
|
9056 | 9d413d1d | balrog | |
9057 | 9d413d1d | balrog | if (nb_drives_opt < MAX_DRIVES)
|
9058 | 609497ab | balrog | drive_add(NULL, SD_ALIAS);
|
9059 | 9d413d1d | balrog | |
9060 | e4bcb14c | ths | /* open the virtual block devices */
|
9061 | e4bcb14c | ths | |
9062 | e4bcb14c | ths | for(i = 0; i < nb_drives_opt; i++) |
9063 | 609497ab | balrog | if (drive_init(&drives_opt[i], snapshot, machine) == -1) |
9064 | e4bcb14c | ths | exit(1);
|
9065 | 3e3d5815 | balrog | |
9066 | c88676f8 | bellard | register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
9067 | c88676f8 | bellard | register_savevm("ram", 0, 2, ram_save, ram_load, NULL); |
9068 | 8a7ddc38 | bellard | |
9069 | 313aa567 | bellard | /* terminal init */
|
9070 | 740733bb | ths | memset(&display_state, 0, sizeof(display_state)); |
9071 | a20dd508 | bellard | if (nographic) {
|
9072 | 4d3b6f6e | balrog | if (curses) {
|
9073 | 4d3b6f6e | balrog | fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
|
9074 | 4d3b6f6e | balrog | exit(1);
|
9075 | 4d3b6f6e | balrog | } |
9076 | 2ff89790 | ths | /* nearly nothing to do */
|
9077 | 2ff89790 | ths | dumb_display_init(ds); |
9078 | 73fc9742 | ths | } else if (vnc_display != NULL) { |
9079 | 71cab5ca | ths | vnc_display_init(ds); |
9080 | 71cab5ca | ths | if (vnc_display_open(ds, vnc_display) < 0) |
9081 | 71cab5ca | ths | exit(1);
|
9082 | 4d3b6f6e | balrog | } else
|
9083 | 4d3b6f6e | balrog | #if defined(CONFIG_CURSES)
|
9084 | 4d3b6f6e | balrog | if (curses) {
|
9085 | 4d3b6f6e | balrog | curses_display_init(ds, full_screen); |
9086 | 4d3b6f6e | balrog | } else
|
9087 | 4d3b6f6e | balrog | #endif
|
9088 | 4d3b6f6e | balrog | { |
9089 | 5b0753e0 | bellard | #if defined(CONFIG_SDL)
|
9090 | 43523e93 | ths | sdl_display_init(ds, full_screen, no_frame); |
9091 | 5b0753e0 | bellard | #elif defined(CONFIG_COCOA)
|
9092 | 5b0753e0 | bellard | cocoa_display_init(ds, full_screen); |
9093 | 67276f53 | pbrook | #else
|
9094 | 67276f53 | pbrook | dumb_display_init(ds); |
9095 | 313aa567 | bellard | #endif
|
9096 | 313aa567 | bellard | } |
9097 | 0824d6fc | bellard | |
9098 | 5b08fc10 | aliguori | #ifndef _WIN32
|
9099 | 5b08fc10 | aliguori | /* must be after terminal init, SDL library changes signal handlers */
|
9100 | 5b08fc10 | aliguori | termsig_setup(); |
9101 | 5b08fc10 | aliguori | #endif
|
9102 | 5b08fc10 | aliguori | |
9103 | 20d8a3ed | ths | /* Maintain compatibility with multiple stdio monitors */
|
9104 | 20d8a3ed | ths | if (!strcmp(monitor_device,"stdio")) { |
9105 | 20d8a3ed | ths | for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
9106 | fd5f393a | pbrook | const char *devname = serial_devices[i]; |
9107 | fd5f393a | pbrook | if (devname && !strcmp(devname,"mon:stdio")) { |
9108 | fd5f393a | pbrook | monitor_device = NULL;
|
9109 | 20d8a3ed | ths | break;
|
9110 | fd5f393a | pbrook | } else if (devname && !strcmp(devname,"stdio")) { |
9111 | fd5f393a | pbrook | monitor_device = NULL;
|
9112 | fd5f393a | pbrook | serial_devices[i] = "mon:stdio";
|
9113 | 20d8a3ed | ths | break;
|
9114 | 20d8a3ed | ths | } |
9115 | 20d8a3ed | ths | } |
9116 | 20d8a3ed | ths | } |
9117 | fd5f393a | pbrook | if (monitor_device) {
|
9118 | 20d8a3ed | ths | monitor_hd = qemu_chr_open(monitor_device); |
9119 | 20d8a3ed | ths | if (!monitor_hd) {
|
9120 | 20d8a3ed | ths | fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
|
9121 | 20d8a3ed | ths | exit(1);
|
9122 | 20d8a3ed | ths | } |
9123 | 20d8a3ed | ths | monitor_init(monitor_hd, !nographic); |
9124 | 82c643ff | bellard | } |
9125 | 82c643ff | bellard | |
9126 | 8d11df9e | bellard | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
9127 | c03b0f0f | bellard | const char *devname = serial_devices[i]; |
9128 | fd5f393a | pbrook | if (devname && strcmp(devname, "none")) { |
9129 | c03b0f0f | bellard | serial_hds[i] = qemu_chr_open(devname); |
9130 | 8d11df9e | bellard | if (!serial_hds[i]) {
|
9131 | 5fafdf24 | ths | fprintf(stderr, "qemu: could not open serial device '%s'\n",
|
9132 | c03b0f0f | bellard | devname); |
9133 | 8d11df9e | bellard | exit(1);
|
9134 | 8d11df9e | bellard | } |
9135 | af3a9031 | ths | if (strstart(devname, "vc", 0)) |
9136 | 7ba1260a | bellard | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
|
9137 | 8d11df9e | bellard | } |
9138 | 82c643ff | bellard | } |
9139 | 82c643ff | bellard | |
9140 | 6508fe59 | bellard | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
9141 | c03b0f0f | bellard | const char *devname = parallel_devices[i]; |
9142 | fd5f393a | pbrook | if (devname && strcmp(devname, "none")) { |
9143 | c03b0f0f | bellard | parallel_hds[i] = qemu_chr_open(devname); |
9144 | 6508fe59 | bellard | if (!parallel_hds[i]) {
|
9145 | 5fafdf24 | ths | fprintf(stderr, "qemu: could not open parallel device '%s'\n",
|
9146 | c03b0f0f | bellard | devname); |
9147 | 6508fe59 | bellard | exit(1);
|
9148 | 6508fe59 | bellard | } |
9149 | af3a9031 | ths | if (strstart(devname, "vc", 0)) |
9150 | 7ba1260a | bellard | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
|
9151 | 6508fe59 | bellard | } |
9152 | 6508fe59 | bellard | } |
9153 | 6508fe59 | bellard | |
9154 | b881c2c6 | blueswir1 | machine->init(ram_size, vga_ram_size, boot_devices, ds, |
9155 | 94fc95cd | j_mayer | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
9156 | 73332e5c | bellard | |
9157 | 0d92ed30 | pbrook | /* init USB devices */
|
9158 | 0d92ed30 | pbrook | if (usb_enabled) {
|
9159 | 0d92ed30 | pbrook | for(i = 0; i < usb_devices_index; i++) { |
9160 | 0d92ed30 | pbrook | if (usb_device_add(usb_devices[i]) < 0) { |
9161 | 0d92ed30 | pbrook | fprintf(stderr, "Warning: could not add USB device %s\n",
|
9162 | 0d92ed30 | pbrook | usb_devices[i]); |
9163 | 0d92ed30 | pbrook | } |
9164 | 0d92ed30 | pbrook | } |
9165 | 0d92ed30 | pbrook | } |
9166 | 0d92ed30 | pbrook | |
9167 | 740733bb | ths | if (display_state.dpy_refresh) {
|
9168 | 740733bb | ths | display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state); |
9169 | 740733bb | ths | qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock)); |
9170 | 740733bb | ths | } |
9171 | 7f7f9873 | bellard | |
9172 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
9173 | b4608c04 | bellard | if (use_gdbstub) {
|
9174 | c636bb66 | bellard | /* XXX: use standard host:port notation and modify options
|
9175 | c636bb66 | bellard | accordingly. */
|
9176 | cfc3475a | pbrook | if (gdbserver_start(gdbstub_port) < 0) { |
9177 | cfc3475a | pbrook | fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
|
9178 | c636bb66 | bellard | gdbstub_port); |
9179 | 8a7ddc38 | bellard | exit(1);
|
9180 | 8a7ddc38 | bellard | } |
9181 | 45669e00 | balrog | } |
9182 | 67b915a5 | bellard | #endif
|
9183 | 45669e00 | balrog | |
9184 | d63d307f | bellard | if (loadvm)
|
9185 | faea38e7 | bellard | do_loadvm(loadvm); |
9186 | d63d307f | bellard | |
9187 | 67b915a5 | bellard | { |
9188 | 5905b2e5 | bellard | /* XXX: simplify init */
|
9189 | 83ab7950 | aliguori | read_passwords(); |
9190 | 3c07f8e8 | pbrook | if (autostart) {
|
9191 | 5905b2e5 | bellard | vm_start(); |
9192 | 5905b2e5 | bellard | } |
9193 | 0824d6fc | bellard | } |
9194 | ffd843bc | ths | |
9195 | 71e3ceb8 | ths | if (daemonize) {
|
9196 | 71e3ceb8 | ths | uint8_t status = 0;
|
9197 | 71e3ceb8 | ths | ssize_t len; |
9198 | 71e3ceb8 | ths | int fd;
|
9199 | 71e3ceb8 | ths | |
9200 | 71e3ceb8 | ths | again1:
|
9201 | 71e3ceb8 | ths | len = write(fds[1], &status, 1); |
9202 | 71e3ceb8 | ths | if (len == -1 && (errno == EINTR)) |
9203 | 71e3ceb8 | ths | goto again1;
|
9204 | 71e3ceb8 | ths | |
9205 | 71e3ceb8 | ths | if (len != 1) |
9206 | 71e3ceb8 | ths | exit(1);
|
9207 | 71e3ceb8 | ths | |
9208 | bd54b863 | aliguori | chdir("/");
|
9209 | aeb30be6 | balrog | TFR(fd = open("/dev/null", O_RDWR));
|
9210 | 71e3ceb8 | ths | if (fd == -1) |
9211 | 71e3ceb8 | ths | exit(1);
|
9212 | 71e3ceb8 | ths | |
9213 | 71e3ceb8 | ths | dup2(fd, 0);
|
9214 | 71e3ceb8 | ths | dup2(fd, 1);
|
9215 | 71e3ceb8 | ths | dup2(fd, 2);
|
9216 | 71e3ceb8 | ths | |
9217 | 71e3ceb8 | ths | close(fd); |
9218 | 71e3ceb8 | ths | } |
9219 | 71e3ceb8 | ths | |
9220 | 8a7ddc38 | bellard | main_loop(); |
9221 | 40c3bac3 | bellard | quit_timers(); |
9222 | b46a8906 | ths | |
9223 | 7d294b61 | ths | #if !defined(_WIN32)
|
9224 | b46a8906 | ths | /* close network clients */
|
9225 | b46a8906 | ths | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
9226 | b46a8906 | ths | VLANClientState *vc; |
9227 | b46a8906 | ths | |
9228 | 7d294b61 | ths | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
9229 | b46a8906 | ths | if (vc->fd_read == tap_receive) {
|
9230 | b46a8906 | ths | char ifname[64]; |
9231 | b46a8906 | ths | TAPState *s = vc->opaque; |
9232 | b46a8906 | ths | |
9233 | b46a8906 | ths | if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 && |
9234 | b46a8906 | ths | s->down_script[0])
|
9235 | b46a8906 | ths | launch_script(s->down_script, ifname, s->fd); |
9236 | b46a8906 | ths | } |
9237 | 8a16d273 | ths | #if defined(CONFIG_VDE)
|
9238 | 8a16d273 | ths | if (vc->fd_read == vde_from_qemu) {
|
9239 | 8a16d273 | ths | VDEState *s = vc->opaque; |
9240 | 8a16d273 | ths | vde_close(s->vde); |
9241 | 8a16d273 | ths | } |
9242 | 8a16d273 | ths | #endif
|
9243 | 4fddf62a | ths | } |
9244 | 7d294b61 | ths | } |
9245 | 7d294b61 | ths | #endif
|
9246 | 0824d6fc | bellard | return 0; |
9247 | 0824d6fc | bellard | } |