]>
Commit | Line | Data |
---|---|---|
ad96090a BS |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
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. | |
23 | */ | |
24 | #include <stdint.h> | |
25 | #include <stdarg.h> | |
b2e0a138 | 26 | #include <stdlib.h> |
ad96090a | 27 | #ifndef _WIN32 |
1c47cb16 | 28 | #include <sys/types.h> |
ad96090a BS |
29 | #include <sys/mman.h> |
30 | #endif | |
31 | #include "config.h" | |
32 | #include "monitor.h" | |
33 | #include "sysemu.h" | |
34 | #include "arch_init.h" | |
35 | #include "audio/audio.h" | |
36 | #include "hw/pc.h" | |
37 | #include "hw/pci.h" | |
38 | #include "hw/audiodev.h" | |
39 | #include "kvm.h" | |
40 | #include "migration.h" | |
41 | #include "net.h" | |
42 | #include "gdbstub.h" | |
43 | #include "hw/smbios.h" | |
86e775c6 | 44 | #include "exec-memory.h" |
302fe51b | 45 | #include "hw/pcspk.h" |
17ad9b35 | 46 | #include "qemu/page_cache.h" |
99afc91d | 47 | #include "qmp-commands.h" |
ad96090a | 48 | |
3a697f69 OW |
49 | #ifdef DEBUG_ARCH_INIT |
50 | #define DPRINTF(fmt, ...) \ | |
51 | do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0) | |
52 | #else | |
53 | #define DPRINTF(fmt, ...) \ | |
54 | do { } while (0) | |
55 | #endif | |
56 | ||
ad96090a BS |
57 | #ifdef TARGET_SPARC |
58 | int graphic_width = 1024; | |
59 | int graphic_height = 768; | |
60 | int graphic_depth = 8; | |
61 | #else | |
62 | int graphic_width = 800; | |
63 | int graphic_height = 600; | |
64 | int graphic_depth = 15; | |
65 | #endif | |
66 | ||
ad96090a BS |
67 | |
68 | #if defined(TARGET_ALPHA) | |
69 | #define QEMU_ARCH QEMU_ARCH_ALPHA | |
70 | #elif defined(TARGET_ARM) | |
71 | #define QEMU_ARCH QEMU_ARCH_ARM | |
72 | #elif defined(TARGET_CRIS) | |
73 | #define QEMU_ARCH QEMU_ARCH_CRIS | |
74 | #elif defined(TARGET_I386) | |
75 | #define QEMU_ARCH QEMU_ARCH_I386 | |
76 | #elif defined(TARGET_M68K) | |
77 | #define QEMU_ARCH QEMU_ARCH_M68K | |
81ea0e13 MW |
78 | #elif defined(TARGET_LM32) |
79 | #define QEMU_ARCH QEMU_ARCH_LM32 | |
ad96090a BS |
80 | #elif defined(TARGET_MICROBLAZE) |
81 | #define QEMU_ARCH QEMU_ARCH_MICROBLAZE | |
82 | #elif defined(TARGET_MIPS) | |
83 | #define QEMU_ARCH QEMU_ARCH_MIPS | |
e67db06e JL |
84 | #elif defined(TARGET_OPENRISC) |
85 | #define QEMU_ARCH QEMU_ARCH_OPENRISC | |
ad96090a BS |
86 | #elif defined(TARGET_PPC) |
87 | #define QEMU_ARCH QEMU_ARCH_PPC | |
88 | #elif defined(TARGET_S390X) | |
89 | #define QEMU_ARCH QEMU_ARCH_S390X | |
90 | #elif defined(TARGET_SH4) | |
91 | #define QEMU_ARCH QEMU_ARCH_SH4 | |
92 | #elif defined(TARGET_SPARC) | |
93 | #define QEMU_ARCH QEMU_ARCH_SPARC | |
2328826b MF |
94 | #elif defined(TARGET_XTENSA) |
95 | #define QEMU_ARCH QEMU_ARCH_XTENSA | |
4f23a1e6 GX |
96 | #elif defined(TARGET_UNICORE32) |
97 | #define QEMU_ARCH QEMU_ARCH_UNICORE32 | |
ad96090a BS |
98 | #endif |
99 | ||
100 | const uint32_t arch_type = QEMU_ARCH; | |
101 | ||
102 | /***********************************************************/ | |
103 | /* ram save/restore */ | |
104 | ||
d20878d2 YT |
105 | #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */ |
106 | #define RAM_SAVE_FLAG_COMPRESS 0x02 | |
107 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 | |
108 | #define RAM_SAVE_FLAG_PAGE 0x08 | |
109 | #define RAM_SAVE_FLAG_EOS 0x10 | |
110 | #define RAM_SAVE_FLAG_CONTINUE 0x20 | |
17ad9b35 | 111 | #define RAM_SAVE_FLAG_XBZRLE 0x40 |
ad96090a | 112 | |
86003615 PB |
113 | #ifdef __ALTIVEC__ |
114 | #include <altivec.h> | |
115 | #define VECTYPE vector unsigned char | |
116 | #define SPLAT(p) vec_splat(vec_ld(0, p), 0) | |
117 | #define ALL_EQ(v1, v2) vec_all_eq(v1, v2) | |
f283edc4 AF |
118 | /* altivec.h may redefine the bool macro as vector type. |
119 | * Reset it to POSIX semantics. */ | |
120 | #undef bool | |
121 | #define bool _Bool | |
86003615 PB |
122 | #elif defined __SSE2__ |
123 | #include <emmintrin.h> | |
124 | #define VECTYPE __m128i | |
125 | #define SPLAT(p) _mm_set1_epi8(*(p)) | |
126 | #define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF) | |
127 | #else | |
128 | #define VECTYPE unsigned long | |
129 | #define SPLAT(p) (*(p) * (~0UL / 255)) | |
130 | #define ALL_EQ(v1, v2) ((v1) == (v2)) | |
131 | #endif | |
132 | ||
b5a8fe5e | 133 | |
756557de EH |
134 | static struct defconfig_file { |
135 | const char *filename; | |
f29a5614 EH |
136 | /* Indicates it is an user config file (disabled by -no-user-config) */ |
137 | bool userconfig; | |
756557de | 138 | } default_config_files[] = { |
e2d87bff | 139 | { CONFIG_QEMU_DATADIR "/cpus-" TARGET_ARCH ".conf", false }, |
f29a5614 EH |
140 | { CONFIG_QEMU_CONFDIR "/qemu.conf", true }, |
141 | { CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf", true }, | |
756557de EH |
142 | { NULL }, /* end of list */ |
143 | }; | |
144 | ||
145 | ||
f29a5614 | 146 | int qemu_read_default_config_files(bool userconfig) |
b5a8fe5e EH |
147 | { |
148 | int ret; | |
756557de | 149 | struct defconfig_file *f; |
b5a8fe5e | 150 | |
756557de | 151 | for (f = default_config_files; f->filename; f++) { |
f29a5614 EH |
152 | if (!userconfig && f->userconfig) { |
153 | continue; | |
154 | } | |
756557de EH |
155 | ret = qemu_read_config_file(f->filename); |
156 | if (ret < 0 && ret != -ENOENT) { | |
157 | return ret; | |
158 | } | |
b5a8fe5e | 159 | } |
756557de | 160 | |
b5a8fe5e EH |
161 | return 0; |
162 | } | |
163 | ||
86003615 | 164 | static int is_dup_page(uint8_t *page) |
ad96090a | 165 | { |
86003615 PB |
166 | VECTYPE *p = (VECTYPE *)page; |
167 | VECTYPE val = SPLAT(page); | |
ad96090a BS |
168 | int i; |
169 | ||
86003615 PB |
170 | for (i = 0; i < TARGET_PAGE_SIZE / sizeof(VECTYPE); i++) { |
171 | if (!ALL_EQ(val, p[i])) { | |
ad96090a BS |
172 | return 0; |
173 | } | |
174 | } | |
175 | ||
176 | return 1; | |
177 | } | |
178 | ||
17ad9b35 OW |
179 | /* struct contains XBZRLE cache and a static page |
180 | used by the compression */ | |
181 | static struct { | |
182 | /* buffer used for XBZRLE encoding */ | |
183 | uint8_t *encoded_buf; | |
184 | /* buffer for storing page content */ | |
185 | uint8_t *current_buf; | |
186 | /* buffer used for XBZRLE decoding */ | |
187 | uint8_t *decoded_buf; | |
188 | /* Cache for XBZRLE */ | |
189 | PageCache *cache; | |
190 | } XBZRLE = { | |
191 | .encoded_buf = NULL, | |
192 | .current_buf = NULL, | |
193 | .decoded_buf = NULL, | |
194 | .cache = NULL, | |
195 | }; | |
196 | ||
9e1ba4cc OW |
197 | |
198 | int64_t xbzrle_cache_resize(int64_t new_size) | |
199 | { | |
200 | if (XBZRLE.cache != NULL) { | |
201 | return cache_resize(XBZRLE.cache, new_size / TARGET_PAGE_SIZE) * | |
202 | TARGET_PAGE_SIZE; | |
203 | } | |
204 | return pow2floor(new_size); | |
205 | } | |
206 | ||
004d4c10 OW |
207 | /* accounting for migration statistics */ |
208 | typedef struct AccountingInfo { | |
209 | uint64_t dup_pages; | |
210 | uint64_t norm_pages; | |
211 | uint64_t iterations; | |
f36d55af OW |
212 | uint64_t xbzrle_bytes; |
213 | uint64_t xbzrle_pages; | |
214 | uint64_t xbzrle_cache_miss; | |
215 | uint64_t xbzrle_overflows; | |
004d4c10 OW |
216 | } AccountingInfo; |
217 | ||
218 | static AccountingInfo acct_info; | |
219 | ||
220 | static void acct_clear(void) | |
221 | { | |
222 | memset(&acct_info, 0, sizeof(acct_info)); | |
223 | } | |
224 | ||
225 | uint64_t dup_mig_bytes_transferred(void) | |
226 | { | |
227 | return acct_info.dup_pages * TARGET_PAGE_SIZE; | |
228 | } | |
229 | ||
230 | uint64_t dup_mig_pages_transferred(void) | |
231 | { | |
232 | return acct_info.dup_pages; | |
233 | } | |
234 | ||
235 | uint64_t norm_mig_bytes_transferred(void) | |
236 | { | |
237 | return acct_info.norm_pages * TARGET_PAGE_SIZE; | |
238 | } | |
239 | ||
240 | uint64_t norm_mig_pages_transferred(void) | |
241 | { | |
242 | return acct_info.norm_pages; | |
243 | } | |
244 | ||
f36d55af OW |
245 | uint64_t xbzrle_mig_bytes_transferred(void) |
246 | { | |
247 | return acct_info.xbzrle_bytes; | |
248 | } | |
249 | ||
250 | uint64_t xbzrle_mig_pages_transferred(void) | |
251 | { | |
252 | return acct_info.xbzrle_pages; | |
253 | } | |
254 | ||
255 | uint64_t xbzrle_mig_pages_cache_miss(void) | |
256 | { | |
257 | return acct_info.xbzrle_cache_miss; | |
258 | } | |
259 | ||
260 | uint64_t xbzrle_mig_pages_overflow(void) | |
261 | { | |
262 | return acct_info.xbzrle_overflows; | |
263 | } | |
264 | ||
0c51f43d OW |
265 | static void save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset, |
266 | int cont, int flag) | |
267 | { | |
268 | qemu_put_be64(f, offset | cont | flag); | |
269 | if (!cont) { | |
270 | qemu_put_byte(f, strlen(block->idstr)); | |
271 | qemu_put_buffer(f, (uint8_t *)block->idstr, | |
272 | strlen(block->idstr)); | |
273 | } | |
274 | ||
275 | } | |
276 | ||
17ad9b35 OW |
277 | #define ENCODING_FLAG_XBZRLE 0x1 |
278 | ||
279 | static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data, | |
280 | ram_addr_t current_addr, RAMBlock *block, | |
dd051c72 | 281 | ram_addr_t offset, int cont, bool last_stage) |
17ad9b35 OW |
282 | { |
283 | int encoded_len = 0, bytes_sent = -1; | |
284 | uint8_t *prev_cached_page; | |
285 | ||
286 | if (!cache_is_cached(XBZRLE.cache, current_addr)) { | |
dd051c72 JQ |
287 | if (!last_stage) { |
288 | cache_insert(XBZRLE.cache, current_addr, | |
289 | g_memdup(current_data, TARGET_PAGE_SIZE)); | |
290 | } | |
f36d55af | 291 | acct_info.xbzrle_cache_miss++; |
17ad9b35 OW |
292 | return -1; |
293 | } | |
294 | ||
295 | prev_cached_page = get_cached_data(XBZRLE.cache, current_addr); | |
296 | ||
297 | /* save current buffer into memory */ | |
298 | memcpy(XBZRLE.current_buf, current_data, TARGET_PAGE_SIZE); | |
299 | ||
300 | /* XBZRLE encoding (if there is no overflow) */ | |
301 | encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf, | |
302 | TARGET_PAGE_SIZE, XBZRLE.encoded_buf, | |
303 | TARGET_PAGE_SIZE); | |
304 | if (encoded_len == 0) { | |
305 | DPRINTF("Skipping unmodified page\n"); | |
306 | return 0; | |
307 | } else if (encoded_len == -1) { | |
308 | DPRINTF("Overflow\n"); | |
f36d55af | 309 | acct_info.xbzrle_overflows++; |
17ad9b35 OW |
310 | /* update data in the cache */ |
311 | memcpy(prev_cached_page, current_data, TARGET_PAGE_SIZE); | |
312 | return -1; | |
313 | } | |
314 | ||
315 | /* we need to update the data in the cache, in order to get the same data */ | |
dd051c72 JQ |
316 | if (!last_stage) { |
317 | memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE); | |
318 | } | |
17ad9b35 OW |
319 | |
320 | /* Send XBZRLE based compressed page */ | |
321 | save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_XBZRLE); | |
322 | qemu_put_byte(f, ENCODING_FLAG_XBZRLE); | |
323 | qemu_put_be16(f, encoded_len); | |
324 | qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len); | |
325 | bytes_sent = encoded_len + 1 + 2; | |
f36d55af OW |
326 | acct_info.xbzrle_pages++; |
327 | acct_info.xbzrle_bytes += bytes_sent; | |
17ad9b35 OW |
328 | |
329 | return bytes_sent; | |
330 | } | |
331 | ||
760e77ea AW |
332 | static RAMBlock *last_block; |
333 | static ram_addr_t last_offset; | |
334 | ||
6c779f22 OW |
335 | /* |
336 | * ram_save_block: Writes a page of memory to the stream f | |
337 | * | |
338 | * Returns: 0: if the page hasn't changed | |
339 | * -1: if there are no more dirty pages | |
340 | * n: the amount of bytes written in other case | |
341 | */ | |
342 | ||
dd051c72 | 343 | static int ram_save_block(QEMUFile *f, bool last_stage) |
ad96090a | 344 | { |
e44359c3 AW |
345 | RAMBlock *block = last_block; |
346 | ram_addr_t offset = last_offset; | |
6c779f22 | 347 | int bytes_sent = -1; |
71c510e2 | 348 | MemoryRegion *mr; |
17ad9b35 | 349 | ram_addr_t current_addr; |
ad96090a | 350 | |
e44359c3 AW |
351 | if (!block) |
352 | block = QLIST_FIRST(&ram_list.blocks); | |
353 | ||
e44359c3 | 354 | do { |
71c510e2 | 355 | mr = block->mr; |
cd7a45c9 BS |
356 | if (memory_region_get_dirty(mr, offset, TARGET_PAGE_SIZE, |
357 | DIRTY_MEMORY_MIGRATION)) { | |
ad96090a | 358 | uint8_t *p; |
a55bbe31 | 359 | int cont = (block == last_block) ? RAM_SAVE_FLAG_CONTINUE : 0; |
ad96090a | 360 | |
71c510e2 AK |
361 | memory_region_reset_dirty(mr, offset, TARGET_PAGE_SIZE, |
362 | DIRTY_MEMORY_MIGRATION); | |
ad96090a | 363 | |
71c510e2 | 364 | p = memory_region_get_ram_ptr(mr) + offset; |
ad96090a | 365 | |
86003615 | 366 | if (is_dup_page(p)) { |
004d4c10 | 367 | acct_info.dup_pages++; |
0c51f43d | 368 | save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_COMPRESS); |
ad96090a | 369 | qemu_put_byte(f, *p); |
3fc250b4 | 370 | bytes_sent = 1; |
17ad9b35 OW |
371 | } else if (migrate_use_xbzrle()) { |
372 | current_addr = block->offset + offset; | |
373 | bytes_sent = save_xbzrle_page(f, p, current_addr, block, | |
dd051c72 JQ |
374 | offset, cont, last_stage); |
375 | if (!last_stage) { | |
376 | p = get_cached_data(XBZRLE.cache, current_addr); | |
377 | } | |
17ad9b35 OW |
378 | } |
379 | ||
380 | /* either we didn't send yet (we may have had XBZRLE overflow) */ | |
381 | if (bytes_sent == -1) { | |
0c51f43d | 382 | save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE); |
ad96090a | 383 | qemu_put_buffer(f, p, TARGET_PAGE_SIZE); |
3fc250b4 | 384 | bytes_sent = TARGET_PAGE_SIZE; |
004d4c10 | 385 | acct_info.norm_pages++; |
ad96090a BS |
386 | } |
387 | ||
17ad9b35 OW |
388 | /* if page is unmodified, continue to the next */ |
389 | if (bytes_sent != 0) { | |
390 | break; | |
391 | } | |
ad96090a | 392 | } |
e44359c3 AW |
393 | |
394 | offset += TARGET_PAGE_SIZE; | |
395 | if (offset >= block->length) { | |
396 | offset = 0; | |
397 | block = QLIST_NEXT(block, next); | |
398 | if (!block) | |
399 | block = QLIST_FIRST(&ram_list.blocks); | |
400 | } | |
71c510e2 | 401 | } while (block != last_block || offset != last_offset); |
e44359c3 AW |
402 | |
403 | last_block = block; | |
404 | last_offset = offset; | |
ad96090a | 405 | |
3fc250b4 | 406 | return bytes_sent; |
ad96090a BS |
407 | } |
408 | ||
409 | static uint64_t bytes_transferred; | |
410 | ||
411 | static ram_addr_t ram_save_remaining(void) | |
412 | { | |
45f33f01 | 413 | return ram_list.dirty_pages; |
ad96090a BS |
414 | } |
415 | ||
416 | uint64_t ram_bytes_remaining(void) | |
417 | { | |
418 | return ram_save_remaining() * TARGET_PAGE_SIZE; | |
419 | } | |
420 | ||
421 | uint64_t ram_bytes_transferred(void) | |
422 | { | |
423 | return bytes_transferred; | |
424 | } | |
425 | ||
426 | uint64_t ram_bytes_total(void) | |
427 | { | |
d17b5288 AW |
428 | RAMBlock *block; |
429 | uint64_t total = 0; | |
430 | ||
431 | QLIST_FOREACH(block, &ram_list.blocks, next) | |
432 | total += block->length; | |
433 | ||
434 | return total; | |
ad96090a BS |
435 | } |
436 | ||
b2e0a138 MT |
437 | static int block_compar(const void *a, const void *b) |
438 | { | |
439 | RAMBlock * const *ablock = a; | |
440 | RAMBlock * const *bblock = b; | |
8fec98b4 AK |
441 | |
442 | return strcmp((*ablock)->idstr, (*bblock)->idstr); | |
b2e0a138 MT |
443 | } |
444 | ||
445 | static void sort_ram_list(void) | |
446 | { | |
447 | RAMBlock *block, *nblock, **blocks; | |
448 | int n; | |
449 | n = 0; | |
450 | QLIST_FOREACH(block, &ram_list.blocks, next) { | |
451 | ++n; | |
452 | } | |
7267c094 | 453 | blocks = g_malloc(n * sizeof *blocks); |
b2e0a138 MT |
454 | n = 0; |
455 | QLIST_FOREACH_SAFE(block, &ram_list.blocks, next, nblock) { | |
456 | blocks[n++] = block; | |
457 | QLIST_REMOVE(block, next); | |
458 | } | |
459 | qsort(blocks, n, sizeof *blocks, block_compar); | |
460 | while (--n >= 0) { | |
461 | QLIST_INSERT_HEAD(&ram_list.blocks, blocks[n], next); | |
462 | } | |
7267c094 | 463 | g_free(blocks); |
b2e0a138 MT |
464 | } |
465 | ||
8e21cd32 OW |
466 | static void migration_end(void) |
467 | { | |
468 | memory_global_dirty_log_stop(); | |
17ad9b35 OW |
469 | |
470 | if (migrate_use_xbzrle()) { | |
471 | cache_fini(XBZRLE.cache); | |
472 | g_free(XBZRLE.cache); | |
473 | g_free(XBZRLE.encoded_buf); | |
474 | g_free(XBZRLE.current_buf); | |
475 | g_free(XBZRLE.decoded_buf); | |
476 | XBZRLE.cache = NULL; | |
477 | } | |
8e21cd32 OW |
478 | } |
479 | ||
9b5bfab0 JQ |
480 | static void ram_migration_cancel(void *opaque) |
481 | { | |
482 | migration_end(); | |
483 | } | |
484 | ||
4508bd9e JQ |
485 | #define MAX_WAIT 50 /* ms, half buffered_file limit */ |
486 | ||
d1315aac | 487 | static int ram_save_setup(QEMUFile *f, void *opaque) |
ad96090a BS |
488 | { |
489 | ram_addr_t addr; | |
d1315aac | 490 | RAMBlock *block; |
ad96090a | 491 | |
d1315aac JQ |
492 | bytes_transferred = 0; |
493 | last_block = NULL; | |
494 | last_offset = 0; | |
495 | sort_ram_list(); | |
ad96090a | 496 | |
17ad9b35 OW |
497 | if (migrate_use_xbzrle()) { |
498 | XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() / | |
499 | TARGET_PAGE_SIZE, | |
500 | TARGET_PAGE_SIZE); | |
501 | if (!XBZRLE.cache) { | |
502 | DPRINTF("Error creating cache\n"); | |
503 | return -1; | |
504 | } | |
505 | XBZRLE.encoded_buf = g_malloc0(TARGET_PAGE_SIZE); | |
506 | XBZRLE.current_buf = g_malloc(TARGET_PAGE_SIZE); | |
004d4c10 | 507 | acct_clear(); |
17ad9b35 OW |
508 | } |
509 | ||
d1315aac JQ |
510 | /* Make sure all dirty bits are set */ |
511 | QLIST_FOREACH(block, &ram_list.blocks, next) { | |
512 | for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) { | |
513 | if (!memory_region_get_dirty(block->mr, addr, TARGET_PAGE_SIZE, | |
514 | DIRTY_MEMORY_MIGRATION)) { | |
515 | memory_region_set_dirty(block->mr, addr, TARGET_PAGE_SIZE); | |
ad96090a BS |
516 | } |
517 | } | |
d1315aac | 518 | } |
ad96090a | 519 | |
d1315aac | 520 | memory_global_dirty_log_start(); |
ad96090a | 521 | |
d1315aac | 522 | qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE); |
97ab12d4 | 523 | |
d1315aac JQ |
524 | QLIST_FOREACH(block, &ram_list.blocks, next) { |
525 | qemu_put_byte(f, strlen(block->idstr)); | |
526 | qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr)); | |
527 | qemu_put_be64(f, block->length); | |
ad96090a BS |
528 | } |
529 | ||
d1315aac JQ |
530 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
531 | ||
532 | return 0; | |
533 | } | |
534 | ||
16310a3c | 535 | static int ram_save_iterate(QEMUFile *f, void *opaque) |
d1315aac JQ |
536 | { |
537 | uint64_t bytes_transferred_last; | |
538 | double bwidth = 0; | |
539 | int ret; | |
540 | int i; | |
16310a3c | 541 | uint64_t expected_time; |
d1315aac | 542 | |
ad96090a BS |
543 | bytes_transferred_last = bytes_transferred; |
544 | bwidth = qemu_get_clock_ns(rt_clock); | |
545 | ||
4508bd9e | 546 | i = 0; |
2975725f | 547 | while ((ret = qemu_file_rate_limit(f)) == 0) { |
3fc250b4 | 548 | int bytes_sent; |
ad96090a | 549 | |
dd051c72 | 550 | bytes_sent = ram_save_block(f, false); |
6c779f22 OW |
551 | /* no more blocks to sent */ |
552 | if (bytes_sent < 0) { | |
ad96090a BS |
553 | break; |
554 | } | |
6c779f22 | 555 | bytes_transferred += bytes_sent; |
004d4c10 | 556 | acct_info.iterations++; |
4508bd9e JQ |
557 | /* we want to check in the 1st loop, just in case it was the 1st time |
558 | and we had to sync the dirty bitmap. | |
559 | qemu_get_clock_ns() is a bit expensive, so we only check each some | |
560 | iterations | |
561 | */ | |
562 | if ((i & 63) == 0) { | |
563 | uint64_t t1 = (qemu_get_clock_ns(rt_clock) - bwidth) / 1000000; | |
564 | if (t1 > MAX_WAIT) { | |
ef37a699 | 565 | DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n", |
4508bd9e JQ |
566 | t1, i); |
567 | break; | |
568 | } | |
569 | } | |
570 | i++; | |
ad96090a BS |
571 | } |
572 | ||
2975725f JQ |
573 | if (ret < 0) { |
574 | return ret; | |
575 | } | |
576 | ||
ad96090a BS |
577 | bwidth = qemu_get_clock_ns(rt_clock) - bwidth; |
578 | bwidth = (bytes_transferred - bytes_transferred_last) / bwidth; | |
579 | ||
580 | /* if we haven't transferred anything this round, force expected_time to a | |
581 | * a very high value, but without crashing */ | |
582 | if (bwidth == 0) { | |
583 | bwidth = 0.000001; | |
584 | } | |
585 | ||
16310a3c JQ |
586 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
587 | ||
588 | expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; | |
589 | ||
ef37a699 | 590 | DPRINTF("ram_save_live: expected(%" PRIu64 ") <= max(%" PRIu64 ")?\n", |
16310a3c JQ |
591 | expected_time, migrate_max_downtime()); |
592 | ||
00d94f3f JQ |
593 | if (expected_time <= migrate_max_downtime()) { |
594 | memory_global_sync_dirty_bitmap(get_system_memory()); | |
595 | expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; | |
596 | ||
597 | return expected_time <= migrate_max_downtime(); | |
598 | } | |
599 | return 0; | |
16310a3c JQ |
600 | } |
601 | ||
602 | static int ram_save_complete(QEMUFile *f, void *opaque) | |
603 | { | |
16310a3c JQ |
604 | memory_global_sync_dirty_bitmap(get_system_memory()); |
605 | ||
ad96090a | 606 | /* try transferring iterative blocks of memory */ |
3a697f69 | 607 | |
16310a3c | 608 | /* flush all remaining blocks regardless of rate limiting */ |
6c779f22 | 609 | while (true) { |
3fc250b4 PR |
610 | int bytes_sent; |
611 | ||
dd051c72 | 612 | bytes_sent = ram_save_block(f, true); |
6c779f22 OW |
613 | /* no more blocks to sent */ |
614 | if (bytes_sent < 0) { | |
615 | break; | |
ad96090a | 616 | } |
16310a3c | 617 | bytes_transferred += bytes_sent; |
ad96090a | 618 | } |
16310a3c | 619 | memory_global_dirty_log_stop(); |
ad96090a BS |
620 | |
621 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); | |
622 | ||
5b3c9638 | 623 | return 0; |
ad96090a BS |
624 | } |
625 | ||
17ad9b35 OW |
626 | static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host) |
627 | { | |
628 | int ret, rc = 0; | |
629 | unsigned int xh_len; | |
630 | int xh_flags; | |
631 | ||
632 | if (!XBZRLE.decoded_buf) { | |
633 | XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE); | |
634 | } | |
635 | ||
636 | /* extract RLE header */ | |
637 | xh_flags = qemu_get_byte(f); | |
638 | xh_len = qemu_get_be16(f); | |
639 | ||
640 | if (xh_flags != ENCODING_FLAG_XBZRLE) { | |
641 | fprintf(stderr, "Failed to load XBZRLE page - wrong compression!\n"); | |
642 | return -1; | |
643 | } | |
644 | ||
645 | if (xh_len > TARGET_PAGE_SIZE) { | |
646 | fprintf(stderr, "Failed to load XBZRLE page - len overflow!\n"); | |
647 | return -1; | |
648 | } | |
649 | /* load data and decode */ | |
650 | qemu_get_buffer(f, XBZRLE.decoded_buf, xh_len); | |
651 | ||
652 | /* decode RLE */ | |
653 | ret = xbzrle_decode_buffer(XBZRLE.decoded_buf, xh_len, host, | |
654 | TARGET_PAGE_SIZE); | |
655 | if (ret == -1) { | |
656 | fprintf(stderr, "Failed to load XBZRLE page - decode error!\n"); | |
657 | rc = -1; | |
658 | } else if (ret > TARGET_PAGE_SIZE) { | |
659 | fprintf(stderr, "Failed to load XBZRLE page - size %d exceeds %d!\n", | |
660 | ret, TARGET_PAGE_SIZE); | |
661 | abort(); | |
662 | } | |
663 | ||
664 | return rc; | |
665 | } | |
666 | ||
a55bbe31 AW |
667 | static inline void *host_from_stream_offset(QEMUFile *f, |
668 | ram_addr_t offset, | |
669 | int flags) | |
670 | { | |
671 | static RAMBlock *block = NULL; | |
672 | char id[256]; | |
673 | uint8_t len; | |
674 | ||
675 | if (flags & RAM_SAVE_FLAG_CONTINUE) { | |
676 | if (!block) { | |
677 | fprintf(stderr, "Ack, bad migration stream!\n"); | |
678 | return NULL; | |
679 | } | |
680 | ||
dc94a7ed | 681 | return memory_region_get_ram_ptr(block->mr) + offset; |
a55bbe31 AW |
682 | } |
683 | ||
684 | len = qemu_get_byte(f); | |
685 | qemu_get_buffer(f, (uint8_t *)id, len); | |
686 | id[len] = 0; | |
687 | ||
688 | QLIST_FOREACH(block, &ram_list.blocks, next) { | |
689 | if (!strncmp(id, block->idstr, sizeof(id))) | |
dc94a7ed | 690 | return memory_region_get_ram_ptr(block->mr) + offset; |
a55bbe31 AW |
691 | } |
692 | ||
693 | fprintf(stderr, "Can't find block %s!\n", id); | |
694 | return NULL; | |
695 | } | |
696 | ||
7908c78d | 697 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
ad96090a BS |
698 | { |
699 | ram_addr_t addr; | |
3a697f69 | 700 | int flags, ret = 0; |
42802d47 | 701 | int error; |
3a697f69 OW |
702 | static uint64_t seq_iter; |
703 | ||
704 | seq_iter++; | |
ad96090a | 705 | |
f09f2189 | 706 | if (version_id < 4 || version_id > 4) { |
ad96090a BS |
707 | return -EINVAL; |
708 | } | |
709 | ||
710 | do { | |
711 | addr = qemu_get_be64(f); | |
712 | ||
713 | flags = addr & ~TARGET_PAGE_MASK; | |
714 | addr &= TARGET_PAGE_MASK; | |
715 | ||
716 | if (flags & RAM_SAVE_FLAG_MEM_SIZE) { | |
f09f2189 | 717 | if (version_id == 4) { |
97ab12d4 AW |
718 | /* Synchronize RAM block list */ |
719 | char id[256]; | |
720 | ram_addr_t length; | |
721 | ram_addr_t total_ram_bytes = addr; | |
722 | ||
723 | while (total_ram_bytes) { | |
724 | RAMBlock *block; | |
725 | uint8_t len; | |
726 | ||
727 | len = qemu_get_byte(f); | |
728 | qemu_get_buffer(f, (uint8_t *)id, len); | |
729 | id[len] = 0; | |
730 | length = qemu_get_be64(f); | |
731 | ||
732 | QLIST_FOREACH(block, &ram_list.blocks, next) { | |
733 | if (!strncmp(id, block->idstr, sizeof(id))) { | |
3a697f69 OW |
734 | if (block->length != length) { |
735 | ret = -EINVAL; | |
736 | goto done; | |
737 | } | |
97ab12d4 AW |
738 | break; |
739 | } | |
740 | } | |
741 | ||
742 | if (!block) { | |
fb787f81 AW |
743 | fprintf(stderr, "Unknown ramblock \"%s\", cannot " |
744 | "accept migration\n", id); | |
3a697f69 OW |
745 | ret = -EINVAL; |
746 | goto done; | |
97ab12d4 AW |
747 | } |
748 | ||
749 | total_ram_bytes -= length; | |
750 | } | |
ad96090a BS |
751 | } |
752 | } | |
753 | ||
754 | if (flags & RAM_SAVE_FLAG_COMPRESS) { | |
97ab12d4 AW |
755 | void *host; |
756 | uint8_t ch; | |
757 | ||
f09f2189 | 758 | host = host_from_stream_offset(f, addr, flags); |
492fb99c MT |
759 | if (!host) { |
760 | return -EINVAL; | |
761 | } | |
97ab12d4 | 762 | |
97ab12d4 AW |
763 | ch = qemu_get_byte(f); |
764 | memset(host, ch, TARGET_PAGE_SIZE); | |
ad96090a BS |
765 | #ifndef _WIN32 |
766 | if (ch == 0 && | |
767 | (!kvm_enabled() || kvm_has_sync_mmu())) { | |
e78815a5 | 768 | qemu_madvise(host, TARGET_PAGE_SIZE, QEMU_MADV_DONTNEED); |
ad96090a BS |
769 | } |
770 | #endif | |
771 | } else if (flags & RAM_SAVE_FLAG_PAGE) { | |
97ab12d4 AW |
772 | void *host; |
773 | ||
f09f2189 | 774 | host = host_from_stream_offset(f, addr, flags); |
0ff1f9f5 OW |
775 | if (!host) { |
776 | return -EINVAL; | |
777 | } | |
97ab12d4 | 778 | |
97ab12d4 | 779 | qemu_get_buffer(f, host, TARGET_PAGE_SIZE); |
17ad9b35 OW |
780 | } else if (flags & RAM_SAVE_FLAG_XBZRLE) { |
781 | if (!migrate_use_xbzrle()) { | |
782 | return -EINVAL; | |
783 | } | |
784 | void *host = host_from_stream_offset(f, addr, flags); | |
785 | if (!host) { | |
786 | return -EINVAL; | |
787 | } | |
788 | ||
789 | if (load_xbzrle(f, addr, host) < 0) { | |
790 | ret = -EINVAL; | |
791 | goto done; | |
792 | } | |
ad96090a | 793 | } |
42802d47 JQ |
794 | error = qemu_file_get_error(f); |
795 | if (error) { | |
3a697f69 OW |
796 | ret = error; |
797 | goto done; | |
ad96090a BS |
798 | } |
799 | } while (!(flags & RAM_SAVE_FLAG_EOS)); | |
800 | ||
3a697f69 | 801 | done: |
ef37a699 IM |
802 | DPRINTF("Completed load of VM with exit code %d seq iteration " |
803 | "%" PRIu64 "\n", ret, seq_iter); | |
3a697f69 | 804 | return ret; |
ad96090a BS |
805 | } |
806 | ||
7908c78d | 807 | SaveVMHandlers savevm_ram_handlers = { |
d1315aac | 808 | .save_live_setup = ram_save_setup, |
16310a3c JQ |
809 | .save_live_iterate = ram_save_iterate, |
810 | .save_live_complete = ram_save_complete, | |
7908c78d | 811 | .load_state = ram_load, |
9b5bfab0 | 812 | .cancel = ram_migration_cancel, |
7908c78d JQ |
813 | }; |
814 | ||
ad96090a | 815 | #ifdef HAS_AUDIO |
0dfa5ef9 IY |
816 | struct soundhw { |
817 | const char *name; | |
818 | const char *descr; | |
819 | int enabled; | |
820 | int isa; | |
821 | union { | |
4a0f031d | 822 | int (*init_isa) (ISABus *bus); |
0dfa5ef9 IY |
823 | int (*init_pci) (PCIBus *bus); |
824 | } init; | |
825 | }; | |
826 | ||
827 | static struct soundhw soundhw[] = { | |
ad96090a | 828 | #ifdef HAS_AUDIO_CHOICE |
da12872a | 829 | #ifdef CONFIG_PCSPK |
ad96090a BS |
830 | { |
831 | "pcspk", | |
832 | "PC speaker", | |
833 | 0, | |
834 | 1, | |
835 | { .init_isa = pcspk_audio_init } | |
836 | }, | |
837 | #endif | |
838 | ||
839 | #ifdef CONFIG_SB16 | |
840 | { | |
841 | "sb16", | |
842 | "Creative Sound Blaster 16", | |
843 | 0, | |
844 | 1, | |
845 | { .init_isa = SB16_init } | |
846 | }, | |
847 | #endif | |
848 | ||
849 | #ifdef CONFIG_CS4231A | |
850 | { | |
851 | "cs4231a", | |
852 | "CS4231A", | |
853 | 0, | |
854 | 1, | |
855 | { .init_isa = cs4231a_init } | |
856 | }, | |
857 | #endif | |
858 | ||
859 | #ifdef CONFIG_ADLIB | |
860 | { | |
861 | "adlib", | |
862 | #ifdef HAS_YMF262 | |
863 | "Yamaha YMF262 (OPL3)", | |
864 | #else | |
865 | "Yamaha YM3812 (OPL2)", | |
866 | #endif | |
867 | 0, | |
868 | 1, | |
869 | { .init_isa = Adlib_init } | |
870 | }, | |
871 | #endif | |
872 | ||
873 | #ifdef CONFIG_GUS | |
874 | { | |
875 | "gus", | |
876 | "Gravis Ultrasound GF1", | |
877 | 0, | |
878 | 1, | |
879 | { .init_isa = GUS_init } | |
880 | }, | |
881 | #endif | |
882 | ||
883 | #ifdef CONFIG_AC97 | |
884 | { | |
885 | "ac97", | |
886 | "Intel 82801AA AC97 Audio", | |
887 | 0, | |
888 | 0, | |
889 | { .init_pci = ac97_init } | |
890 | }, | |
891 | #endif | |
892 | ||
893 | #ifdef CONFIG_ES1370 | |
894 | { | |
895 | "es1370", | |
896 | "ENSONIQ AudioPCI ES1370", | |
897 | 0, | |
898 | 0, | |
899 | { .init_pci = es1370_init } | |
900 | }, | |
901 | #endif | |
902 | ||
d61a4ce8 GH |
903 | #ifdef CONFIG_HDA |
904 | { | |
905 | "hda", | |
906 | "Intel HD Audio", | |
907 | 0, | |
908 | 0, | |
909 | { .init_pci = intel_hda_and_codec_init } | |
910 | }, | |
911 | #endif | |
912 | ||
ad96090a BS |
913 | #endif /* HAS_AUDIO_CHOICE */ |
914 | ||
915 | { NULL, NULL, 0, 0, { NULL } } | |
916 | }; | |
917 | ||
918 | void select_soundhw(const char *optarg) | |
919 | { | |
920 | struct soundhw *c; | |
921 | ||
c8057f95 | 922 | if (is_help_option(optarg)) { |
ad96090a BS |
923 | show_valid_cards: |
924 | ||
925 | printf("Valid sound card names (comma separated):\n"); | |
926 | for (c = soundhw; c->name; ++c) { | |
927 | printf ("%-11s %s\n", c->name, c->descr); | |
928 | } | |
929 | printf("\n-soundhw all will enable all of the above\n"); | |
c8057f95 | 930 | exit(!is_help_option(optarg)); |
ad96090a BS |
931 | } |
932 | else { | |
933 | size_t l; | |
934 | const char *p; | |
935 | char *e; | |
936 | int bad_card = 0; | |
937 | ||
938 | if (!strcmp(optarg, "all")) { | |
939 | for (c = soundhw; c->name; ++c) { | |
940 | c->enabled = 1; | |
941 | } | |
942 | return; | |
943 | } | |
944 | ||
945 | p = optarg; | |
946 | while (*p) { | |
947 | e = strchr(p, ','); | |
948 | l = !e ? strlen(p) : (size_t) (e - p); | |
949 | ||
950 | for (c = soundhw; c->name; ++c) { | |
951 | if (!strncmp(c->name, p, l) && !c->name[l]) { | |
952 | c->enabled = 1; | |
953 | break; | |
954 | } | |
955 | } | |
956 | ||
957 | if (!c->name) { | |
958 | if (l > 80) { | |
959 | fprintf(stderr, | |
960 | "Unknown sound card name (too big to show)\n"); | |
961 | } | |
962 | else { | |
963 | fprintf(stderr, "Unknown sound card name `%.*s'\n", | |
964 | (int) l, p); | |
965 | } | |
966 | bad_card = 1; | |
967 | } | |
968 | p += l + (e != NULL); | |
969 | } | |
970 | ||
971 | if (bad_card) { | |
972 | goto show_valid_cards; | |
973 | } | |
974 | } | |
975 | } | |
0dfa5ef9 | 976 | |
4a0f031d | 977 | void audio_init(ISABus *isa_bus, PCIBus *pci_bus) |
0dfa5ef9 IY |
978 | { |
979 | struct soundhw *c; | |
980 | ||
981 | for (c = soundhw; c->name; ++c) { | |
982 | if (c->enabled) { | |
983 | if (c->isa) { | |
4a0f031d HP |
984 | if (isa_bus) { |
985 | c->init.init_isa(isa_bus); | |
0dfa5ef9 IY |
986 | } |
987 | } else { | |
988 | if (pci_bus) { | |
989 | c->init.init_pci(pci_bus); | |
990 | } | |
991 | } | |
992 | } | |
993 | } | |
994 | } | |
ad96090a BS |
995 | #else |
996 | void select_soundhw(const char *optarg) | |
997 | { | |
998 | } | |
4a0f031d | 999 | void audio_init(ISABus *isa_bus, PCIBus *pci_bus) |
0dfa5ef9 IY |
1000 | { |
1001 | } | |
ad96090a BS |
1002 | #endif |
1003 | ||
1004 | int qemu_uuid_parse(const char *str, uint8_t *uuid) | |
1005 | { | |
1006 | int ret; | |
1007 | ||
1008 | if (strlen(str) != 36) { | |
1009 | return -1; | |
1010 | } | |
1011 | ||
1012 | ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3], | |
1013 | &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9], | |
1014 | &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], | |
1015 | &uuid[15]); | |
1016 | ||
1017 | if (ret != 16) { | |
1018 | return -1; | |
1019 | } | |
1020 | #ifdef TARGET_I386 | |
1021 | smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid); | |
1022 | #endif | |
1023 | return 0; | |
1024 | } | |
1025 | ||
1026 | void do_acpitable_option(const char *optarg) | |
1027 | { | |
1028 | #ifdef TARGET_I386 | |
1029 | if (acpi_table_add(optarg) < 0) { | |
1030 | fprintf(stderr, "Wrong acpi table provided\n"); | |
1031 | exit(1); | |
1032 | } | |
1033 | #endif | |
1034 | } | |
1035 | ||
1036 | void do_smbios_option(const char *optarg) | |
1037 | { | |
1038 | #ifdef TARGET_I386 | |
1039 | if (smbios_entry_add(optarg) < 0) { | |
1040 | fprintf(stderr, "Wrong smbios provided\n"); | |
1041 | exit(1); | |
1042 | } | |
1043 | #endif | |
1044 | } | |
1045 | ||
1046 | void cpudef_init(void) | |
1047 | { | |
1048 | #if defined(cpudef_setup) | |
1049 | cpudef_setup(); /* parse cpu definitions in target config file */ | |
1050 | #endif | |
1051 | } | |
1052 | ||
1053 | int audio_available(void) | |
1054 | { | |
1055 | #ifdef HAS_AUDIO | |
1056 | return 1; | |
1057 | #else | |
1058 | return 0; | |
1059 | #endif | |
1060 | } | |
1061 | ||
303d4e86 AP |
1062 | int tcg_available(void) |
1063 | { | |
1064 | return 1; | |
1065 | } | |
1066 | ||
ad96090a BS |
1067 | int kvm_available(void) |
1068 | { | |
1069 | #ifdef CONFIG_KVM | |
1070 | return 1; | |
1071 | #else | |
1072 | return 0; | |
1073 | #endif | |
1074 | } | |
1075 | ||
1076 | int xen_available(void) | |
1077 | { | |
1078 | #ifdef CONFIG_XEN | |
1079 | return 1; | |
1080 | #else | |
1081 | return 0; | |
1082 | #endif | |
1083 | } | |
99afc91d DB |
1084 | |
1085 | ||
1086 | TargetInfo *qmp_query_target(Error **errp) | |
1087 | { | |
1088 | TargetInfo *info = g_malloc0(sizeof(*info)); | |
1089 | ||
1090 | info->arch = TARGET_TYPE; | |
1091 | ||
1092 | return info; | |
1093 | } |