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