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