root / vl.c @ 9ad97e65
History | View | Annotate | Download (237.1 kB)
1 | 0824d6fc | bellard | /*
|
---|---|---|---|
2 | 80cabfad | bellard | * QEMU System Emulator
|
3 | 5fafdf24 | ths | *
|
4 | 68d0f70e | bellard | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | 1df912cf | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 1df912cf | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 1df912cf | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 1df912cf | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 1df912cf | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 1df912cf | bellard | * furnished to do so, subject to the following conditions:
|
12 | 1df912cf | bellard | *
|
13 | 1df912cf | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 1df912cf | bellard | * all copies or substantial portions of the Software.
|
15 | 1df912cf | bellard | *
|
16 | 1df912cf | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 1df912cf | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 1df912cf | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 1df912cf | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 1df912cf | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 1df912cf | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 1df912cf | bellard | * THE SOFTWARE.
|
23 | 0824d6fc | bellard | */
|
24 | 87ecb68b | pbrook | #include "hw/hw.h" |
25 | 87ecb68b | pbrook | #include "hw/boards.h" |
26 | 87ecb68b | pbrook | #include "hw/usb.h" |
27 | 87ecb68b | pbrook | #include "hw/pcmcia.h" |
28 | 87ecb68b | pbrook | #include "hw/pc.h" |
29 | 87ecb68b | pbrook | #include "hw/audiodev.h" |
30 | 87ecb68b | pbrook | #include "hw/isa.h" |
31 | 2e4d9fb1 | aurel32 | #include "hw/baum.h" |
32 | 87ecb68b | pbrook | #include "net.h" |
33 | 87ecb68b | pbrook | #include "console.h" |
34 | 87ecb68b | pbrook | #include "sysemu.h" |
35 | 87ecb68b | pbrook | #include "gdbstub.h" |
36 | 87ecb68b | pbrook | #include "qemu-timer.h" |
37 | 87ecb68b | pbrook | #include "qemu-char.h" |
38 | 87ecb68b | pbrook | #include "block.h" |
39 | 87ecb68b | pbrook | #include "audio/audio.h" |
40 | 67b915a5 | bellard | |
41 | 0824d6fc | bellard | #include <unistd.h> |
42 | 0824d6fc | bellard | #include <fcntl.h> |
43 | 0824d6fc | bellard | #include <signal.h> |
44 | 0824d6fc | bellard | #include <time.h> |
45 | 0824d6fc | bellard | #include <errno.h> |
46 | 67b915a5 | bellard | #include <sys/time.h> |
47 | c88676f8 | bellard | #include <zlib.h> |
48 | 67b915a5 | bellard | |
49 | 67b915a5 | bellard | #ifndef _WIN32
|
50 | 67b915a5 | bellard | #include <sys/times.h> |
51 | f1510b2c | bellard | #include <sys/wait.h> |
52 | 67b915a5 | bellard | #include <termios.h> |
53 | 67b915a5 | bellard | #include <sys/poll.h> |
54 | 67b915a5 | bellard | #include <sys/mman.h> |
55 | f1510b2c | bellard | #include <sys/ioctl.h> |
56 | f1510b2c | bellard | #include <sys/socket.h> |
57 | c94c8d64 | bellard | #include <netinet/in.h> |
58 | 9d728e8c | bellard | #include <dirent.h> |
59 | 7c9d8e07 | bellard | #include <netdb.h> |
60 | cb4b976b | ths | #include <sys/select.h> |
61 | cb4b976b | ths | #include <arpa/inet.h> |
62 | 7d3505c5 | bellard | #ifdef _BSD
|
63 | 7d3505c5 | bellard | #include <sys/stat.h> |
64 | 83fb7adf | bellard | #ifndef __APPLE__
|
65 | 7d3505c5 | bellard | #include <libutil.h> |
66 | 83fb7adf | bellard | #endif
|
67 | 5c40d2bd | ths | #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
|
68 | 5c40d2bd | ths | #include <freebsd/stdlib.h> |
69 | 7d3505c5 | bellard | #else
|
70 | ec530c81 | bellard | #ifndef __sun__
|
71 | f1510b2c | bellard | #include <linux/if.h> |
72 | f1510b2c | bellard | #include <linux/if_tun.h> |
73 | 7d3505c5 | bellard | #include <pty.h> |
74 | 7d3505c5 | bellard | #include <malloc.h> |
75 | fd872598 | bellard | #include <linux/rtc.h> |
76 | bd494f4c | ths | |
77 | bd494f4c | ths | /* For the benefit of older linux systems which don't supply it,
|
78 | bd494f4c | ths | we use a local copy of hpet.h. */
|
79 | bd494f4c | ths | /* #include <linux/hpet.h> */
|
80 | bd494f4c | ths | #include "hpet.h" |
81 | bd494f4c | ths | |
82 | e57a8c0e | bellard | #include <linux/ppdev.h> |
83 | 5867c88a | ths | #include <linux/parport.h> |
84 | d5d10bc3 | ths | #else
|
85 | d5d10bc3 | ths | #include <sys/stat.h> |
86 | d5d10bc3 | ths | #include <sys/ethernet.h> |
87 | d5d10bc3 | ths | #include <sys/sockio.h> |
88 | d5d10bc3 | ths | #include <netinet/arp.h> |
89 | d5d10bc3 | ths | #include <netinet/in.h> |
90 | d5d10bc3 | ths | #include <netinet/in_systm.h> |
91 | d5d10bc3 | ths | #include <netinet/ip.h> |
92 | d5d10bc3 | ths | #include <netinet/ip_icmp.h> // must come after ip.h |
93 | d5d10bc3 | ths | #include <netinet/udp.h> |
94 | d5d10bc3 | ths | #include <netinet/tcp.h> |
95 | d5d10bc3 | ths | #include <net/if.h> |
96 | d5d10bc3 | ths | #include <syslog.h> |
97 | d5d10bc3 | ths | #include <stropts.h> |
98 | 67b915a5 | bellard | #endif
|
99 | 7d3505c5 | bellard | #endif
|
100 | cb4b976b | ths | #else
|
101 | cb4b976b | ths | #include <winsock2.h> |
102 | cb4b976b | ths | int inet_aton(const char *cp, struct in_addr *ia); |
103 | ec530c81 | bellard | #endif
|
104 | 67b915a5 | bellard | |
105 | c20709aa | bellard | #if defined(CONFIG_SLIRP)
|
106 | c20709aa | bellard | #include "libslirp.h" |
107 | c20709aa | bellard | #endif
|
108 | c20709aa | bellard | |
109 | 8a16d273 | ths | #if defined(CONFIG_VDE)
|
110 | 8a16d273 | ths | #include <libvdeplug.h> |
111 | 8a16d273 | ths | #endif
|
112 | 8a16d273 | ths | |
113 | 67b915a5 | bellard | #ifdef _WIN32
|
114 | 7d3505c5 | bellard | #include <malloc.h> |
115 | 67b915a5 | bellard | #include <sys/timeb.h> |
116 | 4fddf62a | ths | #include <mmsystem.h> |
117 | 67b915a5 | bellard | #define getopt_long_only getopt_long
|
118 | 67b915a5 | bellard | #define memalign(align, size) malloc(size)
|
119 | 67b915a5 | bellard | #endif
|
120 | 67b915a5 | bellard | |
121 | 6ca957f0 | bellard | #include "qemu_socket.h" |
122 | 6ca957f0 | bellard | |
123 | 73332e5c | bellard | #ifdef CONFIG_SDL
|
124 | 96bcd4f8 | bellard | #ifdef __APPLE__
|
125 | 83fb7adf | bellard | #include <SDL/SDL.h> |
126 | 96bcd4f8 | bellard | #endif
|
127 | 73332e5c | bellard | #endif /* CONFIG_SDL */ |
128 | 0824d6fc | bellard | |
129 | 5b0753e0 | bellard | #ifdef CONFIG_COCOA
|
130 | 5b0753e0 | bellard | #undef main
|
131 | 5b0753e0 | bellard | #define main qemu_main
|
132 | 5b0753e0 | bellard | #endif /* CONFIG_COCOA */ |
133 | 5b0753e0 | bellard | |
134 | 0824d6fc | bellard | #include "disas.h" |
135 | fc01f7e7 | bellard | |
136 | 8a7ddc38 | bellard | #include "exec-all.h" |
137 | 0824d6fc | bellard | |
138 | 5a67135a | bellard | #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
139 | b46a8906 | ths | #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown" |
140 | a14d6c8c | pbrook | #ifdef __sun__
|
141 | a14d6c8c | pbrook | #define SMBD_COMMAND "/usr/sfw/sbin/smbd" |
142 | a14d6c8c | pbrook | #else
|
143 | a14d6c8c | pbrook | #define SMBD_COMMAND "/usr/sbin/smbd" |
144 | a14d6c8c | pbrook | #endif
|
145 | f1510b2c | bellard | |
146 | 0824d6fc | bellard | //#define DEBUG_UNUSED_IOPORT
|
147 | fd872598 | bellard | //#define DEBUG_IOPORT
|
148 | 330d0414 | bellard | |
149 | 77d4bc34 | bellard | #ifdef TARGET_PPC
|
150 | 77d4bc34 | bellard | #define DEFAULT_RAM_SIZE 144 |
151 | 77d4bc34 | bellard | #else
|
152 | 1bfe856e | bellard | #define DEFAULT_RAM_SIZE 128 |
153 | 77d4bc34 | bellard | #endif
|
154 | 8a7ddc38 | bellard | /* in ms */
|
155 | 8a7ddc38 | bellard | #define GUI_REFRESH_INTERVAL 30 |
156 | 313aa567 | bellard | |
157 | 0d92ed30 | pbrook | /* Max number of USB devices that can be specified on the commandline. */
|
158 | 0d92ed30 | pbrook | #define MAX_USB_CMDLINE 8 |
159 | 0d92ed30 | pbrook | |
160 | 7dea1da4 | bellard | /* XXX: use a two level table to limit memory usage */
|
161 | 7dea1da4 | bellard | #define MAX_IOPORTS 65536 |
162 | 0824d6fc | bellard | |
163 | 80cabfad | bellard | const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
164 | 1192dad8 | j_mayer | const char *bios_name = NULL; |
165 | c4b1fcc0 | bellard | void *ioport_opaque[MAX_IOPORTS];
|
166 | fc01f7e7 | bellard | IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
|
167 | fc01f7e7 | bellard | IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
|
168 | e4bcb14c | ths | /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
|
169 | faea38e7 | bellard | to store the VM snapshots */
|
170 | e4bcb14c | ths | DriveInfo drives_table[MAX_DRIVES+1];
|
171 | e4bcb14c | ths | int nb_drives;
|
172 | faea38e7 | bellard | /* point to the block driver where the snapshots are managed */
|
173 | faea38e7 | bellard | BlockDriverState *bs_snapshots; |
174 | 313aa567 | bellard | int vga_ram_size;
|
175 | 313aa567 | bellard | static DisplayState display_state;
|
176 | a20dd508 | bellard | int nographic;
|
177 | 4d3b6f6e | balrog | int curses;
|
178 | 3d11d0eb | bellard | const char* keyboard_layout = NULL; |
179 | 313aa567 | bellard | int64_t ticks_per_sec; |
180 | 00f82b8a | aurel32 | ram_addr_t ram_size; |
181 | 80cabfad | bellard | int pit_min_timer_count = 0; |
182 | c4b1fcc0 | bellard | int nb_nics;
|
183 | 7c9d8e07 | bellard | NICInfo nd_table[MAX_NICS]; |
184 | 8a7ddc38 | bellard | int vm_running;
|
185 | f6503059 | balrog | static int rtc_utc = 1; |
186 | f6503059 | balrog | static int rtc_date_offset = -1; /* -1 means no change */ |
187 | 1bfe856e | bellard | int cirrus_vga_enabled = 1; |
188 | d34cab9f | ths | int vmsvga_enabled = 0; |
189 | d827220b | bellard | #ifdef TARGET_SPARC
|
190 | d827220b | bellard | int graphic_width = 1024; |
191 | d827220b | bellard | int graphic_height = 768; |
192 | eee0b836 | blueswir1 | int graphic_depth = 8; |
193 | d827220b | bellard | #else
|
194 | 1bfe856e | bellard | int graphic_width = 800; |
195 | 1bfe856e | bellard | int graphic_height = 600; |
196 | e9b137c2 | bellard | int graphic_depth = 15; |
197 | eee0b836 | blueswir1 | #endif
|
198 | d63d307f | bellard | int full_screen = 0; |
199 | 43523e93 | ths | int no_frame = 0; |
200 | 667accab | ths | int no_quit = 0; |
201 | 8d11df9e | bellard | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
202 | 6508fe59 | bellard | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
203 | a09db21f | bellard | #ifdef TARGET_I386
|
204 | a09db21f | bellard | int win2k_install_hack = 0; |
205 | a09db21f | bellard | #endif
|
206 | bb36d470 | bellard | int usb_enabled = 0; |
207 | 7c9d8e07 | bellard | static VLANState *first_vlan;
|
208 | 6a00d601 | bellard | int smp_cpus = 1; |
209 | 73fc9742 | ths | const char *vnc_display; |
210 | d3e9db93 | bellard | #if defined(TARGET_SPARC)
|
211 | ba3c64fb | bellard | #define MAX_CPUS 16 |
212 | d3e9db93 | bellard | #elif defined(TARGET_I386)
|
213 | d3e9db93 | bellard | #define MAX_CPUS 255 |
214 | ba3c64fb | bellard | #else
|
215 | d3e9db93 | bellard | #define MAX_CPUS 1 |
216 | ba3c64fb | bellard | #endif
|
217 | 6515b203 | bellard | int acpi_enabled = 1; |
218 | 52ca8d6a | bellard | int fd_bootchk = 1; |
219 | d1beab82 | bellard | int no_reboot = 0; |
220 | b2f76161 | aurel32 | int no_shutdown = 0; |
221 | 9467cd46 | balrog | int cursor_hide = 1; |
222 | a171fe39 | balrog | int graphic_rotate = 0; |
223 | 71e3ceb8 | ths | int daemonize = 0; |
224 | 9ae02555 | ths | const char *option_rom[MAX_OPTION_ROMS]; |
225 | 9ae02555 | ths | int nb_option_roms;
|
226 | 8e71621f | pbrook | int semihosting_enabled = 0; |
227 | 3c07f8e8 | pbrook | int autostart = 1; |
228 | 2b8f2d41 | balrog | #ifdef TARGET_ARM
|
229 | 2b8f2d41 | balrog | int old_param = 0; |
230 | 2b8f2d41 | balrog | #endif
|
231 | c35734b2 | ths | const char *qemu_name; |
232 | 3780e197 | ths | int alt_grab = 0; |
233 | 66508601 | blueswir1 | #ifdef TARGET_SPARC
|
234 | 66508601 | blueswir1 | unsigned int nb_prom_envs = 0; |
235 | 66508601 | blueswir1 | const char *prom_envs[MAX_PROM_ENVS]; |
236 | 66508601 | blueswir1 | #endif
|
237 | e4bcb14c | ths | int nb_drives_opt;
|
238 | 609497ab | balrog | struct drive_opt {
|
239 | 609497ab | balrog | const char *file; |
240 | 609497ab | balrog | char opt[1024]; |
241 | 609497ab | balrog | } drives_opt[MAX_DRIVES]; |
242 | 0824d6fc | bellard | |
243 | ee5605e5 | balrog | static CPUState *cur_cpu;
|
244 | ee5605e5 | balrog | static CPUState *next_cpu;
|
245 | 76ea08f9 | balrog | static int event_pending = 1; |
246 | bf20dc07 | ths | /* Conversion factor from emulated instructions to virtual clock ticks. */
|
247 | 2e70f6ef | pbrook | static int icount_time_shift; |
248 | bf20dc07 | ths | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
|
249 | 2e70f6ef | pbrook | #define MAX_ICOUNT_SHIFT 10 |
250 | 2e70f6ef | pbrook | /* Compensate for varying guest execution speed. */
|
251 | 2e70f6ef | pbrook | static int64_t qemu_icount_bias;
|
252 | 2e70f6ef | pbrook | QEMUTimer *icount_rt_timer; |
253 | 2e70f6ef | pbrook | QEMUTimer *icount_vm_timer; |
254 | ee5605e5 | balrog | |
255 | aeb30be6 | balrog | #define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR) |
256 | aeb30be6 | balrog | |
257 | 0824d6fc | bellard | /***********************************************************/
|
258 | 26aa7d72 | bellard | /* x86 ISA bus support */
|
259 | 26aa7d72 | bellard | |
260 | 26aa7d72 | bellard | target_phys_addr_t isa_mem_base = 0;
|
261 | 3de388f6 | bellard | PicState2 *isa_pic; |
262 | 0824d6fc | bellard | |
263 | 477e3edf | aliguori | static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
|
264 | 477e3edf | aliguori | static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
|
265 | 477e3edf | aliguori | |
266 | 477e3edf | aliguori | static uint32_t ioport_read(int index, uint32_t address) |
267 | 477e3edf | aliguori | { |
268 | 477e3edf | aliguori | static IOPortReadFunc *default_func[3] = { |
269 | 477e3edf | aliguori | default_ioport_readb, |
270 | 477e3edf | aliguori | default_ioport_readw, |
271 | 477e3edf | aliguori | default_ioport_readl |
272 | 477e3edf | aliguori | }; |
273 | 477e3edf | aliguori | IOPortReadFunc *func = ioport_read_table[index][address]; |
274 | 477e3edf | aliguori | if (!func)
|
275 | 477e3edf | aliguori | func = default_func[index]; |
276 | 477e3edf | aliguori | return func(ioport_opaque[address], address);
|
277 | 477e3edf | aliguori | } |
278 | 477e3edf | aliguori | |
279 | 477e3edf | aliguori | static void ioport_write(int index, uint32_t address, uint32_t data) |
280 | 477e3edf | aliguori | { |
281 | 477e3edf | aliguori | static IOPortWriteFunc *default_func[3] = { |
282 | 477e3edf | aliguori | default_ioport_writeb, |
283 | 477e3edf | aliguori | default_ioport_writew, |
284 | 477e3edf | aliguori | default_ioport_writel |
285 | 477e3edf | aliguori | }; |
286 | 477e3edf | aliguori | IOPortWriteFunc *func = ioport_write_table[index][address]; |
287 | 477e3edf | aliguori | if (!func)
|
288 | 477e3edf | aliguori | func = default_func[index]; |
289 | 477e3edf | aliguori | func(ioport_opaque[address], address, data); |
290 | 477e3edf | aliguori | } |
291 | 477e3edf | aliguori | |
292 | 9596ebb7 | pbrook | static uint32_t default_ioport_readb(void *opaque, uint32_t address) |
293 | 0824d6fc | bellard | { |
294 | 0824d6fc | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
295 | 1196be37 | ths | fprintf(stderr, "unused inb: port=0x%04x\n", address);
|
296 | 0824d6fc | bellard | #endif
|
297 | fc01f7e7 | bellard | return 0xff; |
298 | 0824d6fc | bellard | } |
299 | 0824d6fc | bellard | |
300 | 9596ebb7 | pbrook | static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
301 | 0824d6fc | bellard | { |
302 | 0824d6fc | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
303 | 1196be37 | ths | fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
|
304 | 0824d6fc | bellard | #endif
|
305 | 0824d6fc | bellard | } |
306 | 0824d6fc | bellard | |
307 | 0824d6fc | bellard | /* default is to make two byte accesses */
|
308 | 9596ebb7 | pbrook | static uint32_t default_ioport_readw(void *opaque, uint32_t address) |
309 | 0824d6fc | bellard | { |
310 | 0824d6fc | bellard | uint32_t data; |
311 | 477e3edf | aliguori | data = ioport_read(0, address);
|
312 | db45c29a | bellard | address = (address + 1) & (MAX_IOPORTS - 1); |
313 | 477e3edf | aliguori | data |= ioport_read(0, address) << 8; |
314 | 0824d6fc | bellard | return data;
|
315 | 0824d6fc | bellard | } |
316 | 0824d6fc | bellard | |
317 | 9596ebb7 | pbrook | static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
318 | 0824d6fc | bellard | { |
319 | 477e3edf | aliguori | ioport_write(0, address, data & 0xff); |
320 | db45c29a | bellard | address = (address + 1) & (MAX_IOPORTS - 1); |
321 | 477e3edf | aliguori | ioport_write(0, address, (data >> 8) & 0xff); |
322 | 0824d6fc | bellard | } |
323 | 0824d6fc | bellard | |
324 | 9596ebb7 | pbrook | static uint32_t default_ioport_readl(void *opaque, uint32_t address) |
325 | 0824d6fc | bellard | { |
326 | fc01f7e7 | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
327 | 1196be37 | ths | fprintf(stderr, "unused inl: port=0x%04x\n", address);
|
328 | fc01f7e7 | bellard | #endif
|
329 | fc01f7e7 | bellard | return 0xffffffff; |
330 | 0824d6fc | bellard | } |
331 | 0824d6fc | bellard | |
332 | 9596ebb7 | pbrook | static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
333 | 0824d6fc | bellard | { |
334 | fc01f7e7 | bellard | #ifdef DEBUG_UNUSED_IOPORT
|
335 | 1196be37 | ths | fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
|
336 | fc01f7e7 | bellard | #endif
|
337 | 0824d6fc | bellard | } |
338 | 0824d6fc | bellard | |
339 | fc01f7e7 | bellard | /* size is the word size in byte */
|
340 | 5fafdf24 | ths | int register_ioport_read(int start, int length, int size, |
341 | c4b1fcc0 | bellard | IOPortReadFunc *func, void *opaque)
|
342 | f1510b2c | bellard | { |
343 | fc01f7e7 | bellard | int i, bsize;
|
344 | f1510b2c | bellard | |
345 | c4b1fcc0 | bellard | if (size == 1) { |
346 | fc01f7e7 | bellard | bsize = 0;
|
347 | c4b1fcc0 | bellard | } else if (size == 2) { |
348 | fc01f7e7 | bellard | bsize = 1;
|
349 | c4b1fcc0 | bellard | } else if (size == 4) { |
350 | fc01f7e7 | bellard | bsize = 2;
|
351 | c4b1fcc0 | bellard | } else {
|
352 | 88fdf56f | balrog | hw_error("register_ioport_read: invalid size");
|
353 | fc01f7e7 | bellard | return -1; |
354 | c4b1fcc0 | bellard | } |
355 | c4b1fcc0 | bellard | for(i = start; i < start + length; i += size) {
|
356 | fc01f7e7 | bellard | ioport_read_table[bsize][i] = func; |
357 | c4b1fcc0 | bellard | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
358 | 88fdf56f | balrog | hw_error("register_ioport_read: invalid opaque");
|
359 | c4b1fcc0 | bellard | ioport_opaque[i] = opaque; |
360 | c4b1fcc0 | bellard | } |
361 | f1510b2c | bellard | return 0; |
362 | f1510b2c | bellard | } |
363 | f1510b2c | bellard | |
364 | fc01f7e7 | bellard | /* size is the word size in byte */
|
365 | 5fafdf24 | ths | int register_ioport_write(int start, int length, int size, |
366 | c4b1fcc0 | bellard | IOPortWriteFunc *func, void *opaque)
|
367 | f1510b2c | bellard | { |
368 | fc01f7e7 | bellard | int i, bsize;
|
369 | f1510b2c | bellard | |
370 | c4b1fcc0 | bellard | if (size == 1) { |
371 | fc01f7e7 | bellard | bsize = 0;
|
372 | c4b1fcc0 | bellard | } else if (size == 2) { |
373 | fc01f7e7 | bellard | bsize = 1;
|
374 | c4b1fcc0 | bellard | } else if (size == 4) { |
375 | fc01f7e7 | bellard | bsize = 2;
|
376 | c4b1fcc0 | bellard | } else {
|
377 | 88fdf56f | balrog | hw_error("register_ioport_write: invalid size");
|
378 | fc01f7e7 | bellard | return -1; |
379 | c4b1fcc0 | bellard | } |
380 | c4b1fcc0 | bellard | for(i = start; i < start + length; i += size) {
|
381 | fc01f7e7 | bellard | ioport_write_table[bsize][i] = func; |
382 | 88fdf56f | balrog | if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
383 | 88fdf56f | balrog | hw_error("register_ioport_write: invalid opaque");
|
384 | c4b1fcc0 | bellard | ioport_opaque[i] = opaque; |
385 | c4b1fcc0 | bellard | } |
386 | f1510b2c | bellard | return 0; |
387 | f1510b2c | bellard | } |
388 | f1510b2c | bellard | |
389 | 69b91039 | bellard | void isa_unassign_ioport(int start, int length) |
390 | 69b91039 | bellard | { |
391 | 69b91039 | bellard | int i;
|
392 | 69b91039 | bellard | |
393 | 69b91039 | bellard | for(i = start; i < start + length; i++) {
|
394 | 69b91039 | bellard | ioport_read_table[0][i] = default_ioport_readb;
|
395 | 69b91039 | bellard | ioport_read_table[1][i] = default_ioport_readw;
|
396 | 69b91039 | bellard | ioport_read_table[2][i] = default_ioport_readl;
|
397 | 69b91039 | bellard | |
398 | 69b91039 | bellard | ioport_write_table[0][i] = default_ioport_writeb;
|
399 | 69b91039 | bellard | ioport_write_table[1][i] = default_ioport_writew;
|
400 | 69b91039 | bellard | ioport_write_table[2][i] = default_ioport_writel;
|
401 | 69b91039 | bellard | } |
402 | 69b91039 | bellard | } |
403 | 69b91039 | bellard | |
404 | 20f32282 | bellard | /***********************************************************/
|
405 | 20f32282 | bellard | |
406 | c45886db | bellard | void cpu_outb(CPUState *env, int addr, int val) |
407 | 0824d6fc | bellard | { |
408 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
409 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
410 | fd872598 | bellard | fprintf(logfile, "outb: %04x %02x\n", addr, val);
|
411 | 3b46e624 | ths | #endif
|
412 | 477e3edf | aliguori | ioport_write(0, addr, val);
|
413 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
414 | 89bfc105 | bellard | if (env)
|
415 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
416 | 89bfc105 | bellard | #endif
|
417 | 0824d6fc | bellard | } |
418 | 0824d6fc | bellard | |
419 | c45886db | bellard | void cpu_outw(CPUState *env, int addr, int val) |
420 | 0824d6fc | bellard | { |
421 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
422 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
423 | fd872598 | bellard | fprintf(logfile, "outw: %04x %04x\n", addr, val);
|
424 | 3b46e624 | ths | #endif
|
425 | 477e3edf | aliguori | ioport_write(1, addr, val);
|
426 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
427 | 89bfc105 | bellard | if (env)
|
428 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
429 | 89bfc105 | bellard | #endif
|
430 | 0824d6fc | bellard | } |
431 | 0824d6fc | bellard | |
432 | c45886db | bellard | void cpu_outl(CPUState *env, int addr, int val) |
433 | 0824d6fc | bellard | { |
434 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
435 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
436 | fd872598 | bellard | fprintf(logfile, "outl: %04x %08x\n", addr, val);
|
437 | fd872598 | bellard | #endif
|
438 | 477e3edf | aliguori | ioport_write(2, addr, val);
|
439 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
440 | 89bfc105 | bellard | if (env)
|
441 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
442 | 89bfc105 | bellard | #endif
|
443 | 0824d6fc | bellard | } |
444 | 0824d6fc | bellard | |
445 | c45886db | bellard | int cpu_inb(CPUState *env, int addr) |
446 | 0824d6fc | bellard | { |
447 | fd872598 | bellard | int val;
|
448 | 477e3edf | aliguori | val = ioport_read(0, addr);
|
449 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
450 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
451 | fd872598 | bellard | fprintf(logfile, "inb : %04x %02x\n", addr, val);
|
452 | fd872598 | bellard | #endif
|
453 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
454 | 89bfc105 | bellard | if (env)
|
455 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
456 | 89bfc105 | bellard | #endif
|
457 | fd872598 | bellard | return val;
|
458 | 0824d6fc | bellard | } |
459 | 0824d6fc | bellard | |
460 | c45886db | bellard | int cpu_inw(CPUState *env, int addr) |
461 | 0824d6fc | bellard | { |
462 | fd872598 | bellard | int val;
|
463 | 477e3edf | aliguori | val = ioport_read(1, addr);
|
464 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
465 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
466 | fd872598 | bellard | fprintf(logfile, "inw : %04x %04x\n", addr, val);
|
467 | fd872598 | bellard | #endif
|
468 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
469 | 89bfc105 | bellard | if (env)
|
470 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
471 | 89bfc105 | bellard | #endif
|
472 | fd872598 | bellard | return val;
|
473 | 0824d6fc | bellard | } |
474 | 0824d6fc | bellard | |
475 | c45886db | bellard | int cpu_inl(CPUState *env, int addr) |
476 | 0824d6fc | bellard | { |
477 | fd872598 | bellard | int val;
|
478 | 477e3edf | aliguori | val = ioport_read(2, addr);
|
479 | fd872598 | bellard | #ifdef DEBUG_IOPORT
|
480 | fd872598 | bellard | if (loglevel & CPU_LOG_IOPORT)
|
481 | fd872598 | bellard | fprintf(logfile, "inl : %04x %08x\n", addr, val);
|
482 | fd872598 | bellard | #endif
|
483 | 89bfc105 | bellard | #ifdef USE_KQEMU
|
484 | 89bfc105 | bellard | if (env)
|
485 | 89bfc105 | bellard | env->last_io_time = cpu_get_time_fast(); |
486 | 89bfc105 | bellard | #endif
|
487 | fd872598 | bellard | return val;
|
488 | 0824d6fc | bellard | } |
489 | 0824d6fc | bellard | |
490 | 0824d6fc | bellard | /***********************************************************/
|
491 | 0824d6fc | bellard | void hw_error(const char *fmt, ...) |
492 | 0824d6fc | bellard | { |
493 | 0824d6fc | bellard | va_list ap; |
494 | 6a00d601 | bellard | CPUState *env; |
495 | 0824d6fc | bellard | |
496 | 0824d6fc | bellard | va_start(ap, fmt); |
497 | 0824d6fc | bellard | fprintf(stderr, "qemu: hardware error: ");
|
498 | 0824d6fc | bellard | vfprintf(stderr, fmt, ap); |
499 | 0824d6fc | bellard | fprintf(stderr, "\n");
|
500 | 6a00d601 | bellard | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
501 | 6a00d601 | bellard | fprintf(stderr, "CPU #%d:\n", env->cpu_index);
|
502 | 0824d6fc | bellard | #ifdef TARGET_I386
|
503 | 6a00d601 | bellard | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
504 | c45886db | bellard | #else
|
505 | 6a00d601 | bellard | cpu_dump_state(env, stderr, fprintf, 0);
|
506 | 0824d6fc | bellard | #endif
|
507 | 6a00d601 | bellard | } |
508 | 0824d6fc | bellard | va_end(ap); |
509 | 0824d6fc | bellard | abort(); |
510 | 0824d6fc | bellard | } |
511 | 0824d6fc | bellard | |
512 | 8a7ddc38 | bellard | /***********************************************************/
|
513 | 63066f4f | bellard | /* keyboard/mouse */
|
514 | 63066f4f | bellard | |
515 | 63066f4f | bellard | static QEMUPutKBDEvent *qemu_put_kbd_event;
|
516 | 63066f4f | bellard | static void *qemu_put_kbd_event_opaque; |
517 | 455204eb | ths | static QEMUPutMouseEntry *qemu_put_mouse_event_head;
|
518 | 455204eb | ths | static QEMUPutMouseEntry *qemu_put_mouse_event_current;
|
519 | 63066f4f | bellard | |
520 | 63066f4f | bellard | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
521 | 63066f4f | bellard | { |
522 | 63066f4f | bellard | qemu_put_kbd_event_opaque = opaque; |
523 | 63066f4f | bellard | qemu_put_kbd_event = func; |
524 | 63066f4f | bellard | } |
525 | 63066f4f | bellard | |
526 | 455204eb | ths | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
527 | 455204eb | ths | void *opaque, int absolute, |
528 | 455204eb | ths | const char *name) |
529 | 63066f4f | bellard | { |
530 | 455204eb | ths | QEMUPutMouseEntry *s, *cursor; |
531 | 455204eb | ths | |
532 | 455204eb | ths | s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
|
533 | 455204eb | ths | if (!s)
|
534 | 455204eb | ths | return NULL; |
535 | 455204eb | ths | |
536 | 455204eb | ths | s->qemu_put_mouse_event = func; |
537 | 455204eb | ths | s->qemu_put_mouse_event_opaque = opaque; |
538 | 455204eb | ths | s->qemu_put_mouse_event_absolute = absolute; |
539 | 455204eb | ths | s->qemu_put_mouse_event_name = qemu_strdup(name); |
540 | 455204eb | ths | s->next = NULL;
|
541 | 455204eb | ths | |
542 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
543 | 455204eb | ths | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
544 | 455204eb | ths | return s;
|
545 | 455204eb | ths | } |
546 | 455204eb | ths | |
547 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
548 | 455204eb | ths | while (cursor->next != NULL) |
549 | 455204eb | ths | cursor = cursor->next; |
550 | 455204eb | ths | |
551 | 455204eb | ths | cursor->next = s; |
552 | 455204eb | ths | qemu_put_mouse_event_current = s; |
553 | 455204eb | ths | |
554 | 455204eb | ths | return s;
|
555 | 455204eb | ths | } |
556 | 455204eb | ths | |
557 | 455204eb | ths | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
|
558 | 455204eb | ths | { |
559 | 455204eb | ths | QEMUPutMouseEntry *prev = NULL, *cursor;
|
560 | 455204eb | ths | |
561 | 455204eb | ths | if (!qemu_put_mouse_event_head || entry == NULL) |
562 | 455204eb | ths | return;
|
563 | 455204eb | ths | |
564 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
565 | 455204eb | ths | while (cursor != NULL && cursor != entry) { |
566 | 455204eb | ths | prev = cursor; |
567 | 455204eb | ths | cursor = cursor->next; |
568 | 455204eb | ths | } |
569 | 455204eb | ths | |
570 | 455204eb | ths | if (cursor == NULL) // does not exist or list empty |
571 | 455204eb | ths | return;
|
572 | 455204eb | ths | else if (prev == NULL) { // entry is head |
573 | 455204eb | ths | qemu_put_mouse_event_head = cursor->next; |
574 | 455204eb | ths | if (qemu_put_mouse_event_current == entry)
|
575 | 455204eb | ths | qemu_put_mouse_event_current = cursor->next; |
576 | 455204eb | ths | qemu_free(entry->qemu_put_mouse_event_name); |
577 | 455204eb | ths | qemu_free(entry); |
578 | 455204eb | ths | return;
|
579 | 455204eb | ths | } |
580 | 455204eb | ths | |
581 | 455204eb | ths | prev->next = entry->next; |
582 | 455204eb | ths | |
583 | 455204eb | ths | if (qemu_put_mouse_event_current == entry)
|
584 | 455204eb | ths | qemu_put_mouse_event_current = prev; |
585 | 455204eb | ths | |
586 | 455204eb | ths | qemu_free(entry->qemu_put_mouse_event_name); |
587 | 455204eb | ths | qemu_free(entry); |
588 | 63066f4f | bellard | } |
589 | 63066f4f | bellard | |
590 | 63066f4f | bellard | void kbd_put_keycode(int keycode) |
591 | 63066f4f | bellard | { |
592 | 63066f4f | bellard | if (qemu_put_kbd_event) {
|
593 | 63066f4f | bellard | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
594 | 63066f4f | bellard | } |
595 | 63066f4f | bellard | } |
596 | 63066f4f | bellard | |
597 | 63066f4f | bellard | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
598 | 63066f4f | bellard | { |
599 | 455204eb | ths | QEMUPutMouseEvent *mouse_event; |
600 | 455204eb | ths | void *mouse_event_opaque;
|
601 | a171fe39 | balrog | int width;
|
602 | 455204eb | ths | |
603 | 455204eb | ths | if (!qemu_put_mouse_event_current) {
|
604 | 455204eb | ths | return;
|
605 | 455204eb | ths | } |
606 | 455204eb | ths | |
607 | 455204eb | ths | mouse_event = |
608 | 455204eb | ths | qemu_put_mouse_event_current->qemu_put_mouse_event; |
609 | 455204eb | ths | mouse_event_opaque = |
610 | 455204eb | ths | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
611 | 455204eb | ths | |
612 | 455204eb | ths | if (mouse_event) {
|
613 | a171fe39 | balrog | if (graphic_rotate) {
|
614 | a171fe39 | balrog | if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
|
615 | a171fe39 | balrog | width = 0x7fff;
|
616 | a171fe39 | balrog | else
|
617 | b94ed577 | aurel32 | width = graphic_width - 1;
|
618 | a171fe39 | balrog | mouse_event(mouse_event_opaque, |
619 | a171fe39 | balrog | width - dy, dx, dz, buttons_state); |
620 | a171fe39 | balrog | } else
|
621 | a171fe39 | balrog | mouse_event(mouse_event_opaque, |
622 | a171fe39 | balrog | dx, dy, dz, buttons_state); |
623 | 63066f4f | bellard | } |
624 | 63066f4f | bellard | } |
625 | 63066f4f | bellard | |
626 | 09b26c5e | bellard | int kbd_mouse_is_absolute(void) |
627 | 09b26c5e | bellard | { |
628 | 455204eb | ths | if (!qemu_put_mouse_event_current)
|
629 | 455204eb | ths | return 0; |
630 | 455204eb | ths | |
631 | 455204eb | ths | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
|
632 | 455204eb | ths | } |
633 | 455204eb | ths | |
634 | 455204eb | ths | void do_info_mice(void) |
635 | 455204eb | ths | { |
636 | 455204eb | ths | QEMUPutMouseEntry *cursor; |
637 | 455204eb | ths | int index = 0; |
638 | 455204eb | ths | |
639 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
640 | 455204eb | ths | term_printf("No mouse devices connected\n");
|
641 | 455204eb | ths | return;
|
642 | 455204eb | ths | } |
643 | 455204eb | ths | |
644 | 455204eb | ths | term_printf("Mouse devices available:\n");
|
645 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
646 | 455204eb | ths | while (cursor != NULL) { |
647 | 455204eb | ths | term_printf("%c Mouse #%d: %s\n",
|
648 | 455204eb | ths | (cursor == qemu_put_mouse_event_current ? '*' : ' '), |
649 | 455204eb | ths | index, cursor->qemu_put_mouse_event_name); |
650 | 455204eb | ths | index++; |
651 | 455204eb | ths | cursor = cursor->next; |
652 | 455204eb | ths | } |
653 | 455204eb | ths | } |
654 | 455204eb | ths | |
655 | 455204eb | ths | void do_mouse_set(int index) |
656 | 455204eb | ths | { |
657 | 455204eb | ths | QEMUPutMouseEntry *cursor; |
658 | 455204eb | ths | int i = 0; |
659 | 455204eb | ths | |
660 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
661 | 455204eb | ths | term_printf("No mouse devices connected\n");
|
662 | 455204eb | ths | return;
|
663 | 455204eb | ths | } |
664 | 455204eb | ths | |
665 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
666 | 455204eb | ths | while (cursor != NULL && index != i) { |
667 | 455204eb | ths | i++; |
668 | 455204eb | ths | cursor = cursor->next; |
669 | 455204eb | ths | } |
670 | 455204eb | ths | |
671 | 455204eb | ths | if (cursor != NULL) |
672 | 455204eb | ths | qemu_put_mouse_event_current = cursor; |
673 | 455204eb | ths | else
|
674 | 455204eb | ths | term_printf("Mouse at given index not found\n");
|
675 | 09b26c5e | bellard | } |
676 | 09b26c5e | bellard | |
677 | 1dce7c3c | bellard | /* compute with 96 bit intermediate result: (a*b)/c */
|
678 | 1dce7c3c | bellard | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
679 | 0824d6fc | bellard | { |
680 | 1dce7c3c | bellard | union {
|
681 | 1dce7c3c | bellard | uint64_t ll; |
682 | 1dce7c3c | bellard | struct {
|
683 | 1dce7c3c | bellard | #ifdef WORDS_BIGENDIAN
|
684 | 1dce7c3c | bellard | uint32_t high, low; |
685 | 1dce7c3c | bellard | #else
|
686 | 1dce7c3c | bellard | uint32_t low, high; |
687 | 3b46e624 | ths | #endif
|
688 | 1dce7c3c | bellard | } l; |
689 | 1dce7c3c | bellard | } u, res; |
690 | 1dce7c3c | bellard | uint64_t rl, rh; |
691 | 0824d6fc | bellard | |
692 | 1dce7c3c | bellard | u.ll = a; |
693 | 1dce7c3c | bellard | rl = (uint64_t)u.l.low * (uint64_t)b; |
694 | 1dce7c3c | bellard | rh = (uint64_t)u.l.high * (uint64_t)b; |
695 | 1dce7c3c | bellard | rh += (rl >> 32);
|
696 | 1dce7c3c | bellard | res.l.high = rh / c; |
697 | 1dce7c3c | bellard | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
698 | 1dce7c3c | bellard | return res.ll;
|
699 | 34865134 | bellard | } |
700 | 34865134 | bellard | |
701 | 1dce7c3c | bellard | /***********************************************************/
|
702 | 1dce7c3c | bellard | /* real time host monotonic timer */
|
703 | 34865134 | bellard | |
704 | 1dce7c3c | bellard | #define QEMU_TIMER_BASE 1000000000LL |
705 | 34865134 | bellard | |
706 | 1dce7c3c | bellard | #ifdef WIN32
|
707 | 0824d6fc | bellard | |
708 | 1dce7c3c | bellard | static int64_t clock_freq;
|
709 | 1115dde7 | bellard | |
710 | 1dce7c3c | bellard | static void init_get_clock(void) |
711 | 1115dde7 | bellard | { |
712 | a8e5ac33 | bellard | LARGE_INTEGER freq; |
713 | a8e5ac33 | bellard | int ret;
|
714 | 1dce7c3c | bellard | ret = QueryPerformanceFrequency(&freq); |
715 | 1dce7c3c | bellard | if (ret == 0) { |
716 | 1dce7c3c | bellard | fprintf(stderr, "Could not calibrate ticks\n");
|
717 | 1dce7c3c | bellard | exit(1);
|
718 | 1dce7c3c | bellard | } |
719 | 1dce7c3c | bellard | clock_freq = freq.QuadPart; |
720 | 1115dde7 | bellard | } |
721 | 1115dde7 | bellard | |
722 | 1dce7c3c | bellard | static int64_t get_clock(void) |
723 | b8076a74 | bellard | { |
724 | 1dce7c3c | bellard | LARGE_INTEGER ti; |
725 | 1dce7c3c | bellard | QueryPerformanceCounter(&ti); |
726 | 1dce7c3c | bellard | return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
|
727 | b8076a74 | bellard | } |
728 | b8076a74 | bellard | |
729 | 1dce7c3c | bellard | #else
|
730 | 90cb9493 | bellard | |
731 | 1dce7c3c | bellard | static int use_rt_clock; |
732 | 1dce7c3c | bellard | |
733 | 1dce7c3c | bellard | static void init_get_clock(void) |
734 | 90cb9493 | bellard | { |
735 | 1dce7c3c | bellard | use_rt_clock = 0;
|
736 | 1dce7c3c | bellard | #if defined(__linux__)
|
737 | 1dce7c3c | bellard | { |
738 | 1dce7c3c | bellard | struct timespec ts;
|
739 | 1dce7c3c | bellard | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
740 | 1dce7c3c | bellard | use_rt_clock = 1;
|
741 | 1dce7c3c | bellard | } |
742 | 1dce7c3c | bellard | } |
743 | 1dce7c3c | bellard | #endif
|
744 | 90cb9493 | bellard | } |
745 | 90cb9493 | bellard | |
746 | 1dce7c3c | bellard | static int64_t get_clock(void) |
747 | fdbb4691 | bellard | { |
748 | 1dce7c3c | bellard | #if defined(__linux__)
|
749 | 1dce7c3c | bellard | if (use_rt_clock) {
|
750 | 1dce7c3c | bellard | struct timespec ts;
|
751 | 1dce7c3c | bellard | clock_gettime(CLOCK_MONOTONIC, &ts); |
752 | 1dce7c3c | bellard | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
753 | 5fafdf24 | ths | } else
|
754 | fdbb4691 | bellard | #endif
|
755 | 1dce7c3c | bellard | { |
756 | 1dce7c3c | bellard | /* XXX: using gettimeofday leads to problems if the date
|
757 | 1dce7c3c | bellard | changes, so it should be avoided. */
|
758 | 1dce7c3c | bellard | struct timeval tv;
|
759 | 1dce7c3c | bellard | gettimeofday(&tv, NULL);
|
760 | 1dce7c3c | bellard | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
761 | 1dce7c3c | bellard | } |
762 | fdbb4691 | bellard | } |
763 | 34865134 | bellard | #endif
|
764 | 34865134 | bellard | |
765 | 2e70f6ef | pbrook | /* Return the virtual CPU time, based on the instruction counter. */
|
766 | 2e70f6ef | pbrook | static int64_t cpu_get_icount(void) |
767 | 2e70f6ef | pbrook | { |
768 | 2e70f6ef | pbrook | int64_t icount; |
769 | 2e70f6ef | pbrook | CPUState *env = cpu_single_env;; |
770 | 2e70f6ef | pbrook | icount = qemu_icount; |
771 | 2e70f6ef | pbrook | if (env) {
|
772 | 2e70f6ef | pbrook | if (!can_do_io(env))
|
773 | 2e70f6ef | pbrook | fprintf(stderr, "Bad clock read\n");
|
774 | 2e70f6ef | pbrook | icount -= (env->icount_decr.u16.low + env->icount_extra); |
775 | 2e70f6ef | pbrook | } |
776 | 2e70f6ef | pbrook | return qemu_icount_bias + (icount << icount_time_shift);
|
777 | 2e70f6ef | pbrook | } |
778 | 2e70f6ef | pbrook | |
779 | 1dce7c3c | bellard | /***********************************************************/
|
780 | 1dce7c3c | bellard | /* guest cycle counter */
|
781 | 1dce7c3c | bellard | |
782 | eade0f19 | bellard | static int64_t cpu_ticks_prev;
|
783 | 34865134 | bellard | static int64_t cpu_ticks_offset;
|
784 | 1dce7c3c | bellard | static int64_t cpu_clock_offset;
|
785 | 8a7ddc38 | bellard | static int cpu_ticks_enabled; |
786 | 34865134 | bellard | |
787 | 1dce7c3c | bellard | /* return the host CPU cycle counter and handle stop/restart */
|
788 | 1dce7c3c | bellard | int64_t cpu_get_ticks(void)
|
789 | 34865134 | bellard | { |
790 | 2e70f6ef | pbrook | if (use_icount) {
|
791 | 2e70f6ef | pbrook | return cpu_get_icount();
|
792 | 2e70f6ef | pbrook | } |
793 | 8a7ddc38 | bellard | if (!cpu_ticks_enabled) {
|
794 | 8a7ddc38 | bellard | return cpu_ticks_offset;
|
795 | 8a7ddc38 | bellard | } else {
|
796 | eade0f19 | bellard | int64_t ticks; |
797 | eade0f19 | bellard | ticks = cpu_get_real_ticks(); |
798 | eade0f19 | bellard | if (cpu_ticks_prev > ticks) {
|
799 | eade0f19 | bellard | /* Note: non increasing ticks may happen if the host uses
|
800 | eade0f19 | bellard | software suspend */
|
801 | eade0f19 | bellard | cpu_ticks_offset += cpu_ticks_prev - ticks; |
802 | eade0f19 | bellard | } |
803 | eade0f19 | bellard | cpu_ticks_prev = ticks; |
804 | eade0f19 | bellard | return ticks + cpu_ticks_offset;
|
805 | 8a7ddc38 | bellard | } |
806 | 34865134 | bellard | } |
807 | 34865134 | bellard | |
808 | 1dce7c3c | bellard | /* return the host CPU monotonic timer and handle stop/restart */
|
809 | 1dce7c3c | bellard | static int64_t cpu_get_clock(void) |
810 | 1dce7c3c | bellard | { |
811 | 1dce7c3c | bellard | int64_t ti; |
812 | 1dce7c3c | bellard | if (!cpu_ticks_enabled) {
|
813 | 1dce7c3c | bellard | return cpu_clock_offset;
|
814 | 1dce7c3c | bellard | } else {
|
815 | 1dce7c3c | bellard | ti = get_clock(); |
816 | 1dce7c3c | bellard | return ti + cpu_clock_offset;
|
817 | 1dce7c3c | bellard | } |
818 | 1dce7c3c | bellard | } |
819 | 1dce7c3c | bellard | |
820 | 34865134 | bellard | /* enable cpu_get_ticks() */
|
821 | 34865134 | bellard | void cpu_enable_ticks(void) |
822 | 34865134 | bellard | { |
823 | 8a7ddc38 | bellard | if (!cpu_ticks_enabled) {
|
824 | 8a7ddc38 | bellard | cpu_ticks_offset -= cpu_get_real_ticks(); |
825 | 1dce7c3c | bellard | cpu_clock_offset -= get_clock(); |
826 | 8a7ddc38 | bellard | cpu_ticks_enabled = 1;
|
827 | 8a7ddc38 | bellard | } |
828 | 34865134 | bellard | } |
829 | 34865134 | bellard | |
830 | 34865134 | bellard | /* disable cpu_get_ticks() : the clock is stopped. You must not call
|
831 | 34865134 | bellard | cpu_get_ticks() after that. */
|
832 | 34865134 | bellard | void cpu_disable_ticks(void) |
833 | 34865134 | bellard | { |
834 | 8a7ddc38 | bellard | if (cpu_ticks_enabled) {
|
835 | 8a7ddc38 | bellard | cpu_ticks_offset = cpu_get_ticks(); |
836 | 1dce7c3c | bellard | cpu_clock_offset = cpu_get_clock(); |
837 | 8a7ddc38 | bellard | cpu_ticks_enabled = 0;
|
838 | 8a7ddc38 | bellard | } |
839 | 34865134 | bellard | } |
840 | 34865134 | bellard | |
841 | 1dce7c3c | bellard | /***********************************************************/
|
842 | 1dce7c3c | bellard | /* timers */
|
843 | 5fafdf24 | ths | |
844 | 8a7ddc38 | bellard | #define QEMU_TIMER_REALTIME 0 |
845 | 8a7ddc38 | bellard | #define QEMU_TIMER_VIRTUAL 1 |
846 | 8a7ddc38 | bellard | |
847 | 8a7ddc38 | bellard | struct QEMUClock {
|
848 | 8a7ddc38 | bellard | int type;
|
849 | 8a7ddc38 | bellard | /* XXX: add frequency */
|
850 | 8a7ddc38 | bellard | }; |
851 | 8a7ddc38 | bellard | |
852 | 8a7ddc38 | bellard | struct QEMUTimer {
|
853 | 8a7ddc38 | bellard | QEMUClock *clock; |
854 | 8a7ddc38 | bellard | int64_t expire_time; |
855 | 8a7ddc38 | bellard | QEMUTimerCB *cb; |
856 | 8a7ddc38 | bellard | void *opaque;
|
857 | 8a7ddc38 | bellard | struct QEMUTimer *next;
|
858 | 8a7ddc38 | bellard | }; |
859 | 8a7ddc38 | bellard | |
860 | c8994013 | ths | struct qemu_alarm_timer {
|
861 | c8994013 | ths | char const *name; |
862 | efe75411 | ths | unsigned int flags; |
863 | c8994013 | ths | |
864 | c8994013 | ths | int (*start)(struct qemu_alarm_timer *t); |
865 | c8994013 | ths | void (*stop)(struct qemu_alarm_timer *t); |
866 | efe75411 | ths | void (*rearm)(struct qemu_alarm_timer *t); |
867 | c8994013 | ths | void *priv;
|
868 | c8994013 | ths | }; |
869 | c8994013 | ths | |
870 | efe75411 | ths | #define ALARM_FLAG_DYNTICKS 0x1 |
871 | d5d08334 | balrog | #define ALARM_FLAG_EXPIRED 0x2 |
872 | efe75411 | ths | |
873 | efe75411 | ths | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
874 | efe75411 | ths | { |
875 | efe75411 | ths | return t->flags & ALARM_FLAG_DYNTICKS;
|
876 | efe75411 | ths | } |
877 | efe75411 | ths | |
878 | efe75411 | ths | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
879 | efe75411 | ths | { |
880 | efe75411 | ths | if (!alarm_has_dynticks(t))
|
881 | efe75411 | ths | return;
|
882 | efe75411 | ths | |
883 | efe75411 | ths | t->rearm(t); |
884 | efe75411 | ths | } |
885 | efe75411 | ths | |
886 | efe75411 | ths | /* TODO: MIN_TIMER_REARM_US should be optimized */
|
887 | efe75411 | ths | #define MIN_TIMER_REARM_US 250 |
888 | efe75411 | ths | |
889 | c8994013 | ths | static struct qemu_alarm_timer *alarm_timer; |
890 | 8a7ddc38 | bellard | |
891 | 40c3bac3 | bellard | #ifdef _WIN32
|
892 | c8994013 | ths | |
893 | c8994013 | ths | struct qemu_alarm_win32 {
|
894 | c8994013 | ths | MMRESULT timerId; |
895 | c8994013 | ths | HANDLE host_alarm; |
896 | c8994013 | ths | unsigned int period; |
897 | c8994013 | ths | } alarm_win32_data = {0, NULL, -1}; |
898 | c8994013 | ths | |
899 | c8994013 | ths | static int win32_start_timer(struct qemu_alarm_timer *t); |
900 | c8994013 | ths | static void win32_stop_timer(struct qemu_alarm_timer *t); |
901 | efe75411 | ths | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
902 | c8994013 | ths | |
903 | 40c3bac3 | bellard | #else
|
904 | c8994013 | ths | |
905 | c8994013 | ths | static int unix_start_timer(struct qemu_alarm_timer *t); |
906 | c8994013 | ths | static void unix_stop_timer(struct qemu_alarm_timer *t); |
907 | c8994013 | ths | |
908 | 231c6586 | ths | #ifdef __linux__
|
909 | 231c6586 | ths | |
910 | efe75411 | ths | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
911 | efe75411 | ths | static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
912 | efe75411 | ths | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
913 | efe75411 | ths | |
914 | c40ec5a9 | ths | static int hpet_start_timer(struct qemu_alarm_timer *t); |
915 | c40ec5a9 | ths | static void hpet_stop_timer(struct qemu_alarm_timer *t); |
916 | c40ec5a9 | ths | |
917 | c8994013 | ths | static int rtc_start_timer(struct qemu_alarm_timer *t); |
918 | c8994013 | ths | static void rtc_stop_timer(struct qemu_alarm_timer *t); |
919 | c8994013 | ths | |
920 | efe75411 | ths | #endif /* __linux__ */ |
921 | 8a7ddc38 | bellard | |
922 | c8994013 | ths | #endif /* _WIN32 */ |
923 | c8994013 | ths | |
924 | 2e70f6ef | pbrook | /* Correlation between real and virtual time is always going to be
|
925 | bf20dc07 | ths | fairly approximate, so ignore small variation.
|
926 | 2e70f6ef | pbrook | When the guest is idle real and virtual time will be aligned in
|
927 | 2e70f6ef | pbrook | the IO wait loop. */
|
928 | 2e70f6ef | pbrook | #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10) |
929 | 2e70f6ef | pbrook | |
930 | 2e70f6ef | pbrook | static void icount_adjust(void) |
931 | 2e70f6ef | pbrook | { |
932 | 2e70f6ef | pbrook | int64_t cur_time; |
933 | 2e70f6ef | pbrook | int64_t cur_icount; |
934 | 2e70f6ef | pbrook | int64_t delta; |
935 | 2e70f6ef | pbrook | static int64_t last_delta;
|
936 | 2e70f6ef | pbrook | /* If the VM is not running, then do nothing. */
|
937 | 2e70f6ef | pbrook | if (!vm_running)
|
938 | 2e70f6ef | pbrook | return;
|
939 | 2e70f6ef | pbrook | |
940 | 2e70f6ef | pbrook | cur_time = cpu_get_clock(); |
941 | 2e70f6ef | pbrook | cur_icount = qemu_get_clock(vm_clock); |
942 | 2e70f6ef | pbrook | delta = cur_icount - cur_time; |
943 | 2e70f6ef | pbrook | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
|
944 | 2e70f6ef | pbrook | if (delta > 0 |
945 | 2e70f6ef | pbrook | && last_delta + ICOUNT_WOBBLE < delta * 2
|
946 | 2e70f6ef | pbrook | && icount_time_shift > 0) {
|
947 | 2e70f6ef | pbrook | /* The guest is getting too far ahead. Slow time down. */
|
948 | 2e70f6ef | pbrook | icount_time_shift--; |
949 | 2e70f6ef | pbrook | } |
950 | 2e70f6ef | pbrook | if (delta < 0 |
951 | 2e70f6ef | pbrook | && last_delta - ICOUNT_WOBBLE > delta * 2
|
952 | 2e70f6ef | pbrook | && icount_time_shift < MAX_ICOUNT_SHIFT) { |
953 | 2e70f6ef | pbrook | /* The guest is getting too far behind. Speed time up. */
|
954 | 2e70f6ef | pbrook | icount_time_shift++; |
955 | 2e70f6ef | pbrook | } |
956 | 2e70f6ef | pbrook | last_delta = delta; |
957 | 2e70f6ef | pbrook | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
958 | 2e70f6ef | pbrook | } |
959 | 2e70f6ef | pbrook | |
960 | 2e70f6ef | pbrook | static void icount_adjust_rt(void * opaque) |
961 | 2e70f6ef | pbrook | { |
962 | 2e70f6ef | pbrook | qemu_mod_timer(icount_rt_timer, |
963 | 2e70f6ef | pbrook | qemu_get_clock(rt_clock) + 1000);
|
964 | 2e70f6ef | pbrook | icount_adjust(); |
965 | 2e70f6ef | pbrook | } |
966 | 2e70f6ef | pbrook | |
967 | 2e70f6ef | pbrook | static void icount_adjust_vm(void * opaque) |
968 | 2e70f6ef | pbrook | { |
969 | 2e70f6ef | pbrook | qemu_mod_timer(icount_vm_timer, |
970 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
|
971 | 2e70f6ef | pbrook | icount_adjust(); |
972 | 2e70f6ef | pbrook | } |
973 | 2e70f6ef | pbrook | |
974 | 2e70f6ef | pbrook | static void init_icount_adjust(void) |
975 | 2e70f6ef | pbrook | { |
976 | 2e70f6ef | pbrook | /* Have both realtime and virtual time triggers for speed adjustment.
|
977 | 2e70f6ef | pbrook | The realtime trigger catches emulated time passing too slowly,
|
978 | 2e70f6ef | pbrook | the virtual time trigger catches emulated time passing too fast.
|
979 | 2e70f6ef | pbrook | Realtime triggers occur even when idle, so use them less frequently
|
980 | 2e70f6ef | pbrook | than VM triggers. */
|
981 | 2e70f6ef | pbrook | icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
|
982 | 2e70f6ef | pbrook | qemu_mod_timer(icount_rt_timer, |
983 | 2e70f6ef | pbrook | qemu_get_clock(rt_clock) + 1000);
|
984 | 2e70f6ef | pbrook | icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
|
985 | 2e70f6ef | pbrook | qemu_mod_timer(icount_vm_timer, |
986 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
|
987 | 2e70f6ef | pbrook | } |
988 | 2e70f6ef | pbrook | |
989 | c8994013 | ths | static struct qemu_alarm_timer alarm_timers[] = { |
990 | efe75411 | ths | #ifndef _WIN32
|
991 | 231c6586 | ths | #ifdef __linux__
|
992 | efe75411 | ths | {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
|
993 | efe75411 | ths | dynticks_stop_timer, dynticks_rearm_timer, NULL},
|
994 | c40ec5a9 | ths | /* HPET - if available - is preferred */
|
995 | efe75411 | ths | {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
996 | c40ec5a9 | ths | /* ...otherwise try RTC */
|
997 | efe75411 | ths | {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
998 | c8994013 | ths | #endif
|
999 | efe75411 | ths | {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
1000 | c8994013 | ths | #else
|
1001 | efe75411 | ths | {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
|
1002 | efe75411 | ths | win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
1003 | efe75411 | ths | {"win32", 0, win32_start_timer, |
1004 | efe75411 | ths | win32_stop_timer, NULL, &alarm_win32_data},
|
1005 | c8994013 | ths | #endif
|
1006 | c8994013 | ths | {NULL, }
|
1007 | c8994013 | ths | }; |
1008 | c8994013 | ths | |
1009 | 3f47aa8c | blueswir1 | static void show_available_alarms(void) |
1010 | f3dcfada | ths | { |
1011 | f3dcfada | ths | int i;
|
1012 | f3dcfada | ths | |
1013 | f3dcfada | ths | printf("Available alarm timers, in order of precedence:\n");
|
1014 | f3dcfada | ths | for (i = 0; alarm_timers[i].name; i++) |
1015 | f3dcfada | ths | printf("%s\n", alarm_timers[i].name);
|
1016 | f3dcfada | ths | } |
1017 | f3dcfada | ths | |
1018 | f3dcfada | ths | static void configure_alarms(char const *opt) |
1019 | f3dcfada | ths | { |
1020 | f3dcfada | ths | int i;
|
1021 | f3dcfada | ths | int cur = 0; |
1022 | f3dcfada | ths | int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1; |
1023 | f3dcfada | ths | char *arg;
|
1024 | f3dcfada | ths | char *name;
|
1025 | 2e70f6ef | pbrook | struct qemu_alarm_timer tmp;
|
1026 | f3dcfada | ths | |
1027 | 3adda04c | aurel32 | if (!strcmp(opt, "?")) { |
1028 | f3dcfada | ths | show_available_alarms(); |
1029 | f3dcfada | ths | exit(0);
|
1030 | f3dcfada | ths | } |
1031 | f3dcfada | ths | |
1032 | f3dcfada | ths | arg = strdup(opt); |
1033 | f3dcfada | ths | |
1034 | f3dcfada | ths | /* Reorder the array */
|
1035 | f3dcfada | ths | name = strtok(arg, ",");
|
1036 | f3dcfada | ths | while (name) {
|
1037 | e2b577e5 | balrog | for (i = 0; i < count && alarm_timers[i].name; i++) { |
1038 | f3dcfada | ths | if (!strcmp(alarm_timers[i].name, name))
|
1039 | f3dcfada | ths | break;
|
1040 | f3dcfada | ths | } |
1041 | f3dcfada | ths | |
1042 | f3dcfada | ths | if (i == count) {
|
1043 | f3dcfada | ths | fprintf(stderr, "Unknown clock %s\n", name);
|
1044 | f3dcfada | ths | goto next;
|
1045 | f3dcfada | ths | } |
1046 | f3dcfada | ths | |
1047 | f3dcfada | ths | if (i < cur)
|
1048 | f3dcfada | ths | /* Ignore */
|
1049 | f3dcfada | ths | goto next;
|
1050 | f3dcfada | ths | |
1051 | f3dcfada | ths | /* Swap */
|
1052 | f3dcfada | ths | tmp = alarm_timers[i]; |
1053 | f3dcfada | ths | alarm_timers[i] = alarm_timers[cur]; |
1054 | f3dcfada | ths | alarm_timers[cur] = tmp; |
1055 | f3dcfada | ths | |
1056 | f3dcfada | ths | cur++; |
1057 | f3dcfada | ths | next:
|
1058 | f3dcfada | ths | name = strtok(NULL, ","); |
1059 | f3dcfada | ths | } |
1060 | f3dcfada | ths | |
1061 | f3dcfada | ths | free(arg); |
1062 | f3dcfada | ths | |
1063 | f3dcfada | ths | if (cur) {
|
1064 | 2e70f6ef | pbrook | /* Disable remaining timers */
|
1065 | f3dcfada | ths | for (i = cur; i < count; i++)
|
1066 | f3dcfada | ths | alarm_timers[i].name = NULL;
|
1067 | 3adda04c | aurel32 | } else {
|
1068 | 3adda04c | aurel32 | show_available_alarms(); |
1069 | 3adda04c | aurel32 | exit(1);
|
1070 | f3dcfada | ths | } |
1071 | f3dcfada | ths | } |
1072 | f3dcfada | ths | |
1073 | c8994013 | ths | QEMUClock *rt_clock; |
1074 | c8994013 | ths | QEMUClock *vm_clock; |
1075 | c8994013 | ths | |
1076 | c8994013 | ths | static QEMUTimer *active_timers[2]; |
1077 | c8994013 | ths | |
1078 | 9596ebb7 | pbrook | static QEMUClock *qemu_new_clock(int type) |
1079 | 8a7ddc38 | bellard | { |
1080 | 8a7ddc38 | bellard | QEMUClock *clock; |
1081 | 8a7ddc38 | bellard | clock = qemu_mallocz(sizeof(QEMUClock));
|
1082 | 8a7ddc38 | bellard | if (!clock)
|
1083 | 8a7ddc38 | bellard | return NULL; |
1084 | 8a7ddc38 | bellard | clock->type = type; |
1085 | 8a7ddc38 | bellard | return clock;
|
1086 | 8a7ddc38 | bellard | } |
1087 | 8a7ddc38 | bellard | |
1088 | 8a7ddc38 | bellard | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
|
1089 | 8a7ddc38 | bellard | { |
1090 | 8a7ddc38 | bellard | QEMUTimer *ts; |
1091 | 8a7ddc38 | bellard | |
1092 | 8a7ddc38 | bellard | ts = qemu_mallocz(sizeof(QEMUTimer));
|
1093 | 8a7ddc38 | bellard | ts->clock = clock; |
1094 | 8a7ddc38 | bellard | ts->cb = cb; |
1095 | 8a7ddc38 | bellard | ts->opaque = opaque; |
1096 | 8a7ddc38 | bellard | return ts;
|
1097 | 8a7ddc38 | bellard | } |
1098 | 8a7ddc38 | bellard | |
1099 | 8a7ddc38 | bellard | void qemu_free_timer(QEMUTimer *ts)
|
1100 | 8a7ddc38 | bellard | { |
1101 | 8a7ddc38 | bellard | qemu_free(ts); |
1102 | 8a7ddc38 | bellard | } |
1103 | 8a7ddc38 | bellard | |
1104 | 8a7ddc38 | bellard | /* stop a timer, but do not dealloc it */
|
1105 | 8a7ddc38 | bellard | void qemu_del_timer(QEMUTimer *ts)
|
1106 | 8a7ddc38 | bellard | { |
1107 | 8a7ddc38 | bellard | QEMUTimer **pt, *t; |
1108 | 8a7ddc38 | bellard | |
1109 | 8a7ddc38 | bellard | /* NOTE: this code must be signal safe because
|
1110 | 8a7ddc38 | bellard | qemu_timer_expired() can be called from a signal. */
|
1111 | 8a7ddc38 | bellard | pt = &active_timers[ts->clock->type]; |
1112 | 8a7ddc38 | bellard | for(;;) {
|
1113 | 8a7ddc38 | bellard | t = *pt; |
1114 | 8a7ddc38 | bellard | if (!t)
|
1115 | 8a7ddc38 | bellard | break;
|
1116 | 8a7ddc38 | bellard | if (t == ts) {
|
1117 | 8a7ddc38 | bellard | *pt = t->next; |
1118 | 8a7ddc38 | bellard | break;
|
1119 | 8a7ddc38 | bellard | } |
1120 | 8a7ddc38 | bellard | pt = &t->next; |
1121 | 8a7ddc38 | bellard | } |
1122 | 8a7ddc38 | bellard | } |
1123 | 8a7ddc38 | bellard | |
1124 | 8a7ddc38 | bellard | /* modify the current timer so that it will be fired when current_time
|
1125 | 8a7ddc38 | bellard | >= expire_time. The corresponding callback will be called. */
|
1126 | 8a7ddc38 | bellard | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
|
1127 | 8a7ddc38 | bellard | { |
1128 | 8a7ddc38 | bellard | QEMUTimer **pt, *t; |
1129 | 8a7ddc38 | bellard | |
1130 | 8a7ddc38 | bellard | qemu_del_timer(ts); |
1131 | 8a7ddc38 | bellard | |
1132 | 8a7ddc38 | bellard | /* add the timer in the sorted list */
|
1133 | 8a7ddc38 | bellard | /* NOTE: this code must be signal safe because
|
1134 | 8a7ddc38 | bellard | qemu_timer_expired() can be called from a signal. */
|
1135 | 8a7ddc38 | bellard | pt = &active_timers[ts->clock->type]; |
1136 | 8a7ddc38 | bellard | for(;;) {
|
1137 | 8a7ddc38 | bellard | t = *pt; |
1138 | 8a7ddc38 | bellard | if (!t)
|
1139 | 8a7ddc38 | bellard | break;
|
1140 | 5fafdf24 | ths | if (t->expire_time > expire_time)
|
1141 | 8a7ddc38 | bellard | break;
|
1142 | 8a7ddc38 | bellard | pt = &t->next; |
1143 | 8a7ddc38 | bellard | } |
1144 | 8a7ddc38 | bellard | ts->expire_time = expire_time; |
1145 | 8a7ddc38 | bellard | ts->next = *pt; |
1146 | 8a7ddc38 | bellard | *pt = ts; |
1147 | d5d08334 | balrog | |
1148 | d5d08334 | balrog | /* Rearm if necessary */
|
1149 | 2e70f6ef | pbrook | if (pt == &active_timers[ts->clock->type]) {
|
1150 | 2e70f6ef | pbrook | if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { |
1151 | 2e70f6ef | pbrook | qemu_rearm_alarm_timer(alarm_timer); |
1152 | 2e70f6ef | pbrook | } |
1153 | 2e70f6ef | pbrook | /* Interrupt execution to force deadline recalculation. */
|
1154 | 2e70f6ef | pbrook | if (use_icount && cpu_single_env) {
|
1155 | 2e70f6ef | pbrook | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
1156 | 2e70f6ef | pbrook | } |
1157 | 2e70f6ef | pbrook | } |
1158 | 8a7ddc38 | bellard | } |
1159 | 8a7ddc38 | bellard | |
1160 | 8a7ddc38 | bellard | int qemu_timer_pending(QEMUTimer *ts)
|
1161 | 8a7ddc38 | bellard | { |
1162 | 8a7ddc38 | bellard | QEMUTimer *t; |
1163 | 8a7ddc38 | bellard | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
1164 | 8a7ddc38 | bellard | if (t == ts)
|
1165 | 8a7ddc38 | bellard | return 1; |
1166 | 8a7ddc38 | bellard | } |
1167 | 8a7ddc38 | bellard | return 0; |
1168 | 8a7ddc38 | bellard | } |
1169 | 8a7ddc38 | bellard | |
1170 | 8a7ddc38 | bellard | static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
1171 | 8a7ddc38 | bellard | { |
1172 | 8a7ddc38 | bellard | if (!timer_head)
|
1173 | 8a7ddc38 | bellard | return 0; |
1174 | 8a7ddc38 | bellard | return (timer_head->expire_time <= current_time);
|
1175 | 8a7ddc38 | bellard | } |
1176 | 8a7ddc38 | bellard | |
1177 | 8a7ddc38 | bellard | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
1178 | 8a7ddc38 | bellard | { |
1179 | 8a7ddc38 | bellard | QEMUTimer *ts; |
1180 | 3b46e624 | ths | |
1181 | 8a7ddc38 | bellard | for(;;) {
|
1182 | 8a7ddc38 | bellard | ts = *ptimer_head; |
1183 | e95c8d51 | bellard | if (!ts || ts->expire_time > current_time)
|
1184 | 8a7ddc38 | bellard | break;
|
1185 | 8a7ddc38 | bellard | /* remove timer from the list before calling the callback */
|
1186 | 8a7ddc38 | bellard | *ptimer_head = ts->next; |
1187 | 8a7ddc38 | bellard | ts->next = NULL;
|
1188 | 3b46e624 | ths | |
1189 | 8a7ddc38 | bellard | /* run the callback (the timer list can be modified) */
|
1190 | 8a7ddc38 | bellard | ts->cb(ts->opaque); |
1191 | 8a7ddc38 | bellard | } |
1192 | 8a7ddc38 | bellard | } |
1193 | 8a7ddc38 | bellard | |
1194 | 8a7ddc38 | bellard | int64_t qemu_get_clock(QEMUClock *clock) |
1195 | 8a7ddc38 | bellard | { |
1196 | 8a7ddc38 | bellard | switch(clock->type) {
|
1197 | 8a7ddc38 | bellard | case QEMU_TIMER_REALTIME:
|
1198 | 1dce7c3c | bellard | return get_clock() / 1000000; |
1199 | 8a7ddc38 | bellard | default:
|
1200 | 8a7ddc38 | bellard | case QEMU_TIMER_VIRTUAL:
|
1201 | 2e70f6ef | pbrook | if (use_icount) {
|
1202 | 2e70f6ef | pbrook | return cpu_get_icount();
|
1203 | 2e70f6ef | pbrook | } else {
|
1204 | 2e70f6ef | pbrook | return cpu_get_clock();
|
1205 | 2e70f6ef | pbrook | } |
1206 | 8a7ddc38 | bellard | } |
1207 | 8a7ddc38 | bellard | } |
1208 | 8a7ddc38 | bellard | |
1209 | 1dce7c3c | bellard | static void init_timers(void) |
1210 | 1dce7c3c | bellard | { |
1211 | 1dce7c3c | bellard | init_get_clock(); |
1212 | 1dce7c3c | bellard | ticks_per_sec = QEMU_TIMER_BASE; |
1213 | 1dce7c3c | bellard | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
1214 | 1dce7c3c | bellard | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
1215 | 1dce7c3c | bellard | } |
1216 | 1dce7c3c | bellard | |
1217 | 8a7ddc38 | bellard | /* save a timer */
|
1218 | 8a7ddc38 | bellard | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
|
1219 | 8a7ddc38 | bellard | { |
1220 | 8a7ddc38 | bellard | uint64_t expire_time; |
1221 | 8a7ddc38 | bellard | |
1222 | 8a7ddc38 | bellard | if (qemu_timer_pending(ts)) {
|
1223 | 8a7ddc38 | bellard | expire_time = ts->expire_time; |
1224 | 8a7ddc38 | bellard | } else {
|
1225 | 8a7ddc38 | bellard | expire_time = -1;
|
1226 | 8a7ddc38 | bellard | } |
1227 | 8a7ddc38 | bellard | qemu_put_be64(f, expire_time); |
1228 | 8a7ddc38 | bellard | } |
1229 | 8a7ddc38 | bellard | |
1230 | 8a7ddc38 | bellard | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
|
1231 | 8a7ddc38 | bellard | { |
1232 | 8a7ddc38 | bellard | uint64_t expire_time; |
1233 | 8a7ddc38 | bellard | |
1234 | 8a7ddc38 | bellard | expire_time = qemu_get_be64(f); |
1235 | 8a7ddc38 | bellard | if (expire_time != -1) { |
1236 | 8a7ddc38 | bellard | qemu_mod_timer(ts, expire_time); |
1237 | 8a7ddc38 | bellard | } else {
|
1238 | 8a7ddc38 | bellard | qemu_del_timer(ts); |
1239 | 8a7ddc38 | bellard | } |
1240 | 8a7ddc38 | bellard | } |
1241 | 8a7ddc38 | bellard | |
1242 | 8a7ddc38 | bellard | static void timer_save(QEMUFile *f, void *opaque) |
1243 | 8a7ddc38 | bellard | { |
1244 | 8a7ddc38 | bellard | if (cpu_ticks_enabled) {
|
1245 | 8a7ddc38 | bellard | hw_error("cannot save state if virtual timers are running");
|
1246 | 8a7ddc38 | bellard | } |
1247 | bee8d684 | ths | qemu_put_be64(f, cpu_ticks_offset); |
1248 | bee8d684 | ths | qemu_put_be64(f, ticks_per_sec); |
1249 | bee8d684 | ths | qemu_put_be64(f, cpu_clock_offset); |
1250 | 8a7ddc38 | bellard | } |
1251 | 8a7ddc38 | bellard | |
1252 | 8a7ddc38 | bellard | static int timer_load(QEMUFile *f, void *opaque, int version_id) |
1253 | 8a7ddc38 | bellard | { |
1254 | c88676f8 | bellard | if (version_id != 1 && version_id != 2) |
1255 | 8a7ddc38 | bellard | return -EINVAL;
|
1256 | 8a7ddc38 | bellard | if (cpu_ticks_enabled) {
|
1257 | 8a7ddc38 | bellard | return -EINVAL;
|
1258 | 8a7ddc38 | bellard | } |
1259 | bee8d684 | ths | cpu_ticks_offset=qemu_get_be64(f); |
1260 | bee8d684 | ths | ticks_per_sec=qemu_get_be64(f); |
1261 | c88676f8 | bellard | if (version_id == 2) { |
1262 | bee8d684 | ths | cpu_clock_offset=qemu_get_be64(f); |
1263 | c88676f8 | bellard | } |
1264 | 8a7ddc38 | bellard | return 0; |
1265 | 8a7ddc38 | bellard | } |
1266 | 8a7ddc38 | bellard | |
1267 | 67b915a5 | bellard | #ifdef _WIN32
|
1268 | 5fafdf24 | ths | void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
|
1269 | 67b915a5 | bellard | DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
1270 | 67b915a5 | bellard | #else
|
1271 | 8a7ddc38 | bellard | static void host_alarm_handler(int host_signum) |
1272 | 67b915a5 | bellard | #endif
|
1273 | 8a7ddc38 | bellard | { |
1274 | 02ba45c5 | bellard | #if 0
|
1275 | 02ba45c5 | bellard | #define DISP_FREQ 1000
|
1276 | 02ba45c5 | bellard | {
|
1277 | 02ba45c5 | bellard | static int64_t delta_min = INT64_MAX;
|
1278 | 02ba45c5 | bellard | static int64_t delta_max, delta_cum, last_clock, delta, ti;
|
1279 | 02ba45c5 | bellard | static int count;
|
1280 | 02ba45c5 | bellard | ti = qemu_get_clock(vm_clock);
|
1281 | 02ba45c5 | bellard | if (last_clock != 0) {
|
1282 | 02ba45c5 | bellard | delta = ti - last_clock;
|
1283 | 02ba45c5 | bellard | if (delta < delta_min)
|
1284 | 02ba45c5 | bellard | delta_min = delta;
|
1285 | 02ba45c5 | bellard | if (delta > delta_max)
|
1286 | 02ba45c5 | bellard | delta_max = delta;
|
1287 | 02ba45c5 | bellard | delta_cum += delta;
|
1288 | 02ba45c5 | bellard | if (++count == DISP_FREQ) {
|
1289 | 26a76461 | bellard | printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
|
1290 | 02ba45c5 | bellard | muldiv64(delta_min, 1000000, ticks_per_sec),
|
1291 | 02ba45c5 | bellard | muldiv64(delta_max, 1000000, ticks_per_sec),
|
1292 | 02ba45c5 | bellard | muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
|
1293 | 02ba45c5 | bellard | (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
|
1294 | 02ba45c5 | bellard | count = 0;
|
1295 | 02ba45c5 | bellard | delta_min = INT64_MAX;
|
1296 | 02ba45c5 | bellard | delta_max = 0;
|
1297 | 02ba45c5 | bellard | delta_cum = 0;
|
1298 | 02ba45c5 | bellard | }
|
1299 | 02ba45c5 | bellard | }
|
1300 | 02ba45c5 | bellard | last_clock = ti;
|
1301 | 02ba45c5 | bellard | }
|
1302 | 02ba45c5 | bellard | #endif
|
1303 | efe75411 | ths | if (alarm_has_dynticks(alarm_timer) ||
|
1304 | 2e70f6ef | pbrook | (!use_icount && |
1305 | 2e70f6ef | pbrook | qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
1306 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock))) || |
1307 | 8a7ddc38 | bellard | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
1308 | 8a7ddc38 | bellard | qemu_get_clock(rt_clock))) { |
1309 | 06d9f2f7 | bellard | #ifdef _WIN32
|
1310 | c8994013 | ths | struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv; |
1311 | c8994013 | ths | SetEvent(data->host_alarm); |
1312 | 06d9f2f7 | bellard | #endif
|
1313 | ee5605e5 | balrog | CPUState *env = next_cpu; |
1314 | ee5605e5 | balrog | |
1315 | d5d08334 | balrog | alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
1316 | d5d08334 | balrog | |
1317 | 4f8eb8da | balrog | if (env) {
|
1318 | 4f8eb8da | balrog | /* stop the currently executing cpu because a timer occured */
|
1319 | 4f8eb8da | balrog | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
1320 | a332e112 | bellard | #ifdef USE_KQEMU
|
1321 | 4f8eb8da | balrog | if (env->kqemu_enabled) {
|
1322 | 4f8eb8da | balrog | kqemu_cpu_interrupt(env); |
1323 | 4f8eb8da | balrog | } |
1324 | ee5605e5 | balrog | #endif
|
1325 | 4f8eb8da | balrog | } |
1326 | ee5605e5 | balrog | event_pending = 1;
|
1327 | 8a7ddc38 | bellard | } |
1328 | 8a7ddc38 | bellard | } |
1329 | 8a7ddc38 | bellard | |
1330 | 2e70f6ef | pbrook | static int64_t qemu_next_deadline(void) |
1331 | efe75411 | ths | { |
1332 | 2e70f6ef | pbrook | int64_t delta; |
1333 | efe75411 | ths | |
1334 | efe75411 | ths | if (active_timers[QEMU_TIMER_VIRTUAL]) {
|
1335 | 2e70f6ef | pbrook | delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time - |
1336 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock); |
1337 | 2e70f6ef | pbrook | } else {
|
1338 | 2e70f6ef | pbrook | /* To avoid problems with overflow limit this to 2^32. */
|
1339 | 2e70f6ef | pbrook | delta = INT32_MAX; |
1340 | efe75411 | ths | } |
1341 | efe75411 | ths | |
1342 | 2e70f6ef | pbrook | if (delta < 0) |
1343 | 2e70f6ef | pbrook | delta = 0;
|
1344 | efe75411 | ths | |
1345 | 2e70f6ef | pbrook | return delta;
|
1346 | 2e70f6ef | pbrook | } |
1347 | 2e70f6ef | pbrook | |
1348 | 2e70f6ef | pbrook | static uint64_t qemu_next_deadline_dyntick(void) |
1349 | 2e70f6ef | pbrook | { |
1350 | 2e70f6ef | pbrook | int64_t delta; |
1351 | 2e70f6ef | pbrook | int64_t rtdelta; |
1352 | 2e70f6ef | pbrook | |
1353 | 2e70f6ef | pbrook | if (use_icount)
|
1354 | 2e70f6ef | pbrook | delta = INT32_MAX; |
1355 | 2e70f6ef | pbrook | else
|
1356 | 2e70f6ef | pbrook | delta = (qemu_next_deadline() + 999) / 1000; |
1357 | 2e70f6ef | pbrook | |
1358 | 2e70f6ef | pbrook | if (active_timers[QEMU_TIMER_REALTIME]) {
|
1359 | 2e70f6ef | pbrook | rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time - |
1360 | 2e70f6ef | pbrook | qemu_get_clock(rt_clock))*1000;
|
1361 | 2e70f6ef | pbrook | if (rtdelta < delta)
|
1362 | 2e70f6ef | pbrook | delta = rtdelta; |
1363 | 2e70f6ef | pbrook | } |
1364 | 2e70f6ef | pbrook | |
1365 | 2e70f6ef | pbrook | if (delta < MIN_TIMER_REARM_US)
|
1366 | 2e70f6ef | pbrook | delta = MIN_TIMER_REARM_US; |
1367 | 2e70f6ef | pbrook | |
1368 | 2e70f6ef | pbrook | return delta;
|
1369 | efe75411 | ths | } |
1370 | efe75411 | ths | |
1371 | fd872598 | bellard | #ifndef _WIN32
|
1372 | fd872598 | bellard | |
1373 | 829309c7 | bellard | #if defined(__linux__)
|
1374 | 829309c7 | bellard | |
1375 | fd872598 | bellard | #define RTC_FREQ 1024 |
1376 | fd872598 | bellard | |
1377 | c8994013 | ths | static void enable_sigio_timer(int fd) |
1378 | c8994013 | ths | { |
1379 | c8994013 | ths | struct sigaction act;
|
1380 | c8994013 | ths | |
1381 | c8994013 | ths | /* timer signal */
|
1382 | c8994013 | ths | sigfillset(&act.sa_mask); |
1383 | c8994013 | ths | act.sa_flags = 0;
|
1384 | c8994013 | ths | act.sa_handler = host_alarm_handler; |
1385 | c8994013 | ths | |
1386 | c8994013 | ths | sigaction(SIGIO, &act, NULL);
|
1387 | c8994013 | ths | fcntl(fd, F_SETFL, O_ASYNC); |
1388 | c8994013 | ths | fcntl(fd, F_SETOWN, getpid()); |
1389 | c8994013 | ths | } |
1390 | 829309c7 | bellard | |
1391 | c40ec5a9 | ths | static int hpet_start_timer(struct qemu_alarm_timer *t) |
1392 | c40ec5a9 | ths | { |
1393 | c40ec5a9 | ths | struct hpet_info info;
|
1394 | c40ec5a9 | ths | int r, fd;
|
1395 | c40ec5a9 | ths | |
1396 | c40ec5a9 | ths | fd = open("/dev/hpet", O_RDONLY);
|
1397 | c40ec5a9 | ths | if (fd < 0) |
1398 | c40ec5a9 | ths | return -1; |
1399 | c40ec5a9 | ths | |
1400 | c40ec5a9 | ths | /* Set frequency */
|
1401 | c40ec5a9 | ths | r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
1402 | c40ec5a9 | ths | if (r < 0) { |
1403 | c40ec5a9 | ths | fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
|
1404 | c40ec5a9 | ths | "error, but for better emulation accuracy type:\n"
|
1405 | c40ec5a9 | ths | "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
|
1406 | c40ec5a9 | ths | goto fail;
|
1407 | c40ec5a9 | ths | } |
1408 | c40ec5a9 | ths | |
1409 | c40ec5a9 | ths | /* Check capabilities */
|
1410 | c40ec5a9 | ths | r = ioctl(fd, HPET_INFO, &info); |
1411 | c40ec5a9 | ths | if (r < 0) |
1412 | c40ec5a9 | ths | goto fail;
|
1413 | c40ec5a9 | ths | |
1414 | c40ec5a9 | ths | /* Enable periodic mode */
|
1415 | c40ec5a9 | ths | r = ioctl(fd, HPET_EPI, 0);
|
1416 | c40ec5a9 | ths | if (info.hi_flags && (r < 0)) |
1417 | c40ec5a9 | ths | goto fail;
|
1418 | c40ec5a9 | ths | |
1419 | c40ec5a9 | ths | /* Enable interrupt */
|
1420 | c40ec5a9 | ths | r = ioctl(fd, HPET_IE_ON, 0);
|
1421 | c40ec5a9 | ths | if (r < 0) |
1422 | c40ec5a9 | ths | goto fail;
|
1423 | c40ec5a9 | ths | |
1424 | c40ec5a9 | ths | enable_sigio_timer(fd); |
1425 | fcdc2129 | pbrook | t->priv = (void *)(long)fd; |
1426 | c40ec5a9 | ths | |
1427 | c40ec5a9 | ths | return 0; |
1428 | c40ec5a9 | ths | fail:
|
1429 | c40ec5a9 | ths | close(fd); |
1430 | c40ec5a9 | ths | return -1; |
1431 | c40ec5a9 | ths | } |
1432 | c40ec5a9 | ths | |
1433 | c40ec5a9 | ths | static void hpet_stop_timer(struct qemu_alarm_timer *t) |
1434 | c40ec5a9 | ths | { |
1435 | fcdc2129 | pbrook | int fd = (long)t->priv; |
1436 | c40ec5a9 | ths | |
1437 | c40ec5a9 | ths | close(fd); |
1438 | c40ec5a9 | ths | } |
1439 | c40ec5a9 | ths | |
1440 | c8994013 | ths | static int rtc_start_timer(struct qemu_alarm_timer *t) |
1441 | fd872598 | bellard | { |
1442 | c8994013 | ths | int rtc_fd;
|
1443 | b5a23ad4 | balrog | unsigned long current_rtc_freq = 0; |
1444 | c8994013 | ths | |
1445 | aeb30be6 | balrog | TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
|
1446 | fd872598 | bellard | if (rtc_fd < 0) |
1447 | fd872598 | bellard | return -1; |
1448 | b5a23ad4 | balrog | ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
1449 | b5a23ad4 | balrog | if (current_rtc_freq != RTC_FREQ &&
|
1450 | b5a23ad4 | balrog | ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
|
1451 | fd872598 | bellard | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
|
1452 | fd872598 | bellard | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
|
1453 | fd872598 | bellard | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
|
1454 | fd872598 | bellard | goto fail;
|
1455 | fd872598 | bellard | } |
1456 | fd872598 | bellard | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
1457 | fd872598 | bellard | fail:
|
1458 | fd872598 | bellard | close(rtc_fd); |
1459 | fd872598 | bellard | return -1; |
1460 | fd872598 | bellard | } |
1461 | c8994013 | ths | |
1462 | c8994013 | ths | enable_sigio_timer(rtc_fd); |
1463 | c8994013 | ths | |
1464 | fcdc2129 | pbrook | t->priv = (void *)(long)rtc_fd; |
1465 | c8994013 | ths | |
1466 | fd872598 | bellard | return 0; |
1467 | fd872598 | bellard | } |
1468 | fd872598 | bellard | |
1469 | c8994013 | ths | static void rtc_stop_timer(struct qemu_alarm_timer *t) |
1470 | 829309c7 | bellard | { |
1471 | fcdc2129 | pbrook | int rtc_fd = (long)t->priv; |
1472 | c8994013 | ths | |
1473 | c8994013 | ths | close(rtc_fd); |
1474 | 829309c7 | bellard | } |
1475 | 829309c7 | bellard | |
1476 | efe75411 | ths | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
1477 | efe75411 | ths | { |
1478 | efe75411 | ths | struct sigevent ev;
|
1479 | efe75411 | ths | timer_t host_timer; |
1480 | efe75411 | ths | struct sigaction act;
|
1481 | efe75411 | ths | |
1482 | efe75411 | ths | sigfillset(&act.sa_mask); |
1483 | efe75411 | ths | act.sa_flags = 0;
|
1484 | efe75411 | ths | act.sa_handler = host_alarm_handler; |
1485 | efe75411 | ths | |
1486 | efe75411 | ths | sigaction(SIGALRM, &act, NULL);
|
1487 | efe75411 | ths | |
1488 | efe75411 | ths | ev.sigev_value.sival_int = 0;
|
1489 | efe75411 | ths | ev.sigev_notify = SIGEV_SIGNAL; |
1490 | efe75411 | ths | ev.sigev_signo = SIGALRM; |
1491 | efe75411 | ths | |
1492 | efe75411 | ths | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
|
1493 | efe75411 | ths | perror("timer_create");
|
1494 | efe75411 | ths | |
1495 | efe75411 | ths | /* disable dynticks */
|
1496 | efe75411 | ths | fprintf(stderr, "Dynamic Ticks disabled\n");
|
1497 | efe75411 | ths | |
1498 | efe75411 | ths | return -1; |
1499 | efe75411 | ths | } |
1500 | efe75411 | ths | |
1501 | efe75411 | ths | t->priv = (void *)host_timer;
|
1502 | efe75411 | ths | |
1503 | efe75411 | ths | return 0; |
1504 | efe75411 | ths | } |
1505 | efe75411 | ths | |
1506 | efe75411 | ths | static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
1507 | efe75411 | ths | { |
1508 | efe75411 | ths | timer_t host_timer = (timer_t)t->priv; |
1509 | efe75411 | ths | |
1510 | efe75411 | ths | timer_delete(host_timer); |
1511 | efe75411 | ths | } |
1512 | efe75411 | ths | |
1513 | efe75411 | ths | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
1514 | efe75411 | ths | { |
1515 | efe75411 | ths | timer_t host_timer = (timer_t)t->priv; |
1516 | efe75411 | ths | struct itimerspec timeout;
|
1517 | efe75411 | ths | int64_t nearest_delta_us = INT64_MAX; |
1518 | efe75411 | ths | int64_t current_us; |
1519 | efe75411 | ths | |
1520 | efe75411 | ths | if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1521 | efe75411 | ths | !active_timers[QEMU_TIMER_VIRTUAL]) |
1522 | d5d08334 | balrog | return;
|
1523 | efe75411 | ths | |
1524 | 2e70f6ef | pbrook | nearest_delta_us = qemu_next_deadline_dyntick(); |
1525 | efe75411 | ths | |
1526 | efe75411 | ths | /* check whether a timer is already running */
|
1527 | efe75411 | ths | if (timer_gettime(host_timer, &timeout)) {
|
1528 | efe75411 | ths | perror("gettime");
|
1529 | efe75411 | ths | fprintf(stderr, "Internal timer error: aborting\n");
|
1530 | efe75411 | ths | exit(1);
|
1531 | efe75411 | ths | } |
1532 | efe75411 | ths | current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
1533 | efe75411 | ths | if (current_us && current_us <= nearest_delta_us)
|
1534 | efe75411 | ths | return;
|
1535 | efe75411 | ths | |
1536 | efe75411 | ths | timeout.it_interval.tv_sec = 0;
|
1537 | efe75411 | ths | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
1538 | efe75411 | ths | timeout.it_value.tv_sec = nearest_delta_us / 1000000;
|
1539 | efe75411 | ths | timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
1540 | efe75411 | ths | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
1541 | efe75411 | ths | perror("settime");
|
1542 | efe75411 | ths | fprintf(stderr, "Internal timer error: aborting\n");
|
1543 | efe75411 | ths | exit(1);
|
1544 | efe75411 | ths | } |
1545 | efe75411 | ths | } |
1546 | efe75411 | ths | |
1547 | 70744b3a | ths | #endif /* defined(__linux__) */ |
1548 | 231c6586 | ths | |
1549 | c8994013 | ths | static int unix_start_timer(struct qemu_alarm_timer *t) |
1550 | c8994013 | ths | { |
1551 | c8994013 | ths | struct sigaction act;
|
1552 | c8994013 | ths | struct itimerval itv;
|
1553 | c8994013 | ths | int err;
|
1554 | c8994013 | ths | |
1555 | c8994013 | ths | /* timer signal */
|
1556 | c8994013 | ths | sigfillset(&act.sa_mask); |
1557 | c8994013 | ths | act.sa_flags = 0;
|
1558 | c8994013 | ths | act.sa_handler = host_alarm_handler; |
1559 | c8994013 | ths | |
1560 | c8994013 | ths | sigaction(SIGALRM, &act, NULL);
|
1561 | c8994013 | ths | |
1562 | c8994013 | ths | itv.it_interval.tv_sec = 0;
|
1563 | c8994013 | ths | /* for i386 kernel 2.6 to get 1 ms */
|
1564 | c8994013 | ths | itv.it_interval.tv_usec = 999;
|
1565 | c8994013 | ths | itv.it_value.tv_sec = 0;
|
1566 | c8994013 | ths | itv.it_value.tv_usec = 10 * 1000; |
1567 | c8994013 | ths | |
1568 | c8994013 | ths | err = setitimer(ITIMER_REAL, &itv, NULL);
|
1569 | c8994013 | ths | if (err)
|
1570 | c8994013 | ths | return -1; |
1571 | c8994013 | ths | |
1572 | c8994013 | ths | return 0; |
1573 | c8994013 | ths | } |
1574 | c8994013 | ths | |
1575 | c8994013 | ths | static void unix_stop_timer(struct qemu_alarm_timer *t) |
1576 | c8994013 | ths | { |
1577 | c8994013 | ths | struct itimerval itv;
|
1578 | c8994013 | ths | |
1579 | c8994013 | ths | memset(&itv, 0, sizeof(itv)); |
1580 | c8994013 | ths | setitimer(ITIMER_REAL, &itv, NULL);
|
1581 | c8994013 | ths | } |
1582 | c8994013 | ths | |
1583 | 829309c7 | bellard | #endif /* !defined(_WIN32) */ |
1584 | fd872598 | bellard | |
1585 | c8994013 | ths | #ifdef _WIN32
|
1586 | c8994013 | ths | |
1587 | c8994013 | ths | static int win32_start_timer(struct qemu_alarm_timer *t) |
1588 | c8994013 | ths | { |
1589 | c8994013 | ths | TIMECAPS tc; |
1590 | c8994013 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1591 | efe75411 | ths | UINT flags; |
1592 | c8994013 | ths | |
1593 | c8994013 | ths | data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL); |
1594 | c8994013 | ths | if (!data->host_alarm) {
|
1595 | c8994013 | ths | perror("Failed CreateEvent");
|
1596 | c396a7f0 | ths | return -1; |
1597 | c8994013 | ths | } |
1598 | c8994013 | ths | |
1599 | c8994013 | ths | memset(&tc, 0, sizeof(tc)); |
1600 | c8994013 | ths | timeGetDevCaps(&tc, sizeof(tc));
|
1601 | c8994013 | ths | |
1602 | c8994013 | ths | if (data->period < tc.wPeriodMin)
|
1603 | c8994013 | ths | data->period = tc.wPeriodMin; |
1604 | c8994013 | ths | |
1605 | c8994013 | ths | timeBeginPeriod(data->period); |
1606 | c8994013 | ths | |
1607 | efe75411 | ths | flags = TIME_CALLBACK_FUNCTION; |
1608 | efe75411 | ths | if (alarm_has_dynticks(t))
|
1609 | efe75411 | ths | flags |= TIME_ONESHOT; |
1610 | efe75411 | ths | else
|
1611 | efe75411 | ths | flags |= TIME_PERIODIC; |
1612 | efe75411 | ths | |
1613 | c8994013 | ths | data->timerId = timeSetEvent(1, // interval (ms) |
1614 | c8994013 | ths | data->period, // resolution
|
1615 | c8994013 | ths | host_alarm_handler, // function
|
1616 | c8994013 | ths | (DWORD)t, // parameter
|
1617 | efe75411 | ths | flags); |
1618 | c8994013 | ths | |
1619 | c8994013 | ths | if (!data->timerId) {
|
1620 | c8994013 | ths | perror("Failed to initialize win32 alarm timer");
|
1621 | c8994013 | ths | |
1622 | c8994013 | ths | timeEndPeriod(data->period); |
1623 | c8994013 | ths | CloseHandle(data->host_alarm); |
1624 | c8994013 | ths | return -1; |
1625 | c8994013 | ths | } |
1626 | c8994013 | ths | |
1627 | c8994013 | ths | qemu_add_wait_object(data->host_alarm, NULL, NULL); |
1628 | c8994013 | ths | |
1629 | c8994013 | ths | return 0; |
1630 | c8994013 | ths | } |
1631 | c8994013 | ths | |
1632 | c8994013 | ths | static void win32_stop_timer(struct qemu_alarm_timer *t) |
1633 | c8994013 | ths | { |
1634 | c8994013 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1635 | c8994013 | ths | |
1636 | c8994013 | ths | timeKillEvent(data->timerId); |
1637 | c8994013 | ths | timeEndPeriod(data->period); |
1638 | c8994013 | ths | |
1639 | c8994013 | ths | CloseHandle(data->host_alarm); |
1640 | c8994013 | ths | } |
1641 | c8994013 | ths | |
1642 | efe75411 | ths | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
1643 | efe75411 | ths | { |
1644 | efe75411 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1645 | efe75411 | ths | uint64_t nearest_delta_us; |
1646 | efe75411 | ths | |
1647 | efe75411 | ths | if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1648 | efe75411 | ths | !active_timers[QEMU_TIMER_VIRTUAL]) |
1649 | d5d08334 | balrog | return;
|
1650 | efe75411 | ths | |
1651 | 2e70f6ef | pbrook | nearest_delta_us = qemu_next_deadline_dyntick(); |
1652 | efe75411 | ths | nearest_delta_us /= 1000;
|
1653 | efe75411 | ths | |
1654 | efe75411 | ths | timeKillEvent(data->timerId); |
1655 | efe75411 | ths | |
1656 | efe75411 | ths | data->timerId = timeSetEvent(1,
|
1657 | efe75411 | ths | data->period, |
1658 | efe75411 | ths | host_alarm_handler, |
1659 | efe75411 | ths | (DWORD)t, |
1660 | efe75411 | ths | TIME_ONESHOT | TIME_PERIODIC); |
1661 | efe75411 | ths | |
1662 | efe75411 | ths | if (!data->timerId) {
|
1663 | efe75411 | ths | perror("Failed to re-arm win32 alarm timer");
|
1664 | efe75411 | ths | |
1665 | efe75411 | ths | timeEndPeriod(data->period); |
1666 | efe75411 | ths | CloseHandle(data->host_alarm); |
1667 | efe75411 | ths | exit(1);
|
1668 | efe75411 | ths | } |
1669 | efe75411 | ths | } |
1670 | efe75411 | ths | |
1671 | c8994013 | ths | #endif /* _WIN32 */ |
1672 | c8994013 | ths | |
1673 | 1dce7c3c | bellard | static void init_timer_alarm(void) |
1674 | 8a7ddc38 | bellard | { |
1675 | c8994013 | ths | struct qemu_alarm_timer *t;
|
1676 | c8994013 | ths | int i, err = -1; |
1677 | c8994013 | ths | |
1678 | c8994013 | ths | for (i = 0; alarm_timers[i].name; i++) { |
1679 | c8994013 | ths | t = &alarm_timers[i]; |
1680 | c8994013 | ths | |
1681 | c8994013 | ths | err = t->start(t); |
1682 | c8994013 | ths | if (!err)
|
1683 | c8994013 | ths | break;
|
1684 | 67b915a5 | bellard | } |
1685 | fd872598 | bellard | |
1686 | c8994013 | ths | if (err) {
|
1687 | c8994013 | ths | fprintf(stderr, "Unable to find any suitable alarm timer.\n");
|
1688 | c8994013 | ths | fprintf(stderr, "Terminating\n");
|
1689 | c8994013 | ths | exit(1);
|
1690 | 67b915a5 | bellard | } |
1691 | c8994013 | ths | |
1692 | c8994013 | ths | alarm_timer = t; |
1693 | 8a7ddc38 | bellard | } |
1694 | 8a7ddc38 | bellard | |
1695 | 9596ebb7 | pbrook | static void quit_timers(void) |
1696 | 40c3bac3 | bellard | { |
1697 | c8994013 | ths | alarm_timer->stop(alarm_timer); |
1698 | c8994013 | ths | alarm_timer = NULL;
|
1699 | 40c3bac3 | bellard | } |
1700 | 40c3bac3 | bellard | |
1701 | c4b1fcc0 | bellard | /***********************************************************/
|
1702 | f6503059 | balrog | /* host time/date access */
|
1703 | f6503059 | balrog | void qemu_get_timedate(struct tm *tm, int offset) |
1704 | f6503059 | balrog | { |
1705 | f6503059 | balrog | time_t ti; |
1706 | f6503059 | balrog | struct tm *ret;
|
1707 | f6503059 | balrog | |
1708 | f6503059 | balrog | time(&ti); |
1709 | f6503059 | balrog | ti += offset; |
1710 | f6503059 | balrog | if (rtc_date_offset == -1) { |
1711 | f6503059 | balrog | if (rtc_utc)
|
1712 | f6503059 | balrog | ret = gmtime(&ti); |
1713 | f6503059 | balrog | else
|
1714 | f6503059 | balrog | ret = localtime(&ti); |
1715 | f6503059 | balrog | } else {
|
1716 | f6503059 | balrog | ti -= rtc_date_offset; |
1717 | f6503059 | balrog | ret = gmtime(&ti); |
1718 | f6503059 | balrog | } |
1719 | f6503059 | balrog | |
1720 | f6503059 | balrog | memcpy(tm, ret, sizeof(struct tm)); |
1721 | f6503059 | balrog | } |
1722 | f6503059 | balrog | |
1723 | f6503059 | balrog | int qemu_timedate_diff(struct tm *tm) |
1724 | f6503059 | balrog | { |
1725 | f6503059 | balrog | time_t seconds; |
1726 | f6503059 | balrog | |
1727 | f6503059 | balrog | if (rtc_date_offset == -1) |
1728 | f6503059 | balrog | if (rtc_utc)
|
1729 | f6503059 | balrog | seconds = mktimegm(tm); |
1730 | f6503059 | balrog | else
|
1731 | f6503059 | balrog | seconds = mktime(tm); |
1732 | f6503059 | balrog | else
|
1733 | f6503059 | balrog | seconds = mktimegm(tm) + rtc_date_offset; |
1734 | f6503059 | balrog | |
1735 | f6503059 | balrog | return seconds - time(NULL); |
1736 | f6503059 | balrog | } |
1737 | f6503059 | balrog | |
1738 | f6503059 | balrog | /***********************************************************/
|
1739 | 82c643ff | bellard | /* character device */
|
1740 | 313aa567 | bellard | |
1741 | e5b0bc44 | pbrook | static void qemu_chr_event(CharDriverState *s, int event) |
1742 | e5b0bc44 | pbrook | { |
1743 | e5b0bc44 | pbrook | if (!s->chr_event)
|
1744 | e5b0bc44 | pbrook | return;
|
1745 | e5b0bc44 | pbrook | s->chr_event(s->handler_opaque, event); |
1746 | e5b0bc44 | pbrook | } |
1747 | e5b0bc44 | pbrook | |
1748 | 86e94dea | ths | static void qemu_chr_reset_bh(void *opaque) |
1749 | 86e94dea | ths | { |
1750 | 86e94dea | ths | CharDriverState *s = opaque; |
1751 | e5b0bc44 | pbrook | qemu_chr_event(s, CHR_EVENT_RESET); |
1752 | 86e94dea | ths | qemu_bh_delete(s->bh); |
1753 | 86e94dea | ths | s->bh = NULL;
|
1754 | 86e94dea | ths | } |
1755 | 86e94dea | ths | |
1756 | 86e94dea | ths | void qemu_chr_reset(CharDriverState *s)
|
1757 | 86e94dea | ths | { |
1758 | 86e94dea | ths | if (s->bh == NULL) { |
1759 | 86e94dea | ths | s->bh = qemu_bh_new(qemu_chr_reset_bh, s); |
1760 | 86e94dea | ths | qemu_bh_schedule(s->bh); |
1761 | 86e94dea | ths | } |
1762 | 86e94dea | ths | } |
1763 | 86e94dea | ths | |
1764 | 82c643ff | bellard | int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
1765 | 82c643ff | bellard | { |
1766 | 82c643ff | bellard | return s->chr_write(s, buf, len);
|
1767 | 82c643ff | bellard | } |
1768 | 67b915a5 | bellard | |
1769 | e57a8c0e | bellard | int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg) |
1770 | f8d179e3 | bellard | { |
1771 | e57a8c0e | bellard | if (!s->chr_ioctl)
|
1772 | e57a8c0e | bellard | return -ENOTSUP;
|
1773 | e57a8c0e | bellard | return s->chr_ioctl(s, cmd, arg);
|
1774 | f8d179e3 | bellard | } |
1775 | f8d179e3 | bellard | |
1776 | e5b0bc44 | pbrook | int qemu_chr_can_read(CharDriverState *s)
|
1777 | e5b0bc44 | pbrook | { |
1778 | e5b0bc44 | pbrook | if (!s->chr_can_read)
|
1779 | e5b0bc44 | pbrook | return 0; |
1780 | e5b0bc44 | pbrook | return s->chr_can_read(s->handler_opaque);
|
1781 | e5b0bc44 | pbrook | } |
1782 | e5b0bc44 | pbrook | |
1783 | e5b0bc44 | pbrook | void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len) |
1784 | e5b0bc44 | pbrook | { |
1785 | e5b0bc44 | pbrook | s->chr_read(s->handler_opaque, buf, len); |
1786 | e5b0bc44 | pbrook | } |
1787 | e5b0bc44 | pbrook | |
1788 | bd9bdce6 | balrog | void qemu_chr_accept_input(CharDriverState *s)
|
1789 | bd9bdce6 | balrog | { |
1790 | bd9bdce6 | balrog | if (s->chr_accept_input)
|
1791 | bd9bdce6 | balrog | s->chr_accept_input(s); |
1792 | bd9bdce6 | balrog | } |
1793 | e5b0bc44 | pbrook | |
1794 | 82c643ff | bellard | void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
1795 | 67b915a5 | bellard | { |
1796 | 82c643ff | bellard | char buf[4096]; |
1797 | 82c643ff | bellard | va_list ap; |
1798 | 82c643ff | bellard | va_start(ap, fmt); |
1799 | 82c643ff | bellard | vsnprintf(buf, sizeof(buf), fmt, ap);
|
1800 | ffe8ab83 | ths | qemu_chr_write(s, (uint8_t *)buf, strlen(buf)); |
1801 | 82c643ff | bellard | va_end(ap); |
1802 | 67b915a5 | bellard | } |
1803 | 67b915a5 | bellard | |
1804 | 5905b2e5 | bellard | void qemu_chr_send_event(CharDriverState *s, int event) |
1805 | 5905b2e5 | bellard | { |
1806 | 5905b2e5 | bellard | if (s->chr_send_event)
|
1807 | 5905b2e5 | bellard | s->chr_send_event(s, event); |
1808 | 5905b2e5 | bellard | } |
1809 | 5905b2e5 | bellard | |
1810 | 5fafdf24 | ths | void qemu_chr_add_handlers(CharDriverState *s,
|
1811 | 5fafdf24 | ths | IOCanRWHandler *fd_can_read, |
1812 | e5b0bc44 | pbrook | IOReadHandler *fd_read, |
1813 | e5b0bc44 | pbrook | IOEventHandler *fd_event, |
1814 | e5b0bc44 | pbrook | void *opaque)
|
1815 | 82c643ff | bellard | { |
1816 | e5b0bc44 | pbrook | s->chr_can_read = fd_can_read; |
1817 | e5b0bc44 | pbrook | s->chr_read = fd_read; |
1818 | e5b0bc44 | pbrook | s->chr_event = fd_event; |
1819 | e5b0bc44 | pbrook | s->handler_opaque = opaque; |
1820 | e5b0bc44 | pbrook | if (s->chr_update_read_handler)
|
1821 | e5b0bc44 | pbrook | s->chr_update_read_handler(s); |
1822 | 82c643ff | bellard | } |
1823 | 3b46e624 | ths | |
1824 | 82c643ff | bellard | static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1825 | 313aa567 | bellard | { |
1826 | 82c643ff | bellard | return len;
|
1827 | 82c643ff | bellard | } |
1828 | 82c643ff | bellard | |
1829 | 52f61fde | ths | static CharDriverState *qemu_chr_open_null(void) |
1830 | 82c643ff | bellard | { |
1831 | 82c643ff | bellard | CharDriverState *chr; |
1832 | 82c643ff | bellard | |
1833 | 82c643ff | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
1834 | 82c643ff | bellard | if (!chr)
|
1835 | 82c643ff | bellard | return NULL; |
1836 | 82c643ff | bellard | chr->chr_write = null_chr_write; |
1837 | 82c643ff | bellard | return chr;
|
1838 | 82c643ff | bellard | } |
1839 | 82c643ff | bellard | |
1840 | 20d8a3ed | ths | /* MUX driver for serial I/O splitting */
|
1841 | 20d8a3ed | ths | static int term_timestamps; |
1842 | 20d8a3ed | ths | static int64_t term_timestamps_start;
|
1843 | 9c1de612 | ths | #define MAX_MUX 4 |
1844 | bd9bdce6 | balrog | #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ |
1845 | bd9bdce6 | balrog | #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) |
1846 | 20d8a3ed | ths | typedef struct { |
1847 | 20d8a3ed | ths | IOCanRWHandler *chr_can_read[MAX_MUX]; |
1848 | 20d8a3ed | ths | IOReadHandler *chr_read[MAX_MUX]; |
1849 | 20d8a3ed | ths | IOEventHandler *chr_event[MAX_MUX]; |
1850 | 20d8a3ed | ths | void *ext_opaque[MAX_MUX];
|
1851 | 20d8a3ed | ths | CharDriverState *drv; |
1852 | bd9bdce6 | balrog | unsigned char buffer[MUX_BUFFER_SIZE]; |
1853 | bd9bdce6 | balrog | int prod;
|
1854 | bd9bdce6 | balrog | int cons;
|
1855 | 20d8a3ed | ths | int mux_cnt;
|
1856 | 20d8a3ed | ths | int term_got_escape;
|
1857 | 20d8a3ed | ths | int max_size;
|
1858 | 20d8a3ed | ths | } MuxDriver; |
1859 | 20d8a3ed | ths | |
1860 | 20d8a3ed | ths | |
1861 | 20d8a3ed | ths | static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1862 | 20d8a3ed | ths | { |
1863 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
1864 | 20d8a3ed | ths | int ret;
|
1865 | 20d8a3ed | ths | if (!term_timestamps) {
|
1866 | 20d8a3ed | ths | ret = d->drv->chr_write(d->drv, buf, len); |
1867 | 20d8a3ed | ths | } else {
|
1868 | 20d8a3ed | ths | int i;
|
1869 | 20d8a3ed | ths | |
1870 | 20d8a3ed | ths | ret = 0;
|
1871 | 20d8a3ed | ths | for(i = 0; i < len; i++) { |
1872 | 20d8a3ed | ths | ret += d->drv->chr_write(d->drv, buf+i, 1);
|
1873 | 20d8a3ed | ths | if (buf[i] == '\n') { |
1874 | 20d8a3ed | ths | char buf1[64]; |
1875 | 20d8a3ed | ths | int64_t ti; |
1876 | 20d8a3ed | ths | int secs;
|
1877 | 20d8a3ed | ths | |
1878 | 20d8a3ed | ths | ti = get_clock(); |
1879 | 20d8a3ed | ths | if (term_timestamps_start == -1) |
1880 | 20d8a3ed | ths | term_timestamps_start = ti; |
1881 | 20d8a3ed | ths | ti -= term_timestamps_start; |
1882 | 20d8a3ed | ths | secs = ti / 1000000000;
|
1883 | 20d8a3ed | ths | snprintf(buf1, sizeof(buf1),
|
1884 | 20d8a3ed | ths | "[%02d:%02d:%02d.%03d] ",
|
1885 | 20d8a3ed | ths | secs / 3600,
|
1886 | 20d8a3ed | ths | (secs / 60) % 60, |
1887 | 20d8a3ed | ths | secs % 60,
|
1888 | 20d8a3ed | ths | (int)((ti / 1000000) % 1000)); |
1889 | ffe8ab83 | ths | d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
1890 | 20d8a3ed | ths | } |
1891 | 20d8a3ed | ths | } |
1892 | 20d8a3ed | ths | } |
1893 | 20d8a3ed | ths | return ret;
|
1894 | 20d8a3ed | ths | } |
1895 | 20d8a3ed | ths | |
1896 | 20d8a3ed | ths | static char *mux_help[] = { |
1897 | 20d8a3ed | ths | "% h print this help\n\r",
|
1898 | 20d8a3ed | ths | "% x exit emulator\n\r",
|
1899 | 20d8a3ed | ths | "% s save disk data back to file (if -snapshot)\n\r",
|
1900 | 20d8a3ed | ths | "% t toggle console timestamps\n\r"
|
1901 | 20d8a3ed | ths | "% b send break (magic sysrq)\n\r",
|
1902 | 20d8a3ed | ths | "% c switch between console and monitor\n\r",
|
1903 | 20d8a3ed | ths | "% % sends %\n\r",
|
1904 | 20d8a3ed | ths | NULL
|
1905 | 20d8a3ed | ths | }; |
1906 | 20d8a3ed | ths | |
1907 | 20d8a3ed | ths | static int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
1908 | 20d8a3ed | ths | static void mux_print_help(CharDriverState *chr) |
1909 | 20d8a3ed | ths | { |
1910 | 20d8a3ed | ths | int i, j;
|
1911 | 20d8a3ed | ths | char ebuf[15] = "Escape-Char"; |
1912 | 20d8a3ed | ths | char cbuf[50] = "\n\r"; |
1913 | 20d8a3ed | ths | |
1914 | 20d8a3ed | ths | if (term_escape_char > 0 && term_escape_char < 26) { |
1915 | 20d8a3ed | ths | sprintf(cbuf,"\n\r");
|
1916 | 20d8a3ed | ths | sprintf(ebuf,"C-%c", term_escape_char - 1 + 'a'); |
1917 | 20d8a3ed | ths | } else {
|
1918 | ffe8ab83 | ths | sprintf(cbuf,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
|
1919 | ffe8ab83 | ths | term_escape_char); |
1920 | 20d8a3ed | ths | } |
1921 | ffe8ab83 | ths | chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf)); |
1922 | 20d8a3ed | ths | for (i = 0; mux_help[i] != NULL; i++) { |
1923 | 20d8a3ed | ths | for (j=0; mux_help[i][j] != '\0'; j++) { |
1924 | 20d8a3ed | ths | if (mux_help[i][j] == '%') |
1925 | ffe8ab83 | ths | chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf)); |
1926 | 20d8a3ed | ths | else
|
1927 | ffe8ab83 | ths | chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
|
1928 | 20d8a3ed | ths | } |
1929 | 20d8a3ed | ths | } |
1930 | 20d8a3ed | ths | } |
1931 | 20d8a3ed | ths | |
1932 | 20d8a3ed | ths | static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
1933 | 20d8a3ed | ths | { |
1934 | 20d8a3ed | ths | if (d->term_got_escape) {
|
1935 | 20d8a3ed | ths | d->term_got_escape = 0;
|
1936 | 20d8a3ed | ths | if (ch == term_escape_char)
|
1937 | 20d8a3ed | ths | goto send_char;
|
1938 | 20d8a3ed | ths | switch(ch) {
|
1939 | 20d8a3ed | ths | case '?': |
1940 | 20d8a3ed | ths | case 'h': |
1941 | 20d8a3ed | ths | mux_print_help(chr); |
1942 | 20d8a3ed | ths | break;
|
1943 | 20d8a3ed | ths | case 'x': |
1944 | 20d8a3ed | ths | { |
1945 | 20d8a3ed | ths | char *term = "QEMU: Terminated\n\r"; |
1946 | ffe8ab83 | ths | chr->chr_write(chr,(uint8_t *)term,strlen(term)); |
1947 | 20d8a3ed | ths | exit(0);
|
1948 | 20d8a3ed | ths | break;
|
1949 | 20d8a3ed | ths | } |
1950 | 20d8a3ed | ths | case 's': |
1951 | 20d8a3ed | ths | { |
1952 | 20d8a3ed | ths | int i;
|
1953 | e4bcb14c | ths | for (i = 0; i < nb_drives; i++) { |
1954 | e4bcb14c | ths | bdrv_commit(drives_table[i].bdrv); |
1955 | 20d8a3ed | ths | } |
1956 | 20d8a3ed | ths | } |
1957 | 20d8a3ed | ths | break;
|
1958 | 20d8a3ed | ths | case 'b': |
1959 | 36ddb83b | balrog | qemu_chr_event(chr, CHR_EVENT_BREAK); |
1960 | 20d8a3ed | ths | break;
|
1961 | 20d8a3ed | ths | case 'c': |
1962 | 20d8a3ed | ths | /* Switch to the next registered device */
|
1963 | 20d8a3ed | ths | chr->focus++; |
1964 | 20d8a3ed | ths | if (chr->focus >= d->mux_cnt)
|
1965 | 20d8a3ed | ths | chr->focus = 0;
|
1966 | 20d8a3ed | ths | break;
|
1967 | 20d8a3ed | ths | case 't': |
1968 | 20d8a3ed | ths | term_timestamps = !term_timestamps; |
1969 | 20d8a3ed | ths | term_timestamps_start = -1;
|
1970 | 20d8a3ed | ths | break;
|
1971 | 20d8a3ed | ths | } |
1972 | 20d8a3ed | ths | } else if (ch == term_escape_char) { |
1973 | 20d8a3ed | ths | d->term_got_escape = 1;
|
1974 | 20d8a3ed | ths | } else {
|
1975 | 20d8a3ed | ths | send_char:
|
1976 | 20d8a3ed | ths | return 1; |
1977 | 20d8a3ed | ths | } |
1978 | 20d8a3ed | ths | return 0; |
1979 | 20d8a3ed | ths | } |
1980 | 20d8a3ed | ths | |
1981 | bd9bdce6 | balrog | static void mux_chr_accept_input(CharDriverState *chr) |
1982 | bd9bdce6 | balrog | { |
1983 | bd9bdce6 | balrog | int m = chr->focus;
|
1984 | bd9bdce6 | balrog | MuxDriver *d = chr->opaque; |
1985 | bd9bdce6 | balrog | |
1986 | bd9bdce6 | balrog | while (d->prod != d->cons &&
|
1987 | bd9bdce6 | balrog | d->chr_can_read[m] && |
1988 | bd9bdce6 | balrog | d->chr_can_read[m](d->ext_opaque[m])) { |
1989 | bd9bdce6 | balrog | d->chr_read[m](d->ext_opaque[m], |
1990 | bd9bdce6 | balrog | &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
|
1991 | bd9bdce6 | balrog | } |
1992 | bd9bdce6 | balrog | } |
1993 | bd9bdce6 | balrog | |
1994 | 20d8a3ed | ths | static int mux_chr_can_read(void *opaque) |
1995 | 20d8a3ed | ths | { |
1996 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
1997 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
1998 | bd9bdce6 | balrog | |
1999 | bd9bdce6 | balrog | if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
|
2000 | bd9bdce6 | balrog | return 1; |
2001 | 20d8a3ed | ths | if (d->chr_can_read[chr->focus])
|
2002 | bd9bdce6 | balrog | return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
|
2003 | 20d8a3ed | ths | return 0; |
2004 | 20d8a3ed | ths | } |
2005 | 20d8a3ed | ths | |
2006 | 20d8a3ed | ths | static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
2007 | 20d8a3ed | ths | { |
2008 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2009 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
2010 | bd9bdce6 | balrog | int m = chr->focus;
|
2011 | 20d8a3ed | ths | int i;
|
2012 | bd9bdce6 | balrog | |
2013 | bd9bdce6 | balrog | mux_chr_accept_input (opaque); |
2014 | bd9bdce6 | balrog | |
2015 | 20d8a3ed | ths | for(i = 0; i < size; i++) |
2016 | bd9bdce6 | balrog | if (mux_proc_byte(chr, d, buf[i])) {
|
2017 | bd9bdce6 | balrog | if (d->prod == d->cons &&
|
2018 | bd9bdce6 | balrog | d->chr_can_read[m] && |
2019 | bd9bdce6 | balrog | d->chr_can_read[m](d->ext_opaque[m])) |
2020 | bd9bdce6 | balrog | d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
|
2021 | bd9bdce6 | balrog | else
|
2022 | bd9bdce6 | balrog | d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i]; |
2023 | bd9bdce6 | balrog | } |
2024 | 20d8a3ed | ths | } |
2025 | 20d8a3ed | ths | |
2026 | 20d8a3ed | ths | static void mux_chr_event(void *opaque, int event) |
2027 | 20d8a3ed | ths | { |
2028 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2029 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
2030 | 20d8a3ed | ths | int i;
|
2031 | 20d8a3ed | ths | |
2032 | 20d8a3ed | ths | /* Send the event to all registered listeners */
|
2033 | 20d8a3ed | ths | for (i = 0; i < d->mux_cnt; i++) |
2034 | 20d8a3ed | ths | if (d->chr_event[i])
|
2035 | 20d8a3ed | ths | d->chr_event[i](d->ext_opaque[i], event); |
2036 | 20d8a3ed | ths | } |
2037 | 20d8a3ed | ths | |
2038 | 20d8a3ed | ths | static void mux_chr_update_read_handler(CharDriverState *chr) |
2039 | 20d8a3ed | ths | { |
2040 | 20d8a3ed | ths | MuxDriver *d = chr->opaque; |
2041 | 20d8a3ed | ths | |
2042 | 20d8a3ed | ths | if (d->mux_cnt >= MAX_MUX) {
|
2043 | 20d8a3ed | ths | fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
|
2044 | 20d8a3ed | ths | return;
|
2045 | 20d8a3ed | ths | } |
2046 | 20d8a3ed | ths | d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
2047 | 20d8a3ed | ths | d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
2048 | 20d8a3ed | ths | d->chr_read[d->mux_cnt] = chr->chr_read; |
2049 | 20d8a3ed | ths | d->chr_event[d->mux_cnt] = chr->chr_event; |
2050 | 20d8a3ed | ths | /* Fix up the real driver with mux routines */
|
2051 | 20d8a3ed | ths | if (d->mux_cnt == 0) { |
2052 | 20d8a3ed | ths | qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
2053 | 20d8a3ed | ths | mux_chr_event, chr); |
2054 | 20d8a3ed | ths | } |
2055 | 20d8a3ed | ths | chr->focus = d->mux_cnt; |
2056 | 20d8a3ed | ths | d->mux_cnt++; |
2057 | 20d8a3ed | ths | } |
2058 | 20d8a3ed | ths | |
2059 | 9596ebb7 | pbrook | static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
|
2060 | 20d8a3ed | ths | { |
2061 | 20d8a3ed | ths | CharDriverState *chr; |
2062 | 20d8a3ed | ths | MuxDriver *d; |
2063 | 20d8a3ed | ths | |
2064 | 20d8a3ed | ths | chr = qemu_mallocz(sizeof(CharDriverState));
|
2065 | 20d8a3ed | ths | if (!chr)
|
2066 | 20d8a3ed | ths | return NULL; |
2067 | 20d8a3ed | ths | d = qemu_mallocz(sizeof(MuxDriver));
|
2068 | 20d8a3ed | ths | if (!d) {
|
2069 | 20d8a3ed | ths | free(chr); |
2070 | 20d8a3ed | ths | return NULL; |
2071 | 20d8a3ed | ths | } |
2072 | 20d8a3ed | ths | |
2073 | 20d8a3ed | ths | chr->opaque = d; |
2074 | 20d8a3ed | ths | d->drv = drv; |
2075 | 20d8a3ed | ths | chr->focus = -1;
|
2076 | 20d8a3ed | ths | chr->chr_write = mux_chr_write; |
2077 | 20d8a3ed | ths | chr->chr_update_read_handler = mux_chr_update_read_handler; |
2078 | bd9bdce6 | balrog | chr->chr_accept_input = mux_chr_accept_input; |
2079 | 20d8a3ed | ths | return chr;
|
2080 | 20d8a3ed | ths | } |
2081 | 20d8a3ed | ths | |
2082 | 20d8a3ed | ths | |
2083 | fd1dff4b | bellard | #ifdef _WIN32
|
2084 | 82c643ff | bellard | |
2085 | fd1dff4b | bellard | static void socket_cleanup(void) |
2086 | fd1dff4b | bellard | { |
2087 | fd1dff4b | bellard | WSACleanup(); |
2088 | fd1dff4b | bellard | } |
2089 | 82c643ff | bellard | |
2090 | fd1dff4b | bellard | static int socket_init(void) |
2091 | fd1dff4b | bellard | { |
2092 | fd1dff4b | bellard | WSADATA Data; |
2093 | fd1dff4b | bellard | int ret, err;
|
2094 | fd1dff4b | bellard | |
2095 | fd1dff4b | bellard | ret = WSAStartup(MAKEWORD(2,2), &Data); |
2096 | fd1dff4b | bellard | if (ret != 0) { |
2097 | fd1dff4b | bellard | err = WSAGetLastError(); |
2098 | fd1dff4b | bellard | fprintf(stderr, "WSAStartup: %d\n", err);
|
2099 | fd1dff4b | bellard | return -1; |
2100 | fd1dff4b | bellard | } |
2101 | fd1dff4b | bellard | atexit(socket_cleanup); |
2102 | fd1dff4b | bellard | return 0; |
2103 | fd1dff4b | bellard | } |
2104 | fd1dff4b | bellard | |
2105 | fd1dff4b | bellard | static int send_all(int fd, const uint8_t *buf, int len1) |
2106 | fd1dff4b | bellard | { |
2107 | fd1dff4b | bellard | int ret, len;
|
2108 | 3b46e624 | ths | |
2109 | fd1dff4b | bellard | len = len1; |
2110 | fd1dff4b | bellard | while (len > 0) { |
2111 | fd1dff4b | bellard | ret = send(fd, buf, len, 0);
|
2112 | fd1dff4b | bellard | if (ret < 0) { |
2113 | fd1dff4b | bellard | int errno;
|
2114 | fd1dff4b | bellard | errno = WSAGetLastError(); |
2115 | fd1dff4b | bellard | if (errno != WSAEWOULDBLOCK) {
|
2116 | fd1dff4b | bellard | return -1; |
2117 | fd1dff4b | bellard | } |
2118 | fd1dff4b | bellard | } else if (ret == 0) { |
2119 | fd1dff4b | bellard | break;
|
2120 | fd1dff4b | bellard | } else {
|
2121 | fd1dff4b | bellard | buf += ret; |
2122 | fd1dff4b | bellard | len -= ret; |
2123 | fd1dff4b | bellard | } |
2124 | fd1dff4b | bellard | } |
2125 | fd1dff4b | bellard | return len1 - len;
|
2126 | fd1dff4b | bellard | } |
2127 | fd1dff4b | bellard | |
2128 | fd1dff4b | bellard | void socket_set_nonblock(int fd) |
2129 | fd1dff4b | bellard | { |
2130 | fd1dff4b | bellard | unsigned long opt = 1; |
2131 | fd1dff4b | bellard | ioctlsocket(fd, FIONBIO, &opt); |
2132 | fd1dff4b | bellard | } |
2133 | fd1dff4b | bellard | |
2134 | fd1dff4b | bellard | #else
|
2135 | fd1dff4b | bellard | |
2136 | 1d96905d | bellard | static int unix_write(int fd, const uint8_t *buf, int len1) |
2137 | 1d96905d | bellard | { |
2138 | 1d96905d | bellard | int ret, len;
|
2139 | 1d96905d | bellard | |
2140 | 1d96905d | bellard | len = len1; |
2141 | 1d96905d | bellard | while (len > 0) { |
2142 | 1d96905d | bellard | ret = write(fd, buf, len); |
2143 | 1d96905d | bellard | if (ret < 0) { |
2144 | 1d96905d | bellard | if (errno != EINTR && errno != EAGAIN)
|
2145 | 1d96905d | bellard | return -1; |
2146 | 1d96905d | bellard | } else if (ret == 0) { |
2147 | 1d96905d | bellard | break;
|
2148 | 1d96905d | bellard | } else {
|
2149 | 1d96905d | bellard | buf += ret; |
2150 | 1d96905d | bellard | len -= ret; |
2151 | 1d96905d | bellard | } |
2152 | 1d96905d | bellard | } |
2153 | 1d96905d | bellard | return len1 - len;
|
2154 | 1d96905d | bellard | } |
2155 | 1d96905d | bellard | |
2156 | fd1dff4b | bellard | static inline int send_all(int fd, const uint8_t *buf, int len1) |
2157 | fd1dff4b | bellard | { |
2158 | fd1dff4b | bellard | return unix_write(fd, buf, len1);
|
2159 | fd1dff4b | bellard | } |
2160 | fd1dff4b | bellard | |
2161 | fd1dff4b | bellard | void socket_set_nonblock(int fd) |
2162 | fd1dff4b | bellard | { |
2163 | fd58ff9d | ths | int f;
|
2164 | fd58ff9d | ths | f = fcntl(fd, F_GETFL); |
2165 | fd58ff9d | ths | fcntl(fd, F_SETFL, f | O_NONBLOCK); |
2166 | fd1dff4b | bellard | } |
2167 | fd1dff4b | bellard | #endif /* !_WIN32 */ |
2168 | fd1dff4b | bellard | |
2169 | fd1dff4b | bellard | #ifndef _WIN32
|
2170 | fd1dff4b | bellard | |
2171 | fd1dff4b | bellard | typedef struct { |
2172 | fd1dff4b | bellard | int fd_in, fd_out;
|
2173 | fd1dff4b | bellard | int max_size;
|
2174 | fd1dff4b | bellard | } FDCharDriver; |
2175 | fd1dff4b | bellard | |
2176 | 20d8a3ed | ths | #define STDIO_MAX_CLIENTS 1 |
2177 | 20d8a3ed | ths | static int stdio_nb_clients = 0; |
2178 | fd1dff4b | bellard | |
2179 | 82c643ff | bellard | static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
2180 | 82c643ff | bellard | { |
2181 | 82c643ff | bellard | FDCharDriver *s = chr->opaque; |
2182 | 1d96905d | bellard | return unix_write(s->fd_out, buf, len);
|
2183 | 82c643ff | bellard | } |
2184 | 82c643ff | bellard | |
2185 | 7c9d8e07 | bellard | static int fd_chr_read_poll(void *opaque) |
2186 | 7c9d8e07 | bellard | { |
2187 | 7c9d8e07 | bellard | CharDriverState *chr = opaque; |
2188 | 7c9d8e07 | bellard | FDCharDriver *s = chr->opaque; |
2189 | 7c9d8e07 | bellard | |
2190 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
2191 | 7c9d8e07 | bellard | return s->max_size;
|
2192 | 7c9d8e07 | bellard | } |
2193 | 7c9d8e07 | bellard | |
2194 | 7c9d8e07 | bellard | static void fd_chr_read(void *opaque) |
2195 | 7c9d8e07 | bellard | { |
2196 | 7c9d8e07 | bellard | CharDriverState *chr = opaque; |
2197 | 7c9d8e07 | bellard | FDCharDriver *s = chr->opaque; |
2198 | 7c9d8e07 | bellard | int size, len;
|
2199 | 7c9d8e07 | bellard | uint8_t buf[1024];
|
2200 | 3b46e624 | ths | |
2201 | 7c9d8e07 | bellard | len = sizeof(buf);
|
2202 | 7c9d8e07 | bellard | if (len > s->max_size)
|
2203 | 7c9d8e07 | bellard | len = s->max_size; |
2204 | 7c9d8e07 | bellard | if (len == 0) |
2205 | 7c9d8e07 | bellard | return;
|
2206 | 7c9d8e07 | bellard | size = read(s->fd_in, buf, len); |
2207 | 188157fe | pbrook | if (size == 0) { |
2208 | 188157fe | pbrook | /* FD has been closed. Remove it from the active list. */
|
2209 | 188157fe | pbrook | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
2210 | 188157fe | pbrook | return;
|
2211 | 188157fe | pbrook | } |
2212 | 7c9d8e07 | bellard | if (size > 0) { |
2213 | e5b0bc44 | pbrook | qemu_chr_read(chr, buf, size); |
2214 | 7c9d8e07 | bellard | } |
2215 | 7c9d8e07 | bellard | } |
2216 | 7c9d8e07 | bellard | |
2217 | e5b0bc44 | pbrook | static void fd_chr_update_read_handler(CharDriverState *chr) |
2218 | 82c643ff | bellard | { |
2219 | 82c643ff | bellard | FDCharDriver *s = chr->opaque; |
2220 | 82c643ff | bellard | |
2221 | f8d179e3 | bellard | if (s->fd_in >= 0) { |
2222 | f8d179e3 | bellard | if (nographic && s->fd_in == 0) { |
2223 | f8d179e3 | bellard | } else {
|
2224 | 5fafdf24 | ths | qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, |
2225 | 7c9d8e07 | bellard | fd_chr_read, NULL, chr);
|
2226 | f8d179e3 | bellard | } |
2227 | 82c643ff | bellard | } |
2228 | 82c643ff | bellard | } |
2229 | 82c643ff | bellard | |
2230 | a11d070e | balrog | static void fd_chr_close(struct CharDriverState *chr) |
2231 | a11d070e | balrog | { |
2232 | a11d070e | balrog | FDCharDriver *s = chr->opaque; |
2233 | a11d070e | balrog | |
2234 | a11d070e | balrog | if (s->fd_in >= 0) { |
2235 | a11d070e | balrog | if (nographic && s->fd_in == 0) { |
2236 | a11d070e | balrog | } else {
|
2237 | a11d070e | balrog | qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
2238 | a11d070e | balrog | } |
2239 | a11d070e | balrog | } |
2240 | a11d070e | balrog | |
2241 | a11d070e | balrog | qemu_free(s); |
2242 | a11d070e | balrog | } |
2243 | a11d070e | balrog | |
2244 | 82c643ff | bellard | /* open a character device to a unix fd */
|
2245 | 52f61fde | ths | static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
2246 | 82c643ff | bellard | { |
2247 | 82c643ff | bellard | CharDriverState *chr; |
2248 | 82c643ff | bellard | FDCharDriver *s; |
2249 | 82c643ff | bellard | |
2250 | 82c643ff | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
2251 | 82c643ff | bellard | if (!chr)
|
2252 | 82c643ff | bellard | return NULL; |
2253 | 82c643ff | bellard | s = qemu_mallocz(sizeof(FDCharDriver));
|
2254 | 82c643ff | bellard | if (!s) {
|
2255 | 82c643ff | bellard | free(chr); |
2256 | 82c643ff | bellard | return NULL; |
2257 | 82c643ff | bellard | } |
2258 | 82c643ff | bellard | s->fd_in = fd_in; |
2259 | 82c643ff | bellard | s->fd_out = fd_out; |
2260 | 82c643ff | bellard | chr->opaque = s; |
2261 | 82c643ff | bellard | chr->chr_write = fd_chr_write; |
2262 | e5b0bc44 | pbrook | chr->chr_update_read_handler = fd_chr_update_read_handler; |
2263 | a11d070e | balrog | chr->chr_close = fd_chr_close; |
2264 | 86e94dea | ths | |
2265 | 86e94dea | ths | qemu_chr_reset(chr); |
2266 | 86e94dea | ths | |
2267 | 82c643ff | bellard | return chr;
|
2268 | 82c643ff | bellard | } |
2269 | 82c643ff | bellard | |
2270 | 52f61fde | ths | static CharDriverState *qemu_chr_open_file_out(const char *file_out) |
2271 | f8d179e3 | bellard | { |
2272 | f8d179e3 | bellard | int fd_out;
|
2273 | f8d179e3 | bellard | |
2274 | aeb30be6 | balrog | TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
|
2275 | f8d179e3 | bellard | if (fd_out < 0) |
2276 | f8d179e3 | bellard | return NULL; |
2277 | f8d179e3 | bellard | return qemu_chr_open_fd(-1, fd_out); |
2278 | f8d179e3 | bellard | } |
2279 | f8d179e3 | bellard | |
2280 | 52f61fde | ths | static CharDriverState *qemu_chr_open_pipe(const char *filename) |
2281 | f8d179e3 | bellard | { |
2282 | c26c1c4b | ths | int fd_in, fd_out;
|
2283 | c26c1c4b | ths | char filename_in[256], filename_out[256]; |
2284 | c26c1c4b | ths | |
2285 | c26c1c4b | ths | snprintf(filename_in, 256, "%s.in", filename); |
2286 | c26c1c4b | ths | snprintf(filename_out, 256, "%s.out", filename); |
2287 | aeb30be6 | balrog | TFR(fd_in = open(filename_in, O_RDWR | O_BINARY)); |
2288 | aeb30be6 | balrog | TFR(fd_out = open(filename_out, O_RDWR | O_BINARY)); |
2289 | c26c1c4b | ths | if (fd_in < 0 || fd_out < 0) { |
2290 | c26c1c4b | ths | if (fd_in >= 0) |
2291 | c26c1c4b | ths | close(fd_in); |
2292 | c26c1c4b | ths | if (fd_out >= 0) |
2293 | c26c1c4b | ths | close(fd_out); |
2294 | aeb30be6 | balrog | TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY)); |
2295 | c26c1c4b | ths | if (fd_in < 0) |
2296 | c26c1c4b | ths | return NULL; |
2297 | c26c1c4b | ths | } |
2298 | c26c1c4b | ths | return qemu_chr_open_fd(fd_in, fd_out);
|
2299 | f8d179e3 | bellard | } |
2300 | f8d179e3 | bellard | |
2301 | f8d179e3 | bellard | |
2302 | 82c643ff | bellard | /* for STDIO, we handle the case where several clients use it
|
2303 | 82c643ff | bellard | (nographic mode) */
|
2304 | 82c643ff | bellard | |
2305 | aa0bc6b6 | bellard | #define TERM_FIFO_MAX_SIZE 1 |
2306 | aa0bc6b6 | bellard | |
2307 | aa0bc6b6 | bellard | static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
|
2308 | 1dce7c3c | bellard | static int term_fifo_size; |
2309 | 330d0414 | bellard | |
2310 | 7c9d8e07 | bellard | static int stdio_read_poll(void *opaque) |
2311 | 82c643ff | bellard | { |
2312 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2313 | aa0bc6b6 | bellard | |
2314 | 20d8a3ed | ths | /* try to flush the queue if needed */
|
2315 | 20d8a3ed | ths | if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) { |
2316 | 20d8a3ed | ths | qemu_chr_read(chr, term_fifo, 1);
|
2317 | 20d8a3ed | ths | term_fifo_size = 0;
|
2318 | aa0bc6b6 | bellard | } |
2319 | 20d8a3ed | ths | /* see if we can absorb more chars */
|
2320 | 20d8a3ed | ths | if (term_fifo_size == 0) |
2321 | 20d8a3ed | ths | return 1; |
2322 | 20d8a3ed | ths | else
|
2323 | 20d8a3ed | ths | return 0; |
2324 | 82c643ff | bellard | } |
2325 | 82c643ff | bellard | |
2326 | 7c9d8e07 | bellard | static void stdio_read(void *opaque) |
2327 | 82c643ff | bellard | { |
2328 | 7c9d8e07 | bellard | int size;
|
2329 | 7c9d8e07 | bellard | uint8_t buf[1];
|
2330 | 20d8a3ed | ths | CharDriverState *chr = opaque; |
2331 | 20d8a3ed | ths | |
2332 | 7c9d8e07 | bellard | size = read(0, buf, 1); |
2333 | 519945df | pbrook | if (size == 0) { |
2334 | 519945df | pbrook | /* stdin has been closed. Remove it from the active list. */
|
2335 | 519945df | pbrook | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
2336 | 519945df | pbrook | return;
|
2337 | 519945df | pbrook | } |
2338 | 20d8a3ed | ths | if (size > 0) { |
2339 | 20d8a3ed | ths | if (qemu_chr_can_read(chr) > 0) { |
2340 | 20d8a3ed | ths | qemu_chr_read(chr, buf, 1);
|
2341 | 20d8a3ed | ths | } else if (term_fifo_size == 0) { |
2342 | 20d8a3ed | ths | term_fifo[term_fifo_size++] = buf[0];
|
2343 | 1dce7c3c | bellard | } |
2344 | 1dce7c3c | bellard | } |
2345 | 1dce7c3c | bellard | } |
2346 | 1dce7c3c | bellard | |
2347 | 8d11df9e | bellard | /* init terminal so that we can grab keys */
|
2348 | 8d11df9e | bellard | static struct termios oldtty; |
2349 | 8d11df9e | bellard | static int old_fd0_flags; |
2350 | a11d070e | balrog | static int term_atexit_done; |
2351 | 8d11df9e | bellard | |
2352 | 8d11df9e | bellard | static void term_exit(void) |
2353 | 8d11df9e | bellard | { |
2354 | 8d11df9e | bellard | tcsetattr (0, TCSANOW, &oldtty);
|
2355 | 8d11df9e | bellard | fcntl(0, F_SETFL, old_fd0_flags);
|
2356 | 8d11df9e | bellard | } |
2357 | 8d11df9e | bellard | |
2358 | 8d11df9e | bellard | static void term_init(void) |
2359 | 8d11df9e | bellard | { |
2360 | 8d11df9e | bellard | struct termios tty;
|
2361 | 8d11df9e | bellard | |
2362 | 8d11df9e | bellard | tcgetattr (0, &tty);
|
2363 | 8d11df9e | bellard | oldtty = tty; |
2364 | 8d11df9e | bellard | old_fd0_flags = fcntl(0, F_GETFL);
|
2365 | 8d11df9e | bellard | |
2366 | 8d11df9e | bellard | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
2367 | 8d11df9e | bellard | |INLCR|IGNCR|ICRNL|IXON); |
2368 | 8d11df9e | bellard | tty.c_oflag |= OPOST; |
2369 | 8d11df9e | bellard | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
2370 | 8d11df9e | bellard | /* if graphical mode, we allow Ctrl-C handling */
|
2371 | 8d11df9e | bellard | if (nographic)
|
2372 | 8d11df9e | bellard | tty.c_lflag &= ~ISIG; |
2373 | 8d11df9e | bellard | tty.c_cflag &= ~(CSIZE|PARENB); |
2374 | 8d11df9e | bellard | tty.c_cflag |= CS8; |
2375 | 8d11df9e | bellard | tty.c_cc[VMIN] = 1;
|
2376 | 8d11df9e | bellard | tty.c_cc[VTIME] = 0;
|
2377 | 3b46e624 | ths | |
2378 | 8d11df9e | bellard | tcsetattr (0, TCSANOW, &tty);
|
2379 | 8d11df9e | bellard | |
2380 | a11d070e | balrog | if (!term_atexit_done++)
|
2381 | a11d070e | balrog | atexit(term_exit); |
2382 | 8d11df9e | bellard | |
2383 | 8d11df9e | bellard | fcntl(0, F_SETFL, O_NONBLOCK);
|
2384 | 8d11df9e | bellard | } |
2385 | 8d11df9e | bellard | |
2386 | a11d070e | balrog | static void qemu_chr_close_stdio(struct CharDriverState *chr) |
2387 | a11d070e | balrog | { |
2388 | a11d070e | balrog | term_exit(); |
2389 | a11d070e | balrog | stdio_nb_clients--; |
2390 | a11d070e | balrog | qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
2391 | a11d070e | balrog | fd_chr_close(chr); |
2392 | a11d070e | balrog | } |
2393 | a11d070e | balrog | |
2394 | 52f61fde | ths | static CharDriverState *qemu_chr_open_stdio(void) |
2395 | 82c643ff | bellard | { |
2396 | 82c643ff | bellard | CharDriverState *chr; |
2397 | 82c643ff | bellard | |
2398 | 20d8a3ed | ths | if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
|
2399 | 20d8a3ed | ths | return NULL; |
2400 | 20d8a3ed | ths | chr = qemu_chr_open_fd(0, 1); |
2401 | a11d070e | balrog | chr->chr_close = qemu_chr_close_stdio; |
2402 | 20d8a3ed | ths | qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr); |
2403 | 20d8a3ed | ths | stdio_nb_clients++; |
2404 | 20d8a3ed | ths | term_init(); |
2405 | 20d8a3ed | ths | |
2406 | 82c643ff | bellard | return chr;
|
2407 | 82c643ff | bellard | } |
2408 | 82c643ff | bellard | |
2409 | 64b7b733 | aurel32 | #ifdef __sun__
|
2410 | 64b7b733 | aurel32 | /* Once Solaris has openpty(), this is going to be removed. */
|
2411 | 64b7b733 | aurel32 | int openpty(int *amaster, int *aslave, char *name, |
2412 | 64b7b733 | aurel32 | struct termios *termp, struct winsize *winp) |
2413 | 64b7b733 | aurel32 | { |
2414 | 64b7b733 | aurel32 | const char *slave; |
2415 | 64b7b733 | aurel32 | int mfd = -1, sfd = -1; |
2416 | 64b7b733 | aurel32 | |
2417 | 64b7b733 | aurel32 | *amaster = *aslave = -1;
|
2418 | 64b7b733 | aurel32 | |
2419 | 64b7b733 | aurel32 | mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
2420 | 64b7b733 | aurel32 | if (mfd < 0) |
2421 | 64b7b733 | aurel32 | goto err;
|
2422 | 64b7b733 | aurel32 | |
2423 | 64b7b733 | aurel32 | if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) |
2424 | 64b7b733 | aurel32 | goto err;
|
2425 | 64b7b733 | aurel32 | |
2426 | 64b7b733 | aurel32 | if ((slave = ptsname(mfd)) == NULL) |
2427 | 64b7b733 | aurel32 | goto err;
|
2428 | 64b7b733 | aurel32 | |
2429 | 64b7b733 | aurel32 | if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) |
2430 | 64b7b733 | aurel32 | goto err;
|
2431 | 64b7b733 | aurel32 | |
2432 | 64b7b733 | aurel32 | if (ioctl(sfd, I_PUSH, "ptem") == -1 || |
2433 | 64b7b733 | aurel32 | (termp != NULL && tcgetattr(sfd, termp) < 0)) |
2434 | 64b7b733 | aurel32 | goto err;
|
2435 | 64b7b733 | aurel32 | |
2436 | 64b7b733 | aurel32 | if (amaster)
|
2437 | 64b7b733 | aurel32 | *amaster = mfd; |
2438 | 64b7b733 | aurel32 | if (aslave)
|
2439 | 64b7b733 | aurel32 | *aslave = sfd; |
2440 | 64b7b733 | aurel32 | if (winp)
|
2441 | 64b7b733 | aurel32 | ioctl(sfd, TIOCSWINSZ, winp); |
2442 | 64b7b733 | aurel32 | |
2443 | 64b7b733 | aurel32 | return 0; |
2444 | 64b7b733 | aurel32 | |
2445 | 64b7b733 | aurel32 | err:
|
2446 | 64b7b733 | aurel32 | if (sfd != -1) |
2447 | 64b7b733 | aurel32 | close(sfd); |
2448 | 64b7b733 | aurel32 | close(mfd); |
2449 | 64b7b733 | aurel32 | return -1; |
2450 | 64b7b733 | aurel32 | } |
2451 | 64b7b733 | aurel32 | |
2452 | 64b7b733 | aurel32 | void cfmakeraw (struct termios *termios_p) |
2453 | 64b7b733 | aurel32 | { |
2454 | 64b7b733 | aurel32 | termios_p->c_iflag &= |
2455 | 64b7b733 | aurel32 | ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); |
2456 | 64b7b733 | aurel32 | termios_p->c_oflag &= ~OPOST; |
2457 | 64b7b733 | aurel32 | termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); |
2458 | 64b7b733 | aurel32 | termios_p->c_cflag &= ~(CSIZE|PARENB); |
2459 | 64b7b733 | aurel32 | termios_p->c_cflag |= CS8; |
2460 | 64b7b733 | aurel32 | |
2461 | 64b7b733 | aurel32 | termios_p->c_cc[VMIN] = 0;
|
2462 | 64b7b733 | aurel32 | termios_p->c_cc[VTIME] = 0;
|
2463 | 64b7b733 | aurel32 | } |
2464 | 64b7b733 | aurel32 | #endif
|
2465 | 64b7b733 | aurel32 | |
2466 | aec62507 | ths | #if defined(__linux__) || defined(__sun__)
|
2467 | 279e694b | aliguori | |
2468 | 279e694b | aliguori | typedef struct { |
2469 | 279e694b | aliguori | int fd;
|
2470 | 279e694b | aliguori | int connected;
|
2471 | 279e694b | aliguori | int polling;
|
2472 | 279e694b | aliguori | int read_bytes;
|
2473 | 279e694b | aliguori | QEMUTimer *timer; |
2474 | 279e694b | aliguori | } PtyCharDriver; |
2475 | 279e694b | aliguori | |
2476 | 279e694b | aliguori | static void pty_chr_update_read_handler(CharDriverState *chr); |
2477 | 279e694b | aliguori | static void pty_chr_state(CharDriverState *chr, int connected); |
2478 | 279e694b | aliguori | |
2479 | 279e694b | aliguori | static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
2480 | 279e694b | aliguori | { |
2481 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2482 | 279e694b | aliguori | |
2483 | 279e694b | aliguori | if (!s->connected) {
|
2484 | 279e694b | aliguori | /* guest sends data, check for (re-)connect */
|
2485 | 279e694b | aliguori | pty_chr_update_read_handler(chr); |
2486 | 279e694b | aliguori | return 0; |
2487 | 279e694b | aliguori | } |
2488 | 279e694b | aliguori | return unix_write(s->fd, buf, len);
|
2489 | 279e694b | aliguori | } |
2490 | 279e694b | aliguori | |
2491 | 279e694b | aliguori | static int pty_chr_read_poll(void *opaque) |
2492 | 279e694b | aliguori | { |
2493 | 279e694b | aliguori | CharDriverState *chr = opaque; |
2494 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2495 | 279e694b | aliguori | |
2496 | 279e694b | aliguori | s->read_bytes = qemu_chr_can_read(chr); |
2497 | 279e694b | aliguori | return s->read_bytes;
|
2498 | 279e694b | aliguori | } |
2499 | 279e694b | aliguori | |
2500 | 279e694b | aliguori | static void pty_chr_read(void *opaque) |
2501 | 279e694b | aliguori | { |
2502 | 279e694b | aliguori | CharDriverState *chr = opaque; |
2503 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2504 | 279e694b | aliguori | int size, len;
|
2505 | 279e694b | aliguori | uint8_t buf[1024];
|
2506 | 279e694b | aliguori | |
2507 | 279e694b | aliguori | len = sizeof(buf);
|
2508 | 279e694b | aliguori | if (len > s->read_bytes)
|
2509 | 279e694b | aliguori | len = s->read_bytes; |
2510 | 279e694b | aliguori | if (len == 0) |
2511 | 279e694b | aliguori | return;
|
2512 | 279e694b | aliguori | size = read(s->fd, buf, len); |
2513 | 279e694b | aliguori | if ((size == -1 && errno == EIO) || |
2514 | 279e694b | aliguori | (size == 0)) {
|
2515 | 279e694b | aliguori | pty_chr_state(chr, 0);
|
2516 | 279e694b | aliguori | return;
|
2517 | 279e694b | aliguori | } |
2518 | 279e694b | aliguori | if (size > 0) { |
2519 | 279e694b | aliguori | pty_chr_state(chr, 1);
|
2520 | 279e694b | aliguori | qemu_chr_read(chr, buf, size); |
2521 | 279e694b | aliguori | } |
2522 | 279e694b | aliguori | } |
2523 | 279e694b | aliguori | |
2524 | 279e694b | aliguori | static void pty_chr_update_read_handler(CharDriverState *chr) |
2525 | 279e694b | aliguori | { |
2526 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2527 | 279e694b | aliguori | |
2528 | 279e694b | aliguori | qemu_set_fd_handler2(s->fd, pty_chr_read_poll, |
2529 | 279e694b | aliguori | pty_chr_read, NULL, chr);
|
2530 | 279e694b | aliguori | s->polling = 1;
|
2531 | 279e694b | aliguori | /*
|
2532 | 279e694b | aliguori | * Short timeout here: just need wait long enougth that qemu makes
|
2533 | 279e694b | aliguori | * it through the poll loop once. When reconnected we want a
|
2534 | 279e694b | aliguori | * short timeout so we notice it almost instantly. Otherwise
|
2535 | 279e694b | aliguori | * read() gives us -EIO instantly, making pty_chr_state() reset the
|
2536 | 279e694b | aliguori | * timeout to the normal (much longer) poll interval before the
|
2537 | 279e694b | aliguori | * timer triggers.
|
2538 | 279e694b | aliguori | */
|
2539 | 279e694b | aliguori | qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
|
2540 | 279e694b | aliguori | } |
2541 | 279e694b | aliguori | |
2542 | 279e694b | aliguori | static void pty_chr_state(CharDriverState *chr, int connected) |
2543 | 279e694b | aliguori | { |
2544 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2545 | 279e694b | aliguori | |
2546 | 279e694b | aliguori | if (!connected) {
|
2547 | 279e694b | aliguori | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
2548 | 279e694b | aliguori | s->connected = 0;
|
2549 | 279e694b | aliguori | s->polling = 0;
|
2550 | 279e694b | aliguori | /* (re-)connect poll interval for idle guests: once per second.
|
2551 | 279e694b | aliguori | * We check more frequently in case the guests sends data to
|
2552 | 279e694b | aliguori | * the virtual device linked to our pty. */
|
2553 | 279e694b | aliguori | qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
|
2554 | 279e694b | aliguori | } else {
|
2555 | 279e694b | aliguori | if (!s->connected)
|
2556 | 279e694b | aliguori | qemu_chr_reset(chr); |
2557 | 279e694b | aliguori | s->connected = 1;
|
2558 | 279e694b | aliguori | } |
2559 | 279e694b | aliguori | } |
2560 | 279e694b | aliguori | |
2561 | 279e694b | aliguori | void pty_chr_timer(void *opaque) |
2562 | 279e694b | aliguori | { |
2563 | 279e694b | aliguori | struct CharDriverState *chr = opaque;
|
2564 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2565 | 279e694b | aliguori | |
2566 | 279e694b | aliguori | if (s->connected)
|
2567 | 279e694b | aliguori | return;
|
2568 | 279e694b | aliguori | if (s->polling) {
|
2569 | 279e694b | aliguori | /* If we arrive here without polling being cleared due
|
2570 | 279e694b | aliguori | * read returning -EIO, then we are (re-)connected */
|
2571 | 279e694b | aliguori | pty_chr_state(chr, 1);
|
2572 | 279e694b | aliguori | return;
|
2573 | 279e694b | aliguori | } |
2574 | 279e694b | aliguori | |
2575 | 279e694b | aliguori | /* Next poll ... */
|
2576 | 279e694b | aliguori | pty_chr_update_read_handler(chr); |
2577 | 279e694b | aliguori | } |
2578 | 279e694b | aliguori | |
2579 | 279e694b | aliguori | static void pty_chr_close(struct CharDriverState *chr) |
2580 | 279e694b | aliguori | { |
2581 | 279e694b | aliguori | PtyCharDriver *s = chr->opaque; |
2582 | 279e694b | aliguori | |
2583 | 279e694b | aliguori | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
2584 | 279e694b | aliguori | close(s->fd); |
2585 | 279e694b | aliguori | qemu_free(s); |
2586 | 279e694b | aliguori | } |
2587 | 279e694b | aliguori | |
2588 | 52f61fde | ths | static CharDriverState *qemu_chr_open_pty(void) |
2589 | 82c643ff | bellard | { |
2590 | 279e694b | aliguori | CharDriverState *chr; |
2591 | 279e694b | aliguori | PtyCharDriver *s; |
2592 | 91fc2119 | bellard | struct termios tty;
|
2593 | 279e694b | aliguori | int slave_fd;
|
2594 | 279e694b | aliguori | |
2595 | 279e694b | aliguori | chr = qemu_mallocz(sizeof(CharDriverState));
|
2596 | 279e694b | aliguori | if (!chr)
|
2597 | 279e694b | aliguori | return NULL; |
2598 | 279e694b | aliguori | s = qemu_mallocz(sizeof(PtyCharDriver));
|
2599 | 279e694b | aliguori | if (!s) {
|
2600 | 279e694b | aliguori | qemu_free(chr); |
2601 | 279e694b | aliguori | return NULL; |
2602 | 279e694b | aliguori | } |
2603 | 3b46e624 | ths | |
2604 | 279e694b | aliguori | if (openpty(&s->fd, &slave_fd, NULL, NULL, NULL) < 0) { |
2605 | 82c643ff | bellard | return NULL; |
2606 | 82c643ff | bellard | } |
2607 | 3b46e624 | ths | |
2608 | 64b7b733 | aurel32 | /* Set raw attributes on the pty. */
|
2609 | 64b7b733 | aurel32 | cfmakeraw(&tty); |
2610 | 64b7b733 | aurel32 | tcsetattr(slave_fd, TCSAFLUSH, &tty); |
2611 | 279e694b | aliguori | close(slave_fd); |
2612 | 279e694b | aliguori | |
2613 | 279e694b | aliguori | fprintf(stderr, "char device redirected to %s\n", ptsname(s->fd));
|
2614 | 91fc2119 | bellard | |
2615 | 279e694b | aliguori | chr->opaque = s; |
2616 | 279e694b | aliguori | chr->chr_write = pty_chr_write; |
2617 | 279e694b | aliguori | chr->chr_update_read_handler = pty_chr_update_read_handler; |
2618 | 279e694b | aliguori | chr->chr_close = pty_chr_close; |
2619 | 279e694b | aliguori | |
2620 | 279e694b | aliguori | s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr); |
2621 | 279e694b | aliguori | |
2622 | 279e694b | aliguori | return chr;
|
2623 | 82c643ff | bellard | } |
2624 | f8d179e3 | bellard | |
2625 | 5fafdf24 | ths | static void tty_serial_init(int fd, int speed, |
2626 | f8d179e3 | bellard | int parity, int data_bits, int stop_bits) |
2627 | f8d179e3 | bellard | { |
2628 | f8d179e3 | bellard | struct termios tty;
|
2629 | f8d179e3 | bellard | speed_t spd; |
2630 | f8d179e3 | bellard | |
2631 | e57a8c0e | bellard | #if 0
|
2632 | 5fafdf24 | ths | printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
|
2633 | e57a8c0e | bellard | speed, parity, data_bits, stop_bits);
|
2634 | e57a8c0e | bellard | #endif
|
2635 | e57a8c0e | bellard | tcgetattr (fd, &tty); |
2636 | f8d179e3 | bellard | |
2637 | a7954218 | balrog | #define MARGIN 1.1 |
2638 | a7954218 | balrog | if (speed <= 50 * MARGIN) |
2639 | f8d179e3 | bellard | spd = B50; |
2640 | a7954218 | balrog | else if (speed <= 75 * MARGIN) |
2641 | f8d179e3 | bellard | spd = B75; |
2642 | a7954218 | balrog | else if (speed <= 300 * MARGIN) |
2643 | f8d179e3 | bellard | spd = B300; |
2644 | a7954218 | balrog | else if (speed <= 600 * MARGIN) |
2645 | f8d179e3 | bellard | spd = B600; |
2646 | a7954218 | balrog | else if (speed <= 1200 * MARGIN) |
2647 | f8d179e3 | bellard | spd = B1200; |
2648 | a7954218 | balrog | else if (speed <= 2400 * MARGIN) |
2649 | f8d179e3 | bellard | spd = B2400; |
2650 | a7954218 | balrog | else if (speed <= 4800 * MARGIN) |
2651 | f8d179e3 | bellard | spd = B4800; |
2652 | a7954218 | balrog | else if (speed <= 9600 * MARGIN) |
2653 | f8d179e3 | bellard | spd = B9600; |
2654 | a7954218 | balrog | else if (speed <= 19200 * MARGIN) |
2655 | f8d179e3 | bellard | spd = B19200; |
2656 | a7954218 | balrog | else if (speed <= 38400 * MARGIN) |
2657 | f8d179e3 | bellard | spd = B38400; |
2658 | a7954218 | balrog | else if (speed <= 57600 * MARGIN) |
2659 | f8d179e3 | bellard | spd = B57600; |
2660 | a7954218 | balrog | else if (speed <= 115200 * MARGIN) |
2661 | a7954218 | balrog | spd = B115200; |
2662 | a7954218 | balrog | else
|
2663 | f8d179e3 | bellard | spd = B115200; |
2664 | f8d179e3 | bellard | |
2665 | f8d179e3 | bellard | cfsetispeed(&tty, spd); |
2666 | f8d179e3 | bellard | cfsetospeed(&tty, spd); |
2667 | f8d179e3 | bellard | |
2668 | f8d179e3 | bellard | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
2669 | f8d179e3 | bellard | |INLCR|IGNCR|ICRNL|IXON); |
2670 | f8d179e3 | bellard | tty.c_oflag |= OPOST; |
2671 | f8d179e3 | bellard | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
2672 | 094eed6c | bellard | tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
2673 | f8d179e3 | bellard | switch(data_bits) {
|
2674 | f8d179e3 | bellard | default:
|
2675 | f8d179e3 | bellard | case 8: |
2676 | f8d179e3 | bellard | tty.c_cflag |= CS8; |
2677 | f8d179e3 | bellard | break;
|
2678 | f8d179e3 | bellard | case 7: |
2679 | f8d179e3 | bellard | tty.c_cflag |= CS7; |
2680 | f8d179e3 | bellard | break;
|
2681 | f8d179e3 | bellard | case 6: |
2682 | f8d179e3 | bellard | tty.c_cflag |= CS6; |
2683 | f8d179e3 | bellard | break;
|
2684 | f8d179e3 | bellard | case 5: |
2685 | f8d179e3 | bellard | tty.c_cflag |= CS5; |
2686 | f8d179e3 | bellard | break;
|
2687 | f8d179e3 | bellard | } |
2688 | f8d179e3 | bellard | switch(parity) {
|
2689 | f8d179e3 | bellard | default:
|
2690 | f8d179e3 | bellard | case 'N': |
2691 | f8d179e3 | bellard | break;
|
2692 | f8d179e3 | bellard | case 'E': |
2693 | f8d179e3 | bellard | tty.c_cflag |= PARENB; |
2694 | f8d179e3 | bellard | break;
|
2695 | f8d179e3 | bellard | case 'O': |
2696 | f8d179e3 | bellard | tty.c_cflag |= PARENB | PARODD; |
2697 | f8d179e3 | bellard | break;
|
2698 | f8d179e3 | bellard | } |
2699 | 094eed6c | bellard | if (stop_bits == 2) |
2700 | 094eed6c | bellard | tty.c_cflag |= CSTOPB; |
2701 | 3b46e624 | ths | |
2702 | f8d179e3 | bellard | tcsetattr (fd, TCSANOW, &tty); |
2703 | f8d179e3 | bellard | } |
2704 | f8d179e3 | bellard | |
2705 | e57a8c0e | bellard | static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
2706 | f8d179e3 | bellard | { |
2707 | f8d179e3 | bellard | FDCharDriver *s = chr->opaque; |
2708 | 3b46e624 | ths | |
2709 | e57a8c0e | bellard | switch(cmd) {
|
2710 | e57a8c0e | bellard | case CHR_IOCTL_SERIAL_SET_PARAMS:
|
2711 | e57a8c0e | bellard | { |
2712 | e57a8c0e | bellard | QEMUSerialSetParams *ssp = arg; |
2713 | 5fafdf24 | ths | tty_serial_init(s->fd_in, ssp->speed, ssp->parity, |
2714 | e57a8c0e | bellard | ssp->data_bits, ssp->stop_bits); |
2715 | e57a8c0e | bellard | } |
2716 | e57a8c0e | bellard | break;
|
2717 | e57a8c0e | bellard | case CHR_IOCTL_SERIAL_SET_BREAK:
|
2718 | e57a8c0e | bellard | { |
2719 | e57a8c0e | bellard | int enable = *(int *)arg; |
2720 | e57a8c0e | bellard | if (enable)
|
2721 | e57a8c0e | bellard | tcsendbreak(s->fd_in, 1);
|
2722 | e57a8c0e | bellard | } |
2723 | e57a8c0e | bellard | break;
|
2724 | e57a8c0e | bellard | default:
|
2725 | e57a8c0e | bellard | return -ENOTSUP;
|
2726 | e57a8c0e | bellard | } |
2727 | e57a8c0e | bellard | return 0; |
2728 | f8d179e3 | bellard | } |
2729 | f8d179e3 | bellard | |
2730 | 52f61fde | ths | static CharDriverState *qemu_chr_open_tty(const char *filename) |
2731 | f8d179e3 | bellard | { |
2732 | f8d179e3 | bellard | CharDriverState *chr; |
2733 | f8d179e3 | bellard | int fd;
|
2734 | f8d179e3 | bellard | |
2735 | aeb30be6 | balrog | TFR(fd = open(filename, O_RDWR | O_NONBLOCK)); |
2736 | f8d179e3 | bellard | tty_serial_init(fd, 115200, 'N', 8, 1); |
2737 | f8d179e3 | bellard | chr = qemu_chr_open_fd(fd, fd); |
2738 | aeb30be6 | balrog | if (!chr) {
|
2739 | aeb30be6 | balrog | close(fd); |
2740 | f8d179e3 | bellard | return NULL; |
2741 | aeb30be6 | balrog | } |
2742 | e57a8c0e | bellard | chr->chr_ioctl = tty_serial_ioctl; |
2743 | 86e94dea | ths | qemu_chr_reset(chr); |
2744 | e57a8c0e | bellard | return chr;
|
2745 | e57a8c0e | bellard | } |
2746 | aec62507 | ths | #else /* ! __linux__ && ! __sun__ */ |
2747 | aec62507 | ths | static CharDriverState *qemu_chr_open_pty(void) |
2748 | aec62507 | ths | { |
2749 | aec62507 | ths | return NULL; |
2750 | aec62507 | ths | } |
2751 | aec62507 | ths | #endif /* __linux__ || __sun__ */ |
2752 | e57a8c0e | bellard | |
2753 | aec62507 | ths | #if defined(__linux__)
|
2754 | 5867c88a | ths | typedef struct { |
2755 | 5867c88a | ths | int fd;
|
2756 | 5867c88a | ths | int mode;
|
2757 | 5867c88a | ths | } ParallelCharDriver; |
2758 | 5867c88a | ths | |
2759 | 5867c88a | ths | static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
2760 | 5867c88a | ths | { |
2761 | 5867c88a | ths | if (s->mode != mode) {
|
2762 | 5867c88a | ths | int m = mode;
|
2763 | 5867c88a | ths | if (ioctl(s->fd, PPSETMODE, &m) < 0) |
2764 | 5867c88a | ths | return 0; |
2765 | 5867c88a | ths | s->mode = mode; |
2766 | 5867c88a | ths | } |
2767 | 5867c88a | ths | return 1; |
2768 | 5867c88a | ths | } |
2769 | 5867c88a | ths | |
2770 | e57a8c0e | bellard | static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
2771 | e57a8c0e | bellard | { |
2772 | 5867c88a | ths | ParallelCharDriver *drv = chr->opaque; |
2773 | 5867c88a | ths | int fd = drv->fd;
|
2774 | e57a8c0e | bellard | uint8_t b; |
2775 | e57a8c0e | bellard | |
2776 | e57a8c0e | bellard | switch(cmd) {
|
2777 | e57a8c0e | bellard | case CHR_IOCTL_PP_READ_DATA:
|
2778 | e57a8c0e | bellard | if (ioctl(fd, PPRDATA, &b) < 0) |
2779 | e57a8c0e | bellard | return -ENOTSUP;
|
2780 | e57a8c0e | bellard | *(uint8_t *)arg = b; |
2781 | e57a8c0e | bellard | break;
|
2782 | e57a8c0e | bellard | case CHR_IOCTL_PP_WRITE_DATA:
|
2783 | e57a8c0e | bellard | b = *(uint8_t *)arg; |
2784 | e57a8c0e | bellard | if (ioctl(fd, PPWDATA, &b) < 0) |
2785 | e57a8c0e | bellard | return -ENOTSUP;
|
2786 | e57a8c0e | bellard | break;
|
2787 | e57a8c0e | bellard | case CHR_IOCTL_PP_READ_CONTROL:
|
2788 | e57a8c0e | bellard | if (ioctl(fd, PPRCONTROL, &b) < 0) |
2789 | e57a8c0e | bellard | return -ENOTSUP;
|
2790 | 5867c88a | ths | /* Linux gives only the lowest bits, and no way to know data
|
2791 | 5867c88a | ths | direction! For better compatibility set the fixed upper
|
2792 | 5867c88a | ths | bits. */
|
2793 | 5867c88a | ths | *(uint8_t *)arg = b | 0xc0;
|
2794 | e57a8c0e | bellard | break;
|
2795 | e57a8c0e | bellard | case CHR_IOCTL_PP_WRITE_CONTROL:
|
2796 | e57a8c0e | bellard | b = *(uint8_t *)arg; |
2797 | e57a8c0e | bellard | if (ioctl(fd, PPWCONTROL, &b) < 0) |
2798 | e57a8c0e | bellard | return -ENOTSUP;
|
2799 | e57a8c0e | bellard | break;
|
2800 | e57a8c0e | bellard | case CHR_IOCTL_PP_READ_STATUS:
|
2801 | e57a8c0e | bellard | if (ioctl(fd, PPRSTATUS, &b) < 0) |
2802 | e57a8c0e | bellard | return -ENOTSUP;
|
2803 | e57a8c0e | bellard | *(uint8_t *)arg = b; |
2804 | e57a8c0e | bellard | break;
|
2805 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_READ_ADDR:
|
2806 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
2807 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2808 | 5867c88a | ths | int n = read(fd, parg->buffer, parg->count);
|
2809 | 5867c88a | ths | if (n != parg->count) {
|
2810 | 5867c88a | ths | return -EIO;
|
2811 | 5867c88a | ths | } |
2812 | 5867c88a | ths | } |
2813 | 5867c88a | ths | break;
|
2814 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_READ:
|
2815 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
2816 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2817 | 5867c88a | ths | int n = read(fd, parg->buffer, parg->count);
|
2818 | 5867c88a | ths | if (n != parg->count) {
|
2819 | 5867c88a | ths | return -EIO;
|
2820 | 5867c88a | ths | } |
2821 | 5867c88a | ths | } |
2822 | 5867c88a | ths | break;
|
2823 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_WRITE_ADDR:
|
2824 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
2825 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2826 | 5867c88a | ths | int n = write(fd, parg->buffer, parg->count);
|
2827 | 5867c88a | ths | if (n != parg->count) {
|
2828 | 5867c88a | ths | return -EIO;
|
2829 | 5867c88a | ths | } |
2830 | 5867c88a | ths | } |
2831 | 5867c88a | ths | break;
|
2832 | 5867c88a | ths | case CHR_IOCTL_PP_EPP_WRITE:
|
2833 | 5867c88a | ths | if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
2834 | 5867c88a | ths | struct ParallelIOArg *parg = arg;
|
2835 | 5867c88a | ths | int n = write(fd, parg->buffer, parg->count);
|
2836 | 5867c88a | ths | if (n != parg->count) {
|
2837 | 5867c88a | ths | return -EIO;
|
2838 | 5867c88a | ths | } |
2839 | 5867c88a | ths | } |
2840 | 5867c88a | ths | break;
|
2841 | e57a8c0e | bellard | default:
|
2842 | e57a8c0e | bellard | return -ENOTSUP;
|
2843 | e57a8c0e | bellard | } |
2844 | e57a8c0e | bellard | return 0; |
2845 | e57a8c0e | bellard | } |
2846 | e57a8c0e | bellard | |
2847 | 5867c88a | ths | static void pp_close(CharDriverState *chr) |
2848 | 5867c88a | ths | { |
2849 | 5867c88a | ths | ParallelCharDriver *drv = chr->opaque; |
2850 | 5867c88a | ths | int fd = drv->fd;
|
2851 | 5867c88a | ths | |
2852 | 5867c88a | ths | pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
2853 | 5867c88a | ths | ioctl(fd, PPRELEASE); |
2854 | 5867c88a | ths | close(fd); |
2855 | 5867c88a | ths | qemu_free(drv); |
2856 | 5867c88a | ths | } |
2857 | 5867c88a | ths | |
2858 | 52f61fde | ths | static CharDriverState *qemu_chr_open_pp(const char *filename) |
2859 | e57a8c0e | bellard | { |
2860 | e57a8c0e | bellard | CharDriverState *chr; |
2861 | 5867c88a | ths | ParallelCharDriver *drv; |
2862 | e57a8c0e | bellard | int fd;
|
2863 | e57a8c0e | bellard | |
2864 | aeb30be6 | balrog | TFR(fd = open(filename, O_RDWR)); |
2865 | e57a8c0e | bellard | if (fd < 0) |
2866 | e57a8c0e | bellard | return NULL; |
2867 | e57a8c0e | bellard | |
2868 | e57a8c0e | bellard | if (ioctl(fd, PPCLAIM) < 0) { |
2869 | e57a8c0e | bellard | close(fd); |
2870 | e57a8c0e | bellard | return NULL; |
2871 | e57a8c0e | bellard | } |
2872 | e57a8c0e | bellard | |
2873 | 5867c88a | ths | drv = qemu_mallocz(sizeof(ParallelCharDriver));
|
2874 | 5867c88a | ths | if (!drv) {
|
2875 | 5867c88a | ths | close(fd); |
2876 | 5867c88a | ths | return NULL; |
2877 | 5867c88a | ths | } |
2878 | 5867c88a | ths | drv->fd = fd; |
2879 | 5867c88a | ths | drv->mode = IEEE1284_MODE_COMPAT; |
2880 | 5867c88a | ths | |
2881 | e57a8c0e | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
2882 | e57a8c0e | bellard | if (!chr) {
|
2883 | 5867c88a | ths | qemu_free(drv); |
2884 | e57a8c0e | bellard | close(fd); |
2885 | e57a8c0e | bellard | return NULL; |
2886 | e57a8c0e | bellard | } |
2887 | e57a8c0e | bellard | chr->chr_write = null_chr_write; |
2888 | e57a8c0e | bellard | chr->chr_ioctl = pp_ioctl; |
2889 | 5867c88a | ths | chr->chr_close = pp_close; |
2890 | 5867c88a | ths | chr->opaque = drv; |
2891 | 86e94dea | ths | |
2892 | 86e94dea | ths | qemu_chr_reset(chr); |
2893 | 86e94dea | ths | |
2894 | f8d179e3 | bellard | return chr;
|
2895 | f8d179e3 | bellard | } |
2896 | aec62507 | ths | #endif /* __linux__ */ |
2897 | f8d179e3 | bellard | |
2898 | aec62507 | ths | #else /* _WIN32 */ |
2899 | 82c643ff | bellard | |
2900 | f331110f | bellard | typedef struct { |
2901 | f331110f | bellard | int max_size;
|
2902 | f331110f | bellard | HANDLE hcom, hrecv, hsend; |
2903 | f331110f | bellard | OVERLAPPED orecv, osend; |
2904 | f331110f | bellard | BOOL fpipe; |
2905 | f331110f | bellard | DWORD len; |
2906 | f331110f | bellard | } WinCharState; |
2907 | f331110f | bellard | |
2908 | f331110f | bellard | #define NSENDBUF 2048 |
2909 | f331110f | bellard | #define NRECVBUF 2048 |
2910 | f331110f | bellard | #define MAXCONNECT 1 |
2911 | f331110f | bellard | #define NTIMEOUT 5000 |
2912 | f331110f | bellard | |
2913 | f331110f | bellard | static int win_chr_poll(void *opaque); |
2914 | f331110f | bellard | static int win_chr_pipe_poll(void *opaque); |
2915 | f331110f | bellard | |
2916 | 087f4ae0 | ths | static void win_chr_close(CharDriverState *chr) |
2917 | f331110f | bellard | { |
2918 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
2919 | 087f4ae0 | ths | |
2920 | f331110f | bellard | if (s->hsend) {
|
2921 | f331110f | bellard | CloseHandle(s->hsend); |
2922 | f331110f | bellard | s->hsend = NULL;
|
2923 | f331110f | bellard | } |
2924 | f331110f | bellard | if (s->hrecv) {
|
2925 | f331110f | bellard | CloseHandle(s->hrecv); |
2926 | f331110f | bellard | s->hrecv = NULL;
|
2927 | f331110f | bellard | } |
2928 | f331110f | bellard | if (s->hcom) {
|
2929 | f331110f | bellard | CloseHandle(s->hcom); |
2930 | f331110f | bellard | s->hcom = NULL;
|
2931 | f331110f | bellard | } |
2932 | f331110f | bellard | if (s->fpipe)
|
2933 | 087f4ae0 | ths | qemu_del_polling_cb(win_chr_pipe_poll, chr); |
2934 | f331110f | bellard | else
|
2935 | 087f4ae0 | ths | qemu_del_polling_cb(win_chr_poll, chr); |
2936 | f331110f | bellard | } |
2937 | f331110f | bellard | |
2938 | 087f4ae0 | ths | static int win_chr_init(CharDriverState *chr, const char *filename) |
2939 | f331110f | bellard | { |
2940 | f331110f | bellard | WinCharState *s = chr->opaque; |
2941 | f331110f | bellard | COMMCONFIG comcfg; |
2942 | f331110f | bellard | COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
2943 | f331110f | bellard | COMSTAT comstat; |
2944 | f331110f | bellard | DWORD size; |
2945 | f331110f | bellard | DWORD err; |
2946 | 3b46e624 | ths | |
2947 | f331110f | bellard | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
2948 | f331110f | bellard | if (!s->hsend) {
|
2949 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
2950 | f331110f | bellard | goto fail;
|
2951 | f331110f | bellard | } |
2952 | f331110f | bellard | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
2953 | f331110f | bellard | if (!s->hrecv) {
|
2954 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
2955 | f331110f | bellard | goto fail;
|
2956 | f331110f | bellard | } |
2957 | f331110f | bellard | |
2958 | f331110f | bellard | s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
2959 | f331110f | bellard | OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
|
2960 | f331110f | bellard | if (s->hcom == INVALID_HANDLE_VALUE) {
|
2961 | f331110f | bellard | fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
|
2962 | f331110f | bellard | s->hcom = NULL;
|
2963 | f331110f | bellard | goto fail;
|
2964 | f331110f | bellard | } |
2965 | 3b46e624 | ths | |
2966 | f331110f | bellard | if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
|
2967 | f331110f | bellard | fprintf(stderr, "Failed SetupComm\n");
|
2968 | f331110f | bellard | goto fail;
|
2969 | f331110f | bellard | } |
2970 | 3b46e624 | ths | |
2971 | f331110f | bellard | ZeroMemory(&comcfg, sizeof(COMMCONFIG));
|
2972 | f331110f | bellard | size = sizeof(COMMCONFIG);
|
2973 | f331110f | bellard | GetDefaultCommConfig(filename, &comcfg, &size); |
2974 | f331110f | bellard | comcfg.dcb.DCBlength = sizeof(DCB);
|
2975 | f331110f | bellard | CommConfigDialog(filename, NULL, &comcfg);
|
2976 | f331110f | bellard | |
2977 | f331110f | bellard | if (!SetCommState(s->hcom, &comcfg.dcb)) {
|
2978 | f331110f | bellard | fprintf(stderr, "Failed SetCommState\n");
|
2979 | f331110f | bellard | goto fail;
|
2980 | f331110f | bellard | } |
2981 | f331110f | bellard | |
2982 | f331110f | bellard | if (!SetCommMask(s->hcom, EV_ERR)) {
|
2983 | f331110f | bellard | fprintf(stderr, "Failed SetCommMask\n");
|
2984 | f331110f | bellard | goto fail;
|
2985 | f331110f | bellard | } |
2986 | f331110f | bellard | |
2987 | f331110f | bellard | cto.ReadIntervalTimeout = MAXDWORD; |
2988 | f331110f | bellard | if (!SetCommTimeouts(s->hcom, &cto)) {
|
2989 | f331110f | bellard | fprintf(stderr, "Failed SetCommTimeouts\n");
|
2990 | f331110f | bellard | goto fail;
|
2991 | f331110f | bellard | } |
2992 | 3b46e624 | ths | |
2993 | f331110f | bellard | if (!ClearCommError(s->hcom, &err, &comstat)) {
|
2994 | f331110f | bellard | fprintf(stderr, "Failed ClearCommError\n");
|
2995 | f331110f | bellard | goto fail;
|
2996 | f331110f | bellard | } |
2997 | 087f4ae0 | ths | qemu_add_polling_cb(win_chr_poll, chr); |
2998 | f331110f | bellard | return 0; |
2999 | f331110f | bellard | |
3000 | f331110f | bellard | fail:
|
3001 | 087f4ae0 | ths | win_chr_close(chr); |
3002 | f331110f | bellard | return -1; |
3003 | f331110f | bellard | } |
3004 | f331110f | bellard | |
3005 | f331110f | bellard | static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
3006 | f331110f | bellard | { |
3007 | f331110f | bellard | WinCharState *s = chr->opaque; |
3008 | f331110f | bellard | DWORD len, ret, size, err; |
3009 | f331110f | bellard | |
3010 | f331110f | bellard | len = len1; |
3011 | f331110f | bellard | ZeroMemory(&s->osend, sizeof(s->osend));
|
3012 | f331110f | bellard | s->osend.hEvent = s->hsend; |
3013 | f331110f | bellard | while (len > 0) { |
3014 | f331110f | bellard | if (s->hsend)
|
3015 | f331110f | bellard | ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
3016 | f331110f | bellard | else
|
3017 | f331110f | bellard | ret = WriteFile(s->hcom, buf, len, &size, NULL);
|
3018 | f331110f | bellard | if (!ret) {
|
3019 | f331110f | bellard | err = GetLastError(); |
3020 | f331110f | bellard | if (err == ERROR_IO_PENDING) {
|
3021 | f331110f | bellard | ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
3022 | f331110f | bellard | if (ret) {
|
3023 | f331110f | bellard | buf += size; |
3024 | f331110f | bellard | len -= size; |
3025 | f331110f | bellard | } else {
|
3026 | f331110f | bellard | break;
|
3027 | f331110f | bellard | } |
3028 | f331110f | bellard | } else {
|
3029 | f331110f | bellard | break;
|
3030 | f331110f | bellard | } |
3031 | f331110f | bellard | } else {
|
3032 | f331110f | bellard | buf += size; |
3033 | f331110f | bellard | len -= size; |
3034 | f331110f | bellard | } |
3035 | f331110f | bellard | } |
3036 | f331110f | bellard | return len1 - len;
|
3037 | f331110f | bellard | } |
3038 | f331110f | bellard | |
3039 | 087f4ae0 | ths | static int win_chr_read_poll(CharDriverState *chr) |
3040 | f331110f | bellard | { |
3041 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3042 | 087f4ae0 | ths | |
3043 | 087f4ae0 | ths | s->max_size = qemu_chr_can_read(chr); |
3044 | f331110f | bellard | return s->max_size;
|
3045 | f331110f | bellard | } |
3046 | e5b0bc44 | pbrook | |
3047 | 087f4ae0 | ths | static void win_chr_readfile(CharDriverState *chr) |
3048 | f331110f | bellard | { |
3049 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3050 | f331110f | bellard | int ret, err;
|
3051 | f331110f | bellard | uint8_t buf[1024];
|
3052 | f331110f | bellard | DWORD size; |
3053 | 3b46e624 | ths | |
3054 | f331110f | bellard | ZeroMemory(&s->orecv, sizeof(s->orecv));
|
3055 | f331110f | bellard | s->orecv.hEvent = s->hrecv; |
3056 | f331110f | bellard | ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
3057 | f331110f | bellard | if (!ret) {
|
3058 | f331110f | bellard | err = GetLastError(); |
3059 | f331110f | bellard | if (err == ERROR_IO_PENDING) {
|
3060 | f331110f | bellard | ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
3061 | f331110f | bellard | } |
3062 | f331110f | bellard | } |
3063 | f331110f | bellard | |
3064 | f331110f | bellard | if (size > 0) { |
3065 | 087f4ae0 | ths | qemu_chr_read(chr, buf, size); |
3066 | f331110f | bellard | } |
3067 | f331110f | bellard | } |
3068 | f331110f | bellard | |
3069 | 087f4ae0 | ths | static void win_chr_read(CharDriverState *chr) |
3070 | f331110f | bellard | { |
3071 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3072 | 087f4ae0 | ths | |
3073 | f331110f | bellard | if (s->len > s->max_size)
|
3074 | f331110f | bellard | s->len = s->max_size; |
3075 | f331110f | bellard | if (s->len == 0) |
3076 | f331110f | bellard | return;
|
3077 | 3b46e624 | ths | |
3078 | 087f4ae0 | ths | win_chr_readfile(chr); |
3079 | f331110f | bellard | } |
3080 | f331110f | bellard | |
3081 | f331110f | bellard | static int win_chr_poll(void *opaque) |
3082 | f331110f | bellard | { |
3083 | 087f4ae0 | ths | CharDriverState *chr = opaque; |
3084 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3085 | f331110f | bellard | COMSTAT status; |
3086 | f331110f | bellard | DWORD comerr; |
3087 | 3b46e624 | ths | |
3088 | f331110f | bellard | ClearCommError(s->hcom, &comerr, &status); |
3089 | f331110f | bellard | if (status.cbInQue > 0) { |
3090 | f331110f | bellard | s->len = status.cbInQue; |
3091 | 087f4ae0 | ths | win_chr_read_poll(chr); |
3092 | 087f4ae0 | ths | win_chr_read(chr); |
3093 | f331110f | bellard | return 1; |
3094 | f331110f | bellard | } |
3095 | f331110f | bellard | return 0; |
3096 | f331110f | bellard | } |
3097 | f331110f | bellard | |
3098 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win(const char *filename) |
3099 | f331110f | bellard | { |
3100 | f331110f | bellard | CharDriverState *chr; |
3101 | f331110f | bellard | WinCharState *s; |
3102 | 3b46e624 | ths | |
3103 | f331110f | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3104 | f331110f | bellard | if (!chr)
|
3105 | f331110f | bellard | return NULL; |
3106 | f331110f | bellard | s = qemu_mallocz(sizeof(WinCharState));
|
3107 | f331110f | bellard | if (!s) {
|
3108 | f331110f | bellard | free(chr); |
3109 | f331110f | bellard | return NULL; |
3110 | f331110f | bellard | } |
3111 | f331110f | bellard | chr->opaque = s; |
3112 | f331110f | bellard | chr->chr_write = win_chr_write; |
3113 | f331110f | bellard | chr->chr_close = win_chr_close; |
3114 | f331110f | bellard | |
3115 | 087f4ae0 | ths | if (win_chr_init(chr, filename) < 0) { |
3116 | f331110f | bellard | free(s); |
3117 | f331110f | bellard | free(chr); |
3118 | f331110f | bellard | return NULL; |
3119 | f331110f | bellard | } |
3120 | 86e94dea | ths | qemu_chr_reset(chr); |
3121 | f331110f | bellard | return chr;
|
3122 | f331110f | bellard | } |
3123 | f331110f | bellard | |
3124 | f331110f | bellard | static int win_chr_pipe_poll(void *opaque) |
3125 | f331110f | bellard | { |
3126 | 087f4ae0 | ths | CharDriverState *chr = opaque; |
3127 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3128 | f331110f | bellard | DWORD size; |
3129 | f331110f | bellard | |
3130 | f331110f | bellard | PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
3131 | f331110f | bellard | if (size > 0) { |
3132 | f331110f | bellard | s->len = size; |
3133 | 087f4ae0 | ths | win_chr_read_poll(chr); |
3134 | 087f4ae0 | ths | win_chr_read(chr); |
3135 | f331110f | bellard | return 1; |
3136 | f331110f | bellard | } |
3137 | f331110f | bellard | return 0; |
3138 | f331110f | bellard | } |
3139 | f331110f | bellard | |
3140 | 087f4ae0 | ths | static int win_chr_pipe_init(CharDriverState *chr, const char *filename) |
3141 | f331110f | bellard | { |
3142 | 087f4ae0 | ths | WinCharState *s = chr->opaque; |
3143 | f331110f | bellard | OVERLAPPED ov; |
3144 | f331110f | bellard | int ret;
|
3145 | f331110f | bellard | DWORD size; |
3146 | f331110f | bellard | char openname[256]; |
3147 | 3b46e624 | ths | |
3148 | f331110f | bellard | s->fpipe = TRUE; |
3149 | f331110f | bellard | |
3150 | f331110f | bellard | s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
3151 | f331110f | bellard | if (!s->hsend) {
|
3152 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
3153 | f331110f | bellard | goto fail;
|
3154 | f331110f | bellard | } |
3155 | f331110f | bellard | s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
3156 | f331110f | bellard | if (!s->hrecv) {
|
3157 | f331110f | bellard | fprintf(stderr, "Failed CreateEvent\n");
|
3158 | f331110f | bellard | goto fail;
|
3159 | f331110f | bellard | } |
3160 | 3b46e624 | ths | |
3161 | f331110f | bellard | snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
3162 | f331110f | bellard | s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
3163 | f331110f | bellard | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
3164 | f331110f | bellard | PIPE_WAIT, |
3165 | f331110f | bellard | MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
|
3166 | f331110f | bellard | if (s->hcom == INVALID_HANDLE_VALUE) {
|
3167 | f331110f | bellard | fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
|
3168 | f331110f | bellard | s->hcom = NULL;
|
3169 | f331110f | bellard | goto fail;
|
3170 | f331110f | bellard | } |
3171 | f331110f | bellard | |
3172 | f331110f | bellard | ZeroMemory(&ov, sizeof(ov));
|
3173 | f331110f | bellard | ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
3174 | f331110f | bellard | ret = ConnectNamedPipe(s->hcom, &ov); |
3175 | f331110f | bellard | if (ret) {
|
3176 | f331110f | bellard | fprintf(stderr, "Failed ConnectNamedPipe\n");
|
3177 | f331110f | bellard | goto fail;
|
3178 | f331110f | bellard | } |
3179 | f331110f | bellard | |
3180 | f331110f | bellard | ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
3181 | f331110f | bellard | if (!ret) {
|
3182 | f331110f | bellard | fprintf(stderr, "Failed GetOverlappedResult\n");
|
3183 | f331110f | bellard | if (ov.hEvent) {
|
3184 | f331110f | bellard | CloseHandle(ov.hEvent); |
3185 | f331110f | bellard | ov.hEvent = NULL;
|
3186 | f331110f | bellard | } |
3187 | f331110f | bellard | goto fail;
|
3188 | f331110f | bellard | } |
3189 | f331110f | bellard | |
3190 | f331110f | bellard | if (ov.hEvent) {
|
3191 | f331110f | bellard | CloseHandle(ov.hEvent); |
3192 | f331110f | bellard | ov.hEvent = NULL;
|
3193 | f331110f | bellard | } |
3194 | 087f4ae0 | ths | qemu_add_polling_cb(win_chr_pipe_poll, chr); |
3195 | f331110f | bellard | return 0; |
3196 | f331110f | bellard | |
3197 | f331110f | bellard | fail:
|
3198 | 087f4ae0 | ths | win_chr_close(chr); |
3199 | f331110f | bellard | return -1; |
3200 | f331110f | bellard | } |
3201 | f331110f | bellard | |
3202 | f331110f | bellard | |
3203 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win_pipe(const char *filename) |
3204 | f331110f | bellard | { |
3205 | f331110f | bellard | CharDriverState *chr; |
3206 | f331110f | bellard | WinCharState *s; |
3207 | f331110f | bellard | |
3208 | f331110f | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3209 | f331110f | bellard | if (!chr)
|
3210 | f331110f | bellard | return NULL; |
3211 | f331110f | bellard | s = qemu_mallocz(sizeof(WinCharState));
|
3212 | f331110f | bellard | if (!s) {
|
3213 | f331110f | bellard | free(chr); |
3214 | f331110f | bellard | return NULL; |
3215 | f331110f | bellard | } |
3216 | f331110f | bellard | chr->opaque = s; |
3217 | f331110f | bellard | chr->chr_write = win_chr_write; |
3218 | f331110f | bellard | chr->chr_close = win_chr_close; |
3219 | 3b46e624 | ths | |
3220 | 087f4ae0 | ths | if (win_chr_pipe_init(chr, filename) < 0) { |
3221 | f331110f | bellard | free(s); |
3222 | f331110f | bellard | free(chr); |
3223 | f331110f | bellard | return NULL; |
3224 | f331110f | bellard | } |
3225 | 86e94dea | ths | qemu_chr_reset(chr); |
3226 | f331110f | bellard | return chr;
|
3227 | f331110f | bellard | } |
3228 | f331110f | bellard | |
3229 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
|
3230 | f331110f | bellard | { |
3231 | f331110f | bellard | CharDriverState *chr; |
3232 | f331110f | bellard | WinCharState *s; |
3233 | f331110f | bellard | |
3234 | f331110f | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3235 | f331110f | bellard | if (!chr)
|
3236 | f331110f | bellard | return NULL; |
3237 | f331110f | bellard | s = qemu_mallocz(sizeof(WinCharState));
|
3238 | f331110f | bellard | if (!s) {
|
3239 | f331110f | bellard | free(chr); |
3240 | f331110f | bellard | return NULL; |
3241 | f331110f | bellard | } |
3242 | f331110f | bellard | s->hcom = fd_out; |
3243 | f331110f | bellard | chr->opaque = s; |
3244 | f331110f | bellard | chr->chr_write = win_chr_write; |
3245 | 86e94dea | ths | qemu_chr_reset(chr); |
3246 | f331110f | bellard | return chr;
|
3247 | f331110f | bellard | } |
3248 | 72d46479 | ths | |
3249 | 72d46479 | ths | static CharDriverState *qemu_chr_open_win_con(const char *filename) |
3250 | 72d46479 | ths | { |
3251 | 72d46479 | ths | return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
|
3252 | 72d46479 | ths | } |
3253 | 72d46479 | ths | |
3254 | 52f61fde | ths | static CharDriverState *qemu_chr_open_win_file_out(const char *file_out) |
3255 | f331110f | bellard | { |
3256 | f331110f | bellard | HANDLE fd_out; |
3257 | 3b46e624 | ths | |
3258 | f331110f | bellard | fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
|
3259 | f331110f | bellard | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
3260 | f331110f | bellard | if (fd_out == INVALID_HANDLE_VALUE)
|
3261 | f331110f | bellard | return NULL; |
3262 | f331110f | bellard | |
3263 | f331110f | bellard | return qemu_chr_open_win_file(fd_out);
|
3264 | f331110f | bellard | } |
3265 | aec62507 | ths | #endif /* !_WIN32 */ |
3266 | f331110f | bellard | |
3267 | 0bab00f3 | bellard | /***********************************************************/
|
3268 | 0bab00f3 | bellard | /* UDP Net console */
|
3269 | 0bab00f3 | bellard | |
3270 | 0bab00f3 | bellard | typedef struct { |
3271 | 0bab00f3 | bellard | int fd;
|
3272 | 0bab00f3 | bellard | struct sockaddr_in daddr;
|
3273 | 60fe76f3 | ths | uint8_t buf[1024];
|
3274 | 0bab00f3 | bellard | int bufcnt;
|
3275 | 0bab00f3 | bellard | int bufptr;
|
3276 | 0bab00f3 | bellard | int max_size;
|
3277 | 0bab00f3 | bellard | } NetCharDriver; |
3278 | 0bab00f3 | bellard | |
3279 | 0bab00f3 | bellard | static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
3280 | 0bab00f3 | bellard | { |
3281 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3282 | 0bab00f3 | bellard | |
3283 | 0bab00f3 | bellard | return sendto(s->fd, buf, len, 0, |
3284 | 0bab00f3 | bellard | (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in)); |
3285 | 0bab00f3 | bellard | } |
3286 | 0bab00f3 | bellard | |
3287 | 0bab00f3 | bellard | static int udp_chr_read_poll(void *opaque) |
3288 | 0bab00f3 | bellard | { |
3289 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3290 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3291 | 0bab00f3 | bellard | |
3292 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3293 | 0bab00f3 | bellard | |
3294 | 0bab00f3 | bellard | /* If there were any stray characters in the queue process them
|
3295 | 0bab00f3 | bellard | * first
|
3296 | 0bab00f3 | bellard | */
|
3297 | 0bab00f3 | bellard | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
3298 | e5b0bc44 | pbrook | qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
3299 | 0bab00f3 | bellard | s->bufptr++; |
3300 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3301 | 0bab00f3 | bellard | } |
3302 | 0bab00f3 | bellard | return s->max_size;
|
3303 | 0bab00f3 | bellard | } |
3304 | 0bab00f3 | bellard | |
3305 | 0bab00f3 | bellard | static void udp_chr_read(void *opaque) |
3306 | 0bab00f3 | bellard | { |
3307 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3308 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3309 | 0bab00f3 | bellard | |
3310 | 0bab00f3 | bellard | if (s->max_size == 0) |
3311 | 0bab00f3 | bellard | return;
|
3312 | 0bab00f3 | bellard | s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0); |
3313 | 0bab00f3 | bellard | s->bufptr = s->bufcnt; |
3314 | 0bab00f3 | bellard | if (s->bufcnt <= 0) |
3315 | 0bab00f3 | bellard | return;
|
3316 | 0bab00f3 | bellard | |
3317 | 0bab00f3 | bellard | s->bufptr = 0;
|
3318 | 0bab00f3 | bellard | while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
3319 | e5b0bc44 | pbrook | qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
3320 | 0bab00f3 | bellard | s->bufptr++; |
3321 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3322 | 0bab00f3 | bellard | } |
3323 | 0bab00f3 | bellard | } |
3324 | 0bab00f3 | bellard | |
3325 | e5b0bc44 | pbrook | static void udp_chr_update_read_handler(CharDriverState *chr) |
3326 | 0bab00f3 | bellard | { |
3327 | 0bab00f3 | bellard | NetCharDriver *s = chr->opaque; |
3328 | 0bab00f3 | bellard | |
3329 | 0bab00f3 | bellard | if (s->fd >= 0) { |
3330 | 0bab00f3 | bellard | qemu_set_fd_handler2(s->fd, udp_chr_read_poll, |
3331 | 0bab00f3 | bellard | udp_chr_read, NULL, chr);
|
3332 | 0bab00f3 | bellard | } |
3333 | 0bab00f3 | bellard | } |
3334 | 0bab00f3 | bellard | |
3335 | 0bab00f3 | bellard | int parse_host_port(struct sockaddr_in *saddr, const char *str); |
3336 | 52f61fde | ths | #ifndef _WIN32
|
3337 | 52f61fde | ths | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str); |
3338 | 52f61fde | ths | #endif
|
3339 | 951f1351 | bellard | int parse_host_src_port(struct sockaddr_in *haddr, |
3340 | 951f1351 | bellard | struct sockaddr_in *saddr,
|
3341 | 951f1351 | bellard | const char *str); |
3342 | 0bab00f3 | bellard | |
3343 | 52f61fde | ths | static CharDriverState *qemu_chr_open_udp(const char *def) |
3344 | 0bab00f3 | bellard | { |
3345 | 0bab00f3 | bellard | CharDriverState *chr = NULL;
|
3346 | 0bab00f3 | bellard | NetCharDriver *s = NULL;
|
3347 | 0bab00f3 | bellard | int fd = -1; |
3348 | 951f1351 | bellard | struct sockaddr_in saddr;
|
3349 | 0bab00f3 | bellard | |
3350 | 0bab00f3 | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3351 | 0bab00f3 | bellard | if (!chr)
|
3352 | 0bab00f3 | bellard | goto return_err;
|
3353 | 0bab00f3 | bellard | s = qemu_mallocz(sizeof(NetCharDriver));
|
3354 | 0bab00f3 | bellard | if (!s)
|
3355 | 0bab00f3 | bellard | goto return_err;
|
3356 | 0bab00f3 | bellard | |
3357 | 0bab00f3 | bellard | fd = socket(PF_INET, SOCK_DGRAM, 0);
|
3358 | 0bab00f3 | bellard | if (fd < 0) { |
3359 | 0bab00f3 | bellard | perror("socket(PF_INET, SOCK_DGRAM)");
|
3360 | 0bab00f3 | bellard | goto return_err;
|
3361 | 0bab00f3 | bellard | } |
3362 | 0bab00f3 | bellard | |
3363 | 951f1351 | bellard | if (parse_host_src_port(&s->daddr, &saddr, def) < 0) { |
3364 | 951f1351 | bellard | printf("Could not parse: %s\n", def);
|
3365 | 951f1351 | bellard | goto return_err;
|
3366 | 0bab00f3 | bellard | } |
3367 | 0bab00f3 | bellard | |
3368 | 951f1351 | bellard | if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) |
3369 | 0bab00f3 | bellard | { |
3370 | 0bab00f3 | bellard | perror("bind");
|
3371 | 0bab00f3 | bellard | goto return_err;
|
3372 | 0bab00f3 | bellard | } |
3373 | 0bab00f3 | bellard | |
3374 | 0bab00f3 | bellard | s->fd = fd; |
3375 | 0bab00f3 | bellard | s->bufcnt = 0;
|
3376 | 0bab00f3 | bellard | s->bufptr = 0;
|
3377 | 0bab00f3 | bellard | chr->opaque = s; |
3378 | 0bab00f3 | bellard | chr->chr_write = udp_chr_write; |
3379 | e5b0bc44 | pbrook | chr->chr_update_read_handler = udp_chr_update_read_handler; |
3380 | 0bab00f3 | bellard | return chr;
|
3381 | 0bab00f3 | bellard | |
3382 | 0bab00f3 | bellard | return_err:
|
3383 | 0bab00f3 | bellard | if (chr)
|
3384 | 0bab00f3 | bellard | free(chr); |
3385 | 0bab00f3 | bellard | if (s)
|
3386 | 0bab00f3 | bellard | free(s); |
3387 | 0bab00f3 | bellard | if (fd >= 0) |
3388 | 0bab00f3 | bellard | closesocket(fd); |
3389 | 0bab00f3 | bellard | return NULL; |
3390 | 0bab00f3 | bellard | } |
3391 | 0bab00f3 | bellard | |
3392 | 0bab00f3 | bellard | /***********************************************************/
|
3393 | 0bab00f3 | bellard | /* TCP Net console */
|
3394 | 0bab00f3 | bellard | |
3395 | 0bab00f3 | bellard | typedef struct { |
3396 | 0bab00f3 | bellard | int fd, listen_fd;
|
3397 | 0bab00f3 | bellard | int connected;
|
3398 | 0bab00f3 | bellard | int max_size;
|
3399 | 951f1351 | bellard | int do_telnetopt;
|
3400 | e5b0bc44 | pbrook | int do_nodelay;
|
3401 | ffd843bc | ths | int is_unix;
|
3402 | 0bab00f3 | bellard | } TCPCharDriver; |
3403 | 0bab00f3 | bellard | |
3404 | 0bab00f3 | bellard | static void tcp_chr_accept(void *opaque); |
3405 | 0bab00f3 | bellard | |
3406 | 0bab00f3 | bellard | static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
3407 | 0bab00f3 | bellard | { |
3408 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3409 | 0bab00f3 | bellard | if (s->connected) {
|
3410 | 0bab00f3 | bellard | return send_all(s->fd, buf, len);
|
3411 | 0bab00f3 | bellard | } else {
|
3412 | 0bab00f3 | bellard | /* XXX: indicate an error ? */
|
3413 | 0bab00f3 | bellard | return len;
|
3414 | 0bab00f3 | bellard | } |
3415 | 0bab00f3 | bellard | } |
3416 | 0bab00f3 | bellard | |
3417 | 0bab00f3 | bellard | static int tcp_chr_read_poll(void *opaque) |
3418 | 0bab00f3 | bellard | { |
3419 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3420 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3421 | 0bab00f3 | bellard | if (!s->connected)
|
3422 | 0bab00f3 | bellard | return 0; |
3423 | e5b0bc44 | pbrook | s->max_size = qemu_chr_can_read(chr); |
3424 | 0bab00f3 | bellard | return s->max_size;
|
3425 | 0bab00f3 | bellard | } |
3426 | 0bab00f3 | bellard | |
3427 | 951f1351 | bellard | #define IAC 255 |
3428 | 951f1351 | bellard | #define IAC_BREAK 243 |
3429 | 951f1351 | bellard | static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
3430 | 951f1351 | bellard | TCPCharDriver *s, |
3431 | 60fe76f3 | ths | uint8_t *buf, int *size)
|
3432 | 951f1351 | bellard | { |
3433 | 951f1351 | bellard | /* Handle any telnet client's basic IAC options to satisfy char by
|
3434 | 951f1351 | bellard | * char mode with no echo. All IAC options will be removed from
|
3435 | 951f1351 | bellard | * the buf and the do_telnetopt variable will be used to track the
|
3436 | 951f1351 | bellard | * state of the width of the IAC information.
|
3437 | 951f1351 | bellard | *
|
3438 | 951f1351 | bellard | * IAC commands come in sets of 3 bytes with the exception of the
|
3439 | 951f1351 | bellard | * "IAC BREAK" command and the double IAC.
|
3440 | 951f1351 | bellard | */
|
3441 | 951f1351 | bellard | |
3442 | 951f1351 | bellard | int i;
|
3443 | 951f1351 | bellard | int j = 0; |
3444 | 951f1351 | bellard | |
3445 | 951f1351 | bellard | for (i = 0; i < *size; i++) { |
3446 | 951f1351 | bellard | if (s->do_telnetopt > 1) { |
3447 | 951f1351 | bellard | if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
3448 | 951f1351 | bellard | /* Double IAC means send an IAC */
|
3449 | 951f1351 | bellard | if (j != i)
|
3450 | 951f1351 | bellard | buf[j] = buf[i]; |
3451 | 951f1351 | bellard | j++; |
3452 | 951f1351 | bellard | s->do_telnetopt = 1;
|
3453 | 951f1351 | bellard | } else {
|
3454 | 951f1351 | bellard | if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
3455 | 951f1351 | bellard | /* Handle IAC break commands by sending a serial break */
|
3456 | e5b0bc44 | pbrook | qemu_chr_event(chr, CHR_EVENT_BREAK); |
3457 | 951f1351 | bellard | s->do_telnetopt++; |
3458 | 951f1351 | bellard | } |
3459 | 951f1351 | bellard | s->do_telnetopt++; |
3460 | 951f1351 | bellard | } |
3461 | 951f1351 | bellard | if (s->do_telnetopt >= 4) { |
3462 | 951f1351 | bellard | s->do_telnetopt = 1;
|
3463 | 951f1351 | bellard | } |
3464 | 951f1351 | bellard | } else {
|
3465 | 951f1351 | bellard | if ((unsigned char)buf[i] == IAC) { |
3466 | 951f1351 | bellard | s->do_telnetopt = 2;
|
3467 | 951f1351 | bellard | } else {
|
3468 | 951f1351 | bellard | if (j != i)
|
3469 | 951f1351 | bellard | buf[j] = buf[i]; |
3470 | 951f1351 | bellard | j++; |
3471 | 951f1351 | bellard | } |
3472 | 951f1351 | bellard | } |
3473 | 951f1351 | bellard | } |
3474 | 951f1351 | bellard | *size = j; |
3475 | 951f1351 | bellard | } |
3476 | 951f1351 | bellard | |
3477 | 0bab00f3 | bellard | static void tcp_chr_read(void *opaque) |
3478 | 0bab00f3 | bellard | { |
3479 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3480 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3481 | 0bab00f3 | bellard | uint8_t buf[1024];
|
3482 | 0bab00f3 | bellard | int len, size;
|
3483 | 0bab00f3 | bellard | |
3484 | 0bab00f3 | bellard | if (!s->connected || s->max_size <= 0) |
3485 | 0bab00f3 | bellard | return;
|
3486 | 0bab00f3 | bellard | len = sizeof(buf);
|
3487 | 0bab00f3 | bellard | if (len > s->max_size)
|
3488 | 0bab00f3 | bellard | len = s->max_size; |
3489 | 0bab00f3 | bellard | size = recv(s->fd, buf, len, 0);
|
3490 | 0bab00f3 | bellard | if (size == 0) { |
3491 | 0bab00f3 | bellard | /* connection closed */
|
3492 | 0bab00f3 | bellard | s->connected = 0;
|
3493 | 0bab00f3 | bellard | if (s->listen_fd >= 0) { |
3494 | 0bab00f3 | bellard | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
3495 | 0bab00f3 | bellard | } |
3496 | 0bab00f3 | bellard | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
3497 | 0bab00f3 | bellard | closesocket(s->fd); |
3498 | 0bab00f3 | bellard | s->fd = -1;
|
3499 | 0bab00f3 | bellard | } else if (size > 0) { |
3500 | 951f1351 | bellard | if (s->do_telnetopt)
|
3501 | 951f1351 | bellard | tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
3502 | 951f1351 | bellard | if (size > 0) |
3503 | e5b0bc44 | pbrook | qemu_chr_read(chr, buf, size); |
3504 | 0bab00f3 | bellard | } |
3505 | 0bab00f3 | bellard | } |
3506 | 0bab00f3 | bellard | |
3507 | 0bab00f3 | bellard | static void tcp_chr_connect(void *opaque) |
3508 | 0bab00f3 | bellard | { |
3509 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3510 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3511 | 0bab00f3 | bellard | |
3512 | 0bab00f3 | bellard | s->connected = 1;
|
3513 | 0bab00f3 | bellard | qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, |
3514 | 0bab00f3 | bellard | tcp_chr_read, NULL, chr);
|
3515 | 86e94dea | ths | qemu_chr_reset(chr); |
3516 | 0bab00f3 | bellard | } |
3517 | 0bab00f3 | bellard | |
3518 | 951f1351 | bellard | #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
3519 | 951f1351 | bellard | static void tcp_chr_telnet_init(int fd) |
3520 | 951f1351 | bellard | { |
3521 | 951f1351 | bellard | char buf[3]; |
3522 | 951f1351 | bellard | /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
|
3523 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
3524 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3525 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
3526 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3527 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
3528 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3529 | 951f1351 | bellard | IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
3530 | 951f1351 | bellard | send(fd, (char *)buf, 3, 0); |
3531 | 951f1351 | bellard | } |
3532 | 951f1351 | bellard | |
3533 | f7499989 | pbrook | static void socket_set_nodelay(int fd) |
3534 | f7499989 | pbrook | { |
3535 | f7499989 | pbrook | int val = 1; |
3536 | f7499989 | pbrook | setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
3537 | f7499989 | pbrook | } |
3538 | f7499989 | pbrook | |
3539 | 0bab00f3 | bellard | static void tcp_chr_accept(void *opaque) |
3540 | 0bab00f3 | bellard | { |
3541 | 0bab00f3 | bellard | CharDriverState *chr = opaque; |
3542 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3543 | 0bab00f3 | bellard | struct sockaddr_in saddr;
|
3544 | ffd843bc | ths | #ifndef _WIN32
|
3545 | ffd843bc | ths | struct sockaddr_un uaddr;
|
3546 | ffd843bc | ths | #endif
|
3547 | ffd843bc | ths | struct sockaddr *addr;
|
3548 | 0bab00f3 | bellard | socklen_t len; |
3549 | 0bab00f3 | bellard | int fd;
|
3550 | 0bab00f3 | bellard | |
3551 | 0bab00f3 | bellard | for(;;) {
|
3552 | ffd843bc | ths | #ifndef _WIN32
|
3553 | ffd843bc | ths | if (s->is_unix) {
|
3554 | ffd843bc | ths | len = sizeof(uaddr);
|
3555 | ffd843bc | ths | addr = (struct sockaddr *)&uaddr;
|
3556 | ffd843bc | ths | } else
|
3557 | ffd843bc | ths | #endif
|
3558 | ffd843bc | ths | { |
3559 | ffd843bc | ths | len = sizeof(saddr);
|
3560 | ffd843bc | ths | addr = (struct sockaddr *)&saddr;
|
3561 | ffd843bc | ths | } |
3562 | ffd843bc | ths | fd = accept(s->listen_fd, addr, &len); |
3563 | 0bab00f3 | bellard | if (fd < 0 && errno != EINTR) { |
3564 | 0bab00f3 | bellard | return;
|
3565 | 0bab00f3 | bellard | } else if (fd >= 0) { |
3566 | 951f1351 | bellard | if (s->do_telnetopt)
|
3567 | 951f1351 | bellard | tcp_chr_telnet_init(fd); |
3568 | 0bab00f3 | bellard | break;
|
3569 | 0bab00f3 | bellard | } |
3570 | 0bab00f3 | bellard | } |
3571 | 0bab00f3 | bellard | socket_set_nonblock(fd); |
3572 | f7499989 | pbrook | if (s->do_nodelay)
|
3573 | f7499989 | pbrook | socket_set_nodelay(fd); |
3574 | 0bab00f3 | bellard | s->fd = fd; |
3575 | 0bab00f3 | bellard | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
3576 | 0bab00f3 | bellard | tcp_chr_connect(chr); |
3577 | 0bab00f3 | bellard | } |
3578 | 0bab00f3 | bellard | |
3579 | 0bab00f3 | bellard | static void tcp_chr_close(CharDriverState *chr) |
3580 | 0bab00f3 | bellard | { |
3581 | 0bab00f3 | bellard | TCPCharDriver *s = chr->opaque; |
3582 | 0bab00f3 | bellard | if (s->fd >= 0) |
3583 | 0bab00f3 | bellard | closesocket(s->fd); |
3584 | 0bab00f3 | bellard | if (s->listen_fd >= 0) |
3585 | 0bab00f3 | bellard | closesocket(s->listen_fd); |
3586 | 0bab00f3 | bellard | qemu_free(s); |
3587 | 0bab00f3 | bellard | } |
3588 | 0bab00f3 | bellard | |
3589 | 5fafdf24 | ths | static CharDriverState *qemu_chr_open_tcp(const char *host_str, |
3590 | ffd843bc | ths | int is_telnet,
|
3591 | ffd843bc | ths | int is_unix)
|
3592 | 0bab00f3 | bellard | { |
3593 | 0bab00f3 | bellard | CharDriverState *chr = NULL;
|
3594 | 0bab00f3 | bellard | TCPCharDriver *s = NULL;
|
3595 | 0bab00f3 | bellard | int fd = -1, ret, err, val; |
3596 | 951f1351 | bellard | int is_listen = 0; |
3597 | 951f1351 | bellard | int is_waitconnect = 1; |
3598 | f7499989 | pbrook | int do_nodelay = 0; |
3599 | 951f1351 | bellard | const char *ptr; |
3600 | 0bab00f3 | bellard | struct sockaddr_in saddr;
|
3601 | ffd843bc | ths | #ifndef _WIN32
|
3602 | ffd843bc | ths | struct sockaddr_un uaddr;
|
3603 | ffd843bc | ths | #endif
|
3604 | ffd843bc | ths | struct sockaddr *addr;
|
3605 | ffd843bc | ths | socklen_t addrlen; |
3606 | 0bab00f3 | bellard | |
3607 | ffd843bc | ths | #ifndef _WIN32
|
3608 | ffd843bc | ths | if (is_unix) {
|
3609 | ffd843bc | ths | addr = (struct sockaddr *)&uaddr;
|
3610 | ffd843bc | ths | addrlen = sizeof(uaddr);
|
3611 | ffd843bc | ths | if (parse_unix_path(&uaddr, host_str) < 0) |
3612 | ffd843bc | ths | goto fail;
|
3613 | ffd843bc | ths | } else
|
3614 | ffd843bc | ths | #endif
|
3615 | ffd843bc | ths | { |
3616 | ffd843bc | ths | addr = (struct sockaddr *)&saddr;
|
3617 | ffd843bc | ths | addrlen = sizeof(saddr);
|
3618 | ffd843bc | ths | if (parse_host_port(&saddr, host_str) < 0) |
3619 | ffd843bc | ths | goto fail;
|
3620 | ffd843bc | ths | } |
3621 | 0bab00f3 | bellard | |
3622 | 951f1351 | bellard | ptr = host_str; |
3623 | 951f1351 | bellard | while((ptr = strchr(ptr,','))) { |
3624 | 951f1351 | bellard | ptr++; |
3625 | 951f1351 | bellard | if (!strncmp(ptr,"server",6)) { |
3626 | 951f1351 | bellard | is_listen = 1;
|
3627 | 951f1351 | bellard | } else if (!strncmp(ptr,"nowait",6)) { |
3628 | 951f1351 | bellard | is_waitconnect = 0;
|
3629 | f7499989 | pbrook | } else if (!strncmp(ptr,"nodelay",6)) { |
3630 | f7499989 | pbrook | do_nodelay = 1;
|
3631 | 951f1351 | bellard | } else {
|
3632 | 951f1351 | bellard | printf("Unknown option: %s\n", ptr);
|
3633 | 951f1351 | bellard | goto fail;
|
3634 | 951f1351 | bellard | } |
3635 | 951f1351 | bellard | } |
3636 | 951f1351 | bellard | if (!is_listen)
|
3637 | 951f1351 | bellard | is_waitconnect = 0;
|
3638 | 951f1351 | bellard | |
3639 | 0bab00f3 | bellard | chr = qemu_mallocz(sizeof(CharDriverState));
|
3640 | 0bab00f3 | bellard | if (!chr)
|
3641 | 0bab00f3 | bellard | goto fail;
|
3642 | 0bab00f3 | bellard | s = qemu_mallocz(sizeof(TCPCharDriver));
|
3643 | 0bab00f3 | bellard | if (!s)
|
3644 | 0bab00f3 | bellard | goto fail;
|
3645 | ffd843bc | ths | |
3646 | ffd843bc | ths | #ifndef _WIN32
|
3647 | ffd843bc | ths | if (is_unix)
|
3648 | ffd843bc | ths | fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
3649 | ffd843bc | ths | else
|
3650 | ffd843bc | ths | #endif
|
3651 | ffd843bc | ths | fd = socket(PF_INET, SOCK_STREAM, 0);
|
3652 | 5fafdf24 | ths | |
3653 | 5fafdf24 | ths | if (fd < 0) |
3654 | 0bab00f3 | bellard | goto fail;
|
3655 | 951f1351 | bellard | |
3656 | 951f1351 | bellard | if (!is_waitconnect)
|
3657 | 951f1351 | bellard | socket_set_nonblock(fd); |
3658 | 0bab00f3 | bellard | |
3659 | 0bab00f3 | bellard | s->connected = 0;
|
3660 | 0bab00f3 | bellard | s->fd = -1;
|
3661 | 0bab00f3 | bellard | s->listen_fd = -1;
|
3662 | ffd843bc | ths | s->is_unix = is_unix; |
3663 | f7499989 | pbrook | s->do_nodelay = do_nodelay && !is_unix; |
3664 | ffd843bc | ths | |
3665 | ffd843bc | ths | chr->opaque = s; |
3666 | ffd843bc | ths | chr->chr_write = tcp_chr_write; |
3667 | ffd843bc | ths | chr->chr_close = tcp_chr_close; |
3668 | ffd843bc | ths | |
3669 | 0bab00f3 | bellard | if (is_listen) {
|
3670 | 0bab00f3 | bellard | /* allow fast reuse */
|
3671 | ffd843bc | ths | #ifndef _WIN32
|
3672 | ffd843bc | ths | if (is_unix) {
|
3673 | ffd843bc | ths | char path[109]; |
3674 | ae45d369 | bellard | pstrcpy(path, sizeof(path), uaddr.sun_path);
|
3675 | ffd843bc | ths | unlink(path); |
3676 | ffd843bc | ths | } else
|
3677 | ffd843bc | ths | #endif
|
3678 | ffd843bc | ths | { |
3679 | ffd843bc | ths | val = 1;
|
3680 | ffd843bc | ths | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
3681 | ffd843bc | ths | } |
3682 | 3b46e624 | ths | |
3683 | ffd843bc | ths | ret = bind(fd, addr, addrlen); |
3684 | ffd843bc | ths | if (ret < 0) |
3685 | 0bab00f3 | bellard | goto fail;
|
3686 | ffd843bc | ths | |
3687 | 0bab00f3 | bellard | ret = listen(fd, 0);
|
3688 | 0bab00f3 | bellard | if (ret < 0) |
3689 | 0bab00f3 | bellard | goto fail;
|
3690 | ffd843bc | ths | |
3691 | 0bab00f3 | bellard | s->listen_fd = fd; |
3692 | 0bab00f3 | bellard | qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
3693 | 951f1351 | bellard | if (is_telnet)
|
3694 | 951f1351 | bellard | s->do_telnetopt = 1;
|
3695 | 0bab00f3 | bellard | } else {
|
3696 | 0bab00f3 | bellard | for(;;) {
|
3697 | ffd843bc | ths | ret = connect(fd, addr, addrlen); |
3698 | 0bab00f3 | bellard | if (ret < 0) { |
3699 | 0bab00f3 | bellard | err = socket_error(); |
3700 | 0bab00f3 | bellard | if (err == EINTR || err == EWOULDBLOCK) {
|
3701 | 0bab00f3 | bellard | } else if (err == EINPROGRESS) { |
3702 | 0bab00f3 | bellard | break;
|
3703 | f5b12268 | ths | #ifdef _WIN32
|
3704 | f5b12268 | ths | } else if (err == WSAEALREADY) { |
3705 | f5b12268 | ths | break;
|
3706 | f5b12268 | ths | #endif
|
3707 | 0bab00f3 | bellard | } else {
|
3708 | 0bab00f3 | bellard | goto fail;
|
3709 | 0bab00f3 | bellard | } |
3710 | 0bab00f3 | bellard | } else {
|
3711 | 0bab00f3 | bellard | s->connected = 1;
|
3712 | 0bab00f3 | bellard | break;
|
3713 | 0bab00f3 | bellard | } |
3714 | 0bab00f3 | bellard | } |
3715 | 0bab00f3 | bellard | s->fd = fd; |
3716 | f7499989 | pbrook | socket_set_nodelay(fd); |
3717 | 0bab00f3 | bellard | if (s->connected)
|
3718 | 0bab00f3 | bellard | tcp_chr_connect(chr); |
3719 | 0bab00f3 | bellard | else
|
3720 | 0bab00f3 | bellard | qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
|
3721 | 0bab00f3 | bellard | } |
3722 | 3b46e624 | ths | |
3723 | 951f1351 | bellard | if (is_listen && is_waitconnect) {
|
3724 | 951f1351 | bellard | printf("QEMU waiting for connection on: %s\n", host_str);
|
3725 | 951f1351 | bellard | tcp_chr_accept(chr); |
3726 | 951f1351 | bellard | socket_set_nonblock(s->listen_fd); |
3727 | 951f1351 | bellard | } |
3728 | 951f1351 | bellard | |
3729 | 0bab00f3 | bellard | return chr;
|
3730 | 0bab00f3 | bellard | fail:
|
3731 | 0bab00f3 | bellard | if (fd >= 0) |
3732 | 0bab00f3 | bellard | closesocket(fd); |
3733 | 0bab00f3 | bellard | qemu_free(s); |
3734 | 0bab00f3 | bellard | qemu_free(chr); |
3735 | 0bab00f3 | bellard | return NULL; |
3736 | 0bab00f3 | bellard | } |
3737 | 0bab00f3 | bellard | |
3738 | 82c643ff | bellard | CharDriverState *qemu_chr_open(const char *filename) |
3739 | 82c643ff | bellard | { |
3740 | f8d179e3 | bellard | const char *p; |
3741 | fd1dff4b | bellard | |
3742 | 82c643ff | bellard | if (!strcmp(filename, "vc")) { |
3743 | af3a9031 | ths | return text_console_init(&display_state, 0); |
3744 | af3a9031 | ths | } else if (strstart(filename, "vc:", &p)) { |
3745 | af3a9031 | ths | return text_console_init(&display_state, p);
|
3746 | 82c643ff | bellard | } else if (!strcmp(filename, "null")) { |
3747 | 82c643ff | bellard | return qemu_chr_open_null();
|
3748 | 5fafdf24 | ths | } else
|
3749 | 0bab00f3 | bellard | if (strstart(filename, "tcp:", &p)) { |
3750 | ffd843bc | ths | return qemu_chr_open_tcp(p, 0, 0); |
3751 | 0bab00f3 | bellard | } else
|
3752 | 951f1351 | bellard | if (strstart(filename, "telnet:", &p)) { |
3753 | ffd843bc | ths | return qemu_chr_open_tcp(p, 1, 0); |
3754 | 0bab00f3 | bellard | } else
|
3755 | 0bab00f3 | bellard | if (strstart(filename, "udp:", &p)) { |
3756 | 0bab00f3 | bellard | return qemu_chr_open_udp(p);
|
3757 | 0bab00f3 | bellard | } else
|
3758 | 20d8a3ed | ths | if (strstart(filename, "mon:", &p)) { |
3759 | 20d8a3ed | ths | CharDriverState *drv = qemu_chr_open(p); |
3760 | 20d8a3ed | ths | if (drv) {
|
3761 | 20d8a3ed | ths | drv = qemu_chr_open_mux(drv); |
3762 | 20d8a3ed | ths | monitor_init(drv, !nographic); |
3763 | 20d8a3ed | ths | return drv;
|
3764 | 20d8a3ed | ths | } |
3765 | 20d8a3ed | ths | printf("Unable to open driver: %s\n", p);
|
3766 | 20d8a3ed | ths | return 0; |
3767 | 20d8a3ed | ths | } else
|
3768 | 7664728b | bellard | #ifndef _WIN32
|
3769 | ffd843bc | ths | if (strstart(filename, "unix:", &p)) { |
3770 | ffd843bc | ths | return qemu_chr_open_tcp(p, 0, 1); |
3771 | ffd843bc | ths | } else if (strstart(filename, "file:", &p)) { |
3772 | f8d179e3 | bellard | return qemu_chr_open_file_out(p);
|
3773 | f8d179e3 | bellard | } else if (strstart(filename, "pipe:", &p)) { |
3774 | f8d179e3 | bellard | return qemu_chr_open_pipe(p);
|
3775 | 7664728b | bellard | } else if (!strcmp(filename, "pty")) { |
3776 | 82c643ff | bellard | return qemu_chr_open_pty();
|
3777 | 82c643ff | bellard | } else if (!strcmp(filename, "stdio")) { |
3778 | 82c643ff | bellard | return qemu_chr_open_stdio();
|
3779 | 5fafdf24 | ths | } else
|
3780 | f8d179e3 | bellard | #if defined(__linux__)
|
3781 | e57a8c0e | bellard | if (strstart(filename, "/dev/parport", NULL)) { |
3782 | e57a8c0e | bellard | return qemu_chr_open_pp(filename);
|
3783 | 5fafdf24 | ths | } else
|
3784 | aec62507 | ths | #endif
|
3785 | 3fda388a | ths | #if defined(__linux__) || defined(__sun__)
|
3786 | f8d179e3 | bellard | if (strstart(filename, "/dev/", NULL)) { |
3787 | f8d179e3 | bellard | return qemu_chr_open_tty(filename);
|
3788 | 3fda388a | ths | } else
|
3789 | 3fda388a | ths | #endif
|
3790 | aec62507 | ths | #else /* !_WIN32 */ |
3791 | f331110f | bellard | if (strstart(filename, "COM", NULL)) { |
3792 | f331110f | bellard | return qemu_chr_open_win(filename);
|
3793 | f331110f | bellard | } else
|
3794 | f331110f | bellard | if (strstart(filename, "pipe:", &p)) { |
3795 | f331110f | bellard | return qemu_chr_open_win_pipe(p);
|
3796 | f331110f | bellard | } else
|
3797 | 72d46479 | ths | if (strstart(filename, "con:", NULL)) { |
3798 | 72d46479 | ths | return qemu_chr_open_win_con(filename);
|
3799 | 72d46479 | ths | } else
|
3800 | f331110f | bellard | if (strstart(filename, "file:", &p)) { |
3801 | f331110f | bellard | return qemu_chr_open_win_file_out(p);
|
3802 | 2e4d9fb1 | aurel32 | } else
|
3803 | 2e4d9fb1 | aurel32 | #endif
|
3804 | 2e4d9fb1 | aurel32 | #ifdef CONFIG_BRLAPI
|
3805 | 2e4d9fb1 | aurel32 | if (!strcmp(filename, "braille")) { |
3806 | 2e4d9fb1 | aurel32 | return chr_baum_init();
|
3807 | 2e4d9fb1 | aurel32 | } else
|
3808 | f331110f | bellard | #endif
|
3809 | 82c643ff | bellard | { |
3810 | 82c643ff | bellard | return NULL; |
3811 | 82c643ff | bellard | } |
3812 | 82c643ff | bellard | } |
3813 | 82c643ff | bellard | |
3814 | f331110f | bellard | void qemu_chr_close(CharDriverState *chr)
|
3815 | f331110f | bellard | { |
3816 | f331110f | bellard | if (chr->chr_close)
|
3817 | f331110f | bellard | chr->chr_close(chr); |
3818 | a11d070e | balrog | qemu_free(chr); |
3819 | f331110f | bellard | } |
3820 | f331110f | bellard | |
3821 | 80cabfad | bellard | /***********************************************************/
|
3822 | 7c9d8e07 | bellard | /* network device redirectors */
|
3823 | 330d0414 | bellard | |
3824 | 3f4afa14 | j_mayer | __attribute__ (( unused )) |
3825 | 9596ebb7 | pbrook | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
3826 | c20709aa | bellard | { |
3827 | c20709aa | bellard | int len, i, j, c;
|
3828 | c20709aa | bellard | |
3829 | c20709aa | bellard | for(i=0;i<size;i+=16) { |
3830 | c20709aa | bellard | len = size - i; |
3831 | c20709aa | bellard | if (len > 16) |
3832 | c20709aa | bellard | len = 16;
|
3833 | c20709aa | bellard | fprintf(f, "%08x ", i);
|
3834 | c20709aa | bellard | for(j=0;j<16;j++) { |
3835 | c20709aa | bellard | if (j < len)
|
3836 | c20709aa | bellard | fprintf(f, " %02x", buf[i+j]);
|
3837 | c20709aa | bellard | else
|
3838 | c20709aa | bellard | fprintf(f, " ");
|
3839 | c20709aa | bellard | } |
3840 | c20709aa | bellard | fprintf(f, " ");
|
3841 | c20709aa | bellard | for(j=0;j<len;j++) { |
3842 | c20709aa | bellard | c = buf[i+j]; |
3843 | c20709aa | bellard | if (c < ' ' || c > '~') |
3844 | c20709aa | bellard | c = '.';
|
3845 | c20709aa | bellard | fprintf(f, "%c", c);
|
3846 | c20709aa | bellard | } |
3847 | c20709aa | bellard | fprintf(f, "\n");
|
3848 | c20709aa | bellard | } |
3849 | c20709aa | bellard | } |
3850 | c20709aa | bellard | |
3851 | 7c9d8e07 | bellard | static int parse_macaddr(uint8_t *macaddr, const char *p) |
3852 | c20709aa | bellard | { |
3853 | 7c9d8e07 | bellard | int i;
|
3854 | 76ea08f9 | balrog | char *last_char;
|
3855 | 76ea08f9 | balrog | long int offset; |
3856 | 76ea08f9 | balrog | |
3857 | 76ea08f9 | balrog | errno = 0;
|
3858 | 76ea08f9 | balrog | offset = strtol(p, &last_char, 0);
|
3859 | 76ea08f9 | balrog | if (0 == errno && '\0' == *last_char && |
3860 | 76ea08f9 | balrog | offset >= 0 && offset <= 0xFFFFFF) { |
3861 | 76ea08f9 | balrog | macaddr[3] = (offset & 0xFF0000) >> 16; |
3862 | 76ea08f9 | balrog | macaddr[4] = (offset & 0xFF00) >> 8; |
3863 | 76ea08f9 | balrog | macaddr[5] = offset & 0xFF; |
3864 | 76ea08f9 | balrog | return 0; |
3865 | 76ea08f9 | balrog | } else {
|
3866 | 76ea08f9 | balrog | for(i = 0; i < 6; i++) { |
3867 | 76ea08f9 | balrog | macaddr[i] = strtol(p, (char **)&p, 16); |
3868 | 76ea08f9 | balrog | if (i == 5) { |
3869 | 76ea08f9 | balrog | if (*p != '\0') |
3870 | 76ea08f9 | balrog | return -1; |
3871 | 76ea08f9 | balrog | } else {
|
3872 | 76ea08f9 | balrog | if (*p != ':' && *p != '-') |
3873 | 76ea08f9 | balrog | return -1; |
3874 | 76ea08f9 | balrog | p++; |
3875 | 76ea08f9 | balrog | } |
3876 | 7c9d8e07 | bellard | } |
3877 | 76ea08f9 | balrog | return 0; |
3878 | 7c9d8e07 | bellard | } |
3879 | 76ea08f9 | balrog | |
3880 | 76ea08f9 | balrog | return -1; |
3881 | c20709aa | bellard | } |
3882 | 67b915a5 | bellard | |
3883 | 7c9d8e07 | bellard | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
3884 | 67b915a5 | bellard | { |
3885 | 7c9d8e07 | bellard | const char *p, *p1; |
3886 | 7c9d8e07 | bellard | int len;
|
3887 | 7c9d8e07 | bellard | p = *pp; |
3888 | 7c9d8e07 | bellard | p1 = strchr(p, sep); |
3889 | 7c9d8e07 | bellard | if (!p1)
|
3890 | 7c9d8e07 | bellard | return -1; |
3891 | 7c9d8e07 | bellard | len = p1 - p; |
3892 | 7c9d8e07 | bellard | p1++; |
3893 | 7c9d8e07 | bellard | if (buf_size > 0) { |
3894 | 7c9d8e07 | bellard | if (len > buf_size - 1) |
3895 | 7c9d8e07 | bellard | len = buf_size - 1;
|
3896 | 7c9d8e07 | bellard | memcpy(buf, p, len); |
3897 | 7c9d8e07 | bellard | buf[len] = '\0';
|
3898 | 7c9d8e07 | bellard | } |
3899 | 7c9d8e07 | bellard | *pp = p1; |
3900 | 7c9d8e07 | bellard | return 0; |
3901 | c20709aa | bellard | } |
3902 | c20709aa | bellard | |
3903 | 951f1351 | bellard | int parse_host_src_port(struct sockaddr_in *haddr, |
3904 | 951f1351 | bellard | struct sockaddr_in *saddr,
|
3905 | 951f1351 | bellard | const char *input_str) |
3906 | 951f1351 | bellard | { |
3907 | 951f1351 | bellard | char *str = strdup(input_str);
|
3908 | 951f1351 | bellard | char *host_str = str;
|
3909 | 951f1351 | bellard | char *src_str;
|
3910 | 951f1351 | bellard | char *ptr;
|
3911 | 951f1351 | bellard | |
3912 | 951f1351 | bellard | /*
|
3913 | 951f1351 | bellard | * Chop off any extra arguments at the end of the string which
|
3914 | 951f1351 | bellard | * would start with a comma, then fill in the src port information
|
3915 | 951f1351 | bellard | * if it was provided else use the "any address" and "any port".
|
3916 | 951f1351 | bellard | */
|
3917 | 951f1351 | bellard | if ((ptr = strchr(str,','))) |
3918 | 951f1351 | bellard | *ptr = '\0';
|
3919 | 951f1351 | bellard | |
3920 | 951f1351 | bellard | if ((src_str = strchr(input_str,'@'))) { |
3921 | 951f1351 | bellard | *src_str = '\0';
|
3922 | 951f1351 | bellard | src_str++; |
3923 | 951f1351 | bellard | } |
3924 | 951f1351 | bellard | |
3925 | 951f1351 | bellard | if (parse_host_port(haddr, host_str) < 0) |
3926 | 951f1351 | bellard | goto fail;
|
3927 | 951f1351 | bellard | |
3928 | 951f1351 | bellard | if (!src_str || *src_str == '\0') |
3929 | 951f1351 | bellard | src_str = ":0";
|
3930 | 951f1351 | bellard | |
3931 | 951f1351 | bellard | if (parse_host_port(saddr, src_str) < 0) |
3932 | 951f1351 | bellard | goto fail;
|
3933 | 951f1351 | bellard | |
3934 | 951f1351 | bellard | free(str); |
3935 | 951f1351 | bellard | return(0); |
3936 | 951f1351 | bellard | |
3937 | 951f1351 | bellard | fail:
|
3938 | 951f1351 | bellard | free(str); |
3939 | 951f1351 | bellard | return -1; |
3940 | 951f1351 | bellard | } |
3941 | 951f1351 | bellard | |
3942 | 7c9d8e07 | bellard | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
3943 | 7c9d8e07 | bellard | { |
3944 | 7c9d8e07 | bellard | char buf[512]; |
3945 | 7c9d8e07 | bellard | struct hostent *he;
|
3946 | 7c9d8e07 | bellard | const char *p, *r; |
3947 | 7c9d8e07 | bellard | int port;
|
3948 | 7c9d8e07 | bellard | |
3949 | 7c9d8e07 | bellard | p = str; |
3950 | 7c9d8e07 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
3951 | 7c9d8e07 | bellard | return -1; |
3952 | 7c9d8e07 | bellard | saddr->sin_family = AF_INET; |
3953 | 7c9d8e07 | bellard | if (buf[0] == '\0') { |
3954 | 7c9d8e07 | bellard | saddr->sin_addr.s_addr = 0;
|
3955 | 7c9d8e07 | bellard | } else {
|
3956 | 7c9d8e07 | bellard | if (isdigit(buf[0])) { |
3957 | 7c9d8e07 | bellard | if (!inet_aton(buf, &saddr->sin_addr))
|
3958 | 7c9d8e07 | bellard | return -1; |
3959 | 7c9d8e07 | bellard | } else {
|
3960 | 7c9d8e07 | bellard | if ((he = gethostbyname(buf)) == NULL) |
3961 | 7c9d8e07 | bellard | return - 1; |
3962 | 7c9d8e07 | bellard | saddr->sin_addr = *(struct in_addr *)he->h_addr;
|
3963 | 7c9d8e07 | bellard | } |
3964 | 7c9d8e07 | bellard | } |
3965 | 7c9d8e07 | bellard | port = strtol(p, (char **)&r, 0); |
3966 | 7c9d8e07 | bellard | if (r == p)
|
3967 | 7c9d8e07 | bellard | return -1; |
3968 | 7c9d8e07 | bellard | saddr->sin_port = htons(port); |
3969 | 7c9d8e07 | bellard | return 0; |
3970 | 7c9d8e07 | bellard | } |
3971 | c20709aa | bellard | |
3972 | 52f61fde | ths | #ifndef _WIN32
|
3973 | 52f61fde | ths | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) |
3974 | ffd843bc | ths | { |
3975 | ffd843bc | ths | const char *p; |
3976 | ffd843bc | ths | int len;
|
3977 | ffd843bc | ths | |
3978 | ffd843bc | ths | len = MIN(108, strlen(str));
|
3979 | ffd843bc | ths | p = strchr(str, ',');
|
3980 | ffd843bc | ths | if (p)
|
3981 | ffd843bc | ths | len = MIN(len, p - str); |
3982 | ffd843bc | ths | |
3983 | ffd843bc | ths | memset(uaddr, 0, sizeof(*uaddr)); |
3984 | ffd843bc | ths | |
3985 | ffd843bc | ths | uaddr->sun_family = AF_UNIX; |
3986 | ffd843bc | ths | memcpy(uaddr->sun_path, str, len); |
3987 | ffd843bc | ths | |
3988 | ffd843bc | ths | return 0; |
3989 | ffd843bc | ths | } |
3990 | 52f61fde | ths | #endif
|
3991 | ffd843bc | ths | |
3992 | 7c9d8e07 | bellard | /* find or alloc a new VLAN */
|
3993 | 7c9d8e07 | bellard | VLANState *qemu_find_vlan(int id)
|
3994 | c20709aa | bellard | { |
3995 | 7c9d8e07 | bellard | VLANState **pvlan, *vlan; |
3996 | 7c9d8e07 | bellard | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
3997 | 7c9d8e07 | bellard | if (vlan->id == id)
|
3998 | 7c9d8e07 | bellard | return vlan;
|
3999 | 7c9d8e07 | bellard | } |
4000 | 7c9d8e07 | bellard | vlan = qemu_mallocz(sizeof(VLANState));
|
4001 | 7c9d8e07 | bellard | if (!vlan)
|
4002 | 7c9d8e07 | bellard | return NULL; |
4003 | 7c9d8e07 | bellard | vlan->id = id; |
4004 | 7c9d8e07 | bellard | vlan->next = NULL;
|
4005 | 7c9d8e07 | bellard | pvlan = &first_vlan; |
4006 | 7c9d8e07 | bellard | while (*pvlan != NULL) |
4007 | 7c9d8e07 | bellard | pvlan = &(*pvlan)->next; |
4008 | 7c9d8e07 | bellard | *pvlan = vlan; |
4009 | 7c9d8e07 | bellard | return vlan;
|
4010 | c20709aa | bellard | } |
4011 | c20709aa | bellard | |
4012 | 7c9d8e07 | bellard | VLANClientState *qemu_new_vlan_client(VLANState *vlan, |
4013 | d861b05e | pbrook | IOReadHandler *fd_read, |
4014 | d861b05e | pbrook | IOCanRWHandler *fd_can_read, |
4015 | d861b05e | pbrook | void *opaque)
|
4016 | c20709aa | bellard | { |
4017 | 7c9d8e07 | bellard | VLANClientState *vc, **pvc; |
4018 | 7c9d8e07 | bellard | vc = qemu_mallocz(sizeof(VLANClientState));
|
4019 | 7c9d8e07 | bellard | if (!vc)
|
4020 | 7c9d8e07 | bellard | return NULL; |
4021 | 7c9d8e07 | bellard | vc->fd_read = fd_read; |
4022 | d861b05e | pbrook | vc->fd_can_read = fd_can_read; |
4023 | 7c9d8e07 | bellard | vc->opaque = opaque; |
4024 | 7c9d8e07 | bellard | vc->vlan = vlan; |
4025 | 7c9d8e07 | bellard | |
4026 | 7c9d8e07 | bellard | vc->next = NULL;
|
4027 | 7c9d8e07 | bellard | pvc = &vlan->first_client; |
4028 | 7c9d8e07 | bellard | while (*pvc != NULL) |
4029 | 7c9d8e07 | bellard | pvc = &(*pvc)->next; |
4030 | 7c9d8e07 | bellard | *pvc = vc; |
4031 | 7c9d8e07 | bellard | return vc;
|
4032 | c20709aa | bellard | } |
4033 | c20709aa | bellard | |
4034 | dcf414d6 | balrog | void qemu_del_vlan_client(VLANClientState *vc)
|
4035 | dcf414d6 | balrog | { |
4036 | dcf414d6 | balrog | VLANClientState **pvc = &vc->vlan->first_client; |
4037 | dcf414d6 | balrog | |
4038 | dcf414d6 | balrog | while (*pvc != NULL) |
4039 | dcf414d6 | balrog | if (*pvc == vc) {
|
4040 | dcf414d6 | balrog | *pvc = vc->next; |
4041 | dcf414d6 | balrog | free(vc); |
4042 | dcf414d6 | balrog | break;
|
4043 | dcf414d6 | balrog | } else
|
4044 | dcf414d6 | balrog | pvc = &(*pvc)->next; |
4045 | dcf414d6 | balrog | } |
4046 | dcf414d6 | balrog | |
4047 | d861b05e | pbrook | int qemu_can_send_packet(VLANClientState *vc1)
|
4048 | d861b05e | pbrook | { |
4049 | d861b05e | pbrook | VLANState *vlan = vc1->vlan; |
4050 | d861b05e | pbrook | VLANClientState *vc; |
4051 | d861b05e | pbrook | |
4052 | d861b05e | pbrook | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
4053 | d861b05e | pbrook | if (vc != vc1) {
|
4054 | fbd1711d | balrog | if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
|
4055 | fbd1711d | balrog | return 1; |
4056 | d861b05e | pbrook | } |
4057 | d861b05e | pbrook | } |
4058 | fbd1711d | balrog | return 0; |
4059 | d861b05e | pbrook | } |
4060 | d861b05e | pbrook | |
4061 | 7c9d8e07 | bellard | void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size) |
4062 | c20709aa | bellard | { |
4063 | 7c9d8e07 | bellard | VLANState *vlan = vc1->vlan; |
4064 | 7c9d8e07 | bellard | VLANClientState *vc; |
4065 | 7c9d8e07 | bellard | |
4066 | 7c9d8e07 | bellard | #if 0
|
4067 | 7c9d8e07 | bellard | printf("vlan %d send:\n", vlan->id);
|
4068 | 7c9d8e07 | bellard | hex_dump(stdout, buf, size);
|
4069 | 7c9d8e07 | bellard | #endif
|
4070 | 7c9d8e07 | bellard | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
4071 | 7c9d8e07 | bellard | if (vc != vc1) {
|
4072 | 7c9d8e07 | bellard | vc->fd_read(vc->opaque, buf, size); |
4073 | 7c9d8e07 | bellard | } |
4074 | 7c9d8e07 | bellard | } |
4075 | 67b915a5 | bellard | } |
4076 | 67b915a5 | bellard | |
4077 | c20709aa | bellard | #if defined(CONFIG_SLIRP)
|
4078 | c20709aa | bellard | |
4079 | c20709aa | bellard | /* slirp network adapter */
|
4080 | c20709aa | bellard | |
4081 | c20709aa | bellard | static int slirp_inited; |
4082 | 7c9d8e07 | bellard | static VLANClientState *slirp_vc;
|
4083 | c20709aa | bellard | |
4084 | c20709aa | bellard | int slirp_can_output(void) |
4085 | c20709aa | bellard | { |
4086 | 3b7f5d47 | pbrook | return !slirp_vc || qemu_can_send_packet(slirp_vc);
|
4087 | c20709aa | bellard | } |
4088 | c20709aa | bellard | |
4089 | c20709aa | bellard | void slirp_output(const uint8_t *pkt, int pkt_len) |
4090 | 67b915a5 | bellard | { |
4091 | c20709aa | bellard | #if 0
|
4092 | 7c9d8e07 | bellard | printf("slirp output:\n");
|
4093 | c20709aa | bellard | hex_dump(stdout, pkt, pkt_len);
|
4094 | c20709aa | bellard | #endif
|
4095 | 3b7f5d47 | pbrook | if (!slirp_vc)
|
4096 | 3b7f5d47 | pbrook | return;
|
4097 | 7c9d8e07 | bellard | qemu_send_packet(slirp_vc, pkt, pkt_len); |
4098 | 67b915a5 | bellard | } |
4099 | 67b915a5 | bellard | |
4100 | 7c9d8e07 | bellard | static void slirp_receive(void *opaque, const uint8_t *buf, int size) |
4101 | c20709aa | bellard | { |
4102 | c20709aa | bellard | #if 0
|
4103 | 7c9d8e07 | bellard | printf("slirp input:\n");
|
4104 | c20709aa | bellard | hex_dump(stdout, buf, size);
|
4105 | c20709aa | bellard | #endif
|
4106 | c20709aa | bellard | slirp_input(buf, size); |
4107 | c20709aa | bellard | } |
4108 | c20709aa | bellard | |
4109 | 7c9d8e07 | bellard | static int net_slirp_init(VLANState *vlan) |
4110 | c20709aa | bellard | { |
4111 | c20709aa | bellard | if (!slirp_inited) {
|
4112 | c20709aa | bellard | slirp_inited = 1;
|
4113 | c20709aa | bellard | slirp_init(); |
4114 | c20709aa | bellard | } |
4115 | 5fafdf24 | ths | slirp_vc = qemu_new_vlan_client(vlan, |
4116 | d861b05e | pbrook | slirp_receive, NULL, NULL); |
4117 | 7c9d8e07 | bellard | snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector"); |
4118 | 9bf05444 | bellard | return 0; |
4119 | 9bf05444 | bellard | } |
4120 | 9bf05444 | bellard | |
4121 | 9bf05444 | bellard | static void net_slirp_redir(const char *redir_str) |
4122 | 9bf05444 | bellard | { |
4123 | 9bf05444 | bellard | int is_udp;
|
4124 | 9bf05444 | bellard | char buf[256], *r; |
4125 | 9bf05444 | bellard | const char *p; |
4126 | 9bf05444 | bellard | struct in_addr guest_addr;
|
4127 | 9bf05444 | bellard | int host_port, guest_port;
|
4128 | 3b46e624 | ths | |
4129 | 9bf05444 | bellard | if (!slirp_inited) {
|
4130 | 9bf05444 | bellard | slirp_inited = 1;
|
4131 | 9bf05444 | bellard | slirp_init(); |
4132 | 9bf05444 | bellard | } |
4133 | 9bf05444 | bellard | |
4134 | 9bf05444 | bellard | p = redir_str; |
4135 | 9bf05444 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
4136 | 9bf05444 | bellard | goto fail;
|
4137 | 9bf05444 | bellard | if (!strcmp(buf, "tcp")) { |
4138 | 9bf05444 | bellard | is_udp = 0;
|
4139 | 9bf05444 | bellard | } else if (!strcmp(buf, "udp")) { |
4140 | 9bf05444 | bellard | is_udp = 1;
|
4141 | 9bf05444 | bellard | } else {
|
4142 | 9bf05444 | bellard | goto fail;
|
4143 | 9bf05444 | bellard | } |
4144 | 9bf05444 | bellard | |
4145 | 9bf05444 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
4146 | 9bf05444 | bellard | goto fail;
|
4147 | 9bf05444 | bellard | host_port = strtol(buf, &r, 0);
|
4148 | 9bf05444 | bellard | if (r == buf)
|
4149 | 9bf05444 | bellard | goto fail;
|
4150 | 9bf05444 | bellard | |
4151 | 9bf05444 | bellard | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
4152 | 9bf05444 | bellard | goto fail;
|
4153 | 9bf05444 | bellard | if (buf[0] == '\0') { |
4154 | 9bf05444 | bellard | pstrcpy(buf, sizeof(buf), "10.0.2.15"); |
4155 | 9bf05444 | bellard | } |
4156 | 9bf05444 | bellard | if (!inet_aton(buf, &guest_addr))
|
4157 | 9bf05444 | bellard | goto fail;
|
4158 | 3b46e624 | ths | |
4159 | 9bf05444 | bellard | guest_port = strtol(p, &r, 0);
|
4160 | 9bf05444 | bellard | if (r == p)
|
4161 | 9bf05444 | bellard | goto fail;
|
4162 | 3b46e624 | ths | |
4163 | 9bf05444 | bellard | if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { |
4164 | 9bf05444 | bellard | fprintf(stderr, "qemu: could not set up redirection\n");
|
4165 | 9bf05444 | bellard | exit(1);
|
4166 | 9bf05444 | bellard | } |
4167 | 9bf05444 | bellard | return;
|
4168 | 9bf05444 | bellard | fail:
|
4169 | 9bf05444 | bellard | fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
|
4170 | 9bf05444 | bellard | exit(1);
|
4171 | 9bf05444 | bellard | } |
4172 | 3b46e624 | ths | |
4173 | c94c8d64 | bellard | #ifndef _WIN32
|
4174 | c94c8d64 | bellard | |
4175 | 9d728e8c | bellard | char smb_dir[1024]; |
4176 | 9d728e8c | bellard | |
4177 | 044fae83 | balrog | static void erase_dir(char *dir_name) |
4178 | 9d728e8c | bellard | { |
4179 | 9d728e8c | bellard | DIR *d; |
4180 | 9d728e8c | bellard | struct dirent *de;
|
4181 | 9d728e8c | bellard | char filename[1024]; |
4182 | 9d728e8c | bellard | |
4183 | 9d728e8c | bellard | /* erase all the files in the directory */
|
4184 | 044fae83 | balrog | if ((d = opendir(dir_name)) != 0) { |
4185 | 044fae83 | balrog | for(;;) {
|
4186 | 044fae83 | balrog | de = readdir(d); |
4187 | 044fae83 | balrog | if (!de)
|
4188 | 044fae83 | balrog | break;
|
4189 | 044fae83 | balrog | if (strcmp(de->d_name, ".") != 0 && |
4190 | 044fae83 | balrog | strcmp(de->d_name, "..") != 0) { |
4191 | 044fae83 | balrog | snprintf(filename, sizeof(filename), "%s/%s", |
4192 | 044fae83 | balrog | smb_dir, de->d_name); |
4193 | 044fae83 | balrog | if (unlink(filename) != 0) /* is it a directory? */ |
4194 | 044fae83 | balrog | erase_dir(filename); |
4195 | 044fae83 | balrog | } |
4196 | 9d728e8c | bellard | } |
4197 | 044fae83 | balrog | closedir(d); |
4198 | 044fae83 | balrog | rmdir(dir_name); |
4199 | 9d728e8c | bellard | } |
4200 | 044fae83 | balrog | } |
4201 | 044fae83 | balrog | |
4202 | 044fae83 | balrog | /* automatic user mode samba server configuration */
|
4203 | 044fae83 | balrog | static void smb_exit(void) |
4204 | 044fae83 | balrog | { |
4205 | 044fae83 | balrog | erase_dir(smb_dir); |
4206 | 9d728e8c | bellard | } |
4207 | 9d728e8c | bellard | |
4208 | 9d728e8c | bellard | /* automatic user mode samba server configuration */
|
4209 | 9596ebb7 | pbrook | static void net_slirp_smb(const char *exported_dir) |
4210 | 9d728e8c | bellard | { |
4211 | 9d728e8c | bellard | char smb_conf[1024]; |
4212 | 9d728e8c | bellard | char smb_cmdline[1024]; |
4213 | 9d728e8c | bellard | FILE *f; |
4214 | 9d728e8c | bellard | |
4215 | 9d728e8c | bellard | if (!slirp_inited) {
|
4216 | 9d728e8c | bellard | slirp_inited = 1;
|
4217 | 9d728e8c | bellard | slirp_init(); |
4218 | 9d728e8c | bellard | } |
4219 | 9d728e8c | bellard | |
4220 | 9d728e8c | bellard | /* XXX: better tmp dir construction */
|
4221 | 9d728e8c | bellard | snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); |
4222 | 9d728e8c | bellard | if (mkdir(smb_dir, 0700) < 0) { |
4223 | 9d728e8c | bellard | fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
|
4224 | 9d728e8c | bellard | exit(1);
|
4225 | 9d728e8c | bellard | } |
4226 | 9d728e8c | bellard | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); |
4227 | 3b46e624 | ths | |
4228 | 9d728e8c | bellard | f = fopen(smb_conf, "w");
|
4229 | 9d728e8c | bellard | if (!f) {
|
4230 | 9d728e8c | bellard | fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
|
4231 | 9d728e8c | bellard | exit(1);
|
4232 | 9d728e8c | bellard | } |
4233 | 5fafdf24 | ths | fprintf(f, |
4234 | 9d728e8c | bellard | "[global]\n"
|
4235 | 157777ef | bellard | "private dir=%s\n"
|
4236 | 157777ef | bellard | "smb ports=0\n"
|
4237 | 157777ef | bellard | "socket address=127.0.0.1\n"
|
4238 | 9d728e8c | bellard | "pid directory=%s\n"
|
4239 | 9d728e8c | bellard | "lock directory=%s\n"
|
4240 | 9d728e8c | bellard | "log file=%s/log.smbd\n"
|
4241 | 9d728e8c | bellard | "smb passwd file=%s/smbpasswd\n"
|
4242 | 03ffbb69 | bellard | "security = share\n"
|
4243 | 9d728e8c | bellard | "[qemu]\n"
|
4244 | 9d728e8c | bellard | "path=%s\n"
|
4245 | 9d728e8c | bellard | "read only=no\n"
|
4246 | 9d728e8c | bellard | "guest ok=yes\n",
|
4247 | 9d728e8c | bellard | smb_dir, |
4248 | 157777ef | bellard | smb_dir, |
4249 | 9d728e8c | bellard | smb_dir, |
4250 | 9d728e8c | bellard | smb_dir, |
4251 | 9d728e8c | bellard | smb_dir, |
4252 | 9d728e8c | bellard | exported_dir |
4253 | 9d728e8c | bellard | ); |
4254 | 9d728e8c | bellard | fclose(f); |
4255 | 9d728e8c | bellard | atexit(smb_exit); |
4256 | 9d728e8c | bellard | |
4257 | a14d6c8c | pbrook | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
4258 | a14d6c8c | pbrook | SMBD_COMMAND, smb_conf); |
4259 | 3b46e624 | ths | |
4260 | 9d728e8c | bellard | slirp_add_exec(0, smb_cmdline, 4, 139); |
4261 | 9d728e8c | bellard | } |
4262 | 9bf05444 | bellard | |
4263 | c94c8d64 | bellard | #endif /* !defined(_WIN32) */ |
4264 | 31a60e22 | blueswir1 | void do_info_slirp(void) |
4265 | 31a60e22 | blueswir1 | { |
4266 | 31a60e22 | blueswir1 | slirp_stats(); |
4267 | 31a60e22 | blueswir1 | } |
4268 | c94c8d64 | bellard | |
4269 | c20709aa | bellard | #endif /* CONFIG_SLIRP */ |
4270 | c20709aa | bellard | |
4271 | c20709aa | bellard | #if !defined(_WIN32)
|
4272 | 7c9d8e07 | bellard | |
4273 | 7c9d8e07 | bellard | typedef struct TAPState { |
4274 | 7c9d8e07 | bellard | VLANClientState *vc; |
4275 | 7c9d8e07 | bellard | int fd;
|
4276 | b46a8906 | ths | char down_script[1024]; |
4277 | 7c9d8e07 | bellard | } TAPState; |
4278 | 7c9d8e07 | bellard | |
4279 | 7c9d8e07 | bellard | static void tap_receive(void *opaque, const uint8_t *buf, int size) |
4280 | 7c9d8e07 | bellard | { |
4281 | 7c9d8e07 | bellard | TAPState *s = opaque; |
4282 | 7c9d8e07 | bellard | int ret;
|
4283 | 7c9d8e07 | bellard | for(;;) {
|
4284 | 7c9d8e07 | bellard | ret = write(s->fd, buf, size); |
4285 | 7c9d8e07 | bellard | if (ret < 0 && (errno == EINTR || errno == EAGAIN)) { |
4286 | 7c9d8e07 | bellard | } else {
|
4287 | 7c9d8e07 | bellard | break;
|
4288 | 7c9d8e07 | bellard | } |
4289 | 7c9d8e07 | bellard | } |
4290 | 7c9d8e07 | bellard | } |
4291 | 7c9d8e07 | bellard | |
4292 | 7c9d8e07 | bellard | static void tap_send(void *opaque) |
4293 | 7c9d8e07 | bellard | { |
4294 | 7c9d8e07 | bellard | TAPState *s = opaque; |
4295 | 7c9d8e07 | bellard | uint8_t buf[4096];
|
4296 | 7c9d8e07 | bellard | int size;
|
4297 | 7c9d8e07 | bellard | |
4298 | d5d10bc3 | ths | #ifdef __sun__
|
4299 | d5d10bc3 | ths | struct strbuf sbuf;
|
4300 | d5d10bc3 | ths | int f = 0; |
4301 | d5d10bc3 | ths | sbuf.maxlen = sizeof(buf);
|
4302 | d5d10bc3 | ths | sbuf.buf = buf; |
4303 | d5d10bc3 | ths | size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1; |
4304 | d5d10bc3 | ths | #else
|
4305 | 7c9d8e07 | bellard | size = read(s->fd, buf, sizeof(buf));
|
4306 | d5d10bc3 | ths | #endif
|
4307 | 7c9d8e07 | bellard | if (size > 0) { |
4308 | 7c9d8e07 | bellard | qemu_send_packet(s->vc, buf, size); |
4309 | 7c9d8e07 | bellard | } |
4310 | 7c9d8e07 | bellard | } |
4311 | 7c9d8e07 | bellard | |
4312 | 7c9d8e07 | bellard | /* fd support */
|
4313 | 7c9d8e07 | bellard | |
4314 | 7c9d8e07 | bellard | static TAPState *net_tap_fd_init(VLANState *vlan, int fd) |
4315 | 7c9d8e07 | bellard | { |
4316 | 7c9d8e07 | bellard | TAPState *s; |
4317 | 7c9d8e07 | bellard | |
4318 | 7c9d8e07 | bellard | s = qemu_mallocz(sizeof(TAPState));
|
4319 | 7c9d8e07 | bellard | if (!s)
|
4320 | 7c9d8e07 | bellard | return NULL; |
4321 | 7c9d8e07 | bellard | s->fd = fd; |
4322 | d861b05e | pbrook | s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
|
4323 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, tap_send, NULL, s);
|
4324 | 7c9d8e07 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd); |
4325 | 7c9d8e07 | bellard | return s;
|
4326 | 7c9d8e07 | bellard | } |
4327 | 7c9d8e07 | bellard | |
4328 | 5c40d2bd | ths | #if defined (_BSD) || defined (__FreeBSD_kernel__)
|
4329 | 7c9d8e07 | bellard | static int tap_open(char *ifname, int ifname_size) |
4330 | 7d3505c5 | bellard | { |
4331 | 7d3505c5 | bellard | int fd;
|
4332 | 7d3505c5 | bellard | char *dev;
|
4333 | 7d3505c5 | bellard | struct stat s;
|
4334 | 67b915a5 | bellard | |
4335 | aeb30be6 | balrog | TFR(fd = open("/dev/tap", O_RDWR));
|
4336 | 7d3505c5 | bellard | if (fd < 0) { |
4337 | 7d3505c5 | bellard | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
|
4338 | 7d3505c5 | bellard | return -1; |
4339 | 7d3505c5 | bellard | } |
4340 | 7d3505c5 | bellard | |
4341 | 7d3505c5 | bellard | fstat(fd, &s); |
4342 | 7d3505c5 | bellard | dev = devname(s.st_rdev, S_IFCHR); |
4343 | 7d3505c5 | bellard | pstrcpy(ifname, ifname_size, dev); |
4344 | 7d3505c5 | bellard | |
4345 | 7d3505c5 | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
4346 | 7d3505c5 | bellard | return fd;
|
4347 | 7d3505c5 | bellard | } |
4348 | ec530c81 | bellard | #elif defined(__sun__)
|
4349 | d5d10bc3 | ths | #define TUNNEWPPA (('T'<<16) | 0x0001) |
4350 | 5fafdf24 | ths | /*
|
4351 | 5fafdf24 | ths | * Allocate TAP device, returns opened fd.
|
4352 | d5d10bc3 | ths | * Stores dev name in the first arg(must be large enough).
|
4353 | 3b46e624 | ths | */
|
4354 | d5d10bc3 | ths | int tap_alloc(char *dev) |
4355 | d5d10bc3 | ths | { |
4356 | d5d10bc3 | ths | int tap_fd, if_fd, ppa = -1; |
4357 | d5d10bc3 | ths | static int ip_fd = 0; |
4358 | d5d10bc3 | ths | char *ptr;
|
4359 | d5d10bc3 | ths | |
4360 | d5d10bc3 | ths | static int arp_fd = 0; |
4361 | d5d10bc3 | ths | int ip_muxid, arp_muxid;
|
4362 | d5d10bc3 | ths | struct strioctl strioc_if, strioc_ppa;
|
4363 | d5d10bc3 | ths | int link_type = I_PLINK;;
|
4364 | d5d10bc3 | ths | struct lifreq ifr;
|
4365 | d5d10bc3 | ths | char actual_name[32] = ""; |
4366 | d5d10bc3 | ths | |
4367 | d5d10bc3 | ths | memset(&ifr, 0x0, sizeof(ifr)); |
4368 | d5d10bc3 | ths | |
4369 | d5d10bc3 | ths | if( *dev ){
|
4370 | 5fafdf24 | ths | ptr = dev; |
4371 | 5fafdf24 | ths | while( *ptr && !isdigit((int)*ptr) ) ptr++; |
4372 | d5d10bc3 | ths | ppa = atoi(ptr); |
4373 | d5d10bc3 | ths | } |
4374 | d5d10bc3 | ths | |
4375 | d5d10bc3 | ths | /* Check if IP device was opened */
|
4376 | d5d10bc3 | ths | if( ip_fd )
|
4377 | d5d10bc3 | ths | close(ip_fd); |
4378 | d5d10bc3 | ths | |
4379 | aeb30be6 | balrog | TFR(ip_fd = open("/dev/udp", O_RDWR, 0)); |
4380 | aeb30be6 | balrog | if (ip_fd < 0) { |
4381 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
|
4382 | d5d10bc3 | ths | return -1; |
4383 | d5d10bc3 | ths | } |
4384 | d5d10bc3 | ths | |
4385 | aeb30be6 | balrog | TFR(tap_fd = open("/dev/tap", O_RDWR, 0)); |
4386 | aeb30be6 | balrog | if (tap_fd < 0) { |
4387 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't open /dev/tap");
|
4388 | d5d10bc3 | ths | return -1; |
4389 | d5d10bc3 | ths | } |
4390 | d5d10bc3 | ths | |
4391 | d5d10bc3 | ths | /* Assign a new PPA and get its unit number. */
|
4392 | d5d10bc3 | ths | strioc_ppa.ic_cmd = TUNNEWPPA; |
4393 | d5d10bc3 | ths | strioc_ppa.ic_timout = 0;
|
4394 | d5d10bc3 | ths | strioc_ppa.ic_len = sizeof(ppa);
|
4395 | d5d10bc3 | ths | strioc_ppa.ic_dp = (char *)&ppa;
|
4396 | d5d10bc3 | ths | if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) |
4397 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't assign new interface");
|
4398 | d5d10bc3 | ths | |
4399 | aeb30be6 | balrog | TFR(if_fd = open("/dev/tap", O_RDWR, 0)); |
4400 | aeb30be6 | balrog | if (if_fd < 0) { |
4401 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't open /dev/tap (2)");
|
4402 | d5d10bc3 | ths | return -1; |
4403 | d5d10bc3 | ths | } |
4404 | d5d10bc3 | ths | if(ioctl(if_fd, I_PUSH, "ip") < 0){ |
4405 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't push IP module");
|
4406 | d5d10bc3 | ths | return -1; |
4407 | d5d10bc3 | ths | } |
4408 | d5d10bc3 | ths | |
4409 | d5d10bc3 | ths | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) |
4410 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't get flags\n");
|
4411 | d5d10bc3 | ths | |
4412 | d5d10bc3 | ths | snprintf (actual_name, 32, "tap%d", ppa); |
4413 | d5d10bc3 | ths | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
|
4414 | d5d10bc3 | ths | |
4415 | d5d10bc3 | ths | ifr.lifr_ppa = ppa; |
4416 | d5d10bc3 | ths | /* Assign ppa according to the unit number returned by tun device */
|
4417 | d5d10bc3 | ths | |
4418 | d5d10bc3 | ths | if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) |
4419 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't set PPA %d", ppa);
|
4420 | d5d10bc3 | ths | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) |
4421 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't get flags\n");
|
4422 | d5d10bc3 | ths | /* Push arp module to if_fd */
|
4423 | d5d10bc3 | ths | if (ioctl (if_fd, I_PUSH, "arp") < 0) |
4424 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't push ARP module (2)");
|
4425 | d5d10bc3 | ths | |
4426 | d5d10bc3 | ths | /* Push arp module to ip_fd */
|
4427 | d5d10bc3 | ths | if (ioctl (ip_fd, I_POP, NULL) < 0) |
4428 | d5d10bc3 | ths | syslog (LOG_ERR, "I_POP failed\n");
|
4429 | d5d10bc3 | ths | if (ioctl (ip_fd, I_PUSH, "arp") < 0) |
4430 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't push ARP module (3)\n");
|
4431 | d5d10bc3 | ths | /* Open arp_fd */
|
4432 | aeb30be6 | balrog | TFR(arp_fd = open ("/dev/tap", O_RDWR, 0)); |
4433 | aeb30be6 | balrog | if (arp_fd < 0) |
4434 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't open %s\n", "/dev/tap"); |
4435 | d5d10bc3 | ths | |
4436 | d5d10bc3 | ths | /* Set ifname to arp */
|
4437 | d5d10bc3 | ths | strioc_if.ic_cmd = SIOCSLIFNAME; |
4438 | d5d10bc3 | ths | strioc_if.ic_timout = 0;
|
4439 | d5d10bc3 | ths | strioc_if.ic_len = sizeof(ifr);
|
4440 | d5d10bc3 | ths | strioc_if.ic_dp = (char *)𝔦
|
4441 | d5d10bc3 | ths | if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ |
4442 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't set ifname to arp\n");
|
4443 | d5d10bc3 | ths | } |
4444 | d5d10bc3 | ths | |
4445 | d5d10bc3 | ths | if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ |
4446 | d5d10bc3 | ths | syslog(LOG_ERR, "Can't link TAP device to IP");
|
4447 | d5d10bc3 | ths | return -1; |
4448 | d5d10bc3 | ths | } |
4449 | d5d10bc3 | ths | |
4450 | d5d10bc3 | ths | if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) |
4451 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't link TAP device to ARP");
|
4452 | d5d10bc3 | ths | |
4453 | d5d10bc3 | ths | close (if_fd); |
4454 | d5d10bc3 | ths | |
4455 | d5d10bc3 | ths | memset(&ifr, 0x0, sizeof(ifr)); |
4456 | d5d10bc3 | ths | strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
|
4457 | d5d10bc3 | ths | ifr.lifr_ip_muxid = ip_muxid; |
4458 | d5d10bc3 | ths | ifr.lifr_arp_muxid = arp_muxid; |
4459 | d5d10bc3 | ths | |
4460 | d5d10bc3 | ths | if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) |
4461 | d5d10bc3 | ths | { |
4462 | d5d10bc3 | ths | ioctl (ip_fd, I_PUNLINK , arp_muxid); |
4463 | d5d10bc3 | ths | ioctl (ip_fd, I_PUNLINK, ip_muxid); |
4464 | d5d10bc3 | ths | syslog (LOG_ERR, "Can't set multiplexor id");
|
4465 | d5d10bc3 | ths | } |
4466 | d5d10bc3 | ths | |
4467 | d5d10bc3 | ths | sprintf(dev, "tap%d", ppa);
|
4468 | d5d10bc3 | ths | return tap_fd;
|
4469 | d5d10bc3 | ths | } |
4470 | d5d10bc3 | ths | |
4471 | ec530c81 | bellard | static int tap_open(char *ifname, int ifname_size) |
4472 | ec530c81 | bellard | { |
4473 | d5d10bc3 | ths | char dev[10]=""; |
4474 | d5d10bc3 | ths | int fd;
|
4475 | d5d10bc3 | ths | if( (fd = tap_alloc(dev)) < 0 ){ |
4476 | d5d10bc3 | ths | fprintf(stderr, "Cannot allocate TAP device\n");
|
4477 | d5d10bc3 | ths | return -1; |
4478 | d5d10bc3 | ths | } |
4479 | d5d10bc3 | ths | pstrcpy(ifname, ifname_size, dev); |
4480 | d5d10bc3 | ths | fcntl(fd, F_SETFL, O_NONBLOCK); |
4481 | d5d10bc3 | ths | return fd;
|
4482 | ec530c81 | bellard | } |
4483 | 7d3505c5 | bellard | #else
|
4484 | 7c9d8e07 | bellard | static int tap_open(char *ifname, int ifname_size) |
4485 | 330d0414 | bellard | { |
4486 | 80cabfad | bellard | struct ifreq ifr;
|
4487 | c4b1fcc0 | bellard | int fd, ret;
|
4488 | 3b46e624 | ths | |
4489 | aeb30be6 | balrog | TFR(fd = open("/dev/net/tun", O_RDWR));
|
4490 | 80cabfad | bellard | if (fd < 0) { |
4491 | 80cabfad | bellard | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
|
4492 | 80cabfad | bellard | return -1; |
4493 | 330d0414 | bellard | } |
4494 | 80cabfad | bellard | memset(&ifr, 0, sizeof(ifr)); |
4495 | 80cabfad | bellard | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
4496 | 7c9d8e07 | bellard | if (ifname[0] != '\0') |
4497 | 7c9d8e07 | bellard | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
4498 | 7c9d8e07 | bellard | else
|
4499 | 7c9d8e07 | bellard | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
|
4500 | 80cabfad | bellard | ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
|
4501 | 80cabfad | bellard | if (ret != 0) { |
4502 | 80cabfad | bellard | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
|
4503 | 80cabfad | bellard | close(fd); |
4504 | 80cabfad | bellard | return -1; |
4505 | 80cabfad | bellard | } |
4506 | c4b1fcc0 | bellard | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
4507 | 80cabfad | bellard | fcntl(fd, F_SETFL, O_NONBLOCK); |
4508 | c4b1fcc0 | bellard | return fd;
|
4509 | c4b1fcc0 | bellard | } |
4510 | 7d3505c5 | bellard | #endif
|
4511 | 330d0414 | bellard | |
4512 | b46a8906 | ths | static int launch_script(const char *setup_script, const char *ifname, int fd) |
4513 | 7c9d8e07 | bellard | { |
4514 | b46a8906 | ths | int pid, status;
|
4515 | 7c9d8e07 | bellard | char *args[3]; |
4516 | 7c9d8e07 | bellard | char **parg;
|
4517 | 7c9d8e07 | bellard | |
4518 | b46a8906 | ths | /* try to launch network script */
|
4519 | 7c9d8e07 | bellard | pid = fork(); |
4520 | 7c9d8e07 | bellard | if (pid >= 0) { |
4521 | 7c9d8e07 | bellard | if (pid == 0) { |
4522 | 50d3eeae | ths | int open_max = sysconf (_SC_OPEN_MAX), i;
|
4523 | 50d3eeae | ths | for (i = 0; i < open_max; i++) |
4524 | 50d3eeae | ths | if (i != STDIN_FILENO &&
|
4525 | 50d3eeae | ths | i != STDOUT_FILENO && |
4526 | 50d3eeae | ths | i != STDERR_FILENO && |
4527 | 50d3eeae | ths | i != fd) |
4528 | 50d3eeae | ths | close(i); |
4529 | 50d3eeae | ths | |
4530 | 7c9d8e07 | bellard | parg = args; |
4531 | 7c9d8e07 | bellard | *parg++ = (char *)setup_script;
|
4532 | b46a8906 | ths | *parg++ = (char *)ifname;
|
4533 | 7c9d8e07 | bellard | *parg++ = NULL;
|
4534 | 7c9d8e07 | bellard | execv(setup_script, args); |
4535 | 4a38940d | bellard | _exit(1);
|
4536 | 7c9d8e07 | bellard | } |
4537 | 7c9d8e07 | bellard | while (waitpid(pid, &status, 0) != pid); |
4538 | 7c9d8e07 | bellard | if (!WIFEXITED(status) ||
|
4539 | 7c9d8e07 | bellard | WEXITSTATUS(status) != 0) {
|
4540 | 7c9d8e07 | bellard | fprintf(stderr, "%s: could not launch network script\n",
|
4541 | 7c9d8e07 | bellard | setup_script); |
4542 | 7c9d8e07 | bellard | return -1; |
4543 | 7c9d8e07 | bellard | } |
4544 | 7c9d8e07 | bellard | } |
4545 | b46a8906 | ths | return 0; |
4546 | b46a8906 | ths | } |
4547 | b46a8906 | ths | |
4548 | b46a8906 | ths | static int net_tap_init(VLANState *vlan, const char *ifname1, |
4549 | b46a8906 | ths | const char *setup_script, const char *down_script) |
4550 | b46a8906 | ths | { |
4551 | b46a8906 | ths | TAPState *s; |
4552 | b46a8906 | ths | int fd;
|
4553 | b46a8906 | ths | char ifname[128]; |
4554 | b46a8906 | ths | |
4555 | b46a8906 | ths | if (ifname1 != NULL) |
4556 | b46a8906 | ths | pstrcpy(ifname, sizeof(ifname), ifname1);
|
4557 | b46a8906 | ths | else
|
4558 | b46a8906 | ths | ifname[0] = '\0'; |
4559 | b46a8906 | ths | TFR(fd = tap_open(ifname, sizeof(ifname)));
|
4560 | b46a8906 | ths | if (fd < 0) |
4561 | b46a8906 | ths | return -1; |
4562 | b46a8906 | ths | |
4563 | b46a8906 | ths | if (!setup_script || !strcmp(setup_script, "no")) |
4564 | b46a8906 | ths | setup_script = "";
|
4565 | b46a8906 | ths | if (setup_script[0] != '\0') { |
4566 | b46a8906 | ths | if (launch_script(setup_script, ifname, fd))
|
4567 | b46a8906 | ths | return -1; |
4568 | 7c9d8e07 | bellard | } |
4569 | 7c9d8e07 | bellard | s = net_tap_fd_init(vlan, fd); |
4570 | 7c9d8e07 | bellard | if (!s)
|
4571 | 7c9d8e07 | bellard | return -1; |
4572 | 5fafdf24 | ths | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4573 | 7c9d8e07 | bellard | "tap: ifname=%s setup_script=%s", ifname, setup_script);
|
4574 | b46a8906 | ths | if (down_script && strcmp(down_script, "no")) |
4575 | b46a8906 | ths | snprintf(s->down_script, sizeof(s->down_script), "%s", down_script); |
4576 | 7c9d8e07 | bellard | return 0; |
4577 | 7c9d8e07 | bellard | } |
4578 | 7c9d8e07 | bellard | |
4579 | fd1dff4b | bellard | #endif /* !_WIN32 */ |
4580 | fd1dff4b | bellard | |
4581 | 8a16d273 | ths | #if defined(CONFIG_VDE)
|
4582 | 8a16d273 | ths | typedef struct VDEState { |
4583 | 8a16d273 | ths | VLANClientState *vc; |
4584 | 8a16d273 | ths | VDECONN *vde; |
4585 | 8a16d273 | ths | } VDEState; |
4586 | 8a16d273 | ths | |
4587 | 8a16d273 | ths | static void vde_to_qemu(void *opaque) |
4588 | 8a16d273 | ths | { |
4589 | 8a16d273 | ths | VDEState *s = opaque; |
4590 | 8a16d273 | ths | uint8_t buf[4096];
|
4591 | 8a16d273 | ths | int size;
|
4592 | 8a16d273 | ths | |
4593 | 8a16d273 | ths | size = vde_recv(s->vde, buf, sizeof(buf), 0); |
4594 | 8a16d273 | ths | if (size > 0) { |
4595 | 8a16d273 | ths | qemu_send_packet(s->vc, buf, size); |
4596 | 8a16d273 | ths | } |
4597 | 8a16d273 | ths | } |
4598 | 8a16d273 | ths | |
4599 | 8a16d273 | ths | static void vde_from_qemu(void *opaque, const uint8_t *buf, int size) |
4600 | 8a16d273 | ths | { |
4601 | 8a16d273 | ths | VDEState *s = opaque; |
4602 | 8a16d273 | ths | int ret;
|
4603 | 8a16d273 | ths | for(;;) {
|
4604 | 8a16d273 | ths | ret = vde_send(s->vde, buf, size, 0);
|
4605 | 8a16d273 | ths | if (ret < 0 && errno == EINTR) { |
4606 | 8a16d273 | ths | } else {
|
4607 | 8a16d273 | ths | break;
|
4608 | 8a16d273 | ths | } |
4609 | 8a16d273 | ths | } |
4610 | 8a16d273 | ths | } |
4611 | 8a16d273 | ths | |
4612 | 8a16d273 | ths | static int net_vde_init(VLANState *vlan, const char *sock, int port, |
4613 | 8a16d273 | ths | const char *group, int mode) |
4614 | 8a16d273 | ths | { |
4615 | 8a16d273 | ths | VDEState *s; |
4616 | 8a16d273 | ths | char *init_group = strlen(group) ? (char *)group : NULL; |
4617 | 8a16d273 | ths | char *init_sock = strlen(sock) ? (char *)sock : NULL; |
4618 | 8a16d273 | ths | |
4619 | 8a16d273 | ths | struct vde_open_args args = {
|
4620 | 8a16d273 | ths | .port = port, |
4621 | 8a16d273 | ths | .group = init_group, |
4622 | 8a16d273 | ths | .mode = mode, |
4623 | 8a16d273 | ths | }; |
4624 | 8a16d273 | ths | |
4625 | 8a16d273 | ths | s = qemu_mallocz(sizeof(VDEState));
|
4626 | 8a16d273 | ths | if (!s)
|
4627 | 8a16d273 | ths | return -1; |
4628 | 8a16d273 | ths | s->vde = vde_open(init_sock, "QEMU", &args);
|
4629 | 8a16d273 | ths | if (!s->vde){
|
4630 | 8a16d273 | ths | free(s); |
4631 | 8a16d273 | ths | return -1; |
4632 | 8a16d273 | ths | } |
4633 | 8a16d273 | ths | s->vc = qemu_new_vlan_client(vlan, vde_from_qemu, NULL, s);
|
4634 | 8a16d273 | ths | qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
|
4635 | 8a16d273 | ths | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "vde: sock=%s fd=%d", |
4636 | 8a16d273 | ths | sock, vde_datafd(s->vde)); |
4637 | 8a16d273 | ths | return 0; |
4638 | 8a16d273 | ths | } |
4639 | 8a16d273 | ths | #endif
|
4640 | 8a16d273 | ths | |
4641 | 7c9d8e07 | bellard | /* network connection */
|
4642 | 7c9d8e07 | bellard | typedef struct NetSocketState { |
4643 | 7c9d8e07 | bellard | VLANClientState *vc; |
4644 | 7c9d8e07 | bellard | int fd;
|
4645 | 7c9d8e07 | bellard | int state; /* 0 = getting length, 1 = getting data */ |
4646 | 7c9d8e07 | bellard | int index;
|
4647 | 7c9d8e07 | bellard | int packet_len;
|
4648 | 7c9d8e07 | bellard | uint8_t buf[4096];
|
4649 | 3d830459 | bellard | struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
4650 | 7c9d8e07 | bellard | } NetSocketState; |
4651 | 7c9d8e07 | bellard | |
4652 | 7c9d8e07 | bellard | typedef struct NetSocketListenState { |
4653 | 7c9d8e07 | bellard | VLANState *vlan; |
4654 | 7c9d8e07 | bellard | int fd;
|
4655 | 7c9d8e07 | bellard | } NetSocketListenState; |
4656 | 7c9d8e07 | bellard | |
4657 | 7c9d8e07 | bellard | /* XXX: we consider we can send the whole packet without blocking */
|
4658 | 7c9d8e07 | bellard | static void net_socket_receive(void *opaque, const uint8_t *buf, int size) |
4659 | c20709aa | bellard | { |
4660 | 7c9d8e07 | bellard | NetSocketState *s = opaque; |
4661 | 7c9d8e07 | bellard | uint32_t len; |
4662 | 7c9d8e07 | bellard | len = htonl(size); |
4663 | 7c9d8e07 | bellard | |
4664 | fd1dff4b | bellard | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
4665 | fd1dff4b | bellard | send_all(s->fd, buf, size); |
4666 | c20709aa | bellard | } |
4667 | c20709aa | bellard | |
4668 | 3d830459 | bellard | static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size) |
4669 | 3d830459 | bellard | { |
4670 | 3d830459 | bellard | NetSocketState *s = opaque; |
4671 | 5fafdf24 | ths | sendto(s->fd, buf, size, 0,
|
4672 | 3d830459 | bellard | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
4673 | 3d830459 | bellard | } |
4674 | 3d830459 | bellard | |
4675 | 7c9d8e07 | bellard | static void net_socket_send(void *opaque) |
4676 | c4b1fcc0 | bellard | { |
4677 | 7c9d8e07 | bellard | NetSocketState *s = opaque; |
4678 | fd1dff4b | bellard | int l, size, err;
|
4679 | 7c9d8e07 | bellard | uint8_t buf1[4096];
|
4680 | 7c9d8e07 | bellard | const uint8_t *buf;
|
4681 | 7c9d8e07 | bellard | |
4682 | fd1dff4b | bellard | size = recv(s->fd, buf1, sizeof(buf1), 0); |
4683 | fd1dff4b | bellard | if (size < 0) { |
4684 | fd1dff4b | bellard | err = socket_error(); |
4685 | 5fafdf24 | ths | if (err != EWOULDBLOCK)
|
4686 | fd1dff4b | bellard | goto eoc;
|
4687 | fd1dff4b | bellard | } else if (size == 0) { |
4688 | 7c9d8e07 | bellard | /* end of connection */
|
4689 | fd1dff4b | bellard | eoc:
|
4690 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
4691 | fd1dff4b | bellard | closesocket(s->fd); |
4692 | 7c9d8e07 | bellard | return;
|
4693 | 7c9d8e07 | bellard | } |
4694 | 7c9d8e07 | bellard | buf = buf1; |
4695 | 7c9d8e07 | bellard | while (size > 0) { |
4696 | 7c9d8e07 | bellard | /* reassemble a packet from the network */
|
4697 | 7c9d8e07 | bellard | switch(s->state) {
|
4698 | 7c9d8e07 | bellard | case 0: |
4699 | 7c9d8e07 | bellard | l = 4 - s->index;
|
4700 | 7c9d8e07 | bellard | if (l > size)
|
4701 | 7c9d8e07 | bellard | l = size; |
4702 | 7c9d8e07 | bellard | memcpy(s->buf + s->index, buf, l); |
4703 | 7c9d8e07 | bellard | buf += l; |
4704 | 7c9d8e07 | bellard | size -= l; |
4705 | 7c9d8e07 | bellard | s->index += l; |
4706 | 7c9d8e07 | bellard | if (s->index == 4) { |
4707 | 7c9d8e07 | bellard | /* got length */
|
4708 | 7c9d8e07 | bellard | s->packet_len = ntohl(*(uint32_t *)s->buf); |
4709 | 7c9d8e07 | bellard | s->index = 0;
|
4710 | 7c9d8e07 | bellard | s->state = 1;
|
4711 | 7c9d8e07 | bellard | } |
4712 | 7c9d8e07 | bellard | break;
|
4713 | 7c9d8e07 | bellard | case 1: |
4714 | 7c9d8e07 | bellard | l = s->packet_len - s->index; |
4715 | 7c9d8e07 | bellard | if (l > size)
|
4716 | 7c9d8e07 | bellard | l = size; |
4717 | 7c9d8e07 | bellard | memcpy(s->buf + s->index, buf, l); |
4718 | 7c9d8e07 | bellard | s->index += l; |
4719 | 7c9d8e07 | bellard | buf += l; |
4720 | 7c9d8e07 | bellard | size -= l; |
4721 | 7c9d8e07 | bellard | if (s->index >= s->packet_len) {
|
4722 | 7c9d8e07 | bellard | qemu_send_packet(s->vc, s->buf, s->packet_len); |
4723 | 7c9d8e07 | bellard | s->index = 0;
|
4724 | 7c9d8e07 | bellard | s->state = 0;
|
4725 | 7c9d8e07 | bellard | } |
4726 | 7c9d8e07 | bellard | break;
|
4727 | 7c9d8e07 | bellard | } |
4728 | 7c9d8e07 | bellard | } |
4729 | c20709aa | bellard | } |
4730 | c20709aa | bellard | |
4731 | 3d830459 | bellard | static void net_socket_send_dgram(void *opaque) |
4732 | 3d830459 | bellard | { |
4733 | 3d830459 | bellard | NetSocketState *s = opaque; |
4734 | 3d830459 | bellard | int size;
|
4735 | 3d830459 | bellard | |
4736 | 3d830459 | bellard | size = recv(s->fd, s->buf, sizeof(s->buf), 0); |
4737 | 5fafdf24 | ths | if (size < 0) |
4738 | 3d830459 | bellard | return;
|
4739 | 3d830459 | bellard | if (size == 0) { |
4740 | 3d830459 | bellard | /* end of connection */
|
4741 | 3d830459 | bellard | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
4742 | 3d830459 | bellard | return;
|
4743 | 3d830459 | bellard | } |
4744 | 3d830459 | bellard | qemu_send_packet(s->vc, s->buf, size); |
4745 | 3d830459 | bellard | } |
4746 | 3d830459 | bellard | |
4747 | 3d830459 | bellard | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
4748 | 3d830459 | bellard | { |
4749 | 3d830459 | bellard | struct ip_mreq imr;
|
4750 | 3d830459 | bellard | int fd;
|
4751 | 3d830459 | bellard | int val, ret;
|
4752 | 3d830459 | bellard | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
|
4753 | 3d830459 | bellard | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
|
4754 | 5fafdf24 | ths | inet_ntoa(mcastaddr->sin_addr), |
4755 | fd1dff4b | bellard | (int)ntohl(mcastaddr->sin_addr.s_addr));
|
4756 | 3d830459 | bellard | return -1; |
4757 | 3d830459 | bellard | |
4758 | 3d830459 | bellard | } |
4759 | 3d830459 | bellard | fd = socket(PF_INET, SOCK_DGRAM, 0);
|
4760 | 3d830459 | bellard | if (fd < 0) { |
4761 | 3d830459 | bellard | perror("socket(PF_INET, SOCK_DGRAM)");
|
4762 | 3d830459 | bellard | return -1; |
4763 | 3d830459 | bellard | } |
4764 | 3d830459 | bellard | |
4765 | fd1dff4b | bellard | val = 1;
|
4766 | 5fafdf24 | ths | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
4767 | fd1dff4b | bellard | (const char *)&val, sizeof(val)); |
4768 | fd1dff4b | bellard | if (ret < 0) { |
4769 | fd1dff4b | bellard | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
|
4770 | fd1dff4b | bellard | goto fail;
|
4771 | fd1dff4b | bellard | } |
4772 | fd1dff4b | bellard | |
4773 | fd1dff4b | bellard | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
4774 | fd1dff4b | bellard | if (ret < 0) { |
4775 | fd1dff4b | bellard | perror("bind");
|
4776 | fd1dff4b | bellard | goto fail;
|
4777 | fd1dff4b | bellard | } |
4778 | 3b46e624 | ths | |
4779 | 3d830459 | bellard | /* Add host to multicast group */
|
4780 | 3d830459 | bellard | imr.imr_multiaddr = mcastaddr->sin_addr; |
4781 | 3d830459 | bellard | imr.imr_interface.s_addr = htonl(INADDR_ANY); |
4782 | 3d830459 | bellard | |
4783 | 5fafdf24 | ths | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
4784 | fd1dff4b | bellard | (const char *)&imr, sizeof(struct ip_mreq)); |
4785 | 3d830459 | bellard | if (ret < 0) { |
4786 | 3d830459 | bellard | perror("setsockopt(IP_ADD_MEMBERSHIP)");
|
4787 | 3d830459 | bellard | goto fail;
|
4788 | 3d830459 | bellard | } |
4789 | 3d830459 | bellard | |
4790 | 3d830459 | bellard | /* Force mcast msgs to loopback (eg. several QEMUs in same host */
|
4791 | 3d830459 | bellard | val = 1;
|
4792 | 5fafdf24 | ths | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
4793 | fd1dff4b | bellard | (const char *)&val, sizeof(val)); |
4794 | 3d830459 | bellard | if (ret < 0) { |
4795 | 3d830459 | bellard | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
|
4796 | 3d830459 | bellard | goto fail;
|
4797 | 3d830459 | bellard | } |
4798 | 3d830459 | bellard | |
4799 | fd1dff4b | bellard | socket_set_nonblock(fd); |
4800 | 3d830459 | bellard | return fd;
|
4801 | 3d830459 | bellard | fail:
|
4802 | 5fafdf24 | ths | if (fd >= 0) |
4803 | 0bab00f3 | bellard | closesocket(fd); |
4804 | 3d830459 | bellard | return -1; |
4805 | 3d830459 | bellard | } |
4806 | 3d830459 | bellard | |
4807 | 5fafdf24 | ths | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, |
4808 | 3d830459 | bellard | int is_connected)
|
4809 | 3d830459 | bellard | { |
4810 | 3d830459 | bellard | struct sockaddr_in saddr;
|
4811 | 3d830459 | bellard | int newfd;
|
4812 | 3d830459 | bellard | socklen_t saddr_len; |
4813 | 3d830459 | bellard | NetSocketState *s; |
4814 | 3d830459 | bellard | |
4815 | 3d830459 | bellard | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
|
4816 | 5fafdf24 | ths | * Because this may be "shared" socket from a "master" process, datagrams would be recv()
|
4817 | 3d830459 | bellard | * by ONLY ONE process: we must "clone" this dgram socket --jjo
|
4818 | 3d830459 | bellard | */
|
4819 | 3d830459 | bellard | |
4820 | 3d830459 | bellard | if (is_connected) {
|
4821 | 3d830459 | bellard | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
4822 | 3d830459 | bellard | /* must be bound */
|
4823 | 3d830459 | bellard | if (saddr.sin_addr.s_addr==0) { |
4824 | 3d830459 | bellard | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
|
4825 | 3d830459 | bellard | fd); |
4826 | 3d830459 | bellard | return NULL; |
4827 | 3d830459 | bellard | } |
4828 | 3d830459 | bellard | /* clone dgram socket */
|
4829 | 3d830459 | bellard | newfd = net_socket_mcast_create(&saddr); |
4830 | 3d830459 | bellard | if (newfd < 0) { |
4831 | 3d830459 | bellard | /* error already reported by net_socket_mcast_create() */
|
4832 | 3d830459 | bellard | close(fd); |
4833 | 3d830459 | bellard | return NULL; |
4834 | 3d830459 | bellard | } |
4835 | 3d830459 | bellard | /* clone newfd to fd, close newfd */
|
4836 | 3d830459 | bellard | dup2(newfd, fd); |
4837 | 3d830459 | bellard | close(newfd); |
4838 | 5fafdf24 | ths | |
4839 | 3d830459 | bellard | } else {
|
4840 | 3d830459 | bellard | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
|
4841 | 3d830459 | bellard | fd, strerror(errno)); |
4842 | 3d830459 | bellard | return NULL; |
4843 | 3d830459 | bellard | } |
4844 | 3d830459 | bellard | } |
4845 | 3d830459 | bellard | |
4846 | 3d830459 | bellard | s = qemu_mallocz(sizeof(NetSocketState));
|
4847 | 3d830459 | bellard | if (!s)
|
4848 | 3d830459 | bellard | return NULL; |
4849 | 3d830459 | bellard | s->fd = fd; |
4850 | 3d830459 | bellard | |
4851 | d861b05e | pbrook | s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
|
4852 | 3d830459 | bellard | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
|
4853 | 3d830459 | bellard | |
4854 | 3d830459 | bellard | /* mcast: save bound address as dst */
|
4855 | 3d830459 | bellard | if (is_connected) s->dgram_dst=saddr;
|
4856 | 3d830459 | bellard | |
4857 | 3d830459 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4858 | 5fafdf24 | ths | "socket: fd=%d (%s mcast=%s:%d)",
|
4859 | 3d830459 | bellard | fd, is_connected? "cloned" : "", |
4860 | 3d830459 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4861 | 3d830459 | bellard | return s;
|
4862 | 3d830459 | bellard | } |
4863 | 3d830459 | bellard | |
4864 | 7c9d8e07 | bellard | static void net_socket_connect(void *opaque) |
4865 | c20709aa | bellard | { |
4866 | 7c9d8e07 | bellard | NetSocketState *s = opaque; |
4867 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
|
4868 | 7c9d8e07 | bellard | } |
4869 | c4b1fcc0 | bellard | |
4870 | 5fafdf24 | ths | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, |
4871 | 7c9d8e07 | bellard | int is_connected)
|
4872 | 7c9d8e07 | bellard | { |
4873 | 7c9d8e07 | bellard | NetSocketState *s; |
4874 | 7c9d8e07 | bellard | s = qemu_mallocz(sizeof(NetSocketState));
|
4875 | 7c9d8e07 | bellard | if (!s)
|
4876 | 7c9d8e07 | bellard | return NULL; |
4877 | 7c9d8e07 | bellard | s->fd = fd; |
4878 | 5fafdf24 | ths | s->vc = qemu_new_vlan_client(vlan, |
4879 | d861b05e | pbrook | net_socket_receive, NULL, s);
|
4880 | 7c9d8e07 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4881 | 7c9d8e07 | bellard | "socket: fd=%d", fd);
|
4882 | 7c9d8e07 | bellard | if (is_connected) {
|
4883 | 7c9d8e07 | bellard | net_socket_connect(s); |
4884 | 7c9d8e07 | bellard | } else {
|
4885 | 7c9d8e07 | bellard | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
|
4886 | 7c9d8e07 | bellard | } |
4887 | 7c9d8e07 | bellard | return s;
|
4888 | 7c9d8e07 | bellard | } |
4889 | c4b1fcc0 | bellard | |
4890 | 5fafdf24 | ths | static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, |
4891 | 3d830459 | bellard | int is_connected)
|
4892 | 3d830459 | bellard | { |
4893 | 3d830459 | bellard | int so_type=-1, optlen=sizeof(so_type); |
4894 | 3d830459 | bellard | |
4895 | 69b34976 | ths | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, |
4896 | 69b34976 | ths | (socklen_t *)&optlen)< 0) {
|
4897 | 931f03ee | ths | fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
|
4898 | 3d830459 | bellard | return NULL; |
4899 | 3d830459 | bellard | } |
4900 | 3d830459 | bellard | switch(so_type) {
|
4901 | 3d830459 | bellard | case SOCK_DGRAM:
|
4902 | 3d830459 | bellard | return net_socket_fd_init_dgram(vlan, fd, is_connected);
|
4903 | 3d830459 | bellard | case SOCK_STREAM:
|
4904 | 3d830459 | bellard | return net_socket_fd_init_stream(vlan, fd, is_connected);
|
4905 | 3d830459 | bellard | default:
|
4906 | 3d830459 | bellard | /* who knows ... this could be a eg. a pty, do warn and continue as stream */
|
4907 | 3d830459 | bellard | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
|
4908 | 3d830459 | bellard | return net_socket_fd_init_stream(vlan, fd, is_connected);
|
4909 | 3d830459 | bellard | } |
4910 | 3d830459 | bellard | return NULL; |
4911 | 3d830459 | bellard | } |
4912 | 3d830459 | bellard | |
4913 | 7c9d8e07 | bellard | static void net_socket_accept(void *opaque) |
4914 | 7c9d8e07 | bellard | { |
4915 | 3b46e624 | ths | NetSocketListenState *s = opaque; |
4916 | 7c9d8e07 | bellard | NetSocketState *s1; |
4917 | 7c9d8e07 | bellard | struct sockaddr_in saddr;
|
4918 | 7c9d8e07 | bellard | socklen_t len; |
4919 | 7c9d8e07 | bellard | int fd;
|
4920 | 7c9d8e07 | bellard | |
4921 | 7c9d8e07 | bellard | for(;;) {
|
4922 | 7c9d8e07 | bellard | len = sizeof(saddr);
|
4923 | 7c9d8e07 | bellard | fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
|
4924 | 7c9d8e07 | bellard | if (fd < 0 && errno != EINTR) { |
4925 | 7c9d8e07 | bellard | return;
|
4926 | 7c9d8e07 | bellard | } else if (fd >= 0) { |
4927 | 7c9d8e07 | bellard | break;
|
4928 | 80cabfad | bellard | } |
4929 | 330d0414 | bellard | } |
4930 | 5fafdf24 | ths | s1 = net_socket_fd_init(s->vlan, fd, 1);
|
4931 | 7c9d8e07 | bellard | if (!s1) {
|
4932 | 0bab00f3 | bellard | closesocket(fd); |
4933 | 7c9d8e07 | bellard | } else {
|
4934 | 7c9d8e07 | bellard | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
|
4935 | 5fafdf24 | ths | "socket: connection from %s:%d",
|
4936 | 7c9d8e07 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4937 | 7c9d8e07 | bellard | } |
4938 | 7c9d8e07 | bellard | } |
4939 | 7c9d8e07 | bellard | |
4940 | 7c9d8e07 | bellard | static int net_socket_listen_init(VLANState *vlan, const char *host_str) |
4941 | 7c9d8e07 | bellard | { |
4942 | 7c9d8e07 | bellard | NetSocketListenState *s; |
4943 | 7c9d8e07 | bellard | int fd, val, ret;
|
4944 | 7c9d8e07 | bellard | struct sockaddr_in saddr;
|
4945 | 7c9d8e07 | bellard | |
4946 | 7c9d8e07 | bellard | if (parse_host_port(&saddr, host_str) < 0) |
4947 | 7c9d8e07 | bellard | return -1; |
4948 | 3b46e624 | ths | |
4949 | 7c9d8e07 | bellard | s = qemu_mallocz(sizeof(NetSocketListenState));
|
4950 | 7c9d8e07 | bellard | if (!s)
|
4951 | 7c9d8e07 | bellard | return -1; |
4952 | 7c9d8e07 | bellard | |
4953 | 7c9d8e07 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
4954 | 7c9d8e07 | bellard | if (fd < 0) { |
4955 | 7c9d8e07 | bellard | perror("socket");
|
4956 | 7c9d8e07 | bellard | return -1; |
4957 | 7c9d8e07 | bellard | } |
4958 | fd1dff4b | bellard | socket_set_nonblock(fd); |
4959 | 7c9d8e07 | bellard | |
4960 | 7c9d8e07 | bellard | /* allow fast reuse */
|
4961 | 7c9d8e07 | bellard | val = 1;
|
4962 | fd1dff4b | bellard | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
4963 | 3b46e624 | ths | |
4964 | 7c9d8e07 | bellard | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
4965 | 7c9d8e07 | bellard | if (ret < 0) { |
4966 | 7c9d8e07 | bellard | perror("bind");
|
4967 | 7c9d8e07 | bellard | return -1; |
4968 | 7c9d8e07 | bellard | } |
4969 | 7c9d8e07 | bellard | ret = listen(fd, 0);
|
4970 | 7c9d8e07 | bellard | if (ret < 0) { |
4971 | 7c9d8e07 | bellard | perror("listen");
|
4972 | 7c9d8e07 | bellard | return -1; |
4973 | 7c9d8e07 | bellard | } |
4974 | 7c9d8e07 | bellard | s->vlan = vlan; |
4975 | 7c9d8e07 | bellard | s->fd = fd; |
4976 | 7c9d8e07 | bellard | qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
|
4977 | 80cabfad | bellard | return 0; |
4978 | 330d0414 | bellard | } |
4979 | 330d0414 | bellard | |
4980 | 7c9d8e07 | bellard | static int net_socket_connect_init(VLANState *vlan, const char *host_str) |
4981 | 330d0414 | bellard | { |
4982 | 7c9d8e07 | bellard | NetSocketState *s; |
4983 | fd1dff4b | bellard | int fd, connected, ret, err;
|
4984 | 7c9d8e07 | bellard | struct sockaddr_in saddr;
|
4985 | 7c9d8e07 | bellard | |
4986 | 7c9d8e07 | bellard | if (parse_host_port(&saddr, host_str) < 0) |
4987 | 7c9d8e07 | bellard | return -1; |
4988 | 7c9d8e07 | bellard | |
4989 | 7c9d8e07 | bellard | fd = socket(PF_INET, SOCK_STREAM, 0);
|
4990 | 7c9d8e07 | bellard | if (fd < 0) { |
4991 | 7c9d8e07 | bellard | perror("socket");
|
4992 | 7c9d8e07 | bellard | return -1; |
4993 | 7c9d8e07 | bellard | } |
4994 | fd1dff4b | bellard | socket_set_nonblock(fd); |
4995 | 7c9d8e07 | bellard | |
4996 | 7c9d8e07 | bellard | connected = 0;
|
4997 | 7c9d8e07 | bellard | for(;;) {
|
4998 | 7c9d8e07 | bellard | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
4999 | 7c9d8e07 | bellard | if (ret < 0) { |
5000 | fd1dff4b | bellard | err = socket_error(); |
5001 | fd1dff4b | bellard | if (err == EINTR || err == EWOULDBLOCK) {
|
5002 | fd1dff4b | bellard | } else if (err == EINPROGRESS) { |
5003 | 7c9d8e07 | bellard | break;
|
5004 | f5b12268 | ths | #ifdef _WIN32
|
5005 | f5b12268 | ths | } else if (err == WSAEALREADY) { |
5006 | f5b12268 | ths | break;
|
5007 | f5b12268 | ths | #endif
|
5008 | 7c9d8e07 | bellard | } else {
|
5009 | 7c9d8e07 | bellard | perror("connect");
|
5010 | fd1dff4b | bellard | closesocket(fd); |
5011 | 7c9d8e07 | bellard | return -1; |
5012 | 7c9d8e07 | bellard | } |
5013 | 7c9d8e07 | bellard | } else {
|
5014 | 7c9d8e07 | bellard | connected = 1;
|
5015 | 7c9d8e07 | bellard | break;
|
5016 | 7c9d8e07 | bellard | } |
5017 | 7c9d8e07 | bellard | } |
5018 | 7c9d8e07 | bellard | s = net_socket_fd_init(vlan, fd, connected); |
5019 | 7c9d8e07 | bellard | if (!s)
|
5020 | 7c9d8e07 | bellard | return -1; |
5021 | 7c9d8e07 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
5022 | 5fafdf24 | ths | "socket: connect to %s:%d",
|
5023 | 7c9d8e07 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
5024 | c20709aa | bellard | return 0; |
5025 | 80cabfad | bellard | } |
5026 | 330d0414 | bellard | |
5027 | 3d830459 | bellard | static int net_socket_mcast_init(VLANState *vlan, const char *host_str) |
5028 | 3d830459 | bellard | { |
5029 | 3d830459 | bellard | NetSocketState *s; |
5030 | 3d830459 | bellard | int fd;
|
5031 | 3d830459 | bellard | struct sockaddr_in saddr;
|
5032 | 3d830459 | bellard | |
5033 | 3d830459 | bellard | if (parse_host_port(&saddr, host_str) < 0) |
5034 | 3d830459 | bellard | return -1; |
5035 | 3d830459 | bellard | |
5036 | 3d830459 | bellard | |
5037 | 3d830459 | bellard | fd = net_socket_mcast_create(&saddr); |
5038 | 3d830459 | bellard | if (fd < 0) |
5039 | 3d830459 | bellard | return -1; |
5040 | 3d830459 | bellard | |
5041 | 3d830459 | bellard | s = net_socket_fd_init(vlan, fd, 0);
|
5042 | 3d830459 | bellard | if (!s)
|
5043 | 3d830459 | bellard | return -1; |
5044 | 3d830459 | bellard | |
5045 | 3d830459 | bellard | s->dgram_dst = saddr; |
5046 | 3b46e624 | ths | |
5047 | 3d830459 | bellard | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
5048 | 5fafdf24 | ths | "socket: mcast=%s:%d",
|
5049 | 3d830459 | bellard | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
5050 | 3d830459 | bellard | return 0; |
5051 | 3d830459 | bellard | |
5052 | 3d830459 | bellard | } |
5053 | 3d830459 | bellard | |
5054 | 609497ab | balrog | static const char *get_opt_name(char *buf, int buf_size, const char *p) |
5055 | 609497ab | balrog | { |
5056 | 609497ab | balrog | char *q;
|
5057 | 609497ab | balrog | |
5058 | 609497ab | balrog | q = buf; |
5059 | 609497ab | balrog | while (*p != '\0' && *p != '=') { |
5060 | 609497ab | balrog | if (q && (q - buf) < buf_size - 1) |
5061 | 609497ab | balrog | *q++ = *p; |
5062 | 609497ab | balrog | p++; |
5063 | 609497ab | balrog | } |
5064 | 609497ab | balrog | if (q)
|
5065 | 609497ab | balrog | *q = '\0';
|
5066 | 609497ab | balrog | |
5067 | 609497ab | balrog | return p;
|
5068 | 609497ab | balrog | } |
5069 | 609497ab | balrog | |
5070 | 609497ab | balrog | static const char *get_opt_value(char *buf, int buf_size, const char *p) |
5071 | e4bcb14c | ths | { |
5072 | e4bcb14c | ths | char *q;
|
5073 | e4bcb14c | ths | |
5074 | e4bcb14c | ths | q = buf; |
5075 | e4bcb14c | ths | while (*p != '\0') { |
5076 | 609497ab | balrog | if (*p == ',') { |
5077 | 609497ab | balrog | if (*(p + 1) != ',') |
5078 | e4bcb14c | ths | break;
|
5079 | e4bcb14c | ths | p++; |
5080 | 609497ab | balrog | } |
5081 | e4bcb14c | ths | if (q && (q - buf) < buf_size - 1) |
5082 | e4bcb14c | ths | *q++ = *p; |
5083 | e4bcb14c | ths | p++; |
5084 | e4bcb14c | ths | } |
5085 | e4bcb14c | ths | if (q)
|
5086 | e4bcb14c | ths | *q = '\0';
|
5087 | e4bcb14c | ths | |
5088 | e4bcb14c | ths | return p;
|
5089 | e4bcb14c | ths | } |
5090 | e4bcb14c | ths | |
5091 | 7c9d8e07 | bellard | static int get_param_value(char *buf, int buf_size, |
5092 | 7c9d8e07 | bellard | const char *tag, const char *str) |
5093 | 7c9d8e07 | bellard | { |
5094 | 7c9d8e07 | bellard | const char *p; |
5095 | 7c9d8e07 | bellard | char option[128]; |
5096 | 7c9d8e07 | bellard | |
5097 | 7c9d8e07 | bellard | p = str; |
5098 | 7c9d8e07 | bellard | for(;;) {
|
5099 | 609497ab | balrog | p = get_opt_name(option, sizeof(option), p);
|
5100 | 7c9d8e07 | bellard | if (*p != '=') |
5101 | 7c9d8e07 | bellard | break;
|
5102 | 7c9d8e07 | bellard | p++; |
5103 | 7c9d8e07 | bellard | if (!strcmp(tag, option)) {
|
5104 | 609497ab | balrog | (void)get_opt_value(buf, buf_size, p);
|
5105 | e4bcb14c | ths | return strlen(buf);
|
5106 | 7c9d8e07 | bellard | } else {
|
5107 | 609497ab | balrog | p = get_opt_value(NULL, 0, p); |
5108 | 7c9d8e07 | bellard | } |
5109 | 7c9d8e07 | bellard | if (*p != ',') |
5110 | 7c9d8e07 | bellard | break;
|
5111 | 7c9d8e07 | bellard | p++; |
5112 | 7c9d8e07 | bellard | } |
5113 | 7c9d8e07 | bellard | return 0; |
5114 | 7c9d8e07 | bellard | } |
5115 | 7c9d8e07 | bellard | |
5116 | e4bcb14c | ths | static int check_params(char *buf, int buf_size, |
5117 | e4bcb14c | ths | char **params, const char *str) |
5118 | e4bcb14c | ths | { |
5119 | e4bcb14c | ths | const char *p; |
5120 | e4bcb14c | ths | int i;
|
5121 | e4bcb14c | ths | |
5122 | e4bcb14c | ths | p = str; |
5123 | e4bcb14c | ths | for(;;) {
|
5124 | 609497ab | balrog | p = get_opt_name(buf, buf_size, p); |
5125 | e4bcb14c | ths | if (*p != '=') |
5126 | e4bcb14c | ths | return -1; |
5127 | e4bcb14c | ths | p++; |
5128 | e4bcb14c | ths | for(i = 0; params[i] != NULL; i++) |
5129 | e4bcb14c | ths | if (!strcmp(params[i], buf))
|
5130 | e4bcb14c | ths | break;
|
5131 | e4bcb14c | ths | if (params[i] == NULL) |
5132 | e4bcb14c | ths | return -1; |
5133 | 609497ab | balrog | p = get_opt_value(NULL, 0, p); |
5134 | e4bcb14c | ths | if (*p != ',') |
5135 | e4bcb14c | ths | break;
|
5136 | e4bcb14c | ths | p++; |
5137 | e4bcb14c | ths | } |
5138 | e4bcb14c | ths | return 0; |
5139 | e4bcb14c | ths | } |
5140 | e4bcb14c | ths | |
5141 | 9ad97e65 | balrog | static int net_client_init(const char *device, const char *p) |
5142 | 7c9d8e07 | bellard | { |
5143 | 7c9d8e07 | bellard | char buf[1024]; |
5144 | 7c9d8e07 | bellard | int vlan_id, ret;
|
5145 | 7c9d8e07 | bellard | VLANState *vlan; |
5146 | 7c9d8e07 | bellard | |
5147 | 7c9d8e07 | bellard | vlan_id = 0;
|
5148 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "vlan", p)) { |
5149 | 7c9d8e07 | bellard | vlan_id = strtol(buf, NULL, 0); |
5150 | 7c9d8e07 | bellard | } |
5151 | 7c9d8e07 | bellard | vlan = qemu_find_vlan(vlan_id); |
5152 | 7c9d8e07 | bellard | if (!vlan) {
|
5153 | 7c9d8e07 | bellard | fprintf(stderr, "Could not create vlan %d\n", vlan_id);
|
5154 | 7c9d8e07 | bellard | return -1; |
5155 | 7c9d8e07 | bellard | } |
5156 | 7c9d8e07 | bellard | if (!strcmp(device, "nic")) { |
5157 | 7c9d8e07 | bellard | NICInfo *nd; |
5158 | 7c9d8e07 | bellard | uint8_t *macaddr; |
5159 | 7c9d8e07 | bellard | |
5160 | 7c9d8e07 | bellard | if (nb_nics >= MAX_NICS) {
|
5161 | 7c9d8e07 | bellard | fprintf(stderr, "Too Many NICs\n");
|
5162 | 7c9d8e07 | bellard | return -1; |
5163 | 7c9d8e07 | bellard | } |
5164 | 7c9d8e07 | bellard | nd = &nd_table[nb_nics]; |
5165 | 7c9d8e07 | bellard | macaddr = nd->macaddr; |
5166 | 7c9d8e07 | bellard | macaddr[0] = 0x52; |
5167 | 7c9d8e07 | bellard | macaddr[1] = 0x54; |
5168 | 7c9d8e07 | bellard | macaddr[2] = 0x00; |
5169 | 7c9d8e07 | bellard | macaddr[3] = 0x12; |
5170 | 7c9d8e07 | bellard | macaddr[4] = 0x34; |
5171 | 7c9d8e07 | bellard | macaddr[5] = 0x56 + nb_nics; |
5172 | 7c9d8e07 | bellard | |
5173 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "macaddr", p)) { |
5174 | 7c9d8e07 | bellard | if (parse_macaddr(macaddr, buf) < 0) { |
5175 | 7c9d8e07 | bellard | fprintf(stderr, "invalid syntax for ethernet address\n");
|
5176 | 7c9d8e07 | bellard | return -1; |
5177 | 7c9d8e07 | bellard | } |
5178 | 7c9d8e07 | bellard | } |
5179 | a41b2ff2 | pbrook | if (get_param_value(buf, sizeof(buf), "model", p)) { |
5180 | a41b2ff2 | pbrook | nd->model = strdup(buf); |
5181 | a41b2ff2 | pbrook | } |
5182 | 7c9d8e07 | bellard | nd->vlan = vlan; |
5183 | 7c9d8e07 | bellard | nb_nics++; |
5184 | 833c7174 | blueswir1 | vlan->nb_guest_devs++; |
5185 | 7c9d8e07 | bellard | ret = 0;
|
5186 | 7c9d8e07 | bellard | } else
|
5187 | 7c9d8e07 | bellard | if (!strcmp(device, "none")) { |
5188 | 7c9d8e07 | bellard | /* does nothing. It is needed to signal that no network cards
|
5189 | 7c9d8e07 | bellard | are wanted */
|
5190 | 7c9d8e07 | bellard | ret = 0;
|
5191 | 7c9d8e07 | bellard | } else
|
5192 | 7c9d8e07 | bellard | #ifdef CONFIG_SLIRP
|
5193 | 7c9d8e07 | bellard | if (!strcmp(device, "user")) { |
5194 | 115defd1 | pbrook | if (get_param_value(buf, sizeof(buf), "hostname", p)) { |
5195 | 3f423c9c | bellard | pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
|
5196 | 115defd1 | pbrook | } |
5197 | 833c7174 | blueswir1 | vlan->nb_host_devs++; |
5198 | 7c9d8e07 | bellard | ret = net_slirp_init(vlan); |
5199 | 7c9d8e07 | bellard | } else
|
5200 | 7c9d8e07 | bellard | #endif
|
5201 | 7fb843f8 | bellard | #ifdef _WIN32
|
5202 | 7fb843f8 | bellard | if (!strcmp(device, "tap")) { |
5203 | 7fb843f8 | bellard | char ifname[64]; |
5204 | 7fb843f8 | bellard | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
5205 | 7fb843f8 | bellard | fprintf(stderr, "tap: no interface name\n");
|
5206 | 7fb843f8 | bellard | return -1; |
5207 | 7fb843f8 | bellard | } |
5208 | 833c7174 | blueswir1 | vlan->nb_host_devs++; |
5209 | 7fb843f8 | bellard | ret = tap_win32_init(vlan, ifname); |
5210 | 7fb843f8 | bellard | } else
|
5211 | 7fb843f8 | bellard | #else
|
5212 | 7c9d8e07 | bellard | if (!strcmp(device, "tap")) { |
5213 | 7c9d8e07 | bellard | char ifname[64]; |
5214 | b46a8906 | ths | char setup_script[1024], down_script[1024]; |
5215 | 7c9d8e07 | bellard | int fd;
|
5216 | 4f01035f | pbrook | vlan->nb_host_devs++; |
5217 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
5218 | 7c9d8e07 | bellard | fd = strtol(buf, NULL, 0); |
5219 | 64538cdf | pbrook | fcntl(fd, F_SETFL, O_NONBLOCK); |
5220 | 7c9d8e07 | bellard | ret = -1;
|
5221 | 7c9d8e07 | bellard | if (net_tap_fd_init(vlan, fd))
|
5222 | 7c9d8e07 | bellard | ret = 0;
|
5223 | 7c9d8e07 | bellard | } else {
|
5224 | bf8c5342 | bellard | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
5225 | bf8c5342 | bellard | ifname[0] = '\0'; |
5226 | bf8c5342 | bellard | } |
5227 | 7c9d8e07 | bellard | if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) { |
5228 | 7c9d8e07 | bellard | pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
|
5229 | 7c9d8e07 | bellard | } |
5230 | b46a8906 | ths | if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) { |
5231 | b46a8906 | ths | pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
|
5232 | b46a8906 | ths | } |
5233 | b46a8906 | ths | ret = net_tap_init(vlan, ifname, setup_script, down_script); |
5234 | 7c9d8e07 | bellard | } |
5235 | 7c9d8e07 | bellard | } else
|
5236 | fd1dff4b | bellard | #endif
|
5237 | 7c9d8e07 | bellard | if (!strcmp(device, "socket")) { |
5238 | 7c9d8e07 | bellard | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
5239 | 7c9d8e07 | bellard | int fd;
|
5240 | 7c9d8e07 | bellard | fd = strtol(buf, NULL, 0); |
5241 | 7c9d8e07 | bellard | ret = -1;
|
5242 | 7c9d8e07 | bellard | if (net_socket_fd_init(vlan, fd, 1)) |
5243 | 7c9d8e07 | bellard | ret = 0;
|
5244 | 7c9d8e07 | bellard | } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) { |
5245 | 7c9d8e07 | bellard | ret = net_socket_listen_init(vlan, buf); |
5246 | 7c9d8e07 | bellard | } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { |
5247 | 7c9d8e07 | bellard | ret = net_socket_connect_init(vlan, buf); |
5248 | 3d830459 | bellard | } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { |
5249 | 3d830459 | bellard | ret = net_socket_mcast_init(vlan, buf); |
5250 | 7c9d8e07 | bellard | } else {
|
5251 | 7c9d8e07 | bellard | fprintf(stderr, "Unknown socket options: %s\n", p);
|
5252 | 7c9d8e07 | bellard | return -1; |
5253 | 7c9d8e07 | bellard | } |
5254 | 833c7174 | blueswir1 | vlan->nb_host_devs++; |
5255 | 7c9d8e07 | bellard | } else
|
5256 | 8a16d273 | ths | #ifdef CONFIG_VDE
|
5257 | 8a16d273 | ths | if (!strcmp(device, "vde")) { |
5258 | 8a16d273 | ths | char vde_sock[1024], vde_group[512]; |
5259 | 8a16d273 | ths | int vde_port, vde_mode;
|
5260 | 8a16d273 | ths | vlan->nb_host_devs++; |
5261 | 8a16d273 | ths | if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) { |
5262 | 8a16d273 | ths | vde_sock[0] = '\0'; |
5263 | 8a16d273 | ths | } |
5264 | 8a16d273 | ths | if (get_param_value(buf, sizeof(buf), "port", p) > 0) { |
5265 | 8a16d273 | ths | vde_port = strtol(buf, NULL, 10); |
5266 | 8a16d273 | ths | } else {
|
5267 | 8a16d273 | ths | vde_port = 0;
|
5268 | 8a16d273 | ths | } |
5269 | 8a16d273 | ths | if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) { |
5270 | 8a16d273 | ths | vde_group[0] = '\0'; |
5271 | 8a16d273 | ths | } |
5272 | 8a16d273 | ths | if (get_param_value(buf, sizeof(buf), "mode", p) > 0) { |
5273 | 8a16d273 | ths | vde_mode = strtol(buf, NULL, 8); |
5274 | 8a16d273 | ths | } else {
|
5275 | 8a16d273 | ths | vde_mode = 0700;
|
5276 | 8a16d273 | ths | } |
5277 | 8a16d273 | ths | ret = net_vde_init(vlan, vde_sock, vde_port, vde_group, vde_mode); |
5278 | 8a16d273 | ths | } else
|
5279 | 8a16d273 | ths | #endif
|
5280 | 7c9d8e07 | bellard | { |
5281 | 7c9d8e07 | bellard | fprintf(stderr, "Unknown network device: %s\n", device);
|
5282 | 7c9d8e07 | bellard | return -1; |
5283 | 7c9d8e07 | bellard | } |
5284 | 7c9d8e07 | bellard | if (ret < 0) { |
5285 | 7c9d8e07 | bellard | fprintf(stderr, "Could not initialize device '%s'\n", device);
|
5286 | 7c9d8e07 | bellard | } |
5287 | 3b46e624 | ths | |
5288 | 7c9d8e07 | bellard | return ret;
|
5289 | 7c9d8e07 | bellard | } |
5290 | 7c9d8e07 | bellard | |
5291 | 9ad97e65 | balrog | static int net_client_parse(const char *str) |
5292 | 9ad97e65 | balrog | { |
5293 | 9ad97e65 | balrog | const char *p; |
5294 | 9ad97e65 | balrog | char *q;
|
5295 | 9ad97e65 | balrog | char device[64]; |
5296 | 9ad97e65 | balrog | |
5297 | 9ad97e65 | balrog | p = str; |
5298 | 9ad97e65 | balrog | q = device; |
5299 | 9ad97e65 | balrog | while (*p != '\0' && *p != ',') { |
5300 | 9ad97e65 | balrog | if ((q - device) < sizeof(device) - 1) |
5301 | 9ad97e65 | balrog | *q++ = *p; |
5302 | 9ad97e65 | balrog | p++; |
5303 | 9ad97e65 | balrog | } |
5304 | 9ad97e65 | balrog | *q = '\0';
|
5305 | 9ad97e65 | balrog | if (*p == ',') |
5306 | 9ad97e65 | balrog | p++; |
5307 | 9ad97e65 | balrog | |
5308 | 9ad97e65 | balrog | return net_client_init(device, p);
|
5309 | 9ad97e65 | balrog | } |
5310 | 9ad97e65 | balrog | |
5311 | 7c9d8e07 | bellard | void do_info_network(void) |
5312 | 7c9d8e07 | bellard | { |
5313 | 7c9d8e07 | bellard | VLANState *vlan; |
5314 | 7c9d8e07 | bellard | VLANClientState *vc; |
5315 | 7c9d8e07 | bellard | |
5316 | 7c9d8e07 | bellard | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
5317 | 7c9d8e07 | bellard | term_printf("VLAN %d devices:\n", vlan->id);
|
5318 | 7c9d8e07 | bellard | for(vc = vlan->first_client; vc != NULL; vc = vc->next) |
5319 | 7c9d8e07 | bellard | term_printf(" %s\n", vc->info_str);
|
5320 | 7c9d8e07 | bellard | } |
5321 | 7c9d8e07 | bellard | } |
5322 | 42550fde | ths | |
5323 | 609497ab | balrog | #define HD_ALIAS "index=%d,media=disk" |
5324 | e4bcb14c | ths | #ifdef TARGET_PPC
|
5325 | e4bcb14c | ths | #define CDROM_ALIAS "index=1,media=cdrom" |
5326 | e4bcb14c | ths | #else
|
5327 | e4bcb14c | ths | #define CDROM_ALIAS "index=2,media=cdrom" |
5328 | e4bcb14c | ths | #endif
|
5329 | e4bcb14c | ths | #define FD_ALIAS "index=%d,if=floppy" |
5330 | 609497ab | balrog | #define PFLASH_ALIAS "if=pflash" |
5331 | 609497ab | balrog | #define MTD_ALIAS "if=mtd" |
5332 | 9d413d1d | balrog | #define SD_ALIAS "index=0,if=sd" |
5333 | e4bcb14c | ths | |
5334 | 609497ab | balrog | static int drive_add(const char *file, const char *fmt, ...) |
5335 | e4bcb14c | ths | { |
5336 | e4bcb14c | ths | va_list ap; |
5337 | e4bcb14c | ths | |
5338 | e4bcb14c | ths | if (nb_drives_opt >= MAX_DRIVES) {
|
5339 | e4bcb14c | ths | fprintf(stderr, "qemu: too many drives\n");
|
5340 | e4bcb14c | ths | exit(1);
|
5341 | e4bcb14c | ths | } |
5342 | e4bcb14c | ths | |
5343 | 609497ab | balrog | drives_opt[nb_drives_opt].file = file; |
5344 | e4bcb14c | ths | va_start(ap, fmt); |
5345 | 609497ab | balrog | vsnprintf(drives_opt[nb_drives_opt].opt, |
5346 | 609497ab | balrog | sizeof(drives_opt[0].opt), fmt, ap); |
5347 | e4bcb14c | ths | va_end(ap); |
5348 | e4bcb14c | ths | |
5349 | e4bcb14c | ths | return nb_drives_opt++;
|
5350 | e4bcb14c | ths | } |
5351 | e4bcb14c | ths | |
5352 | f60d39bc | ths | int drive_get_index(BlockInterfaceType type, int bus, int unit) |
5353 | e4bcb14c | ths | { |
5354 | e4bcb14c | ths | int index;
|
5355 | e4bcb14c | ths | |
5356 | e4bcb14c | ths | /* seek interface, bus and unit */
|
5357 | e4bcb14c | ths | |
5358 | e4bcb14c | ths | for (index = 0; index < nb_drives; index++) |
5359 | f60d39bc | ths | if (drives_table[index].type == type &&
|
5360 | e4bcb14c | ths | drives_table[index].bus == bus && |
5361 | e4bcb14c | ths | drives_table[index].unit == unit) |
5362 | e4bcb14c | ths | return index;
|
5363 | e4bcb14c | ths | |
5364 | e4bcb14c | ths | return -1; |
5365 | e4bcb14c | ths | } |
5366 | e4bcb14c | ths | |
5367 | f60d39bc | ths | int drive_get_max_bus(BlockInterfaceType type)
|
5368 | e4bcb14c | ths | { |
5369 | e4bcb14c | ths | int max_bus;
|
5370 | e4bcb14c | ths | int index;
|
5371 | e4bcb14c | ths | |
5372 | e4bcb14c | ths | max_bus = -1;
|
5373 | e4bcb14c | ths | for (index = 0; index < nb_drives; index++) { |
5374 | f60d39bc | ths | if(drives_table[index].type == type &&
|
5375 | e4bcb14c | ths | drives_table[index].bus > max_bus) |
5376 | e4bcb14c | ths | max_bus = drives_table[index].bus; |
5377 | e4bcb14c | ths | } |
5378 | e4bcb14c | ths | return max_bus;
|
5379 | e4bcb14c | ths | } |
5380 | e4bcb14c | ths | |
5381 | a1620fac | aurel32 | static void bdrv_format_print(void *opaque, const char *name) |
5382 | a1620fac | aurel32 | { |
5383 | a1620fac | aurel32 | fprintf(stderr, " %s", name);
|
5384 | a1620fac | aurel32 | } |
5385 | a1620fac | aurel32 | |
5386 | 609497ab | balrog | static int drive_init(struct drive_opt *arg, int snapshot, |
5387 | 609497ab | balrog | QEMUMachine *machine) |
5388 | e4bcb14c | ths | { |
5389 | e4bcb14c | ths | char buf[128]; |
5390 | e4bcb14c | ths | char file[1024]; |
5391 | c8522bdf | balrog | char devname[128]; |
5392 | c8522bdf | balrog | const char *mediastr = ""; |
5393 | f60d39bc | ths | BlockInterfaceType type; |
5394 | e4bcb14c | ths | enum { MEDIA_DISK, MEDIA_CDROM } media;
|
5395 | e4bcb14c | ths | int bus_id, unit_id;
|
5396 | e4bcb14c | ths | int cyls, heads, secs, translation;
|
5397 | e4bcb14c | ths | BlockDriverState *bdrv; |
5398 | 1e72d3b7 | aurel32 | BlockDriver *drv = NULL;
|
5399 | e4bcb14c | ths | int max_devs;
|
5400 | e4bcb14c | ths | int index;
|
5401 | 33f00271 | balrog | int cache;
|
5402 | 33f00271 | balrog | int bdrv_flags;
|
5403 | 609497ab | balrog | char *str = arg->opt;
|
5404 | e4bcb14c | ths | char *params[] = { "bus", "unit", "if", "index", "cyls", "heads", |
5405 | 33f00271 | balrog | "secs", "trans", "media", "snapshot", "file", |
5406 | 1e72d3b7 | aurel32 | "cache", "format", NULL }; |
5407 | e4bcb14c | ths | |
5408 | e4bcb14c | ths | if (check_params(buf, sizeof(buf), params, str) < 0) { |
5409 | ff993638 | balrog | fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
|
5410 | e4bcb14c | ths | buf, str); |
5411 | e4bcb14c | ths | return -1; |
5412 | e4bcb14c | ths | } |
5413 | e4bcb14c | ths | |
5414 | e4bcb14c | ths | file[0] = 0; |
5415 | e4bcb14c | ths | cyls = heads = secs = 0;
|
5416 | e4bcb14c | ths | bus_id = 0;
|
5417 | e4bcb14c | ths | unit_id = -1;
|
5418 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_AUTO; |
5419 | e4bcb14c | ths | index = -1;
|
5420 | 33f00271 | balrog | cache = 1;
|
5421 | e4bcb14c | ths | |
5422 | e4bcb14c | ths | if (!strcmp(machine->name, "realview") || |
5423 | e4bcb14c | ths | !strcmp(machine->name, "SS-5") ||
|
5424 | e4bcb14c | ths | !strcmp(machine->name, "SS-10") ||
|
5425 | e4bcb14c | ths | !strcmp(machine->name, "SS-600MP") ||
|
5426 | e4bcb14c | ths | !strcmp(machine->name, "versatilepb") ||
|
5427 | e4bcb14c | ths | !strcmp(machine->name, "versatileab")) {
|
5428 | f60d39bc | ths | type = IF_SCSI; |
5429 | e4bcb14c | ths | max_devs = MAX_SCSI_DEVS; |
5430 | c8522bdf | balrog | strcpy(devname, "scsi");
|
5431 | e4bcb14c | ths | } else {
|
5432 | f60d39bc | ths | type = IF_IDE; |
5433 | e4bcb14c | ths | max_devs = MAX_IDE_DEVS; |
5434 | c8522bdf | balrog | strcpy(devname, "ide");
|
5435 | e4bcb14c | ths | } |
5436 | e4bcb14c | ths | media = MEDIA_DISK; |
5437 | e4bcb14c | ths | |
5438 | e4bcb14c | ths | /* extract parameters */
|
5439 | e4bcb14c | ths | |
5440 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "bus", str)) { |
5441 | e4bcb14c | ths | bus_id = strtol(buf, NULL, 0); |
5442 | e4bcb14c | ths | if (bus_id < 0) { |
5443 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
|
5444 | e4bcb14c | ths | return -1; |
5445 | e4bcb14c | ths | } |
5446 | e4bcb14c | ths | } |
5447 | e4bcb14c | ths | |
5448 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "unit", str)) { |
5449 | e4bcb14c | ths | unit_id = strtol(buf, NULL, 0); |
5450 | e4bcb14c | ths | if (unit_id < 0) { |
5451 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
|
5452 | e4bcb14c | ths | return -1; |
5453 | e4bcb14c | ths | } |
5454 | e4bcb14c | ths | } |
5455 | e4bcb14c | ths | |
5456 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "if", str)) { |
5457 | ae45d369 | bellard | pstrcpy(devname, sizeof(devname), buf);
|
5458 | e4bcb14c | ths | if (!strcmp(buf, "ide")) { |
5459 | f60d39bc | ths | type = IF_IDE; |
5460 | e4bcb14c | ths | max_devs = MAX_IDE_DEVS; |
5461 | e4bcb14c | ths | } else if (!strcmp(buf, "scsi")) { |
5462 | f60d39bc | ths | type = IF_SCSI; |
5463 | e4bcb14c | ths | max_devs = MAX_SCSI_DEVS; |
5464 | e4bcb14c | ths | } else if (!strcmp(buf, "floppy")) { |
5465 | f60d39bc | ths | type = IF_FLOPPY; |
5466 | e4bcb14c | ths | max_devs = 0;
|
5467 | e4bcb14c | ths | } else if (!strcmp(buf, "pflash")) { |
5468 | f60d39bc | ths | type = IF_PFLASH; |
5469 | e4bcb14c | ths | max_devs = 0;
|
5470 | e4bcb14c | ths | } else if (!strcmp(buf, "mtd")) { |
5471 | f60d39bc | ths | type = IF_MTD; |
5472 | e4bcb14c | ths | max_devs = 0;
|
5473 | e4bcb14c | ths | } else if (!strcmp(buf, "sd")) { |
5474 | f60d39bc | ths | type = IF_SD; |
5475 | e4bcb14c | ths | max_devs = 0;
|
5476 | e4bcb14c | ths | } else {
|
5477 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
|
5478 | e4bcb14c | ths | return -1; |
5479 | e4bcb14c | ths | } |
5480 | e4bcb14c | ths | } |
5481 | e4bcb14c | ths | |
5482 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "index", str)) { |
5483 | e4bcb14c | ths | index = strtol(buf, NULL, 0); |
5484 | e4bcb14c | ths | if (index < 0) { |
5485 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid index\n", str);
|
5486 | e4bcb14c | ths | return -1; |
5487 | e4bcb14c | ths | } |
5488 | e4bcb14c | ths | } |
5489 | e4bcb14c | ths | |
5490 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "cyls", str)) { |
5491 | e4bcb14c | ths | cyls = strtol(buf, NULL, 0); |
5492 | e4bcb14c | ths | } |
5493 | e4bcb14c | ths | |
5494 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "heads", str)) { |
5495 | e4bcb14c | ths | heads = strtol(buf, NULL, 0); |
5496 | e4bcb14c | ths | } |
5497 | e4bcb14c | ths | |
5498 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "secs", str)) { |
5499 | e4bcb14c | ths | secs = strtol(buf, NULL, 0); |
5500 | e4bcb14c | ths | } |
5501 | e4bcb14c | ths | |
5502 | e4bcb14c | ths | if (cyls || heads || secs) {
|
5503 | e4bcb14c | ths | if (cyls < 1 || cyls > 16383) { |
5504 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
|
5505 | e4bcb14c | ths | return -1; |
5506 | e4bcb14c | ths | } |
5507 | e4bcb14c | ths | if (heads < 1 || heads > 16) { |
5508 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
|
5509 | e4bcb14c | ths | return -1; |
5510 | e4bcb14c | ths | } |
5511 | e4bcb14c | ths | if (secs < 1 || secs > 63) { |
5512 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
|
5513 | e4bcb14c | ths | return -1; |
5514 | e4bcb14c | ths | } |
5515 | e4bcb14c | ths | } |
5516 | e4bcb14c | ths | |
5517 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "trans", str)) { |
5518 | e4bcb14c | ths | if (!cyls) {
|
5519 | e4bcb14c | ths | fprintf(stderr, |
5520 | e4bcb14c | ths | "qemu: '%s' trans must be used with cyls,heads and secs\n",
|
5521 | e4bcb14c | ths | str); |
5522 | e4bcb14c | ths | return -1; |
5523 | e4bcb14c | ths | } |
5524 | e4bcb14c | ths | if (!strcmp(buf, "none")) |
5525 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_NONE; |
5526 | e4bcb14c | ths | else if (!strcmp(buf, "lba")) |
5527 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_LBA; |
5528 | e4bcb14c | ths | else if (!strcmp(buf, "auto")) |
5529 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_AUTO; |
5530 | e4bcb14c | ths | else {
|
5531 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
|
5532 | e4bcb14c | ths | return -1; |
5533 | e4bcb14c | ths | } |
5534 | e4bcb14c | ths | } |
5535 | e4bcb14c | ths | |
5536 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "media", str)) { |
5537 | e4bcb14c | ths | if (!strcmp(buf, "disk")) { |
5538 | e4bcb14c | ths | media = MEDIA_DISK; |
5539 | e4bcb14c | ths | } else if (!strcmp(buf, "cdrom")) { |
5540 | e4bcb14c | ths | if (cyls || secs || heads) {
|
5541 | e4bcb14c | ths | fprintf(stderr, |
5542 | e4bcb14c | ths | "qemu: '%s' invalid physical CHS format\n", str);
|
5543 | e4bcb14c | ths | return -1; |
5544 | e4bcb14c | ths | } |
5545 | e4bcb14c | ths | media = MEDIA_CDROM; |
5546 | e4bcb14c | ths | } else {
|
5547 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid media\n", str);
|
5548 | e4bcb14c | ths | return -1; |
5549 | e4bcb14c | ths | } |
5550 | e4bcb14c | ths | } |
5551 | e4bcb14c | ths | |
5552 | e4bcb14c | ths | if (get_param_value(buf, sizeof(buf), "snapshot", str)) { |
5553 | e4bcb14c | ths | if (!strcmp(buf, "on")) |
5554 | e4bcb14c | ths | snapshot = 1;
|
5555 | e4bcb14c | ths | else if (!strcmp(buf, "off")) |
5556 | e4bcb14c | ths | snapshot = 0;
|
5557 | e4bcb14c | ths | else {
|
5558 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
|
5559 | e4bcb14c | ths | return -1; |
5560 | e4bcb14c | ths | } |
5561 | e4bcb14c | ths | } |
5562 | e4bcb14c | ths | |
5563 | 33f00271 | balrog | if (get_param_value(buf, sizeof(buf), "cache", str)) { |
5564 | 33f00271 | balrog | if (!strcmp(buf, "off")) |
5565 | 33f00271 | balrog | cache = 0;
|
5566 | 33f00271 | balrog | else if (!strcmp(buf, "on")) |
5567 | 33f00271 | balrog | cache = 1;
|
5568 | 33f00271 | balrog | else {
|
5569 | 33f00271 | balrog | fprintf(stderr, "qemu: invalid cache option\n");
|
5570 | 33f00271 | balrog | return -1; |
5571 | 33f00271 | balrog | } |
5572 | 33f00271 | balrog | } |
5573 | 33f00271 | balrog | |
5574 | 1e72d3b7 | aurel32 | if (get_param_value(buf, sizeof(buf), "format", str)) { |
5575 | a1620fac | aurel32 | if (strcmp(buf, "?") == 0) { |
5576 | a1620fac | aurel32 | fprintf(stderr, "qemu: Supported formats:");
|
5577 | a1620fac | aurel32 | bdrv_iterate_format(bdrv_format_print, NULL);
|
5578 | a1620fac | aurel32 | fprintf(stderr, "\n");
|
5579 | a1620fac | aurel32 | return -1; |
5580 | a1620fac | aurel32 | } |
5581 | 1e72d3b7 | aurel32 | drv = bdrv_find_format(buf); |
5582 | 1e72d3b7 | aurel32 | if (!drv) {
|
5583 | 1e72d3b7 | aurel32 | fprintf(stderr, "qemu: '%s' invalid format\n", buf);
|
5584 | 1e72d3b7 | aurel32 | return -1; |
5585 | 1e72d3b7 | aurel32 | } |
5586 | 1e72d3b7 | aurel32 | } |
5587 | 1e72d3b7 | aurel32 | |
5588 | 609497ab | balrog | if (arg->file == NULL) |
5589 | 609497ab | balrog | get_param_value(file, sizeof(file), "file", str); |
5590 | 609497ab | balrog | else
|
5591 | 609497ab | balrog | pstrcpy(file, sizeof(file), arg->file);
|
5592 | e4bcb14c | ths | |
5593 | e4bcb14c | ths | /* compute bus and unit according index */
|
5594 | e4bcb14c | ths | |
5595 | e4bcb14c | ths | if (index != -1) { |
5596 | e4bcb14c | ths | if (bus_id != 0 || unit_id != -1) { |
5597 | e4bcb14c | ths | fprintf(stderr, |
5598 | e4bcb14c | ths | "qemu: '%s' index cannot be used with bus and unit\n", str);
|
5599 | e4bcb14c | ths | return -1; |
5600 | e4bcb14c | ths | } |
5601 | e4bcb14c | ths | if (max_devs == 0) |
5602 | e4bcb14c | ths | { |
5603 | e4bcb14c | ths | unit_id = index; |
5604 | e4bcb14c | ths | bus_id = 0;
|
5605 | e4bcb14c | ths | } else {
|
5606 | e4bcb14c | ths | unit_id = index % max_devs; |
5607 | e4bcb14c | ths | bus_id = index / max_devs; |
5608 | e4bcb14c | ths | } |
5609 | e4bcb14c | ths | } |
5610 | e4bcb14c | ths | |
5611 | e4bcb14c | ths | /* if user doesn't specify a unit_id,
|
5612 | e4bcb14c | ths | * try to find the first free
|
5613 | e4bcb14c | ths | */
|
5614 | e4bcb14c | ths | |
5615 | e4bcb14c | ths | if (unit_id == -1) { |
5616 | e4bcb14c | ths | unit_id = 0;
|
5617 | f60d39bc | ths | while (drive_get_index(type, bus_id, unit_id) != -1) { |
5618 | e4bcb14c | ths | unit_id++; |
5619 | e4bcb14c | ths | if (max_devs && unit_id >= max_devs) {
|
5620 | e4bcb14c | ths | unit_id -= max_devs; |
5621 | e4bcb14c | ths | bus_id++; |
5622 | e4bcb14c | ths | } |
5623 | e4bcb14c | ths | } |
5624 | e4bcb14c | ths | } |
5625 | e4bcb14c | ths | |
5626 | e4bcb14c | ths | /* check unit id */
|
5627 | e4bcb14c | ths | |
5628 | e4bcb14c | ths | if (max_devs && unit_id >= max_devs) {
|
5629 | e4bcb14c | ths | fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
|
5630 | e4bcb14c | ths | str, unit_id, max_devs - 1);
|
5631 | e4bcb14c | ths | return -1; |
5632 | e4bcb14c | ths | } |
5633 | e4bcb14c | ths | |
5634 | e4bcb14c | ths | /*
|
5635 | e4bcb14c | ths | * ignore multiple definitions
|
5636 | e4bcb14c | ths | */
|
5637 | e4bcb14c | ths | |
5638 | f60d39bc | ths | if (drive_get_index(type, bus_id, unit_id) != -1) |
5639 | e4bcb14c | ths | return 0; |
5640 | e4bcb14c | ths | |
5641 | e4bcb14c | ths | /* init */
|
5642 | e4bcb14c | ths | |
5643 | f60d39bc | ths | if (type == IF_IDE || type == IF_SCSI)
|
5644 | c8522bdf | balrog | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
5645 | e6198a70 | balrog | if (max_devs)
|
5646 | e6198a70 | balrog | snprintf(buf, sizeof(buf), "%s%i%s%i", |
5647 | e6198a70 | balrog | devname, bus_id, mediastr, unit_id); |
5648 | e6198a70 | balrog | else
|
5649 | e6198a70 | balrog | snprintf(buf, sizeof(buf), "%s%s%i", |
5650 | e6198a70 | balrog | devname, mediastr, unit_id); |
5651 | e4bcb14c | ths | bdrv = bdrv_new(buf); |
5652 | e4bcb14c | ths | drives_table[nb_drives].bdrv = bdrv; |
5653 | f60d39bc | ths | drives_table[nb_drives].type = type; |
5654 | e4bcb14c | ths | drives_table[nb_drives].bus = bus_id; |
5655 | e4bcb14c | ths | drives_table[nb_drives].unit = unit_id; |
5656 | e4bcb14c | ths | nb_drives++; |
5657 | e4bcb14c | ths | |
5658 | f60d39bc | ths | switch(type) {
|
5659 | e4bcb14c | ths | case IF_IDE:
|
5660 | e4bcb14c | ths | case IF_SCSI:
|
5661 | e4bcb14c | ths | switch(media) {
|
5662 | e4bcb14c | ths | case MEDIA_DISK:
|
5663 | e4bcb14c | ths | if (cyls != 0) { |
5664 | e4bcb14c | ths | bdrv_set_geometry_hint(bdrv, cyls, heads, secs); |
5665 | e4bcb14c | ths | bdrv_set_translation_hint(bdrv, translation); |
5666 | e4bcb14c | ths | } |
5667 | e4bcb14c | ths | break;
|
5668 | e4bcb14c | ths | case MEDIA_CDROM:
|
5669 | e4bcb14c | ths | bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM); |
5670 | e4bcb14c | ths | break;
|
5671 | e4bcb14c | ths | } |
5672 | e4bcb14c | ths | break;
|
5673 | e4bcb14c | ths | case IF_SD:
|
5674 | e4bcb14c | ths | /* FIXME: This isn't really a floppy, but it's a reasonable
|
5675 | e4bcb14c | ths | approximation. */
|
5676 | e4bcb14c | ths | case IF_FLOPPY:
|
5677 | e4bcb14c | ths | bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY); |
5678 | e4bcb14c | ths | break;
|
5679 | e4bcb14c | ths | case IF_PFLASH:
|
5680 | e4bcb14c | ths | case IF_MTD:
|
5681 | e4bcb14c | ths | break;
|
5682 | e4bcb14c | ths | } |
5683 | e4bcb14c | ths | if (!file[0]) |
5684 | e4bcb14c | ths | return 0; |
5685 | 33f00271 | balrog | bdrv_flags = 0;
|
5686 | 33f00271 | balrog | if (snapshot)
|
5687 | 33f00271 | balrog | bdrv_flags |= BDRV_O_SNAPSHOT; |
5688 | 33f00271 | balrog | if (!cache)
|
5689 | 33f00271 | balrog | bdrv_flags |= BDRV_O_DIRECT; |
5690 | 1e72d3b7 | aurel32 | if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) { |
5691 | e4bcb14c | ths | fprintf(stderr, "qemu: could not open disk image %s\n",
|
5692 | e4bcb14c | ths | file); |
5693 | e4bcb14c | ths | return -1; |
5694 | e4bcb14c | ths | } |
5695 | e4bcb14c | ths | return 0; |
5696 | e4bcb14c | ths | } |
5697 | e4bcb14c | ths | |
5698 | 330d0414 | bellard | /***********************************************************/
|
5699 | a594cfbf | bellard | /* USB devices */
|
5700 | a594cfbf | bellard | |
5701 | 0d92ed30 | pbrook | static USBPort *used_usb_ports;
|
5702 | 0d92ed30 | pbrook | static USBPort *free_usb_ports;
|
5703 | 0d92ed30 | pbrook | |
5704 | 0d92ed30 | pbrook | /* ??? Maybe change this to register a hub to keep track of the topology. */
|
5705 | 0d92ed30 | pbrook | void qemu_register_usb_port(USBPort *port, void *opaque, int index, |
5706 | 0d92ed30 | pbrook | usb_attachfn attach) |
5707 | 0d92ed30 | pbrook | { |
5708 | 0d92ed30 | pbrook | port->opaque = opaque; |
5709 | 0d92ed30 | pbrook | port->index = index; |
5710 | 0d92ed30 | pbrook | port->attach = attach; |
5711 | 0d92ed30 | pbrook | port->next = free_usb_ports; |
5712 | 0d92ed30 | pbrook | free_usb_ports = port; |
5713 | 0d92ed30 | pbrook | } |
5714 | 0d92ed30 | pbrook | |
5715 | a594cfbf | bellard | static int usb_device_add(const char *devname) |
5716 | a594cfbf | bellard | { |
5717 | a594cfbf | bellard | const char *p; |
5718 | a594cfbf | bellard | USBDevice *dev; |
5719 | 0d92ed30 | pbrook | USBPort *port; |
5720 | a594cfbf | bellard | |
5721 | 0d92ed30 | pbrook | if (!free_usb_ports)
|
5722 | a594cfbf | bellard | return -1; |
5723 | a594cfbf | bellard | |
5724 | a594cfbf | bellard | if (strstart(devname, "host:", &p)) { |
5725 | a594cfbf | bellard | dev = usb_host_device_open(p); |
5726 | a594cfbf | bellard | } else if (!strcmp(devname, "mouse")) { |
5727 | a594cfbf | bellard | dev = usb_mouse_init(); |
5728 | 09b26c5e | bellard | } else if (!strcmp(devname, "tablet")) { |
5729 | 47b2d338 | balrog | dev = usb_tablet_init(); |
5730 | 47b2d338 | balrog | } else if (!strcmp(devname, "keyboard")) { |
5731 | 47b2d338 | balrog | dev = usb_keyboard_init(); |
5732 | 2e5d83bb | pbrook | } else if (strstart(devname, "disk:", &p)) { |
5733 | 2e5d83bb | pbrook | dev = usb_msd_init(p); |
5734 | f6d2a316 | balrog | } else if (!strcmp(devname, "wacom-tablet")) { |
5735 | f6d2a316 | balrog | dev = usb_wacom_init(); |
5736 | a7954218 | balrog | } else if (strstart(devname, "serial:", &p)) { |
5737 | a7954218 | balrog | dev = usb_serial_init(p); |
5738 | 2e4d9fb1 | aurel32 | #ifdef CONFIG_BRLAPI
|
5739 | 2e4d9fb1 | aurel32 | } else if (!strcmp(devname, "braille")) { |
5740 | 2e4d9fb1 | aurel32 | dev = usb_baum_init(); |
5741 | 2e4d9fb1 | aurel32 | #endif
|
5742 | 6c9f886c | balrog | } else if (strstart(devname, "net:", &p)) { |
5743 | 9ad97e65 | balrog | int nic = nb_nics;
|
5744 | 6c9f886c | balrog | |
5745 | 9ad97e65 | balrog | if (net_client_init("nic", p) < 0) |
5746 | 6c9f886c | balrog | return -1; |
5747 | 9ad97e65 | balrog | nd_table[nic].model = "usb";
|
5748 | 9ad97e65 | balrog | dev = usb_net_init(&nd_table[nic]); |
5749 | a594cfbf | bellard | } else {
|
5750 | a594cfbf | bellard | return -1; |
5751 | a594cfbf | bellard | } |
5752 | 0d92ed30 | pbrook | if (!dev)
|
5753 | 0d92ed30 | pbrook | return -1; |
5754 | 0d92ed30 | pbrook | |
5755 | 0d92ed30 | pbrook | /* Find a USB port to add the device to. */
|
5756 | 0d92ed30 | pbrook | port = free_usb_ports; |
5757 | 0d92ed30 | pbrook | if (!port->next) {
|
5758 | 0d92ed30 | pbrook | USBDevice *hub; |
5759 | 0d92ed30 | pbrook | |
5760 | 0d92ed30 | pbrook | /* Create a new hub and chain it on. */
|
5761 | 0d92ed30 | pbrook | free_usb_ports = NULL;
|
5762 | 0d92ed30 | pbrook | port->next = used_usb_ports; |
5763 | 0d92ed30 | pbrook | used_usb_ports = port; |
5764 | 0d92ed30 | pbrook | |
5765 | 0d92ed30 | pbrook | hub = usb_hub_init(VM_USB_HUB_SIZE); |
5766 | 0d92ed30 | pbrook | usb_attach(port, hub); |
5767 | 0d92ed30 | pbrook | port = free_usb_ports; |
5768 | 0d92ed30 | pbrook | } |
5769 | 0d92ed30 | pbrook | |
5770 | 0d92ed30 | pbrook | free_usb_ports = port->next; |
5771 | 0d92ed30 | pbrook | port->next = used_usb_ports; |
5772 | 0d92ed30 | pbrook | used_usb_ports = port; |
5773 | 0d92ed30 | pbrook | usb_attach(port, dev); |
5774 | a594cfbf | bellard | return 0; |
5775 | a594cfbf | bellard | } |
5776 | a594cfbf | bellard | |
5777 | a594cfbf | bellard | static int usb_device_del(const char *devname) |
5778 | a594cfbf | bellard | { |
5779 | 0d92ed30 | pbrook | USBPort *port; |
5780 | 0d92ed30 | pbrook | USBPort **lastp; |
5781 | 059809e4 | bellard | USBDevice *dev; |
5782 | 0d92ed30 | pbrook | int bus_num, addr;
|
5783 | a594cfbf | bellard | const char *p; |
5784 | a594cfbf | bellard | |
5785 | 0d92ed30 | pbrook | if (!used_usb_ports)
|
5786 | a594cfbf | bellard | return -1; |
5787 | a594cfbf | bellard | |
5788 | a594cfbf | bellard | p = strchr(devname, '.');
|
5789 | 5fafdf24 | ths | if (!p)
|
5790 | a594cfbf | bellard | return -1; |
5791 | a594cfbf | bellard | bus_num = strtoul(devname, NULL, 0); |
5792 | a594cfbf | bellard | addr = strtoul(p + 1, NULL, 0); |
5793 | a594cfbf | bellard | if (bus_num != 0) |
5794 | a594cfbf | bellard | return -1; |
5795 | 0d92ed30 | pbrook | |
5796 | 0d92ed30 | pbrook | lastp = &used_usb_ports; |
5797 | 0d92ed30 | pbrook | port = used_usb_ports; |
5798 | 0d92ed30 | pbrook | while (port && port->dev->addr != addr) {
|
5799 | 0d92ed30 | pbrook | lastp = &port->next; |
5800 | 0d92ed30 | pbrook | port = port->next; |
5801 | a594cfbf | bellard | } |
5802 | 0d92ed30 | pbrook | |
5803 | 0d92ed30 | pbrook | if (!port)
|
5804 | a594cfbf | bellard | return -1; |
5805 | 0d92ed30 | pbrook | |
5806 | 059809e4 | bellard | dev = port->dev; |
5807 | 0d92ed30 | pbrook | *lastp = port->next; |
5808 | 0d92ed30 | pbrook | usb_attach(port, NULL);
|
5809 | 059809e4 | bellard | dev->handle_destroy(dev); |
5810 | 0d92ed30 | pbrook | port->next = free_usb_ports; |
5811 | 0d92ed30 | pbrook | free_usb_ports = port; |
5812 | a594cfbf | bellard | return 0; |
5813 | a594cfbf | bellard | } |
5814 | a594cfbf | bellard | |
5815 | a594cfbf | bellard | void do_usb_add(const char *devname) |
5816 | a594cfbf | bellard | { |
5817 | a594cfbf | bellard | int ret;
|
5818 | a594cfbf | bellard | ret = usb_device_add(devname); |
5819 | 5fafdf24 | ths | if (ret < 0) |
5820 | a594cfbf | bellard | term_printf("Could not add USB device '%s'\n", devname);
|
5821 | a594cfbf | bellard | } |
5822 | a594cfbf | bellard | |
5823 | a594cfbf | bellard | void do_usb_del(const char *devname) |
5824 | a594cfbf | bellard | { |
5825 | a594cfbf | bellard | int ret;
|
5826 | a594cfbf | bellard | ret = usb_device_del(devname); |
5827 | 5fafdf24 | ths | if (ret < 0) |
5828 | a594cfbf | bellard | term_printf("Could not remove USB device '%s'\n", devname);
|
5829 | a594cfbf | bellard | } |
5830 | a594cfbf | bellard | |
5831 | a594cfbf | bellard | void usb_info(void) |
5832 | a594cfbf | bellard | { |
5833 | a594cfbf | bellard | USBDevice *dev; |
5834 | 0d92ed30 | pbrook | USBPort *port; |
5835 | a594cfbf | bellard | const char *speed_str; |
5836 | a594cfbf | bellard | |
5837 | 0d92ed30 | pbrook | if (!usb_enabled) {
|
5838 | a594cfbf | bellard | term_printf("USB support not enabled\n");
|
5839 | a594cfbf | bellard | return;
|
5840 | a594cfbf | bellard | } |
5841 | a594cfbf | bellard | |
5842 | 0d92ed30 | pbrook | for (port = used_usb_ports; port; port = port->next) {
|
5843 | 0d92ed30 | pbrook | dev = port->dev; |
5844 | 0d92ed30 | pbrook | if (!dev)
|
5845 | 0d92ed30 | pbrook | continue;
|
5846 | 0d92ed30 | pbrook | switch(dev->speed) {
|
5847 | 5fafdf24 | ths | case USB_SPEED_LOW:
|
5848 | 5fafdf24 | ths | speed_str = "1.5";
|
5849 | 0d92ed30 | pbrook | break;
|
5850 | 5fafdf24 | ths | case USB_SPEED_FULL:
|
5851 | 5fafdf24 | ths | speed_str = "12";
|
5852 | 0d92ed30 | pbrook | break;
|
5853 | 5fafdf24 | ths | case USB_SPEED_HIGH:
|
5854 | 5fafdf24 | ths | speed_str = "480";
|
5855 | 0d92ed30 | pbrook | break;
|
5856 | 0d92ed30 | pbrook | default:
|
5857 | 5fafdf24 | ths | speed_str = "?";
|
5858 | 0d92ed30 | pbrook | break;
|
5859 | a594cfbf | bellard | } |
5860 | 5fafdf24 | ths | term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
|
5861 | 1f6e24e7 | bellard | 0, dev->addr, speed_str, dev->devname);
|
5862 | a594cfbf | bellard | } |
5863 | a594cfbf | bellard | } |
5864 | a594cfbf | bellard | |
5865 | f7cce898 | bellard | /***********************************************************/
|
5866 | 201a51fc | balrog | /* PCMCIA/Cardbus */
|
5867 | 201a51fc | balrog | |
5868 | 201a51fc | balrog | static struct pcmcia_socket_entry_s { |
5869 | 201a51fc | balrog | struct pcmcia_socket_s *socket;
|
5870 | 201a51fc | balrog | struct pcmcia_socket_entry_s *next;
|
5871 | 201a51fc | balrog | } *pcmcia_sockets = 0;
|
5872 | 201a51fc | balrog | |
5873 | 201a51fc | balrog | void pcmcia_socket_register(struct pcmcia_socket_s *socket) |
5874 | 201a51fc | balrog | { |
5875 | 201a51fc | balrog | struct pcmcia_socket_entry_s *entry;
|
5876 | 201a51fc | balrog | |
5877 | 201a51fc | balrog | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); |
5878 | 201a51fc | balrog | entry->socket = socket; |
5879 | 201a51fc | balrog | entry->next = pcmcia_sockets; |
5880 | 201a51fc | balrog | pcmcia_sockets = entry; |
5881 | 201a51fc | balrog | } |
5882 | 201a51fc | balrog | |
5883 | 201a51fc | balrog | void pcmcia_socket_unregister(struct pcmcia_socket_s *socket) |
5884 | 201a51fc | balrog | { |
5885 | 201a51fc | balrog | struct pcmcia_socket_entry_s *entry, **ptr;
|
5886 | 201a51fc | balrog | |
5887 | 201a51fc | balrog | ptr = &pcmcia_sockets; |
5888 | 201a51fc | balrog | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
|
5889 | 201a51fc | balrog | if (entry->socket == socket) {
|
5890 | 201a51fc | balrog | *ptr = entry->next; |
5891 | 201a51fc | balrog | qemu_free(entry); |
5892 | 201a51fc | balrog | } |
5893 | 201a51fc | balrog | } |
5894 | 201a51fc | balrog | |
5895 | 201a51fc | balrog | void pcmcia_info(void) |
5896 | 201a51fc | balrog | { |
5897 | 201a51fc | balrog | struct pcmcia_socket_entry_s *iter;
|
5898 | 201a51fc | balrog | if (!pcmcia_sockets)
|
5899 | 201a51fc | balrog | term_printf("No PCMCIA sockets\n");
|
5900 | 201a51fc | balrog | |
5901 | 201a51fc | balrog | for (iter = pcmcia_sockets; iter; iter = iter->next)
|
5902 | 201a51fc | balrog | term_printf("%s: %s\n", iter->socket->slot_string,
|
5903 | 201a51fc | balrog | iter->socket->attached ? iter->socket->card_string : |
5904 | 201a51fc | balrog | "Empty");
|
5905 | 201a51fc | balrog | } |
5906 | 201a51fc | balrog | |
5907 | 201a51fc | balrog | /***********************************************************/
|
5908 | 2ff89790 | ths | /* dumb display */
|
5909 | 2ff89790 | ths | |
5910 | 2ff89790 | ths | static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
5911 | 2ff89790 | ths | { |
5912 | 2ff89790 | ths | } |
5913 | 2ff89790 | ths | |
5914 | 2ff89790 | ths | static void dumb_resize(DisplayState *ds, int w, int h) |
5915 | 2ff89790 | ths | { |
5916 | 2ff89790 | ths | } |
5917 | 2ff89790 | ths | |
5918 | 2ff89790 | ths | static void dumb_refresh(DisplayState *ds) |
5919 | 2ff89790 | ths | { |
5920 | 2ff89790 | ths | #if defined(CONFIG_SDL)
|
5921 | 2ff89790 | ths | vga_hw_update(); |
5922 | 2ff89790 | ths | #endif
|
5923 | 2ff89790 | ths | } |
5924 | 2ff89790 | ths | |
5925 | 2ff89790 | ths | static void dumb_display_init(DisplayState *ds) |
5926 | 2ff89790 | ths | { |
5927 | 2ff89790 | ths | ds->data = NULL;
|
5928 | 2ff89790 | ths | ds->linesize = 0;
|
5929 | 2ff89790 | ths | ds->depth = 0;
|
5930 | 2ff89790 | ths | ds->dpy_update = dumb_update; |
5931 | 2ff89790 | ths | ds->dpy_resize = dumb_resize; |
5932 | 2ff89790 | ths | ds->dpy_refresh = dumb_refresh; |
5933 | 2ff89790 | ths | } |
5934 | 2ff89790 | ths | |
5935 | 2ff89790 | ths | /***********************************************************/
|
5936 | 8a7ddc38 | bellard | /* I/O handling */
|
5937 | 0824d6fc | bellard | |
5938 | c4b1fcc0 | bellard | #define MAX_IO_HANDLERS 64 |
5939 | c4b1fcc0 | bellard | |
5940 | c4b1fcc0 | bellard | typedef struct IOHandlerRecord { |
5941 | c4b1fcc0 | bellard | int fd;
|
5942 | 7c9d8e07 | bellard | IOCanRWHandler *fd_read_poll; |
5943 | 7c9d8e07 | bellard | IOHandler *fd_read; |
5944 | 7c9d8e07 | bellard | IOHandler *fd_write; |
5945 | cafffd40 | ths | int deleted;
|
5946 | c4b1fcc0 | bellard | void *opaque;
|
5947 | c4b1fcc0 | bellard | /* temporary data */
|
5948 | c4b1fcc0 | bellard | struct pollfd *ufd;
|
5949 | 8a7ddc38 | bellard | struct IOHandlerRecord *next;
|
5950 | c4b1fcc0 | bellard | } IOHandlerRecord; |
5951 | c4b1fcc0 | bellard | |
5952 | 8a7ddc38 | bellard | static IOHandlerRecord *first_io_handler;
|
5953 | c4b1fcc0 | bellard | |
5954 | 7c9d8e07 | bellard | /* XXX: fd_read_poll should be suppressed, but an API change is
|
5955 | 7c9d8e07 | bellard | necessary in the character devices to suppress fd_can_read(). */
|
5956 | 5fafdf24 | ths | int qemu_set_fd_handler2(int fd, |
5957 | 5fafdf24 | ths | IOCanRWHandler *fd_read_poll, |
5958 | 5fafdf24 | ths | IOHandler *fd_read, |
5959 | 5fafdf24 | ths | IOHandler *fd_write, |
5960 | 7c9d8e07 | bellard | void *opaque)
|
5961 | c4b1fcc0 | bellard | { |
5962 | 7c9d8e07 | bellard | IOHandlerRecord **pioh, *ioh; |
5963 | c4b1fcc0 | bellard | |
5964 | 7c9d8e07 | bellard | if (!fd_read && !fd_write) {
|
5965 | 7c9d8e07 | bellard | pioh = &first_io_handler; |
5966 | 7c9d8e07 | bellard | for(;;) {
|
5967 | 7c9d8e07 | bellard | ioh = *pioh; |
5968 | 7c9d8e07 | bellard | if (ioh == NULL) |
5969 | 7c9d8e07 | bellard | break;
|
5970 | 7c9d8e07 | bellard | if (ioh->fd == fd) {
|
5971 | cafffd40 | ths | ioh->deleted = 1;
|
5972 | 7c9d8e07 | bellard | break;
|
5973 | 7c9d8e07 | bellard | } |
5974 | 7c9d8e07 | bellard | pioh = &ioh->next; |
5975 | 7c9d8e07 | bellard | } |
5976 | 7c9d8e07 | bellard | } else {
|
5977 | 7c9d8e07 | bellard | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
5978 | 7c9d8e07 | bellard | if (ioh->fd == fd)
|
5979 | 7c9d8e07 | bellard | goto found;
|
5980 | 7c9d8e07 | bellard | } |
5981 | 7c9d8e07 | bellard | ioh = qemu_mallocz(sizeof(IOHandlerRecord));
|
5982 | 7c9d8e07 | bellard | if (!ioh)
|
5983 | 7c9d8e07 | bellard | return -1; |
5984 | 7c9d8e07 | bellard | ioh->next = first_io_handler; |
5985 | 7c9d8e07 | bellard | first_io_handler = ioh; |
5986 | 7c9d8e07 | bellard | found:
|
5987 | 7c9d8e07 | bellard | ioh->fd = fd; |
5988 | 7c9d8e07 | bellard | ioh->fd_read_poll = fd_read_poll; |
5989 | 7c9d8e07 | bellard | ioh->fd_read = fd_read; |
5990 | 7c9d8e07 | bellard | ioh->fd_write = fd_write; |
5991 | 7c9d8e07 | bellard | ioh->opaque = opaque; |
5992 | cafffd40 | ths | ioh->deleted = 0;
|
5993 | 7c9d8e07 | bellard | } |
5994 | c4b1fcc0 | bellard | return 0; |
5995 | c4b1fcc0 | bellard | } |
5996 | c4b1fcc0 | bellard | |
5997 | 5fafdf24 | ths | int qemu_set_fd_handler(int fd, |
5998 | 5fafdf24 | ths | IOHandler *fd_read, |
5999 | 5fafdf24 | ths | IOHandler *fd_write, |
6000 | 7c9d8e07 | bellard | void *opaque)
|
6001 | 8a7ddc38 | bellard | { |
6002 | 7c9d8e07 | bellard | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
6003 | 8a7ddc38 | bellard | } |
6004 | 8a7ddc38 | bellard | |
6005 | 8a7ddc38 | bellard | /***********************************************************/
|
6006 | f331110f | bellard | /* Polling handling */
|
6007 | f331110f | bellard | |
6008 | f331110f | bellard | typedef struct PollingEntry { |
6009 | f331110f | bellard | PollingFunc *func; |
6010 | f331110f | bellard | void *opaque;
|
6011 | f331110f | bellard | struct PollingEntry *next;
|
6012 | f331110f | bellard | } PollingEntry; |
6013 | f331110f | bellard | |
6014 | f331110f | bellard | static PollingEntry *first_polling_entry;
|
6015 | f331110f | bellard | |
6016 | f331110f | bellard | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
6017 | f331110f | bellard | { |
6018 | f331110f | bellard | PollingEntry **ppe, *pe; |
6019 | f331110f | bellard | pe = qemu_mallocz(sizeof(PollingEntry));
|
6020 | f331110f | bellard | if (!pe)
|
6021 | f331110f | bellard | return -1; |
6022 | f331110f | bellard | pe->func = func; |
6023 | f331110f | bellard | pe->opaque = opaque; |
6024 | f331110f | bellard | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
6025 | f331110f | bellard | *ppe = pe; |
6026 | f331110f | bellard | return 0; |
6027 | f331110f | bellard | } |
6028 | f331110f | bellard | |
6029 | f331110f | bellard | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
6030 | f331110f | bellard | { |
6031 | f331110f | bellard | PollingEntry **ppe, *pe; |
6032 | f331110f | bellard | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
6033 | f331110f | bellard | pe = *ppe; |
6034 | f331110f | bellard | if (pe->func == func && pe->opaque == opaque) {
|
6035 | f331110f | bellard | *ppe = pe->next; |
6036 | f331110f | bellard | qemu_free(pe); |
6037 | f331110f | bellard | break;
|
6038 | f331110f | bellard | } |
6039 | f331110f | bellard | } |
6040 | f331110f | bellard | } |
6041 | f331110f | bellard | |
6042 | a18e524a | bellard | #ifdef _WIN32
|
6043 | a18e524a | bellard | /***********************************************************/
|
6044 | a18e524a | bellard | /* Wait objects support */
|
6045 | a18e524a | bellard | typedef struct WaitObjects { |
6046 | a18e524a | bellard | int num;
|
6047 | a18e524a | bellard | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
|
6048 | a18e524a | bellard | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
|
6049 | a18e524a | bellard | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
6050 | a18e524a | bellard | } WaitObjects; |
6051 | a18e524a | bellard | |
6052 | a18e524a | bellard | static WaitObjects wait_objects = {0}; |
6053 | 3b46e624 | ths | |
6054 | a18e524a | bellard | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
6055 | a18e524a | bellard | { |
6056 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
6057 | a18e524a | bellard | |
6058 | a18e524a | bellard | if (w->num >= MAXIMUM_WAIT_OBJECTS)
|
6059 | a18e524a | bellard | return -1; |
6060 | a18e524a | bellard | w->events[w->num] = handle; |
6061 | a18e524a | bellard | w->func[w->num] = func; |
6062 | a18e524a | bellard | w->opaque[w->num] = opaque; |
6063 | a18e524a | bellard | w->num++; |
6064 | a18e524a | bellard | return 0; |
6065 | a18e524a | bellard | } |
6066 | a18e524a | bellard | |
6067 | a18e524a | bellard | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
6068 | a18e524a | bellard | { |
6069 | a18e524a | bellard | int i, found;
|
6070 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
6071 | a18e524a | bellard | |
6072 | a18e524a | bellard | found = 0;
|
6073 | a18e524a | bellard | for (i = 0; i < w->num; i++) { |
6074 | a18e524a | bellard | if (w->events[i] == handle)
|
6075 | a18e524a | bellard | found = 1;
|
6076 | a18e524a | bellard | if (found) {
|
6077 | a18e524a | bellard | w->events[i] = w->events[i + 1];
|
6078 | a18e524a | bellard | w->func[i] = w->func[i + 1];
|
6079 | a18e524a | bellard | w->opaque[i] = w->opaque[i + 1];
|
6080 | 3b46e624 | ths | } |
6081 | a18e524a | bellard | } |
6082 | a18e524a | bellard | if (found)
|
6083 | a18e524a | bellard | w->num--; |
6084 | a18e524a | bellard | } |
6085 | a18e524a | bellard | #endif
|
6086 | a18e524a | bellard | |
6087 | f331110f | bellard | /***********************************************************/
|
6088 | 8a7ddc38 | bellard | /* savevm/loadvm support */
|
6089 | 8a7ddc38 | bellard | |
6090 | faea38e7 | bellard | #define IO_BUF_SIZE 32768 |
6091 | faea38e7 | bellard | |
6092 | faea38e7 | bellard | struct QEMUFile {
|
6093 | faea38e7 | bellard | FILE *outfile; |
6094 | faea38e7 | bellard | BlockDriverState *bs; |
6095 | faea38e7 | bellard | int is_file;
|
6096 | faea38e7 | bellard | int is_writable;
|
6097 | faea38e7 | bellard | int64_t base_offset; |
6098 | faea38e7 | bellard | int64_t buf_offset; /* start of buffer when writing, end of buffer
|
6099 | faea38e7 | bellard | when reading */
|
6100 | faea38e7 | bellard | int buf_index;
|
6101 | faea38e7 | bellard | int buf_size; /* 0 when writing */ |
6102 | faea38e7 | bellard | uint8_t buf[IO_BUF_SIZE]; |
6103 | faea38e7 | bellard | }; |
6104 | faea38e7 | bellard | |
6105 | faea38e7 | bellard | QEMUFile *qemu_fopen(const char *filename, const char *mode) |
6106 | faea38e7 | bellard | { |
6107 | faea38e7 | bellard | QEMUFile *f; |
6108 | faea38e7 | bellard | |
6109 | faea38e7 | bellard | f = qemu_mallocz(sizeof(QEMUFile));
|
6110 | faea38e7 | bellard | if (!f)
|
6111 | faea38e7 | bellard | return NULL; |
6112 | faea38e7 | bellard | if (!strcmp(mode, "wb")) { |
6113 | faea38e7 | bellard | f->is_writable = 1;
|
6114 | faea38e7 | bellard | } else if (!strcmp(mode, "rb")) { |
6115 | faea38e7 | bellard | f->is_writable = 0;
|
6116 | faea38e7 | bellard | } else {
|
6117 | faea38e7 | bellard | goto fail;
|
6118 | faea38e7 | bellard | } |
6119 | faea38e7 | bellard | f->outfile = fopen(filename, mode); |
6120 | faea38e7 | bellard | if (!f->outfile)
|
6121 | faea38e7 | bellard | goto fail;
|
6122 | faea38e7 | bellard | f->is_file = 1;
|
6123 | faea38e7 | bellard | return f;
|
6124 | faea38e7 | bellard | fail:
|
6125 | faea38e7 | bellard | if (f->outfile)
|
6126 | faea38e7 | bellard | fclose(f->outfile); |
6127 | faea38e7 | bellard | qemu_free(f); |
6128 | faea38e7 | bellard | return NULL; |
6129 | faea38e7 | bellard | } |
6130 | faea38e7 | bellard | |
6131 | 9596ebb7 | pbrook | static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable) |
6132 | faea38e7 | bellard | { |
6133 | faea38e7 | bellard | QEMUFile *f; |
6134 | faea38e7 | bellard | |
6135 | faea38e7 | bellard | f = qemu_mallocz(sizeof(QEMUFile));
|
6136 | faea38e7 | bellard | if (!f)
|
6137 | faea38e7 | bellard | return NULL; |
6138 | faea38e7 | bellard | f->is_file = 0;
|
6139 | faea38e7 | bellard | f->bs = bs; |
6140 | faea38e7 | bellard | f->is_writable = is_writable; |
6141 | faea38e7 | bellard | f->base_offset = offset; |
6142 | faea38e7 | bellard | return f;
|
6143 | faea38e7 | bellard | } |
6144 | faea38e7 | bellard | |
6145 | faea38e7 | bellard | void qemu_fflush(QEMUFile *f)
|
6146 | faea38e7 | bellard | { |
6147 | faea38e7 | bellard | if (!f->is_writable)
|
6148 | faea38e7 | bellard | return;
|
6149 | faea38e7 | bellard | if (f->buf_index > 0) { |
6150 | faea38e7 | bellard | if (f->is_file) {
|
6151 | faea38e7 | bellard | fseek(f->outfile, f->buf_offset, SEEK_SET); |
6152 | faea38e7 | bellard | fwrite(f->buf, 1, f->buf_index, f->outfile);
|
6153 | faea38e7 | bellard | } else {
|
6154 | 5fafdf24 | ths | bdrv_pwrite(f->bs, f->base_offset + f->buf_offset, |
6155 | faea38e7 | bellard | f->buf, f->buf_index); |
6156 | faea38e7 | bellard | } |
6157 | faea38e7 | bellard | f->buf_offset += f->buf_index; |
6158 | faea38e7 | bellard | f->buf_index = 0;
|
6159 | faea38e7 | bellard | } |
6160 | faea38e7 | bellard | } |
6161 | faea38e7 | bellard | |
6162 | faea38e7 | bellard | static void qemu_fill_buffer(QEMUFile *f) |
6163 | faea38e7 | bellard | { |
6164 | faea38e7 | bellard | int len;
|
6165 | faea38e7 | bellard | |
6166 | faea38e7 | bellard | if (f->is_writable)
|
6167 | faea38e7 | bellard | return;
|
6168 | faea38e7 | bellard | if (f->is_file) {
|
6169 | faea38e7 | bellard | fseek(f->outfile, f->buf_offset, SEEK_SET); |
6170 | faea38e7 | bellard | len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile);
|
6171 | faea38e7 | bellard | if (len < 0) |
6172 | faea38e7 | bellard | len = 0;
|
6173 | faea38e7 | bellard | } else {
|
6174 | 5fafdf24 | ths | len = bdrv_pread(f->bs, f->base_offset + f->buf_offset, |
6175 | faea38e7 | bellard | f->buf, IO_BUF_SIZE); |
6176 | faea38e7 | bellard | if (len < 0) |
6177 | faea38e7 | bellard | len = 0;
|
6178 | faea38e7 | bellard | } |
6179 | faea38e7 | bellard | f->buf_index = 0;
|
6180 | faea38e7 | bellard | f->buf_size = len; |
6181 | faea38e7 | bellard | f->buf_offset += len; |
6182 | faea38e7 | bellard | } |
6183 | faea38e7 | bellard | |
6184 | faea38e7 | bellard | void qemu_fclose(QEMUFile *f)
|
6185 | faea38e7 | bellard | { |
6186 | faea38e7 | bellard | if (f->is_writable)
|
6187 | faea38e7 | bellard | qemu_fflush(f); |
6188 | faea38e7 | bellard | if (f->is_file) {
|
6189 | faea38e7 | bellard | fclose(f->outfile); |
6190 | faea38e7 | bellard | } |
6191 | faea38e7 | bellard | qemu_free(f); |
6192 | faea38e7 | bellard | } |
6193 | faea38e7 | bellard | |
6194 | 8a7ddc38 | bellard | void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
6195 | b4608c04 | bellard | { |
6196 | faea38e7 | bellard | int l;
|
6197 | faea38e7 | bellard | while (size > 0) { |
6198 | faea38e7 | bellard | l = IO_BUF_SIZE - f->buf_index; |
6199 | faea38e7 | bellard | if (l > size)
|
6200 | faea38e7 | bellard | l = size; |
6201 | faea38e7 | bellard | memcpy(f->buf + f->buf_index, buf, l); |
6202 | faea38e7 | bellard | f->buf_index += l; |
6203 | faea38e7 | bellard | buf += l; |
6204 | faea38e7 | bellard | size -= l; |
6205 | faea38e7 | bellard | if (f->buf_index >= IO_BUF_SIZE)
|
6206 | faea38e7 | bellard | qemu_fflush(f); |
6207 | faea38e7 | bellard | } |
6208 | b4608c04 | bellard | } |
6209 | b4608c04 | bellard | |
6210 | 8a7ddc38 | bellard | void qemu_put_byte(QEMUFile *f, int v) |
6211 | b4608c04 | bellard | { |
6212 | faea38e7 | bellard | f->buf[f->buf_index++] = v; |
6213 | faea38e7 | bellard | if (f->buf_index >= IO_BUF_SIZE)
|
6214 | faea38e7 | bellard | qemu_fflush(f); |
6215 | faea38e7 | bellard | } |
6216 | faea38e7 | bellard | |
6217 | faea38e7 | bellard | int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1) |
6218 | faea38e7 | bellard | { |
6219 | faea38e7 | bellard | int size, l;
|
6220 | faea38e7 | bellard | |
6221 | faea38e7 | bellard | size = size1; |
6222 | faea38e7 | bellard | while (size > 0) { |
6223 | faea38e7 | bellard | l = f->buf_size - f->buf_index; |
6224 | faea38e7 | bellard | if (l == 0) { |
6225 | faea38e7 | bellard | qemu_fill_buffer(f); |
6226 | faea38e7 | bellard | l = f->buf_size - f->buf_index; |
6227 | faea38e7 | bellard | if (l == 0) |
6228 | faea38e7 | bellard | break;
|
6229 | faea38e7 | bellard | } |
6230 | faea38e7 | bellard | if (l > size)
|
6231 | faea38e7 | bellard | l = size; |
6232 | faea38e7 | bellard | memcpy(buf, f->buf + f->buf_index, l); |
6233 | faea38e7 | bellard | f->buf_index += l; |
6234 | faea38e7 | bellard | buf += l; |
6235 | faea38e7 | bellard | size -= l; |
6236 | faea38e7 | bellard | } |
6237 | faea38e7 | bellard | return size1 - size;
|
6238 | faea38e7 | bellard | } |
6239 | faea38e7 | bellard | |
6240 | faea38e7 | bellard | int qemu_get_byte(QEMUFile *f)
|
6241 | faea38e7 | bellard | { |
6242 | faea38e7 | bellard | if (f->buf_index >= f->buf_size) {
|
6243 | faea38e7 | bellard | qemu_fill_buffer(f); |
6244 | faea38e7 | bellard | if (f->buf_index >= f->buf_size)
|
6245 | faea38e7 | bellard | return 0; |
6246 | faea38e7 | bellard | } |
6247 | faea38e7 | bellard | return f->buf[f->buf_index++];
|
6248 | faea38e7 | bellard | } |
6249 | faea38e7 | bellard | |
6250 | faea38e7 | bellard | int64_t qemu_ftell(QEMUFile *f) |
6251 | faea38e7 | bellard | { |
6252 | faea38e7 | bellard | return f->buf_offset - f->buf_size + f->buf_index;
|
6253 | faea38e7 | bellard | } |
6254 | faea38e7 | bellard | |
6255 | faea38e7 | bellard | int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
|
6256 | faea38e7 | bellard | { |
6257 | faea38e7 | bellard | if (whence == SEEK_SET) {
|
6258 | faea38e7 | bellard | /* nothing to do */
|
6259 | faea38e7 | bellard | } else if (whence == SEEK_CUR) { |
6260 | faea38e7 | bellard | pos += qemu_ftell(f); |
6261 | faea38e7 | bellard | } else {
|
6262 | faea38e7 | bellard | /* SEEK_END not supported */
|
6263 | faea38e7 | bellard | return -1; |
6264 | faea38e7 | bellard | } |
6265 | faea38e7 | bellard | if (f->is_writable) {
|
6266 | faea38e7 | bellard | qemu_fflush(f); |
6267 | faea38e7 | bellard | f->buf_offset = pos; |
6268 | faea38e7 | bellard | } else {
|
6269 | faea38e7 | bellard | f->buf_offset = pos; |
6270 | faea38e7 | bellard | f->buf_index = 0;
|
6271 | faea38e7 | bellard | f->buf_size = 0;
|
6272 | faea38e7 | bellard | } |
6273 | faea38e7 | bellard | return pos;
|
6274 | 8a7ddc38 | bellard | } |
6275 | 8a7ddc38 | bellard | |
6276 | 8a7ddc38 | bellard | void qemu_put_be16(QEMUFile *f, unsigned int v) |
6277 | 8a7ddc38 | bellard | { |
6278 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 8);
|
6279 | 8a7ddc38 | bellard | qemu_put_byte(f, v); |
6280 | 8a7ddc38 | bellard | } |
6281 | 8a7ddc38 | bellard | |
6282 | 8a7ddc38 | bellard | void qemu_put_be32(QEMUFile *f, unsigned int v) |
6283 | 8a7ddc38 | bellard | { |
6284 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 24);
|
6285 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 16);
|
6286 | 8a7ddc38 | bellard | qemu_put_byte(f, v >> 8);
|
6287 | 8a7ddc38 | bellard | qemu_put_byte(f, v); |
6288 | 8a7ddc38 | bellard | } |
6289 | 8a7ddc38 | bellard | |
6290 | 8a7ddc38 | bellard | void qemu_put_be64(QEMUFile *f, uint64_t v)
|
6291 | 8a7ddc38 | bellard | { |
6292 | 8a7ddc38 | bellard | qemu_put_be32(f, v >> 32);
|
6293 | 8a7ddc38 | bellard | qemu_put_be32(f, v); |
6294 | 8a7ddc38 | bellard | } |
6295 | 8a7ddc38 | bellard | |
6296 | 8a7ddc38 | bellard | unsigned int qemu_get_be16(QEMUFile *f) |
6297 | 8a7ddc38 | bellard | { |
6298 | 8a7ddc38 | bellard | unsigned int v; |
6299 | 8a7ddc38 | bellard | v = qemu_get_byte(f) << 8;
|
6300 | 8a7ddc38 | bellard | v |= qemu_get_byte(f); |
6301 | 8a7ddc38 | bellard | return v;
|
6302 | 8a7ddc38 | bellard | } |
6303 | 8a7ddc38 | bellard | |
6304 | 8a7ddc38 | bellard | unsigned int qemu_get_be32(QEMUFile *f) |
6305 | 8a7ddc38 | bellard | { |
6306 | 8a7ddc38 | bellard | unsigned int v; |
6307 | 8a7ddc38 | bellard | v = qemu_get_byte(f) << 24;
|
6308 | 8a7ddc38 | bellard | v |= qemu_get_byte(f) << 16;
|
6309 | 8a7ddc38 | bellard | v |= qemu_get_byte(f) << 8;
|
6310 | 8a7ddc38 | bellard | v |= qemu_get_byte(f); |
6311 | 8a7ddc38 | bellard | return v;
|
6312 | 8a7ddc38 | bellard | } |
6313 | 8a7ddc38 | bellard | |
6314 | 8a7ddc38 | bellard | uint64_t qemu_get_be64(QEMUFile *f) |
6315 | 8a7ddc38 | bellard | { |
6316 | 8a7ddc38 | bellard | uint64_t v; |
6317 | 8a7ddc38 | bellard | v = (uint64_t)qemu_get_be32(f) << 32;
|
6318 | 8a7ddc38 | bellard | v |= qemu_get_be32(f); |
6319 | 8a7ddc38 | bellard | return v;
|
6320 | 8a7ddc38 | bellard | } |
6321 | 8a7ddc38 | bellard | |
6322 | 8a7ddc38 | bellard | typedef struct SaveStateEntry { |
6323 | 8a7ddc38 | bellard | char idstr[256]; |
6324 | 8a7ddc38 | bellard | int instance_id;
|
6325 | 8a7ddc38 | bellard | int version_id;
|
6326 | 8a7ddc38 | bellard | SaveStateHandler *save_state; |
6327 | 8a7ddc38 | bellard | LoadStateHandler *load_state; |
6328 | 8a7ddc38 | bellard | void *opaque;
|
6329 | 8a7ddc38 | bellard | struct SaveStateEntry *next;
|
6330 | 8a7ddc38 | bellard | } SaveStateEntry; |
6331 | b4608c04 | bellard | |
6332 | 8a7ddc38 | bellard | static SaveStateEntry *first_se;
|
6333 | 8a7ddc38 | bellard | |
6334 | 18be5187 | pbrook | /* TODO: Individual devices generally have very little idea about the rest
|
6335 | 18fdb1c5 | ths | of the system, so instance_id should be removed/replaced.
|
6336 | 18fdb1c5 | ths | Meanwhile pass -1 as instance_id if you do not already have a clearly
|
6337 | 18fdb1c5 | ths | distinguishing id for all instances of your device class. */
|
6338 | 5fafdf24 | ths | int register_savevm(const char *idstr, |
6339 | 5fafdf24 | ths | int instance_id,
|
6340 | 8a7ddc38 | bellard | int version_id,
|
6341 | 8a7ddc38 | bellard | SaveStateHandler *save_state, |
6342 | 8a7ddc38 | bellard | LoadStateHandler *load_state, |
6343 | 8a7ddc38 | bellard | void *opaque)
|
6344 | 8a7ddc38 | bellard | { |
6345 | 8a7ddc38 | bellard | SaveStateEntry *se, **pse; |
6346 | 8a7ddc38 | bellard | |
6347 | 8a7ddc38 | bellard | se = qemu_malloc(sizeof(SaveStateEntry));
|
6348 | 8a7ddc38 | bellard | if (!se)
|
6349 | 8a7ddc38 | bellard | return -1; |
6350 | 8a7ddc38 | bellard | pstrcpy(se->idstr, sizeof(se->idstr), idstr);
|
6351 | 18be5187 | pbrook | se->instance_id = (instance_id == -1) ? 0 : instance_id; |
6352 | 8a7ddc38 | bellard | se->version_id = version_id; |
6353 | 8a7ddc38 | bellard | se->save_state = save_state; |
6354 | 8a7ddc38 | bellard | se->load_state = load_state; |
6355 | 8a7ddc38 | bellard | se->opaque = opaque; |
6356 | 8a7ddc38 | bellard | se->next = NULL;
|
6357 | 8a7ddc38 | bellard | |
6358 | 8a7ddc38 | bellard | /* add at the end of list */
|
6359 | 8a7ddc38 | bellard | pse = &first_se; |
6360 | 18be5187 | pbrook | while (*pse != NULL) { |
6361 | 18be5187 | pbrook | if (instance_id == -1 |
6362 | 18be5187 | pbrook | && strcmp(se->idstr, (*pse)->idstr) == 0
|
6363 | 18be5187 | pbrook | && se->instance_id <= (*pse)->instance_id) |
6364 | 18be5187 | pbrook | se->instance_id = (*pse)->instance_id + 1;
|
6365 | 8a7ddc38 | bellard | pse = &(*pse)->next; |
6366 | 18be5187 | pbrook | } |
6367 | 8a7ddc38 | bellard | *pse = se; |
6368 | 8a7ddc38 | bellard | return 0; |
6369 | 8a7ddc38 | bellard | } |
6370 | 8a7ddc38 | bellard | |
6371 | 8a7ddc38 | bellard | #define QEMU_VM_FILE_MAGIC 0x5145564d |
6372 | faea38e7 | bellard | #define QEMU_VM_FILE_VERSION 0x00000002 |
6373 | 8a7ddc38 | bellard | |
6374 | 9596ebb7 | pbrook | static int qemu_savevm_state(QEMUFile *f) |
6375 | 8a7ddc38 | bellard | { |
6376 | 8a7ddc38 | bellard | SaveStateEntry *se; |
6377 | faea38e7 | bellard | int len, ret;
|
6378 | faea38e7 | bellard | int64_t cur_pos, len_pos, total_len_pos; |
6379 | 313aa567 | bellard | |
6380 | 8a7ddc38 | bellard | qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
6381 | 8a7ddc38 | bellard | qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
6382 | faea38e7 | bellard | total_len_pos = qemu_ftell(f); |
6383 | faea38e7 | bellard | qemu_put_be64(f, 0); /* total size */ |
6384 | 8a7ddc38 | bellard | |
6385 | 8a7ddc38 | bellard | for(se = first_se; se != NULL; se = se->next) { |
6386 | d978c02c | aurel32 | if (se->save_state == NULL) |
6387 | d978c02c | aurel32 | /* this one has a loader only, for backwards compatibility */
|
6388 | d978c02c | aurel32 | continue;
|
6389 | d978c02c | aurel32 | |
6390 | 8a7ddc38 | bellard | /* ID string */
|
6391 | 8a7ddc38 | bellard | len = strlen(se->idstr); |
6392 | 8a7ddc38 | bellard | qemu_put_byte(f, len); |
6393 | ffe8ab83 | ths | qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
6394 | 8a7ddc38 | bellard | |
6395 | 8a7ddc38 | bellard | qemu_put_be32(f, se->instance_id); |
6396 | 8a7ddc38 | bellard | qemu_put_be32(f, se->version_id); |
6397 | 8a7ddc38 | bellard | |
6398 | 8a7ddc38 | bellard | /* record size: filled later */
|
6399 | faea38e7 | bellard | len_pos = qemu_ftell(f); |
6400 | 8a7ddc38 | bellard | qemu_put_be32(f, 0);
|
6401 | 8a7ddc38 | bellard | se->save_state(f, se->opaque); |
6402 | 8a7ddc38 | bellard | |
6403 | 8a7ddc38 | bellard | /* fill record size */
|
6404 | faea38e7 | bellard | cur_pos = qemu_ftell(f); |
6405 | faea38e7 | bellard | len = cur_pos - len_pos - 4;
|
6406 | faea38e7 | bellard | qemu_fseek(f, len_pos, SEEK_SET); |
6407 | 8a7ddc38 | bellard | qemu_put_be32(f, len); |
6408 | faea38e7 | bellard | qemu_fseek(f, cur_pos, SEEK_SET); |
6409 | 8a7ddc38 | bellard | } |
6410 | faea38e7 | bellard | cur_pos = qemu_ftell(f); |
6411 | faea38e7 | bellard | qemu_fseek(f, total_len_pos, SEEK_SET); |
6412 | faea38e7 | bellard | qemu_put_be64(f, cur_pos - total_len_pos - 8);
|
6413 | faea38e7 | bellard | qemu_fseek(f, cur_pos, SEEK_SET); |
6414 | 8a7ddc38 | bellard | |
6415 | 8a7ddc38 | bellard | ret = 0;
|
6416 | 8a7ddc38 | bellard | return ret;
|
6417 | 8a7ddc38 | bellard | } |
6418 | 8a7ddc38 | bellard | |
6419 | 8a7ddc38 | bellard | static SaveStateEntry *find_se(const char *idstr, int instance_id) |
6420 | 8a7ddc38 | bellard | { |
6421 | 8a7ddc38 | bellard | SaveStateEntry *se; |
6422 | 8a7ddc38 | bellard | |
6423 | 8a7ddc38 | bellard | for(se = first_se; se != NULL; se = se->next) { |
6424 | 5fafdf24 | ths | if (!strcmp(se->idstr, idstr) &&
|
6425 | 8a7ddc38 | bellard | instance_id == se->instance_id) |
6426 | 8a7ddc38 | bellard | return se;
|
6427 | 8a7ddc38 | bellard | } |
6428 | 8a7ddc38 | bellard | return NULL; |
6429 | 8a7ddc38 | bellard | } |
6430 | 8a7ddc38 | bellard | |
6431 | 9596ebb7 | pbrook | static int qemu_loadvm_state(QEMUFile *f) |
6432 | 8a7ddc38 | bellard | { |
6433 | 8a7ddc38 | bellard | SaveStateEntry *se; |
6434 | faea38e7 | bellard | int len, ret, instance_id, record_len, version_id;
|
6435 | faea38e7 | bellard | int64_t total_len, end_pos, cur_pos; |
6436 | 8a7ddc38 | bellard | unsigned int v; |
6437 | 8a7ddc38 | bellard | char idstr[256]; |
6438 | 3b46e624 | ths | |
6439 | 8a7ddc38 | bellard | v = qemu_get_be32(f); |
6440 | 8a7ddc38 | bellard | if (v != QEMU_VM_FILE_MAGIC)
|
6441 | 8a7ddc38 | bellard | goto fail;
|
6442 | 8a7ddc38 | bellard | v = qemu_get_be32(f); |
6443 | 8a7ddc38 | bellard | if (v != QEMU_VM_FILE_VERSION) {
|
6444 | 8a7ddc38 | bellard | fail:
|
6445 | 8a7ddc38 | bellard | ret = -1;
|
6446 | 8a7ddc38 | bellard | goto the_end;
|
6447 | 8a7ddc38 | bellard | } |
6448 | faea38e7 | bellard | total_len = qemu_get_be64(f); |
6449 | faea38e7 | bellard | end_pos = total_len + qemu_ftell(f); |
6450 | b4608c04 | bellard | for(;;) {
|
6451 | faea38e7 | bellard | if (qemu_ftell(f) >= end_pos)
|
6452 | cd4c3e88 | bellard | break;
|
6453 | faea38e7 | bellard | len = qemu_get_byte(f); |
6454 | ffe8ab83 | ths | qemu_get_buffer(f, (uint8_t *)idstr, len); |
6455 | 8a7ddc38 | bellard | idstr[len] = '\0';
|
6456 | 8a7ddc38 | bellard | instance_id = qemu_get_be32(f); |
6457 | 8a7ddc38 | bellard | version_id = qemu_get_be32(f); |
6458 | 8a7ddc38 | bellard | record_len = qemu_get_be32(f); |
6459 | 8a7ddc38 | bellard | #if 0
|
6460 | 5fafdf24 | ths | printf("idstr=%s instance=0x%x version=%d len=%d\n",
|
6461 | 8a7ddc38 | bellard | idstr, instance_id, version_id, record_len);
|
6462 | 8a7ddc38 | bellard | #endif
|
6463 | faea38e7 | bellard | cur_pos = qemu_ftell(f); |
6464 | 8a7ddc38 | bellard | se = find_se(idstr, instance_id); |
6465 | 8a7ddc38 | bellard | if (!se) {
|
6466 | 5fafdf24 | ths | fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
|
6467 | 8a7ddc38 | bellard | instance_id, idstr); |
6468 | 8a7ddc38 | bellard | } else {
|
6469 | 8a7ddc38 | bellard | ret = se->load_state(f, se->opaque, version_id); |
6470 | 8a7ddc38 | bellard | if (ret < 0) { |
6471 | 5fafdf24 | ths | fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
|
6472 | 8a7ddc38 | bellard | instance_id, idstr); |
6473 | 8a7ddc38 | bellard | } |
6474 | 34865134 | bellard | } |
6475 | 8a7ddc38 | bellard | /* always seek to exact end of record */
|
6476 | 8a7ddc38 | bellard | qemu_fseek(f, cur_pos + record_len, SEEK_SET); |
6477 | 8a7ddc38 | bellard | } |
6478 | 8a7ddc38 | bellard | ret = 0;
|
6479 | 8a7ddc38 | bellard | the_end:
|
6480 | faea38e7 | bellard | return ret;
|
6481 | faea38e7 | bellard | } |
6482 | faea38e7 | bellard | |
6483 | faea38e7 | bellard | /* device can contain snapshots */
|
6484 | faea38e7 | bellard | static int bdrv_can_snapshot(BlockDriverState *bs) |
6485 | faea38e7 | bellard | { |
6486 | faea38e7 | bellard | return (bs &&
|
6487 | faea38e7 | bellard | !bdrv_is_removable(bs) && |
6488 | faea38e7 | bellard | !bdrv_is_read_only(bs)); |
6489 | faea38e7 | bellard | } |
6490 | faea38e7 | bellard | |
6491 | faea38e7 | bellard | /* device must be snapshots in order to have a reliable snapshot */
|
6492 | faea38e7 | bellard | static int bdrv_has_snapshot(BlockDriverState *bs) |
6493 | faea38e7 | bellard | { |
6494 | faea38e7 | bellard | return (bs &&
|
6495 | faea38e7 | bellard | !bdrv_is_removable(bs) && |
6496 | faea38e7 | bellard | !bdrv_is_read_only(bs)); |
6497 | faea38e7 | bellard | } |
6498 | faea38e7 | bellard | |
6499 | faea38e7 | bellard | static BlockDriverState *get_bs_snapshots(void) |
6500 | faea38e7 | bellard | { |
6501 | faea38e7 | bellard | BlockDriverState *bs; |
6502 | faea38e7 | bellard | int i;
|
6503 | faea38e7 | bellard | |
6504 | faea38e7 | bellard | if (bs_snapshots)
|
6505 | faea38e7 | bellard | return bs_snapshots;
|
6506 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6507 | e4bcb14c | ths | bs = drives_table[i].bdrv; |
6508 | faea38e7 | bellard | if (bdrv_can_snapshot(bs))
|
6509 | faea38e7 | bellard | goto ok;
|
6510 | faea38e7 | bellard | } |
6511 | faea38e7 | bellard | return NULL; |
6512 | faea38e7 | bellard | ok:
|
6513 | faea38e7 | bellard | bs_snapshots = bs; |
6514 | faea38e7 | bellard | return bs;
|
6515 | faea38e7 | bellard | } |
6516 | faea38e7 | bellard | |
6517 | faea38e7 | bellard | static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
6518 | faea38e7 | bellard | const char *name) |
6519 | faea38e7 | bellard | { |
6520 | faea38e7 | bellard | QEMUSnapshotInfo *sn_tab, *sn; |
6521 | faea38e7 | bellard | int nb_sns, i, ret;
|
6522 | 3b46e624 | ths | |
6523 | faea38e7 | bellard | ret = -ENOENT; |
6524 | faea38e7 | bellard | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
6525 | faea38e7 | bellard | if (nb_sns < 0) |
6526 | faea38e7 | bellard | return ret;
|
6527 | faea38e7 | bellard | for(i = 0; i < nb_sns; i++) { |
6528 | faea38e7 | bellard | sn = &sn_tab[i]; |
6529 | faea38e7 | bellard | if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
|
6530 | faea38e7 | bellard | *sn_info = *sn; |
6531 | faea38e7 | bellard | ret = 0;
|
6532 | faea38e7 | bellard | break;
|
6533 | faea38e7 | bellard | } |
6534 | faea38e7 | bellard | } |
6535 | faea38e7 | bellard | qemu_free(sn_tab); |
6536 | faea38e7 | bellard | return ret;
|
6537 | faea38e7 | bellard | } |
6538 | faea38e7 | bellard | |
6539 | faea38e7 | bellard | void do_savevm(const char *name) |
6540 | faea38e7 | bellard | { |
6541 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6542 | faea38e7 | bellard | QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; |
6543 | faea38e7 | bellard | int must_delete, ret, i;
|
6544 | faea38e7 | bellard | BlockDriverInfo bdi1, *bdi = &bdi1; |
6545 | faea38e7 | bellard | QEMUFile *f; |
6546 | faea38e7 | bellard | int saved_vm_running;
|
6547 | 4c279bdf | bellard | #ifdef _WIN32
|
6548 | 4c279bdf | bellard | struct _timeb tb;
|
6549 | 4c279bdf | bellard | #else
|
6550 | faea38e7 | bellard | struct timeval tv;
|
6551 | 4c279bdf | bellard | #endif
|
6552 | faea38e7 | bellard | |
6553 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6554 | faea38e7 | bellard | if (!bs) {
|
6555 | faea38e7 | bellard | term_printf("No block device can accept snapshots\n");
|
6556 | faea38e7 | bellard | return;
|
6557 | faea38e7 | bellard | } |
6558 | faea38e7 | bellard | |
6559 | 6192bc37 | pbrook | /* ??? Should this occur after vm_stop? */
|
6560 | 6192bc37 | pbrook | qemu_aio_flush(); |
6561 | 6192bc37 | pbrook | |
6562 | faea38e7 | bellard | saved_vm_running = vm_running; |
6563 | faea38e7 | bellard | vm_stop(0);
|
6564 | 3b46e624 | ths | |
6565 | faea38e7 | bellard | must_delete = 0;
|
6566 | faea38e7 | bellard | if (name) {
|
6567 | faea38e7 | bellard | ret = bdrv_snapshot_find(bs, old_sn, name); |
6568 | faea38e7 | bellard | if (ret >= 0) { |
6569 | faea38e7 | bellard | must_delete = 1;
|
6570 | faea38e7 | bellard | } |
6571 | faea38e7 | bellard | } |
6572 | faea38e7 | bellard | memset(sn, 0, sizeof(*sn)); |
6573 | faea38e7 | bellard | if (must_delete) {
|
6574 | faea38e7 | bellard | pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
|
6575 | faea38e7 | bellard | pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
|
6576 | faea38e7 | bellard | } else {
|
6577 | faea38e7 | bellard | if (name)
|
6578 | faea38e7 | bellard | pstrcpy(sn->name, sizeof(sn->name), name);
|
6579 | faea38e7 | bellard | } |
6580 | faea38e7 | bellard | |
6581 | faea38e7 | bellard | /* fill auxiliary fields */
|
6582 | 4c279bdf | bellard | #ifdef _WIN32
|
6583 | 4c279bdf | bellard | _ftime(&tb); |
6584 | 4c279bdf | bellard | sn->date_sec = tb.time; |
6585 | 4c279bdf | bellard | sn->date_nsec = tb.millitm * 1000000;
|
6586 | 4c279bdf | bellard | #else
|
6587 | faea38e7 | bellard | gettimeofday(&tv, NULL);
|
6588 | faea38e7 | bellard | sn->date_sec = tv.tv_sec; |
6589 | faea38e7 | bellard | sn->date_nsec = tv.tv_usec * 1000;
|
6590 | 4c279bdf | bellard | #endif
|
6591 | faea38e7 | bellard | sn->vm_clock_nsec = qemu_get_clock(vm_clock); |
6592 | 3b46e624 | ths | |
6593 | faea38e7 | bellard | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
6594 | faea38e7 | bellard | term_printf("Device %s does not support VM state snapshots\n",
|
6595 | faea38e7 | bellard | bdrv_get_device_name(bs)); |
6596 | faea38e7 | bellard | goto the_end;
|
6597 | faea38e7 | bellard | } |
6598 | 3b46e624 | ths | |
6599 | faea38e7 | bellard | /* save the VM state */
|
6600 | faea38e7 | bellard | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
|
6601 | faea38e7 | bellard | if (!f) {
|
6602 | faea38e7 | bellard | term_printf("Could not open VM state file\n");
|
6603 | faea38e7 | bellard | goto the_end;
|
6604 | faea38e7 | bellard | } |
6605 | faea38e7 | bellard | ret = qemu_savevm_state(f); |
6606 | faea38e7 | bellard | sn->vm_state_size = qemu_ftell(f); |
6607 | faea38e7 | bellard | qemu_fclose(f); |
6608 | faea38e7 | bellard | if (ret < 0) { |
6609 | faea38e7 | bellard | term_printf("Error %d while writing VM\n", ret);
|
6610 | faea38e7 | bellard | goto the_end;
|
6611 | faea38e7 | bellard | } |
6612 | 3b46e624 | ths | |
6613 | faea38e7 | bellard | /* create the snapshots */
|
6614 | faea38e7 | bellard | |
6615 | e4bcb14c | ths | for(i = 0; i < nb_drives; i++) { |
6616 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6617 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6618 | faea38e7 | bellard | if (must_delete) {
|
6619 | faea38e7 | bellard | ret = bdrv_snapshot_delete(bs1, old_sn->id_str); |
6620 | faea38e7 | bellard | if (ret < 0) { |
6621 | faea38e7 | bellard | term_printf("Error while deleting snapshot on '%s'\n",
|
6622 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6623 | faea38e7 | bellard | } |
6624 | faea38e7 | bellard | } |
6625 | faea38e7 | bellard | ret = bdrv_snapshot_create(bs1, sn); |
6626 | faea38e7 | bellard | if (ret < 0) { |
6627 | faea38e7 | bellard | term_printf("Error while creating snapshot on '%s'\n",
|
6628 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6629 | faea38e7 | bellard | } |
6630 | faea38e7 | bellard | } |
6631 | faea38e7 | bellard | } |
6632 | faea38e7 | bellard | |
6633 | faea38e7 | bellard | the_end:
|
6634 | 8a7ddc38 | bellard | if (saved_vm_running)
|
6635 | 8a7ddc38 | bellard | vm_start(); |
6636 | faea38e7 | bellard | } |
6637 | faea38e7 | bellard | |
6638 | faea38e7 | bellard | void do_loadvm(const char *name) |
6639 | faea38e7 | bellard | { |
6640 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6641 | faea38e7 | bellard | BlockDriverInfo bdi1, *bdi = &bdi1; |
6642 | faea38e7 | bellard | QEMUFile *f; |
6643 | faea38e7 | bellard | int i, ret;
|
6644 | faea38e7 | bellard | int saved_vm_running;
|
6645 | faea38e7 | bellard | |
6646 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6647 | faea38e7 | bellard | if (!bs) {
|
6648 | faea38e7 | bellard | term_printf("No block device supports snapshots\n");
|
6649 | faea38e7 | bellard | return;
|
6650 | faea38e7 | bellard | } |
6651 | 3b46e624 | ths | |
6652 | 6192bc37 | pbrook | /* Flush all IO requests so they don't interfere with the new state. */
|
6653 | 6192bc37 | pbrook | qemu_aio_flush(); |
6654 | 6192bc37 | pbrook | |
6655 | faea38e7 | bellard | saved_vm_running = vm_running; |
6656 | faea38e7 | bellard | vm_stop(0);
|
6657 | faea38e7 | bellard | |
6658 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6659 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6660 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6661 | faea38e7 | bellard | ret = bdrv_snapshot_goto(bs1, name); |
6662 | faea38e7 | bellard | if (ret < 0) { |
6663 | faea38e7 | bellard | if (bs != bs1)
|
6664 | faea38e7 | bellard | term_printf("Warning: ");
|
6665 | faea38e7 | bellard | switch(ret) {
|
6666 | faea38e7 | bellard | case -ENOTSUP:
|
6667 | faea38e7 | bellard | term_printf("Snapshots not supported on device '%s'\n",
|
6668 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6669 | faea38e7 | bellard | break;
|
6670 | faea38e7 | bellard | case -ENOENT:
|
6671 | faea38e7 | bellard | term_printf("Could not find snapshot '%s' on device '%s'\n",
|
6672 | faea38e7 | bellard | name, bdrv_get_device_name(bs1)); |
6673 | faea38e7 | bellard | break;
|
6674 | faea38e7 | bellard | default:
|
6675 | faea38e7 | bellard | term_printf("Error %d while activating snapshot on '%s'\n",
|
6676 | faea38e7 | bellard | ret, bdrv_get_device_name(bs1)); |
6677 | faea38e7 | bellard | break;
|
6678 | faea38e7 | bellard | } |
6679 | faea38e7 | bellard | /* fatal on snapshot block device */
|
6680 | faea38e7 | bellard | if (bs == bs1)
|
6681 | faea38e7 | bellard | goto the_end;
|
6682 | faea38e7 | bellard | } |
6683 | faea38e7 | bellard | } |
6684 | faea38e7 | bellard | } |
6685 | faea38e7 | bellard | |
6686 | faea38e7 | bellard | if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
6687 | faea38e7 | bellard | term_printf("Device %s does not support VM state snapshots\n",
|
6688 | faea38e7 | bellard | bdrv_get_device_name(bs)); |
6689 | faea38e7 | bellard | return;
|
6690 | faea38e7 | bellard | } |
6691 | 3b46e624 | ths | |
6692 | faea38e7 | bellard | /* restore the VM state */
|
6693 | faea38e7 | bellard | f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
|
6694 | faea38e7 | bellard | if (!f) {
|
6695 | faea38e7 | bellard | term_printf("Could not open VM state file\n");
|
6696 | faea38e7 | bellard | goto the_end;
|
6697 | faea38e7 | bellard | } |
6698 | faea38e7 | bellard | ret = qemu_loadvm_state(f); |
6699 | faea38e7 | bellard | qemu_fclose(f); |
6700 | faea38e7 | bellard | if (ret < 0) { |
6701 | faea38e7 | bellard | term_printf("Error %d while loading VM state\n", ret);
|
6702 | faea38e7 | bellard | } |
6703 | faea38e7 | bellard | the_end:
|
6704 | faea38e7 | bellard | if (saved_vm_running)
|
6705 | faea38e7 | bellard | vm_start(); |
6706 | faea38e7 | bellard | } |
6707 | faea38e7 | bellard | |
6708 | faea38e7 | bellard | void do_delvm(const char *name) |
6709 | faea38e7 | bellard | { |
6710 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6711 | faea38e7 | bellard | int i, ret;
|
6712 | faea38e7 | bellard | |
6713 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6714 | faea38e7 | bellard | if (!bs) {
|
6715 | faea38e7 | bellard | term_printf("No block device supports snapshots\n");
|
6716 | faea38e7 | bellard | return;
|
6717 | faea38e7 | bellard | } |
6718 | 3b46e624 | ths | |
6719 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6720 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6721 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6722 | faea38e7 | bellard | ret = bdrv_snapshot_delete(bs1, name); |
6723 | faea38e7 | bellard | if (ret < 0) { |
6724 | faea38e7 | bellard | if (ret == -ENOTSUP)
|
6725 | faea38e7 | bellard | term_printf("Snapshots not supported on device '%s'\n",
|
6726 | faea38e7 | bellard | bdrv_get_device_name(bs1)); |
6727 | faea38e7 | bellard | else
|
6728 | faea38e7 | bellard | term_printf("Error %d while deleting snapshot on '%s'\n",
|
6729 | faea38e7 | bellard | ret, bdrv_get_device_name(bs1)); |
6730 | faea38e7 | bellard | } |
6731 | faea38e7 | bellard | } |
6732 | faea38e7 | bellard | } |
6733 | faea38e7 | bellard | } |
6734 | faea38e7 | bellard | |
6735 | faea38e7 | bellard | void do_info_snapshots(void) |
6736 | faea38e7 | bellard | { |
6737 | faea38e7 | bellard | BlockDriverState *bs, *bs1; |
6738 | faea38e7 | bellard | QEMUSnapshotInfo *sn_tab, *sn; |
6739 | faea38e7 | bellard | int nb_sns, i;
|
6740 | faea38e7 | bellard | char buf[256]; |
6741 | faea38e7 | bellard | |
6742 | faea38e7 | bellard | bs = get_bs_snapshots(); |
6743 | faea38e7 | bellard | if (!bs) {
|
6744 | faea38e7 | bellard | term_printf("No available block device supports snapshots\n");
|
6745 | faea38e7 | bellard | return;
|
6746 | faea38e7 | bellard | } |
6747 | faea38e7 | bellard | term_printf("Snapshot devices:");
|
6748 | e4bcb14c | ths | for(i = 0; i <= nb_drives; i++) { |
6749 | e4bcb14c | ths | bs1 = drives_table[i].bdrv; |
6750 | faea38e7 | bellard | if (bdrv_has_snapshot(bs1)) {
|
6751 | faea38e7 | bellard | if (bs == bs1)
|
6752 | faea38e7 | bellard | term_printf(" %s", bdrv_get_device_name(bs1));
|
6753 | faea38e7 | bellard | } |
6754 | faea38e7 | bellard | } |
6755 | faea38e7 | bellard | term_printf("\n");
|
6756 | faea38e7 | bellard | |
6757 | faea38e7 | bellard | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
6758 | faea38e7 | bellard | if (nb_sns < 0) { |
6759 | faea38e7 | bellard | term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
|
6760 | faea38e7 | bellard | return;
|
6761 | faea38e7 | bellard | } |
6762 | faea38e7 | bellard | term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
|
6763 | faea38e7 | bellard | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
6764 | faea38e7 | bellard | for(i = 0; i < nb_sns; i++) { |
6765 | faea38e7 | bellard | sn = &sn_tab[i]; |
6766 | faea38e7 | bellard | term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
6767 | faea38e7 | bellard | } |
6768 | faea38e7 | bellard | qemu_free(sn_tab); |
6769 | 8a7ddc38 | bellard | } |
6770 | 8a7ddc38 | bellard | |
6771 | 8a7ddc38 | bellard | /***********************************************************/
|
6772 | 8a7ddc38 | bellard | /* ram save/restore */
|
6773 | 8a7ddc38 | bellard | |
6774 | 8a7ddc38 | bellard | static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
6775 | 8a7ddc38 | bellard | { |
6776 | 8a7ddc38 | bellard | int v;
|
6777 | 8a7ddc38 | bellard | |
6778 | 8a7ddc38 | bellard | v = qemu_get_byte(f); |
6779 | 8a7ddc38 | bellard | switch(v) {
|
6780 | 8a7ddc38 | bellard | case 0: |
6781 | 8a7ddc38 | bellard | if (qemu_get_buffer(f, buf, len) != len)
|
6782 | 8a7ddc38 | bellard | return -EIO;
|
6783 | 8a7ddc38 | bellard | break;
|
6784 | 8a7ddc38 | bellard | case 1: |
6785 | 8a7ddc38 | bellard | v = qemu_get_byte(f); |
6786 | 8a7ddc38 | bellard | memset(buf, v, len); |
6787 | 8a7ddc38 | bellard | break;
|
6788 | 8a7ddc38 | bellard | default:
|
6789 | 8a7ddc38 | bellard | return -EINVAL;
|
6790 | 8a7ddc38 | bellard | } |
6791 | 8a7ddc38 | bellard | return 0; |
6792 | 8a7ddc38 | bellard | } |
6793 | 8a7ddc38 | bellard | |
6794 | c88676f8 | bellard | static int ram_load_v1(QEMUFile *f, void *opaque) |
6795 | c88676f8 | bellard | { |
6796 | 00f82b8a | aurel32 | int ret;
|
6797 | 00f82b8a | aurel32 | ram_addr_t i; |
6798 | c88676f8 | bellard | |
6799 | c88676f8 | bellard | if (qemu_get_be32(f) != phys_ram_size)
|
6800 | c88676f8 | bellard | return -EINVAL;
|
6801 | c88676f8 | bellard | for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
6802 | c88676f8 | bellard | ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
6803 | c88676f8 | bellard | if (ret)
|
6804 | c88676f8 | bellard | return ret;
|
6805 | c88676f8 | bellard | } |
6806 | c88676f8 | bellard | return 0; |
6807 | c88676f8 | bellard | } |
6808 | c88676f8 | bellard | |
6809 | c88676f8 | bellard | #define BDRV_HASH_BLOCK_SIZE 1024 |
6810 | c88676f8 | bellard | #define IOBUF_SIZE 4096 |
6811 | c88676f8 | bellard | #define RAM_CBLOCK_MAGIC 0xfabe |
6812 | c88676f8 | bellard | |
6813 | c88676f8 | bellard | typedef struct RamCompressState { |
6814 | c88676f8 | bellard | z_stream zstream; |
6815 | c88676f8 | bellard | QEMUFile *f; |
6816 | c88676f8 | bellard | uint8_t buf[IOBUF_SIZE]; |
6817 | c88676f8 | bellard | } RamCompressState; |
6818 | c88676f8 | bellard | |
6819 | c88676f8 | bellard | static int ram_compress_open(RamCompressState *s, QEMUFile *f) |
6820 | c88676f8 | bellard | { |
6821 | c88676f8 | bellard | int ret;
|
6822 | c88676f8 | bellard | memset(s, 0, sizeof(*s)); |
6823 | c88676f8 | bellard | s->f = f; |
6824 | c88676f8 | bellard | ret = deflateInit2(&s->zstream, 1,
|
6825 | 5fafdf24 | ths | Z_DEFLATED, 15,
|
6826 | c88676f8 | bellard | 9, Z_DEFAULT_STRATEGY);
|
6827 | c88676f8 | bellard | if (ret != Z_OK)
|
6828 | c88676f8 | bellard | return -1; |
6829 | c88676f8 | bellard | s->zstream.avail_out = IOBUF_SIZE; |
6830 | c88676f8 | bellard | s->zstream.next_out = s->buf; |
6831 | c88676f8 | bellard | return 0; |
6832 | c88676f8 | bellard | } |
6833 | c88676f8 | bellard | |
6834 | c88676f8 | bellard | static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len) |
6835 | c88676f8 | bellard | { |
6836 | c88676f8 | bellard | qemu_put_be16(s->f, RAM_CBLOCK_MAGIC); |
6837 | c88676f8 | bellard | qemu_put_be16(s->f, len); |
6838 | c88676f8 | bellard | qemu_put_buffer(s->f, buf, len); |
6839 | c88676f8 | bellard | } |
6840 | c88676f8 | bellard | |
6841 | c88676f8 | bellard | static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len) |
6842 | c88676f8 | bellard | { |
6843 | c88676f8 | bellard | int ret;
|
6844 | c88676f8 | bellard | |
6845 | c88676f8 | bellard | s->zstream.avail_in = len; |
6846 | c88676f8 | bellard | s->zstream.next_in = (uint8_t *)buf; |
6847 | c88676f8 | bellard | while (s->zstream.avail_in > 0) { |
6848 | c88676f8 | bellard | ret = deflate(&s->zstream, Z_NO_FLUSH); |
6849 | c88676f8 | bellard | if (ret != Z_OK)
|
6850 | c88676f8 | bellard | return -1; |
6851 | c88676f8 | bellard | if (s->zstream.avail_out == 0) { |
6852 | c88676f8 | bellard | ram_put_cblock(s, s->buf, IOBUF_SIZE); |
6853 | c88676f8 | bellard | s->zstream.avail_out = IOBUF_SIZE; |
6854 | c88676f8 | bellard | s->zstream.next_out = s->buf; |
6855 | c88676f8 | bellard | } |
6856 | c88676f8 | bellard | } |
6857 | c88676f8 | bellard | return 0; |
6858 | c88676f8 | bellard | } |
6859 | c88676f8 | bellard | |
6860 | c88676f8 | bellard | static void ram_compress_close(RamCompressState *s) |
6861 | c88676f8 | bellard | { |
6862 | c88676f8 | bellard | int len, ret;
|
6863 | c88676f8 | bellard | |
6864 | c88676f8 | bellard | /* compress last bytes */
|
6865 | c88676f8 | bellard | for(;;) {
|
6866 | c88676f8 | bellard | ret = deflate(&s->zstream, Z_FINISH); |
6867 | c88676f8 | bellard | if (ret == Z_OK || ret == Z_STREAM_END) {
|
6868 | c88676f8 | bellard | len = IOBUF_SIZE - s->zstream.avail_out; |
6869 | c88676f8 | bellard | if (len > 0) { |
6870 | c88676f8 | bellard | ram_put_cblock(s, s->buf, len); |
6871 | c88676f8 | bellard | } |
6872 | c88676f8 | bellard | s->zstream.avail_out = IOBUF_SIZE; |
6873 | c88676f8 | bellard | s->zstream.next_out = s->buf; |
6874 | c88676f8 | bellard | if (ret == Z_STREAM_END)
|
6875 | c88676f8 | bellard | break;
|
6876 | c88676f8 | bellard | } else {
|
6877 | c88676f8 | bellard | goto fail;
|
6878 | c88676f8 | bellard | } |
6879 | c88676f8 | bellard | } |
6880 | c88676f8 | bellard | fail:
|
6881 | c88676f8 | bellard | deflateEnd(&s->zstream); |
6882 | c88676f8 | bellard | } |
6883 | c88676f8 | bellard | |
6884 | c88676f8 | bellard | typedef struct RamDecompressState { |
6885 | c88676f8 | bellard | z_stream zstream; |
6886 | c88676f8 | bellard | QEMUFile *f; |
6887 | c88676f8 | bellard | uint8_t buf[IOBUF_SIZE]; |
6888 | c88676f8 | bellard | } RamDecompressState; |
6889 | c88676f8 | bellard | |
6890 | c88676f8 | bellard | static int ram_decompress_open(RamDecompressState *s, QEMUFile *f) |
6891 | c88676f8 | bellard | { |
6892 | c88676f8 | bellard | int ret;
|
6893 | c88676f8 | bellard | memset(s, 0, sizeof(*s)); |
6894 | c88676f8 | bellard | s->f = f; |
6895 | c88676f8 | bellard | ret = inflateInit(&s->zstream); |
6896 | c88676f8 | bellard | if (ret != Z_OK)
|
6897 | c88676f8 | bellard | return -1; |
6898 | c88676f8 | bellard | return 0; |
6899 | c88676f8 | bellard | } |
6900 | c88676f8 | bellard | |
6901 | c88676f8 | bellard | static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len) |
6902 | c88676f8 | bellard | { |
6903 | c88676f8 | bellard | int ret, clen;
|
6904 | c88676f8 | bellard | |
6905 | c88676f8 | bellard | s->zstream.avail_out = len; |
6906 | c88676f8 | bellard | s->zstream.next_out = buf; |
6907 | c88676f8 | bellard | while (s->zstream.avail_out > 0) { |
6908 | c88676f8 | bellard | if (s->zstream.avail_in == 0) { |
6909 | c88676f8 | bellard | if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
|
6910 | c88676f8 | bellard | return -1; |
6911 | c88676f8 | bellard | clen = qemu_get_be16(s->f); |
6912 | c88676f8 | bellard | if (clen > IOBUF_SIZE)
|
6913 | c88676f8 | bellard | return -1; |
6914 | c88676f8 | bellard | qemu_get_buffer(s->f, s->buf, clen); |
6915 | c88676f8 | bellard | s->zstream.avail_in = clen; |
6916 | c88676f8 | bellard | s->zstream.next_in = s->buf; |
6917 | c88676f8 | bellard | } |
6918 | c88676f8 | bellard | ret = inflate(&s->zstream, Z_PARTIAL_FLUSH); |
6919 | c88676f8 | bellard | if (ret != Z_OK && ret != Z_STREAM_END) {
|
6920 | c88676f8 | bellard | return -1; |
6921 | c88676f8 | bellard | } |
6922 | c88676f8 | bellard | } |
6923 | c88676f8 | bellard | return 0; |
6924 | c88676f8 | bellard | } |
6925 | c88676f8 | bellard | |
6926 | c88676f8 | bellard | static void ram_decompress_close(RamDecompressState *s) |
6927 | c88676f8 | bellard | { |
6928 | c88676f8 | bellard | inflateEnd(&s->zstream); |
6929 | c88676f8 | bellard | } |
6930 | c88676f8 | bellard | |
6931 | 8a7ddc38 | bellard | static void ram_save(QEMUFile *f, void *opaque) |
6932 | 8a7ddc38 | bellard | { |
6933 | 00f82b8a | aurel32 | ram_addr_t i; |
6934 | c88676f8 | bellard | RamCompressState s1, *s = &s1; |
6935 | c88676f8 | bellard | uint8_t buf[10];
|
6936 | 3b46e624 | ths | |
6937 | 8a7ddc38 | bellard | qemu_put_be32(f, phys_ram_size); |
6938 | c88676f8 | bellard | if (ram_compress_open(s, f) < 0) |
6939 | c88676f8 | bellard | return;
|
6940 | c88676f8 | bellard | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
6941 | c88676f8 | bellard | #if 0
|
6942 | c88676f8 | bellard | if (tight_savevm_enabled) {
|
6943 | c88676f8 | bellard | int64_t sector_num;
|
6944 | c88676f8 | bellard | int j;
|
6945 | c88676f8 | bellard | |
6946 | c88676f8 | bellard | /* find if the memory block is available on a virtual
|
6947 | c88676f8 | bellard | block device */
|
6948 | c88676f8 | bellard | sector_num = -1;
|
6949 | e4bcb14c | ths | for(j = 0; j < nb_drives; j++) {
|
6950 | e4bcb14c | ths | sector_num = bdrv_hash_find(drives_table[j].bdrv,
|
6951 | e4bcb14c | ths | phys_ram_base + i,
|
6952 | e4bcb14c | ths | BDRV_HASH_BLOCK_SIZE);
|
6953 | e4bcb14c | ths | if (sector_num >= 0)
|
6954 | e4bcb14c | ths | break;
|
6955 | c88676f8 | bellard | }
|
6956 | e4bcb14c | ths | if (j == nb_drives)
|
6957 | c88676f8 | bellard | goto normal_compress;
|
6958 | c88676f8 | bellard | buf[0] = 1;
|
6959 | c88676f8 | bellard | buf[1] = j;
|
6960 | c88676f8 | bellard | cpu_to_be64wu((uint64_t *)(buf + 2), sector_num);
|
6961 | c88676f8 | bellard | ram_compress_buf(s, buf, 10);
|
6962 | 5fafdf24 | ths | } else
|
6963 | c88676f8 | bellard | #endif
|
6964 | c88676f8 | bellard | { |
6965 | c88676f8 | bellard | // normal_compress:
|
6966 | c88676f8 | bellard | buf[0] = 0; |
6967 | c88676f8 | bellard | ram_compress_buf(s, buf, 1);
|
6968 | c88676f8 | bellard | ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); |
6969 | c88676f8 | bellard | } |
6970 | 8a7ddc38 | bellard | } |
6971 | c88676f8 | bellard | ram_compress_close(s); |
6972 | 8a7ddc38 | bellard | } |
6973 | 8a7ddc38 | bellard | |
6974 | 8a7ddc38 | bellard | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
6975 | 8a7ddc38 | bellard | { |
6976 | c88676f8 | bellard | RamDecompressState s1, *s = &s1; |
6977 | c88676f8 | bellard | uint8_t buf[10];
|
6978 | 00f82b8a | aurel32 | ram_addr_t i; |
6979 | 8a7ddc38 | bellard | |
6980 | c88676f8 | bellard | if (version_id == 1) |
6981 | c88676f8 | bellard | return ram_load_v1(f, opaque);
|
6982 | c88676f8 | bellard | if (version_id != 2) |
6983 | 8a7ddc38 | bellard | return -EINVAL;
|
6984 | 8a7ddc38 | bellard | if (qemu_get_be32(f) != phys_ram_size)
|
6985 | 8a7ddc38 | bellard | return -EINVAL;
|
6986 | c88676f8 | bellard | if (ram_decompress_open(s, f) < 0) |
6987 | c88676f8 | bellard | return -EINVAL;
|
6988 | c88676f8 | bellard | for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
6989 | c88676f8 | bellard | if (ram_decompress_buf(s, buf, 1) < 0) { |
6990 | c88676f8 | bellard | fprintf(stderr, "Error while reading ram block header\n");
|
6991 | c88676f8 | bellard | goto error;
|
6992 | c88676f8 | bellard | } |
6993 | c88676f8 | bellard | if (buf[0] == 0) { |
6994 | c88676f8 | bellard | if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) { |
6995 | 00f82b8a | aurel32 | fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
|
6996 | c88676f8 | bellard | goto error;
|
6997 | c88676f8 | bellard | } |
6998 | 5fafdf24 | ths | } else
|
6999 | c88676f8 | bellard | #if 0
|
7000 | c88676f8 | bellard | if (buf[0] == 1) {
|
7001 | c88676f8 | bellard | int bs_index;
|
7002 | c88676f8 | bellard | int64_t sector_num;
|
7003 | c88676f8 | bellard | |
7004 | c88676f8 | bellard | ram_decompress_buf(s, buf + 1, 9);
|
7005 | c88676f8 | bellard | bs_index = buf[1];
|
7006 | c88676f8 | bellard | sector_num = be64_to_cpupu((const uint64_t *)(buf + 2));
|
7007 | e4bcb14c | ths | if (bs_index >= nb_drives) {
|
7008 | c88676f8 | bellard | fprintf(stderr, "Invalid block device index %d\n", bs_index);
|
7009 | c88676f8 | bellard | goto error;
|
7010 | c88676f8 | bellard | }
|
7011 | e4bcb14c | ths | if (bdrv_read(drives_table[bs_index].bdrv, sector_num,
|
7012 | e4bcb14c | ths | phys_ram_base + i,
|
7013 | c88676f8 | bellard | BDRV_HASH_BLOCK_SIZE / 512) < 0) {
|
7014 | 5fafdf24 | ths | fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n",
|
7015 | c88676f8 | bellard | bs_index, sector_num);
|
7016 | c88676f8 | bellard | goto error;
|
7017 | c88676f8 | bellard | }
|
7018 | 5fafdf24 | ths | } else
|
7019 | c88676f8 | bellard | #endif
|
7020 | c88676f8 | bellard | { |
7021 | c88676f8 | bellard | error:
|
7022 | c88676f8 | bellard | printf("Error block header\n");
|
7023 | c88676f8 | bellard | return -EINVAL;
|
7024 | c88676f8 | bellard | } |
7025 | 8a7ddc38 | bellard | } |
7026 | c88676f8 | bellard | ram_decompress_close(s); |
7027 | 8a7ddc38 | bellard | return 0; |
7028 | 8a7ddc38 | bellard | } |
7029 | 8a7ddc38 | bellard | |
7030 | 8a7ddc38 | bellard | /***********************************************************/
|
7031 | 83f64091 | bellard | /* bottom halves (can be seen as timers which expire ASAP) */
|
7032 | 83f64091 | bellard | |
7033 | 83f64091 | bellard | struct QEMUBH {
|
7034 | 83f64091 | bellard | QEMUBHFunc *cb; |
7035 | 83f64091 | bellard | void *opaque;
|
7036 | 83f64091 | bellard | int scheduled;
|
7037 | 83f64091 | bellard | QEMUBH *next; |
7038 | 83f64091 | bellard | }; |
7039 | 83f64091 | bellard | |
7040 | 83f64091 | bellard | static QEMUBH *first_bh = NULL; |
7041 | 83f64091 | bellard | |
7042 | 83f64091 | bellard | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
|
7043 | 83f64091 | bellard | { |
7044 | 83f64091 | bellard | QEMUBH *bh; |
7045 | 83f64091 | bellard | bh = qemu_mallocz(sizeof(QEMUBH));
|
7046 | 83f64091 | bellard | if (!bh)
|
7047 | 83f64091 | bellard | return NULL; |
7048 | 83f64091 | bellard | bh->cb = cb; |
7049 | 83f64091 | bellard | bh->opaque = opaque; |
7050 | 83f64091 | bellard | return bh;
|
7051 | 83f64091 | bellard | } |
7052 | 83f64091 | bellard | |
7053 | 6eb5733a | bellard | int qemu_bh_poll(void) |
7054 | 83f64091 | bellard | { |
7055 | 83f64091 | bellard | QEMUBH *bh, **pbh; |
7056 | 6eb5733a | bellard | int ret;
|
7057 | 83f64091 | bellard | |
7058 | 6eb5733a | bellard | ret = 0;
|
7059 | 83f64091 | bellard | for(;;) {
|
7060 | 83f64091 | bellard | pbh = &first_bh; |
7061 | 83f64091 | bellard | bh = *pbh; |
7062 | 83f64091 | bellard | if (!bh)
|
7063 | 83f64091 | bellard | break;
|
7064 | 6eb5733a | bellard | ret = 1;
|
7065 | 83f64091 | bellard | *pbh = bh->next; |
7066 | 83f64091 | bellard | bh->scheduled = 0;
|
7067 | 83f64091 | bellard | bh->cb(bh->opaque); |
7068 | 83f64091 | bellard | } |
7069 | 6eb5733a | bellard | return ret;
|
7070 | 83f64091 | bellard | } |
7071 | 83f64091 | bellard | |
7072 | 83f64091 | bellard | void qemu_bh_schedule(QEMUBH *bh)
|
7073 | 83f64091 | bellard | { |
7074 | 83f64091 | bellard | CPUState *env = cpu_single_env; |
7075 | 83f64091 | bellard | if (bh->scheduled)
|
7076 | 83f64091 | bellard | return;
|
7077 | 83f64091 | bellard | bh->scheduled = 1;
|
7078 | 83f64091 | bellard | bh->next = first_bh; |
7079 | 83f64091 | bellard | first_bh = bh; |
7080 | 83f64091 | bellard | |
7081 | 83f64091 | bellard | /* stop the currently executing CPU to execute the BH ASAP */
|
7082 | 83f64091 | bellard | if (env) {
|
7083 | 83f64091 | bellard | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
7084 | 83f64091 | bellard | } |
7085 | 83f64091 | bellard | } |
7086 | 83f64091 | bellard | |
7087 | 83f64091 | bellard | void qemu_bh_cancel(QEMUBH *bh)
|
7088 | 83f64091 | bellard | { |
7089 | 83f64091 | bellard | QEMUBH **pbh; |
7090 | 83f64091 | bellard | if (bh->scheduled) {
|
7091 | 83f64091 | bellard | pbh = &first_bh; |
7092 | 83f64091 | bellard | while (*pbh != bh)
|
7093 | 83f64091 | bellard | pbh = &(*pbh)->next; |
7094 | 83f64091 | bellard | *pbh = bh->next; |
7095 | 83f64091 | bellard | bh->scheduled = 0;
|
7096 | 83f64091 | bellard | } |
7097 | 83f64091 | bellard | } |
7098 | 83f64091 | bellard | |
7099 | 83f64091 | bellard | void qemu_bh_delete(QEMUBH *bh)
|
7100 | 83f64091 | bellard | { |
7101 | 83f64091 | bellard | qemu_bh_cancel(bh); |
7102 | 83f64091 | bellard | qemu_free(bh); |
7103 | 83f64091 | bellard | } |
7104 | 83f64091 | bellard | |
7105 | 83f64091 | bellard | /***********************************************************/
|
7106 | cc1daa40 | bellard | /* machine registration */
|
7107 | cc1daa40 | bellard | |
7108 | cc1daa40 | bellard | QEMUMachine *first_machine = NULL;
|
7109 | cc1daa40 | bellard | |
7110 | cc1daa40 | bellard | int qemu_register_machine(QEMUMachine *m)
|
7111 | cc1daa40 | bellard | { |
7112 | cc1daa40 | bellard | QEMUMachine **pm; |
7113 | cc1daa40 | bellard | pm = &first_machine; |
7114 | cc1daa40 | bellard | while (*pm != NULL) |
7115 | cc1daa40 | bellard | pm = &(*pm)->next; |
7116 | cc1daa40 | bellard | m->next = NULL;
|
7117 | cc1daa40 | bellard | *pm = m; |
7118 | cc1daa40 | bellard | return 0; |
7119 | cc1daa40 | bellard | } |
7120 | cc1daa40 | bellard | |
7121 | 9596ebb7 | pbrook | static QEMUMachine *find_machine(const char *name) |
7122 | cc1daa40 | bellard | { |
7123 | cc1daa40 | bellard | QEMUMachine *m; |
7124 | cc1daa40 | bellard | |
7125 | cc1daa40 | bellard | for(m = first_machine; m != NULL; m = m->next) { |
7126 | cc1daa40 | bellard | if (!strcmp(m->name, name))
|
7127 | cc1daa40 | bellard | return m;
|
7128 | cc1daa40 | bellard | } |
7129 | cc1daa40 | bellard | return NULL; |
7130 | cc1daa40 | bellard | } |
7131 | cc1daa40 | bellard | |
7132 | cc1daa40 | bellard | /***********************************************************/
|
7133 | 8a7ddc38 | bellard | /* main execution loop */
|
7134 | 8a7ddc38 | bellard | |
7135 | 9596ebb7 | pbrook | static void gui_update(void *opaque) |
7136 | 8a7ddc38 | bellard | { |
7137 | 740733bb | ths | DisplayState *ds = opaque; |
7138 | 740733bb | ths | ds->dpy_refresh(ds); |
7139 | f442e08b | aurel32 | qemu_mod_timer(ds->gui_timer, |
7140 | f442e08b | aurel32 | (ds->gui_timer_interval ? |
7141 | f442e08b | aurel32 | ds->gui_timer_interval : |
7142 | f442e08b | aurel32 | GUI_REFRESH_INTERVAL) |
7143 | f442e08b | aurel32 | + qemu_get_clock(rt_clock)); |
7144 | 8a7ddc38 | bellard | } |
7145 | 8a7ddc38 | bellard | |
7146 | 0bd48850 | bellard | struct vm_change_state_entry {
|
7147 | 0bd48850 | bellard | VMChangeStateHandler *cb; |
7148 | 0bd48850 | bellard | void *opaque;
|
7149 | 0bd48850 | bellard | LIST_ENTRY (vm_change_state_entry) entries; |
7150 | 0bd48850 | bellard | }; |
7151 | 0bd48850 | bellard | |
7152 | 0bd48850 | bellard | static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
|
7153 | 0bd48850 | bellard | |
7154 | 0bd48850 | bellard | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
7155 | 0bd48850 | bellard | void *opaque)
|
7156 | 0bd48850 | bellard | { |
7157 | 0bd48850 | bellard | VMChangeStateEntry *e; |
7158 | 0bd48850 | bellard | |
7159 | 0bd48850 | bellard | e = qemu_mallocz(sizeof (*e));
|
7160 | 0bd48850 | bellard | if (!e)
|
7161 | 0bd48850 | bellard | return NULL; |
7162 | 0bd48850 | bellard | |
7163 | 0bd48850 | bellard | e->cb = cb; |
7164 | 0bd48850 | bellard | e->opaque = opaque; |
7165 | 0bd48850 | bellard | LIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
7166 | 0bd48850 | bellard | return e;
|
7167 | 0bd48850 | bellard | } |
7168 | 0bd48850 | bellard | |
7169 | 0bd48850 | bellard | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
|
7170 | 0bd48850 | bellard | { |
7171 | 0bd48850 | bellard | LIST_REMOVE (e, entries); |
7172 | 0bd48850 | bellard | qemu_free (e); |
7173 | 0bd48850 | bellard | } |
7174 | 0bd48850 | bellard | |
7175 | 0bd48850 | bellard | static void vm_state_notify(int running) |
7176 | 0bd48850 | bellard | { |
7177 | 0bd48850 | bellard | VMChangeStateEntry *e; |
7178 | 0bd48850 | bellard | |
7179 | 0bd48850 | bellard | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
|
7180 | 0bd48850 | bellard | e->cb(e->opaque, running); |
7181 | 0bd48850 | bellard | } |
7182 | 0bd48850 | bellard | } |
7183 | 0bd48850 | bellard | |
7184 | 8a7ddc38 | bellard | /* XXX: support several handlers */
|
7185 | 0bd48850 | bellard | static VMStopHandler *vm_stop_cb;
|
7186 | 0bd48850 | bellard | static void *vm_stop_opaque; |
7187 | 8a7ddc38 | bellard | |
7188 | 8a7ddc38 | bellard | int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) |
7189 | 8a7ddc38 | bellard | { |
7190 | 8a7ddc38 | bellard | vm_stop_cb = cb; |
7191 | 8a7ddc38 | bellard | vm_stop_opaque = opaque; |
7192 | 8a7ddc38 | bellard | return 0; |
7193 | 8a7ddc38 | bellard | } |
7194 | 8a7ddc38 | bellard | |
7195 | 8a7ddc38 | bellard | void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) |
7196 | 8a7ddc38 | bellard | { |
7197 | 8a7ddc38 | bellard | vm_stop_cb = NULL;
|
7198 | 8a7ddc38 | bellard | } |
7199 | 8a7ddc38 | bellard | |
7200 | 8a7ddc38 | bellard | void vm_start(void) |
7201 | 8a7ddc38 | bellard | { |
7202 | 8a7ddc38 | bellard | if (!vm_running) {
|
7203 | 8a7ddc38 | bellard | cpu_enable_ticks(); |
7204 | 8a7ddc38 | bellard | vm_running = 1;
|
7205 | 0bd48850 | bellard | vm_state_notify(1);
|
7206 | efe75411 | ths | qemu_rearm_alarm_timer(alarm_timer); |
7207 | 8a7ddc38 | bellard | } |
7208 | 8a7ddc38 | bellard | } |
7209 | 8a7ddc38 | bellard | |
7210 | 5fafdf24 | ths | void vm_stop(int reason) |
7211 | 8a7ddc38 | bellard | { |
7212 | 8a7ddc38 | bellard | if (vm_running) {
|
7213 | 8a7ddc38 | bellard | cpu_disable_ticks(); |
7214 | 8a7ddc38 | bellard | vm_running = 0;
|
7215 | 8a7ddc38 | bellard | if (reason != 0) { |
7216 | 8a7ddc38 | bellard | if (vm_stop_cb) {
|
7217 | 8a7ddc38 | bellard | vm_stop_cb(vm_stop_opaque, reason); |
7218 | 8a7ddc38 | bellard | } |
7219 | 34865134 | bellard | } |
7220 | 0bd48850 | bellard | vm_state_notify(0);
|
7221 | 8a7ddc38 | bellard | } |
7222 | 8a7ddc38 | bellard | } |
7223 | 8a7ddc38 | bellard | |
7224 | bb0c6722 | bellard | /* reset/shutdown handler */
|
7225 | bb0c6722 | bellard | |
7226 | bb0c6722 | bellard | typedef struct QEMUResetEntry { |
7227 | bb0c6722 | bellard | QEMUResetHandler *func; |
7228 | bb0c6722 | bellard | void *opaque;
|
7229 | bb0c6722 | bellard | struct QEMUResetEntry *next;
|
7230 | bb0c6722 | bellard | } QEMUResetEntry; |
7231 | bb0c6722 | bellard | |
7232 | bb0c6722 | bellard | static QEMUResetEntry *first_reset_entry;
|
7233 | bb0c6722 | bellard | static int reset_requested; |
7234 | bb0c6722 | bellard | static int shutdown_requested; |
7235 | 3475187d | bellard | static int powerdown_requested; |
7236 | bb0c6722 | bellard | |
7237 | cf7a2fe2 | aurel32 | int qemu_shutdown_requested(void) |
7238 | cf7a2fe2 | aurel32 | { |
7239 | cf7a2fe2 | aurel32 | int r = shutdown_requested;
|
7240 | cf7a2fe2 | aurel32 | shutdown_requested = 0;
|
7241 | cf7a2fe2 | aurel32 | return r;
|
7242 | cf7a2fe2 | aurel32 | } |
7243 | cf7a2fe2 | aurel32 | |
7244 | cf7a2fe2 | aurel32 | int qemu_reset_requested(void) |
7245 | cf7a2fe2 | aurel32 | { |
7246 | cf7a2fe2 | aurel32 | int r = reset_requested;
|
7247 | cf7a2fe2 | aurel32 | reset_requested = 0;
|
7248 | cf7a2fe2 | aurel32 | return r;
|
7249 | cf7a2fe2 | aurel32 | } |
7250 | cf7a2fe2 | aurel32 | |
7251 | cf7a2fe2 | aurel32 | int qemu_powerdown_requested(void) |
7252 | cf7a2fe2 | aurel32 | { |
7253 | cf7a2fe2 | aurel32 | int r = powerdown_requested;
|
7254 | cf7a2fe2 | aurel32 | powerdown_requested = 0;
|
7255 | cf7a2fe2 | aurel32 | return r;
|
7256 | cf7a2fe2 | aurel32 | } |
7257 | cf7a2fe2 | aurel32 | |
7258 | bb0c6722 | bellard | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
7259 | bb0c6722 | bellard | { |
7260 | bb0c6722 | bellard | QEMUResetEntry **pre, *re; |
7261 | bb0c6722 | bellard | |
7262 | bb0c6722 | bellard | pre = &first_reset_entry; |
7263 | bb0c6722 | bellard | while (*pre != NULL) |
7264 | bb0c6722 | bellard | pre = &(*pre)->next; |
7265 | bb0c6722 | bellard | re = qemu_mallocz(sizeof(QEMUResetEntry));
|
7266 | bb0c6722 | bellard | re->func = func; |
7267 | bb0c6722 | bellard | re->opaque = opaque; |
7268 | bb0c6722 | bellard | re->next = NULL;
|
7269 | bb0c6722 | bellard | *pre = re; |
7270 | bb0c6722 | bellard | } |
7271 | bb0c6722 | bellard | |
7272 | cf7a2fe2 | aurel32 | void qemu_system_reset(void) |
7273 | bb0c6722 | bellard | { |
7274 | bb0c6722 | bellard | QEMUResetEntry *re; |
7275 | bb0c6722 | bellard | |
7276 | bb0c6722 | bellard | /* reset all devices */
|
7277 | bb0c6722 | bellard | for(re = first_reset_entry; re != NULL; re = re->next) { |
7278 | bb0c6722 | bellard | re->func(re->opaque); |
7279 | bb0c6722 | bellard | } |
7280 | bb0c6722 | bellard | } |
7281 | bb0c6722 | bellard | |
7282 | bb0c6722 | bellard | void qemu_system_reset_request(void) |
7283 | bb0c6722 | bellard | { |
7284 | d1beab82 | bellard | if (no_reboot) {
|
7285 | d1beab82 | bellard | shutdown_requested = 1;
|
7286 | d1beab82 | bellard | } else {
|
7287 | d1beab82 | bellard | reset_requested = 1;
|
7288 | d1beab82 | bellard | } |
7289 | 6a00d601 | bellard | if (cpu_single_env)
|
7290 | 6a00d601 | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
7291 | bb0c6722 | bellard | } |
7292 | bb0c6722 | bellard | |
7293 | bb0c6722 | bellard | void qemu_system_shutdown_request(void) |
7294 | bb0c6722 | bellard | { |
7295 | bb0c6722 | bellard | shutdown_requested = 1;
|
7296 | 6a00d601 | bellard | if (cpu_single_env)
|
7297 | 6a00d601 | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
7298 | bb0c6722 | bellard | } |
7299 | bb0c6722 | bellard | |
7300 | 3475187d | bellard | void qemu_system_powerdown_request(void) |
7301 | 3475187d | bellard | { |
7302 | 3475187d | bellard | powerdown_requested = 1;
|
7303 | 6a00d601 | bellard | if (cpu_single_env)
|
7304 | 6a00d601 | bellard | cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
7305 | bb0c6722 | bellard | } |
7306 | bb0c6722 | bellard | |
7307 | 5905b2e5 | bellard | void main_loop_wait(int timeout) |
7308 | 8a7ddc38 | bellard | { |
7309 | cafffd40 | ths | IOHandlerRecord *ioh; |
7310 | e035649e | bellard | fd_set rfds, wfds, xfds; |
7311 | 877cf882 | ths | int ret, nfds;
|
7312 | 877cf882 | ths | #ifdef _WIN32
|
7313 | 877cf882 | ths | int ret2, i;
|
7314 | 877cf882 | ths | #endif
|
7315 | fd1dff4b | bellard | struct timeval tv;
|
7316 | f331110f | bellard | PollingEntry *pe; |
7317 | f331110f | bellard | |
7318 | c4b1fcc0 | bellard | |
7319 | f331110f | bellard | /* XXX: need to suppress polling by better using win32 events */
|
7320 | f331110f | bellard | ret = 0;
|
7321 | f331110f | bellard | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
7322 | f331110f | bellard | ret |= pe->func(pe->opaque); |
7323 | f331110f | bellard | } |
7324 | 38e205a2 | bellard | #ifdef _WIN32
|
7325 | e6b1e558 | ths | if (ret == 0) { |
7326 | a18e524a | bellard | int err;
|
7327 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
7328 | 3b46e624 | ths | |
7329 | a18e524a | bellard | ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout); |
7330 | a18e524a | bellard | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
7331 | a18e524a | bellard | if (w->func[ret - WAIT_OBJECT_0])
|
7332 | a18e524a | bellard | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
7333 | 3b46e624 | ths | |
7334 | 5fafdf24 | ths | /* Check for additional signaled events */
|
7335 | e6b1e558 | ths | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
7336 | 3b46e624 | ths | |
7337 | e6b1e558 | ths | /* Check if event is signaled */
|
7338 | e6b1e558 | ths | ret2 = WaitForSingleObject(w->events[i], 0);
|
7339 | e6b1e558 | ths | if(ret2 == WAIT_OBJECT_0) {
|
7340 | e6b1e558 | ths | if (w->func[i])
|
7341 | e6b1e558 | ths | w->func[i](w->opaque[i]); |
7342 | e6b1e558 | ths | } else if (ret2 == WAIT_TIMEOUT) { |
7343 | e6b1e558 | ths | } else {
|
7344 | e6b1e558 | ths | err = GetLastError(); |
7345 | e6b1e558 | ths | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
|
7346 | 3b46e624 | ths | } |
7347 | 3b46e624 | ths | } |
7348 | a18e524a | bellard | } else if (ret == WAIT_TIMEOUT) { |
7349 | a18e524a | bellard | } else {
|
7350 | a18e524a | bellard | err = GetLastError(); |
7351 | e6b1e558 | ths | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
|
7352 | a18e524a | bellard | } |
7353 | f331110f | bellard | } |
7354 | fd1dff4b | bellard | #endif
|
7355 | fd1dff4b | bellard | /* poll any events */
|
7356 | fd1dff4b | bellard | /* XXX: separate device handlers from system ones */
|
7357 | fd1dff4b | bellard | nfds = -1;
|
7358 | fd1dff4b | bellard | FD_ZERO(&rfds); |
7359 | fd1dff4b | bellard | FD_ZERO(&wfds); |
7360 | e035649e | bellard | FD_ZERO(&xfds); |
7361 | fd1dff4b | bellard | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
7362 | cafffd40 | ths | if (ioh->deleted)
|
7363 | cafffd40 | ths | continue;
|
7364 | fd1dff4b | bellard | if (ioh->fd_read &&
|
7365 | fd1dff4b | bellard | (!ioh->fd_read_poll || |
7366 | fd1dff4b | bellard | ioh->fd_read_poll(ioh->opaque) != 0)) {
|
7367 | fd1dff4b | bellard | FD_SET(ioh->fd, &rfds); |
7368 | fd1dff4b | bellard | if (ioh->fd > nfds)
|
7369 | fd1dff4b | bellard | nfds = ioh->fd; |
7370 | fd1dff4b | bellard | } |
7371 | fd1dff4b | bellard | if (ioh->fd_write) {
|
7372 | fd1dff4b | bellard | FD_SET(ioh->fd, &wfds); |
7373 | fd1dff4b | bellard | if (ioh->fd > nfds)
|
7374 | fd1dff4b | bellard | nfds = ioh->fd; |
7375 | fd1dff4b | bellard | } |
7376 | fd1dff4b | bellard | } |
7377 | 3b46e624 | ths | |
7378 | fd1dff4b | bellard | tv.tv_sec = 0;
|
7379 | fd1dff4b | bellard | #ifdef _WIN32
|
7380 | fd1dff4b | bellard | tv.tv_usec = 0;
|
7381 | 38e205a2 | bellard | #else
|
7382 | fd1dff4b | bellard | tv.tv_usec = timeout * 1000;
|
7383 | fd1dff4b | bellard | #endif
|
7384 | e035649e | bellard | #if defined(CONFIG_SLIRP)
|
7385 | e035649e | bellard | if (slirp_inited) {
|
7386 | e035649e | bellard | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
7387 | e035649e | bellard | } |
7388 | e035649e | bellard | #endif
|
7389 | e035649e | bellard | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
|
7390 | fd1dff4b | bellard | if (ret > 0) { |
7391 | cafffd40 | ths | IOHandlerRecord **pioh; |
7392 | cafffd40 | ths | |
7393 | cafffd40 | ths | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
7394 | 6ab43fdc | ths | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
|
7395 | fd1dff4b | bellard | ioh->fd_read(ioh->opaque); |
7396 | 7c9d8e07 | bellard | } |
7397 | 6ab43fdc | ths | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
|
7398 | fd1dff4b | bellard | ioh->fd_write(ioh->opaque); |
7399 | c4b1fcc0 | bellard | } |
7400 | b4608c04 | bellard | } |
7401 | cafffd40 | ths | |
7402 | cafffd40 | ths | /* remove deleted IO handlers */
|
7403 | cafffd40 | ths | pioh = &first_io_handler; |
7404 | cafffd40 | ths | while (*pioh) {
|
7405 | cafffd40 | ths | ioh = *pioh; |
7406 | cafffd40 | ths | if (ioh->deleted) {
|
7407 | cafffd40 | ths | *pioh = ioh->next; |
7408 | cafffd40 | ths | qemu_free(ioh); |
7409 | 5fafdf24 | ths | } else
|
7410 | cafffd40 | ths | pioh = &ioh->next; |
7411 | cafffd40 | ths | } |
7412 | fd1dff4b | bellard | } |
7413 | c20709aa | bellard | #if defined(CONFIG_SLIRP)
|
7414 | fd1dff4b | bellard | if (slirp_inited) {
|
7415 | e035649e | bellard | if (ret < 0) { |
7416 | e035649e | bellard | FD_ZERO(&rfds); |
7417 | e035649e | bellard | FD_ZERO(&wfds); |
7418 | e035649e | bellard | FD_ZERO(&xfds); |
7419 | c20709aa | bellard | } |
7420 | e035649e | bellard | slirp_select_poll(&rfds, &wfds, &xfds); |
7421 | fd1dff4b | bellard | } |
7422 | c20709aa | bellard | #endif
|
7423 | 83f64091 | bellard | qemu_aio_poll(); |
7424 | c20709aa | bellard | |
7425 | fd1dff4b | bellard | if (vm_running) {
|
7426 | 21b20814 | edgar_igl | if (likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
|
7427 | 5fafdf24 | ths | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
7428 | fd1dff4b | bellard | qemu_get_clock(vm_clock)); |
7429 | fd1dff4b | bellard | /* run dma transfers, if any */
|
7430 | fd1dff4b | bellard | DMA_run(); |
7431 | fd1dff4b | bellard | } |
7432 | 423f0742 | pbrook | |
7433 | fd1dff4b | bellard | /* real time timers */
|
7434 | 5fafdf24 | ths | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
7435 | fd1dff4b | bellard | qemu_get_clock(rt_clock)); |
7436 | 423f0742 | pbrook | |
7437 | d5d08334 | balrog | if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
|
7438 | d5d08334 | balrog | alarm_timer->flags &= ~(ALARM_FLAG_EXPIRED); |
7439 | d5d08334 | balrog | qemu_rearm_alarm_timer(alarm_timer); |
7440 | d5d08334 | balrog | } |
7441 | b99dc0d1 | balrog | |
7442 | 423f0742 | pbrook | /* Check bottom-halves last in case any of the earlier events triggered
|
7443 | 423f0742 | pbrook | them. */
|
7444 | 423f0742 | pbrook | qemu_bh_poll(); |
7445 | 3b46e624 | ths | |
7446 | 5905b2e5 | bellard | } |
7447 | 5905b2e5 | bellard | |
7448 | 9596ebb7 | pbrook | static int main_loop(void) |
7449 | 5905b2e5 | bellard | { |
7450 | 5905b2e5 | bellard | int ret, timeout;
|
7451 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7452 | 89bfc105 | bellard | int64_t ti; |
7453 | 89bfc105 | bellard | #endif
|
7454 | 6a00d601 | bellard | CPUState *env; |
7455 | 5905b2e5 | bellard | |
7456 | 6a00d601 | bellard | cur_cpu = first_cpu; |
7457 | ee5605e5 | balrog | next_cpu = cur_cpu->next_cpu ?: first_cpu; |
7458 | 5905b2e5 | bellard | for(;;) {
|
7459 | 5905b2e5 | bellard | if (vm_running) {
|
7460 | 15a76449 | bellard | |
7461 | 15a76449 | bellard | for(;;) {
|
7462 | 15a76449 | bellard | /* get next cpu */
|
7463 | ee5605e5 | balrog | env = next_cpu; |
7464 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7465 | 89bfc105 | bellard | ti = profile_getclock(); |
7466 | 89bfc105 | bellard | #endif
|
7467 | 2e70f6ef | pbrook | if (use_icount) {
|
7468 | 2e70f6ef | pbrook | int64_t count; |
7469 | 2e70f6ef | pbrook | int decr;
|
7470 | 2e70f6ef | pbrook | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
7471 | 2e70f6ef | pbrook | env->icount_decr.u16.low = 0;
|
7472 | 2e70f6ef | pbrook | env->icount_extra = 0;
|
7473 | 2e70f6ef | pbrook | count = qemu_next_deadline(); |
7474 | 2e70f6ef | pbrook | count = (count + (1 << icount_time_shift) - 1) |
7475 | 2e70f6ef | pbrook | >> icount_time_shift; |
7476 | 2e70f6ef | pbrook | qemu_icount += count; |
7477 | 2e70f6ef | pbrook | decr = (count > 0xffff) ? 0xffff : count; |
7478 | 2e70f6ef | pbrook | count -= decr; |
7479 | 2e70f6ef | pbrook | env->icount_decr.u16.low = decr; |
7480 | 2e70f6ef | pbrook | env->icount_extra = count; |
7481 | 2e70f6ef | pbrook | } |
7482 | 6a00d601 | bellard | ret = cpu_exec(env); |
7483 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7484 | 89bfc105 | bellard | qemu_time += profile_getclock() - ti; |
7485 | 89bfc105 | bellard | #endif
|
7486 | 2e70f6ef | pbrook | if (use_icount) {
|
7487 | 2e70f6ef | pbrook | /* Fold pending instructions back into the
|
7488 | 2e70f6ef | pbrook | instruction counter, and clear the interrupt flag. */
|
7489 | 2e70f6ef | pbrook | qemu_icount -= (env->icount_decr.u16.low |
7490 | 2e70f6ef | pbrook | + env->icount_extra); |
7491 | 2e70f6ef | pbrook | env->icount_decr.u32 = 0;
|
7492 | 2e70f6ef | pbrook | env->icount_extra = 0;
|
7493 | 2e70f6ef | pbrook | } |
7494 | ee5605e5 | balrog | next_cpu = env->next_cpu ?: first_cpu; |
7495 | 95b01009 | aurel32 | if (event_pending && likely(ret != EXCP_DEBUG)) {
|
7496 | ee5605e5 | balrog | ret = EXCP_INTERRUPT; |
7497 | ee5605e5 | balrog | event_pending = 0;
|
7498 | ee5605e5 | balrog | break;
|
7499 | ee5605e5 | balrog | } |
7500 | bd967e05 | pbrook | if (ret == EXCP_HLT) {
|
7501 | bd967e05 | pbrook | /* Give the next CPU a chance to run. */
|
7502 | bd967e05 | pbrook | cur_cpu = env; |
7503 | bd967e05 | pbrook | continue;
|
7504 | bd967e05 | pbrook | } |
7505 | 15a76449 | bellard | if (ret != EXCP_HALTED)
|
7506 | 15a76449 | bellard | break;
|
7507 | 15a76449 | bellard | /* all CPUs are halted ? */
|
7508 | bd967e05 | pbrook | if (env == cur_cpu)
|
7509 | 15a76449 | bellard | break;
|
7510 | 15a76449 | bellard | } |
7511 | 15a76449 | bellard | cur_cpu = env; |
7512 | 15a76449 | bellard | |
7513 | 5905b2e5 | bellard | if (shutdown_requested) {
|
7514 | 3475187d | bellard | ret = EXCP_INTERRUPT; |
7515 | b2f76161 | aurel32 | if (no_shutdown) {
|
7516 | b2f76161 | aurel32 | vm_stop(0);
|
7517 | b2f76161 | aurel32 | no_shutdown = 0;
|
7518 | b2f76161 | aurel32 | } |
7519 | b2f76161 | aurel32 | else
|
7520 | b2f76161 | aurel32 | break;
|
7521 | 5905b2e5 | bellard | } |
7522 | 5905b2e5 | bellard | if (reset_requested) {
|
7523 | 5905b2e5 | bellard | reset_requested = 0;
|
7524 | 5905b2e5 | bellard | qemu_system_reset(); |
7525 | 3475187d | bellard | ret = EXCP_INTERRUPT; |
7526 | 3475187d | bellard | } |
7527 | 3475187d | bellard | if (powerdown_requested) {
|
7528 | 3475187d | bellard | powerdown_requested = 0;
|
7529 | 3475187d | bellard | qemu_system_powerdown(); |
7530 | 3475187d | bellard | ret = EXCP_INTERRUPT; |
7531 | 5905b2e5 | bellard | } |
7532 | 95b01009 | aurel32 | if (unlikely(ret == EXCP_DEBUG)) {
|
7533 | 5905b2e5 | bellard | vm_stop(EXCP_DEBUG); |
7534 | 5905b2e5 | bellard | } |
7535 | bd967e05 | pbrook | /* If all cpus are halted then wait until the next IRQ */
|
7536 | 5905b2e5 | bellard | /* XXX: use timeout computed from timers */
|
7537 | 2e70f6ef | pbrook | if (ret == EXCP_HALTED) {
|
7538 | 2e70f6ef | pbrook | if (use_icount) {
|
7539 | 2e70f6ef | pbrook | int64_t add; |
7540 | 2e70f6ef | pbrook | int64_t delta; |
7541 | 2e70f6ef | pbrook | /* Advance virtual time to the next event. */
|
7542 | 2e70f6ef | pbrook | if (use_icount == 1) { |
7543 | 2e70f6ef | pbrook | /* When not using an adaptive execution frequency
|
7544 | 2e70f6ef | pbrook | we tend to get badly out of sync with real time,
|
7545 | bf20dc07 | ths | so just delay for a reasonable amount of time. */
|
7546 | 2e70f6ef | pbrook | delta = 0;
|
7547 | 2e70f6ef | pbrook | } else {
|
7548 | 2e70f6ef | pbrook | delta = cpu_get_icount() - cpu_get_clock(); |
7549 | 2e70f6ef | pbrook | } |
7550 | 2e70f6ef | pbrook | if (delta > 0) { |
7551 | 2e70f6ef | pbrook | /* If virtual time is ahead of real time then just
|
7552 | 2e70f6ef | pbrook | wait for IO. */
|
7553 | 2e70f6ef | pbrook | timeout = (delta / 1000000) + 1; |
7554 | 2e70f6ef | pbrook | } else {
|
7555 | 2e70f6ef | pbrook | /* Wait for either IO to occur or the next
|
7556 | 2e70f6ef | pbrook | timer event. */
|
7557 | 2e70f6ef | pbrook | add = qemu_next_deadline(); |
7558 | 2e70f6ef | pbrook | /* We advance the timer before checking for IO.
|
7559 | 2e70f6ef | pbrook | Limit the amount we advance so that early IO
|
7560 | 2e70f6ef | pbrook | activity won't get the guest too far ahead. */
|
7561 | 2e70f6ef | pbrook | if (add > 10000000) |
7562 | 2e70f6ef | pbrook | add = 10000000;
|
7563 | 2e70f6ef | pbrook | delta += add; |
7564 | 2e70f6ef | pbrook | add = (add + (1 << icount_time_shift) - 1) |
7565 | 2e70f6ef | pbrook | >> icount_time_shift; |
7566 | 2e70f6ef | pbrook | qemu_icount += add; |
7567 | 2e70f6ef | pbrook | timeout = delta / 1000000;
|
7568 | 2e70f6ef | pbrook | if (timeout < 0) |
7569 | 2e70f6ef | pbrook | timeout = 0;
|
7570 | 2e70f6ef | pbrook | } |
7571 | 2e70f6ef | pbrook | } else {
|
7572 | 2e70f6ef | pbrook | timeout = 10;
|
7573 | 2e70f6ef | pbrook | } |
7574 | 2e70f6ef | pbrook | } else {
|
7575 | 5905b2e5 | bellard | timeout = 0;
|
7576 | 2e70f6ef | pbrook | } |
7577 | 5905b2e5 | bellard | } else {
|
7578 | 5905b2e5 | bellard | timeout = 10;
|
7579 | 5905b2e5 | bellard | } |
7580 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7581 | 89bfc105 | bellard | ti = profile_getclock(); |
7582 | 89bfc105 | bellard | #endif
|
7583 | 5905b2e5 | bellard | main_loop_wait(timeout); |
7584 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
7585 | 89bfc105 | bellard | dev_time += profile_getclock() - ti; |
7586 | 89bfc105 | bellard | #endif
|
7587 | b4608c04 | bellard | } |
7588 | 34865134 | bellard | cpu_disable_ticks(); |
7589 | 34865134 | bellard | return ret;
|
7590 | b4608c04 | bellard | } |
7591 | b4608c04 | bellard | |
7592 | 15f82208 | ths | static void help(int exitcode) |
7593 | 0824d6fc | bellard | { |
7594 | 68d0f70e | bellard | printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n" |
7595 | 0db63474 | bellard | "usage: %s [options] [disk_image]\n"
|
7596 | 0824d6fc | bellard | "\n"
|
7597 | a20dd508 | bellard | "'disk_image' is a raw hard image image for IDE hard disk 0\n"
|
7598 | fc01f7e7 | bellard | "\n"
|
7599 | a20dd508 | bellard | "Standard options:\n"
|
7600 | cc1daa40 | bellard | "-M machine select emulated machine (-M ? for list)\n"
|
7601 | 5adb4839 | pbrook | "-cpu cpu select CPU (-cpu ? for list)\n"
|
7602 | c45886db | bellard | "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
|
7603 | 36b486bb | bellard | "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
|
7604 | 36b486bb | bellard | "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
|
7605 | c4b1fcc0 | bellard | "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
|
7606 | a1620fac | aurel32 | "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
|
7607 | a1620fac | aurel32 | " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
|
7608 | a1620fac | aurel32 | " [,cache=on|off][,format=f]\n"
|
7609 | e4bcb14c | ths | " use 'file' as a drive image\n"
|
7610 | 3e3d5815 | balrog | "-mtdblock file use 'file' as on-board Flash memory image\n"
|
7611 | a1bb27b1 | pbrook | "-sd file use 'file' as SecureDigital card image\n"
|
7612 | 86f55663 | j_mayer | "-pflash file use 'file' as a parallel flash image\n"
|
7613 | eec85c2a | ths | "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
|
7614 | 667accab | ths | "-snapshot write to temporary files instead of disk image files\n"
|
7615 | 667accab | ths | #ifdef CONFIG_SDL
|
7616 | 43523e93 | ths | "-no-frame open SDL window without a frame and window decorations\n"
|
7617 | 3780e197 | ths | "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
|
7618 | 667accab | ths | "-no-quit disable SDL window close capability\n"
|
7619 | 667accab | ths | #endif
|
7620 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7621 | 52ca8d6a | bellard | "-no-fd-bootchk disable boot signature checking for floppy disks\n"
|
7622 | 52ca8d6a | bellard | #endif
|
7623 | a00bad7e | bellard | "-m megs set virtual RAM size to megs MB [default=%d]\n"
|
7624 | 91fc2119 | bellard | "-smp n set the number of CPUs to 'n' [default=1]\n"
|
7625 | c4b1fcc0 | bellard | "-nographic disable graphical output and redirect serial I/Os to console\n"
|
7626 | a171fe39 | balrog | "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
|
7627 | 4ca0074c | bellard | #ifndef _WIN32
|
7628 | 667accab | ths | "-k language use keyboard layout (for example \"fr\" for French)\n"
|
7629 | 4ca0074c | bellard | #endif
|
7630 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7631 | 1d14ffa9 | bellard | "-audio-help print list of audio drivers and their options\n"
|
7632 | c0fe3827 | bellard | "-soundhw c1,... enable audio support\n"
|
7633 | c0fe3827 | bellard | " and only specified sound cards (comma separated list)\n"
|
7634 | c0fe3827 | bellard | " use -soundhw ? to get the list of supported cards\n"
|
7635 | 6a36d84e | bellard | " use -soundhw all to enable all of them\n"
|
7636 | 1d14ffa9 | bellard | #endif
|
7637 | 89980284 | bellard | "-localtime set the real time clock to local time [default=utc]\n"
|
7638 | d63d307f | bellard | "-full-screen start in full screen\n"
|
7639 | a09db21f | bellard | #ifdef TARGET_I386
|
7640 | a09db21f | bellard | "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
|
7641 | a09db21f | bellard | #endif
|
7642 | b389dbfb | bellard | "-usb enable the USB driver (will be the default soon)\n"
|
7643 | b389dbfb | bellard | "-usbdevice name add the host or guest USB device 'name'\n"
|
7644 | 6f7e9aec | bellard | #if defined(TARGET_PPC) || defined(TARGET_SPARC)
|
7645 | 6f7e9aec | bellard | "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
|
7646 | bb0c6722 | bellard | #endif
|
7647 | c35734b2 | ths | "-name string set the name of the guest\n"
|
7648 | c4b1fcc0 | bellard | "\n"
|
7649 | c4b1fcc0 | bellard | "Network options:\n"
|
7650 | a41b2ff2 | pbrook | "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
|
7651 | 7c9d8e07 | bellard | " create a new Network Interface Card and connect it to VLAN 'n'\n"
|
7652 | c20709aa | bellard | #ifdef CONFIG_SLIRP
|
7653 | 115defd1 | pbrook | "-net user[,vlan=n][,hostname=host]\n"
|
7654 | 115defd1 | pbrook | " connect the user mode network stack to VLAN 'n' and send\n"
|
7655 | 115defd1 | pbrook | " hostname 'host' to DHCP clients\n"
|
7656 | 7c9d8e07 | bellard | #endif
|
7657 | 7fb843f8 | bellard | #ifdef _WIN32
|
7658 | 7fb843f8 | bellard | "-net tap[,vlan=n],ifname=name\n"
|
7659 | 7fb843f8 | bellard | " connect the host TAP network interface to VLAN 'n'\n"
|
7660 | 7fb843f8 | bellard | #else
|
7661 | b46a8906 | ths | "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
|
7662 | b46a8906 | ths | " connect the host TAP network interface to VLAN 'n' and use the\n"
|
7663 | b46a8906 | ths | " network scripts 'file' (default=%s)\n"
|
7664 | b46a8906 | ths | " and 'dfile' (default=%s);\n"
|
7665 | b46a8906 | ths | " use '[down]script=no' to disable script execution;\n"
|
7666 | 7c9d8e07 | bellard | " use 'fd=h' to connect to an already opened TAP interface\n"
|
7667 | 7fb843f8 | bellard | #endif
|
7668 | 6a00d601 | bellard | "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
|
7669 | 7c9d8e07 | bellard | " connect the vlan 'n' to another VLAN using a socket connection\n"
|
7670 | 3d830459 | bellard | "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
|
7671 | 3d830459 | bellard | " connect the vlan 'n' to multicast maddr and port\n"
|
7672 | 8a16d273 | ths | #ifdef CONFIG_VDE
|
7673 | 8a16d273 | ths | "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
|
7674 | 8a16d273 | ths | " connect the vlan 'n' to port 'n' of a vde switch running\n"
|
7675 | 8a16d273 | ths | " on host and listening for incoming connections on 'socketpath'.\n"
|
7676 | 8a16d273 | ths | " Use group 'groupname' and mode 'octalmode' to change default\n"
|
7677 | 8a16d273 | ths | " ownership and permissions for communication port.\n"
|
7678 | 8a16d273 | ths | #endif
|
7679 | 7c9d8e07 | bellard | "-net none use it alone to have zero network devices; if no -net option\n"
|
7680 | 7c9d8e07 | bellard | " is provided, the default is '-net nic -net user'\n"
|
7681 | 7c9d8e07 | bellard | "\n"
|
7682 | 7c9d8e07 | bellard | #ifdef CONFIG_SLIRP
|
7683 | 0db1137d | ths | "-tftp dir allow tftp access to files in dir [-net user]\n"
|
7684 | 47d5d01a | ths | "-bootp file advertise file in BOOTP replies\n"
|
7685 | 7c9d8e07 | bellard | #ifndef _WIN32
|
7686 | 7c9d8e07 | bellard | "-smb dir allow SMB access to files in 'dir' [-net user]\n"
|
7687 | c94c8d64 | bellard | #endif
|
7688 | 9bf05444 | bellard | "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
|
7689 | 7c9d8e07 | bellard | " redirect TCP or UDP connections from host to guest [-net user]\n"
|
7690 | c20709aa | bellard | #endif
|
7691 | a20dd508 | bellard | "\n"
|
7692 | c4b1fcc0 | bellard | "Linux boot specific:\n"
|
7693 | a20dd508 | bellard | "-kernel bzImage use 'bzImage' as kernel image\n"
|
7694 | a20dd508 | bellard | "-append cmdline use 'cmdline' as kernel command line\n"
|
7695 | a20dd508 | bellard | "-initrd file use 'file' as initial ram disk\n"
|
7696 | fc01f7e7 | bellard | "\n"
|
7697 | 330d0414 | bellard | "Debug/Expert options:\n"
|
7698 | 82c643ff | bellard | "-monitor dev redirect the monitor to char device 'dev'\n"
|
7699 | 82c643ff | bellard | "-serial dev redirect the serial port to char device 'dev'\n"
|
7700 | 6508fe59 | bellard | "-parallel dev redirect the parallel port to char device 'dev'\n"
|
7701 | f7cce898 | bellard | "-pidfile file Write PID to 'file'\n"
|
7702 | cd6f1169 | bellard | "-S freeze CPU at startup (use 'c' to start execution)\n"
|
7703 | cfc3475a | pbrook | "-s wait gdb connection to port\n"
|
7704 | cfc3475a | pbrook | "-p port set gdb connection port [default=%s]\n"
|
7705 | f193c797 | bellard | "-d item1,... output log to %s (use -d ? for a list of log items)\n"
|
7706 | 46d4767d | bellard | "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
|
7707 | 46d4767d | bellard | " translation (t=none or lba) (usually qemu can guess them)\n"
|
7708 | 87b47350 | bellard | "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
|
7709 | d993e026 | bellard | #ifdef USE_KQEMU
|
7710 | 6515b203 | bellard | "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
|
7711 | d993e026 | bellard | "-no-kqemu disable KQEMU kernel module usage\n"
|
7712 | d993e026 | bellard | #endif
|
7713 | bb0c6722 | bellard | #ifdef TARGET_I386
|
7714 | 1bfe856e | bellard | "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
|
7715 | 1bfe856e | bellard | " (default is CL-GD5446 PCI VGA)\n"
|
7716 | 6515b203 | bellard | "-no-acpi disable ACPI\n"
|
7717 | bb0c6722 | bellard | #endif
|
7718 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
7719 | 4d3b6f6e | balrog | "-curses use a curses/ncurses interface instead of SDL\n"
|
7720 | 4d3b6f6e | balrog | #endif
|
7721 | d1beab82 | bellard | "-no-reboot exit instead of rebooting\n"
|
7722 | b2f76161 | aurel32 | "-no-shutdown stop before shutdown\n"
|
7723 | a8080006 | aurel32 | "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
|
7724 | 24236869 | bellard | "-vnc display start a VNC server on display\n"
|
7725 | 71e3ceb8 | ths | #ifndef _WIN32
|
7726 | 71e3ceb8 | ths | "-daemonize daemonize QEMU after initializing\n"
|
7727 | 71e3ceb8 | ths | #endif
|
7728 | 9ae02555 | ths | "-option-rom rom load a file, rom, into the option ROM space\n"
|
7729 | 66508601 | blueswir1 | #ifdef TARGET_SPARC
|
7730 | 66508601 | blueswir1 | "-prom-env variable=value set OpenBIOS nvram variables\n"
|
7731 | 66508601 | blueswir1 | #endif
|
7732 | f3dcfada | ths | "-clock force the use of the given methods for timer alarm.\n"
|
7733 | 3adda04c | aurel32 | " To see what timers are available use -clock ?\n"
|
7734 | bce61846 | bellard | "-startdate select initial date of the clock\n"
|
7735 | 2e70f6ef | pbrook | "-icount [N|auto]\n"
|
7736 | dd5d6fe9 | pbrook | " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
|
7737 | 0824d6fc | bellard | "\n"
|
7738 | 82c643ff | bellard | "During emulation, the following keys are useful:\n"
|
7739 | 032a8c9e | bellard | "ctrl-alt-f toggle full screen\n"
|
7740 | 032a8c9e | bellard | "ctrl-alt-n switch to virtual console 'n'\n"
|
7741 | 032a8c9e | bellard | "ctrl-alt toggle mouse and keyboard grab\n"
|
7742 | 82c643ff | bellard | "\n"
|
7743 | 82c643ff | bellard | "When using -nographic, press 'ctrl-a h' to get some help.\n"
|
7744 | 82c643ff | bellard | , |
7745 | 0db63474 | bellard | "qemu",
|
7746 | a00bad7e | bellard | DEFAULT_RAM_SIZE, |
7747 | 7c9d8e07 | bellard | #ifndef _WIN32
|
7748 | a00bad7e | bellard | DEFAULT_NETWORK_SCRIPT, |
7749 | b46a8906 | ths | DEFAULT_NETWORK_DOWN_SCRIPT, |
7750 | 7c9d8e07 | bellard | #endif
|
7751 | 6e44ba7f | bellard | DEFAULT_GDBSTUB_PORT, |
7752 | bce61846 | bellard | "/tmp/qemu.log");
|
7753 | 15f82208 | ths | exit(exitcode); |
7754 | 0824d6fc | bellard | } |
7755 | 0824d6fc | bellard | |
7756 | cd6f1169 | bellard | #define HAS_ARG 0x0001 |
7757 | cd6f1169 | bellard | |
7758 | cd6f1169 | bellard | enum {
|
7759 | cd6f1169 | bellard | QEMU_OPTION_h, |
7760 | cd6f1169 | bellard | |
7761 | cc1daa40 | bellard | QEMU_OPTION_M, |
7762 | 94fc95cd | j_mayer | QEMU_OPTION_cpu, |
7763 | cd6f1169 | bellard | QEMU_OPTION_fda, |
7764 | cd6f1169 | bellard | QEMU_OPTION_fdb, |
7765 | cd6f1169 | bellard | QEMU_OPTION_hda, |
7766 | cd6f1169 | bellard | QEMU_OPTION_hdb, |
7767 | cd6f1169 | bellard | QEMU_OPTION_hdc, |
7768 | cd6f1169 | bellard | QEMU_OPTION_hdd, |
7769 | e4bcb14c | ths | QEMU_OPTION_drive, |
7770 | cd6f1169 | bellard | QEMU_OPTION_cdrom, |
7771 | 3e3d5815 | balrog | QEMU_OPTION_mtdblock, |
7772 | a1bb27b1 | pbrook | QEMU_OPTION_sd, |
7773 | 86f55663 | j_mayer | QEMU_OPTION_pflash, |
7774 | cd6f1169 | bellard | QEMU_OPTION_boot, |
7775 | cd6f1169 | bellard | QEMU_OPTION_snapshot, |
7776 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7777 | 52ca8d6a | bellard | QEMU_OPTION_no_fd_bootchk, |
7778 | 52ca8d6a | bellard | #endif
|
7779 | cd6f1169 | bellard | QEMU_OPTION_m, |
7780 | cd6f1169 | bellard | QEMU_OPTION_nographic, |
7781 | a171fe39 | balrog | QEMU_OPTION_portrait, |
7782 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7783 | 1d14ffa9 | bellard | QEMU_OPTION_audio_help, |
7784 | 1d14ffa9 | bellard | QEMU_OPTION_soundhw, |
7785 | 1d14ffa9 | bellard | #endif
|
7786 | cd6f1169 | bellard | |
7787 | 7c9d8e07 | bellard | QEMU_OPTION_net, |
7788 | c7f74643 | bellard | QEMU_OPTION_tftp, |
7789 | 47d5d01a | ths | QEMU_OPTION_bootp, |
7790 | 9d728e8c | bellard | QEMU_OPTION_smb, |
7791 | 9bf05444 | bellard | QEMU_OPTION_redir, |
7792 | cd6f1169 | bellard | |
7793 | cd6f1169 | bellard | QEMU_OPTION_kernel, |
7794 | cd6f1169 | bellard | QEMU_OPTION_append, |
7795 | cd6f1169 | bellard | QEMU_OPTION_initrd, |
7796 | cd6f1169 | bellard | |
7797 | cd6f1169 | bellard | QEMU_OPTION_S, |
7798 | cd6f1169 | bellard | QEMU_OPTION_s, |
7799 | cd6f1169 | bellard | QEMU_OPTION_p, |
7800 | cd6f1169 | bellard | QEMU_OPTION_d, |
7801 | cd6f1169 | bellard | QEMU_OPTION_hdachs, |
7802 | cd6f1169 | bellard | QEMU_OPTION_L, |
7803 | 1192dad8 | j_mayer | QEMU_OPTION_bios, |
7804 | 3d11d0eb | bellard | QEMU_OPTION_k, |
7805 | ee22c2f7 | bellard | QEMU_OPTION_localtime, |
7806 | 1f04275e | bellard | QEMU_OPTION_cirrusvga, |
7807 | d34cab9f | ths | QEMU_OPTION_vmsvga, |
7808 | e9b137c2 | bellard | QEMU_OPTION_g, |
7809 | 1bfe856e | bellard | QEMU_OPTION_std_vga, |
7810 | 20d8a3ed | ths | QEMU_OPTION_echr, |
7811 | 82c643ff | bellard | QEMU_OPTION_monitor, |
7812 | 82c643ff | bellard | QEMU_OPTION_serial, |
7813 | 6508fe59 | bellard | QEMU_OPTION_parallel, |
7814 | d63d307f | bellard | QEMU_OPTION_loadvm, |
7815 | d63d307f | bellard | QEMU_OPTION_full_screen, |
7816 | 43523e93 | ths | QEMU_OPTION_no_frame, |
7817 | 3780e197 | ths | QEMU_OPTION_alt_grab, |
7818 | 667accab | ths | QEMU_OPTION_no_quit, |
7819 | f7cce898 | bellard | QEMU_OPTION_pidfile, |
7820 | d993e026 | bellard | QEMU_OPTION_no_kqemu, |
7821 | 89bfc105 | bellard | QEMU_OPTION_kernel_kqemu, |
7822 | a09db21f | bellard | QEMU_OPTION_win2k_hack, |
7823 | bb36d470 | bellard | QEMU_OPTION_usb, |
7824 | a594cfbf | bellard | QEMU_OPTION_usbdevice, |
7825 | 6a00d601 | bellard | QEMU_OPTION_smp, |
7826 | 24236869 | bellard | QEMU_OPTION_vnc, |
7827 | 6515b203 | bellard | QEMU_OPTION_no_acpi, |
7828 | 4d3b6f6e | balrog | QEMU_OPTION_curses, |
7829 | d1beab82 | bellard | QEMU_OPTION_no_reboot, |
7830 | b2f76161 | aurel32 | QEMU_OPTION_no_shutdown, |
7831 | 9467cd46 | balrog | QEMU_OPTION_show_cursor, |
7832 | 71e3ceb8 | ths | QEMU_OPTION_daemonize, |
7833 | 9ae02555 | ths | QEMU_OPTION_option_rom, |
7834 | c35734b2 | ths | QEMU_OPTION_semihosting, |
7835 | c35734b2 | ths | QEMU_OPTION_name, |
7836 | 66508601 | blueswir1 | QEMU_OPTION_prom_env, |
7837 | 2b8f2d41 | balrog | QEMU_OPTION_old_param, |
7838 | f3dcfada | ths | QEMU_OPTION_clock, |
7839 | 7e0af5d0 | bellard | QEMU_OPTION_startdate, |
7840 | 26a5f13b | bellard | QEMU_OPTION_tb_size, |
7841 | 2e70f6ef | pbrook | QEMU_OPTION_icount, |
7842 | cd6f1169 | bellard | }; |
7843 | cd6f1169 | bellard | |
7844 | cd6f1169 | bellard | typedef struct QEMUOption { |
7845 | cd6f1169 | bellard | const char *name; |
7846 | cd6f1169 | bellard | int flags;
|
7847 | cd6f1169 | bellard | int index;
|
7848 | cd6f1169 | bellard | } QEMUOption; |
7849 | cd6f1169 | bellard | |
7850 | cd6f1169 | bellard | const QEMUOption qemu_options[] = {
|
7851 | cd6f1169 | bellard | { "h", 0, QEMU_OPTION_h }, |
7852 | 64423fb2 | pbrook | { "help", 0, QEMU_OPTION_h }, |
7853 | cd6f1169 | bellard | |
7854 | cc1daa40 | bellard | { "M", HAS_ARG, QEMU_OPTION_M },
|
7855 | 94fc95cd | j_mayer | { "cpu", HAS_ARG, QEMU_OPTION_cpu },
|
7856 | cd6f1169 | bellard | { "fda", HAS_ARG, QEMU_OPTION_fda },
|
7857 | cd6f1169 | bellard | { "fdb", HAS_ARG, QEMU_OPTION_fdb },
|
7858 | cd6f1169 | bellard | { "hda", HAS_ARG, QEMU_OPTION_hda },
|
7859 | cd6f1169 | bellard | { "hdb", HAS_ARG, QEMU_OPTION_hdb },
|
7860 | cd6f1169 | bellard | { "hdc", HAS_ARG, QEMU_OPTION_hdc },
|
7861 | cd6f1169 | bellard | { "hdd", HAS_ARG, QEMU_OPTION_hdd },
|
7862 | e4bcb14c | ths | { "drive", HAS_ARG, QEMU_OPTION_drive },
|
7863 | cd6f1169 | bellard | { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
|
7864 | 3e3d5815 | balrog | { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
|
7865 | a1bb27b1 | pbrook | { "sd", HAS_ARG, QEMU_OPTION_sd },
|
7866 | 86f55663 | j_mayer | { "pflash", HAS_ARG, QEMU_OPTION_pflash },
|
7867 | cd6f1169 | bellard | { "boot", HAS_ARG, QEMU_OPTION_boot },
|
7868 | cd6f1169 | bellard | { "snapshot", 0, QEMU_OPTION_snapshot }, |
7869 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
7870 | 52ca8d6a | bellard | { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk }, |
7871 | 52ca8d6a | bellard | #endif
|
7872 | cd6f1169 | bellard | { "m", HAS_ARG, QEMU_OPTION_m },
|
7873 | cd6f1169 | bellard | { "nographic", 0, QEMU_OPTION_nographic }, |
7874 | a171fe39 | balrog | { "portrait", 0, QEMU_OPTION_portrait }, |
7875 | 3d11d0eb | bellard | { "k", HAS_ARG, QEMU_OPTION_k },
|
7876 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7877 | 1d14ffa9 | bellard | { "audio-help", 0, QEMU_OPTION_audio_help }, |
7878 | 1d14ffa9 | bellard | { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
|
7879 | 1d14ffa9 | bellard | #endif
|
7880 | cd6f1169 | bellard | |
7881 | 7c9d8e07 | bellard | { "net", HAS_ARG, QEMU_OPTION_net},
|
7882 | 158156d1 | bellard | #ifdef CONFIG_SLIRP
|
7883 | c7f74643 | bellard | { "tftp", HAS_ARG, QEMU_OPTION_tftp },
|
7884 | 47d5d01a | ths | { "bootp", HAS_ARG, QEMU_OPTION_bootp },
|
7885 | c94c8d64 | bellard | #ifndef _WIN32
|
7886 | 9d728e8c | bellard | { "smb", HAS_ARG, QEMU_OPTION_smb },
|
7887 | c94c8d64 | bellard | #endif
|
7888 | 9bf05444 | bellard | { "redir", HAS_ARG, QEMU_OPTION_redir },
|
7889 | 158156d1 | bellard | #endif
|
7890 | cd6f1169 | bellard | |
7891 | cd6f1169 | bellard | { "kernel", HAS_ARG, QEMU_OPTION_kernel },
|
7892 | cd6f1169 | bellard | { "append", HAS_ARG, QEMU_OPTION_append },
|
7893 | cd6f1169 | bellard | { "initrd", HAS_ARG, QEMU_OPTION_initrd },
|
7894 | cd6f1169 | bellard | |
7895 | cd6f1169 | bellard | { "S", 0, QEMU_OPTION_S }, |
7896 | cd6f1169 | bellard | { "s", 0, QEMU_OPTION_s }, |
7897 | cd6f1169 | bellard | { "p", HAS_ARG, QEMU_OPTION_p },
|
7898 | cd6f1169 | bellard | { "d", HAS_ARG, QEMU_OPTION_d },
|
7899 | cd6f1169 | bellard | { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
|
7900 | cd6f1169 | bellard | { "L", HAS_ARG, QEMU_OPTION_L },
|
7901 | 1192dad8 | j_mayer | { "bios", HAS_ARG, QEMU_OPTION_bios },
|
7902 | d993e026 | bellard | #ifdef USE_KQEMU
|
7903 | d993e026 | bellard | { "no-kqemu", 0, QEMU_OPTION_no_kqemu }, |
7904 | 89bfc105 | bellard | { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu }, |
7905 | d993e026 | bellard | #endif
|
7906 | 6f7e9aec | bellard | #if defined(TARGET_PPC) || defined(TARGET_SPARC)
|
7907 | e9b137c2 | bellard | { "g", 1, QEMU_OPTION_g }, |
7908 | 77d4bc34 | bellard | #endif
|
7909 | ee22c2f7 | bellard | { "localtime", 0, QEMU_OPTION_localtime }, |
7910 | 1bfe856e | bellard | { "std-vga", 0, QEMU_OPTION_std_vga }, |
7911 | 8b6e0729 | balrog | { "echr", HAS_ARG, QEMU_OPTION_echr },
|
7912 | 8b6e0729 | balrog | { "monitor", HAS_ARG, QEMU_OPTION_monitor },
|
7913 | 8b6e0729 | balrog | { "serial", HAS_ARG, QEMU_OPTION_serial },
|
7914 | 8b6e0729 | balrog | { "parallel", HAS_ARG, QEMU_OPTION_parallel },
|
7915 | d63d307f | bellard | { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
|
7916 | d63d307f | bellard | { "full-screen", 0, QEMU_OPTION_full_screen }, |
7917 | 667accab | ths | #ifdef CONFIG_SDL
|
7918 | 43523e93 | ths | { "no-frame", 0, QEMU_OPTION_no_frame }, |
7919 | 3780e197 | ths | { "alt-grab", 0, QEMU_OPTION_alt_grab }, |
7920 | 667accab | ths | { "no-quit", 0, QEMU_OPTION_no_quit }, |
7921 | 667accab | ths | #endif
|
7922 | f7cce898 | bellard | { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
|
7923 | a09db21f | bellard | { "win2k-hack", 0, QEMU_OPTION_win2k_hack }, |
7924 | a594cfbf | bellard | { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
|
7925 | 6a00d601 | bellard | { "smp", HAS_ARG, QEMU_OPTION_smp },
|
7926 | 24236869 | bellard | { "vnc", HAS_ARG, QEMU_OPTION_vnc },
|
7927 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
7928 | 4d3b6f6e | balrog | { "curses", 0, QEMU_OPTION_curses }, |
7929 | 4d3b6f6e | balrog | #endif
|
7930 | 96d30e48 | ths | |
7931 | 1f04275e | bellard | /* temporary options */
|
7932 | a594cfbf | bellard | { "usb", 0, QEMU_OPTION_usb }, |
7933 | 1f04275e | bellard | { "cirrusvga", 0, QEMU_OPTION_cirrusvga }, |
7934 | d34cab9f | ths | { "vmwarevga", 0, QEMU_OPTION_vmsvga }, |
7935 | 6515b203 | bellard | { "no-acpi", 0, QEMU_OPTION_no_acpi }, |
7936 | d1beab82 | bellard | { "no-reboot", 0, QEMU_OPTION_no_reboot }, |
7937 | b2f76161 | aurel32 | { "no-shutdown", 0, QEMU_OPTION_no_shutdown }, |
7938 | 9467cd46 | balrog | { "show-cursor", 0, QEMU_OPTION_show_cursor }, |
7939 | 71e3ceb8 | ths | { "daemonize", 0, QEMU_OPTION_daemonize }, |
7940 | 9ae02555 | ths | { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
|
7941 | a87295e8 | pbrook | #if defined(TARGET_ARM) || defined(TARGET_M68K)
|
7942 | 8e71621f | pbrook | { "semihosting", 0, QEMU_OPTION_semihosting }, |
7943 | 8e71621f | pbrook | #endif
|
7944 | c35734b2 | ths | { "name", HAS_ARG, QEMU_OPTION_name },
|
7945 | 66508601 | blueswir1 | #if defined(TARGET_SPARC)
|
7946 | 66508601 | blueswir1 | { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
|
7947 | 66508601 | blueswir1 | #endif
|
7948 | 2b8f2d41 | balrog | #if defined(TARGET_ARM)
|
7949 | 2b8f2d41 | balrog | { "old-param", 0, QEMU_OPTION_old_param }, |
7950 | 2b8f2d41 | balrog | #endif
|
7951 | f3dcfada | ths | { "clock", HAS_ARG, QEMU_OPTION_clock },
|
7952 | 7e0af5d0 | bellard | { "startdate", HAS_ARG, QEMU_OPTION_startdate },
|
7953 | 26a5f13b | bellard | { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
|
7954 | 2e70f6ef | pbrook | { "icount", HAS_ARG, QEMU_OPTION_icount },
|
7955 | cd6f1169 | bellard | { NULL },
|
7956 | fc01f7e7 | bellard | }; |
7957 | fc01f7e7 | bellard | |
7958 | 5905b2e5 | bellard | /* password input */
|
7959 | 5905b2e5 | bellard | |
7960 | 2bac6019 | balrog | int qemu_key_check(BlockDriverState *bs, const char *name) |
7961 | 2bac6019 | balrog | { |
7962 | 2bac6019 | balrog | char password[256]; |
7963 | 2bac6019 | balrog | int i;
|
7964 | 2bac6019 | balrog | |
7965 | 2bac6019 | balrog | if (!bdrv_is_encrypted(bs))
|
7966 | 2bac6019 | balrog | return 0; |
7967 | 2bac6019 | balrog | |
7968 | 2bac6019 | balrog | term_printf("%s is encrypted.\n", name);
|
7969 | 2bac6019 | balrog | for(i = 0; i < 3; i++) { |
7970 | 2bac6019 | balrog | monitor_readline("Password: ", 1, password, sizeof(password)); |
7971 | 2bac6019 | balrog | if (bdrv_set_key(bs, password) == 0) |
7972 | 2bac6019 | balrog | return 0; |
7973 | 2bac6019 | balrog | term_printf("invalid password\n");
|
7974 | 2bac6019 | balrog | } |
7975 | 2bac6019 | balrog | return -EPERM;
|
7976 | 2bac6019 | balrog | } |
7977 | 2bac6019 | balrog | |
7978 | 5905b2e5 | bellard | static BlockDriverState *get_bdrv(int index) |
7979 | 5905b2e5 | bellard | { |
7980 | e4bcb14c | ths | if (index > nb_drives)
|
7981 | e4bcb14c | ths | return NULL; |
7982 | e4bcb14c | ths | return drives_table[index].bdrv;
|
7983 | 5905b2e5 | bellard | } |
7984 | 5905b2e5 | bellard | |
7985 | 5905b2e5 | bellard | static void read_passwords(void) |
7986 | 5905b2e5 | bellard | { |
7987 | 5905b2e5 | bellard | BlockDriverState *bs; |
7988 | 2bac6019 | balrog | int i;
|
7989 | 5905b2e5 | bellard | |
7990 | 5905b2e5 | bellard | for(i = 0; i < 6; i++) { |
7991 | 5905b2e5 | bellard | bs = get_bdrv(i); |
7992 | 2bac6019 | balrog | if (bs)
|
7993 | 2bac6019 | balrog | qemu_key_check(bs, bdrv_get_device_name(bs)); |
7994 | 5905b2e5 | bellard | } |
7995 | 5905b2e5 | bellard | } |
7996 | 5905b2e5 | bellard | |
7997 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
7998 | 6a36d84e | bellard | struct soundhw soundhw[] = {
|
7999 | b00052e4 | balrog | #ifdef HAS_AUDIO_CHOICE
|
8000 | 4ce7ff6e | aurel32 | #if defined(TARGET_I386) || defined(TARGET_MIPS)
|
8001 | fd06c375 | bellard | { |
8002 | fd06c375 | bellard | "pcspk",
|
8003 | fd06c375 | bellard | "PC speaker",
|
8004 | fd06c375 | bellard | 0,
|
8005 | fd06c375 | bellard | 1,
|
8006 | fd06c375 | bellard | { .init_isa = pcspk_audio_init } |
8007 | fd06c375 | bellard | }, |
8008 | fd06c375 | bellard | #endif
|
8009 | 6a36d84e | bellard | { |
8010 | 6a36d84e | bellard | "sb16",
|
8011 | 6a36d84e | bellard | "Creative Sound Blaster 16",
|
8012 | 6a36d84e | bellard | 0,
|
8013 | 6a36d84e | bellard | 1,
|
8014 | 6a36d84e | bellard | { .init_isa = SB16_init } |
8015 | 6a36d84e | bellard | }, |
8016 | 6a36d84e | bellard | |
8017 | cc53d26d | malc | #ifdef CONFIG_CS4231A
|
8018 | cc53d26d | malc | { |
8019 | cc53d26d | malc | "cs4231a",
|
8020 | cc53d26d | malc | "CS4231A",
|
8021 | cc53d26d | malc | 0,
|
8022 | cc53d26d | malc | 1,
|
8023 | cc53d26d | malc | { .init_isa = cs4231a_init } |
8024 | cc53d26d | malc | }, |
8025 | cc53d26d | malc | #endif
|
8026 | cc53d26d | malc | |
8027 | 1d14ffa9 | bellard | #ifdef CONFIG_ADLIB
|
8028 | 6a36d84e | bellard | { |
8029 | 6a36d84e | bellard | "adlib",
|
8030 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
8031 | 6a36d84e | bellard | "Yamaha YMF262 (OPL3)",
|
8032 | 1d14ffa9 | bellard | #else
|
8033 | 6a36d84e | bellard | "Yamaha YM3812 (OPL2)",
|
8034 | 1d14ffa9 | bellard | #endif
|
8035 | 6a36d84e | bellard | 0,
|
8036 | 6a36d84e | bellard | 1,
|
8037 | 6a36d84e | bellard | { .init_isa = Adlib_init } |
8038 | 6a36d84e | bellard | }, |
8039 | 1d14ffa9 | bellard | #endif
|
8040 | 6a36d84e | bellard | |
8041 | 1d14ffa9 | bellard | #ifdef CONFIG_GUS
|
8042 | 6a36d84e | bellard | { |
8043 | 6a36d84e | bellard | "gus",
|
8044 | 6a36d84e | bellard | "Gravis Ultrasound GF1",
|
8045 | 6a36d84e | bellard | 0,
|
8046 | 6a36d84e | bellard | 1,
|
8047 | 6a36d84e | bellard | { .init_isa = GUS_init } |
8048 | 6a36d84e | bellard | }, |
8049 | 1d14ffa9 | bellard | #endif
|
8050 | 6a36d84e | bellard | |
8051 | e5c9a13e | balrog | #ifdef CONFIG_AC97
|
8052 | e5c9a13e | balrog | { |
8053 | e5c9a13e | balrog | "ac97",
|
8054 | e5c9a13e | balrog | "Intel 82801AA AC97 Audio",
|
8055 | e5c9a13e | balrog | 0,
|
8056 | e5c9a13e | balrog | 0,
|
8057 | e5c9a13e | balrog | { .init_pci = ac97_init } |
8058 | e5c9a13e | balrog | }, |
8059 | e5c9a13e | balrog | #endif
|
8060 | e5c9a13e | balrog | |
8061 | 6a36d84e | bellard | { |
8062 | 6a36d84e | bellard | "es1370",
|
8063 | 6a36d84e | bellard | "ENSONIQ AudioPCI ES1370",
|
8064 | 6a36d84e | bellard | 0,
|
8065 | 6a36d84e | bellard | 0,
|
8066 | 6a36d84e | bellard | { .init_pci = es1370_init } |
8067 | 6a36d84e | bellard | }, |
8068 | b00052e4 | balrog | #endif
|
8069 | 6a36d84e | bellard | |
8070 | 6a36d84e | bellard | { NULL, NULL, 0, 0, { NULL } } |
8071 | 6a36d84e | bellard | }; |
8072 | 6a36d84e | bellard | |
8073 | 6a36d84e | bellard | static void select_soundhw (const char *optarg) |
8074 | 6a36d84e | bellard | { |
8075 | 6a36d84e | bellard | struct soundhw *c;
|
8076 | 6a36d84e | bellard | |
8077 | 6a36d84e | bellard | if (*optarg == '?') { |
8078 | 6a36d84e | bellard | show_valid_cards:
|
8079 | 6a36d84e | bellard | |
8080 | 6a36d84e | bellard | printf ("Valid sound card names (comma separated):\n");
|
8081 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
8082 | 6a36d84e | bellard | printf ("%-11s %s\n", c->name, c->descr);
|
8083 | 6a36d84e | bellard | } |
8084 | 6a36d84e | bellard | printf ("\n-soundhw all will enable all of the above\n");
|
8085 | 1d14ffa9 | bellard | exit (*optarg != '?');
|
8086 | 1d14ffa9 | bellard | } |
8087 | 1d14ffa9 | bellard | else {
|
8088 | 6a36d84e | bellard | size_t l; |
8089 | 1d14ffa9 | bellard | const char *p; |
8090 | 1d14ffa9 | bellard | char *e;
|
8091 | 1d14ffa9 | bellard | int bad_card = 0; |
8092 | 1d14ffa9 | bellard | |
8093 | 6a36d84e | bellard | if (!strcmp (optarg, "all")) { |
8094 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
8095 | 6a36d84e | bellard | c->enabled = 1;
|
8096 | 6a36d84e | bellard | } |
8097 | 6a36d84e | bellard | return;
|
8098 | 6a36d84e | bellard | } |
8099 | 1d14ffa9 | bellard | |
8100 | 6a36d84e | bellard | p = optarg; |
8101 | 1d14ffa9 | bellard | while (*p) {
|
8102 | 1d14ffa9 | bellard | e = strchr (p, ',');
|
8103 | 1d14ffa9 | bellard | l = !e ? strlen (p) : (size_t) (e - p); |
8104 | 6a36d84e | bellard | |
8105 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
8106 | 6a36d84e | bellard | if (!strncmp (c->name, p, l)) {
|
8107 | 6a36d84e | bellard | c->enabled = 1;
|
8108 | 1d14ffa9 | bellard | break;
|
8109 | 1d14ffa9 | bellard | } |
8110 | 1d14ffa9 | bellard | } |
8111 | 6a36d84e | bellard | |
8112 | 6a36d84e | bellard | if (!c->name) {
|
8113 | 1d14ffa9 | bellard | if (l > 80) { |
8114 | 1d14ffa9 | bellard | fprintf (stderr, |
8115 | 1d14ffa9 | bellard | "Unknown sound card name (too big to show)\n");
|
8116 | 1d14ffa9 | bellard | } |
8117 | 1d14ffa9 | bellard | else {
|
8118 | 1d14ffa9 | bellard | fprintf (stderr, "Unknown sound card name `%.*s'\n",
|
8119 | 1d14ffa9 | bellard | (int) l, p);
|
8120 | 1d14ffa9 | bellard | } |
8121 | 1d14ffa9 | bellard | bad_card = 1;
|
8122 | 1d14ffa9 | bellard | } |
8123 | 1d14ffa9 | bellard | p += l + (e != NULL);
|
8124 | 1d14ffa9 | bellard | } |
8125 | 1d14ffa9 | bellard | |
8126 | 1d14ffa9 | bellard | if (bad_card)
|
8127 | 1d14ffa9 | bellard | goto show_valid_cards;
|
8128 | 1d14ffa9 | bellard | } |
8129 | 1d14ffa9 | bellard | } |
8130 | 1d14ffa9 | bellard | #endif
|
8131 | 1d14ffa9 | bellard | |
8132 | 3587d7e6 | bellard | #ifdef _WIN32
|
8133 | 3587d7e6 | bellard | static BOOL WINAPI qemu_ctrl_handler(DWORD type)
|
8134 | 3587d7e6 | bellard | { |
8135 | 3587d7e6 | bellard | exit(STATUS_CONTROL_C_EXIT); |
8136 | 3587d7e6 | bellard | return TRUE;
|
8137 | 3587d7e6 | bellard | } |
8138 | 3587d7e6 | bellard | #endif
|
8139 | 3587d7e6 | bellard | |
8140 | 7c9d8e07 | bellard | #define MAX_NET_CLIENTS 32 |
8141 | c20709aa | bellard | |
8142 | 0824d6fc | bellard | int main(int argc, char **argv) |
8143 | 0824d6fc | bellard | { |
8144 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
8145 | cfc3475a | pbrook | int use_gdbstub;
|
8146 | cfc3475a | pbrook | const char *gdbstub_port; |
8147 | 67b915a5 | bellard | #endif
|
8148 | 28c5af54 | j_mayer | uint32_t boot_devices_bitmap = 0;
|
8149 | e4bcb14c | ths | int i;
|
8150 | 28c5af54 | j_mayer | int snapshot, linux_boot, net_boot;
|
8151 | 7f7f9873 | bellard | const char *initrd_filename; |
8152 | a20dd508 | bellard | const char *kernel_filename, *kernel_cmdline; |
8153 | 28c5af54 | j_mayer | const char *boot_devices = ""; |
8154 | 313aa567 | bellard | DisplayState *ds = &display_state; |
8155 | 46d4767d | bellard | int cyls, heads, secs, translation;
|
8156 | fd5f393a | pbrook | const char *net_clients[MAX_NET_CLIENTS]; |
8157 | 7c9d8e07 | bellard | int nb_net_clients;
|
8158 | e4bcb14c | ths | int hda_index;
|
8159 | cd6f1169 | bellard | int optind;
|
8160 | cd6f1169 | bellard | const char *r, *optarg; |
8161 | 82c643ff | bellard | CharDriverState *monitor_hd; |
8162 | fd5f393a | pbrook | const char *monitor_device; |
8163 | fd5f393a | pbrook | const char *serial_devices[MAX_SERIAL_PORTS]; |
8164 | 8d11df9e | bellard | int serial_device_index;
|
8165 | fd5f393a | pbrook | const char *parallel_devices[MAX_PARALLEL_PORTS]; |
8166 | 6508fe59 | bellard | int parallel_device_index;
|
8167 | d63d307f | bellard | const char *loadvm = NULL; |
8168 | cc1daa40 | bellard | QEMUMachine *machine; |
8169 | 94fc95cd | j_mayer | const char *cpu_model; |
8170 | fd5f393a | pbrook | const char *usb_devices[MAX_USB_CMDLINE]; |
8171 | a594cfbf | bellard | int usb_devices_index;
|
8172 | 71e3ceb8 | ths | int fds[2]; |
8173 | 26a5f13b | bellard | int tb_size;
|
8174 | 93815bc2 | ths | const char *pid_file = NULL; |
8175 | 833c7174 | blueswir1 | VLANState *vlan; |
8176 | 0bd48850 | bellard | |
8177 | 0bd48850 | bellard | LIST_INIT (&vm_change_state_head); |
8178 | be995c27 | bellard | #ifndef _WIN32
|
8179 | be995c27 | bellard | { |
8180 | be995c27 | bellard | struct sigaction act;
|
8181 | be995c27 | bellard | sigfillset(&act.sa_mask); |
8182 | be995c27 | bellard | act.sa_flags = 0;
|
8183 | be995c27 | bellard | act.sa_handler = SIG_IGN; |
8184 | be995c27 | bellard | sigaction(SIGPIPE, &act, NULL);
|
8185 | be995c27 | bellard | } |
8186 | 3587d7e6 | bellard | #else
|
8187 | 3587d7e6 | bellard | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
8188 | a8e5ac33 | bellard | /* Note: cpu_interrupt() is currently not SMP safe, so we force
|
8189 | a8e5ac33 | bellard | QEMU to run on a single CPU */
|
8190 | a8e5ac33 | bellard | { |
8191 | a8e5ac33 | bellard | HANDLE h; |
8192 | a8e5ac33 | bellard | DWORD mask, smask; |
8193 | a8e5ac33 | bellard | int i;
|
8194 | a8e5ac33 | bellard | h = GetCurrentProcess(); |
8195 | a8e5ac33 | bellard | if (GetProcessAffinityMask(h, &mask, &smask)) {
|
8196 | a8e5ac33 | bellard | for(i = 0; i < 32; i++) { |
8197 | a8e5ac33 | bellard | if (mask & (1 << i)) |
8198 | a8e5ac33 | bellard | break;
|
8199 | a8e5ac33 | bellard | } |
8200 | a8e5ac33 | bellard | if (i != 32) { |
8201 | a8e5ac33 | bellard | mask = 1 << i;
|
8202 | a8e5ac33 | bellard | SetProcessAffinityMask(h, mask); |
8203 | a8e5ac33 | bellard | } |
8204 | a8e5ac33 | bellard | } |
8205 | a8e5ac33 | bellard | } |
8206 | 67b915a5 | bellard | #endif
|
8207 | be995c27 | bellard | |
8208 | cc1daa40 | bellard | register_machines(); |
8209 | cc1daa40 | bellard | machine = first_machine; |
8210 | 94fc95cd | j_mayer | cpu_model = NULL;
|
8211 | fc01f7e7 | bellard | initrd_filename = NULL;
|
8212 | 4fc5d071 | aurel32 | ram_size = 0;
|
8213 | 313aa567 | bellard | vga_ram_size = VGA_RAM_SIZE; |
8214 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
8215 | b4608c04 | bellard | use_gdbstub = 0;
|
8216 | c636bb66 | bellard | gdbstub_port = DEFAULT_GDBSTUB_PORT; |
8217 | 67b915a5 | bellard | #endif
|
8218 | 33e3963e | bellard | snapshot = 0;
|
8219 | a20dd508 | bellard | nographic = 0;
|
8220 | 4d3b6f6e | balrog | curses = 0;
|
8221 | a20dd508 | bellard | kernel_filename = NULL;
|
8222 | a20dd508 | bellard | kernel_cmdline = "";
|
8223 | c4b1fcc0 | bellard | cyls = heads = secs = 0;
|
8224 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_AUTO; |
8225 | c60e08d9 | pbrook | monitor_device = "vc";
|
8226 | c4b1fcc0 | bellard | |
8227 | c75a823c | aurel32 | serial_devices[0] = "vc:80Cx24C"; |
8228 | 8d11df9e | bellard | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
8229 | fd5f393a | pbrook | serial_devices[i] = NULL;
|
8230 | 8d11df9e | bellard | serial_device_index = 0;
|
8231 | 3b46e624 | ths | |
8232 | c75a823c | aurel32 | parallel_devices[0] = "vc:640x480"; |
8233 | 6508fe59 | bellard | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
8234 | fd5f393a | pbrook | parallel_devices[i] = NULL;
|
8235 | 6508fe59 | bellard | parallel_device_index = 0;
|
8236 | 3b46e624 | ths | |
8237 | a594cfbf | bellard | usb_devices_index = 0;
|
8238 | 3b46e624 | ths | |
8239 | 7c9d8e07 | bellard | nb_net_clients = 0;
|
8240 | e4bcb14c | ths | nb_drives = 0;
|
8241 | e4bcb14c | ths | nb_drives_opt = 0;
|
8242 | e4bcb14c | ths | hda_index = -1;
|
8243 | 7c9d8e07 | bellard | |
8244 | 7c9d8e07 | bellard | nb_nics = 0;
|
8245 | 3b46e624 | ths | |
8246 | 26a5f13b | bellard | tb_size = 0;
|
8247 | 26a5f13b | bellard | |
8248 | cd6f1169 | bellard | optind = 1;
|
8249 | 0824d6fc | bellard | for(;;) {
|
8250 | cd6f1169 | bellard | if (optind >= argc)
|
8251 | 0824d6fc | bellard | break;
|
8252 | cd6f1169 | bellard | r = argv[optind]; |
8253 | cd6f1169 | bellard | if (r[0] != '-') { |
8254 | 609497ab | balrog | hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
|
8255 | cd6f1169 | bellard | } else {
|
8256 | cd6f1169 | bellard | const QEMUOption *popt;
|
8257 | cd6f1169 | bellard | |
8258 | cd6f1169 | bellard | optind++; |
8259 | dff5efc8 | pbrook | /* Treat --foo the same as -foo. */
|
8260 | dff5efc8 | pbrook | if (r[1] == '-') |
8261 | dff5efc8 | pbrook | r++; |
8262 | cd6f1169 | bellard | popt = qemu_options; |
8263 | cd6f1169 | bellard | for(;;) {
|
8264 | cd6f1169 | bellard | if (!popt->name) {
|
8265 | 5fafdf24 | ths | fprintf(stderr, "%s: invalid option -- '%s'\n",
|
8266 | cd6f1169 | bellard | argv[0], r);
|
8267 | cd6f1169 | bellard | exit(1);
|
8268 | cd6f1169 | bellard | } |
8269 | cd6f1169 | bellard | if (!strcmp(popt->name, r + 1)) |
8270 | cd6f1169 | bellard | break;
|
8271 | cd6f1169 | bellard | popt++; |
8272 | cd6f1169 | bellard | } |
8273 | cd6f1169 | bellard | if (popt->flags & HAS_ARG) {
|
8274 | cd6f1169 | bellard | if (optind >= argc) {
|
8275 | cd6f1169 | bellard | fprintf(stderr, "%s: option '%s' requires an argument\n",
|
8276 | cd6f1169 | bellard | argv[0], r);
|
8277 | cd6f1169 | bellard | exit(1);
|
8278 | cd6f1169 | bellard | } |
8279 | cd6f1169 | bellard | optarg = argv[optind++]; |
8280 | cd6f1169 | bellard | } else {
|
8281 | cd6f1169 | bellard | optarg = NULL;
|
8282 | cd6f1169 | bellard | } |
8283 | cd6f1169 | bellard | |
8284 | cd6f1169 | bellard | switch(popt->index) {
|
8285 | cc1daa40 | bellard | case QEMU_OPTION_M:
|
8286 | cc1daa40 | bellard | machine = find_machine(optarg); |
8287 | cc1daa40 | bellard | if (!machine) {
|
8288 | cc1daa40 | bellard | QEMUMachine *m; |
8289 | cc1daa40 | bellard | printf("Supported machines are:\n");
|
8290 | cc1daa40 | bellard | for(m = first_machine; m != NULL; m = m->next) { |
8291 | cc1daa40 | bellard | printf("%-10s %s%s\n",
|
8292 | 5fafdf24 | ths | m->name, m->desc, |
8293 | cc1daa40 | bellard | m == first_machine ? " (default)" : ""); |
8294 | cc1daa40 | bellard | } |
8295 | 15f82208 | ths | exit(*optarg != '?');
|
8296 | cc1daa40 | bellard | } |
8297 | cc1daa40 | bellard | break;
|
8298 | 94fc95cd | j_mayer | case QEMU_OPTION_cpu:
|
8299 | 94fc95cd | j_mayer | /* hw initialization will check this */
|
8300 | 15f82208 | ths | if (*optarg == '?') { |
8301 | c732abe2 | j_mayer | /* XXX: implement xxx_cpu_list for targets that still miss it */
|
8302 | c732abe2 | j_mayer | #if defined(cpu_list)
|
8303 | c732abe2 | j_mayer | cpu_list(stdout, &fprintf); |
8304 | 94fc95cd | j_mayer | #endif
|
8305 | 15f82208 | ths | exit(0);
|
8306 | 94fc95cd | j_mayer | } else {
|
8307 | 94fc95cd | j_mayer | cpu_model = optarg; |
8308 | 94fc95cd | j_mayer | } |
8309 | 94fc95cd | j_mayer | break;
|
8310 | cd6f1169 | bellard | case QEMU_OPTION_initrd:
|
8311 | fc01f7e7 | bellard | initrd_filename = optarg; |
8312 | fc01f7e7 | bellard | break;
|
8313 | cd6f1169 | bellard | case QEMU_OPTION_hda:
|
8314 | e4bcb14c | ths | if (cyls == 0) |
8315 | 609497ab | balrog | hda_index = drive_add(optarg, HD_ALIAS, 0);
|
8316 | e4bcb14c | ths | else
|
8317 | 609497ab | balrog | hda_index = drive_add(optarg, HD_ALIAS |
8318 | e4bcb14c | ths | ",cyls=%d,heads=%d,secs=%d%s",
|
8319 | 609497ab | balrog | 0, cyls, heads, secs,
|
8320 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_LBA ? |
8321 | e4bcb14c | ths | ",trans=lba" :
|
8322 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_NONE ? |
8323 | e4bcb14c | ths | ",trans=none" : ""); |
8324 | e4bcb14c | ths | break;
|
8325 | cd6f1169 | bellard | case QEMU_OPTION_hdb:
|
8326 | cc1daa40 | bellard | case QEMU_OPTION_hdc:
|
8327 | cc1daa40 | bellard | case QEMU_OPTION_hdd:
|
8328 | 609497ab | balrog | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
8329 | fc01f7e7 | bellard | break;
|
8330 | e4bcb14c | ths | case QEMU_OPTION_drive:
|
8331 | 609497ab | balrog | drive_add(NULL, "%s", optarg); |
8332 | e4bcb14c | ths | break;
|
8333 | 3e3d5815 | balrog | case QEMU_OPTION_mtdblock:
|
8334 | 609497ab | balrog | drive_add(optarg, MTD_ALIAS); |
8335 | 3e3d5815 | balrog | break;
|
8336 | a1bb27b1 | pbrook | case QEMU_OPTION_sd:
|
8337 | 609497ab | balrog | drive_add(optarg, SD_ALIAS); |
8338 | a1bb27b1 | pbrook | break;
|
8339 | 86f55663 | j_mayer | case QEMU_OPTION_pflash:
|
8340 | 609497ab | balrog | drive_add(optarg, PFLASH_ALIAS); |
8341 | 86f55663 | j_mayer | break;
|
8342 | cd6f1169 | bellard | case QEMU_OPTION_snapshot:
|
8343 | 33e3963e | bellard | snapshot = 1;
|
8344 | 33e3963e | bellard | break;
|
8345 | cd6f1169 | bellard | case QEMU_OPTION_hdachs:
|
8346 | 330d0414 | bellard | { |
8347 | 330d0414 | bellard | const char *p; |
8348 | 330d0414 | bellard | p = optarg; |
8349 | 330d0414 | bellard | cyls = strtol(p, (char **)&p, 0); |
8350 | 46d4767d | bellard | if (cyls < 1 || cyls > 16383) |
8351 | 46d4767d | bellard | goto chs_fail;
|
8352 | 330d0414 | bellard | if (*p != ',') |
8353 | 330d0414 | bellard | goto chs_fail;
|
8354 | 330d0414 | bellard | p++; |
8355 | 330d0414 | bellard | heads = strtol(p, (char **)&p, 0); |
8356 | 46d4767d | bellard | if (heads < 1 || heads > 16) |
8357 | 46d4767d | bellard | goto chs_fail;
|
8358 | 330d0414 | bellard | if (*p != ',') |
8359 | 330d0414 | bellard | goto chs_fail;
|
8360 | 330d0414 | bellard | p++; |
8361 | 330d0414 | bellard | secs = strtol(p, (char **)&p, 0); |
8362 | 46d4767d | bellard | if (secs < 1 || secs > 63) |
8363 | 46d4767d | bellard | goto chs_fail;
|
8364 | 46d4767d | bellard | if (*p == ',') { |
8365 | 46d4767d | bellard | p++; |
8366 | 46d4767d | bellard | if (!strcmp(p, "none")) |
8367 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_NONE; |
8368 | 46d4767d | bellard | else if (!strcmp(p, "lba")) |
8369 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_LBA; |
8370 | 46d4767d | bellard | else if (!strcmp(p, "auto")) |
8371 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_AUTO; |
8372 | 46d4767d | bellard | else
|
8373 | 46d4767d | bellard | goto chs_fail;
|
8374 | 46d4767d | bellard | } else if (*p != '\0') { |
8375 | c4b1fcc0 | bellard | chs_fail:
|
8376 | 46d4767d | bellard | fprintf(stderr, "qemu: invalid physical CHS format\n");
|
8377 | 46d4767d | bellard | exit(1);
|
8378 | c4b1fcc0 | bellard | } |
8379 | e4bcb14c | ths | if (hda_index != -1) |
8380 | 609497ab | balrog | snprintf(drives_opt[hda_index].opt, |
8381 | 609497ab | balrog | sizeof(drives_opt[hda_index].opt),
|
8382 | 609497ab | balrog | HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
|
8383 | 609497ab | balrog | 0, cyls, heads, secs,
|
8384 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_LBA ? |
8385 | e4bcb14c | ths | ",trans=lba" :
|
8386 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_NONE ? |
8387 | e4bcb14c | ths | ",trans=none" : ""); |
8388 | 330d0414 | bellard | } |
8389 | 330d0414 | bellard | break;
|
8390 | cd6f1169 | bellard | case QEMU_OPTION_nographic:
|
8391 | fd5f393a | pbrook | serial_devices[0] = "stdio"; |
8392 | fd5f393a | pbrook | parallel_devices[0] = "null"; |
8393 | fd5f393a | pbrook | monitor_device = "stdio";
|
8394 | a20dd508 | bellard | nographic = 1;
|
8395 | a20dd508 | bellard | break;
|
8396 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
8397 | 4d3b6f6e | balrog | case QEMU_OPTION_curses:
|
8398 | 4d3b6f6e | balrog | curses = 1;
|
8399 | 4d3b6f6e | balrog | break;
|
8400 | 4d3b6f6e | balrog | #endif
|
8401 | a171fe39 | balrog | case QEMU_OPTION_portrait:
|
8402 | a171fe39 | balrog | graphic_rotate = 1;
|
8403 | a171fe39 | balrog | break;
|
8404 | cd6f1169 | bellard | case QEMU_OPTION_kernel:
|
8405 | a20dd508 | bellard | kernel_filename = optarg; |
8406 | a20dd508 | bellard | break;
|
8407 | cd6f1169 | bellard | case QEMU_OPTION_append:
|
8408 | a20dd508 | bellard | kernel_cmdline = optarg; |
8409 | 313aa567 | bellard | break;
|
8410 | cd6f1169 | bellard | case QEMU_OPTION_cdrom:
|
8411 | 609497ab | balrog | drive_add(optarg, CDROM_ALIAS); |
8412 | 36b486bb | bellard | break;
|
8413 | cd6f1169 | bellard | case QEMU_OPTION_boot:
|
8414 | 28c5af54 | j_mayer | boot_devices = optarg; |
8415 | 28c5af54 | j_mayer | /* We just do some generic consistency checks */
|
8416 | 28c5af54 | j_mayer | { |
8417 | 28c5af54 | j_mayer | /* Could easily be extended to 64 devices if needed */
|
8418 | 60fe76f3 | ths | const char *p; |
8419 | 28c5af54 | j_mayer | |
8420 | 28c5af54 | j_mayer | boot_devices_bitmap = 0;
|
8421 | 28c5af54 | j_mayer | for (p = boot_devices; *p != '\0'; p++) { |
8422 | 28c5af54 | j_mayer | /* Allowed boot devices are:
|
8423 | 28c5af54 | j_mayer | * a b : floppy disk drives
|
8424 | 28c5af54 | j_mayer | * c ... f : IDE disk drives
|
8425 | 28c5af54 | j_mayer | * g ... m : machine implementation dependant drives
|
8426 | 28c5af54 | j_mayer | * n ... p : network devices
|
8427 | 28c5af54 | j_mayer | * It's up to each machine implementation to check
|
8428 | 28c5af54 | j_mayer | * if the given boot devices match the actual hardware
|
8429 | 28c5af54 | j_mayer | * implementation and firmware features.
|
8430 | 28c5af54 | j_mayer | */
|
8431 | 28c5af54 | j_mayer | if (*p < 'a' || *p > 'q') { |
8432 | 28c5af54 | j_mayer | fprintf(stderr, "Invalid boot device '%c'\n", *p);
|
8433 | 28c5af54 | j_mayer | exit(1);
|
8434 | 28c5af54 | j_mayer | } |
8435 | 28c5af54 | j_mayer | if (boot_devices_bitmap & (1 << (*p - 'a'))) { |
8436 | 28c5af54 | j_mayer | fprintf(stderr, |
8437 | 28c5af54 | j_mayer | "Boot device '%c' was given twice\n",*p);
|
8438 | 28c5af54 | j_mayer | exit(1);
|
8439 | 28c5af54 | j_mayer | } |
8440 | 28c5af54 | j_mayer | boot_devices_bitmap |= 1 << (*p - 'a'); |
8441 | 28c5af54 | j_mayer | } |
8442 | 36b486bb | bellard | } |
8443 | 36b486bb | bellard | break;
|
8444 | cd6f1169 | bellard | case QEMU_OPTION_fda:
|
8445 | cd6f1169 | bellard | case QEMU_OPTION_fdb:
|
8446 | 609497ab | balrog | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
8447 | c45886db | bellard | break;
|
8448 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
8449 | 52ca8d6a | bellard | case QEMU_OPTION_no_fd_bootchk:
|
8450 | 52ca8d6a | bellard | fd_bootchk = 0;
|
8451 | 52ca8d6a | bellard | break;
|
8452 | 52ca8d6a | bellard | #endif
|
8453 | 7c9d8e07 | bellard | case QEMU_OPTION_net:
|
8454 | 7c9d8e07 | bellard | if (nb_net_clients >= MAX_NET_CLIENTS) {
|
8455 | 7c9d8e07 | bellard | fprintf(stderr, "qemu: too many network clients\n");
|
8456 | c4b1fcc0 | bellard | exit(1);
|
8457 | c4b1fcc0 | bellard | } |
8458 | fd5f393a | pbrook | net_clients[nb_net_clients] = optarg; |
8459 | 7c9d8e07 | bellard | nb_net_clients++; |
8460 | 702c651c | bellard | break;
|
8461 | c7f74643 | bellard | #ifdef CONFIG_SLIRP
|
8462 | c7f74643 | bellard | case QEMU_OPTION_tftp:
|
8463 | c7f74643 | bellard | tftp_prefix = optarg; |
8464 | 9bf05444 | bellard | break;
|
8465 | 47d5d01a | ths | case QEMU_OPTION_bootp:
|
8466 | 47d5d01a | ths | bootp_filename = optarg; |
8467 | 47d5d01a | ths | break;
|
8468 | c94c8d64 | bellard | #ifndef _WIN32
|
8469 | 9d728e8c | bellard | case QEMU_OPTION_smb:
|
8470 | 9d728e8c | bellard | net_slirp_smb(optarg); |
8471 | 9d728e8c | bellard | break;
|
8472 | c94c8d64 | bellard | #endif
|
8473 | 9bf05444 | bellard | case QEMU_OPTION_redir:
|
8474 | 3b46e624 | ths | net_slirp_redir(optarg); |
8475 | 9bf05444 | bellard | break;
|
8476 | c7f74643 | bellard | #endif
|
8477 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
8478 | 1d14ffa9 | bellard | case QEMU_OPTION_audio_help:
|
8479 | 1d14ffa9 | bellard | AUD_help (); |
8480 | 1d14ffa9 | bellard | exit (0);
|
8481 | 1d14ffa9 | bellard | break;
|
8482 | 1d14ffa9 | bellard | case QEMU_OPTION_soundhw:
|
8483 | 1d14ffa9 | bellard | select_soundhw (optarg); |
8484 | 1d14ffa9 | bellard | break;
|
8485 | 1d14ffa9 | bellard | #endif
|
8486 | cd6f1169 | bellard | case QEMU_OPTION_h:
|
8487 | 15f82208 | ths | help(0);
|
8488 | cd6f1169 | bellard | break;
|
8489 | 00f82b8a | aurel32 | case QEMU_OPTION_m: {
|
8490 | 00f82b8a | aurel32 | uint64_t value; |
8491 | 00f82b8a | aurel32 | char *ptr;
|
8492 | 00f82b8a | aurel32 | |
8493 | 00f82b8a | aurel32 | value = strtoul(optarg, &ptr, 10);
|
8494 | 00f82b8a | aurel32 | switch (*ptr) {
|
8495 | 00f82b8a | aurel32 | case 0: case 'M': case 'm': |
8496 | 00f82b8a | aurel32 | value <<= 20;
|
8497 | 00f82b8a | aurel32 | break;
|
8498 | 00f82b8a | aurel32 | case 'G': case 'g': |
8499 | 00f82b8a | aurel32 | value <<= 30;
|
8500 | 00f82b8a | aurel32 | break;
|
8501 | 00f82b8a | aurel32 | default:
|
8502 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
|
8503 | cd6f1169 | bellard | exit(1);
|
8504 | cd6f1169 | bellard | } |
8505 | 00f82b8a | aurel32 | |
8506 | 00f82b8a | aurel32 | /* On 32-bit hosts, QEMU is limited by virtual address space */
|
8507 | 00f82b8a | aurel32 | if (value > (2047 << 20) |
8508 | 00f82b8a | aurel32 | #ifndef USE_KQEMU
|
8509 | 00f82b8a | aurel32 | && HOST_LONG_BITS == 32
|
8510 | 00f82b8a | aurel32 | #endif
|
8511 | 00f82b8a | aurel32 | ) { |
8512 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
|
8513 | 00f82b8a | aurel32 | exit(1);
|
8514 | 00f82b8a | aurel32 | } |
8515 | 00f82b8a | aurel32 | if (value != (uint64_t)(ram_addr_t)value) {
|
8516 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: ram size too large\n");
|
8517 | 00f82b8a | aurel32 | exit(1);
|
8518 | 00f82b8a | aurel32 | } |
8519 | 00f82b8a | aurel32 | ram_size = value; |
8520 | cd6f1169 | bellard | break;
|
8521 | 00f82b8a | aurel32 | } |
8522 | cd6f1169 | bellard | case QEMU_OPTION_d:
|
8523 | cd6f1169 | bellard | { |
8524 | cd6f1169 | bellard | int mask;
|
8525 | cd6f1169 | bellard | CPULogItem *item; |
8526 | 3b46e624 | ths | |
8527 | cd6f1169 | bellard | mask = cpu_str_to_log_mask(optarg); |
8528 | cd6f1169 | bellard | if (!mask) {
|
8529 | cd6f1169 | bellard | printf("Log items (comma separated):\n");
|
8530 | f193c797 | bellard | for(item = cpu_log_items; item->mask != 0; item++) { |
8531 | f193c797 | bellard | printf("%-10s %s\n", item->name, item->help);
|
8532 | f193c797 | bellard | } |
8533 | f193c797 | bellard | exit(1);
|
8534 | cd6f1169 | bellard | } |
8535 | cd6f1169 | bellard | cpu_set_log(mask); |
8536 | f193c797 | bellard | } |
8537 | cd6f1169 | bellard | break;
|
8538 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
8539 | cd6f1169 | bellard | case QEMU_OPTION_s:
|
8540 | cd6f1169 | bellard | use_gdbstub = 1;
|
8541 | cd6f1169 | bellard | break;
|
8542 | cd6f1169 | bellard | case QEMU_OPTION_p:
|
8543 | cfc3475a | pbrook | gdbstub_port = optarg; |
8544 | cd6f1169 | bellard | break;
|
8545 | 67b915a5 | bellard | #endif
|
8546 | cd6f1169 | bellard | case QEMU_OPTION_L:
|
8547 | cd6f1169 | bellard | bios_dir = optarg; |
8548 | cd6f1169 | bellard | break;
|
8549 | 1192dad8 | j_mayer | case QEMU_OPTION_bios:
|
8550 | 1192dad8 | j_mayer | bios_name = optarg; |
8551 | 1192dad8 | j_mayer | break;
|
8552 | cd6f1169 | bellard | case QEMU_OPTION_S:
|
8553 | 3c07f8e8 | pbrook | autostart = 0;
|
8554 | cd6f1169 | bellard | break;
|
8555 | 3d11d0eb | bellard | case QEMU_OPTION_k:
|
8556 | 3d11d0eb | bellard | keyboard_layout = optarg; |
8557 | 3d11d0eb | bellard | break;
|
8558 | ee22c2f7 | bellard | case QEMU_OPTION_localtime:
|
8559 | ee22c2f7 | bellard | rtc_utc = 0;
|
8560 | ee22c2f7 | bellard | break;
|
8561 | 1f04275e | bellard | case QEMU_OPTION_cirrusvga:
|
8562 | 1f04275e | bellard | cirrus_vga_enabled = 1;
|
8563 | d34cab9f | ths | vmsvga_enabled = 0;
|
8564 | d34cab9f | ths | break;
|
8565 | d34cab9f | ths | case QEMU_OPTION_vmsvga:
|
8566 | d34cab9f | ths | cirrus_vga_enabled = 0;
|
8567 | d34cab9f | ths | vmsvga_enabled = 1;
|
8568 | 1f04275e | bellard | break;
|
8569 | 1bfe856e | bellard | case QEMU_OPTION_std_vga:
|
8570 | 1bfe856e | bellard | cirrus_vga_enabled = 0;
|
8571 | d34cab9f | ths | vmsvga_enabled = 0;
|
8572 | 1bfe856e | bellard | break;
|
8573 | e9b137c2 | bellard | case QEMU_OPTION_g:
|
8574 | e9b137c2 | bellard | { |
8575 | e9b137c2 | bellard | const char *p; |
8576 | e9b137c2 | bellard | int w, h, depth;
|
8577 | e9b137c2 | bellard | p = optarg; |
8578 | e9b137c2 | bellard | w = strtol(p, (char **)&p, 10); |
8579 | e9b137c2 | bellard | if (w <= 0) { |
8580 | e9b137c2 | bellard | graphic_error:
|
8581 | e9b137c2 | bellard | fprintf(stderr, "qemu: invalid resolution or depth\n");
|
8582 | e9b137c2 | bellard | exit(1);
|
8583 | e9b137c2 | bellard | } |
8584 | e9b137c2 | bellard | if (*p != 'x') |
8585 | e9b137c2 | bellard | goto graphic_error;
|
8586 | e9b137c2 | bellard | p++; |
8587 | e9b137c2 | bellard | h = strtol(p, (char **)&p, 10); |
8588 | e9b137c2 | bellard | if (h <= 0) |
8589 | e9b137c2 | bellard | goto graphic_error;
|
8590 | e9b137c2 | bellard | if (*p == 'x') { |
8591 | e9b137c2 | bellard | p++; |
8592 | e9b137c2 | bellard | depth = strtol(p, (char **)&p, 10); |
8593 | 5fafdf24 | ths | if (depth != 8 && depth != 15 && depth != 16 && |
8594 | e9b137c2 | bellard | depth != 24 && depth != 32) |
8595 | e9b137c2 | bellard | goto graphic_error;
|
8596 | e9b137c2 | bellard | } else if (*p == '\0') { |
8597 | e9b137c2 | bellard | depth = graphic_depth; |
8598 | e9b137c2 | bellard | } else {
|
8599 | e9b137c2 | bellard | goto graphic_error;
|
8600 | e9b137c2 | bellard | } |
8601 | 3b46e624 | ths | |
8602 | e9b137c2 | bellard | graphic_width = w; |
8603 | e9b137c2 | bellard | graphic_height = h; |
8604 | e9b137c2 | bellard | graphic_depth = depth; |
8605 | e9b137c2 | bellard | } |
8606 | e9b137c2 | bellard | break;
|
8607 | 20d8a3ed | ths | case QEMU_OPTION_echr:
|
8608 | 20d8a3ed | ths | { |
8609 | 20d8a3ed | ths | char *r;
|
8610 | 20d8a3ed | ths | term_escape_char = strtol(optarg, &r, 0);
|
8611 | 20d8a3ed | ths | if (r == optarg)
|
8612 | 20d8a3ed | ths | printf("Bad argument to echr\n");
|
8613 | 20d8a3ed | ths | break;
|
8614 | 20d8a3ed | ths | } |
8615 | 82c643ff | bellard | case QEMU_OPTION_monitor:
|
8616 | fd5f393a | pbrook | monitor_device = optarg; |
8617 | 82c643ff | bellard | break;
|
8618 | 82c643ff | bellard | case QEMU_OPTION_serial:
|
8619 | 8d11df9e | bellard | if (serial_device_index >= MAX_SERIAL_PORTS) {
|
8620 | 8d11df9e | bellard | fprintf(stderr, "qemu: too many serial ports\n");
|
8621 | 8d11df9e | bellard | exit(1);
|
8622 | 8d11df9e | bellard | } |
8623 | fd5f393a | pbrook | serial_devices[serial_device_index] = optarg; |
8624 | 8d11df9e | bellard | serial_device_index++; |
8625 | 82c643ff | bellard | break;
|
8626 | 6508fe59 | bellard | case QEMU_OPTION_parallel:
|
8627 | 6508fe59 | bellard | if (parallel_device_index >= MAX_PARALLEL_PORTS) {
|
8628 | 6508fe59 | bellard | fprintf(stderr, "qemu: too many parallel ports\n");
|
8629 | 6508fe59 | bellard | exit(1);
|
8630 | 6508fe59 | bellard | } |
8631 | fd5f393a | pbrook | parallel_devices[parallel_device_index] = optarg; |
8632 | 6508fe59 | bellard | parallel_device_index++; |
8633 | 6508fe59 | bellard | break;
|
8634 | d63d307f | bellard | case QEMU_OPTION_loadvm:
|
8635 | d63d307f | bellard | loadvm = optarg; |
8636 | d63d307f | bellard | break;
|
8637 | d63d307f | bellard | case QEMU_OPTION_full_screen:
|
8638 | d63d307f | bellard | full_screen = 1;
|
8639 | d63d307f | bellard | break;
|
8640 | 667accab | ths | #ifdef CONFIG_SDL
|
8641 | 43523e93 | ths | case QEMU_OPTION_no_frame:
|
8642 | 43523e93 | ths | no_frame = 1;
|
8643 | 43523e93 | ths | break;
|
8644 | 3780e197 | ths | case QEMU_OPTION_alt_grab:
|
8645 | 3780e197 | ths | alt_grab = 1;
|
8646 | 3780e197 | ths | break;
|
8647 | 667accab | ths | case QEMU_OPTION_no_quit:
|
8648 | 667accab | ths | no_quit = 1;
|
8649 | 667accab | ths | break;
|
8650 | 667accab | ths | #endif
|
8651 | f7cce898 | bellard | case QEMU_OPTION_pidfile:
|
8652 | 93815bc2 | ths | pid_file = optarg; |
8653 | f7cce898 | bellard | break;
|
8654 | a09db21f | bellard | #ifdef TARGET_I386
|
8655 | a09db21f | bellard | case QEMU_OPTION_win2k_hack:
|
8656 | a09db21f | bellard | win2k_install_hack = 1;
|
8657 | a09db21f | bellard | break;
|
8658 | a09db21f | bellard | #endif
|
8659 | d993e026 | bellard | #ifdef USE_KQEMU
|
8660 | d993e026 | bellard | case QEMU_OPTION_no_kqemu:
|
8661 | d993e026 | bellard | kqemu_allowed = 0;
|
8662 | d993e026 | bellard | break;
|
8663 | 89bfc105 | bellard | case QEMU_OPTION_kernel_kqemu:
|
8664 | 89bfc105 | bellard | kqemu_allowed = 2;
|
8665 | 89bfc105 | bellard | break;
|
8666 | d993e026 | bellard | #endif
|
8667 | bb36d470 | bellard | case QEMU_OPTION_usb:
|
8668 | bb36d470 | bellard | usb_enabled = 1;
|
8669 | bb36d470 | bellard | break;
|
8670 | a594cfbf | bellard | case QEMU_OPTION_usbdevice:
|
8671 | a594cfbf | bellard | usb_enabled = 1;
|
8672 | 0d92ed30 | pbrook | if (usb_devices_index >= MAX_USB_CMDLINE) {
|
8673 | a594cfbf | bellard | fprintf(stderr, "Too many USB devices\n");
|
8674 | a594cfbf | bellard | exit(1);
|
8675 | a594cfbf | bellard | } |
8676 | fd5f393a | pbrook | usb_devices[usb_devices_index] = optarg; |
8677 | a594cfbf | bellard | usb_devices_index++; |
8678 | a594cfbf | bellard | break;
|
8679 | 6a00d601 | bellard | case QEMU_OPTION_smp:
|
8680 | 6a00d601 | bellard | smp_cpus = atoi(optarg); |
8681 | ba3c64fb | bellard | if (smp_cpus < 1 || smp_cpus > MAX_CPUS) { |
8682 | 6a00d601 | bellard | fprintf(stderr, "Invalid number of CPUs\n");
|
8683 | 6a00d601 | bellard | exit(1);
|
8684 | 6a00d601 | bellard | } |
8685 | 6a00d601 | bellard | break;
|
8686 | 24236869 | bellard | case QEMU_OPTION_vnc:
|
8687 | 73fc9742 | ths | vnc_display = optarg; |
8688 | 24236869 | bellard | break;
|
8689 | 6515b203 | bellard | case QEMU_OPTION_no_acpi:
|
8690 | 6515b203 | bellard | acpi_enabled = 0;
|
8691 | 6515b203 | bellard | break;
|
8692 | d1beab82 | bellard | case QEMU_OPTION_no_reboot:
|
8693 | d1beab82 | bellard | no_reboot = 1;
|
8694 | d1beab82 | bellard | break;
|
8695 | b2f76161 | aurel32 | case QEMU_OPTION_no_shutdown:
|
8696 | b2f76161 | aurel32 | no_shutdown = 1;
|
8697 | b2f76161 | aurel32 | break;
|
8698 | 9467cd46 | balrog | case QEMU_OPTION_show_cursor:
|
8699 | 9467cd46 | balrog | cursor_hide = 0;
|
8700 | 9467cd46 | balrog | break;
|
8701 | 71e3ceb8 | ths | case QEMU_OPTION_daemonize:
|
8702 | 71e3ceb8 | ths | daemonize = 1;
|
8703 | 71e3ceb8 | ths | break;
|
8704 | 9ae02555 | ths | case QEMU_OPTION_option_rom:
|
8705 | 9ae02555 | ths | if (nb_option_roms >= MAX_OPTION_ROMS) {
|
8706 | 9ae02555 | ths | fprintf(stderr, "Too many option ROMs\n");
|
8707 | 9ae02555 | ths | exit(1);
|
8708 | 9ae02555 | ths | } |
8709 | 9ae02555 | ths | option_rom[nb_option_roms] = optarg; |
8710 | 9ae02555 | ths | nb_option_roms++; |
8711 | 9ae02555 | ths | break;
|
8712 | 8e71621f | pbrook | case QEMU_OPTION_semihosting:
|
8713 | 8e71621f | pbrook | semihosting_enabled = 1;
|
8714 | 8e71621f | pbrook | break;
|
8715 | c35734b2 | ths | case QEMU_OPTION_name:
|
8716 | c35734b2 | ths | qemu_name = optarg; |
8717 | c35734b2 | ths | break;
|
8718 | 66508601 | blueswir1 | #ifdef TARGET_SPARC
|
8719 | 66508601 | blueswir1 | case QEMU_OPTION_prom_env:
|
8720 | 66508601 | blueswir1 | if (nb_prom_envs >= MAX_PROM_ENVS) {
|
8721 | 66508601 | blueswir1 | fprintf(stderr, "Too many prom variables\n");
|
8722 | 66508601 | blueswir1 | exit(1);
|
8723 | 66508601 | blueswir1 | } |
8724 | 66508601 | blueswir1 | prom_envs[nb_prom_envs] = optarg; |
8725 | 66508601 | blueswir1 | nb_prom_envs++; |
8726 | 66508601 | blueswir1 | break;
|
8727 | 66508601 | blueswir1 | #endif
|
8728 | 2b8f2d41 | balrog | #ifdef TARGET_ARM
|
8729 | 2b8f2d41 | balrog | case QEMU_OPTION_old_param:
|
8730 | 2b8f2d41 | balrog | old_param = 1;
|
8731 | 05ebd537 | ths | break;
|
8732 | 2b8f2d41 | balrog | #endif
|
8733 | f3dcfada | ths | case QEMU_OPTION_clock:
|
8734 | f3dcfada | ths | configure_alarms(optarg); |
8735 | f3dcfada | ths | break;
|
8736 | 7e0af5d0 | bellard | case QEMU_OPTION_startdate:
|
8737 | 7e0af5d0 | bellard | { |
8738 | 7e0af5d0 | bellard | struct tm tm;
|
8739 | f6503059 | balrog | time_t rtc_start_date; |
8740 | 7e0af5d0 | bellard | if (!strcmp(optarg, "now")) { |
8741 | f6503059 | balrog | rtc_date_offset = -1;
|
8742 | 7e0af5d0 | bellard | } else {
|
8743 | 7e0af5d0 | bellard | if (sscanf(optarg, "%d-%d-%dT%d:%d:%d", |
8744 | 7e0af5d0 | bellard | &tm.tm_year, |
8745 | 7e0af5d0 | bellard | &tm.tm_mon, |
8746 | 7e0af5d0 | bellard | &tm.tm_mday, |
8747 | 7e0af5d0 | bellard | &tm.tm_hour, |
8748 | 7e0af5d0 | bellard | &tm.tm_min, |
8749 | 7e0af5d0 | bellard | &tm.tm_sec) == 6) {
|
8750 | 7e0af5d0 | bellard | /* OK */
|
8751 | 7e0af5d0 | bellard | } else if (sscanf(optarg, "%d-%d-%d", |
8752 | 7e0af5d0 | bellard | &tm.tm_year, |
8753 | 7e0af5d0 | bellard | &tm.tm_mon, |
8754 | 7e0af5d0 | bellard | &tm.tm_mday) == 3) {
|
8755 | 7e0af5d0 | bellard | tm.tm_hour = 0;
|
8756 | 7e0af5d0 | bellard | tm.tm_min = 0;
|
8757 | 7e0af5d0 | bellard | tm.tm_sec = 0;
|
8758 | 7e0af5d0 | bellard | } else {
|
8759 | 7e0af5d0 | bellard | goto date_fail;
|
8760 | 7e0af5d0 | bellard | } |
8761 | 7e0af5d0 | bellard | tm.tm_year -= 1900;
|
8762 | 7e0af5d0 | bellard | tm.tm_mon--; |
8763 | 3c6b2088 | bellard | rtc_start_date = mktimegm(&tm); |
8764 | 7e0af5d0 | bellard | if (rtc_start_date == -1) { |
8765 | 7e0af5d0 | bellard | date_fail:
|
8766 | 7e0af5d0 | bellard | fprintf(stderr, "Invalid date format. Valid format are:\n"
|
8767 | 7e0af5d0 | bellard | "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
|
8768 | 7e0af5d0 | bellard | exit(1);
|
8769 | 7e0af5d0 | bellard | } |
8770 | f6503059 | balrog | rtc_date_offset = time(NULL) - rtc_start_date;
|
8771 | 7e0af5d0 | bellard | } |
8772 | 7e0af5d0 | bellard | } |
8773 | 7e0af5d0 | bellard | break;
|
8774 | 26a5f13b | bellard | case QEMU_OPTION_tb_size:
|
8775 | 26a5f13b | bellard | tb_size = strtol(optarg, NULL, 0); |
8776 | 26a5f13b | bellard | if (tb_size < 0) |
8777 | 26a5f13b | bellard | tb_size = 0;
|
8778 | 26a5f13b | bellard | break;
|
8779 | 2e70f6ef | pbrook | case QEMU_OPTION_icount:
|
8780 | 2e70f6ef | pbrook | use_icount = 1;
|
8781 | 2e70f6ef | pbrook | if (strcmp(optarg, "auto") == 0) { |
8782 | 2e70f6ef | pbrook | icount_time_shift = -1;
|
8783 | 2e70f6ef | pbrook | } else {
|
8784 | 2e70f6ef | pbrook | icount_time_shift = strtol(optarg, NULL, 0); |
8785 | 2e70f6ef | pbrook | } |
8786 | 2e70f6ef | pbrook | break;
|
8787 | cd6f1169 | bellard | } |
8788 | 0824d6fc | bellard | } |
8789 | 0824d6fc | bellard | } |
8790 | 330d0414 | bellard | |
8791 | 71e3ceb8 | ths | #ifndef _WIN32
|
8792 | 71e3ceb8 | ths | if (daemonize) {
|
8793 | 71e3ceb8 | ths | pid_t pid; |
8794 | 71e3ceb8 | ths | |
8795 | 71e3ceb8 | ths | if (pipe(fds) == -1) |
8796 | 71e3ceb8 | ths | exit(1);
|
8797 | 71e3ceb8 | ths | |
8798 | 71e3ceb8 | ths | pid = fork(); |
8799 | 71e3ceb8 | ths | if (pid > 0) { |
8800 | 71e3ceb8 | ths | uint8_t status; |
8801 | 71e3ceb8 | ths | ssize_t len; |
8802 | 71e3ceb8 | ths | |
8803 | 71e3ceb8 | ths | close(fds[1]);
|
8804 | 71e3ceb8 | ths | |
8805 | 71e3ceb8 | ths | again:
|
8806 | 93815bc2 | ths | len = read(fds[0], &status, 1); |
8807 | 93815bc2 | ths | if (len == -1 && (errno == EINTR)) |
8808 | 93815bc2 | ths | goto again;
|
8809 | 93815bc2 | ths | |
8810 | 93815bc2 | ths | if (len != 1) |
8811 | 93815bc2 | ths | exit(1);
|
8812 | 93815bc2 | ths | else if (status == 1) { |
8813 | 93815bc2 | ths | fprintf(stderr, "Could not acquire pidfile\n");
|
8814 | 93815bc2 | ths | exit(1);
|
8815 | 93815bc2 | ths | } else
|
8816 | 93815bc2 | ths | exit(0);
|
8817 | 71e3ceb8 | ths | } else if (pid < 0) |
8818 | 93815bc2 | ths | exit(1);
|
8819 | 71e3ceb8 | ths | |
8820 | 71e3ceb8 | ths | setsid(); |
8821 | 71e3ceb8 | ths | |
8822 | 71e3ceb8 | ths | pid = fork(); |
8823 | 71e3ceb8 | ths | if (pid > 0) |
8824 | 71e3ceb8 | ths | exit(0);
|
8825 | 71e3ceb8 | ths | else if (pid < 0) |
8826 | 71e3ceb8 | ths | exit(1);
|
8827 | 71e3ceb8 | ths | |
8828 | 71e3ceb8 | ths | umask(027);
|
8829 | 71e3ceb8 | ths | |
8830 | 71e3ceb8 | ths | signal(SIGTSTP, SIG_IGN); |
8831 | 71e3ceb8 | ths | signal(SIGTTOU, SIG_IGN); |
8832 | 71e3ceb8 | ths | signal(SIGTTIN, SIG_IGN); |
8833 | 71e3ceb8 | ths | } |
8834 | 71e3ceb8 | ths | #endif
|
8835 | 71e3ceb8 | ths | |
8836 | aa26bb2d | ths | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
8837 | 93815bc2 | ths | if (daemonize) {
|
8838 | 93815bc2 | ths | uint8_t status = 1;
|
8839 | 93815bc2 | ths | write(fds[1], &status, 1); |
8840 | 93815bc2 | ths | } else
|
8841 | 93815bc2 | ths | fprintf(stderr, "Could not acquire pid file\n");
|
8842 | 93815bc2 | ths | exit(1);
|
8843 | 93815bc2 | ths | } |
8844 | 93815bc2 | ths | |
8845 | ff3fbb30 | bellard | #ifdef USE_KQEMU
|
8846 | ff3fbb30 | bellard | if (smp_cpus > 1) |
8847 | ff3fbb30 | bellard | kqemu_allowed = 0;
|
8848 | ff3fbb30 | bellard | #endif
|
8849 | a20dd508 | bellard | linux_boot = (kernel_filename != NULL);
|
8850 | 7317b8ca | balrog | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
8851 | 6c41b272 | balrog | |
8852 | 28c5af54 | j_mayer | /* XXX: this should not be: some embedded targets just have flash */
|
8853 | 28c5af54 | j_mayer | if (!linux_boot && net_boot == 0 && |
8854 | e4bcb14c | ths | nb_drives_opt == 0)
|
8855 | 15f82208 | ths | help(1);
|
8856 | 0824d6fc | bellard | |
8857 | f8d39c01 | ths | if (!linux_boot && *kernel_cmdline != '\0') { |
8858 | f8d39c01 | ths | fprintf(stderr, "-append only allowed with -kernel option\n");
|
8859 | f8d39c01 | ths | exit(1);
|
8860 | f8d39c01 | ths | } |
8861 | f8d39c01 | ths | |
8862 | f8d39c01 | ths | if (!linux_boot && initrd_filename != NULL) { |
8863 | f8d39c01 | ths | fprintf(stderr, "-initrd only allowed with -kernel option\n");
|
8864 | f8d39c01 | ths | exit(1);
|
8865 | f8d39c01 | ths | } |
8866 | f8d39c01 | ths | |
8867 | 96d30e48 | ths | /* boot to floppy or the default cd if no hard disk defined yet */
|
8868 | 28c5af54 | j_mayer | if (!boot_devices[0]) { |
8869 | e4bcb14c | ths | boot_devices = "cad";
|
8870 | 96d30e48 | ths | } |
8871 | b118d61e | bellard | setvbuf(stdout, NULL, _IOLBF, 0); |
8872 | 3b46e624 | ths | |
8873 | 634fce96 | pbrook | init_timers(); |
8874 | 634fce96 | pbrook | init_timer_alarm(); |
8875 | 83f64091 | bellard | qemu_aio_init(); |
8876 | 2e70f6ef | pbrook | if (use_icount && icount_time_shift < 0) { |
8877 | 2e70f6ef | pbrook | use_icount = 2;
|
8878 | 2e70f6ef | pbrook | /* 125MIPS seems a reasonable initial guess at the guest speed.
|
8879 | 2e70f6ef | pbrook | It will be corrected fairly quickly anyway. */
|
8880 | 2e70f6ef | pbrook | icount_time_shift = 3;
|
8881 | 2e70f6ef | pbrook | init_icount_adjust(); |
8882 | 2e70f6ef | pbrook | } |
8883 | 634fce96 | pbrook | |
8884 | fd1dff4b | bellard | #ifdef _WIN32
|
8885 | fd1dff4b | bellard | socket_init(); |
8886 | fd1dff4b | bellard | #endif
|
8887 | fd1dff4b | bellard | |
8888 | 7c9d8e07 | bellard | /* init network clients */
|
8889 | 7c9d8e07 | bellard | if (nb_net_clients == 0) { |
8890 | 7c9d8e07 | bellard | /* if no clients, we use a default config */
|
8891 | fd5f393a | pbrook | net_clients[0] = "nic"; |
8892 | fd5f393a | pbrook | net_clients[1] = "user"; |
8893 | 7c9d8e07 | bellard | nb_net_clients = 2;
|
8894 | c20709aa | bellard | } |
8895 | c20709aa | bellard | |
8896 | 7c9d8e07 | bellard | for(i = 0;i < nb_net_clients; i++) { |
8897 | 9ad97e65 | balrog | if (net_client_parse(net_clients[i]) < 0) |
8898 | 7c9d8e07 | bellard | exit(1);
|
8899 | 702c651c | bellard | } |
8900 | 833c7174 | blueswir1 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
8901 | 833c7174 | blueswir1 | if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) |
8902 | 833c7174 | blueswir1 | continue;
|
8903 | 9ad97e65 | balrog | if (vlan->nb_guest_devs == 0) |
8904 | 9ad97e65 | balrog | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
|
8905 | 833c7174 | blueswir1 | if (vlan->nb_host_devs == 0) |
8906 | 833c7174 | blueswir1 | fprintf(stderr, |
8907 | 833c7174 | blueswir1 | "Warning: vlan %d is not connected to host network\n",
|
8908 | 833c7174 | blueswir1 | vlan->id); |
8909 | 833c7174 | blueswir1 | } |
8910 | f1510b2c | bellard | |
8911 | eec85c2a | ths | #ifdef TARGET_I386
|
8912 | ed494d87 | balrog | /* XXX: this should be moved in the PC machine instantiation code */
|
8913 | 28c5af54 | j_mayer | if (net_boot != 0) { |
8914 | 28c5af54 | j_mayer | int netroms = 0; |
8915 | 28c5af54 | j_mayer | for (i = 0; i < nb_nics && i < 4; i++) { |
8916 | eec85c2a | ths | const char *model = nd_table[i].model; |
8917 | eec85c2a | ths | char buf[1024]; |
8918 | 28c5af54 | j_mayer | if (net_boot & (1 << i)) { |
8919 | 28c5af54 | j_mayer | if (model == NULL) |
8920 | 28c5af54 | j_mayer | model = "ne2k_pci";
|
8921 | 28c5af54 | j_mayer | snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model); |
8922 | 28c5af54 | j_mayer | if (get_image_size(buf) > 0) { |
8923 | 28c5af54 | j_mayer | if (nb_option_roms >= MAX_OPTION_ROMS) {
|
8924 | 28c5af54 | j_mayer | fprintf(stderr, "Too many option ROMs\n");
|
8925 | 28c5af54 | j_mayer | exit(1);
|
8926 | 28c5af54 | j_mayer | } |
8927 | 28c5af54 | j_mayer | option_rom[nb_option_roms] = strdup(buf); |
8928 | 28c5af54 | j_mayer | nb_option_roms++; |
8929 | 28c5af54 | j_mayer | netroms++; |
8930 | 28c5af54 | j_mayer | } |
8931 | 28c5af54 | j_mayer | } |
8932 | eec85c2a | ths | } |
8933 | 28c5af54 | j_mayer | if (netroms == 0) { |
8934 | eec85c2a | ths | fprintf(stderr, "No valid PXE rom found for network device\n");
|
8935 | eec85c2a | ths | exit(1);
|
8936 | eec85c2a | ths | } |
8937 | eec85c2a | ths | } |
8938 | eec85c2a | ths | #endif
|
8939 | eec85c2a | ths | |
8940 | 0824d6fc | bellard | /* init the memory */
|
8941 | 7fb4fdcf | balrog | phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED; |
8942 | 7fb4fdcf | balrog | |
8943 | 7fb4fdcf | balrog | if (machine->ram_require & RAMSIZE_FIXED) {
|
8944 | 7fb4fdcf | balrog | if (ram_size > 0) { |
8945 | 7fb4fdcf | balrog | if (ram_size < phys_ram_size) {
|
8946 | cd940061 | aurel32 | fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
|
8947 | cd940061 | aurel32 | machine->name, (unsigned long long) phys_ram_size); |
8948 | 7fb4fdcf | balrog | exit(-1);
|
8949 | 7fb4fdcf | balrog | } |
8950 | 7fb4fdcf | balrog | |
8951 | 7fb4fdcf | balrog | phys_ram_size = ram_size; |
8952 | 7fb4fdcf | balrog | } else
|
8953 | 7fb4fdcf | balrog | ram_size = phys_ram_size; |
8954 | 7fb4fdcf | balrog | } else {
|
8955 | 4fc5d071 | aurel32 | if (ram_size == 0) |
8956 | 7fb4fdcf | balrog | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
8957 | 7fb4fdcf | balrog | |
8958 | 7fb4fdcf | balrog | phys_ram_size += ram_size; |
8959 | 7fb4fdcf | balrog | } |
8960 | 9ae02555 | ths | |
8961 | d993e026 | bellard | phys_ram_base = qemu_vmalloc(phys_ram_size); |
8962 | 7f7f9873 | bellard | if (!phys_ram_base) {
|
8963 | 7f7f9873 | bellard | fprintf(stderr, "Could not allocate physical memory\n");
|
8964 | 0824d6fc | bellard | exit(1);
|
8965 | 0824d6fc | bellard | } |
8966 | 0824d6fc | bellard | |
8967 | 26a5f13b | bellard | /* init the dynamic translator */
|
8968 | 26a5f13b | bellard | cpu_exec_init_all(tb_size * 1024 * 1024); |
8969 | 26a5f13b | bellard | |
8970 | 5905b2e5 | bellard | bdrv_init(); |
8971 | c4b1fcc0 | bellard | |
8972 | e4bcb14c | ths | /* we always create the cdrom drive, even if no disk is there */
|
8973 | c4b1fcc0 | bellard | |
8974 | e4bcb14c | ths | if (nb_drives_opt < MAX_DRIVES)
|
8975 | 609497ab | balrog | drive_add(NULL, CDROM_ALIAS);
|
8976 | c4b1fcc0 | bellard | |
8977 | 9d413d1d | balrog | /* we always create at least one floppy */
|
8978 | 33e3963e | bellard | |
8979 | e4bcb14c | ths | if (nb_drives_opt < MAX_DRIVES)
|
8980 | 609497ab | balrog | drive_add(NULL, FD_ALIAS, 0); |
8981 | 86f55663 | j_mayer | |
8982 | 9d413d1d | balrog | /* we always create one sd slot, even if no card is in it */
|
8983 | 9d413d1d | balrog | |
8984 | 9d413d1d | balrog | if (nb_drives_opt < MAX_DRIVES)
|
8985 | 609497ab | balrog | drive_add(NULL, SD_ALIAS);
|
8986 | 9d413d1d | balrog | |
8987 | e4bcb14c | ths | /* open the virtual block devices */
|
8988 | e4bcb14c | ths | |
8989 | e4bcb14c | ths | for(i = 0; i < nb_drives_opt; i++) |
8990 | 609497ab | balrog | if (drive_init(&drives_opt[i], snapshot, machine) == -1) |
8991 | e4bcb14c | ths | exit(1);
|
8992 | 3e3d5815 | balrog | |
8993 | c88676f8 | bellard | register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
8994 | c88676f8 | bellard | register_savevm("ram", 0, 2, ram_save, ram_load, NULL); |
8995 | 8a7ddc38 | bellard | |
8996 | 313aa567 | bellard | /* terminal init */
|
8997 | 740733bb | ths | memset(&display_state, 0, sizeof(display_state)); |
8998 | a20dd508 | bellard | if (nographic) {
|
8999 | 4d3b6f6e | balrog | if (curses) {
|
9000 | 4d3b6f6e | balrog | fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
|
9001 | 4d3b6f6e | balrog | exit(1);
|
9002 | 4d3b6f6e | balrog | } |
9003 | 2ff89790 | ths | /* nearly nothing to do */
|
9004 | 2ff89790 | ths | dumb_display_init(ds); |
9005 | 73fc9742 | ths | } else if (vnc_display != NULL) { |
9006 | 71cab5ca | ths | vnc_display_init(ds); |
9007 | 71cab5ca | ths | if (vnc_display_open(ds, vnc_display) < 0) |
9008 | 71cab5ca | ths | exit(1);
|
9009 | 4d3b6f6e | balrog | } else
|
9010 | 4d3b6f6e | balrog | #if defined(CONFIG_CURSES)
|
9011 | 4d3b6f6e | balrog | if (curses) {
|
9012 | 4d3b6f6e | balrog | curses_display_init(ds, full_screen); |
9013 | 4d3b6f6e | balrog | } else
|
9014 | 4d3b6f6e | balrog | #endif
|
9015 | 4d3b6f6e | balrog | { |
9016 | 5b0753e0 | bellard | #if defined(CONFIG_SDL)
|
9017 | 43523e93 | ths | sdl_display_init(ds, full_screen, no_frame); |
9018 | 5b0753e0 | bellard | #elif defined(CONFIG_COCOA)
|
9019 | 5b0753e0 | bellard | cocoa_display_init(ds, full_screen); |
9020 | 67276f53 | pbrook | #else
|
9021 | 67276f53 | pbrook | dumb_display_init(ds); |
9022 | 313aa567 | bellard | #endif
|
9023 | 313aa567 | bellard | } |
9024 | 0824d6fc | bellard | |
9025 | 20d8a3ed | ths | /* Maintain compatibility with multiple stdio monitors */
|
9026 | 20d8a3ed | ths | if (!strcmp(monitor_device,"stdio")) { |
9027 | 20d8a3ed | ths | for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
9028 | fd5f393a | pbrook | const char *devname = serial_devices[i]; |
9029 | fd5f393a | pbrook | if (devname && !strcmp(devname,"mon:stdio")) { |
9030 | fd5f393a | pbrook | monitor_device = NULL;
|
9031 | 20d8a3ed | ths | break;
|
9032 | fd5f393a | pbrook | } else if (devname && !strcmp(devname,"stdio")) { |
9033 | fd5f393a | pbrook | monitor_device = NULL;
|
9034 | fd5f393a | pbrook | serial_devices[i] = "mon:stdio";
|
9035 | 20d8a3ed | ths | break;
|
9036 | 20d8a3ed | ths | } |
9037 | 20d8a3ed | ths | } |
9038 | 20d8a3ed | ths | } |
9039 | fd5f393a | pbrook | if (monitor_device) {
|
9040 | 20d8a3ed | ths | monitor_hd = qemu_chr_open(monitor_device); |
9041 | 20d8a3ed | ths | if (!monitor_hd) {
|
9042 | 20d8a3ed | ths | fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
|
9043 | 20d8a3ed | ths | exit(1);
|
9044 | 20d8a3ed | ths | } |
9045 | 20d8a3ed | ths | monitor_init(monitor_hd, !nographic); |
9046 | 82c643ff | bellard | } |
9047 | 82c643ff | bellard | |
9048 | 8d11df9e | bellard | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
9049 | c03b0f0f | bellard | const char *devname = serial_devices[i]; |
9050 | fd5f393a | pbrook | if (devname && strcmp(devname, "none")) { |
9051 | c03b0f0f | bellard | serial_hds[i] = qemu_chr_open(devname); |
9052 | 8d11df9e | bellard | if (!serial_hds[i]) {
|
9053 | 5fafdf24 | ths | fprintf(stderr, "qemu: could not open serial device '%s'\n",
|
9054 | c03b0f0f | bellard | devname); |
9055 | 8d11df9e | bellard | exit(1);
|
9056 | 8d11df9e | bellard | } |
9057 | af3a9031 | ths | if (strstart(devname, "vc", 0)) |
9058 | 7ba1260a | bellard | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
|
9059 | 8d11df9e | bellard | } |
9060 | 82c643ff | bellard | } |
9061 | 82c643ff | bellard | |
9062 | 6508fe59 | bellard | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
9063 | c03b0f0f | bellard | const char *devname = parallel_devices[i]; |
9064 | fd5f393a | pbrook | if (devname && strcmp(devname, "none")) { |
9065 | c03b0f0f | bellard | parallel_hds[i] = qemu_chr_open(devname); |
9066 | 6508fe59 | bellard | if (!parallel_hds[i]) {
|
9067 | 5fafdf24 | ths | fprintf(stderr, "qemu: could not open parallel device '%s'\n",
|
9068 | c03b0f0f | bellard | devname); |
9069 | 6508fe59 | bellard | exit(1);
|
9070 | 6508fe59 | bellard | } |
9071 | af3a9031 | ths | if (strstart(devname, "vc", 0)) |
9072 | 7ba1260a | bellard | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
|
9073 | 6508fe59 | bellard | } |
9074 | 6508fe59 | bellard | } |
9075 | 6508fe59 | bellard | |
9076 | b881c2c6 | blueswir1 | machine->init(ram_size, vga_ram_size, boot_devices, ds, |
9077 | 94fc95cd | j_mayer | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
9078 | 73332e5c | bellard | |
9079 | 0d92ed30 | pbrook | /* init USB devices */
|
9080 | 0d92ed30 | pbrook | if (usb_enabled) {
|
9081 | 0d92ed30 | pbrook | for(i = 0; i < usb_devices_index; i++) { |
9082 | 0d92ed30 | pbrook | if (usb_device_add(usb_devices[i]) < 0) { |
9083 | 0d92ed30 | pbrook | fprintf(stderr, "Warning: could not add USB device %s\n",
|
9084 | 0d92ed30 | pbrook | usb_devices[i]); |
9085 | 0d92ed30 | pbrook | } |
9086 | 0d92ed30 | pbrook | } |
9087 | 0d92ed30 | pbrook | } |
9088 | 0d92ed30 | pbrook | |
9089 | 740733bb | ths | if (display_state.dpy_refresh) {
|
9090 | 740733bb | ths | display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state); |
9091 | 740733bb | ths | qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock)); |
9092 | 740733bb | ths | } |
9093 | 7f7f9873 | bellard | |
9094 | 67b915a5 | bellard | #ifdef CONFIG_GDBSTUB
|
9095 | b4608c04 | bellard | if (use_gdbstub) {
|
9096 | c636bb66 | bellard | /* XXX: use standard host:port notation and modify options
|
9097 | c636bb66 | bellard | accordingly. */
|
9098 | cfc3475a | pbrook | if (gdbserver_start(gdbstub_port) < 0) { |
9099 | cfc3475a | pbrook | fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
|
9100 | c636bb66 | bellard | gdbstub_port); |
9101 | 8a7ddc38 | bellard | exit(1);
|
9102 | 8a7ddc38 | bellard | } |
9103 | 45669e00 | balrog | } |
9104 | 67b915a5 | bellard | #endif
|
9105 | 45669e00 | balrog | |
9106 | d63d307f | bellard | if (loadvm)
|
9107 | faea38e7 | bellard | do_loadvm(loadvm); |
9108 | d63d307f | bellard | |
9109 | 67b915a5 | bellard | { |
9110 | 5905b2e5 | bellard | /* XXX: simplify init */
|
9111 | 5905b2e5 | bellard | read_passwords(); |
9112 | 3c07f8e8 | pbrook | if (autostart) {
|
9113 | 5905b2e5 | bellard | vm_start(); |
9114 | 5905b2e5 | bellard | } |
9115 | 0824d6fc | bellard | } |
9116 | ffd843bc | ths | |
9117 | 71e3ceb8 | ths | if (daemonize) {
|
9118 | 71e3ceb8 | ths | uint8_t status = 0;
|
9119 | 71e3ceb8 | ths | ssize_t len; |
9120 | 71e3ceb8 | ths | int fd;
|
9121 | 71e3ceb8 | ths | |
9122 | 71e3ceb8 | ths | again1:
|
9123 | 71e3ceb8 | ths | len = write(fds[1], &status, 1); |
9124 | 71e3ceb8 | ths | if (len == -1 && (errno == EINTR)) |
9125 | 71e3ceb8 | ths | goto again1;
|
9126 | 71e3ceb8 | ths | |
9127 | 71e3ceb8 | ths | if (len != 1) |
9128 | 71e3ceb8 | ths | exit(1);
|
9129 | 71e3ceb8 | ths | |
9130 | bd54b863 | aliguori | chdir("/");
|
9131 | aeb30be6 | balrog | TFR(fd = open("/dev/null", O_RDWR));
|
9132 | 71e3ceb8 | ths | if (fd == -1) |
9133 | 71e3ceb8 | ths | exit(1);
|
9134 | 71e3ceb8 | ths | |
9135 | 71e3ceb8 | ths | dup2(fd, 0);
|
9136 | 71e3ceb8 | ths | dup2(fd, 1);
|
9137 | 71e3ceb8 | ths | dup2(fd, 2);
|
9138 | 71e3ceb8 | ths | |
9139 | 71e3ceb8 | ths | close(fd); |
9140 | 71e3ceb8 | ths | } |
9141 | 71e3ceb8 | ths | |
9142 | 8a7ddc38 | bellard | main_loop(); |
9143 | 40c3bac3 | bellard | quit_timers(); |
9144 | b46a8906 | ths | |
9145 | 7d294b61 | ths | #if !defined(_WIN32)
|
9146 | b46a8906 | ths | /* close network clients */
|
9147 | b46a8906 | ths | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
9148 | b46a8906 | ths | VLANClientState *vc; |
9149 | b46a8906 | ths | |
9150 | 7d294b61 | ths | for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
9151 | b46a8906 | ths | if (vc->fd_read == tap_receive) {
|
9152 | b46a8906 | ths | char ifname[64]; |
9153 | b46a8906 | ths | TAPState *s = vc->opaque; |
9154 | b46a8906 | ths | |
9155 | b46a8906 | ths | if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 && |
9156 | b46a8906 | ths | s->down_script[0])
|
9157 | b46a8906 | ths | launch_script(s->down_script, ifname, s->fd); |
9158 | b46a8906 | ths | } |
9159 | 8a16d273 | ths | #if defined(CONFIG_VDE)
|
9160 | 8a16d273 | ths | if (vc->fd_read == vde_from_qemu) {
|
9161 | 8a16d273 | ths | VDEState *s = vc->opaque; |
9162 | 8a16d273 | ths | vde_close(s->vde); |
9163 | 8a16d273 | ths | } |
9164 | 8a16d273 | ths | #endif
|
9165 | 4fddf62a | ths | } |
9166 | 7d294b61 | ths | } |
9167 | 7d294b61 | ths | #endif
|
9168 | 0824d6fc | bellard | return 0; |
9169 | 0824d6fc | bellard | } |