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