]>
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" |
ad96090a | 46 | |
3a697f69 OW |
47 | #ifdef DEBUG_ARCH_INIT |
48 | #define DPRINTF(fmt, ...) \ | |
49 | do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0) | |
50 | #else | |
51 | #define DPRINTF(fmt, ...) \ | |
52 | do { } while (0) | |
53 | #endif | |
54 | ||
ad96090a BS |
55 | #ifdef TARGET_SPARC |
56 | int graphic_width = 1024; | |
57 | int graphic_height = 768; | |
58 | int graphic_depth = 8; | |
59 | #else | |
60 | int graphic_width = 800; | |
61 | int graphic_height = 600; | |
62 | int graphic_depth = 15; | |
63 | #endif | |
64 | ||
ad96090a BS |
65 | |
66 | #if defined(TARGET_ALPHA) | |
67 | #define QEMU_ARCH QEMU_ARCH_ALPHA | |
68 | #elif defined(TARGET_ARM) | |
69 | #define QEMU_ARCH QEMU_ARCH_ARM | |
70 | #elif defined(TARGET_CRIS) | |
71 | #define QEMU_ARCH QEMU_ARCH_CRIS | |
72 | #elif defined(TARGET_I386) | |
73 | #define QEMU_ARCH QEMU_ARCH_I386 | |
74 | #elif defined(TARGET_M68K) | |
75 | #define QEMU_ARCH QEMU_ARCH_M68K | |
81ea0e13 MW |
76 | #elif defined(TARGET_LM32) |
77 | #define QEMU_ARCH QEMU_ARCH_LM32 | |
ad96090a BS |
78 | #elif defined(TARGET_MICROBLAZE) |
79 | #define QEMU_ARCH QEMU_ARCH_MICROBLAZE | |
80 | #elif defined(TARGET_MIPS) | |
81 | #define QEMU_ARCH QEMU_ARCH_MIPS | |
e67db06e JL |
82 | #elif defined(TARGET_OPENRISC) |
83 | #define QEMU_ARCH QEMU_ARCH_OPENRISC | |
ad96090a BS |
84 | #elif defined(TARGET_PPC) |
85 | #define QEMU_ARCH QEMU_ARCH_PPC | |
86 | #elif defined(TARGET_S390X) | |
87 | #define QEMU_ARCH QEMU_ARCH_S390X | |
88 | #elif defined(TARGET_SH4) | |
89 | #define QEMU_ARCH QEMU_ARCH_SH4 | |
90 | #elif defined(TARGET_SPARC) | |
91 | #define QEMU_ARCH QEMU_ARCH_SPARC | |
2328826b MF |
92 | #elif defined(TARGET_XTENSA) |
93 | #define QEMU_ARCH QEMU_ARCH_XTENSA | |
4f23a1e6 GX |
94 | #elif defined(TARGET_UNICORE32) |
95 | #define QEMU_ARCH QEMU_ARCH_UNICORE32 | |
ad96090a BS |
96 | #endif |
97 | ||
98 | const uint32_t arch_type = QEMU_ARCH; | |
99 | ||
100 | /***********************************************************/ | |
101 | /* ram save/restore */ | |
102 | ||
d20878d2 YT |
103 | #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */ |
104 | #define RAM_SAVE_FLAG_COMPRESS 0x02 | |
105 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 | |
106 | #define RAM_SAVE_FLAG_PAGE 0x08 | |
107 | #define RAM_SAVE_FLAG_EOS 0x10 | |
108 | #define RAM_SAVE_FLAG_CONTINUE 0x20 | |
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 | ||
0c51f43d OW |
176 | static void save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset, |
177 | int cont, int flag) | |
178 | { | |
179 | qemu_put_be64(f, offset | cont | flag); | |
180 | if (!cont) { | |
181 | qemu_put_byte(f, strlen(block->idstr)); | |
182 | qemu_put_buffer(f, (uint8_t *)block->idstr, | |
183 | strlen(block->idstr)); | |
184 | } | |
185 | ||
186 | } | |
187 | ||
760e77ea AW |
188 | static RAMBlock *last_block; |
189 | static ram_addr_t last_offset; | |
190 | ||
6c779f22 OW |
191 | /* |
192 | * ram_save_block: Writes a page of memory to the stream f | |
193 | * | |
194 | * Returns: 0: if the page hasn't changed | |
195 | * -1: if there are no more dirty pages | |
196 | * n: the amount of bytes written in other case | |
197 | */ | |
198 | ||
ad96090a BS |
199 | static int ram_save_block(QEMUFile *f) |
200 | { | |
e44359c3 AW |
201 | RAMBlock *block = last_block; |
202 | ram_addr_t offset = last_offset; | |
6c779f22 | 203 | int bytes_sent = -1; |
71c510e2 | 204 | MemoryRegion *mr; |
ad96090a | 205 | |
e44359c3 AW |
206 | if (!block) |
207 | block = QLIST_FIRST(&ram_list.blocks); | |
208 | ||
e44359c3 | 209 | do { |
71c510e2 | 210 | mr = block->mr; |
cd7a45c9 BS |
211 | if (memory_region_get_dirty(mr, offset, TARGET_PAGE_SIZE, |
212 | DIRTY_MEMORY_MIGRATION)) { | |
ad96090a | 213 | uint8_t *p; |
a55bbe31 | 214 | int cont = (block == last_block) ? RAM_SAVE_FLAG_CONTINUE : 0; |
ad96090a | 215 | |
71c510e2 AK |
216 | memory_region_reset_dirty(mr, offset, TARGET_PAGE_SIZE, |
217 | DIRTY_MEMORY_MIGRATION); | |
ad96090a | 218 | |
71c510e2 | 219 | p = memory_region_get_ram_ptr(mr) + offset; |
ad96090a | 220 | |
86003615 | 221 | if (is_dup_page(p)) { |
0c51f43d | 222 | save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_COMPRESS); |
ad96090a | 223 | qemu_put_byte(f, *p); |
3fc250b4 | 224 | bytes_sent = 1; |
ad96090a | 225 | } else { |
0c51f43d | 226 | save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE); |
ad96090a | 227 | qemu_put_buffer(f, p, TARGET_PAGE_SIZE); |
3fc250b4 | 228 | bytes_sent = TARGET_PAGE_SIZE; |
ad96090a BS |
229 | } |
230 | ||
ad96090a BS |
231 | break; |
232 | } | |
e44359c3 AW |
233 | |
234 | offset += TARGET_PAGE_SIZE; | |
235 | if (offset >= block->length) { | |
236 | offset = 0; | |
237 | block = QLIST_NEXT(block, next); | |
238 | if (!block) | |
239 | block = QLIST_FIRST(&ram_list.blocks); | |
240 | } | |
71c510e2 | 241 | } while (block != last_block || offset != last_offset); |
e44359c3 AW |
242 | |
243 | last_block = block; | |
244 | last_offset = offset; | |
ad96090a | 245 | |
3fc250b4 | 246 | return bytes_sent; |
ad96090a BS |
247 | } |
248 | ||
249 | static uint64_t bytes_transferred; | |
250 | ||
251 | static ram_addr_t ram_save_remaining(void) | |
252 | { | |
45f33f01 | 253 | return ram_list.dirty_pages; |
ad96090a BS |
254 | } |
255 | ||
256 | uint64_t ram_bytes_remaining(void) | |
257 | { | |
258 | return ram_save_remaining() * TARGET_PAGE_SIZE; | |
259 | } | |
260 | ||
261 | uint64_t ram_bytes_transferred(void) | |
262 | { | |
263 | return bytes_transferred; | |
264 | } | |
265 | ||
266 | uint64_t ram_bytes_total(void) | |
267 | { | |
d17b5288 AW |
268 | RAMBlock *block; |
269 | uint64_t total = 0; | |
270 | ||
271 | QLIST_FOREACH(block, &ram_list.blocks, next) | |
272 | total += block->length; | |
273 | ||
274 | return total; | |
ad96090a BS |
275 | } |
276 | ||
b2e0a138 MT |
277 | static int block_compar(const void *a, const void *b) |
278 | { | |
279 | RAMBlock * const *ablock = a; | |
280 | RAMBlock * const *bblock = b; | |
8fec98b4 AK |
281 | |
282 | return strcmp((*ablock)->idstr, (*bblock)->idstr); | |
b2e0a138 MT |
283 | } |
284 | ||
285 | static void sort_ram_list(void) | |
286 | { | |
287 | RAMBlock *block, *nblock, **blocks; | |
288 | int n; | |
289 | n = 0; | |
290 | QLIST_FOREACH(block, &ram_list.blocks, next) { | |
291 | ++n; | |
292 | } | |
7267c094 | 293 | blocks = g_malloc(n * sizeof *blocks); |
b2e0a138 MT |
294 | n = 0; |
295 | QLIST_FOREACH_SAFE(block, &ram_list.blocks, next, nblock) { | |
296 | blocks[n++] = block; | |
297 | QLIST_REMOVE(block, next); | |
298 | } | |
299 | qsort(blocks, n, sizeof *blocks, block_compar); | |
300 | while (--n >= 0) { | |
301 | QLIST_INSERT_HEAD(&ram_list.blocks, blocks[n], next); | |
302 | } | |
7267c094 | 303 | g_free(blocks); |
b2e0a138 MT |
304 | } |
305 | ||
8e21cd32 OW |
306 | static void migration_end(void) |
307 | { | |
308 | memory_global_dirty_log_stop(); | |
309 | } | |
310 | ||
9b5bfab0 JQ |
311 | static void ram_migration_cancel(void *opaque) |
312 | { | |
313 | migration_end(); | |
314 | } | |
315 | ||
4508bd9e JQ |
316 | #define MAX_WAIT 50 /* ms, half buffered_file limit */ |
317 | ||
d1315aac | 318 | static int ram_save_setup(QEMUFile *f, void *opaque) |
ad96090a BS |
319 | { |
320 | ram_addr_t addr; | |
d1315aac | 321 | RAMBlock *block; |
ad96090a | 322 | |
d1315aac JQ |
323 | bytes_transferred = 0; |
324 | last_block = NULL; | |
325 | last_offset = 0; | |
326 | sort_ram_list(); | |
ad96090a | 327 | |
d1315aac JQ |
328 | /* Make sure all dirty bits are set */ |
329 | QLIST_FOREACH(block, &ram_list.blocks, next) { | |
330 | for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) { | |
331 | if (!memory_region_get_dirty(block->mr, addr, TARGET_PAGE_SIZE, | |
332 | DIRTY_MEMORY_MIGRATION)) { | |
333 | memory_region_set_dirty(block->mr, addr, TARGET_PAGE_SIZE); | |
ad96090a BS |
334 | } |
335 | } | |
d1315aac | 336 | } |
ad96090a | 337 | |
d1315aac | 338 | memory_global_dirty_log_start(); |
ad96090a | 339 | |
d1315aac | 340 | qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE); |
97ab12d4 | 341 | |
d1315aac JQ |
342 | QLIST_FOREACH(block, &ram_list.blocks, next) { |
343 | qemu_put_byte(f, strlen(block->idstr)); | |
344 | qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr)); | |
345 | qemu_put_be64(f, block->length); | |
ad96090a BS |
346 | } |
347 | ||
d1315aac JQ |
348 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
349 | ||
350 | return 0; | |
351 | } | |
352 | ||
16310a3c | 353 | static int ram_save_iterate(QEMUFile *f, void *opaque) |
d1315aac JQ |
354 | { |
355 | uint64_t bytes_transferred_last; | |
356 | double bwidth = 0; | |
357 | int ret; | |
358 | int i; | |
16310a3c | 359 | uint64_t expected_time; |
d1315aac | 360 | |
ad96090a BS |
361 | bytes_transferred_last = bytes_transferred; |
362 | bwidth = qemu_get_clock_ns(rt_clock); | |
363 | ||
4508bd9e | 364 | i = 0; |
2975725f | 365 | while ((ret = qemu_file_rate_limit(f)) == 0) { |
3fc250b4 | 366 | int bytes_sent; |
ad96090a | 367 | |
3fc250b4 | 368 | bytes_sent = ram_save_block(f); |
6c779f22 OW |
369 | /* no more blocks to sent */ |
370 | if (bytes_sent < 0) { | |
ad96090a BS |
371 | break; |
372 | } | |
6c779f22 | 373 | bytes_transferred += bytes_sent; |
4508bd9e JQ |
374 | /* we want to check in the 1st loop, just in case it was the 1st time |
375 | and we had to sync the dirty bitmap. | |
376 | qemu_get_clock_ns() is a bit expensive, so we only check each some | |
377 | iterations | |
378 | */ | |
379 | if ((i & 63) == 0) { | |
380 | uint64_t t1 = (qemu_get_clock_ns(rt_clock) - bwidth) / 1000000; | |
381 | if (t1 > MAX_WAIT) { | |
382 | DPRINTF("big wait: " PRIu64 " milliseconds, %d iterations\n", | |
383 | t1, i); | |
384 | break; | |
385 | } | |
386 | } | |
387 | i++; | |
ad96090a BS |
388 | } |
389 | ||
2975725f JQ |
390 | if (ret < 0) { |
391 | return ret; | |
392 | } | |
393 | ||
ad96090a BS |
394 | bwidth = qemu_get_clock_ns(rt_clock) - bwidth; |
395 | bwidth = (bytes_transferred - bytes_transferred_last) / bwidth; | |
396 | ||
397 | /* if we haven't transferred anything this round, force expected_time to a | |
398 | * a very high value, but without crashing */ | |
399 | if (bwidth == 0) { | |
400 | bwidth = 0.000001; | |
401 | } | |
402 | ||
16310a3c JQ |
403 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
404 | ||
405 | expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; | |
406 | ||
407 | DPRINTF("ram_save_live: expected(" PRIu64 ") <= max(" PRIu64 ")?\n", | |
408 | expected_time, migrate_max_downtime()); | |
409 | ||
00d94f3f JQ |
410 | if (expected_time <= migrate_max_downtime()) { |
411 | memory_global_sync_dirty_bitmap(get_system_memory()); | |
412 | expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth; | |
413 | ||
414 | return expected_time <= migrate_max_downtime(); | |
415 | } | |
416 | return 0; | |
16310a3c JQ |
417 | } |
418 | ||
419 | static int ram_save_complete(QEMUFile *f, void *opaque) | |
420 | { | |
16310a3c JQ |
421 | memory_global_sync_dirty_bitmap(get_system_memory()); |
422 | ||
ad96090a | 423 | /* try transferring iterative blocks of memory */ |
3a697f69 | 424 | |
16310a3c | 425 | /* flush all remaining blocks regardless of rate limiting */ |
6c779f22 | 426 | while (true) { |
3fc250b4 PR |
427 | int bytes_sent; |
428 | ||
6c779f22 OW |
429 | bytes_sent = ram_save_block(f); |
430 | /* no more blocks to sent */ | |
431 | if (bytes_sent < 0) { | |
432 | break; | |
ad96090a | 433 | } |
16310a3c | 434 | bytes_transferred += bytes_sent; |
ad96090a | 435 | } |
16310a3c | 436 | memory_global_dirty_log_stop(); |
ad96090a BS |
437 | |
438 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); | |
439 | ||
5b3c9638 | 440 | return 0; |
ad96090a BS |
441 | } |
442 | ||
a55bbe31 AW |
443 | static inline void *host_from_stream_offset(QEMUFile *f, |
444 | ram_addr_t offset, | |
445 | int flags) | |
446 | { | |
447 | static RAMBlock *block = NULL; | |
448 | char id[256]; | |
449 | uint8_t len; | |
450 | ||
451 | if (flags & RAM_SAVE_FLAG_CONTINUE) { | |
452 | if (!block) { | |
453 | fprintf(stderr, "Ack, bad migration stream!\n"); | |
454 | return NULL; | |
455 | } | |
456 | ||
dc94a7ed | 457 | return memory_region_get_ram_ptr(block->mr) + offset; |
a55bbe31 AW |
458 | } |
459 | ||
460 | len = qemu_get_byte(f); | |
461 | qemu_get_buffer(f, (uint8_t *)id, len); | |
462 | id[len] = 0; | |
463 | ||
464 | QLIST_FOREACH(block, &ram_list.blocks, next) { | |
465 | if (!strncmp(id, block->idstr, sizeof(id))) | |
dc94a7ed | 466 | return memory_region_get_ram_ptr(block->mr) + offset; |
a55bbe31 AW |
467 | } |
468 | ||
469 | fprintf(stderr, "Can't find block %s!\n", id); | |
470 | return NULL; | |
471 | } | |
472 | ||
7908c78d | 473 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
ad96090a BS |
474 | { |
475 | ram_addr_t addr; | |
3a697f69 | 476 | int flags, ret = 0; |
42802d47 | 477 | int error; |
3a697f69 OW |
478 | static uint64_t seq_iter; |
479 | ||
480 | seq_iter++; | |
ad96090a | 481 | |
f09f2189 | 482 | if (version_id < 4 || version_id > 4) { |
ad96090a BS |
483 | return -EINVAL; |
484 | } | |
485 | ||
486 | do { | |
487 | addr = qemu_get_be64(f); | |
488 | ||
489 | flags = addr & ~TARGET_PAGE_MASK; | |
490 | addr &= TARGET_PAGE_MASK; | |
491 | ||
492 | if (flags & RAM_SAVE_FLAG_MEM_SIZE) { | |
f09f2189 | 493 | if (version_id == 4) { |
97ab12d4 AW |
494 | /* Synchronize RAM block list */ |
495 | char id[256]; | |
496 | ram_addr_t length; | |
497 | ram_addr_t total_ram_bytes = addr; | |
498 | ||
499 | while (total_ram_bytes) { | |
500 | RAMBlock *block; | |
501 | uint8_t len; | |
502 | ||
503 | len = qemu_get_byte(f); | |
504 | qemu_get_buffer(f, (uint8_t *)id, len); | |
505 | id[len] = 0; | |
506 | length = qemu_get_be64(f); | |
507 | ||
508 | QLIST_FOREACH(block, &ram_list.blocks, next) { | |
509 | if (!strncmp(id, block->idstr, sizeof(id))) { | |
3a697f69 OW |
510 | if (block->length != length) { |
511 | ret = -EINVAL; | |
512 | goto done; | |
513 | } | |
97ab12d4 AW |
514 | break; |
515 | } | |
516 | } | |
517 | ||
518 | if (!block) { | |
fb787f81 AW |
519 | fprintf(stderr, "Unknown ramblock \"%s\", cannot " |
520 | "accept migration\n", id); | |
3a697f69 OW |
521 | ret = -EINVAL; |
522 | goto done; | |
97ab12d4 AW |
523 | } |
524 | ||
525 | total_ram_bytes -= length; | |
526 | } | |
ad96090a BS |
527 | } |
528 | } | |
529 | ||
530 | if (flags & RAM_SAVE_FLAG_COMPRESS) { | |
97ab12d4 AW |
531 | void *host; |
532 | uint8_t ch; | |
533 | ||
f09f2189 | 534 | host = host_from_stream_offset(f, addr, flags); |
492fb99c MT |
535 | if (!host) { |
536 | return -EINVAL; | |
537 | } | |
97ab12d4 | 538 | |
97ab12d4 AW |
539 | ch = qemu_get_byte(f); |
540 | memset(host, ch, TARGET_PAGE_SIZE); | |
ad96090a BS |
541 | #ifndef _WIN32 |
542 | if (ch == 0 && | |
543 | (!kvm_enabled() || kvm_has_sync_mmu())) { | |
e78815a5 | 544 | qemu_madvise(host, TARGET_PAGE_SIZE, QEMU_MADV_DONTNEED); |
ad96090a BS |
545 | } |
546 | #endif | |
547 | } else if (flags & RAM_SAVE_FLAG_PAGE) { | |
97ab12d4 AW |
548 | void *host; |
549 | ||
f09f2189 | 550 | host = host_from_stream_offset(f, addr, flags); |
0ff1f9f5 OW |
551 | if (!host) { |
552 | return -EINVAL; | |
553 | } | |
97ab12d4 | 554 | |
97ab12d4 | 555 | qemu_get_buffer(f, host, TARGET_PAGE_SIZE); |
ad96090a | 556 | } |
42802d47 JQ |
557 | error = qemu_file_get_error(f); |
558 | if (error) { | |
3a697f69 OW |
559 | ret = error; |
560 | goto done; | |
ad96090a BS |
561 | } |
562 | } while (!(flags & RAM_SAVE_FLAG_EOS)); | |
563 | ||
3a697f69 OW |
564 | done: |
565 | DPRINTF("Completed load of VM with exit code %d seq iteration " PRIu64 "\n", | |
566 | ret, seq_iter); | |
567 | return ret; | |
ad96090a BS |
568 | } |
569 | ||
7908c78d | 570 | SaveVMHandlers savevm_ram_handlers = { |
d1315aac | 571 | .save_live_setup = ram_save_setup, |
16310a3c JQ |
572 | .save_live_iterate = ram_save_iterate, |
573 | .save_live_complete = ram_save_complete, | |
7908c78d | 574 | .load_state = ram_load, |
9b5bfab0 | 575 | .cancel = ram_migration_cancel, |
7908c78d JQ |
576 | }; |
577 | ||
ad96090a | 578 | #ifdef HAS_AUDIO |
0dfa5ef9 IY |
579 | struct soundhw { |
580 | const char *name; | |
581 | const char *descr; | |
582 | int enabled; | |
583 | int isa; | |
584 | union { | |
4a0f031d | 585 | int (*init_isa) (ISABus *bus); |
0dfa5ef9 IY |
586 | int (*init_pci) (PCIBus *bus); |
587 | } init; | |
588 | }; | |
589 | ||
590 | static struct soundhw soundhw[] = { | |
ad96090a | 591 | #ifdef HAS_AUDIO_CHOICE |
da12872a | 592 | #ifdef CONFIG_PCSPK |
ad96090a BS |
593 | { |
594 | "pcspk", | |
595 | "PC speaker", | |
596 | 0, | |
597 | 1, | |
598 | { .init_isa = pcspk_audio_init } | |
599 | }, | |
600 | #endif | |
601 | ||
602 | #ifdef CONFIG_SB16 | |
603 | { | |
604 | "sb16", | |
605 | "Creative Sound Blaster 16", | |
606 | 0, | |
607 | 1, | |
608 | { .init_isa = SB16_init } | |
609 | }, | |
610 | #endif | |
611 | ||
612 | #ifdef CONFIG_CS4231A | |
613 | { | |
614 | "cs4231a", | |
615 | "CS4231A", | |
616 | 0, | |
617 | 1, | |
618 | { .init_isa = cs4231a_init } | |
619 | }, | |
620 | #endif | |
621 | ||
622 | #ifdef CONFIG_ADLIB | |
623 | { | |
624 | "adlib", | |
625 | #ifdef HAS_YMF262 | |
626 | "Yamaha YMF262 (OPL3)", | |
627 | #else | |
628 | "Yamaha YM3812 (OPL2)", | |
629 | #endif | |
630 | 0, | |
631 | 1, | |
632 | { .init_isa = Adlib_init } | |
633 | }, | |
634 | #endif | |
635 | ||
636 | #ifdef CONFIG_GUS | |
637 | { | |
638 | "gus", | |
639 | "Gravis Ultrasound GF1", | |
640 | 0, | |
641 | 1, | |
642 | { .init_isa = GUS_init } | |
643 | }, | |
644 | #endif | |
645 | ||
646 | #ifdef CONFIG_AC97 | |
647 | { | |
648 | "ac97", | |
649 | "Intel 82801AA AC97 Audio", | |
650 | 0, | |
651 | 0, | |
652 | { .init_pci = ac97_init } | |
653 | }, | |
654 | #endif | |
655 | ||
656 | #ifdef CONFIG_ES1370 | |
657 | { | |
658 | "es1370", | |
659 | "ENSONIQ AudioPCI ES1370", | |
660 | 0, | |
661 | 0, | |
662 | { .init_pci = es1370_init } | |
663 | }, | |
664 | #endif | |
665 | ||
d61a4ce8 GH |
666 | #ifdef CONFIG_HDA |
667 | { | |
668 | "hda", | |
669 | "Intel HD Audio", | |
670 | 0, | |
671 | 0, | |
672 | { .init_pci = intel_hda_and_codec_init } | |
673 | }, | |
674 | #endif | |
675 | ||
ad96090a BS |
676 | #endif /* HAS_AUDIO_CHOICE */ |
677 | ||
678 | { NULL, NULL, 0, 0, { NULL } } | |
679 | }; | |
680 | ||
681 | void select_soundhw(const char *optarg) | |
682 | { | |
683 | struct soundhw *c; | |
684 | ||
c8057f95 | 685 | if (is_help_option(optarg)) { |
ad96090a BS |
686 | show_valid_cards: |
687 | ||
688 | printf("Valid sound card names (comma separated):\n"); | |
689 | for (c = soundhw; c->name; ++c) { | |
690 | printf ("%-11s %s\n", c->name, c->descr); | |
691 | } | |
692 | printf("\n-soundhw all will enable all of the above\n"); | |
c8057f95 | 693 | exit(!is_help_option(optarg)); |
ad96090a BS |
694 | } |
695 | else { | |
696 | size_t l; | |
697 | const char *p; | |
698 | char *e; | |
699 | int bad_card = 0; | |
700 | ||
701 | if (!strcmp(optarg, "all")) { | |
702 | for (c = soundhw; c->name; ++c) { | |
703 | c->enabled = 1; | |
704 | } | |
705 | return; | |
706 | } | |
707 | ||
708 | p = optarg; | |
709 | while (*p) { | |
710 | e = strchr(p, ','); | |
711 | l = !e ? strlen(p) : (size_t) (e - p); | |
712 | ||
713 | for (c = soundhw; c->name; ++c) { | |
714 | if (!strncmp(c->name, p, l) && !c->name[l]) { | |
715 | c->enabled = 1; | |
716 | break; | |
717 | } | |
718 | } | |
719 | ||
720 | if (!c->name) { | |
721 | if (l > 80) { | |
722 | fprintf(stderr, | |
723 | "Unknown sound card name (too big to show)\n"); | |
724 | } | |
725 | else { | |
726 | fprintf(stderr, "Unknown sound card name `%.*s'\n", | |
727 | (int) l, p); | |
728 | } | |
729 | bad_card = 1; | |
730 | } | |
731 | p += l + (e != NULL); | |
732 | } | |
733 | ||
734 | if (bad_card) { | |
735 | goto show_valid_cards; | |
736 | } | |
737 | } | |
738 | } | |
0dfa5ef9 | 739 | |
4a0f031d | 740 | void audio_init(ISABus *isa_bus, PCIBus *pci_bus) |
0dfa5ef9 IY |
741 | { |
742 | struct soundhw *c; | |
743 | ||
744 | for (c = soundhw; c->name; ++c) { | |
745 | if (c->enabled) { | |
746 | if (c->isa) { | |
4a0f031d HP |
747 | if (isa_bus) { |
748 | c->init.init_isa(isa_bus); | |
0dfa5ef9 IY |
749 | } |
750 | } else { | |
751 | if (pci_bus) { | |
752 | c->init.init_pci(pci_bus); | |
753 | } | |
754 | } | |
755 | } | |
756 | } | |
757 | } | |
ad96090a BS |
758 | #else |
759 | void select_soundhw(const char *optarg) | |
760 | { | |
761 | } | |
4a0f031d | 762 | void audio_init(ISABus *isa_bus, PCIBus *pci_bus) |
0dfa5ef9 IY |
763 | { |
764 | } | |
ad96090a BS |
765 | #endif |
766 | ||
767 | int qemu_uuid_parse(const char *str, uint8_t *uuid) | |
768 | { | |
769 | int ret; | |
770 | ||
771 | if (strlen(str) != 36) { | |
772 | return -1; | |
773 | } | |
774 | ||
775 | ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3], | |
776 | &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9], | |
777 | &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], | |
778 | &uuid[15]); | |
779 | ||
780 | if (ret != 16) { | |
781 | return -1; | |
782 | } | |
783 | #ifdef TARGET_I386 | |
784 | smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid); | |
785 | #endif | |
786 | return 0; | |
787 | } | |
788 | ||
789 | void do_acpitable_option(const char *optarg) | |
790 | { | |
791 | #ifdef TARGET_I386 | |
792 | if (acpi_table_add(optarg) < 0) { | |
793 | fprintf(stderr, "Wrong acpi table provided\n"); | |
794 | exit(1); | |
795 | } | |
796 | #endif | |
797 | } | |
798 | ||
799 | void do_smbios_option(const char *optarg) | |
800 | { | |
801 | #ifdef TARGET_I386 | |
802 | if (smbios_entry_add(optarg) < 0) { | |
803 | fprintf(stderr, "Wrong smbios provided\n"); | |
804 | exit(1); | |
805 | } | |
806 | #endif | |
807 | } | |
808 | ||
809 | void cpudef_init(void) | |
810 | { | |
811 | #if defined(cpudef_setup) | |
812 | cpudef_setup(); /* parse cpu definitions in target config file */ | |
813 | #endif | |
814 | } | |
815 | ||
816 | int audio_available(void) | |
817 | { | |
818 | #ifdef HAS_AUDIO | |
819 | return 1; | |
820 | #else | |
821 | return 0; | |
822 | #endif | |
823 | } | |
824 | ||
303d4e86 AP |
825 | int tcg_available(void) |
826 | { | |
827 | return 1; | |
828 | } | |
829 | ||
ad96090a BS |
830 | int kvm_available(void) |
831 | { | |
832 | #ifdef CONFIG_KVM | |
833 | return 1; | |
834 | #else | |
835 | return 0; | |
836 | #endif | |
837 | } | |
838 | ||
839 | int xen_available(void) | |
840 | { | |
841 | #ifdef CONFIG_XEN | |
842 | return 1; | |
843 | #else | |
844 | return 0; | |
845 | #endif | |
846 | } |