root / vl.c @ 496eb021
History | View | Annotate | Download (152.2 kB)
1 | 0824d6fc | bellard | /*
|
---|---|---|---|
2 | 80cabfad | bellard | * QEMU System Emulator
|
3 | 5fafdf24 | ths | *
|
4 | 68d0f70e | bellard | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | 1df912cf | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 1df912cf | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 1df912cf | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 1df912cf | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 1df912cf | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 1df912cf | bellard | * furnished to do so, subject to the following conditions:
|
12 | 1df912cf | bellard | *
|
13 | 1df912cf | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 1df912cf | bellard | * all copies or substantial portions of the Software.
|
15 | 1df912cf | bellard | *
|
16 | 1df912cf | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 1df912cf | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 1df912cf | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 1df912cf | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 1df912cf | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 1df912cf | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 1df912cf | bellard | * THE SOFTWARE.
|
23 | 0824d6fc | bellard | */
|
24 | 0824d6fc | bellard | #include <unistd.h> |
25 | 0824d6fc | bellard | #include <fcntl.h> |
26 | 0824d6fc | bellard | #include <signal.h> |
27 | 0824d6fc | bellard | #include <time.h> |
28 | 0824d6fc | bellard | #include <errno.h> |
29 | 67b915a5 | bellard | #include <sys/time.h> |
30 | c88676f8 | bellard | #include <zlib.h> |
31 | 67b915a5 | bellard | |
32 | 71e72a19 | Juan Quintela | /* Needed early for CONFIG_BSD etc. */
|
33 | d40cdb10 | blueswir1 | #include "config-host.h" |
34 | d40cdb10 | blueswir1 | |
35 | 67b915a5 | bellard | #ifndef _WIN32
|
36 | 5cea8590 | Paul Brook | #include <libgen.h> |
37 | 0858532e | aliguori | #include <pwd.h> |
38 | 67b915a5 | bellard | #include <sys/times.h> |
39 | f1510b2c | bellard | #include <sys/wait.h> |
40 | 67b915a5 | bellard | #include <termios.h> |
41 | 67b915a5 | bellard | #include <sys/mman.h> |
42 | f1510b2c | bellard | #include <sys/ioctl.h> |
43 | 24646c7e | blueswir1 | #include <sys/resource.h> |
44 | f1510b2c | bellard | #include <sys/socket.h> |
45 | c94c8d64 | bellard | #include <netinet/in.h> |
46 | 24646c7e | blueswir1 | #include <net/if.h> |
47 | 24646c7e | blueswir1 | #if defined(__NetBSD__)
|
48 | 24646c7e | blueswir1 | #include <net/if_tap.h> |
49 | 24646c7e | blueswir1 | #endif
|
50 | 24646c7e | blueswir1 | #ifdef __linux__
|
51 | 24646c7e | blueswir1 | #include <linux/if_tun.h> |
52 | 24646c7e | blueswir1 | #endif
|
53 | 24646c7e | blueswir1 | #include <arpa/inet.h> |
54 | 9d728e8c | bellard | #include <dirent.h> |
55 | 7c9d8e07 | bellard | #include <netdb.h> |
56 | cb4b976b | ths | #include <sys/select.h> |
57 | 71e72a19 | Juan Quintela | #ifdef CONFIG_BSD
|
58 | 7d3505c5 | bellard | #include <sys/stat.h> |
59 | c5e97233 | blueswir1 | #if defined(__FreeBSD__) || defined(__DragonFly__)
|
60 | 7d3505c5 | bellard | #include <libutil.h> |
61 | 24646c7e | blueswir1 | #else
|
62 | 24646c7e | blueswir1 | #include <util.h> |
63 | 128ab2ff | blueswir1 | #endif
|
64 | 5c40d2bd | ths | #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
|
65 | 5c40d2bd | ths | #include <freebsd/stdlib.h> |
66 | 7d3505c5 | bellard | #else
|
67 | 223f0d72 | blueswir1 | #ifdef __linux__
|
68 | 7d3505c5 | bellard | #include <pty.h> |
69 | 7d3505c5 | bellard | #include <malloc.h> |
70 | fd872598 | bellard | #include <linux/rtc.h> |
71 | 1889465a | Andi Kleen | #include <sys/prctl.h> |
72 | bd494f4c | ths | |
73 | bd494f4c | ths | /* For the benefit of older linux systems which don't supply it,
|
74 | bd494f4c | ths | we use a local copy of hpet.h. */
|
75 | bd494f4c | ths | /* #include <linux/hpet.h> */
|
76 | bd494f4c | ths | #include "hpet.h" |
77 | bd494f4c | ths | |
78 | e57a8c0e | bellard | #include <linux/ppdev.h> |
79 | 5867c88a | ths | #include <linux/parport.h> |
80 | 223f0d72 | blueswir1 | #endif
|
81 | 223f0d72 | blueswir1 | #ifdef __sun__
|
82 | d5d10bc3 | ths | #include <sys/stat.h> |
83 | d5d10bc3 | ths | #include <sys/ethernet.h> |
84 | d5d10bc3 | ths | #include <sys/sockio.h> |
85 | d5d10bc3 | ths | #include <netinet/arp.h> |
86 | d5d10bc3 | ths | #include <netinet/in.h> |
87 | d5d10bc3 | ths | #include <netinet/in_systm.h> |
88 | d5d10bc3 | ths | #include <netinet/ip.h> |
89 | d5d10bc3 | ths | #include <netinet/ip_icmp.h> // must come after ip.h |
90 | d5d10bc3 | ths | #include <netinet/udp.h> |
91 | d5d10bc3 | ths | #include <netinet/tcp.h> |
92 | d5d10bc3 | ths | #include <net/if.h> |
93 | d5d10bc3 | ths | #include <syslog.h> |
94 | d5d10bc3 | ths | #include <stropts.h> |
95 | 8d32cf0e | Blue Swirl | /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
|
96 | 8d32cf0e | Blue Swirl | discussion about Solaris header problems */
|
97 | 8d32cf0e | Blue Swirl | extern int madvise(caddr_t, size_t, int); |
98 | 67b915a5 | bellard | #endif
|
99 | 7d3505c5 | bellard | #endif
|
100 | ec530c81 | bellard | #endif
|
101 | 67b915a5 | bellard | |
102 | 9892fbfb | blueswir1 | #if defined(__OpenBSD__)
|
103 | 9892fbfb | blueswir1 | #include <util.h> |
104 | 9892fbfb | blueswir1 | #endif
|
105 | 9892fbfb | blueswir1 | |
106 | 8a16d273 | ths | #if defined(CONFIG_VDE)
|
107 | 8a16d273 | ths | #include <libvdeplug.h> |
108 | 8a16d273 | ths | #endif
|
109 | 8a16d273 | ths | |
110 | 67b915a5 | bellard | #ifdef _WIN32
|
111 | 49dc768d | aliguori | #include <windows.h> |
112 | 4fddf62a | ths | #include <mmsystem.h> |
113 | 67b915a5 | bellard | #endif
|
114 | 67b915a5 | bellard | |
115 | 73332e5c | bellard | #ifdef CONFIG_SDL
|
116 | 59a36a2f | Stefan Weil | #if defined(__APPLE__) || defined(main)
|
117 | 6693665a | Stefan Weil | #include <SDL.h> |
118 | 880fec5d | malc | int qemu_main(int argc, char **argv, char **envp); |
119 | 880fec5d | malc | int main(int argc, char **argv) |
120 | 880fec5d | malc | { |
121 | 59a36a2f | Stefan Weil | return qemu_main(argc, argv, NULL); |
122 | 880fec5d | malc | } |
123 | 880fec5d | malc | #undef main
|
124 | 880fec5d | malc | #define main qemu_main
|
125 | 96bcd4f8 | bellard | #endif
|
126 | 73332e5c | bellard | #endif /* CONFIG_SDL */ |
127 | 0824d6fc | bellard | |
128 | 5b0753e0 | bellard | #ifdef CONFIG_COCOA
|
129 | 5b0753e0 | bellard | #undef main
|
130 | 5b0753e0 | bellard | #define main qemu_main
|
131 | 5b0753e0 | bellard | #endif /* CONFIG_COCOA */ |
132 | 5b0753e0 | bellard | |
133 | 511d2b14 | blueswir1 | #include "hw/hw.h" |
134 | 511d2b14 | blueswir1 | #include "hw/boards.h" |
135 | 511d2b14 | blueswir1 | #include "hw/usb.h" |
136 | 511d2b14 | blueswir1 | #include "hw/pcmcia.h" |
137 | 511d2b14 | blueswir1 | #include "hw/pc.h" |
138 | 511d2b14 | blueswir1 | #include "hw/audiodev.h" |
139 | 511d2b14 | blueswir1 | #include "hw/isa.h" |
140 | 511d2b14 | blueswir1 | #include "hw/baum.h" |
141 | 511d2b14 | blueswir1 | #include "hw/bt.h" |
142 | 9dd986cc | Richard W.M. Jones | #include "hw/watchdog.h" |
143 | b6f6e3d3 | aliguori | #include "hw/smbios.h" |
144 | e37630ca | aliguori | #include "hw/xen.h" |
145 | bd3c948d | Gerd Hoffmann | #include "hw/qdev.h" |
146 | 5ef4efa4 | aurel32 | #include "bt-host.h" |
147 | 511d2b14 | blueswir1 | #include "net.h" |
148 | 511d2b14 | blueswir1 | #include "monitor.h" |
149 | 511d2b14 | blueswir1 | #include "console.h" |
150 | 511d2b14 | blueswir1 | #include "sysemu.h" |
151 | 511d2b14 | blueswir1 | #include "gdbstub.h" |
152 | 511d2b14 | blueswir1 | #include "qemu-timer.h" |
153 | 511d2b14 | blueswir1 | #include "qemu-char.h" |
154 | 511d2b14 | blueswir1 | #include "cache-utils.h" |
155 | 511d2b14 | blueswir1 | #include "block.h" |
156 | a718acec | blueswir1 | #include "dma.h" |
157 | 511d2b14 | blueswir1 | #include "audio/audio.h" |
158 | 511d2b14 | blueswir1 | #include "migration.h" |
159 | 511d2b14 | blueswir1 | #include "kvm.h" |
160 | 511d2b14 | blueswir1 | #include "balloon.h" |
161 | d3f24367 | Kevin Wolf | #include "qemu-option.h" |
162 | 7282a033 | Gerd Hoffmann | #include "qemu-config.h" |
163 | 511d2b14 | blueswir1 | |
164 | 0824d6fc | bellard | #include "disas.h" |
165 | fc01f7e7 | bellard | |
166 | 8a7ddc38 | bellard | #include "exec-all.h" |
167 | 0824d6fc | bellard | |
168 | 511d2b14 | blueswir1 | #include "qemu_socket.h" |
169 | 511d2b14 | blueswir1 | |
170 | d918f23e | Jan Kiszka | #include "slirp/libslirp.h" |
171 | 511d2b14 | blueswir1 | |
172 | 72cf2d4f | Blue Swirl | #include "qemu-queue.h" |
173 | 72cf2d4f | Blue Swirl | |
174 | 9dc63a1e | blueswir1 | //#define DEBUG_NET
|
175 | 9dc63a1e | blueswir1 | //#define DEBUG_SLIRP
|
176 | 330d0414 | bellard | |
177 | 1bfe856e | bellard | #define DEFAULT_RAM_SIZE 128 |
178 | 313aa567 | bellard | |
179 | ddd9bbd9 | Jan Kiszka | /* Maximum number of monitor devices */
|
180 | ddd9bbd9 | Jan Kiszka | #define MAX_MONITOR_DEVICES 10 |
181 | ddd9bbd9 | Jan Kiszka | |
182 | 5cea8590 | Paul Brook | static const char *data_dir; |
183 | 1192dad8 | j_mayer | const char *bios_name = NULL; |
184 | e4bcb14c | ths | /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
|
185 | faea38e7 | bellard | to store the VM snapshots */
|
186 | 72cf2d4f | Blue Swirl | struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
|
187 | 72cf2d4f | Blue Swirl | struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
|
188 | cb5a7aa8 | malc | enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
|
189 | 3023f332 | aliguori | static DisplayState *display_state;
|
190 | 993fbfdb | Anthony Liguori | DisplayType display_type = DT_DEFAULT; |
191 | 3d11d0eb | bellard | const char* keyboard_layout = NULL; |
192 | c227f099 | Anthony Liguori | ram_addr_t ram_size; |
193 | c4b1fcc0 | bellard | int nb_nics;
|
194 | 7c9d8e07 | bellard | NICInfo nd_table[MAX_NICS]; |
195 | 8a7ddc38 | bellard | int vm_running;
|
196 | d399f677 | Paolo Bonzini | int autostart;
|
197 | f6503059 | balrog | static int rtc_utc = 1; |
198 | f6503059 | balrog | static int rtc_date_offset = -1; /* -1 means no change */ |
199 | 86176759 | Zachary Amsden | int vga_interface_type = VGA_CIRRUS;
|
200 | d827220b | bellard | #ifdef TARGET_SPARC
|
201 | d827220b | bellard | int graphic_width = 1024; |
202 | d827220b | bellard | int graphic_height = 768; |
203 | eee0b836 | blueswir1 | int graphic_depth = 8; |
204 | d827220b | bellard | #else
|
205 | 1bfe856e | bellard | int graphic_width = 800; |
206 | 1bfe856e | bellard | int graphic_height = 600; |
207 | e9b137c2 | bellard | int graphic_depth = 15; |
208 | eee0b836 | blueswir1 | #endif
|
209 | dbed7e40 | blueswir1 | static int full_screen = 0; |
210 | 634a21f6 | blueswir1 | #ifdef CONFIG_SDL
|
211 | dbed7e40 | blueswir1 | static int no_frame = 0; |
212 | 634a21f6 | blueswir1 | #endif
|
213 | 667accab | ths | int no_quit = 0; |
214 | 8d11df9e | bellard | CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
215 | 6508fe59 | bellard | CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
216 | 9ede2fde | aliguori | CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES]; |
217 | a09db21f | bellard | #ifdef TARGET_I386
|
218 | a09db21f | bellard | int win2k_install_hack = 0; |
219 | 73822ec8 | aliguori | int rtc_td_hack = 0; |
220 | a09db21f | bellard | #endif
|
221 | bb36d470 | bellard | int usb_enabled = 0; |
222 | 1b530a6d | aurel32 | int singlestep = 0; |
223 | 6a00d601 | bellard | int smp_cpus = 1; |
224 | 6be68d7e | Jes Sorensen | int max_cpus = 0; |
225 | dc6b1c09 | Andre Przywara | int smp_cores = 1; |
226 | dc6b1c09 | Andre Przywara | int smp_threads = 1; |
227 | 73fc9742 | ths | const char *vnc_display; |
228 | 6515b203 | bellard | int acpi_enabled = 1; |
229 | 16b29ae1 | aliguori | int no_hpet = 0; |
230 | 52ca8d6a | bellard | int fd_bootchk = 1; |
231 | d1beab82 | bellard | int no_reboot = 0; |
232 | b2f76161 | aurel32 | int no_shutdown = 0; |
233 | 9467cd46 | balrog | int cursor_hide = 1; |
234 | a171fe39 | balrog | int graphic_rotate = 0; |
235 | 6b35e7bf | Jes Sorensen | uint8_t irq0override = 1;
|
236 | b9e82a59 | blueswir1 | #ifndef _WIN32
|
237 | 71e3ceb8 | ths | int daemonize = 0; |
238 | b9e82a59 | blueswir1 | #endif
|
239 | 09aaa160 | Markus Armbruster | const char *watchdog; |
240 | 9ae02555 | ths | const char *option_rom[MAX_OPTION_ROMS]; |
241 | 9ae02555 | ths | int nb_option_roms;
|
242 | 8e71621f | pbrook | int semihosting_enabled = 0; |
243 | 2b8f2d41 | balrog | #ifdef TARGET_ARM
|
244 | 2b8f2d41 | balrog | int old_param = 0; |
245 | 2b8f2d41 | balrog | #endif
|
246 | c35734b2 | ths | const char *qemu_name; |
247 | 3780e197 | ths | int alt_grab = 0; |
248 | 95efd11c | blueswir1 | #if defined(TARGET_SPARC) || defined(TARGET_PPC)
|
249 | 66508601 | blueswir1 | unsigned int nb_prom_envs = 0; |
250 | 66508601 | blueswir1 | const char *prom_envs[MAX_PROM_ENVS]; |
251 | 66508601 | blueswir1 | #endif
|
252 | 95387491 | Jan Kiszka | int boot_menu;
|
253 | 0824d6fc | bellard | |
254 | 268a362c | aliguori | int nb_numa_nodes;
|
255 | 268a362c | aliguori | uint64_t node_mem[MAX_NODES]; |
256 | 268a362c | aliguori | uint64_t node_cpumask[MAX_NODES]; |
257 | 268a362c | aliguori | |
258 | ee5605e5 | balrog | static CPUState *cur_cpu;
|
259 | ee5605e5 | balrog | static CPUState *next_cpu;
|
260 | 43b96858 | aliguori | static int timer_alarm_pending = 1; |
261 | bf20dc07 | ths | /* Conversion factor from emulated instructions to virtual clock ticks. */
|
262 | 2e70f6ef | pbrook | static int icount_time_shift; |
263 | bf20dc07 | ths | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
|
264 | 2e70f6ef | pbrook | #define MAX_ICOUNT_SHIFT 10 |
265 | 2e70f6ef | pbrook | /* Compensate for varying guest execution speed. */
|
266 | 2e70f6ef | pbrook | static int64_t qemu_icount_bias;
|
267 | dbed7e40 | blueswir1 | static QEMUTimer *icount_rt_timer;
|
268 | dbed7e40 | blueswir1 | static QEMUTimer *icount_vm_timer;
|
269 | 9043b62d | blueswir1 | static QEMUTimer *nographic_timer;
|
270 | ee5605e5 | balrog | |
271 | 8fcb1b90 | blueswir1 | uint8_t qemu_uuid[16];
|
272 | 8fcb1b90 | blueswir1 | |
273 | 76e30d0f | Jan Kiszka | static QEMUBootSetHandler *boot_set_handler;
|
274 | 76e30d0f | Jan Kiszka | static void *boot_set_opaque; |
275 | 76e30d0f | Jan Kiszka | |
276 | 0824d6fc | bellard | /***********************************************************/
|
277 | 26aa7d72 | bellard | /* x86 ISA bus support */
|
278 | 26aa7d72 | bellard | |
279 | c227f099 | Anthony Liguori | target_phys_addr_t isa_mem_base = 0;
|
280 | 3de388f6 | bellard | PicState2 *isa_pic; |
281 | 0824d6fc | bellard | |
282 | 0824d6fc | bellard | /***********************************************************/
|
283 | 0824d6fc | bellard | void hw_error(const char *fmt, ...) |
284 | 0824d6fc | bellard | { |
285 | 0824d6fc | bellard | va_list ap; |
286 | 6a00d601 | bellard | CPUState *env; |
287 | 0824d6fc | bellard | |
288 | 0824d6fc | bellard | va_start(ap, fmt); |
289 | 0824d6fc | bellard | fprintf(stderr, "qemu: hardware error: ");
|
290 | 0824d6fc | bellard | vfprintf(stderr, fmt, ap); |
291 | 0824d6fc | bellard | fprintf(stderr, "\n");
|
292 | 6a00d601 | bellard | for(env = first_cpu; env != NULL; env = env->next_cpu) { |
293 | 6a00d601 | bellard | fprintf(stderr, "CPU #%d:\n", env->cpu_index);
|
294 | 0824d6fc | bellard | #ifdef TARGET_I386
|
295 | 6a00d601 | bellard | cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
296 | c45886db | bellard | #else
|
297 | 6a00d601 | bellard | cpu_dump_state(env, stderr, fprintf, 0);
|
298 | 0824d6fc | bellard | #endif
|
299 | 6a00d601 | bellard | } |
300 | 0824d6fc | bellard | va_end(ap); |
301 | 0824d6fc | bellard | abort(); |
302 | 0824d6fc | bellard | } |
303 | 1889465a | Andi Kleen | |
304 | 1889465a | Andi Kleen | static void set_proc_name(const char *s) |
305 | 1889465a | Andi Kleen | { |
306 | 6ca8d0fd | Nathan Froyd | #if defined(__linux__) && defined(PR_SET_NAME)
|
307 | 1889465a | Andi Kleen | char name[16]; |
308 | 1889465a | Andi Kleen | if (!s)
|
309 | 1889465a | Andi Kleen | return;
|
310 | 1889465a | Andi Kleen | name[sizeof(name) - 1] = 0; |
311 | 1889465a | Andi Kleen | strncpy(name, s, sizeof(name));
|
312 | 1889465a | Andi Kleen | /* Could rewrite argv[0] too, but that's a bit more complicated.
|
313 | 1889465a | Andi Kleen | This simple way is enough for `top'. */
|
314 | 1889465a | Andi Kleen | prctl(PR_SET_NAME, name); |
315 | 1889465a | Andi Kleen | #endif
|
316 | 1889465a | Andi Kleen | } |
317 | df751fa8 | aliguori | |
318 | df751fa8 | aliguori | /***************/
|
319 | df751fa8 | aliguori | /* ballooning */
|
320 | df751fa8 | aliguori | |
321 | df751fa8 | aliguori | static QEMUBalloonEvent *qemu_balloon_event;
|
322 | df751fa8 | aliguori | void *qemu_balloon_event_opaque;
|
323 | df751fa8 | aliguori | |
324 | df751fa8 | aliguori | void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque) |
325 | df751fa8 | aliguori | { |
326 | df751fa8 | aliguori | qemu_balloon_event = func; |
327 | df751fa8 | aliguori | qemu_balloon_event_opaque = opaque; |
328 | df751fa8 | aliguori | } |
329 | df751fa8 | aliguori | |
330 | c227f099 | Anthony Liguori | void qemu_balloon(ram_addr_t target)
|
331 | df751fa8 | aliguori | { |
332 | df751fa8 | aliguori | if (qemu_balloon_event)
|
333 | df751fa8 | aliguori | qemu_balloon_event(qemu_balloon_event_opaque, target); |
334 | df751fa8 | aliguori | } |
335 | df751fa8 | aliguori | |
336 | c227f099 | Anthony Liguori | ram_addr_t qemu_balloon_status(void)
|
337 | df751fa8 | aliguori | { |
338 | df751fa8 | aliguori | if (qemu_balloon_event)
|
339 | df751fa8 | aliguori | return qemu_balloon_event(qemu_balloon_event_opaque, 0); |
340 | df751fa8 | aliguori | return 0; |
341 | df751fa8 | aliguori | } |
342 | 0824d6fc | bellard | |
343 | 8a7ddc38 | bellard | /***********************************************************/
|
344 | 63066f4f | bellard | /* keyboard/mouse */
|
345 | 63066f4f | bellard | |
346 | 63066f4f | bellard | static QEMUPutKBDEvent *qemu_put_kbd_event;
|
347 | 63066f4f | bellard | static void *qemu_put_kbd_event_opaque; |
348 | 455204eb | ths | static QEMUPutMouseEntry *qemu_put_mouse_event_head;
|
349 | 455204eb | ths | static QEMUPutMouseEntry *qemu_put_mouse_event_current;
|
350 | 63066f4f | bellard | |
351 | 63066f4f | bellard | void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
352 | 63066f4f | bellard | { |
353 | 63066f4f | bellard | qemu_put_kbd_event_opaque = opaque; |
354 | 63066f4f | bellard | qemu_put_kbd_event = func; |
355 | 63066f4f | bellard | } |
356 | 63066f4f | bellard | |
357 | 455204eb | ths | QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
358 | 455204eb | ths | void *opaque, int absolute, |
359 | 455204eb | ths | const char *name) |
360 | 63066f4f | bellard | { |
361 | 455204eb | ths | QEMUPutMouseEntry *s, *cursor; |
362 | 455204eb | ths | |
363 | 455204eb | ths | s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
|
364 | 455204eb | ths | |
365 | 455204eb | ths | s->qemu_put_mouse_event = func; |
366 | 455204eb | ths | s->qemu_put_mouse_event_opaque = opaque; |
367 | 455204eb | ths | s->qemu_put_mouse_event_absolute = absolute; |
368 | 455204eb | ths | s->qemu_put_mouse_event_name = qemu_strdup(name); |
369 | 455204eb | ths | s->next = NULL;
|
370 | 455204eb | ths | |
371 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
372 | 455204eb | ths | qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
373 | 455204eb | ths | return s;
|
374 | 455204eb | ths | } |
375 | 455204eb | ths | |
376 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
377 | 455204eb | ths | while (cursor->next != NULL) |
378 | 455204eb | ths | cursor = cursor->next; |
379 | 455204eb | ths | |
380 | 455204eb | ths | cursor->next = s; |
381 | 455204eb | ths | qemu_put_mouse_event_current = s; |
382 | 455204eb | ths | |
383 | 455204eb | ths | return s;
|
384 | 455204eb | ths | } |
385 | 455204eb | ths | |
386 | 455204eb | ths | void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
|
387 | 455204eb | ths | { |
388 | 455204eb | ths | QEMUPutMouseEntry *prev = NULL, *cursor;
|
389 | 455204eb | ths | |
390 | 455204eb | ths | if (!qemu_put_mouse_event_head || entry == NULL) |
391 | 455204eb | ths | return;
|
392 | 455204eb | ths | |
393 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
394 | 455204eb | ths | while (cursor != NULL && cursor != entry) { |
395 | 455204eb | ths | prev = cursor; |
396 | 455204eb | ths | cursor = cursor->next; |
397 | 455204eb | ths | } |
398 | 455204eb | ths | |
399 | 455204eb | ths | if (cursor == NULL) // does not exist or list empty |
400 | 455204eb | ths | return;
|
401 | 455204eb | ths | else if (prev == NULL) { // entry is head |
402 | 455204eb | ths | qemu_put_mouse_event_head = cursor->next; |
403 | 455204eb | ths | if (qemu_put_mouse_event_current == entry)
|
404 | 455204eb | ths | qemu_put_mouse_event_current = cursor->next; |
405 | 455204eb | ths | qemu_free(entry->qemu_put_mouse_event_name); |
406 | 455204eb | ths | qemu_free(entry); |
407 | 455204eb | ths | return;
|
408 | 455204eb | ths | } |
409 | 455204eb | ths | |
410 | 455204eb | ths | prev->next = entry->next; |
411 | 455204eb | ths | |
412 | 455204eb | ths | if (qemu_put_mouse_event_current == entry)
|
413 | 455204eb | ths | qemu_put_mouse_event_current = prev; |
414 | 455204eb | ths | |
415 | 455204eb | ths | qemu_free(entry->qemu_put_mouse_event_name); |
416 | 455204eb | ths | qemu_free(entry); |
417 | 63066f4f | bellard | } |
418 | 63066f4f | bellard | |
419 | 63066f4f | bellard | void kbd_put_keycode(int keycode) |
420 | 63066f4f | bellard | { |
421 | 63066f4f | bellard | if (qemu_put_kbd_event) {
|
422 | 63066f4f | bellard | qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
423 | 63066f4f | bellard | } |
424 | 63066f4f | bellard | } |
425 | 63066f4f | bellard | |
426 | 63066f4f | bellard | void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
427 | 63066f4f | bellard | { |
428 | 455204eb | ths | QEMUPutMouseEvent *mouse_event; |
429 | 455204eb | ths | void *mouse_event_opaque;
|
430 | a171fe39 | balrog | int width;
|
431 | 455204eb | ths | |
432 | 455204eb | ths | if (!qemu_put_mouse_event_current) {
|
433 | 455204eb | ths | return;
|
434 | 455204eb | ths | } |
435 | 455204eb | ths | |
436 | 455204eb | ths | mouse_event = |
437 | 455204eb | ths | qemu_put_mouse_event_current->qemu_put_mouse_event; |
438 | 455204eb | ths | mouse_event_opaque = |
439 | 455204eb | ths | qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
440 | 455204eb | ths | |
441 | 455204eb | ths | if (mouse_event) {
|
442 | a171fe39 | balrog | if (graphic_rotate) {
|
443 | a171fe39 | balrog | if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
|
444 | a171fe39 | balrog | width = 0x7fff;
|
445 | a171fe39 | balrog | else
|
446 | b94ed577 | aurel32 | width = graphic_width - 1;
|
447 | a171fe39 | balrog | mouse_event(mouse_event_opaque, |
448 | a171fe39 | balrog | width - dy, dx, dz, buttons_state); |
449 | a171fe39 | balrog | } else
|
450 | a171fe39 | balrog | mouse_event(mouse_event_opaque, |
451 | a171fe39 | balrog | dx, dy, dz, buttons_state); |
452 | 63066f4f | bellard | } |
453 | 63066f4f | bellard | } |
454 | 63066f4f | bellard | |
455 | 09b26c5e | bellard | int kbd_mouse_is_absolute(void) |
456 | 09b26c5e | bellard | { |
457 | 455204eb | ths | if (!qemu_put_mouse_event_current)
|
458 | 455204eb | ths | return 0; |
459 | 455204eb | ths | |
460 | 455204eb | ths | return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
|
461 | 455204eb | ths | } |
462 | 455204eb | ths | |
463 | 376253ec | aliguori | void do_info_mice(Monitor *mon)
|
464 | 455204eb | ths | { |
465 | 455204eb | ths | QEMUPutMouseEntry *cursor; |
466 | 455204eb | ths | int index = 0; |
467 | 455204eb | ths | |
468 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
469 | 376253ec | aliguori | monitor_printf(mon, "No mouse devices connected\n");
|
470 | 455204eb | ths | return;
|
471 | 455204eb | ths | } |
472 | 455204eb | ths | |
473 | 376253ec | aliguori | monitor_printf(mon, "Mouse devices available:\n");
|
474 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
475 | 455204eb | ths | while (cursor != NULL) { |
476 | 376253ec | aliguori | monitor_printf(mon, "%c Mouse #%d: %s\n",
|
477 | 376253ec | aliguori | (cursor == qemu_put_mouse_event_current ? '*' : ' '), |
478 | 376253ec | aliguori | index, cursor->qemu_put_mouse_event_name); |
479 | 455204eb | ths | index++; |
480 | 455204eb | ths | cursor = cursor->next; |
481 | 455204eb | ths | } |
482 | 455204eb | ths | } |
483 | 455204eb | ths | |
484 | d54908a5 | Luiz Capitulino | void do_mouse_set(Monitor *mon, const QDict *qdict) |
485 | 455204eb | ths | { |
486 | 455204eb | ths | QEMUPutMouseEntry *cursor; |
487 | 455204eb | ths | int i = 0; |
488 | d54908a5 | Luiz Capitulino | int index = qdict_get_int(qdict, "index"); |
489 | 455204eb | ths | |
490 | 455204eb | ths | if (!qemu_put_mouse_event_head) {
|
491 | 376253ec | aliguori | monitor_printf(mon, "No mouse devices connected\n");
|
492 | 455204eb | ths | return;
|
493 | 455204eb | ths | } |
494 | 455204eb | ths | |
495 | 455204eb | ths | cursor = qemu_put_mouse_event_head; |
496 | 455204eb | ths | while (cursor != NULL && index != i) { |
497 | 455204eb | ths | i++; |
498 | 455204eb | ths | cursor = cursor->next; |
499 | 455204eb | ths | } |
500 | 455204eb | ths | |
501 | 455204eb | ths | if (cursor != NULL) |
502 | 455204eb | ths | qemu_put_mouse_event_current = cursor; |
503 | 455204eb | ths | else
|
504 | 376253ec | aliguori | monitor_printf(mon, "Mouse at given index not found\n");
|
505 | 09b26c5e | bellard | } |
506 | 09b26c5e | bellard | |
507 | 1dce7c3c | bellard | /* compute with 96 bit intermediate result: (a*b)/c */
|
508 | 1dce7c3c | bellard | uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
509 | 0824d6fc | bellard | { |
510 | 1dce7c3c | bellard | union {
|
511 | 1dce7c3c | bellard | uint64_t ll; |
512 | 1dce7c3c | bellard | struct {
|
513 | e2542fe2 | Juan Quintela | #ifdef HOST_WORDS_BIGENDIAN
|
514 | 1dce7c3c | bellard | uint32_t high, low; |
515 | 1dce7c3c | bellard | #else
|
516 | 1dce7c3c | bellard | uint32_t low, high; |
517 | 3b46e624 | ths | #endif
|
518 | 1dce7c3c | bellard | } l; |
519 | 1dce7c3c | bellard | } u, res; |
520 | 1dce7c3c | bellard | uint64_t rl, rh; |
521 | 0824d6fc | bellard | |
522 | 1dce7c3c | bellard | u.ll = a; |
523 | 1dce7c3c | bellard | rl = (uint64_t)u.l.low * (uint64_t)b; |
524 | 1dce7c3c | bellard | rh = (uint64_t)u.l.high * (uint64_t)b; |
525 | 1dce7c3c | bellard | rh += (rl >> 32);
|
526 | 1dce7c3c | bellard | res.l.high = rh / c; |
527 | 1dce7c3c | bellard | res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
528 | 1dce7c3c | bellard | return res.ll;
|
529 | 34865134 | bellard | } |
530 | 34865134 | bellard | |
531 | 1dce7c3c | bellard | /***********************************************************/
|
532 | 1dce7c3c | bellard | /* real time host monotonic timer */
|
533 | 34865134 | bellard | |
534 | 1dce7c3c | bellard | #ifdef WIN32
|
535 | 0824d6fc | bellard | |
536 | 1dce7c3c | bellard | static int64_t clock_freq;
|
537 | 1115dde7 | bellard | |
538 | 1dce7c3c | bellard | static void init_get_clock(void) |
539 | 1115dde7 | bellard | { |
540 | a8e5ac33 | bellard | LARGE_INTEGER freq; |
541 | a8e5ac33 | bellard | int ret;
|
542 | 1dce7c3c | bellard | ret = QueryPerformanceFrequency(&freq); |
543 | 1dce7c3c | bellard | if (ret == 0) { |
544 | 1dce7c3c | bellard | fprintf(stderr, "Could not calibrate ticks\n");
|
545 | 1dce7c3c | bellard | exit(1);
|
546 | 1dce7c3c | bellard | } |
547 | 1dce7c3c | bellard | clock_freq = freq.QuadPart; |
548 | 1115dde7 | bellard | } |
549 | 1115dde7 | bellard | |
550 | 1dce7c3c | bellard | static int64_t get_clock(void) |
551 | b8076a74 | bellard | { |
552 | 1dce7c3c | bellard | LARGE_INTEGER ti; |
553 | 1dce7c3c | bellard | QueryPerformanceCounter(&ti); |
554 | 274dfed8 | Anthony Liguori | return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
|
555 | b8076a74 | bellard | } |
556 | b8076a74 | bellard | |
557 | 1dce7c3c | bellard | #else
|
558 | 90cb9493 | bellard | |
559 | 1dce7c3c | bellard | static int use_rt_clock; |
560 | 1dce7c3c | bellard | |
561 | 1dce7c3c | bellard | static void init_get_clock(void) |
562 | 90cb9493 | bellard | { |
563 | 1dce7c3c | bellard | use_rt_clock = 0;
|
564 | c5e97233 | blueswir1 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
565 | c5e97233 | blueswir1 | || defined(__DragonFly__) |
566 | 1dce7c3c | bellard | { |
567 | 1dce7c3c | bellard | struct timespec ts;
|
568 | 1dce7c3c | bellard | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
569 | 1dce7c3c | bellard | use_rt_clock = 1;
|
570 | 1dce7c3c | bellard | } |
571 | 1dce7c3c | bellard | } |
572 | 1dce7c3c | bellard | #endif
|
573 | 90cb9493 | bellard | } |
574 | 90cb9493 | bellard | |
575 | 1dce7c3c | bellard | static int64_t get_clock(void) |
576 | fdbb4691 | bellard | { |
577 | c5e97233 | blueswir1 | #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
578 | c5e97233 | blueswir1 | || defined(__DragonFly__) |
579 | 1dce7c3c | bellard | if (use_rt_clock) {
|
580 | 1dce7c3c | bellard | struct timespec ts;
|
581 | 1dce7c3c | bellard | clock_gettime(CLOCK_MONOTONIC, &ts); |
582 | 1dce7c3c | bellard | return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
583 | 5fafdf24 | ths | } else
|
584 | fdbb4691 | bellard | #endif
|
585 | 1dce7c3c | bellard | { |
586 | 1dce7c3c | bellard | /* XXX: using gettimeofday leads to problems if the date
|
587 | 1dce7c3c | bellard | changes, so it should be avoided. */
|
588 | 1dce7c3c | bellard | struct timeval tv;
|
589 | 1dce7c3c | bellard | gettimeofday(&tv, NULL);
|
590 | 1dce7c3c | bellard | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
591 | 1dce7c3c | bellard | } |
592 | fdbb4691 | bellard | } |
593 | 34865134 | bellard | #endif
|
594 | 34865134 | bellard | |
595 | 2e70f6ef | pbrook | /* Return the virtual CPU time, based on the instruction counter. */
|
596 | 2e70f6ef | pbrook | static int64_t cpu_get_icount(void) |
597 | 2e70f6ef | pbrook | { |
598 | 2e70f6ef | pbrook | int64_t icount; |
599 | 2e70f6ef | pbrook | CPUState *env = cpu_single_env;; |
600 | 2e70f6ef | pbrook | icount = qemu_icount; |
601 | 2e70f6ef | pbrook | if (env) {
|
602 | 2e70f6ef | pbrook | if (!can_do_io(env))
|
603 | 2e70f6ef | pbrook | fprintf(stderr, "Bad clock read\n");
|
604 | 2e70f6ef | pbrook | icount -= (env->icount_decr.u16.low + env->icount_extra); |
605 | 2e70f6ef | pbrook | } |
606 | 2e70f6ef | pbrook | return qemu_icount_bias + (icount << icount_time_shift);
|
607 | 2e70f6ef | pbrook | } |
608 | 2e70f6ef | pbrook | |
609 | 1dce7c3c | bellard | /***********************************************************/
|
610 | 1dce7c3c | bellard | /* guest cycle counter */
|
611 | 1dce7c3c | bellard | |
612 | 6f68e33e | Juan Quintela | typedef struct TimersState { |
613 | 6f68e33e | Juan Quintela | int64_t cpu_ticks_prev; |
614 | 6f68e33e | Juan Quintela | int64_t cpu_ticks_offset; |
615 | 6f68e33e | Juan Quintela | int64_t cpu_clock_offset; |
616 | 6f68e33e | Juan Quintela | int32_t cpu_ticks_enabled; |
617 | 274dfed8 | Anthony Liguori | int64_t dummy; |
618 | 6f68e33e | Juan Quintela | } TimersState; |
619 | 6f68e33e | Juan Quintela | |
620 | 6f68e33e | Juan Quintela | TimersState timers_state; |
621 | 34865134 | bellard | |
622 | 1dce7c3c | bellard | /* return the host CPU cycle counter and handle stop/restart */
|
623 | 1dce7c3c | bellard | int64_t cpu_get_ticks(void)
|
624 | 34865134 | bellard | { |
625 | 2e70f6ef | pbrook | if (use_icount) {
|
626 | 2e70f6ef | pbrook | return cpu_get_icount();
|
627 | 2e70f6ef | pbrook | } |
628 | 6f68e33e | Juan Quintela | if (!timers_state.cpu_ticks_enabled) {
|
629 | 6f68e33e | Juan Quintela | return timers_state.cpu_ticks_offset;
|
630 | 8a7ddc38 | bellard | } else {
|
631 | eade0f19 | bellard | int64_t ticks; |
632 | eade0f19 | bellard | ticks = cpu_get_real_ticks(); |
633 | 6f68e33e | Juan Quintela | if (timers_state.cpu_ticks_prev > ticks) {
|
634 | eade0f19 | bellard | /* Note: non increasing ticks may happen if the host uses
|
635 | eade0f19 | bellard | software suspend */
|
636 | 6f68e33e | Juan Quintela | timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks; |
637 | eade0f19 | bellard | } |
638 | 6f68e33e | Juan Quintela | timers_state.cpu_ticks_prev = ticks; |
639 | 6f68e33e | Juan Quintela | return ticks + timers_state.cpu_ticks_offset;
|
640 | 8a7ddc38 | bellard | } |
641 | 34865134 | bellard | } |
642 | 34865134 | bellard | |
643 | 1dce7c3c | bellard | /* return the host CPU monotonic timer and handle stop/restart */
|
644 | 1dce7c3c | bellard | static int64_t cpu_get_clock(void) |
645 | 1dce7c3c | bellard | { |
646 | 1dce7c3c | bellard | int64_t ti; |
647 | 6f68e33e | Juan Quintela | if (!timers_state.cpu_ticks_enabled) {
|
648 | 6f68e33e | Juan Quintela | return timers_state.cpu_clock_offset;
|
649 | 1dce7c3c | bellard | } else {
|
650 | 1dce7c3c | bellard | ti = get_clock(); |
651 | 6f68e33e | Juan Quintela | return ti + timers_state.cpu_clock_offset;
|
652 | 1dce7c3c | bellard | } |
653 | 1dce7c3c | bellard | } |
654 | 1dce7c3c | bellard | |
655 | 34865134 | bellard | /* enable cpu_get_ticks() */
|
656 | 34865134 | bellard | void cpu_enable_ticks(void) |
657 | 34865134 | bellard | { |
658 | 6f68e33e | Juan Quintela | if (!timers_state.cpu_ticks_enabled) {
|
659 | 6f68e33e | Juan Quintela | timers_state.cpu_ticks_offset -= cpu_get_real_ticks(); |
660 | 6f68e33e | Juan Quintela | timers_state.cpu_clock_offset -= get_clock(); |
661 | 6f68e33e | Juan Quintela | timers_state.cpu_ticks_enabled = 1;
|
662 | 8a7ddc38 | bellard | } |
663 | 34865134 | bellard | } |
664 | 34865134 | bellard | |
665 | 34865134 | bellard | /* disable cpu_get_ticks() : the clock is stopped. You must not call
|
666 | 34865134 | bellard | cpu_get_ticks() after that. */
|
667 | 34865134 | bellard | void cpu_disable_ticks(void) |
668 | 34865134 | bellard | { |
669 | 6f68e33e | Juan Quintela | if (timers_state.cpu_ticks_enabled) {
|
670 | 6f68e33e | Juan Quintela | timers_state.cpu_ticks_offset = cpu_get_ticks(); |
671 | 6f68e33e | Juan Quintela | timers_state.cpu_clock_offset = cpu_get_clock(); |
672 | 6f68e33e | Juan Quintela | timers_state.cpu_ticks_enabled = 0;
|
673 | 8a7ddc38 | bellard | } |
674 | 34865134 | bellard | } |
675 | 34865134 | bellard | |
676 | 1dce7c3c | bellard | /***********************************************************/
|
677 | 1dce7c3c | bellard | /* timers */
|
678 | 5fafdf24 | ths | |
679 | 8a7ddc38 | bellard | #define QEMU_TIMER_REALTIME 0 |
680 | 8a7ddc38 | bellard | #define QEMU_TIMER_VIRTUAL 1 |
681 | 8a7ddc38 | bellard | |
682 | 8a7ddc38 | bellard | struct QEMUClock {
|
683 | 8a7ddc38 | bellard | int type;
|
684 | 8a7ddc38 | bellard | /* XXX: add frequency */
|
685 | 8a7ddc38 | bellard | }; |
686 | 8a7ddc38 | bellard | |
687 | 8a7ddc38 | bellard | struct QEMUTimer {
|
688 | 8a7ddc38 | bellard | QEMUClock *clock; |
689 | 8a7ddc38 | bellard | int64_t expire_time; |
690 | 8a7ddc38 | bellard | QEMUTimerCB *cb; |
691 | 8a7ddc38 | bellard | void *opaque;
|
692 | 8a7ddc38 | bellard | struct QEMUTimer *next;
|
693 | 8a7ddc38 | bellard | }; |
694 | 8a7ddc38 | bellard | |
695 | c8994013 | ths | struct qemu_alarm_timer {
|
696 | c8994013 | ths | char const *name; |
697 | efe75411 | ths | unsigned int flags; |
698 | c8994013 | ths | |
699 | c8994013 | ths | int (*start)(struct qemu_alarm_timer *t); |
700 | c8994013 | ths | void (*stop)(struct qemu_alarm_timer *t); |
701 | efe75411 | ths | void (*rearm)(struct qemu_alarm_timer *t); |
702 | c8994013 | ths | void *priv;
|
703 | c8994013 | ths | }; |
704 | c8994013 | ths | |
705 | efe75411 | ths | #define ALARM_FLAG_DYNTICKS 0x1 |
706 | d5d08334 | balrog | #define ALARM_FLAG_EXPIRED 0x2 |
707 | efe75411 | ths | |
708 | efe75411 | ths | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
709 | efe75411 | ths | { |
710 | e332340a | Jean-Christophe Dubois | return t && (t->flags & ALARM_FLAG_DYNTICKS);
|
711 | efe75411 | ths | } |
712 | efe75411 | ths | |
713 | efe75411 | ths | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
714 | efe75411 | ths | { |
715 | efe75411 | ths | if (!alarm_has_dynticks(t))
|
716 | efe75411 | ths | return;
|
717 | efe75411 | ths | |
718 | efe75411 | ths | t->rearm(t); |
719 | efe75411 | ths | } |
720 | efe75411 | ths | |
721 | efe75411 | ths | /* TODO: MIN_TIMER_REARM_US should be optimized */
|
722 | efe75411 | ths | #define MIN_TIMER_REARM_US 250 |
723 | efe75411 | ths | |
724 | c8994013 | ths | static struct qemu_alarm_timer *alarm_timer; |
725 | 8a7ddc38 | bellard | |
726 | 40c3bac3 | bellard | #ifdef _WIN32
|
727 | c8994013 | ths | |
728 | c8994013 | ths | struct qemu_alarm_win32 {
|
729 | c8994013 | ths | MMRESULT timerId; |
730 | c8994013 | ths | unsigned int period; |
731 | ef28c4b0 | Blue Swirl | } alarm_win32_data = {0, -1}; |
732 | c8994013 | ths | |
733 | c8994013 | ths | static int win32_start_timer(struct qemu_alarm_timer *t); |
734 | c8994013 | ths | static void win32_stop_timer(struct qemu_alarm_timer *t); |
735 | efe75411 | ths | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
736 | c8994013 | ths | |
737 | 40c3bac3 | bellard | #else
|
738 | c8994013 | ths | |
739 | c8994013 | ths | static int unix_start_timer(struct qemu_alarm_timer *t); |
740 | c8994013 | ths | static void unix_stop_timer(struct qemu_alarm_timer *t); |
741 | c8994013 | ths | |
742 | 231c6586 | ths | #ifdef __linux__
|
743 | 231c6586 | ths | |
744 | efe75411 | ths | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
745 | efe75411 | ths | static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
746 | efe75411 | ths | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
747 | efe75411 | ths | |
748 | c40ec5a9 | ths | static int hpet_start_timer(struct qemu_alarm_timer *t); |
749 | c40ec5a9 | ths | static void hpet_stop_timer(struct qemu_alarm_timer *t); |
750 | c40ec5a9 | ths | |
751 | c8994013 | ths | static int rtc_start_timer(struct qemu_alarm_timer *t); |
752 | c8994013 | ths | static void rtc_stop_timer(struct qemu_alarm_timer *t); |
753 | c8994013 | ths | |
754 | efe75411 | ths | #endif /* __linux__ */ |
755 | 8a7ddc38 | bellard | |
756 | c8994013 | ths | #endif /* _WIN32 */ |
757 | c8994013 | ths | |
758 | 2e70f6ef | pbrook | /* Correlation between real and virtual time is always going to be
|
759 | bf20dc07 | ths | fairly approximate, so ignore small variation.
|
760 | 2e70f6ef | pbrook | When the guest is idle real and virtual time will be aligned in
|
761 | 2e70f6ef | pbrook | the IO wait loop. */
|
762 | 274dfed8 | Anthony Liguori | #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10) |
763 | 2e70f6ef | pbrook | |
764 | 2e70f6ef | pbrook | static void icount_adjust(void) |
765 | 2e70f6ef | pbrook | { |
766 | 2e70f6ef | pbrook | int64_t cur_time; |
767 | 2e70f6ef | pbrook | int64_t cur_icount; |
768 | 2e70f6ef | pbrook | int64_t delta; |
769 | 2e70f6ef | pbrook | static int64_t last_delta;
|
770 | 2e70f6ef | pbrook | /* If the VM is not running, then do nothing. */
|
771 | 2e70f6ef | pbrook | if (!vm_running)
|
772 | 2e70f6ef | pbrook | return;
|
773 | 2e70f6ef | pbrook | |
774 | 2e70f6ef | pbrook | cur_time = cpu_get_clock(); |
775 | 2e70f6ef | pbrook | cur_icount = qemu_get_clock(vm_clock); |
776 | 2e70f6ef | pbrook | delta = cur_icount - cur_time; |
777 | 2e70f6ef | pbrook | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
|
778 | 2e70f6ef | pbrook | if (delta > 0 |
779 | 2e70f6ef | pbrook | && last_delta + ICOUNT_WOBBLE < delta * 2
|
780 | 2e70f6ef | pbrook | && icount_time_shift > 0) {
|
781 | 2e70f6ef | pbrook | /* The guest is getting too far ahead. Slow time down. */
|
782 | 2e70f6ef | pbrook | icount_time_shift--; |
783 | 2e70f6ef | pbrook | } |
784 | 2e70f6ef | pbrook | if (delta < 0 |
785 | 2e70f6ef | pbrook | && last_delta - ICOUNT_WOBBLE > delta * 2
|
786 | 2e70f6ef | pbrook | && icount_time_shift < MAX_ICOUNT_SHIFT) { |
787 | 2e70f6ef | pbrook | /* The guest is getting too far behind. Speed time up. */
|
788 | 2e70f6ef | pbrook | icount_time_shift++; |
789 | 2e70f6ef | pbrook | } |
790 | 2e70f6ef | pbrook | last_delta = delta; |
791 | 2e70f6ef | pbrook | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
792 | 2e70f6ef | pbrook | } |
793 | 2e70f6ef | pbrook | |
794 | 2e70f6ef | pbrook | static void icount_adjust_rt(void * opaque) |
795 | 2e70f6ef | pbrook | { |
796 | 2e70f6ef | pbrook | qemu_mod_timer(icount_rt_timer, |
797 | 2e70f6ef | pbrook | qemu_get_clock(rt_clock) + 1000);
|
798 | 2e70f6ef | pbrook | icount_adjust(); |
799 | 2e70f6ef | pbrook | } |
800 | 2e70f6ef | pbrook | |
801 | 2e70f6ef | pbrook | static void icount_adjust_vm(void * opaque) |
802 | 2e70f6ef | pbrook | { |
803 | 2e70f6ef | pbrook | qemu_mod_timer(icount_vm_timer, |
804 | 274dfed8 | Anthony Liguori | qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
|
805 | 2e70f6ef | pbrook | icount_adjust(); |
806 | 2e70f6ef | pbrook | } |
807 | 2e70f6ef | pbrook | |
808 | 2e70f6ef | pbrook | static void init_icount_adjust(void) |
809 | 2e70f6ef | pbrook | { |
810 | 2e70f6ef | pbrook | /* Have both realtime and virtual time triggers for speed adjustment.
|
811 | 2e70f6ef | pbrook | The realtime trigger catches emulated time passing too slowly,
|
812 | 2e70f6ef | pbrook | the virtual time trigger catches emulated time passing too fast.
|
813 | 2e70f6ef | pbrook | Realtime triggers occur even when idle, so use them less frequently
|
814 | 2e70f6ef | pbrook | than VM triggers. */
|
815 | 2e70f6ef | pbrook | icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
|
816 | 2e70f6ef | pbrook | qemu_mod_timer(icount_rt_timer, |
817 | 2e70f6ef | pbrook | qemu_get_clock(rt_clock) + 1000);
|
818 | 2e70f6ef | pbrook | icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
|
819 | 2e70f6ef | pbrook | qemu_mod_timer(icount_vm_timer, |
820 | 274dfed8 | Anthony Liguori | qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
|
821 | 2e70f6ef | pbrook | } |
822 | 2e70f6ef | pbrook | |
823 | c8994013 | ths | static struct qemu_alarm_timer alarm_timers[] = { |
824 | efe75411 | ths | #ifndef _WIN32
|
825 | 231c6586 | ths | #ifdef __linux__
|
826 | efe75411 | ths | {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
|
827 | efe75411 | ths | dynticks_stop_timer, dynticks_rearm_timer, NULL},
|
828 | c40ec5a9 | ths | /* HPET - if available - is preferred */
|
829 | efe75411 | ths | {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
830 | c40ec5a9 | ths | /* ...otherwise try RTC */
|
831 | efe75411 | ths | {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
832 | c8994013 | ths | #endif
|
833 | efe75411 | ths | {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
834 | c8994013 | ths | #else
|
835 | efe75411 | ths | {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
|
836 | efe75411 | ths | win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
837 | efe75411 | ths | {"win32", 0, win32_start_timer, |
838 | efe75411 | ths | win32_stop_timer, NULL, &alarm_win32_data},
|
839 | c8994013 | ths | #endif
|
840 | c8994013 | ths | {NULL, }
|
841 | c8994013 | ths | }; |
842 | c8994013 | ths | |
843 | 3f47aa8c | blueswir1 | static void show_available_alarms(void) |
844 | f3dcfada | ths | { |
845 | f3dcfada | ths | int i;
|
846 | f3dcfada | ths | |
847 | f3dcfada | ths | printf("Available alarm timers, in order of precedence:\n");
|
848 | f3dcfada | ths | for (i = 0; alarm_timers[i].name; i++) |
849 | f3dcfada | ths | printf("%s\n", alarm_timers[i].name);
|
850 | f3dcfada | ths | } |
851 | f3dcfada | ths | |
852 | f3dcfada | ths | static void configure_alarms(char const *opt) |
853 | f3dcfada | ths | { |
854 | f3dcfada | ths | int i;
|
855 | f3dcfada | ths | int cur = 0; |
856 | b1503cda | malc | int count = ARRAY_SIZE(alarm_timers) - 1; |
857 | f3dcfada | ths | char *arg;
|
858 | f3dcfada | ths | char *name;
|
859 | 2e70f6ef | pbrook | struct qemu_alarm_timer tmp;
|
860 | f3dcfada | ths | |
861 | 3adda04c | aurel32 | if (!strcmp(opt, "?")) { |
862 | f3dcfada | ths | show_available_alarms(); |
863 | f3dcfada | ths | exit(0);
|
864 | f3dcfada | ths | } |
865 | f3dcfada | ths | |
866 | 73ffc805 | Jean-Christophe DUBOIS | arg = qemu_strdup(opt); |
867 | f3dcfada | ths | |
868 | f3dcfada | ths | /* Reorder the array */
|
869 | f3dcfada | ths | name = strtok(arg, ",");
|
870 | f3dcfada | ths | while (name) {
|
871 | e2b577e5 | balrog | for (i = 0; i < count && alarm_timers[i].name; i++) { |
872 | f3dcfada | ths | if (!strcmp(alarm_timers[i].name, name))
|
873 | f3dcfada | ths | break;
|
874 | f3dcfada | ths | } |
875 | f3dcfada | ths | |
876 | f3dcfada | ths | if (i == count) {
|
877 | f3dcfada | ths | fprintf(stderr, "Unknown clock %s\n", name);
|
878 | f3dcfada | ths | goto next;
|
879 | f3dcfada | ths | } |
880 | f3dcfada | ths | |
881 | f3dcfada | ths | if (i < cur)
|
882 | f3dcfada | ths | /* Ignore */
|
883 | f3dcfada | ths | goto next;
|
884 | f3dcfada | ths | |
885 | f3dcfada | ths | /* Swap */
|
886 | f3dcfada | ths | tmp = alarm_timers[i]; |
887 | f3dcfada | ths | alarm_timers[i] = alarm_timers[cur]; |
888 | f3dcfada | ths | alarm_timers[cur] = tmp; |
889 | f3dcfada | ths | |
890 | f3dcfada | ths | cur++; |
891 | f3dcfada | ths | next:
|
892 | f3dcfada | ths | name = strtok(NULL, ","); |
893 | f3dcfada | ths | } |
894 | f3dcfada | ths | |
895 | 73ffc805 | Jean-Christophe DUBOIS | qemu_free(arg); |
896 | f3dcfada | ths | |
897 | f3dcfada | ths | if (cur) {
|
898 | 2e70f6ef | pbrook | /* Disable remaining timers */
|
899 | f3dcfada | ths | for (i = cur; i < count; i++)
|
900 | f3dcfada | ths | alarm_timers[i].name = NULL;
|
901 | 3adda04c | aurel32 | } else {
|
902 | 3adda04c | aurel32 | show_available_alarms(); |
903 | 3adda04c | aurel32 | exit(1);
|
904 | f3dcfada | ths | } |
905 | f3dcfada | ths | } |
906 | f3dcfada | ths | |
907 | c8994013 | ths | QEMUClock *rt_clock; |
908 | c8994013 | ths | QEMUClock *vm_clock; |
909 | c8994013 | ths | |
910 | c8994013 | ths | static QEMUTimer *active_timers[2]; |
911 | c8994013 | ths | |
912 | 9596ebb7 | pbrook | static QEMUClock *qemu_new_clock(int type) |
913 | 8a7ddc38 | bellard | { |
914 | 8a7ddc38 | bellard | QEMUClock *clock; |
915 | 8a7ddc38 | bellard | clock = qemu_mallocz(sizeof(QEMUClock));
|
916 | 8a7ddc38 | bellard | clock->type = type; |
917 | 8a7ddc38 | bellard | return clock;
|
918 | 8a7ddc38 | bellard | } |
919 | 8a7ddc38 | bellard | |
920 | 8a7ddc38 | bellard | QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
|
921 | 8a7ddc38 | bellard | { |
922 | 8a7ddc38 | bellard | QEMUTimer *ts; |
923 | 8a7ddc38 | bellard | |
924 | 8a7ddc38 | bellard | ts = qemu_mallocz(sizeof(QEMUTimer));
|
925 | 8a7ddc38 | bellard | ts->clock = clock; |
926 | 8a7ddc38 | bellard | ts->cb = cb; |
927 | 8a7ddc38 | bellard | ts->opaque = opaque; |
928 | 8a7ddc38 | bellard | return ts;
|
929 | 8a7ddc38 | bellard | } |
930 | 8a7ddc38 | bellard | |
931 | 8a7ddc38 | bellard | void qemu_free_timer(QEMUTimer *ts)
|
932 | 8a7ddc38 | bellard | { |
933 | 8a7ddc38 | bellard | qemu_free(ts); |
934 | 8a7ddc38 | bellard | } |
935 | 8a7ddc38 | bellard | |
936 | 8a7ddc38 | bellard | /* stop a timer, but do not dealloc it */
|
937 | 8a7ddc38 | bellard | void qemu_del_timer(QEMUTimer *ts)
|
938 | 8a7ddc38 | bellard | { |
939 | 8a7ddc38 | bellard | QEMUTimer **pt, *t; |
940 | 8a7ddc38 | bellard | |
941 | 8a7ddc38 | bellard | /* NOTE: this code must be signal safe because
|
942 | 8a7ddc38 | bellard | qemu_timer_expired() can be called from a signal. */
|
943 | 8a7ddc38 | bellard | pt = &active_timers[ts->clock->type]; |
944 | 8a7ddc38 | bellard | for(;;) {
|
945 | 8a7ddc38 | bellard | t = *pt; |
946 | 8a7ddc38 | bellard | if (!t)
|
947 | 8a7ddc38 | bellard | break;
|
948 | 8a7ddc38 | bellard | if (t == ts) {
|
949 | 8a7ddc38 | bellard | *pt = t->next; |
950 | 8a7ddc38 | bellard | break;
|
951 | 8a7ddc38 | bellard | } |
952 | 8a7ddc38 | bellard | pt = &t->next; |
953 | 8a7ddc38 | bellard | } |
954 | 8a7ddc38 | bellard | } |
955 | 8a7ddc38 | bellard | |
956 | 8a7ddc38 | bellard | /* modify the current timer so that it will be fired when current_time
|
957 | 8a7ddc38 | bellard | >= expire_time. The corresponding callback will be called. */
|
958 | 8a7ddc38 | bellard | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
|
959 | 8a7ddc38 | bellard | { |
960 | 8a7ddc38 | bellard | QEMUTimer **pt, *t; |
961 | 8a7ddc38 | bellard | |
962 | 8a7ddc38 | bellard | qemu_del_timer(ts); |
963 | 8a7ddc38 | bellard | |
964 | 8a7ddc38 | bellard | /* add the timer in the sorted list */
|
965 | 8a7ddc38 | bellard | /* NOTE: this code must be signal safe because
|
966 | 8a7ddc38 | bellard | qemu_timer_expired() can be called from a signal. */
|
967 | 8a7ddc38 | bellard | pt = &active_timers[ts->clock->type]; |
968 | 8a7ddc38 | bellard | for(;;) {
|
969 | 8a7ddc38 | bellard | t = *pt; |
970 | 8a7ddc38 | bellard | if (!t)
|
971 | 8a7ddc38 | bellard | break;
|
972 | 5fafdf24 | ths | if (t->expire_time > expire_time)
|
973 | 8a7ddc38 | bellard | break;
|
974 | 8a7ddc38 | bellard | pt = &t->next; |
975 | 8a7ddc38 | bellard | } |
976 | 8a7ddc38 | bellard | ts->expire_time = expire_time; |
977 | 8a7ddc38 | bellard | ts->next = *pt; |
978 | 8a7ddc38 | bellard | *pt = ts; |
979 | d5d08334 | balrog | |
980 | d5d08334 | balrog | /* Rearm if necessary */
|
981 | 2e70f6ef | pbrook | if (pt == &active_timers[ts->clock->type]) {
|
982 | 2e70f6ef | pbrook | if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { |
983 | 2e70f6ef | pbrook | qemu_rearm_alarm_timer(alarm_timer); |
984 | 2e70f6ef | pbrook | } |
985 | 2e70f6ef | pbrook | /* Interrupt execution to force deadline recalculation. */
|
986 | d9f75a4e | aliguori | if (use_icount)
|
987 | d9f75a4e | aliguori | qemu_notify_event(); |
988 | 2e70f6ef | pbrook | } |
989 | 8a7ddc38 | bellard | } |
990 | 8a7ddc38 | bellard | |
991 | 8a7ddc38 | bellard | int qemu_timer_pending(QEMUTimer *ts)
|
992 | 8a7ddc38 | bellard | { |
993 | 8a7ddc38 | bellard | QEMUTimer *t; |
994 | 8a7ddc38 | bellard | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
995 | 8a7ddc38 | bellard | if (t == ts)
|
996 | 8a7ddc38 | bellard | return 1; |
997 | 8a7ddc38 | bellard | } |
998 | 8a7ddc38 | bellard | return 0; |
999 | 8a7ddc38 | bellard | } |
1000 | 8a7ddc38 | bellard | |
1001 | 2430ffe4 | Stefano Stabellini | int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
|
1002 | 8a7ddc38 | bellard | { |
1003 | 8a7ddc38 | bellard | if (!timer_head)
|
1004 | 8a7ddc38 | bellard | return 0; |
1005 | 8a7ddc38 | bellard | return (timer_head->expire_time <= current_time);
|
1006 | 8a7ddc38 | bellard | } |
1007 | 8a7ddc38 | bellard | |
1008 | 8a7ddc38 | bellard | static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
1009 | 8a7ddc38 | bellard | { |
1010 | 8a7ddc38 | bellard | QEMUTimer *ts; |
1011 | 3b46e624 | ths | |
1012 | 8a7ddc38 | bellard | for(;;) {
|
1013 | 8a7ddc38 | bellard | ts = *ptimer_head; |
1014 | e95c8d51 | bellard | if (!ts || ts->expire_time > current_time)
|
1015 | 8a7ddc38 | bellard | break;
|
1016 | 8a7ddc38 | bellard | /* remove timer from the list before calling the callback */
|
1017 | 8a7ddc38 | bellard | *ptimer_head = ts->next; |
1018 | 8a7ddc38 | bellard | ts->next = NULL;
|
1019 | 3b46e624 | ths | |
1020 | 8a7ddc38 | bellard | /* run the callback (the timer list can be modified) */
|
1021 | 8a7ddc38 | bellard | ts->cb(ts->opaque); |
1022 | 8a7ddc38 | bellard | } |
1023 | 8a7ddc38 | bellard | } |
1024 | 8a7ddc38 | bellard | |
1025 | 8a7ddc38 | bellard | int64_t qemu_get_clock(QEMUClock *clock) |
1026 | 8a7ddc38 | bellard | { |
1027 | 8a7ddc38 | bellard | switch(clock->type) {
|
1028 | 8a7ddc38 | bellard | case QEMU_TIMER_REALTIME:
|
1029 | 1dce7c3c | bellard | return get_clock() / 1000000; |
1030 | 8a7ddc38 | bellard | default:
|
1031 | 8a7ddc38 | bellard | case QEMU_TIMER_VIRTUAL:
|
1032 | 2e70f6ef | pbrook | if (use_icount) {
|
1033 | 2e70f6ef | pbrook | return cpu_get_icount();
|
1034 | 2e70f6ef | pbrook | } else {
|
1035 | 2e70f6ef | pbrook | return cpu_get_clock();
|
1036 | 2e70f6ef | pbrook | } |
1037 | 8a7ddc38 | bellard | } |
1038 | 8a7ddc38 | bellard | } |
1039 | 8a7ddc38 | bellard | |
1040 | 1dce7c3c | bellard | static void init_timers(void) |
1041 | 1dce7c3c | bellard | { |
1042 | 1dce7c3c | bellard | init_get_clock(); |
1043 | 1dce7c3c | bellard | rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
1044 | 1dce7c3c | bellard | vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
1045 | 1dce7c3c | bellard | } |
1046 | 1dce7c3c | bellard | |
1047 | 8a7ddc38 | bellard | /* save a timer */
|
1048 | 8a7ddc38 | bellard | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
|
1049 | 8a7ddc38 | bellard | { |
1050 | 8a7ddc38 | bellard | uint64_t expire_time; |
1051 | 8a7ddc38 | bellard | |
1052 | 8a7ddc38 | bellard | if (qemu_timer_pending(ts)) {
|
1053 | 8a7ddc38 | bellard | expire_time = ts->expire_time; |
1054 | 8a7ddc38 | bellard | } else {
|
1055 | 8a7ddc38 | bellard | expire_time = -1;
|
1056 | 8a7ddc38 | bellard | } |
1057 | 8a7ddc38 | bellard | qemu_put_be64(f, expire_time); |
1058 | 8a7ddc38 | bellard | } |
1059 | 8a7ddc38 | bellard | |
1060 | 8a7ddc38 | bellard | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
|
1061 | 8a7ddc38 | bellard | { |
1062 | 8a7ddc38 | bellard | uint64_t expire_time; |
1063 | 8a7ddc38 | bellard | |
1064 | 8a7ddc38 | bellard | expire_time = qemu_get_be64(f); |
1065 | 8a7ddc38 | bellard | if (expire_time != -1) { |
1066 | 8a7ddc38 | bellard | qemu_mod_timer(ts, expire_time); |
1067 | 8a7ddc38 | bellard | } else {
|
1068 | 8a7ddc38 | bellard | qemu_del_timer(ts); |
1069 | 8a7ddc38 | bellard | } |
1070 | 8a7ddc38 | bellard | } |
1071 | 8a7ddc38 | bellard | |
1072 | 2faf58cd | Juan Quintela | static const VMStateDescription vmstate_timers = { |
1073 | 2faf58cd | Juan Quintela | .name = "timer",
|
1074 | 2faf58cd | Juan Quintela | .version_id = 2,
|
1075 | 2faf58cd | Juan Quintela | .minimum_version_id = 1,
|
1076 | 2faf58cd | Juan Quintela | .minimum_version_id_old = 1,
|
1077 | 2faf58cd | Juan Quintela | .fields = (VMStateField []) { |
1078 | 2faf58cd | Juan Quintela | VMSTATE_INT64(cpu_ticks_offset, TimersState), |
1079 | 274dfed8 | Anthony Liguori | VMSTATE_INT64(dummy, TimersState), |
1080 | 2faf58cd | Juan Quintela | VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
|
1081 | 2faf58cd | Juan Quintela | VMSTATE_END_OF_LIST() |
1082 | c88676f8 | bellard | } |
1083 | 2faf58cd | Juan Quintela | }; |
1084 | 8a7ddc38 | bellard | |
1085 | 50317c7f | aliguori | static void qemu_event_increment(void); |
1086 | 50317c7f | aliguori | |
1087 | 67b915a5 | bellard | #ifdef _WIN32
|
1088 | b9e82a59 | blueswir1 | static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg, |
1089 | b9e82a59 | blueswir1 | DWORD_PTR dwUser, DWORD_PTR dw1, |
1090 | b9e82a59 | blueswir1 | DWORD_PTR dw2) |
1091 | 67b915a5 | bellard | #else
|
1092 | 8a7ddc38 | bellard | static void host_alarm_handler(int host_signum) |
1093 | 67b915a5 | bellard | #endif
|
1094 | 8a7ddc38 | bellard | { |
1095 | 02ba45c5 | bellard | #if 0
|
1096 | 02ba45c5 | bellard | #define DISP_FREQ 1000
|
1097 | 02ba45c5 | bellard | {
|
1098 | 02ba45c5 | bellard | static int64_t delta_min = INT64_MAX;
|
1099 | 02ba45c5 | bellard | static int64_t delta_max, delta_cum, last_clock, delta, ti;
|
1100 | 02ba45c5 | bellard | static int count;
|
1101 | 02ba45c5 | bellard | ti = qemu_get_clock(vm_clock);
|
1102 | 02ba45c5 | bellard | if (last_clock != 0) {
|
1103 | 02ba45c5 | bellard | delta = ti - last_clock;
|
1104 | 02ba45c5 | bellard | if (delta < delta_min)
|
1105 | 02ba45c5 | bellard | delta_min = delta;
|
1106 | 02ba45c5 | bellard | if (delta > delta_max)
|
1107 | 02ba45c5 | bellard | delta_max = delta;
|
1108 | 02ba45c5 | bellard | delta_cum += delta;
|
1109 | 02ba45c5 | bellard | if (++count == DISP_FREQ) {
|
1110 | 26a76461 | bellard | printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
|
1111 | 6ee093c9 | Juan Quintela | muldiv64(delta_min, 1000000, get_ticks_per_sec()),
|
1112 | 6ee093c9 | Juan Quintela | muldiv64(delta_max, 1000000, get_ticks_per_sec()),
|
1113 | 6ee093c9 | Juan Quintela | muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
|
1114 | 6ee093c9 | Juan Quintela | (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
|
1115 | 02ba45c5 | bellard | count = 0;
|
1116 | 02ba45c5 | bellard | delta_min = INT64_MAX;
|
1117 | 02ba45c5 | bellard | delta_max = 0;
|
1118 | 02ba45c5 | bellard | delta_cum = 0;
|
1119 | 02ba45c5 | bellard | }
|
1120 | 02ba45c5 | bellard | }
|
1121 | 02ba45c5 | bellard | last_clock = ti;
|
1122 | 02ba45c5 | bellard | }
|
1123 | 02ba45c5 | bellard | #endif
|
1124 | efe75411 | ths | if (alarm_has_dynticks(alarm_timer) ||
|
1125 | 2e70f6ef | pbrook | (!use_icount && |
1126 | 2e70f6ef | pbrook | qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
1127 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock))) || |
1128 | 8a7ddc38 | bellard | qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
1129 | 8a7ddc38 | bellard | qemu_get_clock(rt_clock))) { |
1130 | 50317c7f | aliguori | qemu_event_increment(); |
1131 | e332340a | Jean-Christophe Dubois | if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
|
1132 | d5d08334 | balrog | |
1133 | d6dc3d42 | aliguori | #ifndef CONFIG_IOTHREAD
|
1134 | d6dc3d42 | aliguori | if (next_cpu) {
|
1135 | 4f8eb8da | balrog | /* stop the currently executing cpu because a timer occured */
|
1136 | d6dc3d42 | aliguori | cpu_exit(next_cpu); |
1137 | 4f8eb8da | balrog | } |
1138 | d6dc3d42 | aliguori | #endif
|
1139 | 43b96858 | aliguori | timer_alarm_pending = 1;
|
1140 | d9f75a4e | aliguori | qemu_notify_event(); |
1141 | 8a7ddc38 | bellard | } |
1142 | 8a7ddc38 | bellard | } |
1143 | 8a7ddc38 | bellard | |
1144 | 2e70f6ef | pbrook | static int64_t qemu_next_deadline(void) |
1145 | efe75411 | ths | { |
1146 | 2e70f6ef | pbrook | int64_t delta; |
1147 | efe75411 | ths | |
1148 | efe75411 | ths | if (active_timers[QEMU_TIMER_VIRTUAL]) {
|
1149 | 2e70f6ef | pbrook | delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time - |
1150 | 2e70f6ef | pbrook | qemu_get_clock(vm_clock); |
1151 | 2e70f6ef | pbrook | } else {
|
1152 | 2e70f6ef | pbrook | /* To avoid problems with overflow limit this to 2^32. */
|
1153 | 2e70f6ef | pbrook | delta = INT32_MAX; |
1154 | efe75411 | ths | } |
1155 | efe75411 | ths | |
1156 | 2e70f6ef | pbrook | if (delta < 0) |
1157 | 2e70f6ef | pbrook | delta = 0;
|
1158 | efe75411 | ths | |
1159 | 2e70f6ef | pbrook | return delta;
|
1160 | 2e70f6ef | pbrook | } |
1161 | 2e70f6ef | pbrook | |
1162 | 8632fb9a | blueswir1 | #if defined(__linux__) || defined(_WIN32)
|
1163 | 2e70f6ef | pbrook | static uint64_t qemu_next_deadline_dyntick(void) |
1164 | 2e70f6ef | pbrook | { |
1165 | 2e70f6ef | pbrook | int64_t delta; |
1166 | 2e70f6ef | pbrook | int64_t rtdelta; |
1167 | 2e70f6ef | pbrook | |
1168 | 2e70f6ef | pbrook | if (use_icount)
|
1169 | 2e70f6ef | pbrook | delta = INT32_MAX; |
1170 | 2e70f6ef | pbrook | else
|
1171 | 2e70f6ef | pbrook | delta = (qemu_next_deadline() + 999) / 1000; |
1172 | 2e70f6ef | pbrook | |
1173 | 2e70f6ef | pbrook | if (active_timers[QEMU_TIMER_REALTIME]) {
|
1174 | 2e70f6ef | pbrook | rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time - |
1175 | 2e70f6ef | pbrook | qemu_get_clock(rt_clock))*1000;
|
1176 | 2e70f6ef | pbrook | if (rtdelta < delta)
|
1177 | 2e70f6ef | pbrook | delta = rtdelta; |
1178 | 2e70f6ef | pbrook | } |
1179 | 2e70f6ef | pbrook | |
1180 | 2e70f6ef | pbrook | if (delta < MIN_TIMER_REARM_US)
|
1181 | 2e70f6ef | pbrook | delta = MIN_TIMER_REARM_US; |
1182 | 2e70f6ef | pbrook | |
1183 | 2e70f6ef | pbrook | return delta;
|
1184 | efe75411 | ths | } |
1185 | 8632fb9a | blueswir1 | #endif
|
1186 | efe75411 | ths | |
1187 | fd872598 | bellard | #ifndef _WIN32
|
1188 | fd872598 | bellard | |
1189 | 7183b4b4 | aliguori | /* Sets a specific flag */
|
1190 | 7183b4b4 | aliguori | static int fcntl_setfl(int fd, int flag) |
1191 | 7183b4b4 | aliguori | { |
1192 | 7183b4b4 | aliguori | int flags;
|
1193 | 7183b4b4 | aliguori | |
1194 | 7183b4b4 | aliguori | flags = fcntl(fd, F_GETFL); |
1195 | 7183b4b4 | aliguori | if (flags == -1) |
1196 | 7183b4b4 | aliguori | return -errno;
|
1197 | 7183b4b4 | aliguori | |
1198 | 7183b4b4 | aliguori | if (fcntl(fd, F_SETFL, flags | flag) == -1) |
1199 | 7183b4b4 | aliguori | return -errno;
|
1200 | 7183b4b4 | aliguori | |
1201 | 7183b4b4 | aliguori | return 0; |
1202 | 7183b4b4 | aliguori | } |
1203 | 7183b4b4 | aliguori | |
1204 | 829309c7 | bellard | #if defined(__linux__)
|
1205 | 829309c7 | bellard | |
1206 | fd872598 | bellard | #define RTC_FREQ 1024 |
1207 | fd872598 | bellard | |
1208 | de9a95f0 | aurel32 | static void enable_sigio_timer(int fd) |
1209 | c8994013 | ths | { |
1210 | c8994013 | ths | struct sigaction act;
|
1211 | c8994013 | ths | |
1212 | c8994013 | ths | /* timer signal */
|
1213 | c8994013 | ths | sigfillset(&act.sa_mask); |
1214 | c8994013 | ths | act.sa_flags = 0;
|
1215 | c8994013 | ths | act.sa_handler = host_alarm_handler; |
1216 | c8994013 | ths | |
1217 | c8994013 | ths | sigaction(SIGIO, &act, NULL);
|
1218 | 7183b4b4 | aliguori | fcntl_setfl(fd, O_ASYNC); |
1219 | c8994013 | ths | fcntl(fd, F_SETOWN, getpid()); |
1220 | c8994013 | ths | } |
1221 | 829309c7 | bellard | |
1222 | c40ec5a9 | ths | static int hpet_start_timer(struct qemu_alarm_timer *t) |
1223 | c40ec5a9 | ths | { |
1224 | c40ec5a9 | ths | struct hpet_info info;
|
1225 | c40ec5a9 | ths | int r, fd;
|
1226 | c40ec5a9 | ths | |
1227 | c40ec5a9 | ths | fd = open("/dev/hpet", O_RDONLY);
|
1228 | c40ec5a9 | ths | if (fd < 0) |
1229 | c40ec5a9 | ths | return -1; |
1230 | c40ec5a9 | ths | |
1231 | c40ec5a9 | ths | /* Set frequency */
|
1232 | c40ec5a9 | ths | r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
1233 | c40ec5a9 | ths | if (r < 0) { |
1234 | c40ec5a9 | ths | fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
|
1235 | c40ec5a9 | ths | "error, but for better emulation accuracy type:\n"
|
1236 | c40ec5a9 | ths | "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
|
1237 | c40ec5a9 | ths | goto fail;
|
1238 | c40ec5a9 | ths | } |
1239 | c40ec5a9 | ths | |
1240 | c40ec5a9 | ths | /* Check capabilities */
|
1241 | c40ec5a9 | ths | r = ioctl(fd, HPET_INFO, &info); |
1242 | c40ec5a9 | ths | if (r < 0) |
1243 | c40ec5a9 | ths | goto fail;
|
1244 | c40ec5a9 | ths | |
1245 | c40ec5a9 | ths | /* Enable periodic mode */
|
1246 | c40ec5a9 | ths | r = ioctl(fd, HPET_EPI, 0);
|
1247 | c40ec5a9 | ths | if (info.hi_flags && (r < 0)) |
1248 | c40ec5a9 | ths | goto fail;
|
1249 | c40ec5a9 | ths | |
1250 | c40ec5a9 | ths | /* Enable interrupt */
|
1251 | c40ec5a9 | ths | r = ioctl(fd, HPET_IE_ON, 0);
|
1252 | c40ec5a9 | ths | if (r < 0) |
1253 | c40ec5a9 | ths | goto fail;
|
1254 | c40ec5a9 | ths | |
1255 | c40ec5a9 | ths | enable_sigio_timer(fd); |
1256 | fcdc2129 | pbrook | t->priv = (void *)(long)fd; |
1257 | c40ec5a9 | ths | |
1258 | c40ec5a9 | ths | return 0; |
1259 | c40ec5a9 | ths | fail:
|
1260 | c40ec5a9 | ths | close(fd); |
1261 | c40ec5a9 | ths | return -1; |
1262 | c40ec5a9 | ths | } |
1263 | c40ec5a9 | ths | |
1264 | c40ec5a9 | ths | static void hpet_stop_timer(struct qemu_alarm_timer *t) |
1265 | c40ec5a9 | ths | { |
1266 | fcdc2129 | pbrook | int fd = (long)t->priv; |
1267 | c40ec5a9 | ths | |
1268 | c40ec5a9 | ths | close(fd); |
1269 | c40ec5a9 | ths | } |
1270 | c40ec5a9 | ths | |
1271 | c8994013 | ths | static int rtc_start_timer(struct qemu_alarm_timer *t) |
1272 | fd872598 | bellard | { |
1273 | c8994013 | ths | int rtc_fd;
|
1274 | b5a23ad4 | balrog | unsigned long current_rtc_freq = 0; |
1275 | c8994013 | ths | |
1276 | aeb30be6 | balrog | TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
|
1277 | fd872598 | bellard | if (rtc_fd < 0) |
1278 | fd872598 | bellard | return -1; |
1279 | b5a23ad4 | balrog | ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
1280 | b5a23ad4 | balrog | if (current_rtc_freq != RTC_FREQ &&
|
1281 | b5a23ad4 | balrog | ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
|
1282 | fd872598 | bellard | fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
|
1283 | fd872598 | bellard | "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
|
1284 | fd872598 | bellard | "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
|
1285 | fd872598 | bellard | goto fail;
|
1286 | fd872598 | bellard | } |
1287 | fd872598 | bellard | if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
1288 | fd872598 | bellard | fail:
|
1289 | fd872598 | bellard | close(rtc_fd); |
1290 | fd872598 | bellard | return -1; |
1291 | fd872598 | bellard | } |
1292 | c8994013 | ths | |
1293 | c8994013 | ths | enable_sigio_timer(rtc_fd); |
1294 | c8994013 | ths | |
1295 | fcdc2129 | pbrook | t->priv = (void *)(long)rtc_fd; |
1296 | c8994013 | ths | |
1297 | fd872598 | bellard | return 0; |
1298 | fd872598 | bellard | } |
1299 | fd872598 | bellard | |
1300 | c8994013 | ths | static void rtc_stop_timer(struct qemu_alarm_timer *t) |
1301 | 829309c7 | bellard | { |
1302 | fcdc2129 | pbrook | int rtc_fd = (long)t->priv; |
1303 | c8994013 | ths | |
1304 | c8994013 | ths | close(rtc_fd); |
1305 | 829309c7 | bellard | } |
1306 | 829309c7 | bellard | |
1307 | efe75411 | ths | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
1308 | efe75411 | ths | { |
1309 | efe75411 | ths | struct sigevent ev;
|
1310 | efe75411 | ths | timer_t host_timer; |
1311 | efe75411 | ths | struct sigaction act;
|
1312 | efe75411 | ths | |
1313 | efe75411 | ths | sigfillset(&act.sa_mask); |
1314 | efe75411 | ths | act.sa_flags = 0;
|
1315 | efe75411 | ths | act.sa_handler = host_alarm_handler; |
1316 | efe75411 | ths | |
1317 | efe75411 | ths | sigaction(SIGALRM, &act, NULL);
|
1318 | efe75411 | ths | |
1319 | 9ed415b2 | Jean-Christophe Dubois | /*
|
1320 | 9ed415b2 | Jean-Christophe Dubois | * Initialize ev struct to 0 to avoid valgrind complaining
|
1321 | 9ed415b2 | Jean-Christophe Dubois | * about uninitialized data in timer_create call
|
1322 | 9ed415b2 | Jean-Christophe Dubois | */
|
1323 | 9ed415b2 | Jean-Christophe Dubois | memset(&ev, 0, sizeof(ev)); |
1324 | efe75411 | ths | ev.sigev_value.sival_int = 0;
|
1325 | efe75411 | ths | ev.sigev_notify = SIGEV_SIGNAL; |
1326 | efe75411 | ths | ev.sigev_signo = SIGALRM; |
1327 | efe75411 | ths | |
1328 | efe75411 | ths | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
|
1329 | efe75411 | ths | perror("timer_create");
|
1330 | efe75411 | ths | |
1331 | efe75411 | ths | /* disable dynticks */
|
1332 | efe75411 | ths | fprintf(stderr, "Dynamic Ticks disabled\n");
|
1333 | efe75411 | ths | |
1334 | efe75411 | ths | return -1; |
1335 | efe75411 | ths | } |
1336 | efe75411 | ths | |
1337 | 0399bfe0 | blueswir1 | t->priv = (void *)(long)host_timer; |
1338 | efe75411 | ths | |
1339 | efe75411 | ths | return 0; |
1340 | efe75411 | ths | } |
1341 | efe75411 | ths | |
1342 | efe75411 | ths | static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
1343 | efe75411 | ths | { |
1344 | 0399bfe0 | blueswir1 | timer_t host_timer = (timer_t)(long)t->priv;
|
1345 | efe75411 | ths | |
1346 | efe75411 | ths | timer_delete(host_timer); |
1347 | efe75411 | ths | } |
1348 | efe75411 | ths | |
1349 | efe75411 | ths | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
1350 | efe75411 | ths | { |
1351 | 0399bfe0 | blueswir1 | timer_t host_timer = (timer_t)(long)t->priv;
|
1352 | efe75411 | ths | struct itimerspec timeout;
|
1353 | efe75411 | ths | int64_t nearest_delta_us = INT64_MAX; |
1354 | efe75411 | ths | int64_t current_us; |
1355 | efe75411 | ths | |
1356 | efe75411 | ths | if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1357 | efe75411 | ths | !active_timers[QEMU_TIMER_VIRTUAL]) |
1358 | d5d08334 | balrog | return;
|
1359 | efe75411 | ths | |
1360 | 2e70f6ef | pbrook | nearest_delta_us = qemu_next_deadline_dyntick(); |
1361 | efe75411 | ths | |
1362 | efe75411 | ths | /* check whether a timer is already running */
|
1363 | efe75411 | ths | if (timer_gettime(host_timer, &timeout)) {
|
1364 | efe75411 | ths | perror("gettime");
|
1365 | efe75411 | ths | fprintf(stderr, "Internal timer error: aborting\n");
|
1366 | efe75411 | ths | exit(1);
|
1367 | efe75411 | ths | } |
1368 | efe75411 | ths | current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
1369 | efe75411 | ths | if (current_us && current_us <= nearest_delta_us)
|
1370 | efe75411 | ths | return;
|
1371 | efe75411 | ths | |
1372 | efe75411 | ths | timeout.it_interval.tv_sec = 0;
|
1373 | efe75411 | ths | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
1374 | efe75411 | ths | timeout.it_value.tv_sec = nearest_delta_us / 1000000;
|
1375 | efe75411 | ths | timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
1376 | efe75411 | ths | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
1377 | efe75411 | ths | perror("settime");
|
1378 | efe75411 | ths | fprintf(stderr, "Internal timer error: aborting\n");
|
1379 | efe75411 | ths | exit(1);
|
1380 | efe75411 | ths | } |
1381 | efe75411 | ths | } |
1382 | efe75411 | ths | |
1383 | 70744b3a | ths | #endif /* defined(__linux__) */ |
1384 | 231c6586 | ths | |
1385 | c8994013 | ths | static int unix_start_timer(struct qemu_alarm_timer *t) |
1386 | c8994013 | ths | { |
1387 | c8994013 | ths | struct sigaction act;
|
1388 | c8994013 | ths | struct itimerval itv;
|
1389 | c8994013 | ths | int err;
|
1390 | c8994013 | ths | |
1391 | c8994013 | ths | /* timer signal */
|
1392 | c8994013 | ths | sigfillset(&act.sa_mask); |
1393 | c8994013 | ths | act.sa_flags = 0;
|
1394 | c8994013 | ths | act.sa_handler = host_alarm_handler; |
1395 | c8994013 | ths | |
1396 | c8994013 | ths | sigaction(SIGALRM, &act, NULL);
|
1397 | c8994013 | ths | |
1398 | c8994013 | ths | itv.it_interval.tv_sec = 0;
|
1399 | c8994013 | ths | /* for i386 kernel 2.6 to get 1 ms */
|
1400 | c8994013 | ths | itv.it_interval.tv_usec = 999;
|
1401 | c8994013 | ths | itv.it_value.tv_sec = 0;
|
1402 | c8994013 | ths | itv.it_value.tv_usec = 10 * 1000; |
1403 | c8994013 | ths | |
1404 | c8994013 | ths | err = setitimer(ITIMER_REAL, &itv, NULL);
|
1405 | c8994013 | ths | if (err)
|
1406 | c8994013 | ths | return -1; |
1407 | c8994013 | ths | |
1408 | c8994013 | ths | return 0; |
1409 | c8994013 | ths | } |
1410 | c8994013 | ths | |
1411 | c8994013 | ths | static void unix_stop_timer(struct qemu_alarm_timer *t) |
1412 | c8994013 | ths | { |
1413 | c8994013 | ths | struct itimerval itv;
|
1414 | c8994013 | ths | |
1415 | c8994013 | ths | memset(&itv, 0, sizeof(itv)); |
1416 | c8994013 | ths | setitimer(ITIMER_REAL, &itv, NULL);
|
1417 | c8994013 | ths | } |
1418 | c8994013 | ths | |
1419 | 829309c7 | bellard | #endif /* !defined(_WIN32) */ |
1420 | fd872598 | bellard | |
1421 | f49e58dc | aliguori | |
1422 | c8994013 | ths | #ifdef _WIN32
|
1423 | c8994013 | ths | |
1424 | c8994013 | ths | static int win32_start_timer(struct qemu_alarm_timer *t) |
1425 | c8994013 | ths | { |
1426 | c8994013 | ths | TIMECAPS tc; |
1427 | c8994013 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1428 | efe75411 | ths | UINT flags; |
1429 | c8994013 | ths | |
1430 | c8994013 | ths | memset(&tc, 0, sizeof(tc)); |
1431 | c8994013 | ths | timeGetDevCaps(&tc, sizeof(tc));
|
1432 | c8994013 | ths | |
1433 | c8994013 | ths | if (data->period < tc.wPeriodMin)
|
1434 | c8994013 | ths | data->period = tc.wPeriodMin; |
1435 | c8994013 | ths | |
1436 | c8994013 | ths | timeBeginPeriod(data->period); |
1437 | c8994013 | ths | |
1438 | efe75411 | ths | flags = TIME_CALLBACK_FUNCTION; |
1439 | efe75411 | ths | if (alarm_has_dynticks(t))
|
1440 | efe75411 | ths | flags |= TIME_ONESHOT; |
1441 | efe75411 | ths | else
|
1442 | efe75411 | ths | flags |= TIME_PERIODIC; |
1443 | efe75411 | ths | |
1444 | c8994013 | ths | data->timerId = timeSetEvent(1, // interval (ms) |
1445 | c8994013 | ths | data->period, // resolution
|
1446 | c8994013 | ths | host_alarm_handler, // function
|
1447 | c8994013 | ths | (DWORD)t, // parameter
|
1448 | efe75411 | ths | flags); |
1449 | c8994013 | ths | |
1450 | c8994013 | ths | if (!data->timerId) {
|
1451 | 20889d4e | Blue Swirl | fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
|
1452 | 705e83f6 | malc | GetLastError()); |
1453 | c8994013 | ths | timeEndPeriod(data->period); |
1454 | c8994013 | ths | return -1; |
1455 | c8994013 | ths | } |
1456 | c8994013 | ths | |
1457 | c8994013 | ths | return 0; |
1458 | c8994013 | ths | } |
1459 | c8994013 | ths | |
1460 | c8994013 | ths | static void win32_stop_timer(struct qemu_alarm_timer *t) |
1461 | c8994013 | ths | { |
1462 | c8994013 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1463 | c8994013 | ths | |
1464 | c8994013 | ths | timeKillEvent(data->timerId); |
1465 | c8994013 | ths | timeEndPeriod(data->period); |
1466 | c8994013 | ths | } |
1467 | c8994013 | ths | |
1468 | efe75411 | ths | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
1469 | efe75411 | ths | { |
1470 | efe75411 | ths | struct qemu_alarm_win32 *data = t->priv;
|
1471 | efe75411 | ths | uint64_t nearest_delta_us; |
1472 | efe75411 | ths | |
1473 | efe75411 | ths | if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1474 | efe75411 | ths | !active_timers[QEMU_TIMER_VIRTUAL]) |
1475 | d5d08334 | balrog | return;
|
1476 | efe75411 | ths | |
1477 | 2e70f6ef | pbrook | nearest_delta_us = qemu_next_deadline_dyntick(); |
1478 | efe75411 | ths | nearest_delta_us /= 1000;
|
1479 | efe75411 | ths | |
1480 | efe75411 | ths | timeKillEvent(data->timerId); |
1481 | efe75411 | ths | |
1482 | efe75411 | ths | data->timerId = timeSetEvent(1,
|
1483 | efe75411 | ths | data->period, |
1484 | efe75411 | ths | host_alarm_handler, |
1485 | efe75411 | ths | (DWORD)t, |
1486 | efe75411 | ths | TIME_ONESHOT | TIME_PERIODIC); |
1487 | efe75411 | ths | |
1488 | efe75411 | ths | if (!data->timerId) {
|
1489 | 20889d4e | Blue Swirl | fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
|
1490 | 705e83f6 | malc | GetLastError()); |
1491 | efe75411 | ths | |
1492 | efe75411 | ths | timeEndPeriod(data->period); |
1493 | efe75411 | ths | exit(1);
|
1494 | efe75411 | ths | } |
1495 | efe75411 | ths | } |
1496 | efe75411 | ths | |
1497 | c8994013 | ths | #endif /* _WIN32 */ |
1498 | c8994013 | ths | |
1499 | 7183b4b4 | aliguori | static int init_timer_alarm(void) |
1500 | 8a7ddc38 | bellard | { |
1501 | 223f0d72 | blueswir1 | struct qemu_alarm_timer *t = NULL; |
1502 | c8994013 | ths | int i, err = -1; |
1503 | f49e58dc | aliguori | |
1504 | c8994013 | ths | for (i = 0; alarm_timers[i].name; i++) { |
1505 | c8994013 | ths | t = &alarm_timers[i]; |
1506 | c8994013 | ths | |
1507 | c8994013 | ths | err = t->start(t); |
1508 | c8994013 | ths | if (!err)
|
1509 | c8994013 | ths | break;
|
1510 | 67b915a5 | bellard | } |
1511 | fd872598 | bellard | |
1512 | c8994013 | ths | if (err) {
|
1513 | 7183b4b4 | aliguori | err = -ENOENT; |
1514 | 7183b4b4 | aliguori | goto fail;
|
1515 | 67b915a5 | bellard | } |
1516 | c8994013 | ths | |
1517 | c8994013 | ths | alarm_timer = t; |
1518 | 7183b4b4 | aliguori | |
1519 | 6abfbd79 | aliguori | return 0; |
1520 | 7183b4b4 | aliguori | |
1521 | 7183b4b4 | aliguori | fail:
|
1522 | 7183b4b4 | aliguori | return err;
|
1523 | 8a7ddc38 | bellard | } |
1524 | 8a7ddc38 | bellard | |
1525 | 9596ebb7 | pbrook | static void quit_timers(void) |
1526 | 40c3bac3 | bellard | { |
1527 | c8994013 | ths | alarm_timer->stop(alarm_timer); |
1528 | c8994013 | ths | alarm_timer = NULL;
|
1529 | 40c3bac3 | bellard | } |
1530 | 40c3bac3 | bellard | |
1531 | c4b1fcc0 | bellard | /***********************************************************/
|
1532 | f6503059 | balrog | /* host time/date access */
|
1533 | f6503059 | balrog | void qemu_get_timedate(struct tm *tm, int offset) |
1534 | f6503059 | balrog | { |
1535 | f6503059 | balrog | time_t ti; |
1536 | f6503059 | balrog | struct tm *ret;
|
1537 | f6503059 | balrog | |
1538 | f6503059 | balrog | time(&ti); |
1539 | f6503059 | balrog | ti += offset; |
1540 | f6503059 | balrog | if (rtc_date_offset == -1) { |
1541 | f6503059 | balrog | if (rtc_utc)
|
1542 | f6503059 | balrog | ret = gmtime(&ti); |
1543 | f6503059 | balrog | else
|
1544 | f6503059 | balrog | ret = localtime(&ti); |
1545 | f6503059 | balrog | } else {
|
1546 | f6503059 | balrog | ti -= rtc_date_offset; |
1547 | f6503059 | balrog | ret = gmtime(&ti); |
1548 | f6503059 | balrog | } |
1549 | f6503059 | balrog | |
1550 | f6503059 | balrog | memcpy(tm, ret, sizeof(struct tm)); |
1551 | f6503059 | balrog | } |
1552 | f6503059 | balrog | |
1553 | f6503059 | balrog | int qemu_timedate_diff(struct tm *tm) |
1554 | f6503059 | balrog | { |
1555 | f6503059 | balrog | time_t seconds; |
1556 | f6503059 | balrog | |
1557 | f6503059 | balrog | if (rtc_date_offset == -1) |
1558 | f6503059 | balrog | if (rtc_utc)
|
1559 | f6503059 | balrog | seconds = mktimegm(tm); |
1560 | f6503059 | balrog | else
|
1561 | f6503059 | balrog | seconds = mktime(tm); |
1562 | f6503059 | balrog | else
|
1563 | f6503059 | balrog | seconds = mktimegm(tm) + rtc_date_offset; |
1564 | f6503059 | balrog | |
1565 | f6503059 | balrog | return seconds - time(NULL); |
1566 | f6503059 | balrog | } |
1567 | f6503059 | balrog | |
1568 | fd1dff4b | bellard | #ifdef _WIN32
|
1569 | fd1dff4b | bellard | static void socket_cleanup(void) |
1570 | fd1dff4b | bellard | { |
1571 | fd1dff4b | bellard | WSACleanup(); |
1572 | fd1dff4b | bellard | } |
1573 | 82c643ff | bellard | |
1574 | fd1dff4b | bellard | static int socket_init(void) |
1575 | fd1dff4b | bellard | { |
1576 | fd1dff4b | bellard | WSADATA Data; |
1577 | fd1dff4b | bellard | int ret, err;
|
1578 | fd1dff4b | bellard | |
1579 | fd1dff4b | bellard | ret = WSAStartup(MAKEWORD(2,2), &Data); |
1580 | fd1dff4b | bellard | if (ret != 0) { |
1581 | fd1dff4b | bellard | err = WSAGetLastError(); |
1582 | fd1dff4b | bellard | fprintf(stderr, "WSAStartup: %d\n", err);
|
1583 | fd1dff4b | bellard | return -1; |
1584 | fd1dff4b | bellard | } |
1585 | fd1dff4b | bellard | atexit(socket_cleanup); |
1586 | fd1dff4b | bellard | return 0; |
1587 | fd1dff4b | bellard | } |
1588 | 64b7b733 | aurel32 | #endif
|
1589 | 64b7b733 | aurel32 | |
1590 | 1ae26a18 | balrog | /***********************************************************/
|
1591 | 1ae26a18 | balrog | /* Bluetooth support */
|
1592 | 1ae26a18 | balrog | static int nb_hcis; |
1593 | 1ae26a18 | balrog | static int cur_hci; |
1594 | 1ae26a18 | balrog | static struct HCIInfo *hci_table[MAX_NICS]; |
1595 | dc72ac14 | balrog | |
1596 | 1ae26a18 | balrog | static struct bt_vlan_s { |
1597 | 1ae26a18 | balrog | struct bt_scatternet_s net;
|
1598 | 1ae26a18 | balrog | int id;
|
1599 | 1ae26a18 | balrog | struct bt_vlan_s *next;
|
1600 | 1ae26a18 | balrog | } *first_bt_vlan; |
1601 | 1ae26a18 | balrog | |
1602 | 1ae26a18 | balrog | /* find or alloc a new bluetooth "VLAN" */
|
1603 | 674bb261 | blueswir1 | static struct bt_scatternet_s *qemu_find_bt_vlan(int id) |
1604 | 1ae26a18 | balrog | { |
1605 | 1ae26a18 | balrog | struct bt_vlan_s **pvlan, *vlan;
|
1606 | 1ae26a18 | balrog | for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) { |
1607 | 1ae26a18 | balrog | if (vlan->id == id)
|
1608 | 1ae26a18 | balrog | return &vlan->net;
|
1609 | 1ae26a18 | balrog | } |
1610 | 1ae26a18 | balrog | vlan = qemu_mallocz(sizeof(struct bt_vlan_s)); |
1611 | 1ae26a18 | balrog | vlan->id = id; |
1612 | 1ae26a18 | balrog | pvlan = &first_bt_vlan; |
1613 | 1ae26a18 | balrog | while (*pvlan != NULL) |
1614 | 1ae26a18 | balrog | pvlan = &(*pvlan)->next; |
1615 | 1ae26a18 | balrog | *pvlan = vlan; |
1616 | 1ae26a18 | balrog | return &vlan->net;
|
1617 | 1ae26a18 | balrog | } |
1618 | 1ae26a18 | balrog | |
1619 | 1ae26a18 | balrog | static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len) |
1620 | 1ae26a18 | balrog | { |
1621 | 1ae26a18 | balrog | } |
1622 | 1ae26a18 | balrog | |
1623 | 1ae26a18 | balrog | static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr) |
1624 | 1ae26a18 | balrog | { |
1625 | 1ae26a18 | balrog | return -ENOTSUP;
|
1626 | 1ae26a18 | balrog | } |
1627 | 1ae26a18 | balrog | |
1628 | 1ae26a18 | balrog | static struct HCIInfo null_hci = { |
1629 | 1ae26a18 | balrog | .cmd_send = null_hci_send, |
1630 | 1ae26a18 | balrog | .sco_send = null_hci_send, |
1631 | 1ae26a18 | balrog | .acl_send = null_hci_send, |
1632 | 1ae26a18 | balrog | .bdaddr_set = null_hci_addr_set, |
1633 | 1ae26a18 | balrog | }; |
1634 | 1ae26a18 | balrog | |
1635 | 1ae26a18 | balrog | struct HCIInfo *qemu_next_hci(void) |
1636 | 1ae26a18 | balrog | { |
1637 | 1ae26a18 | balrog | if (cur_hci == nb_hcis)
|
1638 | 1ae26a18 | balrog | return &null_hci;
|
1639 | 1ae26a18 | balrog | |
1640 | 1ae26a18 | balrog | return hci_table[cur_hci++];
|
1641 | 1ae26a18 | balrog | } |
1642 | 1ae26a18 | balrog | |
1643 | dc72ac14 | balrog | static struct HCIInfo *hci_init(const char *str) |
1644 | dc72ac14 | balrog | { |
1645 | dc72ac14 | balrog | char *endp;
|
1646 | dc72ac14 | balrog | struct bt_scatternet_s *vlan = 0; |
1647 | dc72ac14 | balrog | |
1648 | dc72ac14 | balrog | if (!strcmp(str, "null")) |
1649 | dc72ac14 | balrog | /* null */
|
1650 | dc72ac14 | balrog | return &null_hci;
|
1651 | dc72ac14 | balrog | else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':')) |
1652 | dc72ac14 | balrog | /* host[:hciN] */
|
1653 | dc72ac14 | balrog | return bt_host_hci(str[4] ? str + 5 : "hci0"); |
1654 | dc72ac14 | balrog | else if (!strncmp(str, "hci", 3)) { |
1655 | dc72ac14 | balrog | /* hci[,vlan=n] */
|
1656 | dc72ac14 | balrog | if (str[3]) { |
1657 | dc72ac14 | balrog | if (!strncmp(str + 3, ",vlan=", 6)) { |
1658 | dc72ac14 | balrog | vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0)); |
1659 | dc72ac14 | balrog | if (*endp)
|
1660 | dc72ac14 | balrog | vlan = 0;
|
1661 | dc72ac14 | balrog | } |
1662 | dc72ac14 | balrog | } else
|
1663 | dc72ac14 | balrog | vlan = qemu_find_bt_vlan(0);
|
1664 | dc72ac14 | balrog | if (vlan)
|
1665 | dc72ac14 | balrog | return bt_new_hci(vlan);
|
1666 | dc72ac14 | balrog | } |
1667 | dc72ac14 | balrog | |
1668 | dc72ac14 | balrog | fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
|
1669 | dc72ac14 | balrog | |
1670 | dc72ac14 | balrog | return 0; |
1671 | dc72ac14 | balrog | } |
1672 | dc72ac14 | balrog | |
1673 | dc72ac14 | balrog | static int bt_hci_parse(const char *str) |
1674 | dc72ac14 | balrog | { |
1675 | dc72ac14 | balrog | struct HCIInfo *hci;
|
1676 | c227f099 | Anthony Liguori | bdaddr_t bdaddr; |
1677 | dc72ac14 | balrog | |
1678 | dc72ac14 | balrog | if (nb_hcis >= MAX_NICS) {
|
1679 | dc72ac14 | balrog | fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
|
1680 | dc72ac14 | balrog | return -1; |
1681 | dc72ac14 | balrog | } |
1682 | dc72ac14 | balrog | |
1683 | dc72ac14 | balrog | hci = hci_init(str); |
1684 | dc72ac14 | balrog | if (!hci)
|
1685 | dc72ac14 | balrog | return -1; |
1686 | dc72ac14 | balrog | |
1687 | dc72ac14 | balrog | bdaddr.b[0] = 0x52; |
1688 | dc72ac14 | balrog | bdaddr.b[1] = 0x54; |
1689 | dc72ac14 | balrog | bdaddr.b[2] = 0x00; |
1690 | dc72ac14 | balrog | bdaddr.b[3] = 0x12; |
1691 | dc72ac14 | balrog | bdaddr.b[4] = 0x34; |
1692 | dc72ac14 | balrog | bdaddr.b[5] = 0x56 + nb_hcis; |
1693 | dc72ac14 | balrog | hci->bdaddr_set(hci, bdaddr.b); |
1694 | dc72ac14 | balrog | |
1695 | dc72ac14 | balrog | hci_table[nb_hcis++] = hci; |
1696 | dc72ac14 | balrog | |
1697 | dc72ac14 | balrog | return 0; |
1698 | dc72ac14 | balrog | } |
1699 | dc72ac14 | balrog | |
1700 | dc72ac14 | balrog | static void bt_vhci_add(int vlan_id) |
1701 | dc72ac14 | balrog | { |
1702 | dc72ac14 | balrog | struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
|
1703 | dc72ac14 | balrog | |
1704 | dc72ac14 | balrog | if (!vlan->slave)
|
1705 | dc72ac14 | balrog | fprintf(stderr, "qemu: warning: adding a VHCI to "
|
1706 | dc72ac14 | balrog | "an empty scatternet %i\n", vlan_id);
|
1707 | dc72ac14 | balrog | |
1708 | dc72ac14 | balrog | bt_vhci_init(bt_new_hci(vlan)); |
1709 | dc72ac14 | balrog | } |
1710 | dc72ac14 | balrog | |
1711 | dc72ac14 | balrog | static struct bt_device_s *bt_device_add(const char *opt) |
1712 | dc72ac14 | balrog | { |
1713 | dc72ac14 | balrog | struct bt_scatternet_s *vlan;
|
1714 | dc72ac14 | balrog | int vlan_id = 0; |
1715 | dc72ac14 | balrog | char *endp = strstr(opt, ",vlan="); |
1716 | dc72ac14 | balrog | int len = (endp ? endp - opt : strlen(opt)) + 1; |
1717 | dc72ac14 | balrog | char devname[10]; |
1718 | dc72ac14 | balrog | |
1719 | dc72ac14 | balrog | pstrcpy(devname, MIN(sizeof(devname), len), opt);
|
1720 | dc72ac14 | balrog | |
1721 | dc72ac14 | balrog | if (endp) {
|
1722 | dc72ac14 | balrog | vlan_id = strtol(endp + 6, &endp, 0); |
1723 | dc72ac14 | balrog | if (*endp) {
|
1724 | dc72ac14 | balrog | fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
|
1725 | dc72ac14 | balrog | return 0; |
1726 | dc72ac14 | balrog | } |
1727 | dc72ac14 | balrog | } |
1728 | dc72ac14 | balrog | |
1729 | dc72ac14 | balrog | vlan = qemu_find_bt_vlan(vlan_id); |
1730 | dc72ac14 | balrog | |
1731 | dc72ac14 | balrog | if (!vlan->slave)
|
1732 | dc72ac14 | balrog | fprintf(stderr, "qemu: warning: adding a slave device to "
|
1733 | dc72ac14 | balrog | "an empty scatternet %i\n", vlan_id);
|
1734 | dc72ac14 | balrog | |
1735 | dc72ac14 | balrog | if (!strcmp(devname, "keyboard")) |
1736 | dc72ac14 | balrog | return bt_keyboard_init(vlan);
|
1737 | dc72ac14 | balrog | |
1738 | dc72ac14 | balrog | fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
|
1739 | dc72ac14 | balrog | return 0; |
1740 | dc72ac14 | balrog | } |
1741 | dc72ac14 | balrog | |
1742 | dc72ac14 | balrog | static int bt_parse(const char *opt) |
1743 | dc72ac14 | balrog | { |
1744 | dc72ac14 | balrog | const char *endp, *p; |
1745 | dc72ac14 | balrog | int vlan;
|
1746 | dc72ac14 | balrog | |
1747 | dc72ac14 | balrog | if (strstart(opt, "hci", &endp)) { |
1748 | dc72ac14 | balrog | if (!*endp || *endp == ',') { |
1749 | dc72ac14 | balrog | if (*endp)
|
1750 | dc72ac14 | balrog | if (!strstart(endp, ",vlan=", 0)) |
1751 | dc72ac14 | balrog | opt = endp + 1;
|
1752 | dc72ac14 | balrog | |
1753 | dc72ac14 | balrog | return bt_hci_parse(opt);
|
1754 | dc72ac14 | balrog | } |
1755 | dc72ac14 | balrog | } else if (strstart(opt, "vhci", &endp)) { |
1756 | dc72ac14 | balrog | if (!*endp || *endp == ',') { |
1757 | dc72ac14 | balrog | if (*endp) {
|
1758 | dc72ac14 | balrog | if (strstart(endp, ",vlan=", &p)) { |
1759 | dc72ac14 | balrog | vlan = strtol(p, (char **) &endp, 0); |
1760 | dc72ac14 | balrog | if (*endp) {
|
1761 | dc72ac14 | balrog | fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
|
1762 | dc72ac14 | balrog | return 1; |
1763 | dc72ac14 | balrog | } |
1764 | dc72ac14 | balrog | } else {
|
1765 | dc72ac14 | balrog | fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1); |
1766 | dc72ac14 | balrog | return 1; |
1767 | dc72ac14 | balrog | } |
1768 | dc72ac14 | balrog | } else
|
1769 | dc72ac14 | balrog | vlan = 0;
|
1770 | dc72ac14 | balrog | |
1771 | dc72ac14 | balrog | bt_vhci_add(vlan); |
1772 | dc72ac14 | balrog | return 0; |
1773 | dc72ac14 | balrog | } |
1774 | dc72ac14 | balrog | } else if (strstart(opt, "device:", &endp)) |
1775 | dc72ac14 | balrog | return !bt_device_add(endp);
|
1776 | dc72ac14 | balrog | |
1777 | dc72ac14 | balrog | fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
|
1778 | dc72ac14 | balrog | return 1; |
1779 | dc72ac14 | balrog | } |
1780 | dc72ac14 | balrog | |
1781 | 1ae26a18 | balrog | /***********************************************************/
|
1782 | 1ae26a18 | balrog | /* QEMU Block devices */
|
1783 | 1ae26a18 | balrog | |
1784 | 609497ab | balrog | #define HD_ALIAS "index=%d,media=disk" |
1785 | e4bcb14c | ths | #define CDROM_ALIAS "index=2,media=cdrom" |
1786 | e4bcb14c | ths | #define FD_ALIAS "index=%d,if=floppy" |
1787 | 609497ab | balrog | #define PFLASH_ALIAS "if=pflash" |
1788 | 609497ab | balrog | #define MTD_ALIAS "if=mtd" |
1789 | 9d413d1d | balrog | #define SD_ALIAS "index=0,if=sd" |
1790 | e4bcb14c | ths | |
1791 | 9dfd7c7a | Gerd Hoffmann | QemuOpts *drive_add(const char *file, const char *fmt, ...) |
1792 | e4bcb14c | ths | { |
1793 | e4bcb14c | ths | va_list ap; |
1794 | 9dfd7c7a | Gerd Hoffmann | char optstr[1024]; |
1795 | 9dfd7c7a | Gerd Hoffmann | QemuOpts *opts; |
1796 | e4bcb14c | ths | |
1797 | e4bcb14c | ths | va_start(ap, fmt); |
1798 | 9dfd7c7a | Gerd Hoffmann | vsnprintf(optstr, sizeof(optstr), fmt, ap);
|
1799 | e4bcb14c | ths | va_end(ap); |
1800 | e4bcb14c | ths | |
1801 | 7282a033 | Gerd Hoffmann | opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL);
|
1802 | 9dfd7c7a | Gerd Hoffmann | if (!opts) {
|
1803 | 9dfd7c7a | Gerd Hoffmann | fprintf(stderr, "%s: huh? duplicate? (%s)\n",
|
1804 | 9dfd7c7a | Gerd Hoffmann | __FUNCTION__, optstr); |
1805 | 9dfd7c7a | Gerd Hoffmann | return NULL; |
1806 | 9dfd7c7a | Gerd Hoffmann | } |
1807 | 9dfd7c7a | Gerd Hoffmann | if (file)
|
1808 | 9dfd7c7a | Gerd Hoffmann | qemu_opt_set(opts, "file", file);
|
1809 | 9dfd7c7a | Gerd Hoffmann | return opts;
|
1810 | b01b1111 | aliguori | } |
1811 | b01b1111 | aliguori | |
1812 | 751c6a17 | Gerd Hoffmann | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) |
1813 | e4bcb14c | ths | { |
1814 | 751c6a17 | Gerd Hoffmann | DriveInfo *dinfo; |
1815 | e4bcb14c | ths | |
1816 | e4bcb14c | ths | /* seek interface, bus and unit */
|
1817 | e4bcb14c | ths | |
1818 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(dinfo, &drives, next) { |
1819 | 751c6a17 | Gerd Hoffmann | if (dinfo->type == type &&
|
1820 | 751c6a17 | Gerd Hoffmann | dinfo->bus == bus && |
1821 | 751c6a17 | Gerd Hoffmann | dinfo->unit == unit) |
1822 | 751c6a17 | Gerd Hoffmann | return dinfo;
|
1823 | 751c6a17 | Gerd Hoffmann | } |
1824 | e4bcb14c | ths | |
1825 | 751c6a17 | Gerd Hoffmann | return NULL; |
1826 | e4bcb14c | ths | } |
1827 | e4bcb14c | ths | |
1828 | 2e810b36 | Gerd Hoffmann | DriveInfo *drive_get_by_id(const char *id) |
1829 | 1dae12e6 | Gerd Hoffmann | { |
1830 | 1dae12e6 | Gerd Hoffmann | DriveInfo *dinfo; |
1831 | 1dae12e6 | Gerd Hoffmann | |
1832 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(dinfo, &drives, next) { |
1833 | 1dae12e6 | Gerd Hoffmann | if (strcmp(id, dinfo->id))
|
1834 | 1dae12e6 | Gerd Hoffmann | continue;
|
1835 | 1dae12e6 | Gerd Hoffmann | return dinfo;
|
1836 | 1dae12e6 | Gerd Hoffmann | } |
1837 | 1dae12e6 | Gerd Hoffmann | return NULL; |
1838 | 1dae12e6 | Gerd Hoffmann | } |
1839 | 1dae12e6 | Gerd Hoffmann | |
1840 | f60d39bc | ths | int drive_get_max_bus(BlockInterfaceType type)
|
1841 | e4bcb14c | ths | { |
1842 | e4bcb14c | ths | int max_bus;
|
1843 | 751c6a17 | Gerd Hoffmann | DriveInfo *dinfo; |
1844 | e4bcb14c | ths | |
1845 | e4bcb14c | ths | max_bus = -1;
|
1846 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(dinfo, &drives, next) { |
1847 | 751c6a17 | Gerd Hoffmann | if(dinfo->type == type &&
|
1848 | 751c6a17 | Gerd Hoffmann | dinfo->bus > max_bus) |
1849 | 751c6a17 | Gerd Hoffmann | max_bus = dinfo->bus; |
1850 | e4bcb14c | ths | } |
1851 | e4bcb14c | ths | return max_bus;
|
1852 | e4bcb14c | ths | } |
1853 | e4bcb14c | ths | |
1854 | fa879c64 | aliguori | const char *drive_get_serial(BlockDriverState *bdrv) |
1855 | fa879c64 | aliguori | { |
1856 | 751c6a17 | Gerd Hoffmann | DriveInfo *dinfo; |
1857 | fa879c64 | aliguori | |
1858 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(dinfo, &drives, next) { |
1859 | 751c6a17 | Gerd Hoffmann | if (dinfo->bdrv == bdrv)
|
1860 | 751c6a17 | Gerd Hoffmann | return dinfo->serial;
|
1861 | 751c6a17 | Gerd Hoffmann | } |
1862 | fa879c64 | aliguori | |
1863 | fa879c64 | aliguori | return "\0"; |
1864 | fa879c64 | aliguori | } |
1865 | fa879c64 | aliguori | |
1866 | 428c5705 | aliguori | BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv) |
1867 | 428c5705 | aliguori | { |
1868 | 751c6a17 | Gerd Hoffmann | DriveInfo *dinfo; |
1869 | 428c5705 | aliguori | |
1870 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(dinfo, &drives, next) { |
1871 | 751c6a17 | Gerd Hoffmann | if (dinfo->bdrv == bdrv)
|
1872 | 751c6a17 | Gerd Hoffmann | return dinfo->onerror;
|
1873 | 751c6a17 | Gerd Hoffmann | } |
1874 | 428c5705 | aliguori | |
1875 | cdad4bd8 | aliguori | return BLOCK_ERR_STOP_ENOSPC;
|
1876 | 428c5705 | aliguori | } |
1877 | 428c5705 | aliguori | |
1878 | a1620fac | aurel32 | static void bdrv_format_print(void *opaque, const char *name) |
1879 | a1620fac | aurel32 | { |
1880 | a1620fac | aurel32 | fprintf(stderr, " %s", name);
|
1881 | a1620fac | aurel32 | } |
1882 | a1620fac | aurel32 | |
1883 | b01b1111 | aliguori | void drive_uninit(BlockDriverState *bdrv)
|
1884 | b01b1111 | aliguori | { |
1885 | 751c6a17 | Gerd Hoffmann | DriveInfo *dinfo; |
1886 | b01b1111 | aliguori | |
1887 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(dinfo, &drives, next) { |
1888 | 751c6a17 | Gerd Hoffmann | if (dinfo->bdrv != bdrv)
|
1889 | 751c6a17 | Gerd Hoffmann | continue;
|
1890 | 9dfd7c7a | Gerd Hoffmann | qemu_opts_del(dinfo->opts); |
1891 | 72cf2d4f | Blue Swirl | QTAILQ_REMOVE(&drives, dinfo, next); |
1892 | 751c6a17 | Gerd Hoffmann | qemu_free(dinfo); |
1893 | 751c6a17 | Gerd Hoffmann | break;
|
1894 | 751c6a17 | Gerd Hoffmann | } |
1895 | b01b1111 | aliguori | } |
1896 | b01b1111 | aliguori | |
1897 | 9dfd7c7a | Gerd Hoffmann | DriveInfo *drive_init(QemuOpts *opts, void *opaque,
|
1898 | 751c6a17 | Gerd Hoffmann | int *fatal_error)
|
1899 | e4bcb14c | ths | { |
1900 | 9dfd7c7a | Gerd Hoffmann | const char *buf; |
1901 | 9dfd7c7a | Gerd Hoffmann | const char *file = NULL; |
1902 | c8522bdf | balrog | char devname[128]; |
1903 | 9dfd7c7a | Gerd Hoffmann | const char *serial; |
1904 | c8522bdf | balrog | const char *mediastr = ""; |
1905 | f60d39bc | ths | BlockInterfaceType type; |
1906 | e4bcb14c | ths | enum { MEDIA_DISK, MEDIA_CDROM } media;
|
1907 | e4bcb14c | ths | int bus_id, unit_id;
|
1908 | e4bcb14c | ths | int cyls, heads, secs, translation;
|
1909 | 1e72d3b7 | aurel32 | BlockDriver *drv = NULL;
|
1910 | 4d73cd3b | aliguori | QEMUMachine *machine = opaque; |
1911 | e4bcb14c | ths | int max_devs;
|
1912 | e4bcb14c | ths | int index;
|
1913 | 33f00271 | balrog | int cache;
|
1914 | 5c6c3a6c | Christoph Hellwig | int aio = 0; |
1915 | 428c5705 | aliguori | int bdrv_flags, onerror;
|
1916 | c2cc47a4 | Markus Armbruster | const char *devaddr; |
1917 | 751c6a17 | Gerd Hoffmann | DriveInfo *dinfo; |
1918 | 9dfd7c7a | Gerd Hoffmann | int snapshot = 0; |
1919 | e4bcb14c | ths | |
1920 | 9dfd7c7a | Gerd Hoffmann | *fatal_error = 1;
|
1921 | e4bcb14c | ths | |
1922 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_AUTO; |
1923 | 0aa217e4 | Kevin Wolf | cache = 1;
|
1924 | e4bcb14c | ths | |
1925 | 4d007814 | Gerd Hoffmann | if (machine && machine->use_scsi) {
|
1926 | f60d39bc | ths | type = IF_SCSI; |
1927 | e4bcb14c | ths | max_devs = MAX_SCSI_DEVS; |
1928 | 363a37d5 | blueswir1 | pstrcpy(devname, sizeof(devname), "scsi"); |
1929 | e4bcb14c | ths | } else {
|
1930 | f60d39bc | ths | type = IF_IDE; |
1931 | e4bcb14c | ths | max_devs = MAX_IDE_DEVS; |
1932 | 363a37d5 | blueswir1 | pstrcpy(devname, sizeof(devname), "ide"); |
1933 | e4bcb14c | ths | } |
1934 | e4bcb14c | ths | media = MEDIA_DISK; |
1935 | e4bcb14c | ths | |
1936 | e4bcb14c | ths | /* extract parameters */
|
1937 | 9dfd7c7a | Gerd Hoffmann | bus_id = qemu_opt_get_number(opts, "bus", 0); |
1938 | 9dfd7c7a | Gerd Hoffmann | unit_id = qemu_opt_get_number(opts, "unit", -1); |
1939 | 9dfd7c7a | Gerd Hoffmann | index = qemu_opt_get_number(opts, "index", -1); |
1940 | e4bcb14c | ths | |
1941 | 9dfd7c7a | Gerd Hoffmann | cyls = qemu_opt_get_number(opts, "cyls", 0); |
1942 | 9dfd7c7a | Gerd Hoffmann | heads = qemu_opt_get_number(opts, "heads", 0); |
1943 | 9dfd7c7a | Gerd Hoffmann | secs = qemu_opt_get_number(opts, "secs", 0); |
1944 | e4bcb14c | ths | |
1945 | 9dfd7c7a | Gerd Hoffmann | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
1946 | e4bcb14c | ths | |
1947 | 9dfd7c7a | Gerd Hoffmann | file = qemu_opt_get(opts, "file");
|
1948 | 9dfd7c7a | Gerd Hoffmann | serial = qemu_opt_get(opts, "serial");
|
1949 | 9dfd7c7a | Gerd Hoffmann | |
1950 | 9dfd7c7a | Gerd Hoffmann | if ((buf = qemu_opt_get(opts, "if")) != NULL) { |
1951 | ae45d369 | bellard | pstrcpy(devname, sizeof(devname), buf);
|
1952 | e4bcb14c | ths | if (!strcmp(buf, "ide")) { |
1953 | f60d39bc | ths | type = IF_IDE; |
1954 | e4bcb14c | ths | max_devs = MAX_IDE_DEVS; |
1955 | e4bcb14c | ths | } else if (!strcmp(buf, "scsi")) { |
1956 | f60d39bc | ths | type = IF_SCSI; |
1957 | e4bcb14c | ths | max_devs = MAX_SCSI_DEVS; |
1958 | e4bcb14c | ths | } else if (!strcmp(buf, "floppy")) { |
1959 | f60d39bc | ths | type = IF_FLOPPY; |
1960 | e4bcb14c | ths | max_devs = 0;
|
1961 | e4bcb14c | ths | } else if (!strcmp(buf, "pflash")) { |
1962 | f60d39bc | ths | type = IF_PFLASH; |
1963 | e4bcb14c | ths | max_devs = 0;
|
1964 | e4bcb14c | ths | } else if (!strcmp(buf, "mtd")) { |
1965 | f60d39bc | ths | type = IF_MTD; |
1966 | e4bcb14c | ths | max_devs = 0;
|
1967 | e4bcb14c | ths | } else if (!strcmp(buf, "sd")) { |
1968 | f60d39bc | ths | type = IF_SD; |
1969 | e4bcb14c | ths | max_devs = 0;
|
1970 | 6e02c38d | aliguori | } else if (!strcmp(buf, "virtio")) { |
1971 | 6e02c38d | aliguori | type = IF_VIRTIO; |
1972 | 6e02c38d | aliguori | max_devs = 0;
|
1973 | 62d23efa | aliguori | } else if (!strcmp(buf, "xen")) { |
1974 | 62d23efa | aliguori | type = IF_XEN; |
1975 | 62d23efa | aliguori | max_devs = 0;
|
1976 | a8659e90 | Gerd Hoffmann | } else if (!strcmp(buf, "none")) { |
1977 | a8659e90 | Gerd Hoffmann | type = IF_NONE; |
1978 | a8659e90 | Gerd Hoffmann | max_devs = 0;
|
1979 | 62d23efa | aliguori | } else {
|
1980 | 9dfd7c7a | Gerd Hoffmann | fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
|
1981 | 751c6a17 | Gerd Hoffmann | return NULL; |
1982 | e4bcb14c | ths | } |
1983 | e4bcb14c | ths | } |
1984 | e4bcb14c | ths | |
1985 | e4bcb14c | ths | if (cyls || heads || secs) {
|
1986 | e4bcb14c | ths | if (cyls < 1 || cyls > 16383) { |
1987 | 9dfd7c7a | Gerd Hoffmann | fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
|
1988 | 751c6a17 | Gerd Hoffmann | return NULL; |
1989 | e4bcb14c | ths | } |
1990 | e4bcb14c | ths | if (heads < 1 || heads > 16) { |
1991 | 9dfd7c7a | Gerd Hoffmann | fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
|
1992 | 751c6a17 | Gerd Hoffmann | return NULL; |
1993 | e4bcb14c | ths | } |
1994 | e4bcb14c | ths | if (secs < 1 || secs > 63) { |
1995 | 9dfd7c7a | Gerd Hoffmann | fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
|
1996 | 751c6a17 | Gerd Hoffmann | return NULL; |
1997 | e4bcb14c | ths | } |
1998 | e4bcb14c | ths | } |
1999 | e4bcb14c | ths | |
2000 | 9dfd7c7a | Gerd Hoffmann | if ((buf = qemu_opt_get(opts, "trans")) != NULL) { |
2001 | e4bcb14c | ths | if (!cyls) {
|
2002 | e4bcb14c | ths | fprintf(stderr, |
2003 | e4bcb14c | ths | "qemu: '%s' trans must be used with cyls,heads and secs\n",
|
2004 | 9dfd7c7a | Gerd Hoffmann | buf); |
2005 | 751c6a17 | Gerd Hoffmann | return NULL; |
2006 | e4bcb14c | ths | } |
2007 | e4bcb14c | ths | if (!strcmp(buf, "none")) |
2008 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_NONE; |
2009 | e4bcb14c | ths | else if (!strcmp(buf, "lba")) |
2010 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_LBA; |
2011 | e4bcb14c | ths | else if (!strcmp(buf, "auto")) |
2012 | e4bcb14c | ths | translation = BIOS_ATA_TRANSLATION_AUTO; |
2013 | e4bcb14c | ths | else {
|
2014 | 9dfd7c7a | Gerd Hoffmann | fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
|
2015 | 751c6a17 | Gerd Hoffmann | return NULL; |
2016 | e4bcb14c | ths | } |
2017 | e4bcb14c | ths | } |
2018 | e4bcb14c | ths | |
2019 | 9dfd7c7a | Gerd Hoffmann | if ((buf = qemu_opt_get(opts, "media")) != NULL) { |
2020 | e4bcb14c | ths | if (!strcmp(buf, "disk")) { |
2021 | e4bcb14c | ths | media = MEDIA_DISK; |
2022 | e4bcb14c | ths | } else if (!strcmp(buf, "cdrom")) { |
2023 | e4bcb14c | ths | if (cyls || secs || heads) {
|
2024 | e4bcb14c | ths | fprintf(stderr, |
2025 | 9dfd7c7a | Gerd Hoffmann | "qemu: '%s' invalid physical CHS format\n", buf);
|
2026 | 751c6a17 | Gerd Hoffmann | return NULL; |
2027 | e4bcb14c | ths | } |
2028 | e4bcb14c | ths | media = MEDIA_CDROM; |
2029 | e4bcb14c | ths | } else {
|
2030 | 9dfd7c7a | Gerd Hoffmann | fprintf(stderr, "qemu: '%s' invalid media\n", buf);
|
2031 | 751c6a17 | Gerd Hoffmann | return NULL; |
2032 | e4bcb14c | ths | } |
2033 | e4bcb14c | ths | } |
2034 | e4bcb14c | ths | |
2035 | 9dfd7c7a | Gerd Hoffmann | if ((buf = qemu_opt_get(opts, "cache")) != NULL) { |
2036 | 9f7965c7 | aliguori | if (!strcmp(buf, "off") || !strcmp(buf, "none")) |
2037 | 33f00271 | balrog | cache = 0;
|
2038 | 9f7965c7 | aliguori | else if (!strcmp(buf, "writethrough")) |
2039 | 33f00271 | balrog | cache = 1;
|
2040 | 9f7965c7 | aliguori | else if (!strcmp(buf, "writeback")) |
2041 | 9f7965c7 | aliguori | cache = 2;
|
2042 | 33f00271 | balrog | else {
|
2043 | 33f00271 | balrog | fprintf(stderr, "qemu: invalid cache option\n");
|
2044 | 751c6a17 | Gerd Hoffmann | return NULL; |
2045 | 33f00271 | balrog | } |
2046 | 33f00271 | balrog | } |
2047 | 33f00271 | balrog | |
2048 | 5c6c3a6c | Christoph Hellwig | #ifdef CONFIG_LINUX_AIO
|
2049 | 5c6c3a6c | Christoph Hellwig | if ((buf = qemu_opt_get(opts, "aio")) != NULL) { |
2050 | 5c6c3a6c | Christoph Hellwig | if (!strcmp(buf, "threads")) |
2051 | 5c6c3a6c | Christoph Hellwig | aio = 0;
|
2052 | 5c6c3a6c | Christoph Hellwig | else if (!strcmp(buf, "native")) |
2053 | 5c6c3a6c | Christoph Hellwig | aio = 1;
|
2054 | 5c6c3a6c | Christoph Hellwig | else {
|
2055 | 5c6c3a6c | Christoph Hellwig | fprintf(stderr, "qemu: invalid aio option\n");
|
2056 | 5c6c3a6c | Christoph Hellwig | return NULL; |
2057 | 5c6c3a6c | Christoph Hellwig | } |
2058 | 5c6c3a6c | Christoph Hellwig | } |
2059 | 5c6c3a6c | Christoph Hellwig | #endif
|
2060 | 5c6c3a6c | Christoph Hellwig | |
2061 | 9dfd7c7a | Gerd Hoffmann | if ((buf = qemu_opt_get(opts, "format")) != NULL) { |
2062 | a1620fac | aurel32 | if (strcmp(buf, "?") == 0) { |
2063 | a1620fac | aurel32 | fprintf(stderr, "qemu: Supported formats:");
|
2064 | a1620fac | aurel32 | bdrv_iterate_format(bdrv_format_print, NULL);
|
2065 | a1620fac | aurel32 | fprintf(stderr, "\n");
|
2066 | 751c6a17 | Gerd Hoffmann | return NULL; |
2067 | a1620fac | aurel32 | } |
2068 | 1e72d3b7 | aurel32 | drv = bdrv_find_format(buf); |
2069 | 1e72d3b7 | aurel32 | if (!drv) {
|
2070 | 1e72d3b7 | aurel32 | fprintf(stderr, "qemu: '%s' invalid format\n", buf);
|
2071 | 751c6a17 | Gerd Hoffmann | return NULL; |
2072 | 1e72d3b7 | aurel32 | } |
2073 | 1e72d3b7 | aurel32 | } |
2074 | 1e72d3b7 | aurel32 | |
2075 | cdad4bd8 | aliguori | onerror = BLOCK_ERR_STOP_ENOSPC; |
2076 | 9dfd7c7a | Gerd Hoffmann | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
2077 | 869a5c6d | aliguori | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
|
2078 | ea8a5d7f | aliguori | fprintf(stderr, "werror is no supported by this format\n");
|
2079 | 751c6a17 | Gerd Hoffmann | return NULL; |
2080 | 428c5705 | aliguori | } |
2081 | 428c5705 | aliguori | if (!strcmp(buf, "ignore")) |
2082 | 428c5705 | aliguori | onerror = BLOCK_ERR_IGNORE; |
2083 | 428c5705 | aliguori | else if (!strcmp(buf, "enospc")) |
2084 | 428c5705 | aliguori | onerror = BLOCK_ERR_STOP_ENOSPC; |
2085 | 428c5705 | aliguori | else if (!strcmp(buf, "stop")) |
2086 | 428c5705 | aliguori | onerror = BLOCK_ERR_STOP_ANY; |
2087 | 428c5705 | aliguori | else if (!strcmp(buf, "report")) |
2088 | 428c5705 | aliguori | onerror = BLOCK_ERR_REPORT; |
2089 | 428c5705 | aliguori | else {
|
2090 | 428c5705 | aliguori | fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
|
2091 | 751c6a17 | Gerd Hoffmann | return NULL; |
2092 | 428c5705 | aliguori | } |
2093 | 428c5705 | aliguori | } |
2094 | 428c5705 | aliguori | |
2095 | 9dfd7c7a | Gerd Hoffmann | if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { |
2096 | c2cc47a4 | Markus Armbruster | if (type != IF_VIRTIO) {
|
2097 | 9dfd7c7a | Gerd Hoffmann | fprintf(stderr, "addr is not supported\n");
|
2098 | 751c6a17 | Gerd Hoffmann | return NULL; |
2099 | c2cc47a4 | Markus Armbruster | } |
2100 | c2cc47a4 | Markus Armbruster | } |
2101 | c2cc47a4 | Markus Armbruster | |
2102 | e4bcb14c | ths | /* compute bus and unit according index */
|
2103 | e4bcb14c | ths | |
2104 | e4bcb14c | ths | if (index != -1) { |
2105 | e4bcb14c | ths | if (bus_id != 0 || unit_id != -1) { |
2106 | e4bcb14c | ths | fprintf(stderr, |
2107 | 9dfd7c7a | Gerd Hoffmann | "qemu: index cannot be used with bus and unit\n");
|
2108 | 751c6a17 | Gerd Hoffmann | return NULL; |
2109 | e4bcb14c | ths | } |
2110 | e4bcb14c | ths | if (max_devs == 0) |
2111 | e4bcb14c | ths | { |
2112 | e4bcb14c | ths | unit_id = index; |
2113 | e4bcb14c | ths | bus_id = 0;
|
2114 | e4bcb14c | ths | } else {
|
2115 | e4bcb14c | ths | unit_id = index % max_devs; |
2116 | e4bcb14c | ths | bus_id = index / max_devs; |
2117 | e4bcb14c | ths | } |
2118 | e4bcb14c | ths | } |
2119 | e4bcb14c | ths | |
2120 | e4bcb14c | ths | /* if user doesn't specify a unit_id,
|
2121 | e4bcb14c | ths | * try to find the first free
|
2122 | e4bcb14c | ths | */
|
2123 | e4bcb14c | ths | |
2124 | e4bcb14c | ths | if (unit_id == -1) { |
2125 | e4bcb14c | ths | unit_id = 0;
|
2126 | 751c6a17 | Gerd Hoffmann | while (drive_get(type, bus_id, unit_id) != NULL) { |
2127 | e4bcb14c | ths | unit_id++; |
2128 | e4bcb14c | ths | if (max_devs && unit_id >= max_devs) {
|
2129 | e4bcb14c | ths | unit_id -= max_devs; |
2130 | e4bcb14c | ths | bus_id++; |
2131 | e4bcb14c | ths | } |
2132 | e4bcb14c | ths | } |
2133 | e4bcb14c | ths | } |
2134 | e4bcb14c | ths | |
2135 | e4bcb14c | ths | /* check unit id */
|
2136 | e4bcb14c | ths | |
2137 | e4bcb14c | ths | if (max_devs && unit_id >= max_devs) {
|
2138 | 9dfd7c7a | Gerd Hoffmann | fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
|
2139 | 9dfd7c7a | Gerd Hoffmann | unit_id, max_devs - 1);
|
2140 | 751c6a17 | Gerd Hoffmann | return NULL; |
2141 | e4bcb14c | ths | } |
2142 | e4bcb14c | ths | |
2143 | e4bcb14c | ths | /*
|
2144 | e4bcb14c | ths | * ignore multiple definitions
|
2145 | e4bcb14c | ths | */
|
2146 | e4bcb14c | ths | |
2147 | 751c6a17 | Gerd Hoffmann | if (drive_get(type, bus_id, unit_id) != NULL) { |
2148 | 751c6a17 | Gerd Hoffmann | *fatal_error = 0;
|
2149 | 751c6a17 | Gerd Hoffmann | return NULL; |
2150 | 751c6a17 | Gerd Hoffmann | } |
2151 | e4bcb14c | ths | |
2152 | e4bcb14c | ths | /* init */
|
2153 | e4bcb14c | ths | |
2154 | 751c6a17 | Gerd Hoffmann | dinfo = qemu_mallocz(sizeof(*dinfo));
|
2155 | e23d9c4d | Gerd Hoffmann | if ((buf = qemu_opts_id(opts)) != NULL) { |
2156 | 9dfd7c7a | Gerd Hoffmann | dinfo->id = qemu_strdup(buf); |
2157 | 9dfd7c7a | Gerd Hoffmann | } else {
|
2158 | 1dae12e6 | Gerd Hoffmann | /* no id supplied -> create one */
|
2159 | 9dfd7c7a | Gerd Hoffmann | dinfo->id = qemu_mallocz(32);
|
2160 | 1dae12e6 | Gerd Hoffmann | if (type == IF_IDE || type == IF_SCSI)
|
2161 | 1dae12e6 | Gerd Hoffmann | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
2162 | 1dae12e6 | Gerd Hoffmann | if (max_devs)
|
2163 | 9dfd7c7a | Gerd Hoffmann | snprintf(dinfo->id, 32, "%s%i%s%i", |
2164 | 1dae12e6 | Gerd Hoffmann | devname, bus_id, mediastr, unit_id); |
2165 | 1dae12e6 | Gerd Hoffmann | else
|
2166 | 9dfd7c7a | Gerd Hoffmann | snprintf(dinfo->id, 32, "%s%s%i", |
2167 | 1dae12e6 | Gerd Hoffmann | devname, mediastr, unit_id); |
2168 | 1dae12e6 | Gerd Hoffmann | } |
2169 | 1dae12e6 | Gerd Hoffmann | dinfo->bdrv = bdrv_new(dinfo->id); |
2170 | 751c6a17 | Gerd Hoffmann | dinfo->devaddr = devaddr; |
2171 | 751c6a17 | Gerd Hoffmann | dinfo->type = type; |
2172 | 751c6a17 | Gerd Hoffmann | dinfo->bus = bus_id; |
2173 | 751c6a17 | Gerd Hoffmann | dinfo->unit = unit_id; |
2174 | 751c6a17 | Gerd Hoffmann | dinfo->onerror = onerror; |
2175 | 9dfd7c7a | Gerd Hoffmann | dinfo->opts = opts; |
2176 | 9dfd7c7a | Gerd Hoffmann | if (serial)
|
2177 | 9dfd7c7a | Gerd Hoffmann | strncpy(dinfo->serial, serial, sizeof(serial));
|
2178 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_TAIL(&drives, dinfo, next); |
2179 | e4bcb14c | ths | |
2180 | f60d39bc | ths | switch(type) {
|
2181 | e4bcb14c | ths | case IF_IDE:
|
2182 | e4bcb14c | ths | case IF_SCSI:
|
2183 | 62d23efa | aliguori | case IF_XEN:
|
2184 | c219331e | Gerd Hoffmann | case IF_NONE:
|
2185 | e4bcb14c | ths | switch(media) {
|
2186 | e4bcb14c | ths | case MEDIA_DISK:
|
2187 | e4bcb14c | ths | if (cyls != 0) { |
2188 | 1dae12e6 | Gerd Hoffmann | bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs); |
2189 | 1dae12e6 | Gerd Hoffmann | bdrv_set_translation_hint(dinfo->bdrv, translation); |
2190 | e4bcb14c | ths | } |
2191 | e4bcb14c | ths | break;
|
2192 | e4bcb14c | ths | case MEDIA_CDROM:
|
2193 | 1dae12e6 | Gerd Hoffmann | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM); |
2194 | e4bcb14c | ths | break;
|
2195 | e4bcb14c | ths | } |
2196 | e4bcb14c | ths | break;
|
2197 | e4bcb14c | ths | case IF_SD:
|
2198 | e4bcb14c | ths | /* FIXME: This isn't really a floppy, but it's a reasonable
|
2199 | e4bcb14c | ths | approximation. */
|
2200 | e4bcb14c | ths | case IF_FLOPPY:
|
2201 | 1dae12e6 | Gerd Hoffmann | bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY); |
2202 | e4bcb14c | ths | break;
|
2203 | e4bcb14c | ths | case IF_PFLASH:
|
2204 | e4bcb14c | ths | case IF_MTD:
|
2205 | e4bcb14c | ths | break;
|
2206 | d176c495 | Gerd Hoffmann | case IF_VIRTIO:
|
2207 | d176c495 | Gerd Hoffmann | /* add virtio block device */
|
2208 | d176c495 | Gerd Hoffmann | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
2209 | d176c495 | Gerd Hoffmann | qemu_opt_set(opts, "driver", "virtio-blk-pci"); |
2210 | d176c495 | Gerd Hoffmann | qemu_opt_set(opts, "drive", dinfo->id);
|
2211 | d176c495 | Gerd Hoffmann | if (devaddr)
|
2212 | d176c495 | Gerd Hoffmann | qemu_opt_set(opts, "addr", devaddr);
|
2213 | d176c495 | Gerd Hoffmann | break;
|
2214 | aae9460e | Paul Brook | case IF_COUNT:
|
2215 | aae9460e | Paul Brook | abort(); |
2216 | e4bcb14c | ths | } |
2217 | 9dfd7c7a | Gerd Hoffmann | if (!file) {
|
2218 | 751c6a17 | Gerd Hoffmann | *fatal_error = 0;
|
2219 | 751c6a17 | Gerd Hoffmann | return NULL; |
2220 | 751c6a17 | Gerd Hoffmann | } |
2221 | 33f00271 | balrog | bdrv_flags = 0;
|
2222 | 9f7965c7 | aliguori | if (snapshot) {
|
2223 | 33f00271 | balrog | bdrv_flags |= BDRV_O_SNAPSHOT; |
2224 | 9f7965c7 | aliguori | cache = 2; /* always use write-back with snapshot */ |
2225 | 9f7965c7 | aliguori | } |
2226 | 9f7965c7 | aliguori | if (cache == 0) /* no caching */ |
2227 | 9f7965c7 | aliguori | bdrv_flags |= BDRV_O_NOCACHE; |
2228 | 9f7965c7 | aliguori | else if (cache == 2) /* write-back */ |
2229 | 9f7965c7 | aliguori | bdrv_flags |= BDRV_O_CACHE_WB; |
2230 | 5c6c3a6c | Christoph Hellwig | |
2231 | 5c6c3a6c | Christoph Hellwig | if (aio == 1) { |
2232 | 5c6c3a6c | Christoph Hellwig | bdrv_flags |= BDRV_O_NATIVE_AIO; |
2233 | 5c6c3a6c | Christoph Hellwig | } else {
|
2234 | 5c6c3a6c | Christoph Hellwig | bdrv_flags &= ~BDRV_O_NATIVE_AIO; |
2235 | 5c6c3a6c | Christoph Hellwig | } |
2236 | 5c6c3a6c | Christoph Hellwig | |
2237 | 1dae12e6 | Gerd Hoffmann | if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) { |
2238 | e4bcb14c | ths | fprintf(stderr, "qemu: could not open disk image %s\n",
|
2239 | e4bcb14c | ths | file); |
2240 | 751c6a17 | Gerd Hoffmann | return NULL; |
2241 | e4bcb14c | ths | } |
2242 | 5c6c3a6c | Christoph Hellwig | |
2243 | 1dae12e6 | Gerd Hoffmann | if (bdrv_key_required(dinfo->bdrv))
|
2244 | c0f4ce77 | aliguori | autostart = 0;
|
2245 | 751c6a17 | Gerd Hoffmann | *fatal_error = 0;
|
2246 | 751c6a17 | Gerd Hoffmann | return dinfo;
|
2247 | e4bcb14c | ths | } |
2248 | e4bcb14c | ths | |
2249 | 9dfd7c7a | Gerd Hoffmann | static int drive_init_func(QemuOpts *opts, void *opaque) |
2250 | 9dfd7c7a | Gerd Hoffmann | { |
2251 | 9dfd7c7a | Gerd Hoffmann | QEMUMachine *machine = opaque; |
2252 | 9dfd7c7a | Gerd Hoffmann | int fatal_error = 0; |
2253 | 9dfd7c7a | Gerd Hoffmann | |
2254 | 9dfd7c7a | Gerd Hoffmann | if (drive_init(opts, machine, &fatal_error) == NULL) { |
2255 | 9dfd7c7a | Gerd Hoffmann | if (fatal_error)
|
2256 | 9dfd7c7a | Gerd Hoffmann | return 1; |
2257 | 9dfd7c7a | Gerd Hoffmann | } |
2258 | 9dfd7c7a | Gerd Hoffmann | return 0; |
2259 | 9dfd7c7a | Gerd Hoffmann | } |
2260 | 9dfd7c7a | Gerd Hoffmann | |
2261 | 9dfd7c7a | Gerd Hoffmann | static int drive_enable_snapshot(QemuOpts *opts, void *opaque) |
2262 | 9dfd7c7a | Gerd Hoffmann | { |
2263 | 9dfd7c7a | Gerd Hoffmann | if (NULL == qemu_opt_get(opts, "snapshot")) { |
2264 | 9dfd7c7a | Gerd Hoffmann | qemu_opt_set(opts, "snapshot", "on"); |
2265 | 9dfd7c7a | Gerd Hoffmann | } |
2266 | 9dfd7c7a | Gerd Hoffmann | return 0; |
2267 | 9dfd7c7a | Gerd Hoffmann | } |
2268 | 9dfd7c7a | Gerd Hoffmann | |
2269 | 76e30d0f | Jan Kiszka | void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque) |
2270 | 76e30d0f | Jan Kiszka | { |
2271 | 76e30d0f | Jan Kiszka | boot_set_handler = func; |
2272 | 76e30d0f | Jan Kiszka | boot_set_opaque = opaque; |
2273 | 76e30d0f | Jan Kiszka | } |
2274 | 76e30d0f | Jan Kiszka | |
2275 | 76e30d0f | Jan Kiszka | int qemu_boot_set(const char *boot_devices) |
2276 | 76e30d0f | Jan Kiszka | { |
2277 | 76e30d0f | Jan Kiszka | if (!boot_set_handler) {
|
2278 | 76e30d0f | Jan Kiszka | return -EINVAL;
|
2279 | 76e30d0f | Jan Kiszka | } |
2280 | 76e30d0f | Jan Kiszka | return boot_set_handler(boot_set_opaque, boot_devices);
|
2281 | 76e30d0f | Jan Kiszka | } |
2282 | 76e30d0f | Jan Kiszka | |
2283 | ef3adf68 | Jan Kiszka | static int parse_bootdevices(char *devices) |
2284 | ef3adf68 | Jan Kiszka | { |
2285 | ef3adf68 | Jan Kiszka | /* We just do some generic consistency checks */
|
2286 | ef3adf68 | Jan Kiszka | const char *p; |
2287 | ef3adf68 | Jan Kiszka | int bitmap = 0; |
2288 | ef3adf68 | Jan Kiszka | |
2289 | ef3adf68 | Jan Kiszka | for (p = devices; *p != '\0'; p++) { |
2290 | ef3adf68 | Jan Kiszka | /* Allowed boot devices are:
|
2291 | ef3adf68 | Jan Kiszka | * a-b: floppy disk drives
|
2292 | ef3adf68 | Jan Kiszka | * c-f: IDE disk drives
|
2293 | ef3adf68 | Jan Kiszka | * g-m: machine implementation dependant drives
|
2294 | ef3adf68 | Jan Kiszka | * n-p: network devices
|
2295 | ef3adf68 | Jan Kiszka | * It's up to each machine implementation to check if the given boot
|
2296 | ef3adf68 | Jan Kiszka | * devices match the actual hardware implementation and firmware
|
2297 | ef3adf68 | Jan Kiszka | * features.
|
2298 | ef3adf68 | Jan Kiszka | */
|
2299 | ef3adf68 | Jan Kiszka | if (*p < 'a' || *p > 'p') { |
2300 | ef3adf68 | Jan Kiszka | fprintf(stderr, "Invalid boot device '%c'\n", *p);
|
2301 | ef3adf68 | Jan Kiszka | exit(1);
|
2302 | ef3adf68 | Jan Kiszka | } |
2303 | ef3adf68 | Jan Kiszka | if (bitmap & (1 << (*p - 'a'))) { |
2304 | ef3adf68 | Jan Kiszka | fprintf(stderr, "Boot device '%c' was given twice\n", *p);
|
2305 | ef3adf68 | Jan Kiszka | exit(1);
|
2306 | ef3adf68 | Jan Kiszka | } |
2307 | ef3adf68 | Jan Kiszka | bitmap |= 1 << (*p - 'a'); |
2308 | ef3adf68 | Jan Kiszka | } |
2309 | ef3adf68 | Jan Kiszka | return bitmap;
|
2310 | ef3adf68 | Jan Kiszka | } |
2311 | ef3adf68 | Jan Kiszka | |
2312 | e0f084bf | Jan Kiszka | static void restore_boot_devices(void *opaque) |
2313 | e0f084bf | Jan Kiszka | { |
2314 | e0f084bf | Jan Kiszka | char *standard_boot_devices = opaque;
|
2315 | e0f084bf | Jan Kiszka | |
2316 | e0f084bf | Jan Kiszka | qemu_boot_set(standard_boot_devices); |
2317 | e0f084bf | Jan Kiszka | |
2318 | e0f084bf | Jan Kiszka | qemu_unregister_reset(restore_boot_devices, standard_boot_devices); |
2319 | e0f084bf | Jan Kiszka | qemu_free(standard_boot_devices); |
2320 | e0f084bf | Jan Kiszka | } |
2321 | e0f084bf | Jan Kiszka | |
2322 | 268a362c | aliguori | static void numa_add(const char *optarg) |
2323 | 268a362c | aliguori | { |
2324 | 268a362c | aliguori | char option[128]; |
2325 | 268a362c | aliguori | char *endptr;
|
2326 | 268a362c | aliguori | unsigned long long value, endvalue; |
2327 | 268a362c | aliguori | int nodenr;
|
2328 | 268a362c | aliguori | |
2329 | 268a362c | aliguori | optarg = get_opt_name(option, 128, optarg, ',') + 1; |
2330 | 268a362c | aliguori | if (!strcmp(option, "node")) { |
2331 | 268a362c | aliguori | if (get_param_value(option, 128, "nodeid", optarg) == 0) { |
2332 | 268a362c | aliguori | nodenr = nb_numa_nodes; |
2333 | 268a362c | aliguori | } else {
|
2334 | 268a362c | aliguori | nodenr = strtoull(option, NULL, 10); |
2335 | 268a362c | aliguori | } |
2336 | 268a362c | aliguori | |
2337 | 268a362c | aliguori | if (get_param_value(option, 128, "mem", optarg) == 0) { |
2338 | 268a362c | aliguori | node_mem[nodenr] = 0;
|
2339 | 268a362c | aliguori | } else {
|
2340 | 268a362c | aliguori | value = strtoull(option, &endptr, 0);
|
2341 | 268a362c | aliguori | switch (*endptr) {
|
2342 | 268a362c | aliguori | case 0: case 'M': case 'm': |
2343 | 268a362c | aliguori | value <<= 20;
|
2344 | 268a362c | aliguori | break;
|
2345 | 268a362c | aliguori | case 'G': case 'g': |
2346 | 268a362c | aliguori | value <<= 30;
|
2347 | 268a362c | aliguori | break;
|
2348 | 268a362c | aliguori | } |
2349 | 268a362c | aliguori | node_mem[nodenr] = value; |
2350 | 268a362c | aliguori | } |
2351 | 268a362c | aliguori | if (get_param_value(option, 128, "cpus", optarg) == 0) { |
2352 | 268a362c | aliguori | node_cpumask[nodenr] = 0;
|
2353 | 268a362c | aliguori | } else {
|
2354 | 268a362c | aliguori | value = strtoull(option, &endptr, 10);
|
2355 | 268a362c | aliguori | if (value >= 64) { |
2356 | 268a362c | aliguori | value = 63;
|
2357 | 268a362c | aliguori | fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
|
2358 | 268a362c | aliguori | } else {
|
2359 | 268a362c | aliguori | if (*endptr == '-') { |
2360 | 268a362c | aliguori | endvalue = strtoull(endptr+1, &endptr, 10); |
2361 | 268a362c | aliguori | if (endvalue >= 63) { |
2362 | 268a362c | aliguori | endvalue = 62;
|
2363 | 268a362c | aliguori | fprintf(stderr, |
2364 | 268a362c | aliguori | "only 63 CPUs in NUMA mode supported.\n");
|
2365 | 268a362c | aliguori | } |
2366 | 268a362c | aliguori | value = (1 << (endvalue + 1)) - (1 << value); |
2367 | 268a362c | aliguori | } else {
|
2368 | 268a362c | aliguori | value = 1 << value;
|
2369 | 268a362c | aliguori | } |
2370 | 268a362c | aliguori | } |
2371 | 268a362c | aliguori | node_cpumask[nodenr] = value; |
2372 | 268a362c | aliguori | } |
2373 | 268a362c | aliguori | nb_numa_nodes++; |
2374 | 268a362c | aliguori | } |
2375 | 268a362c | aliguori | return;
|
2376 | 268a362c | aliguori | } |
2377 | 268a362c | aliguori | |
2378 | dc6b1c09 | Andre Przywara | static void smp_parse(const char *optarg) |
2379 | dc6b1c09 | Andre Przywara | { |
2380 | dc6b1c09 | Andre Przywara | int smp, sockets = 0, threads = 0, cores = 0; |
2381 | dc6b1c09 | Andre Przywara | char *endptr;
|
2382 | dc6b1c09 | Andre Przywara | char option[128]; |
2383 | dc6b1c09 | Andre Przywara | |
2384 | dc6b1c09 | Andre Przywara | smp = strtoul(optarg, &endptr, 10);
|
2385 | dc6b1c09 | Andre Przywara | if (endptr != optarg) {
|
2386 | dc6b1c09 | Andre Przywara | if (*endptr == ',') { |
2387 | dc6b1c09 | Andre Przywara | endptr++; |
2388 | dc6b1c09 | Andre Przywara | } |
2389 | dc6b1c09 | Andre Przywara | } |
2390 | dc6b1c09 | Andre Przywara | if (get_param_value(option, 128, "sockets", endptr) != 0) |
2391 | dc6b1c09 | Andre Przywara | sockets = strtoull(option, NULL, 10); |
2392 | dc6b1c09 | Andre Przywara | if (get_param_value(option, 128, "cores", endptr) != 0) |
2393 | dc6b1c09 | Andre Przywara | cores = strtoull(option, NULL, 10); |
2394 | dc6b1c09 | Andre Przywara | if (get_param_value(option, 128, "threads", endptr) != 0) |
2395 | dc6b1c09 | Andre Przywara | threads = strtoull(option, NULL, 10); |
2396 | dc6b1c09 | Andre Przywara | if (get_param_value(option, 128, "maxcpus", endptr) != 0) |
2397 | dc6b1c09 | Andre Przywara | max_cpus = strtoull(option, NULL, 10); |
2398 | dc6b1c09 | Andre Przywara | |
2399 | dc6b1c09 | Andre Przywara | /* compute missing values, prefer sockets over cores over threads */
|
2400 | dc6b1c09 | Andre Przywara | if (smp == 0 || sockets == 0) { |
2401 | dc6b1c09 | Andre Przywara | sockets = sockets > 0 ? sockets : 1; |
2402 | dc6b1c09 | Andre Przywara | cores = cores > 0 ? cores : 1; |
2403 | dc6b1c09 | Andre Przywara | threads = threads > 0 ? threads : 1; |
2404 | dc6b1c09 | Andre Przywara | if (smp == 0) { |
2405 | dc6b1c09 | Andre Przywara | smp = cores * threads * sockets; |
2406 | dc6b1c09 | Andre Przywara | } else {
|
2407 | dc6b1c09 | Andre Przywara | sockets = smp / (cores * threads); |
2408 | dc6b1c09 | Andre Przywara | } |
2409 | dc6b1c09 | Andre Przywara | } else {
|
2410 | dc6b1c09 | Andre Przywara | if (cores == 0) { |
2411 | dc6b1c09 | Andre Przywara | threads = threads > 0 ? threads : 1; |
2412 | dc6b1c09 | Andre Przywara | cores = smp / (sockets * threads); |
2413 | dc6b1c09 | Andre Przywara | } else {
|
2414 | dc6b1c09 | Andre Przywara | if (sockets == 0) { |
2415 | dc6b1c09 | Andre Przywara | sockets = smp / (cores * threads); |
2416 | dc6b1c09 | Andre Przywara | } else {
|
2417 | dc6b1c09 | Andre Przywara | threads = smp / (cores * sockets); |
2418 | dc6b1c09 | Andre Przywara | } |
2419 | dc6b1c09 | Andre Przywara | } |
2420 | dc6b1c09 | Andre Przywara | } |
2421 | dc6b1c09 | Andre Przywara | smp_cpus = smp; |
2422 | dc6b1c09 | Andre Przywara | smp_cores = cores > 0 ? cores : 1; |
2423 | dc6b1c09 | Andre Przywara | smp_threads = threads > 0 ? threads : 1; |
2424 | dc6b1c09 | Andre Przywara | if (max_cpus == 0) |
2425 | dc6b1c09 | Andre Przywara | max_cpus = smp_cpus; |
2426 | dc6b1c09 | Andre Przywara | } |
2427 | dc6b1c09 | Andre Przywara | |
2428 | 330d0414 | bellard | /***********************************************************/
|
2429 | a594cfbf | bellard | /* USB devices */
|
2430 | a594cfbf | bellard | |
2431 | bb5fc20f | aliguori | static void usb_msd_password_cb(void *opaque, int err) |
2432 | bb5fc20f | aliguori | { |
2433 | bb5fc20f | aliguori | USBDevice *dev = opaque; |
2434 | bb5fc20f | aliguori | |
2435 | bb5fc20f | aliguori | if (!err)
|
2436 | a5d2f727 | Gerd Hoffmann | usb_device_attach(dev); |
2437 | bb5fc20f | aliguori | else
|
2438 | 806b6024 | Gerd Hoffmann | dev->info->handle_destroy(dev); |
2439 | bb5fc20f | aliguori | } |
2440 | bb5fc20f | aliguori | |
2441 | a5d2f727 | Gerd Hoffmann | static struct { |
2442 | a5d2f727 | Gerd Hoffmann | const char *name; |
2443 | a5d2f727 | Gerd Hoffmann | const char *qdev; |
2444 | a5d2f727 | Gerd Hoffmann | } usbdevs[] = { |
2445 | a5d2f727 | Gerd Hoffmann | { |
2446 | a5d2f727 | Gerd Hoffmann | .name = "mouse",
|
2447 | a5d2f727 | Gerd Hoffmann | .qdev = "QEMU USB Mouse",
|
2448 | a5d2f727 | Gerd Hoffmann | },{ |
2449 | a5d2f727 | Gerd Hoffmann | .name = "tablet",
|
2450 | a5d2f727 | Gerd Hoffmann | .qdev = "QEMU USB Tablet",
|
2451 | a5d2f727 | Gerd Hoffmann | },{ |
2452 | a5d2f727 | Gerd Hoffmann | .name = "keyboard",
|
2453 | a5d2f727 | Gerd Hoffmann | .qdev = "QEMU USB Keyboard",
|
2454 | a5d2f727 | Gerd Hoffmann | },{ |
2455 | a5d2f727 | Gerd Hoffmann | .name = "wacom-tablet",
|
2456 | a5d2f727 | Gerd Hoffmann | .qdev = "QEMU PenPartner Tablet",
|
2457 | a5d2f727 | Gerd Hoffmann | } |
2458 | a5d2f727 | Gerd Hoffmann | }; |
2459 | a5d2f727 | Gerd Hoffmann | |
2460 | c0f4ce77 | aliguori | static int usb_device_add(const char *devname, int is_hotplug) |
2461 | a594cfbf | bellard | { |
2462 | a594cfbf | bellard | const char *p; |
2463 | a5d2f727 | Gerd Hoffmann | USBBus *bus = usb_bus_find(-1 /* any */); |
2464 | a5d2f727 | Gerd Hoffmann | USBDevice *dev = NULL;
|
2465 | a5d2f727 | Gerd Hoffmann | int i;
|
2466 | a594cfbf | bellard | |
2467 | a5d2f727 | Gerd Hoffmann | if (!usb_enabled)
|
2468 | a594cfbf | bellard | return -1; |
2469 | a594cfbf | bellard | |
2470 | a5d2f727 | Gerd Hoffmann | /* simple devices which don't need extra care */
|
2471 | a5d2f727 | Gerd Hoffmann | for (i = 0; i < ARRAY_SIZE(usbdevs); i++) { |
2472 | a5d2f727 | Gerd Hoffmann | if (strcmp(devname, usbdevs[i].name) != 0) |
2473 | a5d2f727 | Gerd Hoffmann | continue;
|
2474 | a5d2f727 | Gerd Hoffmann | dev = usb_create_simple(bus, usbdevs[i].qdev); |
2475 | a5d2f727 | Gerd Hoffmann | goto done;
|
2476 | a5d2f727 | Gerd Hoffmann | } |
2477 | a5d2f727 | Gerd Hoffmann | |
2478 | a5d2f727 | Gerd Hoffmann | /* the other ones */
|
2479 | a594cfbf | bellard | if (strstart(devname, "host:", &p)) { |
2480 | a594cfbf | bellard | dev = usb_host_device_open(p); |
2481 | 2e5d83bb | pbrook | } else if (strstart(devname, "disk:", &p)) { |
2482 | c0f4ce77 | aliguori | BlockDriverState *bs; |
2483 | c0f4ce77 | aliguori | |
2484 | bb5fc20f | aliguori | dev = usb_msd_init(p); |
2485 | c0f4ce77 | aliguori | if (!dev)
|
2486 | c0f4ce77 | aliguori | return -1; |
2487 | bb5fc20f | aliguori | bs = usb_msd_get_bdrv(dev); |
2488 | c0f4ce77 | aliguori | if (bdrv_key_required(bs)) {
|
2489 | c0f4ce77 | aliguori | autostart = 0;
|
2490 | bb5fc20f | aliguori | if (is_hotplug) {
|
2491 | 376253ec | aliguori | monitor_read_bdrv_key_start(cur_mon, bs, usb_msd_password_cb, |
2492 | 376253ec | aliguori | dev); |
2493 | bb5fc20f | aliguori | return 0; |
2494 | c0f4ce77 | aliguori | } |
2495 | c0f4ce77 | aliguori | } |
2496 | a7954218 | balrog | } else if (strstart(devname, "serial:", &p)) { |
2497 | a7954218 | balrog | dev = usb_serial_init(p); |
2498 | 2e4d9fb1 | aurel32 | #ifdef CONFIG_BRLAPI
|
2499 | 2e4d9fb1 | aurel32 | } else if (!strcmp(devname, "braille")) { |
2500 | 2e4d9fb1 | aurel32 | dev = usb_baum_init(); |
2501 | 2e4d9fb1 | aurel32 | #endif
|
2502 | 6c9f886c | balrog | } else if (strstart(devname, "net:", &p)) { |
2503 | 9ad97e65 | balrog | int nic = nb_nics;
|
2504 | 6c9f886c | balrog | |
2505 | 10ae5a7a | Jan Kiszka | if (net_client_init(NULL, "nic", p) < 0) |
2506 | 6c9f886c | balrog | return -1; |
2507 | 9ad97e65 | balrog | nd_table[nic].model = "usb";
|
2508 | 9ad97e65 | balrog | dev = usb_net_init(&nd_table[nic]); |
2509 | dc72ac14 | balrog | } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) { |
2510 | dc72ac14 | balrog | dev = usb_bt_init(devname[2] ? hci_init(p) :
|
2511 | dc72ac14 | balrog | bt_new_hci(qemu_find_bt_vlan(0)));
|
2512 | a594cfbf | bellard | } else {
|
2513 | a594cfbf | bellard | return -1; |
2514 | a594cfbf | bellard | } |
2515 | 0d92ed30 | pbrook | if (!dev)
|
2516 | 0d92ed30 | pbrook | return -1; |
2517 | 0d92ed30 | pbrook | |
2518 | a5d2f727 | Gerd Hoffmann | done:
|
2519 | a594cfbf | bellard | return 0; |
2520 | a594cfbf | bellard | } |
2521 | a594cfbf | bellard | |
2522 | 1f3870ab | aliguori | static int usb_device_del(const char *devname) |
2523 | 1f3870ab | aliguori | { |
2524 | 1f3870ab | aliguori | int bus_num, addr;
|
2525 | 1f3870ab | aliguori | const char *p; |
2526 | 1f3870ab | aliguori | |
2527 | 5d0c5750 | aliguori | if (strstart(devname, "host:", &p)) |
2528 | 5d0c5750 | aliguori | return usb_host_device_close(p);
|
2529 | 5d0c5750 | aliguori | |
2530 | a5d2f727 | Gerd Hoffmann | if (!usb_enabled)
|
2531 | 1f3870ab | aliguori | return -1; |
2532 | 1f3870ab | aliguori | |
2533 | 1f3870ab | aliguori | p = strchr(devname, '.');
|
2534 | 1f3870ab | aliguori | if (!p)
|
2535 | 1f3870ab | aliguori | return -1; |
2536 | 1f3870ab | aliguori | bus_num = strtoul(devname, NULL, 0); |
2537 | 1f3870ab | aliguori | addr = strtoul(p + 1, NULL, 0); |
2538 | 1f3870ab | aliguori | |
2539 | a5d2f727 | Gerd Hoffmann | return usb_device_delete_addr(bus_num, addr);
|
2540 | 1f3870ab | aliguori | } |
2541 | 1f3870ab | aliguori | |
2542 | bd3c948d | Gerd Hoffmann | static int usb_parse(const char *cmdline) |
2543 | bd3c948d | Gerd Hoffmann | { |
2544 | bd3c948d | Gerd Hoffmann | return usb_device_add(cmdline, 0); |
2545 | bd3c948d | Gerd Hoffmann | } |
2546 | bd3c948d | Gerd Hoffmann | |
2547 | d54908a5 | Luiz Capitulino | void do_usb_add(Monitor *mon, const QDict *qdict) |
2548 | a594cfbf | bellard | { |
2549 | d54908a5 | Luiz Capitulino | usb_device_add(qdict_get_str(qdict, "devname"), 1); |
2550 | a594cfbf | bellard | } |
2551 | a594cfbf | bellard | |
2552 | d54908a5 | Luiz Capitulino | void do_usb_del(Monitor *mon, const QDict *qdict) |
2553 | a594cfbf | bellard | { |
2554 | d54908a5 | Luiz Capitulino | usb_device_del(qdict_get_str(qdict, "devname"));
|
2555 | a594cfbf | bellard | } |
2556 | a594cfbf | bellard | |
2557 | f7cce898 | bellard | /***********************************************************/
|
2558 | 201a51fc | balrog | /* PCMCIA/Cardbus */
|
2559 | 201a51fc | balrog | |
2560 | 201a51fc | balrog | static struct pcmcia_socket_entry_s { |
2561 | bc24a225 | Paul Brook | PCMCIASocket *socket; |
2562 | 201a51fc | balrog | struct pcmcia_socket_entry_s *next;
|
2563 | 201a51fc | balrog | } *pcmcia_sockets = 0;
|
2564 | 201a51fc | balrog | |
2565 | bc24a225 | Paul Brook | void pcmcia_socket_register(PCMCIASocket *socket)
|
2566 | 201a51fc | balrog | { |
2567 | 201a51fc | balrog | struct pcmcia_socket_entry_s *entry;
|
2568 | 201a51fc | balrog | |
2569 | 201a51fc | balrog | entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); |
2570 | 201a51fc | balrog | entry->socket = socket; |
2571 | 201a51fc | balrog | entry->next = pcmcia_sockets; |
2572 | 201a51fc | balrog | pcmcia_sockets = entry; |
2573 | 201a51fc | balrog | } |
2574 | 201a51fc | balrog | |
2575 | bc24a225 | Paul Brook | void pcmcia_socket_unregister(PCMCIASocket *socket)
|
2576 | 201a51fc | balrog | { |
2577 | 201a51fc | balrog | struct pcmcia_socket_entry_s *entry, **ptr;
|
2578 | 201a51fc | balrog | |
2579 | 201a51fc | balrog | ptr = &pcmcia_sockets; |
2580 | 201a51fc | balrog | for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
|
2581 | 201a51fc | balrog | if (entry->socket == socket) {
|
2582 | 201a51fc | balrog | *ptr = entry->next; |
2583 | 201a51fc | balrog | qemu_free(entry); |
2584 | 201a51fc | balrog | } |
2585 | 201a51fc | balrog | } |
2586 | 201a51fc | balrog | |
2587 | 376253ec | aliguori | void pcmcia_info(Monitor *mon)
|
2588 | 201a51fc | balrog | { |
2589 | 201a51fc | balrog | struct pcmcia_socket_entry_s *iter;
|
2590 | 376253ec | aliguori | |
2591 | 201a51fc | balrog | if (!pcmcia_sockets)
|
2592 | 376253ec | aliguori | monitor_printf(mon, "No PCMCIA sockets\n");
|
2593 | 201a51fc | balrog | |
2594 | 201a51fc | balrog | for (iter = pcmcia_sockets; iter; iter = iter->next)
|
2595 | 376253ec | aliguori | monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
|
2596 | 376253ec | aliguori | iter->socket->attached ? iter->socket->card_string : |
2597 | 376253ec | aliguori | "Empty");
|
2598 | 201a51fc | balrog | } |
2599 | 201a51fc | balrog | |
2600 | 201a51fc | balrog | /***********************************************************/
|
2601 | 3023f332 | aliguori | /* register display */
|
2602 | 3023f332 | aliguori | |
2603 | 7b5d76da | aliguori | struct DisplayAllocator default_allocator = {
|
2604 | 7b5d76da | aliguori | defaultallocator_create_displaysurface, |
2605 | 7b5d76da | aliguori | defaultallocator_resize_displaysurface, |
2606 | 7b5d76da | aliguori | defaultallocator_free_displaysurface |
2607 | 7b5d76da | aliguori | }; |
2608 | 7b5d76da | aliguori | |
2609 | 3023f332 | aliguori | void register_displaystate(DisplayState *ds)
|
2610 | 3023f332 | aliguori | { |
2611 | 3023f332 | aliguori | DisplayState **s; |
2612 | 3023f332 | aliguori | s = &display_state; |
2613 | 3023f332 | aliguori | while (*s != NULL) |
2614 | 3023f332 | aliguori | s = &(*s)->next; |
2615 | 3023f332 | aliguori | ds->next = NULL;
|
2616 | 3023f332 | aliguori | *s = ds; |
2617 | 3023f332 | aliguori | } |
2618 | 3023f332 | aliguori | |
2619 | 3023f332 | aliguori | DisplayState *get_displaystate(void)
|
2620 | 3023f332 | aliguori | { |
2621 | 3023f332 | aliguori | return display_state;
|
2622 | 3023f332 | aliguori | } |
2623 | 3023f332 | aliguori | |
2624 | 7b5d76da | aliguori | DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da) |
2625 | 7b5d76da | aliguori | { |
2626 | 7b5d76da | aliguori | if(ds->allocator == &default_allocator) ds->allocator = da;
|
2627 | 7b5d76da | aliguori | return ds->allocator;
|
2628 | 7b5d76da | aliguori | } |
2629 | 7b5d76da | aliguori | |
2630 | 2ff89790 | ths | /* dumb display */
|
2631 | 2ff89790 | ths | |
2632 | 8f391ab4 | aliguori | static void dumb_display_init(void) |
2633 | 2ff89790 | ths | { |
2634 | 8f391ab4 | aliguori | DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
|
2635 | 7b5d76da | aliguori | ds->allocator = &default_allocator; |
2636 | 7b5d76da | aliguori | ds->surface = qemu_create_displaysurface(ds, 640, 480); |
2637 | 8f391ab4 | aliguori | register_displaystate(ds); |
2638 | 2ff89790 | ths | } |
2639 | 2ff89790 | ths | |
2640 | 2ff89790 | ths | /***********************************************************/
|
2641 | 8a7ddc38 | bellard | /* I/O handling */
|
2642 | 0824d6fc | bellard | |
2643 | c4b1fcc0 | bellard | typedef struct IOHandlerRecord { |
2644 | c4b1fcc0 | bellard | int fd;
|
2645 | 7c9d8e07 | bellard | IOCanRWHandler *fd_read_poll; |
2646 | 7c9d8e07 | bellard | IOHandler *fd_read; |
2647 | 7c9d8e07 | bellard | IOHandler *fd_write; |
2648 | cafffd40 | ths | int deleted;
|
2649 | c4b1fcc0 | bellard | void *opaque;
|
2650 | c4b1fcc0 | bellard | /* temporary data */
|
2651 | c4b1fcc0 | bellard | struct pollfd *ufd;
|
2652 | 8a7ddc38 | bellard | struct IOHandlerRecord *next;
|
2653 | c4b1fcc0 | bellard | } IOHandlerRecord; |
2654 | c4b1fcc0 | bellard | |
2655 | 8a7ddc38 | bellard | static IOHandlerRecord *first_io_handler;
|
2656 | c4b1fcc0 | bellard | |
2657 | 7c9d8e07 | bellard | /* XXX: fd_read_poll should be suppressed, but an API change is
|
2658 | 7c9d8e07 | bellard | necessary in the character devices to suppress fd_can_read(). */
|
2659 | 5fafdf24 | ths | int qemu_set_fd_handler2(int fd, |
2660 | 5fafdf24 | ths | IOCanRWHandler *fd_read_poll, |
2661 | 5fafdf24 | ths | IOHandler *fd_read, |
2662 | 5fafdf24 | ths | IOHandler *fd_write, |
2663 | 7c9d8e07 | bellard | void *opaque)
|
2664 | c4b1fcc0 | bellard | { |
2665 | 7c9d8e07 | bellard | IOHandlerRecord **pioh, *ioh; |
2666 | c4b1fcc0 | bellard | |
2667 | 7c9d8e07 | bellard | if (!fd_read && !fd_write) {
|
2668 | 7c9d8e07 | bellard | pioh = &first_io_handler; |
2669 | 7c9d8e07 | bellard | for(;;) {
|
2670 | 7c9d8e07 | bellard | ioh = *pioh; |
2671 | 7c9d8e07 | bellard | if (ioh == NULL) |
2672 | 7c9d8e07 | bellard | break;
|
2673 | 7c9d8e07 | bellard | if (ioh->fd == fd) {
|
2674 | cafffd40 | ths | ioh->deleted = 1;
|
2675 | 7c9d8e07 | bellard | break;
|
2676 | 7c9d8e07 | bellard | } |
2677 | 7c9d8e07 | bellard | pioh = &ioh->next; |
2678 | 7c9d8e07 | bellard | } |
2679 | 7c9d8e07 | bellard | } else {
|
2680 | 7c9d8e07 | bellard | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
2681 | 7c9d8e07 | bellard | if (ioh->fd == fd)
|
2682 | 7c9d8e07 | bellard | goto found;
|
2683 | 7c9d8e07 | bellard | } |
2684 | 7c9d8e07 | bellard | ioh = qemu_mallocz(sizeof(IOHandlerRecord));
|
2685 | 7c9d8e07 | bellard | ioh->next = first_io_handler; |
2686 | 7c9d8e07 | bellard | first_io_handler = ioh; |
2687 | 7c9d8e07 | bellard | found:
|
2688 | 7c9d8e07 | bellard | ioh->fd = fd; |
2689 | 7c9d8e07 | bellard | ioh->fd_read_poll = fd_read_poll; |
2690 | 7c9d8e07 | bellard | ioh->fd_read = fd_read; |
2691 | 7c9d8e07 | bellard | ioh->fd_write = fd_write; |
2692 | 7c9d8e07 | bellard | ioh->opaque = opaque; |
2693 | cafffd40 | ths | ioh->deleted = 0;
|
2694 | 7c9d8e07 | bellard | } |
2695 | c4b1fcc0 | bellard | return 0; |
2696 | c4b1fcc0 | bellard | } |
2697 | c4b1fcc0 | bellard | |
2698 | 5fafdf24 | ths | int qemu_set_fd_handler(int fd, |
2699 | 5fafdf24 | ths | IOHandler *fd_read, |
2700 | 5fafdf24 | ths | IOHandler *fd_write, |
2701 | 7c9d8e07 | bellard | void *opaque)
|
2702 | 8a7ddc38 | bellard | { |
2703 | 7c9d8e07 | bellard | return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
2704 | 8a7ddc38 | bellard | } |
2705 | 8a7ddc38 | bellard | |
2706 | 56f3a5d0 | aliguori | #ifdef _WIN32
|
2707 | 8a7ddc38 | bellard | /***********************************************************/
|
2708 | f331110f | bellard | /* Polling handling */
|
2709 | f331110f | bellard | |
2710 | f331110f | bellard | typedef struct PollingEntry { |
2711 | f331110f | bellard | PollingFunc *func; |
2712 | f331110f | bellard | void *opaque;
|
2713 | f331110f | bellard | struct PollingEntry *next;
|
2714 | f331110f | bellard | } PollingEntry; |
2715 | f331110f | bellard | |
2716 | f331110f | bellard | static PollingEntry *first_polling_entry;
|
2717 | f331110f | bellard | |
2718 | f331110f | bellard | int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
2719 | f331110f | bellard | { |
2720 | f331110f | bellard | PollingEntry **ppe, *pe; |
2721 | f331110f | bellard | pe = qemu_mallocz(sizeof(PollingEntry));
|
2722 | f331110f | bellard | pe->func = func; |
2723 | f331110f | bellard | pe->opaque = opaque; |
2724 | f331110f | bellard | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
2725 | f331110f | bellard | *ppe = pe; |
2726 | f331110f | bellard | return 0; |
2727 | f331110f | bellard | } |
2728 | f331110f | bellard | |
2729 | f331110f | bellard | void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
2730 | f331110f | bellard | { |
2731 | f331110f | bellard | PollingEntry **ppe, *pe; |
2732 | f331110f | bellard | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
2733 | f331110f | bellard | pe = *ppe; |
2734 | f331110f | bellard | if (pe->func == func && pe->opaque == opaque) {
|
2735 | f331110f | bellard | *ppe = pe->next; |
2736 | f331110f | bellard | qemu_free(pe); |
2737 | f331110f | bellard | break;
|
2738 | f331110f | bellard | } |
2739 | f331110f | bellard | } |
2740 | f331110f | bellard | } |
2741 | f331110f | bellard | |
2742 | a18e524a | bellard | /***********************************************************/
|
2743 | a18e524a | bellard | /* Wait objects support */
|
2744 | a18e524a | bellard | typedef struct WaitObjects { |
2745 | a18e524a | bellard | int num;
|
2746 | a18e524a | bellard | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
|
2747 | a18e524a | bellard | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
|
2748 | a18e524a | bellard | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
2749 | a18e524a | bellard | } WaitObjects; |
2750 | a18e524a | bellard | |
2751 | a18e524a | bellard | static WaitObjects wait_objects = {0}; |
2752 | 3b46e624 | ths | |
2753 | a18e524a | bellard | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
2754 | a18e524a | bellard | { |
2755 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
2756 | a18e524a | bellard | |
2757 | a18e524a | bellard | if (w->num >= MAXIMUM_WAIT_OBJECTS)
|
2758 | a18e524a | bellard | return -1; |
2759 | a18e524a | bellard | w->events[w->num] = handle; |
2760 | a18e524a | bellard | w->func[w->num] = func; |
2761 | a18e524a | bellard | w->opaque[w->num] = opaque; |
2762 | a18e524a | bellard | w->num++; |
2763 | a18e524a | bellard | return 0; |
2764 | a18e524a | bellard | } |
2765 | a18e524a | bellard | |
2766 | a18e524a | bellard | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
2767 | a18e524a | bellard | { |
2768 | a18e524a | bellard | int i, found;
|
2769 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
2770 | a18e524a | bellard | |
2771 | a18e524a | bellard | found = 0;
|
2772 | a18e524a | bellard | for (i = 0; i < w->num; i++) { |
2773 | a18e524a | bellard | if (w->events[i] == handle)
|
2774 | a18e524a | bellard | found = 1;
|
2775 | a18e524a | bellard | if (found) {
|
2776 | a18e524a | bellard | w->events[i] = w->events[i + 1];
|
2777 | a18e524a | bellard | w->func[i] = w->func[i + 1];
|
2778 | a18e524a | bellard | w->opaque[i] = w->opaque[i + 1];
|
2779 | 3b46e624 | ths | } |
2780 | a18e524a | bellard | } |
2781 | a18e524a | bellard | if (found)
|
2782 | a18e524a | bellard | w->num--; |
2783 | a18e524a | bellard | } |
2784 | a18e524a | bellard | #endif
|
2785 | a18e524a | bellard | |
2786 | 8a7ddc38 | bellard | /***********************************************************/
|
2787 | 8a7ddc38 | bellard | /* ram save/restore */
|
2788 | 8a7ddc38 | bellard | |
2789 | 94fb0909 | Juan Quintela | #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */ |
2790 | 475e4277 | aliguori | #define RAM_SAVE_FLAG_COMPRESS 0x02 |
2791 | 475e4277 | aliguori | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 |
2792 | 475e4277 | aliguori | #define RAM_SAVE_FLAG_PAGE 0x08 |
2793 | 475e4277 | aliguori | #define RAM_SAVE_FLAG_EOS 0x10 |
2794 | 475e4277 | aliguori | |
2795 | 475e4277 | aliguori | static int is_dup_page(uint8_t *page, uint8_t ch) |
2796 | 8a7ddc38 | bellard | { |
2797 | 475e4277 | aliguori | uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch; |
2798 | 475e4277 | aliguori | uint32_t *array = (uint32_t *)page; |
2799 | 475e4277 | aliguori | int i;
|
2800 | 3b46e624 | ths | |
2801 | 475e4277 | aliguori | for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) { |
2802 | 475e4277 | aliguori | if (array[i] != val)
|
2803 | 475e4277 | aliguori | return 0; |
2804 | 475e4277 | aliguori | } |
2805 | 475e4277 | aliguori | |
2806 | 475e4277 | aliguori | return 1; |
2807 | 475e4277 | aliguori | } |
2808 | 475e4277 | aliguori | |
2809 | 475e4277 | aliguori | static int ram_save_block(QEMUFile *f) |
2810 | 475e4277 | aliguori | { |
2811 | c227f099 | Anthony Liguori | static ram_addr_t current_addr = 0; |
2812 | c227f099 | Anthony Liguori | ram_addr_t saved_addr = current_addr; |
2813 | c227f099 | Anthony Liguori | ram_addr_t addr = 0;
|
2814 | 475e4277 | aliguori | int found = 0; |
2815 | 475e4277 | aliguori | |
2816 | 94a6b54f | pbrook | while (addr < last_ram_offset) {
|
2817 | 475e4277 | aliguori | if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
|
2818 | 5579c7f3 | pbrook | uint8_t *p; |
2819 | 475e4277 | aliguori | |
2820 | 475e4277 | aliguori | cpu_physical_memory_reset_dirty(current_addr, |
2821 | 475e4277 | aliguori | current_addr + TARGET_PAGE_SIZE, |
2822 | 475e4277 | aliguori | MIGRATION_DIRTY_FLAG); |
2823 | 475e4277 | aliguori | |
2824 | 5579c7f3 | pbrook | p = qemu_get_ram_ptr(current_addr); |
2825 | 475e4277 | aliguori | |
2826 | 5579c7f3 | pbrook | if (is_dup_page(p, *p)) {
|
2827 | 475e4277 | aliguori | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS); |
2828 | 5579c7f3 | pbrook | qemu_put_byte(f, *p); |
2829 | 475e4277 | aliguori | } else {
|
2830 | 475e4277 | aliguori | qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE); |
2831 | 5579c7f3 | pbrook | qemu_put_buffer(f, p, TARGET_PAGE_SIZE); |
2832 | c88676f8 | bellard | } |
2833 | 475e4277 | aliguori | |
2834 | 475e4277 | aliguori | found = 1;
|
2835 | 475e4277 | aliguori | break;
|
2836 | c88676f8 | bellard | } |
2837 | 475e4277 | aliguori | addr += TARGET_PAGE_SIZE; |
2838 | 94a6b54f | pbrook | current_addr = (saved_addr + addr) % last_ram_offset; |
2839 | 8a7ddc38 | bellard | } |
2840 | 475e4277 | aliguori | |
2841 | 475e4277 | aliguori | return found;
|
2842 | 8a7ddc38 | bellard | } |
2843 | 8a7ddc38 | bellard | |
2844 | 9f9e28cd | Glauber Costa | static uint64_t bytes_transferred = 0; |
2845 | 475e4277 | aliguori | |
2846 | c227f099 | Anthony Liguori | static ram_addr_t ram_save_remaining(void) |
2847 | 475e4277 | aliguori | { |
2848 | c227f099 | Anthony Liguori | ram_addr_t addr; |
2849 | c227f099 | Anthony Liguori | ram_addr_t count = 0;
|
2850 | 475e4277 | aliguori | |
2851 | 94a6b54f | pbrook | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
2852 | 475e4277 | aliguori | if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
|
2853 | 475e4277 | aliguori | count++; |
2854 | 475e4277 | aliguori | } |
2855 | 475e4277 | aliguori | |
2856 | 475e4277 | aliguori | return count;
|
2857 | 475e4277 | aliguori | } |
2858 | 475e4277 | aliguori | |
2859 | 9f9e28cd | Glauber Costa | uint64_t ram_bytes_remaining(void)
|
2860 | 9f9e28cd | Glauber Costa | { |
2861 | 9f9e28cd | Glauber Costa | return ram_save_remaining() * TARGET_PAGE_SIZE;
|
2862 | 9f9e28cd | Glauber Costa | } |
2863 | 9f9e28cd | Glauber Costa | |
2864 | 9f9e28cd | Glauber Costa | uint64_t ram_bytes_transferred(void)
|
2865 | 9f9e28cd | Glauber Costa | { |
2866 | 9f9e28cd | Glauber Costa | return bytes_transferred;
|
2867 | 9f9e28cd | Glauber Costa | } |
2868 | 9f9e28cd | Glauber Costa | |
2869 | 9f9e28cd | Glauber Costa | uint64_t ram_bytes_total(void)
|
2870 | 9f9e28cd | Glauber Costa | { |
2871 | 9f9e28cd | Glauber Costa | return last_ram_offset;
|
2872 | 9f9e28cd | Glauber Costa | } |
2873 | 9f9e28cd | Glauber Costa | |
2874 | 475e4277 | aliguori | static int ram_save_live(QEMUFile *f, int stage, void *opaque) |
2875 | 475e4277 | aliguori | { |
2876 | c227f099 | Anthony Liguori | ram_addr_t addr; |
2877 | a0a3fd60 | Glauber Costa | uint64_t bytes_transferred_last; |
2878 | a0a3fd60 | Glauber Costa | double bwidth = 0; |
2879 | a0a3fd60 | Glauber Costa | uint64_t expected_time = 0;
|
2880 | 475e4277 | aliguori | |
2881 | 9fa06385 | Jan Kiszka | if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) { |
2882 | b0a46a33 | Jan Kiszka | qemu_file_set_error(f); |
2883 | b0a46a33 | Jan Kiszka | return 0; |
2884 | b0a46a33 | Jan Kiszka | } |
2885 | b0a46a33 | Jan Kiszka | |
2886 | 475e4277 | aliguori | if (stage == 1) { |
2887 | 475e4277 | aliguori | /* Make sure all dirty bits are set */
|
2888 | 94a6b54f | pbrook | for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) { |
2889 | 475e4277 | aliguori | if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
|
2890 | 475e4277 | aliguori | cpu_physical_memory_set_dirty(addr); |
2891 | 475e4277 | aliguori | } |
2892 | b0a46a33 | Jan Kiszka | |
2893 | 475e4277 | aliguori | /* Enable dirty memory tracking */
|
2894 | 475e4277 | aliguori | cpu_physical_memory_set_dirty_tracking(1);
|
2895 | 475e4277 | aliguori | |
2896 | 94a6b54f | pbrook | qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE); |
2897 | 475e4277 | aliguori | } |
2898 | 475e4277 | aliguori | |
2899 | a0a3fd60 | Glauber Costa | bytes_transferred_last = bytes_transferred; |
2900 | a0a3fd60 | Glauber Costa | bwidth = get_clock(); |
2901 | a0a3fd60 | Glauber Costa | |
2902 | 475e4277 | aliguori | while (!qemu_file_rate_limit(f)) {
|
2903 | 475e4277 | aliguori | int ret;
|
2904 | 475e4277 | aliguori | |
2905 | 475e4277 | aliguori | ret = ram_save_block(f); |
2906 | 9f9e28cd | Glauber Costa | bytes_transferred += ret * TARGET_PAGE_SIZE; |
2907 | 475e4277 | aliguori | if (ret == 0) /* no more blocks */ |
2908 | 475e4277 | aliguori | break;
|
2909 | 475e4277 | aliguori | } |
2910 | 475e4277 | aliguori | |
2911 | a0a3fd60 | Glauber Costa | bwidth = get_clock() - bwidth; |
2912 | a0a3fd60 | Glauber Costa | bwidth = (bytes_transferred - bytes_transferred_last) / bwidth; |
2913 | a0a3fd60 | Glauber Costa | |
2914 | a0a3fd60 | Glauber Costa | /* if we haven't transferred anything this round, force expected_time to a
|
2915 | a0a3fd60 | Glauber Costa | * a very high value, but without crashing */
|
2916 | a0a3fd60 | Glauber Costa | if (bwidth == 0) |
2917 | a0a3fd60 | Glauber Costa | bwidth = 0.000001; |
2918 | a0a3fd60 | Glauber Costa | |
2919 | 475e4277 | aliguori | /* try transferring iterative blocks of memory */
|
2920 | 475e4277 | aliguori | |
2921 | 475e4277 | aliguori | if (stage == 3) { |
2922 | 475e4277 | aliguori | |
2923 | 475e4277 | aliguori | /* flush all remaining blocks regardless of rate limiting */
|
2924 | 9f9e28cd | Glauber Costa | while (ram_save_block(f) != 0) { |
2925 | 9f9e28cd | Glauber Costa | bytes_transferred += TARGET_PAGE_SIZE; |
2926 | 9f9e28cd | Glauber Costa | } |
2927 | 8215e914 | aliguori | cpu_physical_memory_set_dirty_tracking(0);
|
2928 | 475e4277 | aliguori | } |
2929 | 475e4277 | aliguori | |
2930 | 475e4277 | aliguori | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
2931 | 475e4277 | aliguori | |
2932 | a0a3fd60 | Glauber Costa | expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; |
2933 | a0a3fd60 | Glauber Costa | |
2934 | a0a3fd60 | Glauber Costa | return (stage == 2) && (expected_time <= migrate_max_downtime()); |
2935 | 475e4277 | aliguori | } |
2936 | 475e4277 | aliguori | |
2937 | 475e4277 | aliguori | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
2938 | 475e4277 | aliguori | { |
2939 | c227f099 | Anthony Liguori | ram_addr_t addr; |
2940 | 475e4277 | aliguori | int flags;
|
2941 | 475e4277 | aliguori | |
2942 | 475e4277 | aliguori | if (version_id != 3) |
2943 | 475e4277 | aliguori | return -EINVAL;
|
2944 | 475e4277 | aliguori | |
2945 | 475e4277 | aliguori | do {
|
2946 | 475e4277 | aliguori | addr = qemu_get_be64(f); |
2947 | 475e4277 | aliguori | |
2948 | 475e4277 | aliguori | flags = addr & ~TARGET_PAGE_MASK; |
2949 | 475e4277 | aliguori | addr &= TARGET_PAGE_MASK; |
2950 | 475e4277 | aliguori | |
2951 | 475e4277 | aliguori | if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
|
2952 | 94a6b54f | pbrook | if (addr != last_ram_offset)
|
2953 | 475e4277 | aliguori | return -EINVAL;
|
2954 | 475e4277 | aliguori | } |
2955 | 475e4277 | aliguori | |
2956 | 475e4277 | aliguori | if (flags & RAM_SAVE_FLAG_COMPRESS) {
|
2957 | 475e4277 | aliguori | uint8_t ch = qemu_get_byte(f); |
2958 | 779c6bef | Anthony Liguori | memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE); |
2959 | 779c6bef | Anthony Liguori | #ifndef _WIN32
|
2960 | 30868442 | Anthony Liguori | if (ch == 0 && |
2961 | 30868442 | Anthony Liguori | (!kvm_enabled() || kvm_has_sync_mmu())) { |
2962 | 30868442 | Anthony Liguori | madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED); |
2963 | 779c6bef | Anthony Liguori | } |
2964 | 30868442 | Anthony Liguori | #endif
|
2965 | 475e4277 | aliguori | } else if (flags & RAM_SAVE_FLAG_PAGE) |
2966 | 5579c7f3 | pbrook | qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE); |
2967 | 475e4277 | aliguori | } while (!(flags & RAM_SAVE_FLAG_EOS));
|
2968 | 475e4277 | aliguori | |
2969 | 8a7ddc38 | bellard | return 0; |
2970 | 8a7ddc38 | bellard | } |
2971 | 8a7ddc38 | bellard | |
2972 | 9e472e10 | aliguori | void qemu_service_io(void) |
2973 | 9e472e10 | aliguori | { |
2974 | d9f75a4e | aliguori | qemu_notify_event(); |
2975 | 9e472e10 | aliguori | } |
2976 | 9e472e10 | aliguori | |
2977 | 8a7ddc38 | bellard | /***********************************************************/
|
2978 | 83f64091 | bellard | /* bottom halves (can be seen as timers which expire ASAP) */
|
2979 | 83f64091 | bellard | |
2980 | 83f64091 | bellard | struct QEMUBH {
|
2981 | 83f64091 | bellard | QEMUBHFunc *cb; |
2982 | 83f64091 | bellard | void *opaque;
|
2983 | 83f64091 | bellard | int scheduled;
|
2984 | 1b435b10 | aliguori | int idle;
|
2985 | 1b435b10 | aliguori | int deleted;
|
2986 | 83f64091 | bellard | QEMUBH *next; |
2987 | 83f64091 | bellard | }; |
2988 | 83f64091 | bellard | |
2989 | 83f64091 | bellard | static QEMUBH *first_bh = NULL; |
2990 | 83f64091 | bellard | |
2991 | 83f64091 | bellard | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
|
2992 | 83f64091 | bellard | { |
2993 | 83f64091 | bellard | QEMUBH *bh; |
2994 | 83f64091 | bellard | bh = qemu_mallocz(sizeof(QEMUBH));
|
2995 | 83f64091 | bellard | bh->cb = cb; |
2996 | 83f64091 | bellard | bh->opaque = opaque; |
2997 | 1b435b10 | aliguori | bh->next = first_bh; |
2998 | 1b435b10 | aliguori | first_bh = bh; |
2999 | 83f64091 | bellard | return bh;
|
3000 | 83f64091 | bellard | } |
3001 | 83f64091 | bellard | |
3002 | 6eb5733a | bellard | int qemu_bh_poll(void) |
3003 | 83f64091 | bellard | { |
3004 | 1b435b10 | aliguori | QEMUBH *bh, **bhp; |
3005 | 6eb5733a | bellard | int ret;
|
3006 | 83f64091 | bellard | |
3007 | 6eb5733a | bellard | ret = 0;
|
3008 | 1b435b10 | aliguori | for (bh = first_bh; bh; bh = bh->next) {
|
3009 | 1b435b10 | aliguori | if (!bh->deleted && bh->scheduled) {
|
3010 | 1b435b10 | aliguori | bh->scheduled = 0;
|
3011 | 1b435b10 | aliguori | if (!bh->idle)
|
3012 | 1b435b10 | aliguori | ret = 1;
|
3013 | 1b435b10 | aliguori | bh->idle = 0;
|
3014 | 1b435b10 | aliguori | bh->cb(bh->opaque); |
3015 | 1b435b10 | aliguori | } |
3016 | 83f64091 | bellard | } |
3017 | 1b435b10 | aliguori | |
3018 | 1b435b10 | aliguori | /* remove deleted bhs */
|
3019 | 1b435b10 | aliguori | bhp = &first_bh; |
3020 | 1b435b10 | aliguori | while (*bhp) {
|
3021 | 1b435b10 | aliguori | bh = *bhp; |
3022 | 1b435b10 | aliguori | if (bh->deleted) {
|
3023 | 1b435b10 | aliguori | *bhp = bh->next; |
3024 | 1b435b10 | aliguori | qemu_free(bh); |
3025 | 1b435b10 | aliguori | } else
|
3026 | 1b435b10 | aliguori | bhp = &bh->next; |
3027 | 1b435b10 | aliguori | } |
3028 | 1b435b10 | aliguori | |
3029 | 6eb5733a | bellard | return ret;
|
3030 | 83f64091 | bellard | } |
3031 | 83f64091 | bellard | |
3032 | 1b435b10 | aliguori | void qemu_bh_schedule_idle(QEMUBH *bh)
|
3033 | 1b435b10 | aliguori | { |
3034 | 1b435b10 | aliguori | if (bh->scheduled)
|
3035 | 1b435b10 | aliguori | return;
|
3036 | 1b435b10 | aliguori | bh->scheduled = 1;
|
3037 | 1b435b10 | aliguori | bh->idle = 1;
|
3038 | 1b435b10 | aliguori | } |
3039 | 1b435b10 | aliguori | |
3040 | 83f64091 | bellard | void qemu_bh_schedule(QEMUBH *bh)
|
3041 | 83f64091 | bellard | { |
3042 | 83f64091 | bellard | if (bh->scheduled)
|
3043 | 83f64091 | bellard | return;
|
3044 | 83f64091 | bellard | bh->scheduled = 1;
|
3045 | 1b435b10 | aliguori | bh->idle = 0;
|
3046 | 83f64091 | bellard | /* stop the currently executing CPU to execute the BH ASAP */
|
3047 | d9f75a4e | aliguori | qemu_notify_event(); |
3048 | 83f64091 | bellard | } |
3049 | 83f64091 | bellard | |
3050 | 83f64091 | bellard | void qemu_bh_cancel(QEMUBH *bh)
|
3051 | 83f64091 | bellard | { |
3052 | 1b435b10 | aliguori | bh->scheduled = 0;
|
3053 | 83f64091 | bellard | } |
3054 | 83f64091 | bellard | |
3055 | 83f64091 | bellard | void qemu_bh_delete(QEMUBH *bh)
|
3056 | 83f64091 | bellard | { |
3057 | 1b435b10 | aliguori | bh->scheduled = 0;
|
3058 | 1b435b10 | aliguori | bh->deleted = 1;
|
3059 | 83f64091 | bellard | } |
3060 | 83f64091 | bellard | |
3061 | 56f3a5d0 | aliguori | static void qemu_bh_update_timeout(int *timeout) |
3062 | 56f3a5d0 | aliguori | { |
3063 | 56f3a5d0 | aliguori | QEMUBH *bh; |
3064 | 56f3a5d0 | aliguori | |
3065 | 56f3a5d0 | aliguori | for (bh = first_bh; bh; bh = bh->next) {
|
3066 | 56f3a5d0 | aliguori | if (!bh->deleted && bh->scheduled) {
|
3067 | 56f3a5d0 | aliguori | if (bh->idle) {
|
3068 | 56f3a5d0 | aliguori | /* idle bottom halves will be polled at least
|
3069 | 56f3a5d0 | aliguori | * every 10ms */
|
3070 | 56f3a5d0 | aliguori | *timeout = MIN(10, *timeout);
|
3071 | 56f3a5d0 | aliguori | } else {
|
3072 | 56f3a5d0 | aliguori | /* non-idle bottom halves will be executed
|
3073 | 56f3a5d0 | aliguori | * immediately */
|
3074 | 56f3a5d0 | aliguori | *timeout = 0;
|
3075 | 56f3a5d0 | aliguori | break;
|
3076 | 56f3a5d0 | aliguori | } |
3077 | 56f3a5d0 | aliguori | } |
3078 | 56f3a5d0 | aliguori | } |
3079 | 56f3a5d0 | aliguori | } |
3080 | 56f3a5d0 | aliguori | |
3081 | 83f64091 | bellard | /***********************************************************/
|
3082 | cc1daa40 | bellard | /* machine registration */
|
3083 | cc1daa40 | bellard | |
3084 | bdaf78e0 | blueswir1 | static QEMUMachine *first_machine = NULL; |
3085 | 6f338c34 | aliguori | QEMUMachine *current_machine = NULL;
|
3086 | cc1daa40 | bellard | |
3087 | cc1daa40 | bellard | int qemu_register_machine(QEMUMachine *m)
|
3088 | cc1daa40 | bellard | { |
3089 | cc1daa40 | bellard | QEMUMachine **pm; |
3090 | cc1daa40 | bellard | pm = &first_machine; |
3091 | cc1daa40 | bellard | while (*pm != NULL) |
3092 | cc1daa40 | bellard | pm = &(*pm)->next; |
3093 | cc1daa40 | bellard | m->next = NULL;
|
3094 | cc1daa40 | bellard | *pm = m; |
3095 | cc1daa40 | bellard | return 0; |
3096 | cc1daa40 | bellard | } |
3097 | cc1daa40 | bellard | |
3098 | 9596ebb7 | pbrook | static QEMUMachine *find_machine(const char *name) |
3099 | cc1daa40 | bellard | { |
3100 | cc1daa40 | bellard | QEMUMachine *m; |
3101 | cc1daa40 | bellard | |
3102 | cc1daa40 | bellard | for(m = first_machine; m != NULL; m = m->next) { |
3103 | cc1daa40 | bellard | if (!strcmp(m->name, name))
|
3104 | cc1daa40 | bellard | return m;
|
3105 | 3f6599e6 | Mark McLoughlin | if (m->alias && !strcmp(m->alias, name))
|
3106 | 3f6599e6 | Mark McLoughlin | return m;
|
3107 | cc1daa40 | bellard | } |
3108 | cc1daa40 | bellard | return NULL; |
3109 | cc1daa40 | bellard | } |
3110 | cc1daa40 | bellard | |
3111 | 0c257437 | Anthony Liguori | static QEMUMachine *find_default_machine(void) |
3112 | 0c257437 | Anthony Liguori | { |
3113 | 0c257437 | Anthony Liguori | QEMUMachine *m; |
3114 | 0c257437 | Anthony Liguori | |
3115 | 0c257437 | Anthony Liguori | for(m = first_machine; m != NULL; m = m->next) { |
3116 | 0c257437 | Anthony Liguori | if (m->is_default) {
|
3117 | 0c257437 | Anthony Liguori | return m;
|
3118 | 0c257437 | Anthony Liguori | } |
3119 | 0c257437 | Anthony Liguori | } |
3120 | 0c257437 | Anthony Liguori | return NULL; |
3121 | 0c257437 | Anthony Liguori | } |
3122 | 0c257437 | Anthony Liguori | |
3123 | cc1daa40 | bellard | /***********************************************************/
|
3124 | 8a7ddc38 | bellard | /* main execution loop */
|
3125 | 8a7ddc38 | bellard | |
3126 | 9596ebb7 | pbrook | static void gui_update(void *opaque) |
3127 | 8a7ddc38 | bellard | { |
3128 | 7d957bd8 | aliguori | uint64_t interval = GUI_REFRESH_INTERVAL; |
3129 | 740733bb | ths | DisplayState *ds = opaque; |
3130 | 7d957bd8 | aliguori | DisplayChangeListener *dcl = ds->listeners; |
3131 | 7d957bd8 | aliguori | |
3132 | 7d957bd8 | aliguori | dpy_refresh(ds); |
3133 | 7d957bd8 | aliguori | |
3134 | 7d957bd8 | aliguori | while (dcl != NULL) { |
3135 | 7d957bd8 | aliguori | if (dcl->gui_timer_interval &&
|
3136 | 7d957bd8 | aliguori | dcl->gui_timer_interval < interval) |
3137 | 7d957bd8 | aliguori | interval = dcl->gui_timer_interval; |
3138 | 7d957bd8 | aliguori | dcl = dcl->next; |
3139 | 7d957bd8 | aliguori | } |
3140 | 7d957bd8 | aliguori | qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock)); |
3141 | 8a7ddc38 | bellard | } |
3142 | 8a7ddc38 | bellard | |
3143 | 9043b62d | blueswir1 | static void nographic_update(void *opaque) |
3144 | 9043b62d | blueswir1 | { |
3145 | 9043b62d | blueswir1 | uint64_t interval = GUI_REFRESH_INTERVAL; |
3146 | 9043b62d | blueswir1 | |
3147 | 9043b62d | blueswir1 | qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock)); |
3148 | 9043b62d | blueswir1 | } |
3149 | 9043b62d | blueswir1 | |
3150 | 0bd48850 | bellard | struct vm_change_state_entry {
|
3151 | 0bd48850 | bellard | VMChangeStateHandler *cb; |
3152 | 0bd48850 | bellard | void *opaque;
|
3153 | 72cf2d4f | Blue Swirl | QLIST_ENTRY (vm_change_state_entry) entries; |
3154 | 0bd48850 | bellard | }; |
3155 | 0bd48850 | bellard | |
3156 | 72cf2d4f | Blue Swirl | static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
|
3157 | 0bd48850 | bellard | |
3158 | 0bd48850 | bellard | VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
3159 | 0bd48850 | bellard | void *opaque)
|
3160 | 0bd48850 | bellard | { |
3161 | 0bd48850 | bellard | VMChangeStateEntry *e; |
3162 | 0bd48850 | bellard | |
3163 | 0bd48850 | bellard | e = qemu_mallocz(sizeof (*e));
|
3164 | 0bd48850 | bellard | |
3165 | 0bd48850 | bellard | e->cb = cb; |
3166 | 0bd48850 | bellard | e->opaque = opaque; |
3167 | 72cf2d4f | Blue Swirl | QLIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
3168 | 0bd48850 | bellard | return e;
|
3169 | 0bd48850 | bellard | } |
3170 | 0bd48850 | bellard | |
3171 | 0bd48850 | bellard | void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
|
3172 | 0bd48850 | bellard | { |
3173 | 72cf2d4f | Blue Swirl | QLIST_REMOVE (e, entries); |
3174 | 0bd48850 | bellard | qemu_free (e); |
3175 | 0bd48850 | bellard | } |
3176 | 0bd48850 | bellard | |
3177 | 9781e040 | aliguori | static void vm_state_notify(int running, int reason) |
3178 | 0bd48850 | bellard | { |
3179 | 0bd48850 | bellard | VMChangeStateEntry *e; |
3180 | 0bd48850 | bellard | |
3181 | 0bd48850 | bellard | for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
|
3182 | 9781e040 | aliguori | e->cb(e->opaque, running, reason); |
3183 | 0bd48850 | bellard | } |
3184 | 0bd48850 | bellard | } |
3185 | 0bd48850 | bellard | |
3186 | d6dc3d42 | aliguori | static void resume_all_vcpus(void); |
3187 | d6dc3d42 | aliguori | static void pause_all_vcpus(void); |
3188 | d6dc3d42 | aliguori | |
3189 | 8a7ddc38 | bellard | void vm_start(void) |
3190 | 8a7ddc38 | bellard | { |
3191 | 8a7ddc38 | bellard | if (!vm_running) {
|
3192 | 8a7ddc38 | bellard | cpu_enable_ticks(); |
3193 | 8a7ddc38 | bellard | vm_running = 1;
|
3194 | 9781e040 | aliguori | vm_state_notify(1, 0); |
3195 | efe75411 | ths | qemu_rearm_alarm_timer(alarm_timer); |
3196 | d6dc3d42 | aliguori | resume_all_vcpus(); |
3197 | 8a7ddc38 | bellard | } |
3198 | 8a7ddc38 | bellard | } |
3199 | 8a7ddc38 | bellard | |
3200 | bb0c6722 | bellard | /* reset/shutdown handler */
|
3201 | bb0c6722 | bellard | |
3202 | bb0c6722 | bellard | typedef struct QEMUResetEntry { |
3203 | 72cf2d4f | Blue Swirl | QTAILQ_ENTRY(QEMUResetEntry) entry; |
3204 | bb0c6722 | bellard | QEMUResetHandler *func; |
3205 | bb0c6722 | bellard | void *opaque;
|
3206 | bb0c6722 | bellard | } QEMUResetEntry; |
3207 | bb0c6722 | bellard | |
3208 | 72cf2d4f | Blue Swirl | static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
|
3209 | 72cf2d4f | Blue Swirl | QTAILQ_HEAD_INITIALIZER(reset_handlers); |
3210 | bb0c6722 | bellard | static int reset_requested; |
3211 | bb0c6722 | bellard | static int shutdown_requested; |
3212 | 3475187d | bellard | static int powerdown_requested; |
3213 | e568902a | aliguori | static int debug_requested; |
3214 | 6e29f5da | aliguori | static int vmstop_requested; |
3215 | bb0c6722 | bellard | |
3216 | cf7a2fe2 | aurel32 | int qemu_shutdown_requested(void) |
3217 | cf7a2fe2 | aurel32 | { |
3218 | cf7a2fe2 | aurel32 | int r = shutdown_requested;
|
3219 | cf7a2fe2 | aurel32 | shutdown_requested = 0;
|
3220 | cf7a2fe2 | aurel32 | return r;
|
3221 | cf7a2fe2 | aurel32 | } |
3222 | cf7a2fe2 | aurel32 | |
3223 | cf7a2fe2 | aurel32 | int qemu_reset_requested(void) |
3224 | cf7a2fe2 | aurel32 | { |
3225 | cf7a2fe2 | aurel32 | int r = reset_requested;
|
3226 | cf7a2fe2 | aurel32 | reset_requested = 0;
|
3227 | cf7a2fe2 | aurel32 | return r;
|
3228 | cf7a2fe2 | aurel32 | } |
3229 | cf7a2fe2 | aurel32 | |
3230 | cf7a2fe2 | aurel32 | int qemu_powerdown_requested(void) |
3231 | cf7a2fe2 | aurel32 | { |
3232 | cf7a2fe2 | aurel32 | int r = powerdown_requested;
|
3233 | cf7a2fe2 | aurel32 | powerdown_requested = 0;
|
3234 | cf7a2fe2 | aurel32 | return r;
|
3235 | cf7a2fe2 | aurel32 | } |
3236 | cf7a2fe2 | aurel32 | |
3237 | e568902a | aliguori | static int qemu_debug_requested(void) |
3238 | e568902a | aliguori | { |
3239 | e568902a | aliguori | int r = debug_requested;
|
3240 | e568902a | aliguori | debug_requested = 0;
|
3241 | e568902a | aliguori | return r;
|
3242 | e568902a | aliguori | } |
3243 | e568902a | aliguori | |
3244 | 6e29f5da | aliguori | static int qemu_vmstop_requested(void) |
3245 | 6e29f5da | aliguori | { |
3246 | 6e29f5da | aliguori | int r = vmstop_requested;
|
3247 | 6e29f5da | aliguori | vmstop_requested = 0;
|
3248 | 6e29f5da | aliguori | return r;
|
3249 | 6e29f5da | aliguori | } |
3250 | 6e29f5da | aliguori | |
3251 | 6e29f5da | aliguori | static void do_vm_stop(int reason) |
3252 | 6e29f5da | aliguori | { |
3253 | 6e29f5da | aliguori | if (vm_running) {
|
3254 | 6e29f5da | aliguori | cpu_disable_ticks(); |
3255 | 6e29f5da | aliguori | vm_running = 0;
|
3256 | d6dc3d42 | aliguori | pause_all_vcpus(); |
3257 | 6e29f5da | aliguori | vm_state_notify(0, reason);
|
3258 | 6e29f5da | aliguori | } |
3259 | 6e29f5da | aliguori | } |
3260 | 6e29f5da | aliguori | |
3261 | a08d4367 | Jan Kiszka | void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
3262 | bb0c6722 | bellard | { |
3263 | 55ddfe8e | Jan Kiszka | QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
|
3264 | bb0c6722 | bellard | |
3265 | bb0c6722 | bellard | re->func = func; |
3266 | bb0c6722 | bellard | re->opaque = opaque; |
3267 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_TAIL(&reset_handlers, re, entry); |
3268 | bb0c6722 | bellard | } |
3269 | bb0c6722 | bellard | |
3270 | dda9b29f | Jan Kiszka | void qemu_unregister_reset(QEMUResetHandler *func, void *opaque) |
3271 | bb0c6722 | bellard | { |
3272 | bb0c6722 | bellard | QEMUResetEntry *re; |
3273 | bb0c6722 | bellard | |
3274 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(re, &reset_handlers, entry) { |
3275 | dda9b29f | Jan Kiszka | if (re->func == func && re->opaque == opaque) {
|
3276 | 72cf2d4f | Blue Swirl | QTAILQ_REMOVE(&reset_handlers, re, entry); |
3277 | dda9b29f | Jan Kiszka | qemu_free(re); |
3278 | dda9b29f | Jan Kiszka | return;
|
3279 | dda9b29f | Jan Kiszka | } |
3280 | dda9b29f | Jan Kiszka | } |
3281 | dda9b29f | Jan Kiszka | } |
3282 | dda9b29f | Jan Kiszka | |
3283 | dda9b29f | Jan Kiszka | void qemu_system_reset(void) |
3284 | dda9b29f | Jan Kiszka | { |
3285 | dda9b29f | Jan Kiszka | QEMUResetEntry *re, *nre; |
3286 | dda9b29f | Jan Kiszka | |
3287 | dda9b29f | Jan Kiszka | /* reset all devices */
|
3288 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) { |
3289 | bb0c6722 | bellard | re->func(re->opaque); |
3290 | bb0c6722 | bellard | } |
3291 | bb0c6722 | bellard | } |
3292 | bb0c6722 | bellard | |
3293 | bb0c6722 | bellard | void qemu_system_reset_request(void) |
3294 | bb0c6722 | bellard | { |
3295 | d1beab82 | bellard | if (no_reboot) {
|
3296 | d1beab82 | bellard | shutdown_requested = 1;
|
3297 | d1beab82 | bellard | } else {
|
3298 | d1beab82 | bellard | reset_requested = 1;
|
3299 | d1beab82 | bellard | } |
3300 | d9f75a4e | aliguori | qemu_notify_event(); |
3301 | bb0c6722 | bellard | } |
3302 | bb0c6722 | bellard | |
3303 | bb0c6722 | bellard | void qemu_system_shutdown_request(void) |
3304 | bb0c6722 | bellard | { |
3305 | bb0c6722 | bellard | shutdown_requested = 1;
|
3306 | d9f75a4e | aliguori | qemu_notify_event(); |
3307 | bb0c6722 | bellard | } |
3308 | bb0c6722 | bellard | |
3309 | 3475187d | bellard | void qemu_system_powerdown_request(void) |
3310 | 3475187d | bellard | { |
3311 | 3475187d | bellard | powerdown_requested = 1;
|
3312 | d9f75a4e | aliguori | qemu_notify_event(); |
3313 | d9f75a4e | aliguori | } |
3314 | d9f75a4e | aliguori | |
3315 | d6dc3d42 | aliguori | #ifdef CONFIG_IOTHREAD
|
3316 | d6dc3d42 | aliguori | static void qemu_system_vmstop_request(int reason) |
3317 | d9f75a4e | aliguori | { |
3318 | d6dc3d42 | aliguori | vmstop_requested = reason; |
3319 | d6dc3d42 | aliguori | qemu_notify_event(); |
3320 | bb0c6722 | bellard | } |
3321 | d6dc3d42 | aliguori | #endif
|
3322 | bb0c6722 | bellard | |
3323 | 50317c7f | aliguori | #ifndef _WIN32
|
3324 | 50317c7f | aliguori | static int io_thread_fd = -1; |
3325 | 50317c7f | aliguori | |
3326 | 50317c7f | aliguori | static void qemu_event_increment(void) |
3327 | 3fcf7b6b | aliguori | { |
3328 | 50317c7f | aliguori | static const char byte = 0; |
3329 | 50317c7f | aliguori | |
3330 | 50317c7f | aliguori | if (io_thread_fd == -1) |
3331 | 50317c7f | aliguori | return;
|
3332 | 50317c7f | aliguori | |
3333 | 50317c7f | aliguori | write(io_thread_fd, &byte, sizeof(byte));
|
3334 | 50317c7f | aliguori | } |
3335 | 50317c7f | aliguori | |
3336 | 50317c7f | aliguori | static void qemu_event_read(void *opaque) |
3337 | 50317c7f | aliguori | { |
3338 | 50317c7f | aliguori | int fd = (unsigned long)opaque; |
3339 | 50317c7f | aliguori | ssize_t len; |
3340 | 50317c7f | aliguori | |
3341 | 50317c7f | aliguori | /* Drain the notify pipe */
|
3342 | 50317c7f | aliguori | do {
|
3343 | 50317c7f | aliguori | char buffer[512]; |
3344 | 50317c7f | aliguori | len = read(fd, buffer, sizeof(buffer));
|
3345 | 50317c7f | aliguori | } while ((len == -1 && errno == EINTR) || len > 0); |
3346 | 50317c7f | aliguori | } |
3347 | 50317c7f | aliguori | |
3348 | 50317c7f | aliguori | static int qemu_event_init(void) |
3349 | 50317c7f | aliguori | { |
3350 | 50317c7f | aliguori | int err;
|
3351 | 50317c7f | aliguori | int fds[2]; |
3352 | 50317c7f | aliguori | |
3353 | 50317c7f | aliguori | err = pipe(fds); |
3354 | 50317c7f | aliguori | if (err == -1) |
3355 | 50317c7f | aliguori | return -errno;
|
3356 | 50317c7f | aliguori | |
3357 | 50317c7f | aliguori | err = fcntl_setfl(fds[0], O_NONBLOCK);
|
3358 | 50317c7f | aliguori | if (err < 0) |
3359 | 50317c7f | aliguori | goto fail;
|
3360 | 50317c7f | aliguori | |
3361 | 50317c7f | aliguori | err = fcntl_setfl(fds[1], O_NONBLOCK);
|
3362 | 50317c7f | aliguori | if (err < 0) |
3363 | 50317c7f | aliguori | goto fail;
|
3364 | 50317c7f | aliguori | |
3365 | 50317c7f | aliguori | qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL, |
3366 | 50317c7f | aliguori | (void *)(unsigned long)fds[0]); |
3367 | 50317c7f | aliguori | |
3368 | 50317c7f | aliguori | io_thread_fd = fds[1];
|
3369 | a7e21219 | Jan Kiszka | return 0; |
3370 | a7e21219 | Jan Kiszka | |
3371 | 50317c7f | aliguori | fail:
|
3372 | 50317c7f | aliguori | close(fds[0]);
|
3373 | 50317c7f | aliguori | close(fds[1]);
|
3374 | 50317c7f | aliguori | return err;
|
3375 | 50317c7f | aliguori | } |
3376 | 50317c7f | aliguori | #else
|
3377 | 50317c7f | aliguori | HANDLE qemu_event_handle; |
3378 | 50317c7f | aliguori | |
3379 | 50317c7f | aliguori | static void dummy_event_handler(void *opaque) |
3380 | 50317c7f | aliguori | { |
3381 | 50317c7f | aliguori | } |
3382 | 50317c7f | aliguori | |
3383 | 50317c7f | aliguori | static int qemu_event_init(void) |
3384 | 50317c7f | aliguori | { |
3385 | 50317c7f | aliguori | qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL); |
3386 | 50317c7f | aliguori | if (!qemu_event_handle) {
|
3387 | 20889d4e | Blue Swirl | fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
|
3388 | 50317c7f | aliguori | return -1; |
3389 | 50317c7f | aliguori | } |
3390 | 50317c7f | aliguori | qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
|
3391 | 3fcf7b6b | aliguori | return 0; |
3392 | 3fcf7b6b | aliguori | } |
3393 | 3fcf7b6b | aliguori | |
3394 | 50317c7f | aliguori | static void qemu_event_increment(void) |
3395 | 50317c7f | aliguori | { |
3396 | de1c90cf | malc | if (!SetEvent(qemu_event_handle)) {
|
3397 | 20889d4e | Blue Swirl | fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
|
3398 | de1c90cf | malc | GetLastError()); |
3399 | de1c90cf | malc | exit (1);
|
3400 | de1c90cf | malc | } |
3401 | 50317c7f | aliguori | } |
3402 | 50317c7f | aliguori | #endif
|
3403 | 50317c7f | aliguori | |
3404 | d6dc3d42 | aliguori | static int cpu_can_run(CPUState *env) |
3405 | d6dc3d42 | aliguori | { |
3406 | d6dc3d42 | aliguori | if (env->stop)
|
3407 | d6dc3d42 | aliguori | return 0; |
3408 | d6dc3d42 | aliguori | if (env->stopped)
|
3409 | d6dc3d42 | aliguori | return 0; |
3410 | d6dc3d42 | aliguori | return 1; |
3411 | d6dc3d42 | aliguori | } |
3412 | d6dc3d42 | aliguori | |
3413 | d6dc3d42 | aliguori | #ifndef CONFIG_IOTHREAD
|
3414 | 50317c7f | aliguori | static int qemu_init_main_loop(void) |
3415 | 50317c7f | aliguori | { |
3416 | 50317c7f | aliguori | return qemu_event_init();
|
3417 | 50317c7f | aliguori | } |
3418 | 50317c7f | aliguori | |
3419 | 0bf46a40 | aliguori | void qemu_init_vcpu(void *_env) |
3420 | 0bf46a40 | aliguori | { |
3421 | 0bf46a40 | aliguori | CPUState *env = _env; |
3422 | 0bf46a40 | aliguori | |
3423 | 0bf46a40 | aliguori | if (kvm_enabled())
|
3424 | 0bf46a40 | aliguori | kvm_init_vcpu(env); |
3425 | dc6b1c09 | Andre Przywara | env->nr_cores = smp_cores; |
3426 | dc6b1c09 | Andre Przywara | env->nr_threads = smp_threads; |
3427 | 0bf46a40 | aliguori | return;
|
3428 | 0bf46a40 | aliguori | } |
3429 | 0bf46a40 | aliguori | |
3430 | 8edac960 | aliguori | int qemu_cpu_self(void *env) |
3431 | 8edac960 | aliguori | { |
3432 | 8edac960 | aliguori | return 1; |
3433 | 8edac960 | aliguori | } |
3434 | 8edac960 | aliguori | |
3435 | d6dc3d42 | aliguori | static void resume_all_vcpus(void) |
3436 | d6dc3d42 | aliguori | { |
3437 | d6dc3d42 | aliguori | } |
3438 | d6dc3d42 | aliguori | |
3439 | d6dc3d42 | aliguori | static void pause_all_vcpus(void) |
3440 | d6dc3d42 | aliguori | { |
3441 | d6dc3d42 | aliguori | } |
3442 | d6dc3d42 | aliguori | |
3443 | 8edac960 | aliguori | void qemu_cpu_kick(void *env) |
3444 | 8edac960 | aliguori | { |
3445 | 8edac960 | aliguori | return;
|
3446 | 8edac960 | aliguori | } |
3447 | 8edac960 | aliguori | |
3448 | d6dc3d42 | aliguori | void qemu_notify_event(void) |
3449 | d6dc3d42 | aliguori | { |
3450 | d6dc3d42 | aliguori | CPUState *env = cpu_single_env; |
3451 | d6dc3d42 | aliguori | |
3452 | d6dc3d42 | aliguori | if (env) {
|
3453 | d6dc3d42 | aliguori | cpu_exit(env); |
3454 | 4a1418e0 | Anthony Liguori | } |
3455 | d6dc3d42 | aliguori | } |
3456 | d6dc3d42 | aliguori | |
3457 | 4870852c | aliguori | #define qemu_mutex_lock_iothread() do { } while (0) |
3458 | 4870852c | aliguori | #define qemu_mutex_unlock_iothread() do { } while (0) |
3459 | 4870852c | aliguori | |
3460 | 6e29f5da | aliguori | void vm_stop(int reason) |
3461 | 6e29f5da | aliguori | { |
3462 | 6e29f5da | aliguori | do_vm_stop(reason); |
3463 | 6e29f5da | aliguori | } |
3464 | 6e29f5da | aliguori | |
3465 | d6dc3d42 | aliguori | #else /* CONFIG_IOTHREAD */ |
3466 | d6dc3d42 | aliguori | |
3467 | d6dc3d42 | aliguori | #include "qemu-thread.h" |
3468 | d6dc3d42 | aliguori | |
3469 | d6dc3d42 | aliguori | QemuMutex qemu_global_mutex; |
3470 | d6dc3d42 | aliguori | static QemuMutex qemu_fair_mutex;
|
3471 | d6dc3d42 | aliguori | |
3472 | d6dc3d42 | aliguori | static QemuThread io_thread;
|
3473 | d6dc3d42 | aliguori | |
3474 | d6dc3d42 | aliguori | static QemuThread *tcg_cpu_thread;
|
3475 | d6dc3d42 | aliguori | static QemuCond *tcg_halt_cond;
|
3476 | d6dc3d42 | aliguori | |
3477 | d6dc3d42 | aliguori | static int qemu_system_ready; |
3478 | d6dc3d42 | aliguori | /* cpu creation */
|
3479 | d6dc3d42 | aliguori | static QemuCond qemu_cpu_cond;
|
3480 | d6dc3d42 | aliguori | /* system init */
|
3481 | d6dc3d42 | aliguori | static QemuCond qemu_system_cond;
|
3482 | d6dc3d42 | aliguori | static QemuCond qemu_pause_cond;
|
3483 | d6dc3d42 | aliguori | |
3484 | d6dc3d42 | aliguori | static void block_io_signals(void); |
3485 | d6dc3d42 | aliguori | static void unblock_io_signals(void); |
3486 | d6dc3d42 | aliguori | static int tcg_has_work(void); |
3487 | d6dc3d42 | aliguori | |
3488 | d6dc3d42 | aliguori | static int qemu_init_main_loop(void) |
3489 | d6dc3d42 | aliguori | { |
3490 | d6dc3d42 | aliguori | int ret;
|
3491 | d6dc3d42 | aliguori | |
3492 | d6dc3d42 | aliguori | ret = qemu_event_init(); |
3493 | d6dc3d42 | aliguori | if (ret)
|
3494 | d6dc3d42 | aliguori | return ret;
|
3495 | d6dc3d42 | aliguori | |
3496 | d6dc3d42 | aliguori | qemu_cond_init(&qemu_pause_cond); |
3497 | d6dc3d42 | aliguori | qemu_mutex_init(&qemu_fair_mutex); |
3498 | d6dc3d42 | aliguori | qemu_mutex_init(&qemu_global_mutex); |
3499 | d6dc3d42 | aliguori | qemu_mutex_lock(&qemu_global_mutex); |
3500 | d6dc3d42 | aliguori | |
3501 | d6dc3d42 | aliguori | unblock_io_signals(); |
3502 | d6dc3d42 | aliguori | qemu_thread_self(&io_thread); |
3503 | d6dc3d42 | aliguori | |
3504 | d6dc3d42 | aliguori | return 0; |
3505 | d6dc3d42 | aliguori | } |
3506 | d6dc3d42 | aliguori | |
3507 | d6dc3d42 | aliguori | static void qemu_wait_io_event(CPUState *env) |
3508 | d6dc3d42 | aliguori | { |
3509 | d6dc3d42 | aliguori | while (!tcg_has_work())
|
3510 | d6dc3d42 | aliguori | qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
|
3511 | d6dc3d42 | aliguori | |
3512 | d6dc3d42 | aliguori | qemu_mutex_unlock(&qemu_global_mutex); |
3513 | d6dc3d42 | aliguori | |
3514 | d6dc3d42 | aliguori | /*
|
3515 | d6dc3d42 | aliguori | * Users of qemu_global_mutex can be starved, having no chance
|
3516 | d6dc3d42 | aliguori | * to acquire it since this path will get to it first.
|
3517 | d6dc3d42 | aliguori | * So use another lock to provide fairness.
|
3518 | d6dc3d42 | aliguori | */
|
3519 | d6dc3d42 | aliguori | qemu_mutex_lock(&qemu_fair_mutex); |
3520 | d6dc3d42 | aliguori | qemu_mutex_unlock(&qemu_fair_mutex); |
3521 | d6dc3d42 | aliguori | |
3522 | d6dc3d42 | aliguori | qemu_mutex_lock(&qemu_global_mutex); |
3523 | d6dc3d42 | aliguori | if (env->stop) {
|
3524 | d6dc3d42 | aliguori | env->stop = 0;
|
3525 | d6dc3d42 | aliguori | env->stopped = 1;
|
3526 | d6dc3d42 | aliguori | qemu_cond_signal(&qemu_pause_cond); |
3527 | d6dc3d42 | aliguori | } |
3528 | d6dc3d42 | aliguori | } |
3529 | d6dc3d42 | aliguori | |
3530 | d6dc3d42 | aliguori | static int qemu_cpu_exec(CPUState *env); |
3531 | d6dc3d42 | aliguori | |
3532 | d6dc3d42 | aliguori | static void *kvm_cpu_thread_fn(void *arg) |
3533 | d6dc3d42 | aliguori | { |
3534 | d6dc3d42 | aliguori | CPUState *env = arg; |
3535 | d6dc3d42 | aliguori | |
3536 | d6dc3d42 | aliguori | block_io_signals(); |
3537 | d6dc3d42 | aliguori | qemu_thread_self(env->thread); |
3538 | 321c1cb1 | Jean-Christophe DUBOIS | if (kvm_enabled())
|
3539 | 321c1cb1 | Jean-Christophe DUBOIS | kvm_init_vcpu(env); |
3540 | d6dc3d42 | aliguori | |
3541 | d6dc3d42 | aliguori | /* signal CPU creation */
|
3542 | d6dc3d42 | aliguori | qemu_mutex_lock(&qemu_global_mutex); |
3543 | d6dc3d42 | aliguori | env->created = 1;
|
3544 | d6dc3d42 | aliguori | qemu_cond_signal(&qemu_cpu_cond); |
3545 | d6dc3d42 | aliguori | |
3546 | d6dc3d42 | aliguori | /* and wait for machine initialization */
|
3547 | d6dc3d42 | aliguori | while (!qemu_system_ready)
|
3548 | d6dc3d42 | aliguori | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
|
3549 | d6dc3d42 | aliguori | |
3550 | d6dc3d42 | aliguori | while (1) { |
3551 | d6dc3d42 | aliguori | if (cpu_can_run(env))
|
3552 | d6dc3d42 | aliguori | qemu_cpu_exec(env); |
3553 | 1c3173b9 | Anthony Liguori | qemu_wait_io_event(env); |
3554 | d6dc3d42 | aliguori | } |
3555 | d6dc3d42 | aliguori | |
3556 | d6dc3d42 | aliguori | return NULL; |
3557 | d6dc3d42 | aliguori | } |
3558 | d6dc3d42 | aliguori | |
3559 | d6dc3d42 | aliguori | static void tcg_cpu_exec(void); |
3560 | d6dc3d42 | aliguori | |
3561 | d6dc3d42 | aliguori | static void *tcg_cpu_thread_fn(void *arg) |
3562 | d6dc3d42 | aliguori | { |
3563 | d6dc3d42 | aliguori | CPUState *env = arg; |
3564 | d6dc3d42 | aliguori | |
3565 | d6dc3d42 | aliguori | block_io_signals(); |
3566 | d6dc3d42 | aliguori | qemu_thread_self(env->thread); |
3567 | d6dc3d42 | aliguori | |
3568 | d6dc3d42 | aliguori | /* signal CPU creation */
|
3569 | d6dc3d42 | aliguori | qemu_mutex_lock(&qemu_global_mutex); |
3570 | d6dc3d42 | aliguori | for (env = first_cpu; env != NULL; env = env->next_cpu) |
3571 | d6dc3d42 | aliguori | env->created = 1;
|
3572 | d6dc3d42 | aliguori | qemu_cond_signal(&qemu_cpu_cond); |
3573 | d6dc3d42 | aliguori | |
3574 | d6dc3d42 | aliguori | /* and wait for machine initialization */
|
3575 | d6dc3d42 | aliguori | while (!qemu_system_ready)
|
3576 | d6dc3d42 | aliguori | qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
|
3577 | d6dc3d42 | aliguori | |
3578 | d6dc3d42 | aliguori | while (1) { |
3579 | d6dc3d42 | aliguori | tcg_cpu_exec(); |
3580 | d6dc3d42 | aliguori | qemu_wait_io_event(cur_cpu); |
3581 | d6dc3d42 | aliguori | } |
3582 | d6dc3d42 | aliguori | |
3583 | d6dc3d42 | aliguori | return NULL; |
3584 | d6dc3d42 | aliguori | } |
3585 | d6dc3d42 | aliguori | |
3586 | d6dc3d42 | aliguori | void qemu_cpu_kick(void *_env) |
3587 | d6dc3d42 | aliguori | { |
3588 | d6dc3d42 | aliguori | CPUState *env = _env; |
3589 | d6dc3d42 | aliguori | qemu_cond_broadcast(env->halt_cond); |
3590 | d6dc3d42 | aliguori | if (kvm_enabled())
|
3591 | d6dc3d42 | aliguori | qemu_thread_signal(env->thread, SIGUSR1); |
3592 | d6dc3d42 | aliguori | } |
3593 | d6dc3d42 | aliguori | |
3594 | d6dc3d42 | aliguori | int qemu_cpu_self(void *env) |
3595 | d6dc3d42 | aliguori | { |
3596 | d6dc3d42 | aliguori | return (cpu_single_env != NULL); |
3597 | d6dc3d42 | aliguori | } |
3598 | d6dc3d42 | aliguori | |
3599 | d6dc3d42 | aliguori | static void cpu_signal(int sig) |
3600 | d6dc3d42 | aliguori | { |
3601 | d6dc3d42 | aliguori | if (cpu_single_env)
|
3602 | d6dc3d42 | aliguori | cpu_exit(cpu_single_env); |
3603 | d6dc3d42 | aliguori | } |
3604 | d6dc3d42 | aliguori | |
3605 | d6dc3d42 | aliguori | static void block_io_signals(void) |
3606 | d6dc3d42 | aliguori | { |
3607 | d6dc3d42 | aliguori | sigset_t set; |
3608 | d6dc3d42 | aliguori | struct sigaction sigact;
|
3609 | d6dc3d42 | aliguori | |
3610 | d6dc3d42 | aliguori | sigemptyset(&set); |
3611 | d6dc3d42 | aliguori | sigaddset(&set, SIGUSR2); |
3612 | d6dc3d42 | aliguori | sigaddset(&set, SIGIO); |
3613 | d6dc3d42 | aliguori | sigaddset(&set, SIGALRM); |
3614 | d6dc3d42 | aliguori | pthread_sigmask(SIG_BLOCK, &set, NULL);
|
3615 | d6dc3d42 | aliguori | |
3616 | d6dc3d42 | aliguori | sigemptyset(&set); |
3617 | d6dc3d42 | aliguori | sigaddset(&set, SIGUSR1); |
3618 | d6dc3d42 | aliguori | pthread_sigmask(SIG_UNBLOCK, &set, NULL);
|
3619 | d6dc3d42 | aliguori | |
3620 | d6dc3d42 | aliguori | memset(&sigact, 0, sizeof(sigact)); |
3621 | d6dc3d42 | aliguori | sigact.sa_handler = cpu_signal; |
3622 | d6dc3d42 | aliguori | sigaction(SIGUSR1, &sigact, NULL);
|
3623 | d6dc3d42 | aliguori | } |
3624 | d6dc3d42 | aliguori | |
3625 | d6dc3d42 | aliguori | static void unblock_io_signals(void) |
3626 | d6dc3d42 | aliguori | { |
3627 | d6dc3d42 | aliguori | sigset_t set; |
3628 | d6dc3d42 | aliguori | |
3629 | d6dc3d42 | aliguori | sigemptyset(&set); |
3630 | d6dc3d42 | aliguori | sigaddset(&set, SIGUSR2); |
3631 | d6dc3d42 | aliguori | sigaddset(&set, SIGIO); |
3632 | d6dc3d42 | aliguori | sigaddset(&set, SIGALRM); |
3633 | d6dc3d42 | aliguori | pthread_sigmask(SIG_UNBLOCK, &set, NULL);
|
3634 | d6dc3d42 | aliguori | |
3635 | d6dc3d42 | aliguori | sigemptyset(&set); |
3636 | d6dc3d42 | aliguori | sigaddset(&set, SIGUSR1); |
3637 | d6dc3d42 | aliguori | pthread_sigmask(SIG_BLOCK, &set, NULL);
|
3638 | d6dc3d42 | aliguori | } |
3639 | d6dc3d42 | aliguori | |
3640 | d6dc3d42 | aliguori | static void qemu_signal_lock(unsigned int msecs) |
3641 | d6dc3d42 | aliguori | { |
3642 | d6dc3d42 | aliguori | qemu_mutex_lock(&qemu_fair_mutex); |
3643 | d6dc3d42 | aliguori | |
3644 | d6dc3d42 | aliguori | while (qemu_mutex_trylock(&qemu_global_mutex)) {
|
3645 | d6dc3d42 | aliguori | qemu_thread_signal(tcg_cpu_thread, SIGUSR1); |
3646 | d6dc3d42 | aliguori | if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
|
3647 | d6dc3d42 | aliguori | break;
|
3648 | d6dc3d42 | aliguori | } |
3649 | d6dc3d42 | aliguori | qemu_mutex_unlock(&qemu_fair_mutex); |
3650 | d6dc3d42 | aliguori | } |
3651 | d6dc3d42 | aliguori | |
3652 | d6dc3d42 | aliguori | static void qemu_mutex_lock_iothread(void) |
3653 | d6dc3d42 | aliguori | { |
3654 | d6dc3d42 | aliguori | if (kvm_enabled()) {
|
3655 | d6dc3d42 | aliguori | qemu_mutex_lock(&qemu_fair_mutex); |
3656 | d6dc3d42 | aliguori | qemu_mutex_lock(&qemu_global_mutex); |
3657 | d6dc3d42 | aliguori | qemu_mutex_unlock(&qemu_fair_mutex); |
3658 | d6dc3d42 | aliguori | } else
|
3659 | d6dc3d42 | aliguori | qemu_signal_lock(100);
|
3660 | d6dc3d42 | aliguori | } |
3661 | d6dc3d42 | aliguori | |
3662 | d6dc3d42 | aliguori | static void qemu_mutex_unlock_iothread(void) |
3663 | d6dc3d42 | aliguori | { |
3664 | d6dc3d42 | aliguori | qemu_mutex_unlock(&qemu_global_mutex); |
3665 | d6dc3d42 | aliguori | } |
3666 | d6dc3d42 | aliguori | |
3667 | d6dc3d42 | aliguori | static int all_vcpus_paused(void) |
3668 | d6dc3d42 | aliguori | { |
3669 | d6dc3d42 | aliguori | CPUState *penv = first_cpu; |
3670 | d6dc3d42 | aliguori | |
3671 | d6dc3d42 | aliguori | while (penv) {
|
3672 | d6dc3d42 | aliguori | if (!penv->stopped)
|
3673 | d6dc3d42 | aliguori | return 0; |
3674 | d6dc3d42 | aliguori | penv = (CPUState *)penv->next_cpu; |
3675 | d6dc3d42 | aliguori | } |
3676 | d6dc3d42 | aliguori | |
3677 | d6dc3d42 | aliguori | return 1; |
3678 | d6dc3d42 | aliguori | } |
3679 | d6dc3d42 | aliguori | |
3680 | d6dc3d42 | aliguori | static void pause_all_vcpus(void) |
3681 | d6dc3d42 | aliguori | { |
3682 | d6dc3d42 | aliguori | CPUState *penv = first_cpu; |
3683 | d6dc3d42 | aliguori | |
3684 | d6dc3d42 | aliguori | while (penv) {
|
3685 | d6dc3d42 | aliguori | penv->stop = 1;
|
3686 | d6dc3d42 | aliguori | qemu_thread_signal(penv->thread, SIGUSR1); |
3687 | d6dc3d42 | aliguori | qemu_cpu_kick(penv); |
3688 | d6dc3d42 | aliguori | penv = (CPUState *)penv->next_cpu; |
3689 | d6dc3d42 | aliguori | } |
3690 | d6dc3d42 | aliguori | |
3691 | d6dc3d42 | aliguori | while (!all_vcpus_paused()) {
|
3692 | d6dc3d42 | aliguori | qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
|
3693 | d6dc3d42 | aliguori | penv = first_cpu; |
3694 | d6dc3d42 | aliguori | while (penv) {
|
3695 | d6dc3d42 | aliguori | qemu_thread_signal(penv->thread, SIGUSR1); |
3696 | d6dc3d42 | aliguori | penv = (CPUState *)penv->next_cpu; |
3697 | d6dc3d42 | aliguori | } |
3698 | d6dc3d42 | aliguori | } |
3699 | d6dc3d42 | aliguori | } |
3700 | d6dc3d42 | aliguori | |
3701 | d6dc3d42 | aliguori | static void resume_all_vcpus(void) |
3702 | d6dc3d42 | aliguori | { |
3703 | d6dc3d42 | aliguori | CPUState *penv = first_cpu; |
3704 | d6dc3d42 | aliguori | |
3705 | d6dc3d42 | aliguori | while (penv) {
|
3706 | d6dc3d42 | aliguori | penv->stop = 0;
|
3707 | d6dc3d42 | aliguori | penv->stopped = 0;
|
3708 | d6dc3d42 | aliguori | qemu_thread_signal(penv->thread, SIGUSR1); |
3709 | d6dc3d42 | aliguori | qemu_cpu_kick(penv); |
3710 | d6dc3d42 | aliguori | penv = (CPUState *)penv->next_cpu; |
3711 | d6dc3d42 | aliguori | } |
3712 | d6dc3d42 | aliguori | } |
3713 | d6dc3d42 | aliguori | |
3714 | d6dc3d42 | aliguori | static void tcg_init_vcpu(void *_env) |
3715 | d6dc3d42 | aliguori | { |
3716 | d6dc3d42 | aliguori | CPUState *env = _env; |
3717 | d6dc3d42 | aliguori | /* share a single thread for all cpus with TCG */
|
3718 | d6dc3d42 | aliguori | if (!tcg_cpu_thread) {
|
3719 | d6dc3d42 | aliguori | env->thread = qemu_mallocz(sizeof(QemuThread));
|
3720 | d6dc3d42 | aliguori | env->halt_cond = qemu_mallocz(sizeof(QemuCond));
|
3721 | d6dc3d42 | aliguori | qemu_cond_init(env->halt_cond); |
3722 | d6dc3d42 | aliguori | qemu_thread_create(env->thread, tcg_cpu_thread_fn, env); |
3723 | d6dc3d42 | aliguori | while (env->created == 0) |
3724 | d6dc3d42 | aliguori | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
|
3725 | d6dc3d42 | aliguori | tcg_cpu_thread = env->thread; |
3726 | d6dc3d42 | aliguori | tcg_halt_cond = env->halt_cond; |
3727 | d6dc3d42 | aliguori | } else {
|
3728 | d6dc3d42 | aliguori | env->thread = tcg_cpu_thread; |
3729 | d6dc3d42 | aliguori | env->halt_cond = tcg_halt_cond; |
3730 | d6dc3d42 | aliguori | } |
3731 | d6dc3d42 | aliguori | } |
3732 | d6dc3d42 | aliguori | |
3733 | d6dc3d42 | aliguori | static void kvm_start_vcpu(CPUState *env) |
3734 | d6dc3d42 | aliguori | { |
3735 | d6dc3d42 | aliguori | env->thread = qemu_mallocz(sizeof(QemuThread));
|
3736 | d6dc3d42 | aliguori | env->halt_cond = qemu_mallocz(sizeof(QemuCond));
|
3737 | d6dc3d42 | aliguori | qemu_cond_init(env->halt_cond); |
3738 | d6dc3d42 | aliguori | qemu_thread_create(env->thread, kvm_cpu_thread_fn, env); |
3739 | d6dc3d42 | aliguori | while (env->created == 0) |
3740 | d6dc3d42 | aliguori | qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
|
3741 | d6dc3d42 | aliguori | } |
3742 | d6dc3d42 | aliguori | |
3743 | d6dc3d42 | aliguori | void qemu_init_vcpu(void *_env) |
3744 | d6dc3d42 | aliguori | { |
3745 | d6dc3d42 | aliguori | CPUState *env = _env; |
3746 | d6dc3d42 | aliguori | |
3747 | d6dc3d42 | aliguori | if (kvm_enabled())
|
3748 | d6dc3d42 | aliguori | kvm_start_vcpu(env); |
3749 | d6dc3d42 | aliguori | else
|
3750 | d6dc3d42 | aliguori | tcg_init_vcpu(env); |
3751 | dc6b1c09 | Andre Przywara | env->nr_cores = smp_cores; |
3752 | dc6b1c09 | Andre Przywara | env->nr_threads = smp_threads; |
3753 | d6dc3d42 | aliguori | } |
3754 | d6dc3d42 | aliguori | |
3755 | d6dc3d42 | aliguori | void qemu_notify_event(void) |
3756 | d6dc3d42 | aliguori | { |
3757 | d6dc3d42 | aliguori | qemu_event_increment(); |
3758 | d6dc3d42 | aliguori | } |
3759 | d6dc3d42 | aliguori | |
3760 | d6dc3d42 | aliguori | void vm_stop(int reason) |
3761 | d6dc3d42 | aliguori | { |
3762 | d6dc3d42 | aliguori | QemuThread me; |
3763 | d6dc3d42 | aliguori | qemu_thread_self(&me); |
3764 | d6dc3d42 | aliguori | |
3765 | d6dc3d42 | aliguori | if (!qemu_thread_equal(&me, &io_thread)) {
|
3766 | d6dc3d42 | aliguori | qemu_system_vmstop_request(reason); |
3767 | d6dc3d42 | aliguori | /*
|
3768 | d6dc3d42 | aliguori | * FIXME: should not return to device code in case
|
3769 | d6dc3d42 | aliguori | * vm_stop() has been requested.
|
3770 | d6dc3d42 | aliguori | */
|
3771 | d6dc3d42 | aliguori | if (cpu_single_env) {
|
3772 | d6dc3d42 | aliguori | cpu_exit(cpu_single_env); |
3773 | d6dc3d42 | aliguori | cpu_single_env->stop = 1;
|
3774 | d6dc3d42 | aliguori | } |
3775 | d6dc3d42 | aliguori | return;
|
3776 | d6dc3d42 | aliguori | } |
3777 | d6dc3d42 | aliguori | do_vm_stop(reason); |
3778 | d6dc3d42 | aliguori | } |
3779 | d6dc3d42 | aliguori | |
3780 | d6dc3d42 | aliguori | #endif
|
3781 | d6dc3d42 | aliguori | |
3782 | d6dc3d42 | aliguori | |
3783 | 877cf882 | ths | #ifdef _WIN32
|
3784 | 69d6451c | blueswir1 | static void host_main_loop_wait(int *timeout) |
3785 | 56f3a5d0 | aliguori | { |
3786 | 56f3a5d0 | aliguori | int ret, ret2, i;
|
3787 | f331110f | bellard | PollingEntry *pe; |
3788 | f331110f | bellard | |
3789 | c4b1fcc0 | bellard | |
3790 | f331110f | bellard | /* XXX: need to suppress polling by better using win32 events */
|
3791 | f331110f | bellard | ret = 0;
|
3792 | f331110f | bellard | for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
3793 | f331110f | bellard | ret |= pe->func(pe->opaque); |
3794 | f331110f | bellard | } |
3795 | e6b1e558 | ths | if (ret == 0) { |
3796 | a18e524a | bellard | int err;
|
3797 | a18e524a | bellard | WaitObjects *w = &wait_objects; |
3798 | 3b46e624 | ths | |
3799 | 56f3a5d0 | aliguori | ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout); |
3800 | a18e524a | bellard | if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
3801 | a18e524a | bellard | if (w->func[ret - WAIT_OBJECT_0])
|
3802 | a18e524a | bellard | w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
3803 | 3b46e624 | ths | |
3804 | 5fafdf24 | ths | /* Check for additional signaled events */
|
3805 | e6b1e558 | ths | for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
3806 | 3b46e624 | ths | |
3807 | e6b1e558 | ths | /* Check if event is signaled */
|
3808 | e6b1e558 | ths | ret2 = WaitForSingleObject(w->events[i], 0);
|
3809 | e6b1e558 | ths | if(ret2 == WAIT_OBJECT_0) {
|
3810 | e6b1e558 | ths | if (w->func[i])
|
3811 | e6b1e558 | ths | w->func[i](w->opaque[i]); |
3812 | e6b1e558 | ths | } else if (ret2 == WAIT_TIMEOUT) { |
3813 | e6b1e558 | ths | } else {
|
3814 | e6b1e558 | ths | err = GetLastError(); |
3815 | e6b1e558 | ths | fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
|
3816 | 3b46e624 | ths | } |
3817 | 3b46e624 | ths | } |
3818 | a18e524a | bellard | } else if (ret == WAIT_TIMEOUT) { |
3819 | a18e524a | bellard | } else {
|
3820 | a18e524a | bellard | err = GetLastError(); |
3821 | e6b1e558 | ths | fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
|
3822 | a18e524a | bellard | } |
3823 | f331110f | bellard | } |
3824 | 56f3a5d0 | aliguori | |
3825 | 56f3a5d0 | aliguori | *timeout = 0;
|
3826 | 56f3a5d0 | aliguori | } |
3827 | 56f3a5d0 | aliguori | #else
|
3828 | 69d6451c | blueswir1 | static void host_main_loop_wait(int *timeout) |
3829 | 56f3a5d0 | aliguori | { |
3830 | 56f3a5d0 | aliguori | } |
3831 | fd1dff4b | bellard | #endif
|
3832 | 56f3a5d0 | aliguori | |
3833 | 56f3a5d0 | aliguori | void main_loop_wait(int timeout) |
3834 | 56f3a5d0 | aliguori | { |
3835 | 56f3a5d0 | aliguori | IOHandlerRecord *ioh; |
3836 | 56f3a5d0 | aliguori | fd_set rfds, wfds, xfds; |
3837 | 56f3a5d0 | aliguori | int ret, nfds;
|
3838 | 56f3a5d0 | aliguori | struct timeval tv;
|
3839 | 56f3a5d0 | aliguori | |
3840 | 56f3a5d0 | aliguori | qemu_bh_update_timeout(&timeout); |
3841 | 56f3a5d0 | aliguori | |
3842 | 56f3a5d0 | aliguori | host_main_loop_wait(&timeout); |
3843 | 56f3a5d0 | aliguori | |
3844 | fd1dff4b | bellard | /* poll any events */
|
3845 | fd1dff4b | bellard | /* XXX: separate device handlers from system ones */
|
3846 | 6abfbd79 | aliguori | nfds = -1;
|
3847 | fd1dff4b | bellard | FD_ZERO(&rfds); |
3848 | fd1dff4b | bellard | FD_ZERO(&wfds); |
3849 | e035649e | bellard | FD_ZERO(&xfds); |
3850 | fd1dff4b | bellard | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
3851 | cafffd40 | ths | if (ioh->deleted)
|
3852 | cafffd40 | ths | continue;
|
3853 | fd1dff4b | bellard | if (ioh->fd_read &&
|
3854 | fd1dff4b | bellard | (!ioh->fd_read_poll || |
3855 | fd1dff4b | bellard | ioh->fd_read_poll(ioh->opaque) != 0)) {
|
3856 | fd1dff4b | bellard | FD_SET(ioh->fd, &rfds); |
3857 | fd1dff4b | bellard | if (ioh->fd > nfds)
|
3858 | fd1dff4b | bellard | nfds = ioh->fd; |
3859 | fd1dff4b | bellard | } |
3860 | fd1dff4b | bellard | if (ioh->fd_write) {
|
3861 | fd1dff4b | bellard | FD_SET(ioh->fd, &wfds); |
3862 | fd1dff4b | bellard | if (ioh->fd > nfds)
|
3863 | fd1dff4b | bellard | nfds = ioh->fd; |
3864 | fd1dff4b | bellard | } |
3865 | fd1dff4b | bellard | } |
3866 | 3b46e624 | ths | |
3867 | 56f3a5d0 | aliguori | tv.tv_sec = timeout / 1000;
|
3868 | 56f3a5d0 | aliguori | tv.tv_usec = (timeout % 1000) * 1000; |
3869 | 56f3a5d0 | aliguori | |
3870 | d918f23e | Jan Kiszka | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
3871 | d918f23e | Jan Kiszka | |
3872 | 4870852c | aliguori | qemu_mutex_unlock_iothread(); |
3873 | e035649e | bellard | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
|
3874 | 4870852c | aliguori | qemu_mutex_lock_iothread(); |
3875 | fd1dff4b | bellard | if (ret > 0) { |
3876 | cafffd40 | ths | IOHandlerRecord **pioh; |
3877 | cafffd40 | ths | |
3878 | cafffd40 | ths | for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
3879 | 6ab43fdc | ths | if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
|
3880 | fd1dff4b | bellard | ioh->fd_read(ioh->opaque); |
3881 | 7c9d8e07 | bellard | } |
3882 | 6ab43fdc | ths | if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
|
3883 | fd1dff4b | bellard | ioh->fd_write(ioh->opaque); |
3884 | c4b1fcc0 | bellard | } |
3885 | b4608c04 | bellard | } |
3886 | cafffd40 | ths | |
3887 | cafffd40 | ths | /* remove deleted IO handlers */
|
3888 | cafffd40 | ths | pioh = &first_io_handler; |
3889 | cafffd40 | ths | while (*pioh) {
|
3890 | cafffd40 | ths | ioh = *pioh; |
3891 | cafffd40 | ths | if (ioh->deleted) {
|
3892 | cafffd40 | ths | *pioh = ioh->next; |
3893 | cafffd40 | ths | qemu_free(ioh); |
3894 | 5fafdf24 | ths | } else
|
3895 | cafffd40 | ths | pioh = &ioh->next; |
3896 | cafffd40 | ths | } |
3897 | fd1dff4b | bellard | } |
3898 | d918f23e | Jan Kiszka | |
3899 | d918f23e | Jan Kiszka | slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
|
3900 | c20709aa | bellard | |
3901 | 50317c7f | aliguori | /* rearm timer, if not periodic */
|
3902 | 50317c7f | aliguori | if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
|
3903 | 50317c7f | aliguori | alarm_timer->flags &= ~ALARM_FLAG_EXPIRED; |
3904 | 50317c7f | aliguori | qemu_rearm_alarm_timer(alarm_timer); |
3905 | 50317c7f | aliguori | } |
3906 | 50317c7f | aliguori | |
3907 | 357c692c | aliguori | /* vm time timers */
|
3908 | d6dc3d42 | aliguori | if (vm_running) {
|
3909 | d6dc3d42 | aliguori | if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
|
3910 | d6dc3d42 | aliguori | qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
3911 | d6dc3d42 | aliguori | qemu_get_clock(vm_clock)); |
3912 | d6dc3d42 | aliguori | } |
3913 | 357c692c | aliguori | |
3914 | 357c692c | aliguori | /* real time timers */
|
3915 | 357c692c | aliguori | qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
3916 | 357c692c | aliguori | qemu_get_clock(rt_clock)); |
3917 | 357c692c | aliguori | |
3918 | 423f0742 | pbrook | /* Check bottom-halves last in case any of the earlier events triggered
|
3919 | 423f0742 | pbrook | them. */
|
3920 | 423f0742 | pbrook | qemu_bh_poll(); |
3921 | 3b46e624 | ths | |
3922 | 5905b2e5 | bellard | } |
3923 | 5905b2e5 | bellard | |
3924 | 43b96858 | aliguori | static int qemu_cpu_exec(CPUState *env) |
3925 | 5905b2e5 | bellard | { |
3926 | 43b96858 | aliguori | int ret;
|
3927 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
3928 | 89bfc105 | bellard | int64_t ti; |
3929 | 89bfc105 | bellard | #endif
|
3930 | 5905b2e5 | bellard | |
3931 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
3932 | 43b96858 | aliguori | ti = profile_getclock(); |
3933 | 89bfc105 | bellard | #endif
|
3934 | 43b96858 | aliguori | if (use_icount) {
|
3935 | 43b96858 | aliguori | int64_t count; |
3936 | 43b96858 | aliguori | int decr;
|
3937 | 43b96858 | aliguori | qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
3938 | 43b96858 | aliguori | env->icount_decr.u16.low = 0;
|
3939 | 43b96858 | aliguori | env->icount_extra = 0;
|
3940 | 43b96858 | aliguori | count = qemu_next_deadline(); |
3941 | 43b96858 | aliguori | count = (count + (1 << icount_time_shift) - 1) |
3942 | 43b96858 | aliguori | >> icount_time_shift; |
3943 | 43b96858 | aliguori | qemu_icount += count; |
3944 | 43b96858 | aliguori | decr = (count > 0xffff) ? 0xffff : count; |
3945 | 43b96858 | aliguori | count -= decr; |
3946 | 43b96858 | aliguori | env->icount_decr.u16.low = decr; |
3947 | 43b96858 | aliguori | env->icount_extra = count; |
3948 | 43b96858 | aliguori | } |
3949 | 43b96858 | aliguori | ret = cpu_exec(env); |
3950 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
3951 | 43b96858 | aliguori | qemu_time += profile_getclock() - ti; |
3952 | 89bfc105 | bellard | #endif
|
3953 | 43b96858 | aliguori | if (use_icount) {
|
3954 | 43b96858 | aliguori | /* Fold pending instructions back into the
|
3955 | 43b96858 | aliguori | instruction counter, and clear the interrupt flag. */
|
3956 | 43b96858 | aliguori | qemu_icount -= (env->icount_decr.u16.low |
3957 | 43b96858 | aliguori | + env->icount_extra); |
3958 | 43b96858 | aliguori | env->icount_decr.u32 = 0;
|
3959 | 43b96858 | aliguori | env->icount_extra = 0;
|
3960 | 43b96858 | aliguori | } |
3961 | 43b96858 | aliguori | return ret;
|
3962 | 43b96858 | aliguori | } |
3963 | 43b96858 | aliguori | |
3964 | e6e35b1e | aliguori | static void tcg_cpu_exec(void) |
3965 | e6e35b1e | aliguori | { |
3966 | d6dc3d42 | aliguori | int ret = 0; |
3967 | e6e35b1e | aliguori | |
3968 | e6e35b1e | aliguori | if (next_cpu == NULL) |
3969 | e6e35b1e | aliguori | next_cpu = first_cpu; |
3970 | e6e35b1e | aliguori | for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) { |
3971 | e6e35b1e | aliguori | CPUState *env = cur_cpu = next_cpu; |
3972 | e6e35b1e | aliguori | |
3973 | e6e35b1e | aliguori | if (!vm_running)
|
3974 | e6e35b1e | aliguori | break;
|
3975 | e6e35b1e | aliguori | if (timer_alarm_pending) {
|
3976 | e6e35b1e | aliguori | timer_alarm_pending = 0;
|
3977 | e6e35b1e | aliguori | break;
|
3978 | e6e35b1e | aliguori | } |
3979 | d6dc3d42 | aliguori | if (cpu_can_run(env))
|
3980 | d6dc3d42 | aliguori | ret = qemu_cpu_exec(env); |
3981 | e6e35b1e | aliguori | if (ret == EXCP_DEBUG) {
|
3982 | e6e35b1e | aliguori | gdb_set_stop_cpu(env); |
3983 | e6e35b1e | aliguori | debug_requested = 1;
|
3984 | e6e35b1e | aliguori | break;
|
3985 | e6e35b1e | aliguori | } |
3986 | e6e35b1e | aliguori | } |
3987 | e6e35b1e | aliguori | } |
3988 | e6e35b1e | aliguori | |
3989 | 43b96858 | aliguori | static int cpu_has_work(CPUState *env) |
3990 | 43b96858 | aliguori | { |
3991 | d6dc3d42 | aliguori | if (env->stop)
|
3992 | d6dc3d42 | aliguori | return 1; |
3993 | d6dc3d42 | aliguori | if (env->stopped)
|
3994 | d6dc3d42 | aliguori | return 0; |
3995 | 43b96858 | aliguori | if (!env->halted)
|
3996 | 43b96858 | aliguori | return 1; |
3997 | 43b96858 | aliguori | if (qemu_cpu_has_work(env))
|
3998 | 43b96858 | aliguori | return 1; |
3999 | 43b96858 | aliguori | return 0; |
4000 | 43b96858 | aliguori | } |
4001 | 43b96858 | aliguori | |
4002 | 43b96858 | aliguori | static int tcg_has_work(void) |
4003 | 43b96858 | aliguori | { |
4004 | 43b96858 | aliguori | CPUState *env; |
4005 | 43b96858 | aliguori | |
4006 | 43b96858 | aliguori | for (env = first_cpu; env != NULL; env = env->next_cpu) |
4007 | 43b96858 | aliguori | if (cpu_has_work(env))
|
4008 | 43b96858 | aliguori | return 1; |
4009 | 43b96858 | aliguori | return 0; |
4010 | 43b96858 | aliguori | } |
4011 | 43b96858 | aliguori | |
4012 | 43b96858 | aliguori | static int qemu_calculate_timeout(void) |
4013 | 43b96858 | aliguori | { |
4014 | b319820d | Luiz Capitulino | #ifndef CONFIG_IOTHREAD
|
4015 | 43b96858 | aliguori | int timeout;
|
4016 | 43b96858 | aliguori | |
4017 | 43b96858 | aliguori | if (!vm_running)
|
4018 | 43b96858 | aliguori | timeout = 5000;
|
4019 | 43b96858 | aliguori | else if (tcg_has_work()) |
4020 | 43b96858 | aliguori | timeout = 0;
|
4021 | 43b96858 | aliguori | else if (!use_icount) |
4022 | 43b96858 | aliguori | timeout = 5000;
|
4023 | 43b96858 | aliguori | else {
|
4024 | 43b96858 | aliguori | /* XXX: use timeout computed from timers */
|
4025 | 43b96858 | aliguori | int64_t add; |
4026 | 43b96858 | aliguori | int64_t delta; |
4027 | 43b96858 | aliguori | /* Advance virtual time to the next event. */
|
4028 | 43b96858 | aliguori | if (use_icount == 1) { |
4029 | 43b96858 | aliguori | /* When not using an adaptive execution frequency
|
4030 | 43b96858 | aliguori | we tend to get badly out of sync with real time,
|
4031 | 43b96858 | aliguori | so just delay for a reasonable amount of time. */
|
4032 | 43b96858 | aliguori | delta = 0;
|
4033 | 43b96858 | aliguori | } else {
|
4034 | 43b96858 | aliguori | delta = cpu_get_icount() - cpu_get_clock(); |
4035 | 43b96858 | aliguori | } |
4036 | 43b96858 | aliguori | if (delta > 0) { |
4037 | 43b96858 | aliguori | /* If virtual time is ahead of real time then just
|
4038 | 43b96858 | aliguori | wait for IO. */
|
4039 | 43b96858 | aliguori | timeout = (delta / 1000000) + 1; |
4040 | 43b96858 | aliguori | } else {
|
4041 | 43b96858 | aliguori | /* Wait for either IO to occur or the next
|
4042 | 43b96858 | aliguori | timer event. */
|
4043 | 43b96858 | aliguori | add = qemu_next_deadline(); |
4044 | 43b96858 | aliguori | /* We advance the timer before checking for IO.
|
4045 | 43b96858 | aliguori | Limit the amount we advance so that early IO
|
4046 | 43b96858 | aliguori | activity won't get the guest too far ahead. */
|
4047 | 43b96858 | aliguori | if (add > 10000000) |
4048 | 43b96858 | aliguori | add = 10000000;
|
4049 | 43b96858 | aliguori | delta += add; |
4050 | 43b96858 | aliguori | add = (add + (1 << icount_time_shift) - 1) |
4051 | 43b96858 | aliguori | >> icount_time_shift; |
4052 | 43b96858 | aliguori | qemu_icount += add; |
4053 | 43b96858 | aliguori | timeout = delta / 1000000;
|
4054 | 43b96858 | aliguori | if (timeout < 0) |
4055 | 43b96858 | aliguori | timeout = 0;
|
4056 | 43b96858 | aliguori | } |
4057 | 43b96858 | aliguori | } |
4058 | 43b96858 | aliguori | |
4059 | 43b96858 | aliguori | return timeout;
|
4060 | b319820d | Luiz Capitulino | #else /* CONFIG_IOTHREAD */ |
4061 | b319820d | Luiz Capitulino | return 1000; |
4062 | b319820d | Luiz Capitulino | #endif
|
4063 | 43b96858 | aliguori | } |
4064 | 43b96858 | aliguori | |
4065 | 43b96858 | aliguori | static int vm_can_run(void) |
4066 | 43b96858 | aliguori | { |
4067 | 43b96858 | aliguori | if (powerdown_requested)
|
4068 | 43b96858 | aliguori | return 0; |
4069 | 43b96858 | aliguori | if (reset_requested)
|
4070 | 43b96858 | aliguori | return 0; |
4071 | 43b96858 | aliguori | if (shutdown_requested)
|
4072 | 43b96858 | aliguori | return 0; |
4073 | e568902a | aliguori | if (debug_requested)
|
4074 | e568902a | aliguori | return 0; |
4075 | 43b96858 | aliguori | return 1; |
4076 | 43b96858 | aliguori | } |
4077 | 43b96858 | aliguori | |
4078 | d9c32310 | Blue Swirl | qemu_irq qemu_system_powerdown; |
4079 | d9c32310 | Blue Swirl | |
4080 | 43b96858 | aliguori | static void main_loop(void) |
4081 | 43b96858 | aliguori | { |
4082 | 6e29f5da | aliguori | int r;
|
4083 | e6e35b1e | aliguori | |
4084 | d6dc3d42 | aliguori | #ifdef CONFIG_IOTHREAD
|
4085 | d6dc3d42 | aliguori | qemu_system_ready = 1;
|
4086 | d6dc3d42 | aliguori | qemu_cond_broadcast(&qemu_system_cond); |
4087 | d6dc3d42 | aliguori | #endif
|
4088 | d6dc3d42 | aliguori | |
4089 | 6e29f5da | aliguori | for (;;) {
|
4090 | 43b96858 | aliguori | do {
|
4091 | e6e35b1e | aliguori | #ifdef CONFIG_PROFILER
|
4092 | e6e35b1e | aliguori | int64_t ti; |
4093 | e6e35b1e | aliguori | #endif
|
4094 | d6dc3d42 | aliguori | #ifndef CONFIG_IOTHREAD
|
4095 | e6e35b1e | aliguori | tcg_cpu_exec(); |
4096 | d6dc3d42 | aliguori | #endif
|
4097 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
4098 | 43b96858 | aliguori | ti = profile_getclock(); |
4099 | 89bfc105 | bellard | #endif
|
4100 | 43b96858 | aliguori | main_loop_wait(qemu_calculate_timeout()); |
4101 | 89bfc105 | bellard | #ifdef CONFIG_PROFILER
|
4102 | 43b96858 | aliguori | dev_time += profile_getclock() - ti; |
4103 | 89bfc105 | bellard | #endif
|
4104 | e568902a | aliguori | } while (vm_can_run());
|
4105 | 43b96858 | aliguori | |
4106 | e568902a | aliguori | if (qemu_debug_requested())
|
4107 | e568902a | aliguori | vm_stop(EXCP_DEBUG); |
4108 | 43b96858 | aliguori | if (qemu_shutdown_requested()) {
|
4109 | 43b96858 | aliguori | if (no_shutdown) {
|
4110 | 43b96858 | aliguori | vm_stop(0);
|
4111 | 43b96858 | aliguori | no_shutdown = 0;
|
4112 | 43b96858 | aliguori | } else
|
4113 | 43b96858 | aliguori | break;
|
4114 | 43b96858 | aliguori | } |
4115 | d6dc3d42 | aliguori | if (qemu_reset_requested()) {
|
4116 | d6dc3d42 | aliguori | pause_all_vcpus(); |
4117 | 43b96858 | aliguori | qemu_system_reset(); |
4118 | d6dc3d42 | aliguori | resume_all_vcpus(); |
4119 | d6dc3d42 | aliguori | } |
4120 | d9c32310 | Blue Swirl | if (qemu_powerdown_requested()) {
|
4121 | d9c32310 | Blue Swirl | qemu_irq_raise(qemu_system_powerdown); |
4122 | d9c32310 | Blue Swirl | } |
4123 | 6e29f5da | aliguori | if ((r = qemu_vmstop_requested()))
|
4124 | 6e29f5da | aliguori | vm_stop(r); |
4125 | b4608c04 | bellard | } |
4126 | d6dc3d42 | aliguori | pause_all_vcpus(); |
4127 | b4608c04 | bellard | } |
4128 | b4608c04 | bellard | |
4129 | 9bd7e6d9 | pbrook | static void version(void) |
4130 | 9bd7e6d9 | pbrook | { |
4131 | 4a19f1ec | pbrook | printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"); |
4132 | 9bd7e6d9 | pbrook | } |
4133 | 9bd7e6d9 | pbrook | |
4134 | 15f82208 | ths | static void help(int exitcode) |
4135 | 0824d6fc | bellard | { |
4136 | 9bd7e6d9 | pbrook | version(); |
4137 | 9bd7e6d9 | pbrook | printf("usage: %s [options] [disk_image]\n"
|
4138 | 0824d6fc | bellard | "\n"
|
4139 | a20dd508 | bellard | "'disk_image' is a raw hard image image for IDE hard disk 0\n"
|
4140 | fc01f7e7 | bellard | "\n"
|
4141 | 5824d651 | blueswir1 | #define DEF(option, opt_arg, opt_enum, opt_help) \
|
4142 | 5824d651 | blueswir1 | opt_help |
4143 | 5824d651 | blueswir1 | #define DEFHEADING(text) stringify(text) "\n" |
4144 | 5824d651 | blueswir1 | #include "qemu-options.h" |
4145 | 5824d651 | blueswir1 | #undef DEF
|
4146 | 5824d651 | blueswir1 | #undef DEFHEADING
|
4147 | 5824d651 | blueswir1 | #undef GEN_DOCS
|
4148 | 0824d6fc | bellard | "\n"
|
4149 | 82c643ff | bellard | "During emulation, the following keys are useful:\n"
|
4150 | 032a8c9e | bellard | "ctrl-alt-f toggle full screen\n"
|
4151 | 032a8c9e | bellard | "ctrl-alt-n switch to virtual console 'n'\n"
|
4152 | 032a8c9e | bellard | "ctrl-alt toggle mouse and keyboard grab\n"
|
4153 | 82c643ff | bellard | "\n"
|
4154 | 82c643ff | bellard | "When using -nographic, press 'ctrl-a h' to get some help.\n"
|
4155 | 82c643ff | bellard | , |
4156 | 0db63474 | bellard | "qemu",
|
4157 | a00bad7e | bellard | DEFAULT_RAM_SIZE, |
4158 | 7c9d8e07 | bellard | #ifndef _WIN32
|
4159 | a00bad7e | bellard | DEFAULT_NETWORK_SCRIPT, |
4160 | b46a8906 | ths | DEFAULT_NETWORK_DOWN_SCRIPT, |
4161 | 7c9d8e07 | bellard | #endif
|
4162 | 6e44ba7f | bellard | DEFAULT_GDBSTUB_PORT, |
4163 | bce61846 | bellard | "/tmp/qemu.log");
|
4164 | 15f82208 | ths | exit(exitcode); |
4165 | 0824d6fc | bellard | } |
4166 | 0824d6fc | bellard | |
4167 | cd6f1169 | bellard | #define HAS_ARG 0x0001 |
4168 | cd6f1169 | bellard | |
4169 | cd6f1169 | bellard | enum {
|
4170 | 5824d651 | blueswir1 | #define DEF(option, opt_arg, opt_enum, opt_help) \
|
4171 | 5824d651 | blueswir1 | opt_enum, |
4172 | 5824d651 | blueswir1 | #define DEFHEADING(text)
|
4173 | 5824d651 | blueswir1 | #include "qemu-options.h" |
4174 | 5824d651 | blueswir1 | #undef DEF
|
4175 | 5824d651 | blueswir1 | #undef DEFHEADING
|
4176 | 5824d651 | blueswir1 | #undef GEN_DOCS
|
4177 | cd6f1169 | bellard | }; |
4178 | cd6f1169 | bellard | |
4179 | cd6f1169 | bellard | typedef struct QEMUOption { |
4180 | cd6f1169 | bellard | const char *name; |
4181 | cd6f1169 | bellard | int flags;
|
4182 | cd6f1169 | bellard | int index;
|
4183 | cd6f1169 | bellard | } QEMUOption; |
4184 | cd6f1169 | bellard | |
4185 | dbed7e40 | blueswir1 | static const QEMUOption qemu_options[] = { |
4186 | cd6f1169 | bellard | { "h", 0, QEMU_OPTION_h }, |
4187 | 5824d651 | blueswir1 | #define DEF(option, opt_arg, opt_enum, opt_help) \
|
4188 | 5824d651 | blueswir1 | { option, opt_arg, opt_enum }, |
4189 | 5824d651 | blueswir1 | #define DEFHEADING(text)
|
4190 | 5824d651 | blueswir1 | #include "qemu-options.h" |
4191 | 5824d651 | blueswir1 | #undef DEF
|
4192 | 5824d651 | blueswir1 | #undef DEFHEADING
|
4193 | 5824d651 | blueswir1 | #undef GEN_DOCS
|
4194 | cd6f1169 | bellard | { NULL },
|
4195 | fc01f7e7 | bellard | }; |
4196 | fc01f7e7 | bellard | |
4197 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
4198 | 6a36d84e | bellard | struct soundhw soundhw[] = {
|
4199 | b00052e4 | balrog | #ifdef HAS_AUDIO_CHOICE
|
4200 | 4ce7ff6e | aurel32 | #if defined(TARGET_I386) || defined(TARGET_MIPS)
|
4201 | fd06c375 | bellard | { |
4202 | fd06c375 | bellard | "pcspk",
|
4203 | fd06c375 | bellard | "PC speaker",
|
4204 | fd06c375 | bellard | 0,
|
4205 | fd06c375 | bellard | 1,
|
4206 | fd06c375 | bellard | { .init_isa = pcspk_audio_init } |
4207 | fd06c375 | bellard | }, |
4208 | fd06c375 | bellard | #endif
|
4209 | 4c9b53e3 | malc | |
4210 | 4c9b53e3 | malc | #ifdef CONFIG_SB16
|
4211 | 6a36d84e | bellard | { |
4212 | 6a36d84e | bellard | "sb16",
|
4213 | 6a36d84e | bellard | "Creative Sound Blaster 16",
|
4214 | 6a36d84e | bellard | 0,
|
4215 | 6a36d84e | bellard | 1,
|
4216 | 6a36d84e | bellard | { .init_isa = SB16_init } |
4217 | 6a36d84e | bellard | }, |
4218 | 4c9b53e3 | malc | #endif
|
4219 | 6a36d84e | bellard | |
4220 | cc53d26d | malc | #ifdef CONFIG_CS4231A
|
4221 | cc53d26d | malc | { |
4222 | cc53d26d | malc | "cs4231a",
|
4223 | cc53d26d | malc | "CS4231A",
|
4224 | cc53d26d | malc | 0,
|
4225 | cc53d26d | malc | 1,
|
4226 | cc53d26d | malc | { .init_isa = cs4231a_init } |
4227 | cc53d26d | malc | }, |
4228 | cc53d26d | malc | #endif
|
4229 | cc53d26d | malc | |
4230 | 1d14ffa9 | bellard | #ifdef CONFIG_ADLIB
|
4231 | 6a36d84e | bellard | { |
4232 | 6a36d84e | bellard | "adlib",
|
4233 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
4234 | 6a36d84e | bellard | "Yamaha YMF262 (OPL3)",
|
4235 | 1d14ffa9 | bellard | #else
|
4236 | 6a36d84e | bellard | "Yamaha YM3812 (OPL2)",
|
4237 | 1d14ffa9 | bellard | #endif
|
4238 | 6a36d84e | bellard | 0,
|
4239 | 6a36d84e | bellard | 1,
|
4240 | 6a36d84e | bellard | { .init_isa = Adlib_init } |
4241 | 6a36d84e | bellard | }, |
4242 | 1d14ffa9 | bellard | #endif
|
4243 | 6a36d84e | bellard | |
4244 | 1d14ffa9 | bellard | #ifdef CONFIG_GUS
|
4245 | 6a36d84e | bellard | { |
4246 | 6a36d84e | bellard | "gus",
|
4247 | 6a36d84e | bellard | "Gravis Ultrasound GF1",
|
4248 | 6a36d84e | bellard | 0,
|
4249 | 6a36d84e | bellard | 1,
|
4250 | 6a36d84e | bellard | { .init_isa = GUS_init } |
4251 | 6a36d84e | bellard | }, |
4252 | 1d14ffa9 | bellard | #endif
|
4253 | 6a36d84e | bellard | |
4254 | 4c9b53e3 | malc | #ifdef CONFIG_AC97
|
4255 | e5c9a13e | balrog | { |
4256 | e5c9a13e | balrog | "ac97",
|
4257 | e5c9a13e | balrog | "Intel 82801AA AC97 Audio",
|
4258 | e5c9a13e | balrog | 0,
|
4259 | e5c9a13e | balrog | 0,
|
4260 | e5c9a13e | balrog | { .init_pci = ac97_init } |
4261 | e5c9a13e | balrog | }, |
4262 | 4c9b53e3 | malc | #endif
|
4263 | e5c9a13e | balrog | |
4264 | 4c9b53e3 | malc | #ifdef CONFIG_ES1370
|
4265 | 6a36d84e | bellard | { |
4266 | 6a36d84e | bellard | "es1370",
|
4267 | 6a36d84e | bellard | "ENSONIQ AudioPCI ES1370",
|
4268 | 6a36d84e | bellard | 0,
|
4269 | 6a36d84e | bellard | 0,
|
4270 | 6a36d84e | bellard | { .init_pci = es1370_init } |
4271 | 6a36d84e | bellard | }, |
4272 | b00052e4 | balrog | #endif
|
4273 | 6a36d84e | bellard | |
4274 | 4c9b53e3 | malc | #endif /* HAS_AUDIO_CHOICE */ |
4275 | 4c9b53e3 | malc | |
4276 | 6a36d84e | bellard | { NULL, NULL, 0, 0, { NULL } } |
4277 | 6a36d84e | bellard | }; |
4278 | 6a36d84e | bellard | |
4279 | 6a36d84e | bellard | static void select_soundhw (const char *optarg) |
4280 | 6a36d84e | bellard | { |
4281 | 6a36d84e | bellard | struct soundhw *c;
|
4282 | 6a36d84e | bellard | |
4283 | 6a36d84e | bellard | if (*optarg == '?') { |
4284 | 6a36d84e | bellard | show_valid_cards:
|
4285 | 6a36d84e | bellard | |
4286 | 6a36d84e | bellard | printf ("Valid sound card names (comma separated):\n");
|
4287 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
4288 | 6a36d84e | bellard | printf ("%-11s %s\n", c->name, c->descr);
|
4289 | 6a36d84e | bellard | } |
4290 | 6a36d84e | bellard | printf ("\n-soundhw all will enable all of the above\n");
|
4291 | 1d14ffa9 | bellard | exit (*optarg != '?');
|
4292 | 1d14ffa9 | bellard | } |
4293 | 1d14ffa9 | bellard | else {
|
4294 | 6a36d84e | bellard | size_t l; |
4295 | 1d14ffa9 | bellard | const char *p; |
4296 | 1d14ffa9 | bellard | char *e;
|
4297 | 1d14ffa9 | bellard | int bad_card = 0; |
4298 | 1d14ffa9 | bellard | |
4299 | 6a36d84e | bellard | if (!strcmp (optarg, "all")) { |
4300 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
4301 | 6a36d84e | bellard | c->enabled = 1;
|
4302 | 6a36d84e | bellard | } |
4303 | 6a36d84e | bellard | return;
|
4304 | 6a36d84e | bellard | } |
4305 | 1d14ffa9 | bellard | |
4306 | 6a36d84e | bellard | p = optarg; |
4307 | 1d14ffa9 | bellard | while (*p) {
|
4308 | 1d14ffa9 | bellard | e = strchr (p, ',');
|
4309 | 1d14ffa9 | bellard | l = !e ? strlen (p) : (size_t) (e - p); |
4310 | 6a36d84e | bellard | |
4311 | 6a36d84e | bellard | for (c = soundhw; c->name; ++c) {
|
4312 | b3d6fb4a | malc | if (!strncmp (c->name, p, l) && !c->name[l]) {
|
4313 | 6a36d84e | bellard | c->enabled = 1;
|
4314 | 1d14ffa9 | bellard | break;
|
4315 | 1d14ffa9 | bellard | } |
4316 | 1d14ffa9 | bellard | } |
4317 | 6a36d84e | bellard | |
4318 | 6a36d84e | bellard | if (!c->name) {
|
4319 | 1d14ffa9 | bellard | if (l > 80) { |
4320 | 1d14ffa9 | bellard | fprintf (stderr, |
4321 | 1d14ffa9 | bellard | "Unknown sound card name (too big to show)\n");
|
4322 | 1d14ffa9 | bellard | } |
4323 | 1d14ffa9 | bellard | else {
|
4324 | 1d14ffa9 | bellard | fprintf (stderr, "Unknown sound card name `%.*s'\n",
|
4325 | 1d14ffa9 | bellard | (int) l, p);
|
4326 | 1d14ffa9 | bellard | } |
4327 | 1d14ffa9 | bellard | bad_card = 1;
|
4328 | 1d14ffa9 | bellard | } |
4329 | 1d14ffa9 | bellard | p += l + (e != NULL);
|
4330 | 1d14ffa9 | bellard | } |
4331 | 1d14ffa9 | bellard | |
4332 | 1d14ffa9 | bellard | if (bad_card)
|
4333 | 1d14ffa9 | bellard | goto show_valid_cards;
|
4334 | 1d14ffa9 | bellard | } |
4335 | 1d14ffa9 | bellard | } |
4336 | 1d14ffa9 | bellard | #endif
|
4337 | 1d14ffa9 | bellard | |
4338 | 3893c124 | malc | static void select_vgahw (const char *p) |
4339 | 3893c124 | malc | { |
4340 | 3893c124 | malc | const char *opts; |
4341 | 3893c124 | malc | |
4342 | 86176759 | Zachary Amsden | vga_interface_type = VGA_NONE; |
4343 | 3893c124 | malc | if (strstart(p, "std", &opts)) { |
4344 | 86176759 | Zachary Amsden | vga_interface_type = VGA_STD; |
4345 | 3893c124 | malc | } else if (strstart(p, "cirrus", &opts)) { |
4346 | 86176759 | Zachary Amsden | vga_interface_type = VGA_CIRRUS; |
4347 | 3893c124 | malc | } else if (strstart(p, "vmware", &opts)) { |
4348 | 86176759 | Zachary Amsden | vga_interface_type = VGA_VMWARE; |
4349 | 94909d9f | aliguori | } else if (strstart(p, "xenfb", &opts)) { |
4350 | 86176759 | Zachary Amsden | vga_interface_type = VGA_XENFB; |
4351 | 28b85ed8 | aliguori | } else if (!strstart(p, "none", &opts)) { |
4352 | 3893c124 | malc | invalid_vga:
|
4353 | 3893c124 | malc | fprintf(stderr, "Unknown vga type: %s\n", p);
|
4354 | 3893c124 | malc | exit(1);
|
4355 | 3893c124 | malc | } |
4356 | cb5a7aa8 | malc | while (*opts) {
|
4357 | cb5a7aa8 | malc | const char *nextopt; |
4358 | cb5a7aa8 | malc | |
4359 | cb5a7aa8 | malc | if (strstart(opts, ",retrace=", &nextopt)) { |
4360 | cb5a7aa8 | malc | opts = nextopt; |
4361 | cb5a7aa8 | malc | if (strstart(opts, "dumb", &nextopt)) |
4362 | cb5a7aa8 | malc | vga_retrace_method = VGA_RETRACE_DUMB; |
4363 | cb5a7aa8 | malc | else if (strstart(opts, "precise", &nextopt)) |
4364 | cb5a7aa8 | malc | vga_retrace_method = VGA_RETRACE_PRECISE; |
4365 | cb5a7aa8 | malc | else goto invalid_vga; |
4366 | cb5a7aa8 | malc | } else goto invalid_vga; |
4367 | cb5a7aa8 | malc | opts = nextopt; |
4368 | cb5a7aa8 | malc | } |
4369 | 3893c124 | malc | } |
4370 | 3893c124 | malc | |
4371 | 7d4c3d53 | Markus Armbruster | #ifdef TARGET_I386
|
4372 | 7d4c3d53 | Markus Armbruster | static int balloon_parse(const char *arg) |
4373 | 7d4c3d53 | Markus Armbruster | { |
4374 | 382f0743 | Gerd Hoffmann | QemuOpts *opts; |
4375 | 7d4c3d53 | Markus Armbruster | |
4376 | 382f0743 | Gerd Hoffmann | if (strcmp(arg, "none") == 0) { |
4377 | 382f0743 | Gerd Hoffmann | return 0; |
4378 | 382f0743 | Gerd Hoffmann | } |
4379 | 382f0743 | Gerd Hoffmann | |
4380 | 382f0743 | Gerd Hoffmann | if (!strncmp(arg, "virtio", 6)) { |
4381 | 382f0743 | Gerd Hoffmann | if (arg[6] == ',') { |
4382 | 382f0743 | Gerd Hoffmann | /* have params -> parse them */
|
4383 | 382f0743 | Gerd Hoffmann | opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL); |
4384 | 382f0743 | Gerd Hoffmann | if (!opts)
|
4385 | 382f0743 | Gerd Hoffmann | return -1; |
4386 | 382f0743 | Gerd Hoffmann | } else {
|
4387 | 382f0743 | Gerd Hoffmann | /* create empty opts */
|
4388 | 382f0743 | Gerd Hoffmann | opts = qemu_opts_create(&qemu_device_opts, NULL, 0); |
4389 | 7d4c3d53 | Markus Armbruster | } |
4390 | 382f0743 | Gerd Hoffmann | qemu_opt_set(opts, "driver", "virtio-balloon-pci"); |
4391 | 382f0743 | Gerd Hoffmann | return 0; |
4392 | 7d4c3d53 | Markus Armbruster | } |
4393 | 382f0743 | Gerd Hoffmann | |
4394 | 382f0743 | Gerd Hoffmann | return -1; |
4395 | 7d4c3d53 | Markus Armbruster | } |
4396 | 7d4c3d53 | Markus Armbruster | #endif
|
4397 | 7d4c3d53 | Markus Armbruster | |
4398 | 3587d7e6 | bellard | #ifdef _WIN32
|
4399 | 3587d7e6 | bellard | static BOOL WINAPI qemu_ctrl_handler(DWORD type)
|
4400 | 3587d7e6 | bellard | { |
4401 | 3587d7e6 | bellard | exit(STATUS_CONTROL_C_EXIT); |
4402 | 3587d7e6 | bellard | return TRUE;
|
4403 | 3587d7e6 | bellard | } |
4404 | 3587d7e6 | bellard | #endif
|
4405 | 3587d7e6 | bellard | |
4406 | c4be29ff | aliguori | int qemu_uuid_parse(const char *str, uint8_t *uuid) |
4407 | 8fcb1b90 | blueswir1 | { |
4408 | 8fcb1b90 | blueswir1 | int ret;
|
4409 | 8fcb1b90 | blueswir1 | |
4410 | 8fcb1b90 | blueswir1 | if(strlen(str) != 36) |
4411 | 8fcb1b90 | blueswir1 | return -1; |
4412 | 8fcb1b90 | blueswir1 | |
4413 | 8fcb1b90 | blueswir1 | ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3], |
4414 | 8fcb1b90 | blueswir1 | &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9], |
4415 | 8fcb1b90 | blueswir1 | &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]); |
4416 | 8fcb1b90 | blueswir1 | |
4417 | 8fcb1b90 | blueswir1 | if(ret != 16) |
4418 | 8fcb1b90 | blueswir1 | return -1; |
4419 | 8fcb1b90 | blueswir1 | |
4420 | b6f6e3d3 | aliguori | #ifdef TARGET_I386
|
4421 | b6f6e3d3 | aliguori | smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid); |
4422 | b6f6e3d3 | aliguori | #endif
|
4423 | b6f6e3d3 | aliguori | |
4424 | 8fcb1b90 | blueswir1 | return 0; |
4425 | 8fcb1b90 | blueswir1 | } |
4426 | 8fcb1b90 | blueswir1 | |
4427 | 7c9d8e07 | bellard | #define MAX_NET_CLIENTS 32 |
4428 | c20709aa | bellard | |
4429 | 5b08fc10 | aliguori | #ifndef _WIN32
|
4430 | 5b08fc10 | aliguori | |
4431 | 5b08fc10 | aliguori | static void termsig_handler(int signal) |
4432 | 5b08fc10 | aliguori | { |
4433 | 5b08fc10 | aliguori | qemu_system_shutdown_request(); |
4434 | 5b08fc10 | aliguori | } |
4435 | 5b08fc10 | aliguori | |
4436 | 7c3370d4 | Jan Kiszka | static void sigchld_handler(int signal) |
4437 | 7c3370d4 | Jan Kiszka | { |
4438 | 7c3370d4 | Jan Kiszka | waitpid(-1, NULL, WNOHANG); |
4439 | 7c3370d4 | Jan Kiszka | } |
4440 | 7c3370d4 | Jan Kiszka | |
4441 | 7c3370d4 | Jan Kiszka | static void sighandler_setup(void) |
4442 | 5b08fc10 | aliguori | { |
4443 | 5b08fc10 | aliguori | struct sigaction act;
|
4444 | 5b08fc10 | aliguori | |
4445 | 5b08fc10 | aliguori | memset(&act, 0, sizeof(act)); |
4446 | 5b08fc10 | aliguori | act.sa_handler = termsig_handler; |
4447 | 5b08fc10 | aliguori | sigaction(SIGINT, &act, NULL);
|
4448 | 5b08fc10 | aliguori | sigaction(SIGHUP, &act, NULL);
|
4449 | 5b08fc10 | aliguori | sigaction(SIGTERM, &act, NULL);
|
4450 | 7c3370d4 | Jan Kiszka | |
4451 | 7c3370d4 | Jan Kiszka | act.sa_handler = sigchld_handler; |
4452 | 7c3370d4 | Jan Kiszka | act.sa_flags = SA_NOCLDSTOP; |
4453 | 7c3370d4 | Jan Kiszka | sigaction(SIGCHLD, &act, NULL);
|
4454 | 5b08fc10 | aliguori | } |
4455 | 5b08fc10 | aliguori | |
4456 | 5b08fc10 | aliguori | #endif
|
4457 | 5b08fc10 | aliguori | |
4458 | 5cea8590 | Paul Brook | #ifdef _WIN32
|
4459 | 5cea8590 | Paul Brook | /* Look for support files in the same directory as the executable. */
|
4460 | 5cea8590 | Paul Brook | static char *find_datadir(const char *argv0) |
4461 | 5cea8590 | Paul Brook | { |
4462 | 5cea8590 | Paul Brook | char *p;
|
4463 | 5cea8590 | Paul Brook | char buf[MAX_PATH];
|
4464 | 5cea8590 | Paul Brook | DWORD len; |
4465 | 5cea8590 | Paul Brook | |
4466 | 5cea8590 | Paul Brook | len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); |
4467 | 5cea8590 | Paul Brook | if (len == 0) { |
4468 | c5947808 | Blue Swirl | return NULL; |
4469 | 5cea8590 | Paul Brook | } |
4470 | 5cea8590 | Paul Brook | |
4471 | 5cea8590 | Paul Brook | buf[len] = 0;
|
4472 | 5cea8590 | Paul Brook | p = buf + len - 1;
|
4473 | 5cea8590 | Paul Brook | while (p != buf && *p != '\\') |
4474 | 5cea8590 | Paul Brook | p--; |
4475 | 5cea8590 | Paul Brook | *p = 0;
|
4476 | 5cea8590 | Paul Brook | if (access(buf, R_OK) == 0) { |
4477 | 5cea8590 | Paul Brook | return qemu_strdup(buf);
|
4478 | 5cea8590 | Paul Brook | } |
4479 | 5cea8590 | Paul Brook | return NULL; |
4480 | 5cea8590 | Paul Brook | } |
4481 | 5cea8590 | Paul Brook | #else /* !_WIN32 */ |
4482 | 5cea8590 | Paul Brook | |
4483 | 5cea8590 | Paul Brook | /* Find a likely location for support files using the location of the binary.
|
4484 | 5cea8590 | Paul Brook | For installed binaries this will be "$bindir/../share/qemu". When
|
4485 | 5cea8590 | Paul Brook | running from the build tree this will be "$bindir/../pc-bios". */
|
4486 | 5cea8590 | Paul Brook | #define SHARE_SUFFIX "/share/qemu" |
4487 | 5cea8590 | Paul Brook | #define BUILD_SUFFIX "/pc-bios" |
4488 | 5cea8590 | Paul Brook | static char *find_datadir(const char *argv0) |
4489 | 5cea8590 | Paul Brook | { |
4490 | 5cea8590 | Paul Brook | char *dir;
|
4491 | 5cea8590 | Paul Brook | char *p = NULL; |
4492 | 5cea8590 | Paul Brook | char *res;
|
4493 | 5cea8590 | Paul Brook | char buf[PATH_MAX];
|
4494 | 3a41759d | Blue Swirl | size_t max_len; |
4495 | 5cea8590 | Paul Brook | |
4496 | 5cea8590 | Paul Brook | #if defined(__linux__)
|
4497 | 5cea8590 | Paul Brook | { |
4498 | 5cea8590 | Paul Brook | int len;
|
4499 | 5cea8590 | Paul Brook | len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); |
4500 | 5cea8590 | Paul Brook | if (len > 0) { |
4501 | 5cea8590 | Paul Brook | buf[len] = 0;
|
4502 | 5cea8590 | Paul Brook | p = buf; |
4503 | 5cea8590 | Paul Brook | } |
4504 | 5cea8590 | Paul Brook | } |
4505 | 5cea8590 | Paul Brook | #elif defined(__FreeBSD__)
|
4506 | 5cea8590 | Paul Brook | { |
4507 | 5cea8590 | Paul Brook | int len;
|
4508 | 5cea8590 | Paul Brook | len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1); |
4509 | 5cea8590 | Paul Brook | if (len > 0) { |
4510 | 5cea8590 | Paul Brook | buf[len] = 0;
|
4511 | 5cea8590 | Paul Brook | p = buf; |
4512 | 5cea8590 | Paul Brook | } |
4513 | 5cea8590 | Paul Brook | } |
4514 | 5cea8590 | Paul Brook | #endif
|
4515 | 5cea8590 | Paul Brook | /* If we don't have any way of figuring out the actual executable
|
4516 | 5cea8590 | Paul Brook | location then try argv[0]. */
|
4517 | 5cea8590 | Paul Brook | if (!p) {
|
4518 | 4d224196 | Jean-Christophe DUBOIS | p = realpath(argv0, buf); |
4519 | 5cea8590 | Paul Brook | if (!p) {
|
4520 | 5cea8590 | Paul Brook | return NULL; |
4521 | 5cea8590 | Paul Brook | } |
4522 | 5cea8590 | Paul Brook | } |
4523 | 5cea8590 | Paul Brook | dir = dirname(p); |
4524 | 5cea8590 | Paul Brook | dir = dirname(dir); |
4525 | 5cea8590 | Paul Brook | |
4526 | 3a41759d | Blue Swirl | max_len = strlen(dir) + |
4527 | 3a41759d | Blue Swirl | MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
|
4528 | 3a41759d | Blue Swirl | res = qemu_mallocz(max_len); |
4529 | 3a41759d | Blue Swirl | snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
|
4530 | 5cea8590 | Paul Brook | if (access(res, R_OK)) {
|
4531 | 3a41759d | Blue Swirl | snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
|
4532 | 5cea8590 | Paul Brook | if (access(res, R_OK)) {
|
4533 | 5cea8590 | Paul Brook | qemu_free(res); |
4534 | 5cea8590 | Paul Brook | res = NULL;
|
4535 | 5cea8590 | Paul Brook | } |
4536 | 5cea8590 | Paul Brook | } |
4537 | 4d224196 | Jean-Christophe DUBOIS | |
4538 | 5cea8590 | Paul Brook | return res;
|
4539 | 5cea8590 | Paul Brook | } |
4540 | 5cea8590 | Paul Brook | #undef SHARE_SUFFIX
|
4541 | 5cea8590 | Paul Brook | #undef BUILD_SUFFIX
|
4542 | 5cea8590 | Paul Brook | #endif
|
4543 | 5cea8590 | Paul Brook | |
4544 | 5cea8590 | Paul Brook | char *qemu_find_file(int type, const char *name) |
4545 | 5cea8590 | Paul Brook | { |
4546 | 5cea8590 | Paul Brook | int len;
|
4547 | 5cea8590 | Paul Brook | const char *subdir; |
4548 | 5cea8590 | Paul Brook | char *buf;
|
4549 | 5cea8590 | Paul Brook | |
4550 | 5cea8590 | Paul Brook | /* If name contains path separators then try it as a straight path. */
|
4551 | 5cea8590 | Paul Brook | if ((strchr(name, '/') || strchr(name, '\\')) |
4552 | 5cea8590 | Paul Brook | && access(name, R_OK) == 0) {
|
4553 | 73ffc805 | Jean-Christophe DUBOIS | return qemu_strdup(name);
|
4554 | 5cea8590 | Paul Brook | } |
4555 | 5cea8590 | Paul Brook | switch (type) {
|
4556 | 5cea8590 | Paul Brook | case QEMU_FILE_TYPE_BIOS:
|
4557 | 5cea8590 | Paul Brook | subdir = "";
|
4558 | 5cea8590 | Paul Brook | break;
|
4559 | 5cea8590 | Paul Brook | case QEMU_FILE_TYPE_KEYMAP:
|
4560 | 5cea8590 | Paul Brook | subdir = "keymaps/";
|
4561 | 5cea8590 | Paul Brook | break;
|
4562 | 5cea8590 | Paul Brook | default:
|
4563 | 5cea8590 | Paul Brook | abort(); |
4564 | 5cea8590 | Paul Brook | } |
4565 | 5cea8590 | Paul Brook | len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
|
4566 | 5cea8590 | Paul Brook | buf = qemu_mallocz(len); |
4567 | 3a41759d | Blue Swirl | snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
|
4568 | 5cea8590 | Paul Brook | if (access(buf, R_OK)) {
|
4569 | 5cea8590 | Paul Brook | qemu_free(buf); |
4570 | 5cea8590 | Paul Brook | return NULL; |
4571 | 5cea8590 | Paul Brook | } |
4572 | 5cea8590 | Paul Brook | return buf;
|
4573 | 5cea8590 | Paul Brook | } |
4574 | 5cea8590 | Paul Brook | |
4575 | f31d07d1 | Gerd Hoffmann | static int device_init_func(QemuOpts *opts, void *opaque) |
4576 | f31d07d1 | Gerd Hoffmann | { |
4577 | f31d07d1 | Gerd Hoffmann | DeviceState *dev; |
4578 | f31d07d1 | Gerd Hoffmann | |
4579 | f31d07d1 | Gerd Hoffmann | dev = qdev_device_add(opts); |
4580 | f31d07d1 | Gerd Hoffmann | if (!dev)
|
4581 | f31d07d1 | Gerd Hoffmann | return -1; |
4582 | f31d07d1 | Gerd Hoffmann | return 0; |
4583 | f31d07d1 | Gerd Hoffmann | } |
4584 | f31d07d1 | Gerd Hoffmann | |
4585 | bd3c948d | Gerd Hoffmann | struct device_config {
|
4586 | bd3c948d | Gerd Hoffmann | enum {
|
4587 | bd3c948d | Gerd Hoffmann | DEV_USB, /* -usbdevice */
|
4588 | bd3c948d | Gerd Hoffmann | DEV_BT, /* -bt */
|
4589 | bd3c948d | Gerd Hoffmann | } type; |
4590 | bd3c948d | Gerd Hoffmann | const char *cmdline; |
4591 | 72cf2d4f | Blue Swirl | QTAILQ_ENTRY(device_config) next; |
4592 | bd3c948d | Gerd Hoffmann | }; |
4593 | 72cf2d4f | Blue Swirl | QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs); |
4594 | bd3c948d | Gerd Hoffmann | |
4595 | bd3c948d | Gerd Hoffmann | static void add_device_config(int type, const char *cmdline) |
4596 | bd3c948d | Gerd Hoffmann | { |
4597 | bd3c948d | Gerd Hoffmann | struct device_config *conf;
|
4598 | bd3c948d | Gerd Hoffmann | |
4599 | bd3c948d | Gerd Hoffmann | conf = qemu_mallocz(sizeof(*conf));
|
4600 | bd3c948d | Gerd Hoffmann | conf->type = type; |
4601 | bd3c948d | Gerd Hoffmann | conf->cmdline = cmdline; |
4602 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_TAIL(&device_configs, conf, next); |
4603 | bd3c948d | Gerd Hoffmann | } |
4604 | bd3c948d | Gerd Hoffmann | |
4605 | bd3c948d | Gerd Hoffmann | static int foreach_device_config(int type, int (*func)(const char *cmdline)) |
4606 | bd3c948d | Gerd Hoffmann | { |
4607 | bd3c948d | Gerd Hoffmann | struct device_config *conf;
|
4608 | bd3c948d | Gerd Hoffmann | int rc;
|
4609 | bd3c948d | Gerd Hoffmann | |
4610 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(conf, &device_configs, next) { |
4611 | bd3c948d | Gerd Hoffmann | if (conf->type != type)
|
4612 | bd3c948d | Gerd Hoffmann | continue;
|
4613 | bd3c948d | Gerd Hoffmann | rc = func(conf->cmdline); |
4614 | bd3c948d | Gerd Hoffmann | if (0 != rc) |
4615 | bd3c948d | Gerd Hoffmann | return rc;
|
4616 | bd3c948d | Gerd Hoffmann | } |
4617 | bd3c948d | Gerd Hoffmann | return 0; |
4618 | bd3c948d | Gerd Hoffmann | } |
4619 | bd3c948d | Gerd Hoffmann | |
4620 | 902b3d5c | malc | int main(int argc, char **argv, char **envp) |
4621 | 0824d6fc | bellard | { |
4622 | 59030a8c | aliguori | const char *gdbstub_dev = NULL; |
4623 | 28c5af54 | j_mayer | uint32_t boot_devices_bitmap = 0;
|
4624 | e4bcb14c | ths | int i;
|
4625 | 28c5af54 | j_mayer | int snapshot, linux_boot, net_boot;
|
4626 | 7f7f9873 | bellard | const char *initrd_filename; |
4627 | a20dd508 | bellard | const char *kernel_filename, *kernel_cmdline; |
4628 | ef3adf68 | Jan Kiszka | char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */ |
4629 | 3023f332 | aliguori | DisplayState *ds; |
4630 | 7d957bd8 | aliguori | DisplayChangeListener *dcl; |
4631 | 46d4767d | bellard | int cyls, heads, secs, translation;
|
4632 | fd5f393a | pbrook | const char *net_clients[MAX_NET_CLIENTS]; |
4633 | 7c9d8e07 | bellard | int nb_net_clients;
|
4634 | f31d07d1 | Gerd Hoffmann | QemuOpts *hda_opts = NULL, *opts;
|
4635 | cd6f1169 | bellard | int optind;
|
4636 | cd6f1169 | bellard | const char *r, *optarg; |
4637 | ddd9bbd9 | Jan Kiszka | CharDriverState *monitor_hds[MAX_MONITOR_DEVICES]; |
4638 | ddd9bbd9 | Jan Kiszka | const char *monitor_devices[MAX_MONITOR_DEVICES]; |
4639 | ddd9bbd9 | Jan Kiszka | int monitor_device_index;
|
4640 | fd5f393a | pbrook | const char *serial_devices[MAX_SERIAL_PORTS]; |
4641 | 8d11df9e | bellard | int serial_device_index;
|
4642 | fd5f393a | pbrook | const char *parallel_devices[MAX_PARALLEL_PORTS]; |
4643 | 6508fe59 | bellard | int parallel_device_index;
|
4644 | 9ede2fde | aliguori | const char *virtio_consoles[MAX_VIRTIO_CONSOLES]; |
4645 | 9ede2fde | aliguori | int virtio_console_index;
|
4646 | d63d307f | bellard | const char *loadvm = NULL; |
4647 | cc1daa40 | bellard | QEMUMachine *machine; |
4648 | 94fc95cd | j_mayer | const char *cpu_model; |
4649 | b9e82a59 | blueswir1 | #ifndef _WIN32
|
4650 | 71e3ceb8 | ths | int fds[2]; |
4651 | b9e82a59 | blueswir1 | #endif
|
4652 | 26a5f13b | bellard | int tb_size;
|
4653 | 93815bc2 | ths | const char *pid_file = NULL; |
4654 | 5bb7910a | aliguori | const char *incoming = NULL; |
4655 | b9e82a59 | blueswir1 | #ifndef _WIN32
|
4656 | 54042bcf | aliguori | int fd = 0; |
4657 | 54042bcf | aliguori | struct passwd *pwd = NULL; |
4658 | 0858532e | aliguori | const char *chroot_dir = NULL; |
4659 | 0858532e | aliguori | const char *run_as = NULL; |
4660 | b9e82a59 | blueswir1 | #endif
|
4661 | 268a362c | aliguori | CPUState *env; |
4662 | 993fbfdb | Anthony Liguori | int show_vnc_port = 0; |
4663 | 0bd48850 | bellard | |
4664 | ac7531ec | Gerd Hoffmann | qemu_errors_to_file(stderr); |
4665 | 902b3d5c | malc | qemu_cache_utils_init(envp); |
4666 | 902b3d5c | malc | |
4667 | 72cf2d4f | Blue Swirl | QLIST_INIT (&vm_change_state_head); |
4668 | be995c27 | bellard | #ifndef _WIN32
|
4669 | be995c27 | bellard | { |
4670 | be995c27 | bellard | struct sigaction act;
|
4671 | be995c27 | bellard | sigfillset(&act.sa_mask); |
4672 | be995c27 | bellard | act.sa_flags = 0;
|
4673 | be995c27 | bellard | act.sa_handler = SIG_IGN; |
4674 | be995c27 | bellard | sigaction(SIGPIPE, &act, NULL);
|
4675 | be995c27 | bellard | } |
4676 | 3587d7e6 | bellard | #else
|
4677 | 3587d7e6 | bellard | SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
4678 | a8e5ac33 | bellard | /* Note: cpu_interrupt() is currently not SMP safe, so we force
|
4679 | a8e5ac33 | bellard | QEMU to run on a single CPU */
|
4680 | a8e5ac33 | bellard | { |
4681 | a8e5ac33 | bellard | HANDLE h; |
4682 | a8e5ac33 | bellard | DWORD mask, smask; |
4683 | a8e5ac33 | bellard | int i;
|
4684 | a8e5ac33 | bellard | h = GetCurrentProcess(); |
4685 | a8e5ac33 | bellard | if (GetProcessAffinityMask(h, &mask, &smask)) {
|
4686 | a8e5ac33 | bellard | for(i = 0; i < 32; i++) { |
4687 | a8e5ac33 | bellard | if (mask & (1 << i)) |
4688 | a8e5ac33 | bellard | break;
|
4689 | a8e5ac33 | bellard | } |
4690 | a8e5ac33 | bellard | if (i != 32) { |
4691 | a8e5ac33 | bellard | mask = 1 << i;
|
4692 | a8e5ac33 | bellard | SetProcessAffinityMask(h, mask); |
4693 | a8e5ac33 | bellard | } |
4694 | a8e5ac33 | bellard | } |
4695 | a8e5ac33 | bellard | } |
4696 | 67b915a5 | bellard | #endif
|
4697 | be995c27 | bellard | |
4698 | f80f9ec9 | Anthony Liguori | module_call_init(MODULE_INIT_MACHINE); |
4699 | 0c257437 | Anthony Liguori | machine = find_default_machine(); |
4700 | 94fc95cd | j_mayer | cpu_model = NULL;
|
4701 | fc01f7e7 | bellard | initrd_filename = NULL;
|
4702 | 4fc5d071 | aurel32 | ram_size = 0;
|
4703 | 33e3963e | bellard | snapshot = 0;
|
4704 | a20dd508 | bellard | kernel_filename = NULL;
|
4705 | a20dd508 | bellard | kernel_cmdline = "";
|
4706 | c4b1fcc0 | bellard | cyls = heads = secs = 0;
|
4707 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_AUTO; |
4708 | c4b1fcc0 | bellard | |
4709 | c75a823c | aurel32 | serial_devices[0] = "vc:80Cx24C"; |
4710 | 8d11df9e | bellard | for(i = 1; i < MAX_SERIAL_PORTS; i++) |
4711 | fd5f393a | pbrook | serial_devices[i] = NULL;
|
4712 | 8d11df9e | bellard | serial_device_index = 0;
|
4713 | 3b46e624 | ths | |
4714 | 8290edda | aliguori | parallel_devices[0] = "vc:80Cx24C"; |
4715 | 6508fe59 | bellard | for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
4716 | fd5f393a | pbrook | parallel_devices[i] = NULL;
|
4717 | 6508fe59 | bellard | parallel_device_index = 0;
|
4718 | 3b46e624 | ths | |
4719 | 1b8fc811 | aliguori | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) |
4720 | 9ede2fde | aliguori | virtio_consoles[i] = NULL;
|
4721 | 9ede2fde | aliguori | virtio_console_index = 0;
|
4722 | 9ede2fde | aliguori | |
4723 | ddd9bbd9 | Jan Kiszka | monitor_devices[0] = "vc:80Cx24C"; |
4724 | ddd9bbd9 | Jan Kiszka | for (i = 1; i < MAX_MONITOR_DEVICES; i++) { |
4725 | ddd9bbd9 | Jan Kiszka | monitor_devices[i] = NULL;
|
4726 | ddd9bbd9 | Jan Kiszka | } |
4727 | ddd9bbd9 | Jan Kiszka | monitor_device_index = 0;
|
4728 | ddd9bbd9 | Jan Kiszka | |
4729 | 268a362c | aliguori | for (i = 0; i < MAX_NODES; i++) { |
4730 | 268a362c | aliguori | node_mem[i] = 0;
|
4731 | 268a362c | aliguori | node_cpumask[i] = 0;
|
4732 | 268a362c | aliguori | } |
4733 | 268a362c | aliguori | |
4734 | 7c9d8e07 | bellard | nb_net_clients = 0;
|
4735 | 268a362c | aliguori | nb_numa_nodes = 0;
|
4736 | 7c9d8e07 | bellard | nb_nics = 0;
|
4737 | 3b46e624 | ths | |
4738 | 26a5f13b | bellard | tb_size = 0;
|
4739 | 41bd639b | blueswir1 | autostart= 1;
|
4740 | 41bd639b | blueswir1 | |
4741 | cd6f1169 | bellard | optind = 1;
|
4742 | 0824d6fc | bellard | for(;;) {
|
4743 | cd6f1169 | bellard | if (optind >= argc)
|
4744 | 0824d6fc | bellard | break;
|
4745 | cd6f1169 | bellard | r = argv[optind]; |
4746 | cd6f1169 | bellard | if (r[0] != '-') { |
4747 | 9dfd7c7a | Gerd Hoffmann | hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
|
4748 | cd6f1169 | bellard | } else {
|
4749 | cd6f1169 | bellard | const QEMUOption *popt;
|
4750 | cd6f1169 | bellard | |
4751 | cd6f1169 | bellard | optind++; |
4752 | dff5efc8 | pbrook | /* Treat --foo the same as -foo. */
|
4753 | dff5efc8 | pbrook | if (r[1] == '-') |
4754 | dff5efc8 | pbrook | r++; |
4755 | cd6f1169 | bellard | popt = qemu_options; |
4756 | cd6f1169 | bellard | for(;;) {
|
4757 | cd6f1169 | bellard | if (!popt->name) {
|
4758 | 5fafdf24 | ths | fprintf(stderr, "%s: invalid option -- '%s'\n",
|
4759 | cd6f1169 | bellard | argv[0], r);
|
4760 | cd6f1169 | bellard | exit(1);
|
4761 | cd6f1169 | bellard | } |
4762 | cd6f1169 | bellard | if (!strcmp(popt->name, r + 1)) |
4763 | cd6f1169 | bellard | break;
|
4764 | cd6f1169 | bellard | popt++; |
4765 | cd6f1169 | bellard | } |
4766 | cd6f1169 | bellard | if (popt->flags & HAS_ARG) {
|
4767 | cd6f1169 | bellard | if (optind >= argc) {
|
4768 | cd6f1169 | bellard | fprintf(stderr, "%s: option '%s' requires an argument\n",
|
4769 | cd6f1169 | bellard | argv[0], r);
|
4770 | cd6f1169 | bellard | exit(1);
|
4771 | cd6f1169 | bellard | } |
4772 | cd6f1169 | bellard | optarg = argv[optind++]; |
4773 | cd6f1169 | bellard | } else {
|
4774 | cd6f1169 | bellard | optarg = NULL;
|
4775 | cd6f1169 | bellard | } |
4776 | cd6f1169 | bellard | |
4777 | cd6f1169 | bellard | switch(popt->index) {
|
4778 | cc1daa40 | bellard | case QEMU_OPTION_M:
|
4779 | cc1daa40 | bellard | machine = find_machine(optarg); |
4780 | cc1daa40 | bellard | if (!machine) {
|
4781 | cc1daa40 | bellard | QEMUMachine *m; |
4782 | cc1daa40 | bellard | printf("Supported machines are:\n");
|
4783 | cc1daa40 | bellard | for(m = first_machine; m != NULL; m = m->next) { |
4784 | 3f6599e6 | Mark McLoughlin | if (m->alias)
|
4785 | 3f6599e6 | Mark McLoughlin | printf("%-10s %s (alias of %s)\n",
|
4786 | 3f6599e6 | Mark McLoughlin | m->alias, m->desc, m->name); |
4787 | cc1daa40 | bellard | printf("%-10s %s%s\n",
|
4788 | 5fafdf24 | ths | m->name, m->desc, |
4789 | 0c257437 | Anthony Liguori | m->is_default ? " (default)" : ""); |
4790 | cc1daa40 | bellard | } |
4791 | 15f82208 | ths | exit(*optarg != '?');
|
4792 | cc1daa40 | bellard | } |
4793 | cc1daa40 | bellard | break;
|
4794 | 94fc95cd | j_mayer | case QEMU_OPTION_cpu:
|
4795 | 94fc95cd | j_mayer | /* hw initialization will check this */
|
4796 | 15f82208 | ths | if (*optarg == '?') { |
4797 | c732abe2 | j_mayer | /* XXX: implement xxx_cpu_list for targets that still miss it */
|
4798 | c732abe2 | j_mayer | #if defined(cpu_list)
|
4799 | c732abe2 | j_mayer | cpu_list(stdout, &fprintf); |
4800 | 94fc95cd | j_mayer | #endif
|
4801 | 15f82208 | ths | exit(0);
|
4802 | 94fc95cd | j_mayer | } else {
|
4803 | 94fc95cd | j_mayer | cpu_model = optarg; |
4804 | 94fc95cd | j_mayer | } |
4805 | 94fc95cd | j_mayer | break;
|
4806 | cd6f1169 | bellard | case QEMU_OPTION_initrd:
|
4807 | fc01f7e7 | bellard | initrd_filename = optarg; |
4808 | fc01f7e7 | bellard | break;
|
4809 | cd6f1169 | bellard | case QEMU_OPTION_hda:
|
4810 | e4bcb14c | ths | if (cyls == 0) |
4811 | 9dfd7c7a | Gerd Hoffmann | hda_opts = drive_add(optarg, HD_ALIAS, 0);
|
4812 | e4bcb14c | ths | else
|
4813 | 9dfd7c7a | Gerd Hoffmann | hda_opts = drive_add(optarg, HD_ALIAS |
4814 | e4bcb14c | ths | ",cyls=%d,heads=%d,secs=%d%s",
|
4815 | 609497ab | balrog | 0, cyls, heads, secs,
|
4816 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_LBA ? |
4817 | e4bcb14c | ths | ",trans=lba" :
|
4818 | e4bcb14c | ths | translation == BIOS_ATA_TRANSLATION_NONE ? |
4819 | e4bcb14c | ths | ",trans=none" : ""); |
4820 | e4bcb14c | ths | break;
|
4821 | cd6f1169 | bellard | case QEMU_OPTION_hdb:
|
4822 | cc1daa40 | bellard | case QEMU_OPTION_hdc:
|
4823 | cc1daa40 | bellard | case QEMU_OPTION_hdd:
|
4824 | 609497ab | balrog | drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
4825 | fc01f7e7 | bellard | break;
|
4826 | e4bcb14c | ths | case QEMU_OPTION_drive:
|
4827 | 609497ab | balrog | drive_add(NULL, "%s", optarg); |
4828 | e4bcb14c | ths | break;
|
4829 | d058fe03 | Gerd Hoffmann | case QEMU_OPTION_set:
|
4830 | d058fe03 | Gerd Hoffmann | if (qemu_set_option(optarg) != 0) |
4831 | d058fe03 | Gerd Hoffmann | exit(1);
|
4832 | d058fe03 | Gerd Hoffmann | break;
|
4833 | 3e3d5815 | balrog | case QEMU_OPTION_mtdblock:
|
4834 | 609497ab | balrog | drive_add(optarg, MTD_ALIAS); |
4835 | 3e3d5815 | balrog | break;
|
4836 | a1bb27b1 | pbrook | case QEMU_OPTION_sd:
|
4837 | 609497ab | balrog | drive_add(optarg, SD_ALIAS); |
4838 | a1bb27b1 | pbrook | break;
|
4839 | 86f55663 | j_mayer | case QEMU_OPTION_pflash:
|
4840 | 609497ab | balrog | drive_add(optarg, PFLASH_ALIAS); |
4841 | 86f55663 | j_mayer | break;
|
4842 | cd6f1169 | bellard | case QEMU_OPTION_snapshot:
|
4843 | 33e3963e | bellard | snapshot = 1;
|
4844 | 33e3963e | bellard | break;
|
4845 | cd6f1169 | bellard | case QEMU_OPTION_hdachs:
|
4846 | 330d0414 | bellard | { |
4847 | 330d0414 | bellard | const char *p; |
4848 | 330d0414 | bellard | p = optarg; |
4849 | 330d0414 | bellard | cyls = strtol(p, (char **)&p, 0); |
4850 | 46d4767d | bellard | if (cyls < 1 || cyls > 16383) |
4851 | 46d4767d | bellard | goto chs_fail;
|
4852 | 330d0414 | bellard | if (*p != ',') |
4853 | 330d0414 | bellard | goto chs_fail;
|
4854 | 330d0414 | bellard | p++; |
4855 | 330d0414 | bellard | heads = strtol(p, (char **)&p, 0); |
4856 | 46d4767d | bellard | if (heads < 1 || heads > 16) |
4857 | 46d4767d | bellard | goto chs_fail;
|
4858 | 330d0414 | bellard | if (*p != ',') |
4859 | 330d0414 | bellard | goto chs_fail;
|
4860 | 330d0414 | bellard | p++; |
4861 | 330d0414 | bellard | secs = strtol(p, (char **)&p, 0); |
4862 | 46d4767d | bellard | if (secs < 1 || secs > 63) |
4863 | 46d4767d | bellard | goto chs_fail;
|
4864 | 46d4767d | bellard | if (*p == ',') { |
4865 | 46d4767d | bellard | p++; |
4866 | 46d4767d | bellard | if (!strcmp(p, "none")) |
4867 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_NONE; |
4868 | 46d4767d | bellard | else if (!strcmp(p, "lba")) |
4869 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_LBA; |
4870 | 46d4767d | bellard | else if (!strcmp(p, "auto")) |
4871 | 46d4767d | bellard | translation = BIOS_ATA_TRANSLATION_AUTO; |
4872 | 46d4767d | bellard | else
|
4873 | 46d4767d | bellard | goto chs_fail;
|
4874 | 46d4767d | bellard | } else if (*p != '\0') { |
4875 | c4b1fcc0 | bellard | chs_fail:
|
4876 | 46d4767d | bellard | fprintf(stderr, "qemu: invalid physical CHS format\n");
|
4877 | 46d4767d | bellard | exit(1);
|
4878 | c4b1fcc0 | bellard | } |
4879 | 9dfd7c7a | Gerd Hoffmann | if (hda_opts != NULL) { |
4880 | 9dfd7c7a | Gerd Hoffmann | char num[16]; |
4881 | 9dfd7c7a | Gerd Hoffmann | snprintf(num, sizeof(num), "%d", cyls); |
4882 | 9dfd7c7a | Gerd Hoffmann | qemu_opt_set(hda_opts, "cyls", num);
|
4883 | 9dfd7c7a | Gerd Hoffmann | snprintf(num, sizeof(num), "%d", heads); |
4884 | 9dfd7c7a | Gerd Hoffmann | qemu_opt_set(hda_opts, "heads", num);
|
4885 | 9dfd7c7a | Gerd Hoffmann | snprintf(num, sizeof(num), "%d", secs); |
4886 | 9dfd7c7a | Gerd Hoffmann | qemu_opt_set(hda_opts, "secs", num);
|
4887 | 9dfd7c7a | Gerd Hoffmann | if (translation == BIOS_ATA_TRANSLATION_LBA)
|
4888 | 9dfd7c7a | Gerd Hoffmann | qemu_opt_set(hda_opts, "trans", "lba"); |
4889 | 9dfd7c7a | Gerd Hoffmann | if (translation == BIOS_ATA_TRANSLATION_NONE)
|
4890 | 9dfd7c7a | Gerd Hoffmann | qemu_opt_set(hda_opts, "trans", "none"); |
4891 | 9dfd7c7a | Gerd Hoffmann | } |
4892 | 330d0414 | bellard | } |
4893 | 330d0414 | bellard | break;
|
4894 | 268a362c | aliguori | case QEMU_OPTION_numa:
|
4895 | 268a362c | aliguori | if (nb_numa_nodes >= MAX_NODES) {
|
4896 | 268a362c | aliguori | fprintf(stderr, "qemu: too many NUMA nodes\n");
|
4897 | 268a362c | aliguori | exit(1);
|
4898 | 268a362c | aliguori | } |
4899 | 268a362c | aliguori | numa_add(optarg); |
4900 | 268a362c | aliguori | break;
|
4901 | cd6f1169 | bellard | case QEMU_OPTION_nographic:
|
4902 | 993fbfdb | Anthony Liguori | display_type = DT_NOGRAPHIC; |
4903 | a20dd508 | bellard | break;
|
4904 | 4d3b6f6e | balrog | #ifdef CONFIG_CURSES
|
4905 | 4d3b6f6e | balrog | case QEMU_OPTION_curses:
|
4906 | 993fbfdb | Anthony Liguori | display_type = DT_CURSES; |
4907 | 4d3b6f6e | balrog | break;
|
4908 | 4d3b6f6e | balrog | #endif
|
4909 | a171fe39 | balrog | case QEMU_OPTION_portrait:
|
4910 | a171fe39 | balrog | graphic_rotate = 1;
|
4911 | a171fe39 | balrog | break;
|
4912 | cd6f1169 | bellard | case QEMU_OPTION_kernel:
|
4913 | a20dd508 | bellard | kernel_filename = optarg; |
4914 | a20dd508 | bellard | break;
|
4915 | cd6f1169 | bellard | case QEMU_OPTION_append:
|
4916 | a20dd508 | bellard | kernel_cmdline = optarg; |
4917 | 313aa567 | bellard | break;
|
4918 | cd6f1169 | bellard | case QEMU_OPTION_cdrom:
|
4919 | 609497ab | balrog | drive_add(optarg, CDROM_ALIAS); |
4920 | 36b486bb | bellard | break;
|
4921 | cd6f1169 | bellard | case QEMU_OPTION_boot:
|
4922 | 28c5af54 | j_mayer | { |
4923 | ef3adf68 | Jan Kiszka | static const char * const params[] = { |
4924 | 95387491 | Jan Kiszka | "order", "once", "menu", NULL |
4925 | ef3adf68 | Jan Kiszka | }; |
4926 | ef3adf68 | Jan Kiszka | char buf[sizeof(boot_devices)]; |
4927 | e0f084bf | Jan Kiszka | char *standard_boot_devices;
|
4928 | ef3adf68 | Jan Kiszka | int legacy = 0; |
4929 | ef3adf68 | Jan Kiszka | |
4930 | ef3adf68 | Jan Kiszka | if (!strchr(optarg, '=')) { |
4931 | ef3adf68 | Jan Kiszka | legacy = 1;
|
4932 | ef3adf68 | Jan Kiszka | pstrcpy(buf, sizeof(buf), optarg);
|
4933 | ef3adf68 | Jan Kiszka | } else if (check_params(buf, sizeof(buf), params, optarg) < 0) { |
4934 | ef3adf68 | Jan Kiszka | fprintf(stderr, |
4935 | ef3adf68 | Jan Kiszka | "qemu: unknown boot parameter '%s' in '%s'\n",
|
4936 | ef3adf68 | Jan Kiszka | buf, optarg); |
4937 | ef3adf68 | Jan Kiszka | exit(1);
|
4938 | ef3adf68 | Jan Kiszka | } |
4939 | ef3adf68 | Jan Kiszka | |
4940 | ef3adf68 | Jan Kiszka | if (legacy ||
|
4941 | ef3adf68 | Jan Kiszka | get_param_value(buf, sizeof(buf), "order", optarg)) { |
4942 | ef3adf68 | Jan Kiszka | boot_devices_bitmap = parse_bootdevices(buf); |
4943 | ef3adf68 | Jan Kiszka | pstrcpy(boot_devices, sizeof(boot_devices), buf);
|
4944 | 28c5af54 | j_mayer | } |
4945 | e0f084bf | Jan Kiszka | if (!legacy) {
|
4946 | e0f084bf | Jan Kiszka | if (get_param_value(buf, sizeof(buf), |
4947 | e0f084bf | Jan Kiszka | "once", optarg)) {
|
4948 | e0f084bf | Jan Kiszka | boot_devices_bitmap |= parse_bootdevices(buf); |
4949 | e0f084bf | Jan Kiszka | standard_boot_devices = qemu_strdup(boot_devices); |
4950 | e0f084bf | Jan Kiszka | pstrcpy(boot_devices, sizeof(boot_devices), buf);
|
4951 | e0f084bf | Jan Kiszka | qemu_register_reset(restore_boot_devices, |
4952 | e0f084bf | Jan Kiszka | standard_boot_devices); |
4953 | e0f084bf | Jan Kiszka | } |
4954 | 95387491 | Jan Kiszka | if (get_param_value(buf, sizeof(buf), |
4955 | 95387491 | Jan Kiszka | "menu", optarg)) {
|
4956 | 95387491 | Jan Kiszka | if (!strcmp(buf, "on")) { |
4957 | 95387491 | Jan Kiszka | boot_menu = 1;
|
4958 | 95387491 | Jan Kiszka | } else if (!strcmp(buf, "off")) { |
4959 | 95387491 | Jan Kiszka | boot_menu = 0;
|
4960 | 95387491 | Jan Kiszka | } else {
|
4961 | 95387491 | Jan Kiszka | fprintf(stderr, |
4962 | 95387491 | Jan Kiszka | "qemu: invalid option value '%s'\n",
|
4963 | 95387491 | Jan Kiszka | buf); |
4964 | 95387491 | Jan Kiszka | exit(1);
|
4965 | 95387491 | Jan Kiszka | } |
4966 | 95387491 | Jan Kiszka | } |
4967 | e0f084bf | Jan Kiszka | } |
4968 | 36b486bb | bellard | } |
4969 | 36b486bb | bellard | break;
|
4970 | cd6f1169 | bellard | case QEMU_OPTION_fda:
|
4971 | cd6f1169 | bellard | case QEMU_OPTION_fdb:
|
4972 | 609497ab | balrog | drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
4973 | c45886db | bellard | break;
|
4974 | 52ca8d6a | bellard | #ifdef TARGET_I386
|
4975 | 52ca8d6a | bellard | case QEMU_OPTION_no_fd_bootchk:
|
4976 | 52ca8d6a | bellard | fd_bootchk = 0;
|
4977 | 52ca8d6a | bellard | break;
|
4978 | 52ca8d6a | bellard | #endif
|
4979 | 7c9d8e07 | bellard | case QEMU_OPTION_net:
|
4980 | 7c9d8e07 | bellard | if (nb_net_clients >= MAX_NET_CLIENTS) {
|
4981 | 7c9d8e07 | bellard | fprintf(stderr, "qemu: too many network clients\n");
|
4982 | c4b1fcc0 | bellard | exit(1);
|
4983 | c4b1fcc0 | bellard | } |
4984 | fd5f393a | pbrook | net_clients[nb_net_clients] = optarg; |
4985 | 7c9d8e07 | bellard | nb_net_clients++; |
4986 | 702c651c | bellard | break;
|
4987 | c7f74643 | bellard | #ifdef CONFIG_SLIRP
|
4988 | c7f74643 | bellard | case QEMU_OPTION_tftp:
|
4989 | ad196a9d | Jan Kiszka | legacy_tftp_prefix = optarg; |
4990 | 9bf05444 | bellard | break;
|
4991 | 47d5d01a | ths | case QEMU_OPTION_bootp:
|
4992 | ad196a9d | Jan Kiszka | legacy_bootp_filename = optarg; |
4993 | 47d5d01a | ths | break;
|
4994 | c94c8d64 | bellard | #ifndef _WIN32
|
4995 | 9d728e8c | bellard | case QEMU_OPTION_smb:
|
4996 | ad196a9d | Jan Kiszka | net_slirp_smb(optarg); |
4997 | 9d728e8c | bellard | break;
|
4998 | c94c8d64 | bellard | #endif
|
4999 | 9bf05444 | bellard | case QEMU_OPTION_redir:
|
5000 | f3546deb | Jan Kiszka | net_slirp_redir(optarg); |
5001 | 9bf05444 | bellard | break;
|
5002 | c7f74643 | bellard | #endif
|
5003 | dc72ac14 | balrog | case QEMU_OPTION_bt:
|
5004 | bd3c948d | Gerd Hoffmann | add_device_config(DEV_BT, optarg); |
5005 | dc72ac14 | balrog | break;
|
5006 | 1d14ffa9 | bellard | #ifdef HAS_AUDIO
|
5007 | 1d14ffa9 | bellard | case QEMU_OPTION_audio_help:
|
5008 | 1d14ffa9 | bellard | AUD_help (); |
5009 | 1d14ffa9 | bellard | exit (0);
|
5010 | 1d14ffa9 | bellard | break;
|
5011 | 1d14ffa9 | bellard | case QEMU_OPTION_soundhw:
|
5012 | 1d14ffa9 | bellard | select_soundhw (optarg); |
5013 | 1d14ffa9 | bellard | break;
|
5014 | 1d14ffa9 | bellard | #endif
|
5015 | cd6f1169 | bellard | case QEMU_OPTION_h:
|
5016 | 15f82208 | ths | help(0);
|
5017 | cd6f1169 | bellard | break;
|
5018 | 9bd7e6d9 | pbrook | case QEMU_OPTION_version:
|
5019 | 9bd7e6d9 | pbrook | version(); |
5020 | 9bd7e6d9 | pbrook | exit(0);
|
5021 | 9bd7e6d9 | pbrook | break;
|
5022 | 00f82b8a | aurel32 | case QEMU_OPTION_m: {
|
5023 | 00f82b8a | aurel32 | uint64_t value; |
5024 | 00f82b8a | aurel32 | char *ptr;
|
5025 | 00f82b8a | aurel32 | |
5026 | 00f82b8a | aurel32 | value = strtoul(optarg, &ptr, 10);
|
5027 | 00f82b8a | aurel32 | switch (*ptr) {
|
5028 | 00f82b8a | aurel32 | case 0: case 'M': case 'm': |
5029 | 00f82b8a | aurel32 | value <<= 20;
|
5030 | 00f82b8a | aurel32 | break;
|
5031 | 00f82b8a | aurel32 | case 'G': case 'g': |
5032 | 00f82b8a | aurel32 | value <<= 30;
|
5033 | 00f82b8a | aurel32 | break;
|
5034 | 00f82b8a | aurel32 | default:
|
5035 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
|
5036 | cd6f1169 | bellard | exit(1);
|
5037 | cd6f1169 | bellard | } |
5038 | 00f82b8a | aurel32 | |
5039 | 00f82b8a | aurel32 | /* On 32-bit hosts, QEMU is limited by virtual address space */
|
5040 | 4a1418e0 | Anthony Liguori | if (value > (2047 << 20) && HOST_LONG_BITS == 32) { |
5041 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
|
5042 | 00f82b8a | aurel32 | exit(1);
|
5043 | 00f82b8a | aurel32 | } |
5044 | c227f099 | Anthony Liguori | if (value != (uint64_t)(ram_addr_t)value) {
|
5045 | 00f82b8a | aurel32 | fprintf(stderr, "qemu: ram size too large\n");
|
5046 | 00f82b8a | aurel32 | exit(1);
|
5047 | 00f82b8a | aurel32 | } |
5048 | 00f82b8a | aurel32 | ram_size = value; |
5049 | cd6f1169 | bellard | break;
|
5050 | 00f82b8a | aurel32 | } |
5051 | cd6f1169 | bellard | case QEMU_OPTION_d:
|
5052 | cd6f1169 | bellard | { |
5053 | cd6f1169 | bellard | int mask;
|
5054 | c7cd6a37 | blueswir1 | const CPULogItem *item;
|
5055 | 3b46e624 | ths | |
5056 | cd6f1169 | bellard | mask = cpu_str_to_log_mask(optarg); |
5057 | cd6f1169 | bellard | if (!mask) {
|
5058 | cd6f1169 | bellard | printf("Log items (comma separated):\n");
|
5059 | f193c797 | bellard | for(item = cpu_log_items; item->mask != 0; item++) { |
5060 | f193c797 | bellard | printf("%-10s %s\n", item->name, item->help);
|
5061 | f193c797 | bellard | } |
5062 | f193c797 | bellard | exit(1);
|
5063 | cd6f1169 | bellard | } |
5064 | cd6f1169 | bellard | cpu_set_log(mask); |
5065 | f193c797 | bellard | } |
5066 | cd6f1169 | bellard | break;
|
5067 | cd6f1169 | bellard | case QEMU_OPTION_s:
|
5068 | 59030a8c | aliguori | gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
|
5069 | cd6f1169 | bellard | break;
|
5070 | 59030a8c | aliguori | case QEMU_OPTION_gdb:
|
5071 | 59030a8c | aliguori | gdbstub_dev = optarg; |
5072 | cd6f1169 | bellard | break;
|
5073 | cd6f1169 | bellard | case QEMU_OPTION_L:
|
5074 | 5cea8590 | Paul Brook | data_dir = optarg; |
5075 | cd6f1169 | bellard | break;
|
5076 | 1192dad8 | j_mayer | case QEMU_OPTION_bios:
|
5077 | 1192dad8 | j_mayer | bios_name = optarg; |
5078 | 1192dad8 | j_mayer | break;
|
5079 | 1b530a6d | aurel32 | case QEMU_OPTION_singlestep:
|
5080 | 1b530a6d | aurel32 | singlestep = 1;
|
5081 | 1b530a6d | aurel32 | break;
|
5082 | cd6f1169 | bellard | case QEMU_OPTION_S:
|
5083 | 3c07f8e8 | pbrook | autostart = 0;
|
5084 | cd6f1169 | bellard | break;
|
5085 | 5824d651 | blueswir1 | #ifndef _WIN32
|
5086 | 3d11d0eb | bellard | case QEMU_OPTION_k:
|
5087 | 3d11d0eb | bellard | keyboard_layout = optarg; |
5088 | 3d11d0eb | bellard | break;
|
5089 | 5824d651 | blueswir1 | #endif
|
5090 | ee22c2f7 | bellard | case QEMU_OPTION_localtime:
|
5091 | ee22c2f7 | bellard | rtc_utc = 0;
|
5092 | ee22c2f7 | bellard | break;
|
5093 | 3893c124 | malc | case QEMU_OPTION_vga:
|
5094 | 3893c124 | malc | select_vgahw (optarg); |
5095 | 1bfe856e | bellard | break;
|
5096 | 5824d651 | blueswir1 | #if defined(TARGET_PPC) || defined(TARGET_SPARC)
|
5097 | e9b137c2 | bellard | case QEMU_OPTION_g:
|
5098 | e9b137c2 | bellard | { |
5099 | e9b137c2 | bellard | const char *p; |
5100 | e9b137c2 | bellard | int w, h, depth;
|
5101 | e9b137c2 | bellard | p = optarg; |
5102 | e9b137c2 | bellard | w = strtol(p, (char **)&p, 10); |
5103 | e9b137c2 | bellard | if (w <= 0) { |
5104 | e9b137c2 | bellard | graphic_error:
|
5105 | e9b137c2 | bellard | fprintf(stderr, "qemu: invalid resolution or depth\n");
|
5106 | e9b137c2 | bellard | exit(1);
|
5107 | e9b137c2 | bellard | } |
5108 | e9b137c2 | bellard | if (*p != 'x') |
5109 | e9b137c2 | bellard | goto graphic_error;
|
5110 | e9b137c2 | bellard | p++; |
5111 | e9b137c2 | bellard | h = strtol(p, (char **)&p, 10); |
5112 | e9b137c2 | bellard | if (h <= 0) |
5113 | e9b137c2 | bellard | goto graphic_error;
|
5114 | e9b137c2 | bellard | if (*p == 'x') { |
5115 | e9b137c2 | bellard | p++; |
5116 | e9b137c2 | bellard | depth = strtol(p, (char **)&p, 10); |
5117 | 5fafdf24 | ths | if (depth != 8 && depth != 15 && depth != 16 && |
5118 | e9b137c2 | bellard | depth != 24 && depth != 32) |
5119 | e9b137c2 | bellard | goto graphic_error;
|
5120 | e9b137c2 | bellard | } else if (*p == '\0') { |
5121 | e9b137c2 | bellard | depth = graphic_depth; |
5122 | e9b137c2 | bellard | } else {
|
5123 | e9b137c2 | bellard | goto graphic_error;
|
5124 | e9b137c2 | bellard | } |
5125 | 3b46e624 | ths | |
5126 | e9b137c2 | bellard | graphic_width = w; |
5127 | e9b137c2 | bellard | graphic_height = h; |
5128 | e9b137c2 | bellard | graphic_depth = depth; |
5129 | e9b137c2 | bellard | } |
5130 | e9b137c2 | bellard | break;
|
5131 | 5824d651 | blueswir1 | #endif
|
5132 | 20d8a3ed | ths | case QEMU_OPTION_echr:
|
5133 | 20d8a3ed | ths | { |
5134 | 20d8a3ed | ths | char *r;
|
5135 | 20d8a3ed | ths | term_escape_char = strtol(optarg, &r, 0);
|
5136 | 20d8a3ed | ths | if (r == optarg)
|
5137 | 20d8a3ed | ths | printf("Bad argument to echr\n");
|
5138 | 20d8a3ed | ths | break;
|
5139 | 20d8a3ed | ths | } |
5140 | 82c643ff | bellard | case QEMU_OPTION_monitor:
|
5141 | ddd9bbd9 | Jan Kiszka | if (monitor_device_index >= MAX_MONITOR_DEVICES) {
|
5142 | ddd9bbd9 | Jan Kiszka | fprintf(stderr, "qemu: too many monitor devices\n");
|
5143 | ddd9bbd9 | Jan Kiszka | exit(1);
|
5144 | ddd9bbd9 | Jan Kiszka | } |
5145 | ddd9bbd9 | Jan Kiszka | monitor_devices[monitor_device_index] = optarg; |
5146 | ddd9bbd9 | Jan Kiszka | monitor_device_index++; |
5147 | 82c643ff | bellard | break;
|
5148 | 191bc01b | Gerd Hoffmann | case QEMU_OPTION_chardev:
|
5149 | 191bc01b | Gerd Hoffmann | opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend");
|
5150 | 191bc01b | Gerd Hoffmann | if (!opts) {
|
5151 | 191bc01b | Gerd Hoffmann | fprintf(stderr, "parse error: %s\n", optarg);
|
5152 | 191bc01b | Gerd Hoffmann | exit(1);
|
5153 | 191bc01b | Gerd Hoffmann | } |
5154 | 3df04ac3 | Mark McLoughlin | if (qemu_chr_open_opts(opts, NULL) == NULL) { |
5155 | 191bc01b | Gerd Hoffmann | exit(1);
|
5156 | 191bc01b | Gerd Hoffmann | } |
5157 | 191bc01b | Gerd Hoffmann | break;
|
5158 | 82c643ff | bellard | case QEMU_OPTION_serial:
|
5159 | 8d11df9e | bellard | if (serial_device_index >= MAX_SERIAL_PORTS) {
|
5160 | 8d11df9e | bellard | fprintf(stderr, "qemu: too many serial ports\n");
|
5161 | 8d11df9e | bellard | exit(1);
|
5162 | 8d11df9e | bellard | } |
5163 | fd5f393a | pbrook | serial_devices[serial_device_index] = optarg; |
5164 | 8d11df9e | bellard | serial_device_index++; |
5165 | 82c643ff | bellard | break;
|
5166 | 9dd986cc | Richard W.M. Jones | case QEMU_OPTION_watchdog:
|
5167 | 09aaa160 | Markus Armbruster | if (watchdog) {
|
5168 | 09aaa160 | Markus Armbruster | fprintf(stderr, |
5169 | 09aaa160 | Markus Armbruster | "qemu: only one watchdog option may be given\n");
|
5170 | 09aaa160 | Markus Armbruster | return 1; |
5171 | 09aaa160 | Markus Armbruster | } |
5172 | 09aaa160 | Markus Armbruster | watchdog = optarg; |
5173 | 9dd986cc | Richard W.M. Jones | break;
|
5174 | 9dd986cc | Richard W.M. Jones | case QEMU_OPTION_watchdog_action:
|
5175 | 9dd986cc | Richard W.M. Jones | if (select_watchdog_action(optarg) == -1) { |
5176 | 9dd986cc | Richard W.M. Jones | fprintf(stderr, "Unknown -watchdog-action parameter\n");
|
5177 | 9dd986cc | Richard W.M. Jones | exit(1);
|
5178 | 9dd986cc | Richard W.M. Jones | } |
5179 | 9dd986cc | Richard W.M. Jones | break;
|
5180 | 51ecf136 | aliguori | case QEMU_OPTION_virtiocon:
|
5181 | 51ecf136 | aliguori | if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
|
5182 | 51ecf136 | aliguori | fprintf(stderr, "qemu: too many virtio consoles\n");
|
5183 | 51ecf136 | aliguori | exit(1);
|
5184 | 51ecf136 | aliguori | } |
5185 | 51ecf136 | aliguori | virtio_consoles[virtio_console_index] = optarg; |
5186 | 51ecf136 | aliguori | virtio_console_index++; |
5187 | 51ecf136 | aliguori | break;
|
5188 | 6508fe59 | bellard | case QEMU_OPTION_parallel:
|
5189 | 6508fe59 | bellard | if (parallel_device_index >= MAX_PARALLEL_PORTS) {
|
5190 | 6508fe59 | bellard | fprintf(stderr, "qemu: too many parallel ports\n");
|
5191 | 6508fe59 | bellard | exit(1);
|
5192 | 6508fe59 | bellard | } |
5193 | fd5f393a | pbrook | parallel_devices[parallel_device_index] = optarg; |
5194 | 6508fe59 | bellard | parallel_device_index++; |
5195 | 6508fe59 | bellard | break;
|
5196 | d63d307f | bellard | case QEMU_OPTION_loadvm:
|
5197 | d63d307f | bellard | loadvm = optarg; |
5198 | d63d307f | bellard | break;
|
5199 | d63d307f | bellard | case QEMU_OPTION_full_screen:
|
5200 | d63d307f | bellard | full_screen = 1;
|
5201 | d63d307f | bellard | break;
|
5202 | 667accab | ths | #ifdef CONFIG_SDL
|
5203 | 43523e93 | ths | case QEMU_OPTION_no_frame:
|
5204 | 43523e93 | ths | no_frame = 1;
|
5205 | 43523e93 | ths | break;
|
5206 | 3780e197 | ths | case QEMU_OPTION_alt_grab:
|
5207 | 3780e197 | ths | alt_grab = 1;
|
5208 | 3780e197 | ths | break;
|
5209 | 667accab | ths | case QEMU_OPTION_no_quit:
|
5210 | 667accab | ths | no_quit = 1;
|
5211 | 667accab | ths | break;
|
5212 | 7d957bd8 | aliguori | case QEMU_OPTION_sdl:
|
5213 | 993fbfdb | Anthony Liguori | display_type = DT_SDL; |
5214 | 7d957bd8 | aliguori | break;
|
5215 | 667accab | ths | #endif
|
5216 | f7cce898 | bellard | case QEMU_OPTION_pidfile:
|
5217 | 93815bc2 | ths | pid_file = optarg; |
5218 | f7cce898 | bellard | break;
|
5219 | a09db21f | bellard | #ifdef TARGET_I386
|
5220 | a09db21f | bellard | case QEMU_OPTION_win2k_hack:
|
5221 | a09db21f | bellard | win2k_install_hack = 1;
|
5222 | a09db21f | bellard | break;
|
5223 | 73822ec8 | aliguori | case QEMU_OPTION_rtc_td_hack:
|
5224 | 73822ec8 | aliguori | rtc_td_hack = 1;
|
5225 | 73822ec8 | aliguori | break;
|
5226 | 8a92ea2f | aliguori | case QEMU_OPTION_acpitable:
|
5227 | 8a92ea2f | aliguori | if(acpi_table_add(optarg) < 0) { |
5228 | 8a92ea2f | aliguori | fprintf(stderr, "Wrong acpi table provided\n");
|
5229 | 8a92ea2f | aliguori | exit(1);
|
5230 | 8a92ea2f | aliguori | } |
5231 | 8a92ea2f | aliguori | break;
|
5232 | b6f6e3d3 | aliguori | case QEMU_OPTION_smbios:
|
5233 | b6f6e3d3 | aliguori | if(smbios_entry_add(optarg) < 0) { |
5234 | b6f6e3d3 | aliguori | fprintf(stderr, "Wrong smbios provided\n");
|
5235 | b6f6e3d3 | aliguori | exit(1);
|
5236 | b6f6e3d3 | aliguori | } |
5237 | b6f6e3d3 | aliguori | break;
|
5238 | a09db21f | bellard | #endif
|
5239 | 7ba1e619 | aliguori | #ifdef CONFIG_KVM
|
5240 | 7ba1e619 | aliguori | case QEMU_OPTION_enable_kvm:
|
5241 | 7ba1e619 | aliguori | kvm_allowed = 1;
|
5242 | 7ba1e619 | aliguori | break;
|
5243 | 7ba1e619 | aliguori | #endif
|
5244 | bb36d470 | bellard | case QEMU_OPTION_usb:
|
5245 | bb36d470 | bellard | usb_enabled = 1;
|
5246 | bb36d470 | bellard | break;
|
5247 | a594cfbf | bellard | case QEMU_OPTION_usbdevice:
|
5248 | a594cfbf | bellard | usb_enabled = 1;
|
5249 | bd3c948d | Gerd Hoffmann | add_device_config(DEV_USB, optarg); |
5250 | bd3c948d | Gerd Hoffmann | break;
|
5251 | bd3c948d | Gerd Hoffmann | case QEMU_OPTION_device:
|
5252 | f31d07d1 | Gerd Hoffmann | opts = qemu_opts_parse(&qemu_device_opts, optarg, "driver");
|
5253 | f31d07d1 | Gerd Hoffmann | if (!opts) {
|
5254 | f31d07d1 | Gerd Hoffmann | fprintf(stderr, "parse error: %s\n", optarg);
|
5255 | f31d07d1 | Gerd Hoffmann | exit(1);
|
5256 | f31d07d1 | Gerd Hoffmann | } |
5257 | a594cfbf | bellard | break;
|
5258 | 6a00d601 | bellard | case QEMU_OPTION_smp:
|
5259 | dc6b1c09 | Andre Przywara | smp_parse(optarg); |
5260 | b2097003 | aliguori | if (smp_cpus < 1) { |
5261 | 6a00d601 | bellard | fprintf(stderr, "Invalid number of CPUs\n");
|
5262 | 6a00d601 | bellard | exit(1);
|
5263 | 6a00d601 | bellard | } |
5264 | 6be68d7e | Jes Sorensen | if (max_cpus < smp_cpus) {
|
5265 | 6be68d7e | Jes Sorensen | fprintf(stderr, "maxcpus must be equal to or greater than "
|
5266 | 6be68d7e | Jes Sorensen | "smp\n");
|
5267 | 6be68d7e | Jes Sorensen | exit(1);
|
5268 | 6be68d7e | Jes Sorensen | } |
5269 | 6be68d7e | Jes Sorensen | if (max_cpus > 255) { |
5270 | 6be68d7e | Jes Sorensen | fprintf(stderr, "Unsupported number of maxcpus\n");
|
5271 | 6be68d7e | Jes Sorensen | exit(1);
|
5272 | 6be68d7e | Jes Sorensen | } |
5273 | 6a00d601 | bellard | break;
|
5274 | 24236869 | bellard | case QEMU_OPTION_vnc:
|
5275 | 993fbfdb | Anthony Liguori | display_type = DT_VNC; |
5276 | 73fc9742 | ths | vnc_display = optarg; |
5277 | 24236869 | bellard | break;
|
5278 | 5824d651 | blueswir1 | #ifdef TARGET_I386
|
5279 | 6515b203 | bellard | case QEMU_OPTION_no_acpi:
|
5280 | 6515b203 | bellard | acpi_enabled = 0;
|
5281 | 6515b203 | bellard | break;
|
5282 | 16b29ae1 | aliguori | case QEMU_OPTION_no_hpet:
|
5283 | 16b29ae1 | aliguori | no_hpet = 1;
|
5284 | 16b29ae1 | aliguori | break;
|
5285 | 7d4c3d53 | Markus Armbruster | case QEMU_OPTION_balloon:
|
5286 | 7d4c3d53 | Markus Armbruster | if (balloon_parse(optarg) < 0) { |
5287 | 7d4c3d53 | Markus Armbruster | fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
|
5288 | 7d4c3d53 | Markus Armbruster | exit(1);
|
5289 | 7d4c3d53 | Markus Armbruster | } |
5290 | df97b920 | Eduardo Habkost | break;
|
5291 | 5824d651 | blueswir1 | #endif
|
5292 | d1beab82 | bellard | case QEMU_OPTION_no_reboot:
|
5293 | d1beab82 | bellard | no_reboot = 1;
|
5294 | d1beab82 | bellard | break;
|
5295 | b2f76161 | aurel32 | case QEMU_OPTION_no_shutdown:
|
5296 | b2f76161 | aurel32 | no_shutdown = 1;
|
5297 | b2f76161 | aurel32 | break;
|
5298 | 9467cd46 | balrog | case QEMU_OPTION_show_cursor:
|
5299 | 9467cd46 | balrog | cursor_hide = 0;
|
5300 | 9467cd46 | balrog | break;
|
5301 | 8fcb1b90 | blueswir1 | case QEMU_OPTION_uuid:
|
5302 | 8fcb1b90 | blueswir1 | if(qemu_uuid_parse(optarg, qemu_uuid) < 0) { |
5303 | 8fcb1b90 | blueswir1 | fprintf(stderr, "Fail to parse UUID string."
|
5304 | 8fcb1b90 | blueswir1 | " Wrong format.\n");
|
5305 | 8fcb1b90 | blueswir1 | exit(1);
|
5306 | 8fcb1b90 | blueswir1 | } |
5307 | 8fcb1b90 | blueswir1 | break;
|
5308 | 5824d651 | blueswir1 | #ifndef _WIN32
|
5309 | 71e3ceb8 | ths | case QEMU_OPTION_daemonize:
|
5310 | 71e3ceb8 | ths | daemonize = 1;
|
5311 | 71e3ceb8 | ths | break;
|
5312 | 5824d651 | blueswir1 | #endif
|
5313 | 9ae02555 | ths | case QEMU_OPTION_option_rom:
|
5314 | 9ae02555 | ths | if (nb_option_roms >= MAX_OPTION_ROMS) {
|
5315 | 9ae02555 | ths | fprintf(stderr, "Too many option ROMs\n");
|
5316 | 9ae02555 | ths | exit(1);
|
5317 | 9ae02555 | ths | } |
5318 | 9ae02555 | ths | option_rom[nb_option_roms] = optarg; |
5319 | 9ae02555 | ths | nb_option_roms++; |
5320 | 9ae02555 | ths | break;
|
5321 | 5824d651 | blueswir1 | #if defined(TARGET_ARM) || defined(TARGET_M68K)
|
5322 | 8e71621f | pbrook | case QEMU_OPTION_semihosting:
|
5323 | 8e71621f | pbrook | semihosting_enabled = 1;
|
5324 | 8e71621f | pbrook | break;
|
5325 | 5824d651 | blueswir1 | #endif
|
5326 | c35734b2 | ths | case QEMU_OPTION_name:
|
5327 | 1889465a | Andi Kleen | qemu_name = qemu_strdup(optarg); |
5328 | 1889465a | Andi Kleen | { |
5329 | 1889465a | Andi Kleen | char *p = strchr(qemu_name, ','); |
5330 | 1889465a | Andi Kleen | if (p != NULL) { |
5331 | 1889465a | Andi Kleen | *p++ = 0;
|
5332 | 1889465a | Andi Kleen | if (strncmp(p, "process=", 8)) { |
5333 | 1889465a | Andi Kleen | fprintf(stderr, "Unknown subargument %s to -name", p);
|
5334 | 1889465a | Andi Kleen | exit(1);
|
5335 | 1889465a | Andi Kleen | } |
5336 | 1889465a | Andi Kleen | p += 8;
|
5337 | 1889465a | Andi Kleen | set_proc_name(p); |
5338 | 1889465a | Andi Kleen | } |
5339 | 1889465a | Andi Kleen | } |
5340 | c35734b2 | ths | break;
|
5341 | 95efd11c | blueswir1 | #if defined(TARGET_SPARC) || defined(TARGET_PPC)
|
5342 | 66508601 | blueswir1 | case QEMU_OPTION_prom_env:
|
5343 | 66508601 | blueswir1 | if (nb_prom_envs >= MAX_PROM_ENVS) {
|
5344 | 66508601 | blueswir1 | fprintf(stderr, "Too many prom variables\n");
|
5345 | 66508601 | blueswir1 | exit(1);
|
5346 | 66508601 | blueswir1 | } |
5347 | 66508601 | blueswir1 | prom_envs[nb_prom_envs] = optarg; |
5348 | 66508601 | blueswir1 | nb_prom_envs++; |
5349 | 66508601 | blueswir1 | break;
|
5350 | 66508601 | blueswir1 | #endif
|
5351 | 2b8f2d41 | balrog | #ifdef TARGET_ARM
|
5352 | 2b8f2d41 | balrog | case QEMU_OPTION_old_param:
|
5353 | 2b8f2d41 | balrog | old_param = 1;
|
5354 | 05ebd537 | ths | break;
|
5355 | 2b8f2d41 | balrog | #endif
|
5356 | f3dcfada | ths | case QEMU_OPTION_clock:
|
5357 | f3dcfada | ths | configure_alarms(optarg); |
5358 | f3dcfada | ths | break;
|
5359 | 7e0af5d0 | bellard | case QEMU_OPTION_startdate:
|
5360 | 7e0af5d0 | bellard | { |
5361 | 7e0af5d0 | bellard | struct tm tm;
|
5362 | f6503059 | balrog | time_t rtc_start_date; |
5363 | 7e0af5d0 | bellard | if (!strcmp(optarg, "now")) { |
5364 | f6503059 | balrog | rtc_date_offset = -1;
|
5365 | 7e0af5d0 | bellard | } else {
|
5366 | 7e0af5d0 | bellard | if (sscanf(optarg, "%d-%d-%dT%d:%d:%d", |
5367 | 7e0af5d0 | bellard | &tm.tm_year, |
5368 | 7e0af5d0 | bellard | &tm.tm_mon, |
5369 | 7e0af5d0 | bellard | &tm.tm_mday, |
5370 | 7e0af5d0 | bellard | &tm.tm_hour, |
5371 | 7e0af5d0 | bellard | &tm.tm_min, |
5372 | 7e0af5d0 | bellard | &tm.tm_sec) == 6) {
|
5373 | 7e0af5d0 | bellard | /* OK */
|
5374 | 7e0af5d0 | bellard | } else if (sscanf(optarg, "%d-%d-%d", |
5375 | 7e0af5d0 | bellard | &tm.tm_year, |
5376 | 7e0af5d0 | bellard | &tm.tm_mon, |
5377 | 7e0af5d0 | bellard | &tm.tm_mday) == 3) {
|
5378 | 7e0af5d0 | bellard | tm.tm_hour = 0;
|
5379 | 7e0af5d0 | bellard | tm.tm_min = 0;
|
5380 | 7e0af5d0 | bellard | tm.tm_sec = 0;
|
5381 | 7e0af5d0 | bellard | } else {
|
5382 | 7e0af5d0 | bellard | goto date_fail;
|
5383 | 7e0af5d0 | bellard | } |
5384 | 7e0af5d0 | bellard | tm.tm_year -= 1900;
|
5385 | 7e0af5d0 | bellard | tm.tm_mon--; |
5386 | 3c6b2088 | bellard | rtc_start_date = mktimegm(&tm); |
5387 | 7e0af5d0 | bellard | if (rtc_start_date == -1) { |
5388 | 7e0af5d0 | bellard | date_fail:
|
5389 | 7e0af5d0 | bellard | fprintf(stderr, "Invalid date format. Valid format are:\n"
|
5390 | 7e0af5d0 | bellard | "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
|
5391 | 7e0af5d0 | bellard | exit(1);
|
5392 | 7e0af5d0 | bellard | } |
5393 | f6503059 | balrog | rtc_date_offset = time(NULL) - rtc_start_date;
|
5394 | 7e0af5d0 | bellard | } |
5395 | 7e0af5d0 | bellard | } |
5396 | 7e0af5d0 | bellard | break;
|
5397 | 26a5f13b | bellard | case QEMU_OPTION_tb_size:
|
5398 | 26a5f13b | bellard | tb_size = strtol(optarg, NULL, 0); |
5399 | 26a5f13b | bellard | if (tb_size < 0) |
5400 | 26a5f13b | bellard | tb_size = 0;
|
5401 | 26a5f13b | bellard | break;
|
5402 | 2e70f6ef | pbrook | case QEMU_OPTION_icount:
|
5403 | 2e70f6ef | pbrook | use_icount = 1;
|
5404 | 2e70f6ef | pbrook | if (strcmp(optarg, "auto") == 0) { |
5405 | 2e70f6ef | pbrook | icount_time_shift = -1;
|
5406 | 2e70f6ef | pbrook | } else {
|
5407 | 2e70f6ef | pbrook | icount_time_shift = strtol(optarg, NULL, 0); |
5408 | 2e70f6ef | pbrook | } |
5409 | 2e70f6ef | pbrook | break;
|
5410 | 5bb7910a | aliguori | case QEMU_OPTION_incoming:
|
5411 | 5bb7910a | aliguori | incoming = optarg; |
5412 | 5bb7910a | aliguori | break;
|
5413 | 5824d651 | blueswir1 | #ifndef _WIN32
|
5414 | 0858532e | aliguori | case QEMU_OPTION_chroot:
|
5415 | 0858532e | aliguori | chroot_dir = optarg; |
5416 | 0858532e | aliguori | break;
|
5417 | 0858532e | aliguori | case QEMU_OPTION_runas:
|
5418 | 0858532e | aliguori | run_as = optarg; |
5419 | 0858532e | aliguori | break;
|
5420 | 5824d651 | blueswir1 | #endif
|
5421 | e37630ca | aliguori | #ifdef CONFIG_XEN
|
5422 | e37630ca | aliguori | case QEMU_OPTION_xen_domid:
|
5423 | e37630ca | aliguori | xen_domid = atoi(optarg); |
5424 | e37630ca | aliguori | break;
|
5425 | e37630ca | aliguori | case QEMU_OPTION_xen_create:
|
5426 | e37630ca | aliguori | xen_mode = XEN_CREATE; |
5427 | e37630ca | aliguori | break;
|
5428 | e37630ca | aliguori | case QEMU_OPTION_xen_attach:
|
5429 | e37630ca | aliguori | xen_mode = XEN_ATTACH; |
5430 | e37630ca | aliguori | break;
|
5431 | e37630ca | aliguori | #endif
|
5432 | cd6f1169 | bellard | } |
5433 | 0824d6fc | bellard | } |
5434 | 0824d6fc | bellard | } |
5435 | 330d0414 | bellard | |
5436 | 5cea8590 | Paul Brook | /* If no data_dir is specified then try to find it relative to the
|
5437 | 5cea8590 | Paul Brook | executable path. */
|
5438 | 5cea8590 | Paul Brook | if (!data_dir) {
|
5439 | 5cea8590 | Paul Brook | data_dir = find_datadir(argv[0]);
|
5440 | 5cea8590 | Paul Brook | } |
5441 | 5cea8590 | Paul Brook | /* If all else fails use the install patch specified when building. */
|
5442 | 5cea8590 | Paul Brook | if (!data_dir) {
|
5443 | 5cea8590 | Paul Brook | data_dir = CONFIG_QEMU_SHAREDIR; |
5444 | 5cea8590 | Paul Brook | } |
5445 | 5cea8590 | Paul Brook | |
5446 | 6be68d7e | Jes Sorensen | /*
|
5447 | 6be68d7e | Jes Sorensen | * Default to max_cpus = smp_cpus, in case the user doesn't
|
5448 | 6be68d7e | Jes Sorensen | * specify a max_cpus value.
|
5449 | 6be68d7e | Jes Sorensen | */
|
5450 | 6be68d7e | Jes Sorensen | if (!max_cpus)
|
5451 | 6be68d7e | Jes Sorensen | max_cpus = smp_cpus; |
5452 | 6be68d7e | Jes Sorensen | |
5453 | 3d878caa | balrog | machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */ |
5454 | b2097003 | aliguori | if (smp_cpus > machine->max_cpus) {
|
5455 | b2097003 | aliguori | fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
|
5456 | b2097003 | aliguori | "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
|
5457 | b2097003 | aliguori | machine->max_cpus); |
5458 | b2097003 | aliguori | exit(1);
|
5459 | b2097003 | aliguori | } |
5460 | b2097003 | aliguori | |
5461 | 993fbfdb | Anthony Liguori | if (display_type == DT_NOGRAPHIC) {
|
5462 | bc0129d9 | aliguori | if (serial_device_index == 0) |
5463 | bc0129d9 | aliguori | serial_devices[0] = "stdio"; |
5464 | bc0129d9 | aliguori | if (parallel_device_index == 0) |
5465 | bc0129d9 | aliguori | parallel_devices[0] = "null"; |
5466 | ddd9bbd9 | Jan Kiszka | if (strncmp(monitor_devices[0], "vc", 2) == 0) { |
5467 | ddd9bbd9 | Jan Kiszka | monitor_devices[0] = "stdio"; |
5468 | ddd9bbd9 | Jan Kiszka | } |
5469 | bc0129d9 | aliguori | } |
5470 | bc0129d9 | aliguori | |
5471 | 71e3ceb8 | ths | #ifndef _WIN32
|
5472 | 71e3ceb8 | ths | if (daemonize) {
|
5473 | 71e3ceb8 | ths | pid_t pid; |
5474 | 71e3ceb8 | ths | |
5475 | 71e3ceb8 | ths | if (pipe(fds) == -1) |
5476 | 71e3ceb8 | ths | exit(1);
|
5477 | 71e3ceb8 | ths | |
5478 | 71e3ceb8 | ths | pid = fork(); |
5479 | 71e3ceb8 | ths | if (pid > 0) { |
5480 | 71e3ceb8 | ths | uint8_t status; |
5481 | 71e3ceb8 | ths | ssize_t len; |
5482 | 71e3ceb8 | ths | |
5483 | 71e3ceb8 | ths | close(fds[1]);
|
5484 | 71e3ceb8 | ths | |
5485 | 71e3ceb8 | ths | again:
|
5486 | 93815bc2 | ths | len = read(fds[0], &status, 1); |
5487 | 93815bc2 | ths | if (len == -1 && (errno == EINTR)) |
5488 | 93815bc2 | ths | goto again;
|
5489 | 93815bc2 | ths | |
5490 | 93815bc2 | ths | if (len != 1) |
5491 | 93815bc2 | ths | exit(1);
|
5492 | 93815bc2 | ths | else if (status == 1) { |
5493 | 93815bc2 | ths | fprintf(stderr, "Could not acquire pidfile\n");
|
5494 | 93815bc2 | ths | exit(1);
|
5495 | 93815bc2 | ths | } else
|
5496 | 93815bc2 | ths | exit(0);
|
5497 | 71e3ceb8 | ths | } else if (pid < 0) |
5498 | 93815bc2 | ths | exit(1);
|
5499 | 71e3ceb8 | ths | |
5500 | 71e3ceb8 | ths | setsid(); |
5501 | 71e3ceb8 | ths | |
5502 | 71e3ceb8 | ths | pid = fork(); |
5503 | 71e3ceb8 | ths | if (pid > 0) |
5504 | 71e3ceb8 | ths | exit(0);
|
5505 | 71e3ceb8 | ths | else if (pid < 0) |
5506 | 71e3ceb8 | ths | exit(1);
|
5507 | 71e3ceb8 | ths | |
5508 | 71e3ceb8 | ths | umask(027);
|
5509 | 71e3ceb8 | ths | |
5510 | 71e3ceb8 | ths | signal(SIGTSTP, SIG_IGN); |
5511 | 71e3ceb8 | ths | signal(SIGTTOU, SIG_IGN); |
5512 | 71e3ceb8 | ths | signal(SIGTTIN, SIG_IGN); |
5513 | 71e3ceb8 | ths | } |
5514 | 71e3ceb8 | ths | |
5515 | aa26bb2d | ths | if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
5516 | 93815bc2 | ths | if (daemonize) {
|
5517 | 93815bc2 | ths | uint8_t status = 1;
|
5518 | 93815bc2 | ths | write(fds[1], &status, 1); |
5519 | 93815bc2 | ths | } else
|
5520 | 93815bc2 | ths | fprintf(stderr, "Could not acquire pid file\n");
|
5521 | 93815bc2 | ths | exit(1);
|
5522 | 93815bc2 | ths | } |
5523 | b9e82a59 | blueswir1 | #endif
|
5524 | 93815bc2 | ths | |
5525 | 214910a7 | Marcelo Tosatti | if (kvm_enabled()) {
|
5526 | 214910a7 | Marcelo Tosatti | int ret;
|
5527 | 214910a7 | Marcelo Tosatti | |
5528 | 214910a7 | Marcelo Tosatti | ret = kvm_init(smp_cpus); |
5529 | 214910a7 | Marcelo Tosatti | if (ret < 0) { |
5530 | 214910a7 | Marcelo Tosatti | fprintf(stderr, "failed to initialize KVM\n");
|
5531 | 214910a7 | Marcelo Tosatti | exit(1);
|
5532 | 214910a7 | Marcelo Tosatti | } |
5533 | 214910a7 | Marcelo Tosatti | } |
5534 | 214910a7 | Marcelo Tosatti | |
5535 | 3fcf7b6b | aliguori | if (qemu_init_main_loop()) {
|
5536 | 3fcf7b6b | aliguori | fprintf(stderr, "qemu_init_main_loop failed\n");
|
5537 | 3fcf7b6b | aliguori | exit(1);
|
5538 | 3fcf7b6b | aliguori | } |
5539 | a20dd508 | bellard | linux_boot = (kernel_filename != NULL);
|
5540 | 6c41b272 | balrog | |
5541 | f8d39c01 | ths | if (!linux_boot && *kernel_cmdline != '\0') { |
5542 | f8d39c01 | ths | fprintf(stderr, "-append only allowed with -kernel option\n");
|
5543 | f8d39c01 | ths | exit(1);
|
5544 | f8d39c01 | ths | } |
5545 | f8d39c01 | ths | |
5546 | f8d39c01 | ths | if (!linux_boot && initrd_filename != NULL) { |
5547 | f8d39c01 | ths | fprintf(stderr, "-initrd only allowed with -kernel option\n");
|
5548 | f8d39c01 | ths | exit(1);
|
5549 | f8d39c01 | ths | } |
5550 | f8d39c01 | ths | |
5551 | bf65f53f | Filip Navara | #ifndef _WIN32
|
5552 | bf65f53f | Filip Navara | /* Win32 doesn't support line-buffering and requires size >= 2 */
|
5553 | b118d61e | bellard | setvbuf(stdout, NULL, _IOLBF, 0); |
5554 | bf65f53f | Filip Navara | #endif
|
5555 | 3b46e624 | ths | |
5556 | 634fce96 | pbrook | init_timers(); |
5557 | 7183b4b4 | aliguori | if (init_timer_alarm() < 0) { |
5558 | 7183b4b4 | aliguori | fprintf(stderr, "could not initialize alarm timer\n");
|
5559 | 7183b4b4 | aliguori | exit(1);
|
5560 | 7183b4b4 | aliguori | } |
5561 | 2e70f6ef | pbrook | if (use_icount && icount_time_shift < 0) { |
5562 | 2e70f6ef | pbrook | use_icount = 2;
|
5563 | 2e70f6ef | pbrook | /* 125MIPS seems a reasonable initial guess at the guest speed.
|
5564 | 2e70f6ef | pbrook | It will be corrected fairly quickly anyway. */
|
5565 | 2e70f6ef | pbrook | icount_time_shift = 3;
|
5566 | 2e70f6ef | pbrook | init_icount_adjust(); |
5567 | 2e70f6ef | pbrook | } |
5568 | 634fce96 | pbrook | |
5569 | fd1dff4b | bellard | #ifdef _WIN32
|
5570 | fd1dff4b | bellard | socket_init(); |
5571 | fd1dff4b | bellard | #endif
|
5572 | fd1dff4b | bellard | |
5573 | 7c9d8e07 | bellard | /* init network clients */
|
5574 | 7c9d8e07 | bellard | if (nb_net_clients == 0) { |
5575 | 7c9d8e07 | bellard | /* if no clients, we use a default config */
|
5576 | f441b28b | aliguori | net_clients[nb_net_clients++] = "nic";
|
5577 | f441b28b | aliguori | #ifdef CONFIG_SLIRP
|
5578 | f441b28b | aliguori | net_clients[nb_net_clients++] = "user";
|
5579 | f441b28b | aliguori | #endif
|
5580 | c20709aa | bellard | } |
5581 | c20709aa | bellard | |
5582 | 7c9d8e07 | bellard | for(i = 0;i < nb_net_clients; i++) { |
5583 | 9ad97e65 | balrog | if (net_client_parse(net_clients[i]) < 0) |
5584 | 7c9d8e07 | bellard | exit(1);
|
5585 | 702c651c | bellard | } |
5586 | f1510b2c | bellard | |
5587 | 406c8df3 | Glauber Costa | net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
5588 | 406c8df3 | Glauber Costa | net_set_boot_mask(net_boot); |
5589 | 406c8df3 | Glauber Costa | |
5590 | 406c8df3 | Glauber Costa | net_client_check(); |
5591 | eec85c2a | ths | |
5592 | dc72ac14 | balrog | /* init the bluetooth world */
|
5593 | bd3c948d | Gerd Hoffmann | if (foreach_device_config(DEV_BT, bt_parse))
|
5594 | bd3c948d | Gerd Hoffmann | exit(1);
|
5595 | dc72ac14 | balrog | |
5596 | 0824d6fc | bellard | /* init the memory */
|
5597 | 94a6b54f | pbrook | if (ram_size == 0) |
5598 | 94a6b54f | pbrook | ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
5599 | 9ae02555 | ths | |
5600 | 26a5f13b | bellard | /* init the dynamic translator */
|
5601 | 26a5f13b | bellard | cpu_exec_init_all(tb_size * 1024 * 1024); |
5602 | 26a5f13b | bellard | |
5603 | 5905b2e5 | bellard | bdrv_init(); |
5604 | c4b1fcc0 | bellard | |
5605 | e4bcb14c | ths | /* we always create the cdrom drive, even if no disk is there */
|
5606 | 3b0ba927 | Gerd Hoffmann | drive_add(NULL, CDROM_ALIAS);
|
5607 | c4b1fcc0 | bellard | |
5608 | 9d413d1d | balrog | /* we always create at least one floppy */
|
5609 | 3b0ba927 | Gerd Hoffmann | drive_add(NULL, FD_ALIAS, 0); |
5610 | 86f55663 | j_mayer | |
5611 | 9d413d1d | balrog | /* we always create one sd slot, even if no card is in it */
|
5612 | 3b0ba927 | Gerd Hoffmann | drive_add(NULL, SD_ALIAS);
|
5613 | 9d413d1d | balrog | |
5614 | e4bcb14c | ths | /* open the virtual block devices */
|
5615 | 9dfd7c7a | Gerd Hoffmann | if (snapshot)
|
5616 | 7282a033 | Gerd Hoffmann | qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0); |
5617 | 7282a033 | Gerd Hoffmann | if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0) |
5618 | 9dfd7c7a | Gerd Hoffmann | exit(1);
|
5619 | 3e3d5815 | balrog | |
5620 | 2faf58cd | Juan Quintela | vmstate_register(0, &vmstate_timers ,&timers_state);
|
5621 | 475e4277 | aliguori | register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL); |
5622 | 8a7ddc38 | bellard | |
5623 | 3023f332 | aliguori | /* Maintain compatibility with multiple stdio monitors */
|
5624 | ddd9bbd9 | Jan Kiszka | if (!strcmp(monitor_devices[0],"stdio")) { |
5625 | 3023f332 | aliguori | for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
5626 | 3023f332 | aliguori | const char *devname = serial_devices[i]; |
5627 | 3023f332 | aliguori | if (devname && !strcmp(devname,"mon:stdio")) { |
5628 | ddd9bbd9 | Jan Kiszka | monitor_devices[0] = NULL; |
5629 | 3023f332 | aliguori | break;
|
5630 | 3023f332 | aliguori | } else if (devname && !strcmp(devname,"stdio")) { |
5631 | ddd9bbd9 | Jan Kiszka | monitor_devices[0] = NULL; |
5632 | 3023f332 | aliguori | serial_devices[i] = "mon:stdio";
|
5633 | 3023f332 | aliguori | break;
|
5634 | 3023f332 | aliguori | } |
5635 | 3023f332 | aliguori | } |
5636 | 3023f332 | aliguori | } |
5637 | 3023f332 | aliguori | |
5638 | 268a362c | aliguori | if (nb_numa_nodes > 0) { |
5639 | 268a362c | aliguori | int i;
|
5640 | 268a362c | aliguori | |
5641 | 268a362c | aliguori | if (nb_numa_nodes > smp_cpus) {
|
5642 | 268a362c | aliguori | nb_numa_nodes = smp_cpus; |
5643 | 268a362c | aliguori | } |
5644 | 268a362c | aliguori | |
5645 | 268a362c | aliguori | /* If no memory size if given for any node, assume the default case
|
5646 | 268a362c | aliguori | * and distribute the available memory equally across all nodes
|
5647 | 268a362c | aliguori | */
|
5648 | 268a362c | aliguori | for (i = 0; i < nb_numa_nodes; i++) { |
5649 | 268a362c | aliguori | if (node_mem[i] != 0) |
5650 | 268a362c | aliguori | break;
|
5651 | 268a362c | aliguori | } |
5652 | 268a362c | aliguori | if (i == nb_numa_nodes) {
|
5653 | 268a362c | aliguori | uint64_t usedmem = 0;
|
5654 | 268a362c | aliguori | |
5655 | 268a362c | aliguori | /* On Linux, the each node's border has to be 8MB aligned,
|
5656 | 268a362c | aliguori | * the final node gets the rest.
|
5657 | 268a362c | aliguori | */
|
5658 | 268a362c | aliguori | for (i = 0; i < nb_numa_nodes - 1; i++) { |
5659 | 268a362c | aliguori | node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1); |
5660 | 268a362c | aliguori | usedmem += node_mem[i]; |
5661 | 268a362c | aliguori | } |
5662 | 268a362c | aliguori | node_mem[i] = ram_size - usedmem; |
5663 | 268a362c | aliguori | } |
5664 | 268a362c | aliguori | |
5665 | 268a362c | aliguori | for (i = 0; i < nb_numa_nodes; i++) { |
5666 | 268a362c | aliguori | if (node_cpumask[i] != 0) |
5667 | 268a362c | aliguori | break;
|
5668 | 268a362c | aliguori | } |
5669 | 268a362c | aliguori | /* assigning the VCPUs round-robin is easier to implement, guest OSes
|
5670 | 268a362c | aliguori | * must cope with this anyway, because there are BIOSes out there in
|
5671 | 268a362c | aliguori | * real machines which also use this scheme.
|
5672 | 268a362c | aliguori | */
|
5673 | 268a362c | aliguori | if (i == nb_numa_nodes) {
|
5674 | 268a362c | aliguori | for (i = 0; i < smp_cpus; i++) { |
5675 | 268a362c | aliguori | node_cpumask[i % nb_numa_nodes] |= 1 << i;
|
5676 | 268a362c | aliguori | } |
5677 | 268a362c | aliguori | } |
5678 | 268a362c | aliguori | } |
5679 | 268a362c | aliguori | |
5680 | ddd9bbd9 | Jan Kiszka | for (i = 0; i < MAX_MONITOR_DEVICES; i++) { |
5681 | ddd9bbd9 | Jan Kiszka | const char *devname = monitor_devices[i]; |
5682 | ddd9bbd9 | Jan Kiszka | if (devname && strcmp(devname, "none")) { |
5683 | ddd9bbd9 | Jan Kiszka | char label[32]; |
5684 | ddd9bbd9 | Jan Kiszka | if (i == 0) { |
5685 | ddd9bbd9 | Jan Kiszka | snprintf(label, sizeof(label), "monitor"); |
5686 | ddd9bbd9 | Jan Kiszka | } else {
|
5687 | ddd9bbd9 | Jan Kiszka | snprintf(label, sizeof(label), "monitor%d", i); |
5688 | ddd9bbd9 | Jan Kiszka | } |
5689 | ddd9bbd9 | Jan Kiszka | monitor_hds[i] = qemu_chr_open(label, devname, NULL);
|
5690 | ddd9bbd9 | Jan Kiszka | if (!monitor_hds[i]) {
|
5691 | ddd9bbd9 | Jan Kiszka | fprintf(stderr, "qemu: could not open monitor device '%s'\n",
|
5692 | ddd9bbd9 | Jan Kiszka | devname); |
5693 | ddd9bbd9 | Jan Kiszka | exit(1);
|
5694 | ddd9bbd9 | Jan Kiszka | } |
5695 | 4c621805 | aliguori | } |
5696 | 4c621805 | aliguori | } |
5697 | 4c621805 | aliguori | |
5698 | 2796dae0 | aliguori | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
5699 | 2796dae0 | aliguori | const char *devname = serial_devices[i]; |
5700 | 2796dae0 | aliguori | if (devname && strcmp(devname, "none")) { |
5701 | 324a8021 | Aurelien Jarno | char label[32]; |
5702 | 324a8021 | Aurelien Jarno | snprintf(label, sizeof(label), "serial%d", i); |
5703 | ceecf1d1 | aurel32 | serial_hds[i] = qemu_chr_open(label, devname, NULL);
|
5704 | 324a8021 | Aurelien Jarno | if (!serial_hds[i]) {
|
5705 | 324a8021 | Aurelien Jarno | fprintf(stderr, "qemu: could not open serial device '%s'\n",
|
5706 | 324a8021 | Aurelien Jarno | devname); |
5707 | 324a8021 | Aurelien Jarno | exit(1);
|
5708 | 324a8021 | Aurelien Jarno | } |
5709 | 2796dae0 | aliguori | } |
5710 | 2796dae0 | aliguori | } |
5711 | 2796dae0 | aliguori | |
5712 | 2796dae0 | aliguori | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
5713 | 2796dae0 | aliguori | const char *devname = parallel_devices[i]; |
5714 | 2796dae0 | aliguori | if (devname && strcmp(devname, "none")) { |
5715 | 2796dae0 | aliguori | char label[32]; |
5716 | 2796dae0 | aliguori | snprintf(label, sizeof(label), "parallel%d", i); |
5717 | ceecf1d1 | aurel32 | parallel_hds[i] = qemu_chr_open(label, devname, NULL);
|
5718 | 2796dae0 | aliguori | if (!parallel_hds[i]) {
|
5719 | 2796dae0 | aliguori | fprintf(stderr, "qemu: could not open parallel device '%s'\n",
|
5720 | 2796dae0 | aliguori | devname); |
5721 | 2796dae0 | aliguori | exit(1);
|
5722 | 2796dae0 | aliguori | } |
5723 | 2796dae0 | aliguori | } |
5724 | 2796dae0 | aliguori | } |
5725 | 2796dae0 | aliguori | |
5726 | 2796dae0 | aliguori | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { |
5727 | 2796dae0 | aliguori | const char *devname = virtio_consoles[i]; |
5728 | 2796dae0 | aliguori | if (devname && strcmp(devname, "none")) { |
5729 | 2796dae0 | aliguori | char label[32]; |
5730 | 2796dae0 | aliguori | snprintf(label, sizeof(label), "virtcon%d", i); |
5731 | ceecf1d1 | aurel32 | virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
|
5732 | 2796dae0 | aliguori | if (!virtcon_hds[i]) {
|
5733 | 2796dae0 | aliguori | fprintf(stderr, "qemu: could not open virtio console '%s'\n",
|
5734 | 2796dae0 | aliguori | devname); |
5735 | 2796dae0 | aliguori | exit(1);
|
5736 | 2796dae0 | aliguori | } |
5737 | 2796dae0 | aliguori | } |
5738 | 2796dae0 | aliguori | } |
5739 | 2796dae0 | aliguori | |
5740 | aae9460e | Paul Brook | module_call_init(MODULE_INIT_DEVICE); |
5741 | aae9460e | Paul Brook | |
5742 | 09aaa160 | Markus Armbruster | if (watchdog) {
|
5743 | 09aaa160 | Markus Armbruster | i = select_watchdog(watchdog); |
5744 | 09aaa160 | Markus Armbruster | if (i > 0) |
5745 | 09aaa160 | Markus Armbruster | exit (i == 1 ? 1 : 0); |
5746 | 09aaa160 | Markus Armbruster | } |
5747 | 09aaa160 | Markus Armbruster | |
5748 | b6b61144 | Gerd Hoffmann | if (machine->compat_props) {
|
5749 | b6b61144 | Gerd Hoffmann | qdev_prop_register_compat(machine->compat_props); |
5750 | b6b61144 | Gerd Hoffmann | } |
5751 | fbe1b595 | Paul Brook | machine->init(ram_size, boot_devices, |
5752 | 3023f332 | aliguori | kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
5753 | 3023f332 | aliguori | |
5754 | 268a362c | aliguori | |
5755 | 67b3b71d | Juan Quintela | #ifndef _WIN32
|
5756 | 67b3b71d | Juan Quintela | /* must be after terminal init, SDL library changes signal handlers */
|
5757 | 67b3b71d | Juan Quintela | sighandler_setup(); |
5758 | 67b3b71d | Juan Quintela | #endif
|
5759 | 67b3b71d | Juan Quintela | |
5760 | 268a362c | aliguori | for (env = first_cpu; env != NULL; env = env->next_cpu) { |
5761 | 268a362c | aliguori | for (i = 0; i < nb_numa_nodes; i++) { |
5762 | 268a362c | aliguori | if (node_cpumask[i] & (1 << env->cpu_index)) { |
5763 | 268a362c | aliguori | env->numa_node = i; |
5764 | 268a362c | aliguori | } |
5765 | 268a362c | aliguori | } |
5766 | 268a362c | aliguori | } |
5767 | 268a362c | aliguori | |
5768 | 6f338c34 | aliguori | current_machine = machine; |
5769 | 6f338c34 | aliguori | |
5770 | 3023f332 | aliguori | /* init USB devices */
|
5771 | 3023f332 | aliguori | if (usb_enabled) {
|
5772 | bd3c948d | Gerd Hoffmann | foreach_device_config(DEV_USB, usb_parse); |
5773 | 3023f332 | aliguori | } |
5774 | 3023f332 | aliguori | |
5775 | bd3c948d | Gerd Hoffmann | /* init generic devices */
|
5776 | f31d07d1 | Gerd Hoffmann | if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0) |
5777 | bd3c948d | Gerd Hoffmann | exit(1);
|
5778 | bd3c948d | Gerd Hoffmann | |
5779 | 8f391ab4 | aliguori | if (!display_state)
|
5780 | 8f391ab4 | aliguori | dumb_display_init(); |
5781 | 3023f332 | aliguori | /* just use the first displaystate for the moment */
|
5782 | 3023f332 | aliguori | ds = display_state; |
5783 | 993fbfdb | Anthony Liguori | |
5784 | 993fbfdb | Anthony Liguori | if (display_type == DT_DEFAULT) {
|
5785 | 993fbfdb | Anthony Liguori | #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
|
5786 | 993fbfdb | Anthony Liguori | display_type = DT_SDL; |
5787 | 993fbfdb | Anthony Liguori | #else
|
5788 | 993fbfdb | Anthony Liguori | display_type = DT_VNC; |
5789 | 993fbfdb | Anthony Liguori | vnc_display = "localhost:0,to=99";
|
5790 | 993fbfdb | Anthony Liguori | show_vnc_port = 1;
|
5791 | 993fbfdb | Anthony Liguori | #endif
|
5792 | 993fbfdb | Anthony Liguori | } |
5793 | 993fbfdb | Anthony Liguori | |
5794 | 993fbfdb | Anthony Liguori | |
5795 | 993fbfdb | Anthony Liguori | switch (display_type) {
|
5796 | 993fbfdb | Anthony Liguori | case DT_NOGRAPHIC:
|
5797 | 993fbfdb | Anthony Liguori | break;
|
5798 | 4d3b6f6e | balrog | #if defined(CONFIG_CURSES)
|
5799 | 993fbfdb | Anthony Liguori | case DT_CURSES:
|
5800 | 993fbfdb | Anthony Liguori | curses_display_init(ds, full_screen); |
5801 | 993fbfdb | Anthony Liguori | break;
|
5802 | 4d3b6f6e | balrog | #endif
|
5803 | 5b0753e0 | bellard | #if defined(CONFIG_SDL)
|
5804 | 993fbfdb | Anthony Liguori | case DT_SDL:
|
5805 | 993fbfdb | Anthony Liguori | sdl_display_init(ds, full_screen, no_frame); |
5806 | 993fbfdb | Anthony Liguori | break;
|
5807 | 5b0753e0 | bellard | #elif defined(CONFIG_COCOA)
|
5808 | 993fbfdb | Anthony Liguori | case DT_SDL:
|
5809 | 993fbfdb | Anthony Liguori | cocoa_display_init(ds, full_screen); |
5810 | 993fbfdb | Anthony Liguori | break;
|
5811 | 313aa567 | bellard | #endif
|
5812 | 993fbfdb | Anthony Liguori | case DT_VNC:
|
5813 | 993fbfdb | Anthony Liguori | vnc_display_init(ds); |
5814 | 993fbfdb | Anthony Liguori | if (vnc_display_open(ds, vnc_display) < 0) |
5815 | 993fbfdb | Anthony Liguori | exit(1);
|
5816 | f92f8afe | Anthony Liguori | |
5817 | 993fbfdb | Anthony Liguori | if (show_vnc_port) {
|
5818 | 993fbfdb | Anthony Liguori | printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
|
5819 | f92f8afe | Anthony Liguori | } |
5820 | 993fbfdb | Anthony Liguori | break;
|
5821 | 993fbfdb | Anthony Liguori | default:
|
5822 | 993fbfdb | Anthony Liguori | break;
|
5823 | 313aa567 | bellard | } |
5824 | 7d957bd8 | aliguori | dpy_resize(ds); |
5825 | 5b08fc10 | aliguori | |
5826 | 3023f332 | aliguori | dcl = ds->listeners; |
5827 | 3023f332 | aliguori | while (dcl != NULL) { |
5828 | 3023f332 | aliguori | if (dcl->dpy_refresh != NULL) { |
5829 | 3023f332 | aliguori | ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds); |
5830 | 3023f332 | aliguori | qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock)); |
5831 | 20d8a3ed | ths | } |
5832 | 3023f332 | aliguori | dcl = dcl->next; |
5833 | 20d8a3ed | ths | } |
5834 | 3023f332 | aliguori | |
5835 | 993fbfdb | Anthony Liguori | if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
|
5836 | 9043b62d | blueswir1 | nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
|
5837 | 9043b62d | blueswir1 | qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock)); |
5838 | 9043b62d | blueswir1 | } |
5839 | 9043b62d | blueswir1 | |
5840 | 2796dae0 | aliguori | text_consoles_set_display(display_state); |
5841 | 2970a6c9 | aliguori | qemu_chr_initial_reset(); |
5842 | 2796dae0 | aliguori | |
5843 | ddd9bbd9 | Jan Kiszka | for (i = 0; i < MAX_MONITOR_DEVICES; i++) { |
5844 | ddd9bbd9 | Jan Kiszka | if (monitor_devices[i] && monitor_hds[i]) {
|
5845 | ddd9bbd9 | Jan Kiszka | monitor_init(monitor_hds[i], |
5846 | ddd9bbd9 | Jan Kiszka | MONITOR_USE_READLINE | |
5847 | ddd9bbd9 | Jan Kiszka | ((i == 0) ? MONITOR_IS_DEFAULT : 0)); |
5848 | ddd9bbd9 | Jan Kiszka | } |
5849 | ddd9bbd9 | Jan Kiszka | } |
5850 | 82c643ff | bellard | |
5851 | 8d11df9e | bellard | for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
5852 | c03b0f0f | bellard | const char *devname = serial_devices[i]; |
5853 | fd5f393a | pbrook | if (devname && strcmp(devname, "none")) { |
5854 | af3a9031 | ths | if (strstart(devname, "vc", 0)) |
5855 | 7ba1260a | bellard | qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
|
5856 | 8d11df9e | bellard | } |
5857 | 82c643ff | bellard | } |
5858 | 82c643ff | bellard | |
5859 | 6508fe59 | bellard | for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
5860 | c03b0f0f | bellard | const char *devname = parallel_devices[i]; |
5861 | fd5f393a | pbrook | if (devname && strcmp(devname, "none")) { |
5862 | af3a9031 | ths | if (strstart(devname, "vc", 0)) |
5863 | 7ba1260a | bellard | qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
|
5864 | 6508fe59 | bellard | } |
5865 | 6508fe59 | bellard | } |
5866 | 6508fe59 | bellard | |
5867 | 9ede2fde | aliguori | for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) { |
5868 | 9ede2fde | aliguori | const char *devname = virtio_consoles[i]; |
5869 | 2796dae0 | aliguori | if (virtcon_hds[i] && devname) {
|
5870 | 9ede2fde | aliguori | if (strstart(devname, "vc", 0)) |
5871 | 9ede2fde | aliguori | qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
|
5872 | 9ede2fde | aliguori | } |
5873 | 9ede2fde | aliguori | } |
5874 | 9ede2fde | aliguori | |
5875 | 59030a8c | aliguori | if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) { |
5876 | 59030a8c | aliguori | fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
|
5877 | 59030a8c | aliguori | gdbstub_dev); |
5878 | 59030a8c | aliguori | exit(1);
|
5879 | 45669e00 | balrog | } |
5880 | 45669e00 | balrog | |
5881 | 05f2401e | Juan Quintela | if (loadvm) {
|
5882 | 05f2401e | Juan Quintela | if (load_vmstate(cur_mon, loadvm) < 0) { |
5883 | 05f2401e | Juan Quintela | autostart = 0;
|
5884 | 05f2401e | Juan Quintela | } |
5885 | 05f2401e | Juan Quintela | } |
5886 | d63d307f | bellard | |
5887 | 2bb8c10c | Glauber Costa | if (incoming) {
|
5888 | 5bb7910a | aliguori | qemu_start_incoming_migration(incoming); |
5889 | 6b99dadc | Avi Kivity | } else if (autostart) { |
5890 | c0f4ce77 | aliguori | vm_start(); |
5891 | 6b99dadc | Avi Kivity | } |
5892 | ffd843bc | ths | |
5893 | b9e82a59 | blueswir1 | #ifndef _WIN32
|
5894 | 71e3ceb8 | ths | if (daemonize) {
|
5895 | 71e3ceb8 | ths | uint8_t status = 0;
|
5896 | 71e3ceb8 | ths | ssize_t len; |
5897 | 71e3ceb8 | ths | |
5898 | 71e3ceb8 | ths | again1:
|
5899 | 71e3ceb8 | ths | len = write(fds[1], &status, 1); |
5900 | 71e3ceb8 | ths | if (len == -1 && (errno == EINTR)) |
5901 | 71e3ceb8 | ths | goto again1;
|
5902 | 71e3ceb8 | ths | |
5903 | 71e3ceb8 | ths | if (len != 1) |
5904 | 71e3ceb8 | ths | exit(1);
|
5905 | 71e3ceb8 | ths | |
5906 | bd54b863 | aliguori | chdir("/");
|
5907 | aeb30be6 | balrog | TFR(fd = open("/dev/null", O_RDWR));
|
5908 | 71e3ceb8 | ths | if (fd == -1) |
5909 | 71e3ceb8 | ths | exit(1);
|
5910 | 0858532e | aliguori | } |
5911 | 71e3ceb8 | ths | |
5912 | 0858532e | aliguori | if (run_as) {
|
5913 | 0858532e | aliguori | pwd = getpwnam(run_as); |
5914 | 0858532e | aliguori | if (!pwd) {
|
5915 | 0858532e | aliguori | fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
|
5916 | 0858532e | aliguori | exit(1);
|
5917 | 0858532e | aliguori | } |
5918 | 0858532e | aliguori | } |
5919 | 0858532e | aliguori | |
5920 | 0858532e | aliguori | if (chroot_dir) {
|
5921 | 0858532e | aliguori | if (chroot(chroot_dir) < 0) { |
5922 | 0858532e | aliguori | fprintf(stderr, "chroot failed\n");
|
5923 | 0858532e | aliguori | exit(1);
|
5924 | 0858532e | aliguori | } |
5925 | 0858532e | aliguori | chdir("/");
|
5926 | 0858532e | aliguori | } |
5927 | 0858532e | aliguori | |
5928 | 0858532e | aliguori | if (run_as) {
|
5929 | 0858532e | aliguori | if (setgid(pwd->pw_gid) < 0) { |
5930 | 0858532e | aliguori | fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
|
5931 | 0858532e | aliguori | exit(1);
|
5932 | 0858532e | aliguori | } |
5933 | 0858532e | aliguori | if (setuid(pwd->pw_uid) < 0) { |
5934 | 0858532e | aliguori | fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
|
5935 | 0858532e | aliguori | exit(1);
|
5936 | 0858532e | aliguori | } |
5937 | 0858532e | aliguori | if (setuid(0) != -1) { |
5938 | 0858532e | aliguori | fprintf(stderr, "Dropping privileges failed\n");
|
5939 | 0858532e | aliguori | exit(1);
|
5940 | 0858532e | aliguori | } |
5941 | 0858532e | aliguori | } |
5942 | 0858532e | aliguori | |
5943 | 0858532e | aliguori | if (daemonize) {
|
5944 | 0858532e | aliguori | dup2(fd, 0);
|
5945 | 0858532e | aliguori | dup2(fd, 1);
|
5946 | 0858532e | aliguori | dup2(fd, 2);
|
5947 | 71e3ceb8 | ths | |
5948 | 0858532e | aliguori | close(fd); |
5949 | 71e3ceb8 | ths | } |
5950 | b9e82a59 | blueswir1 | #endif
|
5951 | 71e3ceb8 | ths | |
5952 | 8a7ddc38 | bellard | main_loop(); |
5953 | 40c3bac3 | bellard | quit_timers(); |
5954 | 63a01ef8 | aliguori | net_cleanup(); |
5955 | b46a8906 | ths | |
5956 | 0824d6fc | bellard | return 0; |
5957 | 0824d6fc | bellard | } |