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