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