blob: f10692873e4e0dda8a38ff799b44fff6d6ecd5c4 [file] [log] [blame]
Blue Swirlad960902010-03-29 19:23:52 +00001/*
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>
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +020026#include <stdlib.h>
Blue Swirlad960902010-03-29 19:23:52 +000027#ifndef _WIN32
Blue Swirl1c47cb12010-03-30 19:27:34 +000028#include <sys/types.h>
Blue Swirlad960902010-03-29 19:23:52 +000029#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"
Avi Kivity86e775c2011-12-15 16:24:49 +020044#include "exec-memory.h"
Jan Kiszka302fe512012-02-17 11:24:34 +010045#include "hw/pcspk.h"
Blue Swirlad960902010-03-29 19:23:52 +000046
Orit Wasserman3a697f62012-06-19 18:43:15 +030047#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
Blue Swirlad960902010-03-29 19:23:52 +000055#ifdef TARGET_SPARC
56int graphic_width = 1024;
57int graphic_height = 768;
58int graphic_depth = 8;
59#else
60int graphic_width = 800;
61int graphic_height = 600;
62int graphic_depth = 15;
63#endif
64
Blue Swirlad960902010-03-29 19:23:52 +000065
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
Michael Walle81ea0e12011-02-17 23:45:02 +010076#elif defined(TARGET_LM32)
77#define QEMU_ARCH QEMU_ARCH_LM32
Blue Swirlad960902010-03-29 19:23:52 +000078#elif defined(TARGET_MICROBLAZE)
79#define QEMU_ARCH QEMU_ARCH_MICROBLAZE
80#elif defined(TARGET_MIPS)
81#define QEMU_ARCH QEMU_ARCH_MIPS
82#elif defined(TARGET_PPC)
83#define QEMU_ARCH QEMU_ARCH_PPC
84#elif defined(TARGET_S390X)
85#define QEMU_ARCH QEMU_ARCH_S390X
86#elif defined(TARGET_SH4)
87#define QEMU_ARCH QEMU_ARCH_SH4
88#elif defined(TARGET_SPARC)
89#define QEMU_ARCH QEMU_ARCH_SPARC
Max Filippov23288262011-09-06 03:55:25 +040090#elif defined(TARGET_XTENSA)
91#define QEMU_ARCH QEMU_ARCH_XTENSA
Blue Swirlad960902010-03-29 19:23:52 +000092#endif
93
94const uint32_t arch_type = QEMU_ARCH;
95
96/***********************************************************/
97/* ram save/restore */
98
Yoshiaki Tamurad20878d2010-08-18 13:30:12 +090099#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
100#define RAM_SAVE_FLAG_COMPRESS 0x02
101#define RAM_SAVE_FLAG_MEM_SIZE 0x04
102#define RAM_SAVE_FLAG_PAGE 0x08
103#define RAM_SAVE_FLAG_EOS 0x10
104#define RAM_SAVE_FLAG_CONTINUE 0x20
Blue Swirlad960902010-03-29 19:23:52 +0000105
Paolo Bonzini86003612011-12-23 16:17:26 +0100106#ifdef __ALTIVEC__
107#include <altivec.h>
108#define VECTYPE vector unsigned char
109#define SPLAT(p) vec_splat(vec_ld(0, p), 0)
110#define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
Andreas Färberf283edc2012-05-27 16:21:02 +0200111/* altivec.h may redefine the bool macro as vector type.
112 * Reset it to POSIX semantics. */
113#undef bool
114#define bool _Bool
Paolo Bonzini86003612011-12-23 16:17:26 +0100115#elif defined __SSE2__
116#include <emmintrin.h>
117#define VECTYPE __m128i
118#define SPLAT(p) _mm_set1_epi8(*(p))
119#define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
120#else
121#define VECTYPE unsigned long
122#define SPLAT(p) (*(p) * (~0UL / 255))
123#define ALL_EQ(v1, v2) ((v1) == (v2))
124#endif
125
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300126
Eduardo Habkost756557d2012-05-02 13:07:27 -0300127static struct defconfig_file {
128 const char *filename;
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300129 /* Indicates it is an user config file (disabled by -no-user-config) */
130 bool userconfig;
Eduardo Habkost756557d2012-05-02 13:07:27 -0300131} default_config_files[] = {
Eduardo Habkoste2d87bf2012-05-02 13:07:30 -0300132 { CONFIG_QEMU_DATADIR "/cpus-" TARGET_ARCH ".conf", false },
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300133 { CONFIG_QEMU_CONFDIR "/qemu.conf", true },
134 { CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf", true },
Eduardo Habkost756557d2012-05-02 13:07:27 -0300135 { NULL }, /* end of list */
136};
137
138
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300139int qemu_read_default_config_files(bool userconfig)
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300140{
141 int ret;
Eduardo Habkost756557d2012-05-02 13:07:27 -0300142 struct defconfig_file *f;
143
144 for (f = default_config_files; f->filename; f++) {
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300145 if (!userconfig && f->userconfig) {
146 continue;
147 }
Eduardo Habkost756557d2012-05-02 13:07:27 -0300148 ret = qemu_read_config_file(f->filename);
149 if (ret < 0 && ret != -ENOENT) {
150 return ret;
151 }
152 }
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300153
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300154 return 0;
155}
156
Paolo Bonzini86003612011-12-23 16:17:26 +0100157static int is_dup_page(uint8_t *page)
Blue Swirlad960902010-03-29 19:23:52 +0000158{
Paolo Bonzini86003612011-12-23 16:17:26 +0100159 VECTYPE *p = (VECTYPE *)page;
160 VECTYPE val = SPLAT(page);
Blue Swirlad960902010-03-29 19:23:52 +0000161 int i;
162
Paolo Bonzini86003612011-12-23 16:17:26 +0100163 for (i = 0; i < TARGET_PAGE_SIZE / sizeof(VECTYPE); i++) {
164 if (!ALL_EQ(val, p[i])) {
Blue Swirlad960902010-03-29 19:23:52 +0000165 return 0;
166 }
167 }
168
169 return 1;
170}
171
Orit Wasserman0c51f432012-06-19 18:43:14 +0300172static void save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
173 int cont, int flag)
174{
175 qemu_put_be64(f, offset | cont | flag);
176 if (!cont) {
177 qemu_put_byte(f, strlen(block->idstr));
178 qemu_put_buffer(f, (uint8_t *)block->idstr,
179 strlen(block->idstr));
180 }
181
182}
183
Alex Williamson760e77e2010-08-19 10:18:42 -0300184static RAMBlock *last_block;
185static ram_addr_t last_offset;
186
Blue Swirlad960902010-03-29 19:23:52 +0000187static int ram_save_block(QEMUFile *f)
188{
Alex Williamsone44359c2010-06-25 11:09:57 -0600189 RAMBlock *block = last_block;
190 ram_addr_t offset = last_offset;
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200191 int bytes_sent = 0;
Avi Kivity71c510e2011-12-21 13:11:22 +0200192 MemoryRegion *mr;
Blue Swirlad960902010-03-29 19:23:52 +0000193
Alex Williamsone44359c2010-06-25 11:09:57 -0600194 if (!block)
195 block = QLIST_FIRST(&ram_list.blocks);
196
Alex Williamsone44359c2010-06-25 11:09:57 -0600197 do {
Avi Kivity71c510e2011-12-21 13:11:22 +0200198 mr = block->mr;
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000199 if (memory_region_get_dirty(mr, offset, TARGET_PAGE_SIZE,
200 DIRTY_MEMORY_MIGRATION)) {
Blue Swirlad960902010-03-29 19:23:52 +0000201 uint8_t *p;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600202 int cont = (block == last_block) ? RAM_SAVE_FLAG_CONTINUE : 0;
Blue Swirlad960902010-03-29 19:23:52 +0000203
Avi Kivity71c510e2011-12-21 13:11:22 +0200204 memory_region_reset_dirty(mr, offset, TARGET_PAGE_SIZE,
205 DIRTY_MEMORY_MIGRATION);
Blue Swirlad960902010-03-29 19:23:52 +0000206
Avi Kivity71c510e2011-12-21 13:11:22 +0200207 p = memory_region_get_ram_ptr(mr) + offset;
Blue Swirlad960902010-03-29 19:23:52 +0000208
Paolo Bonzini86003612011-12-23 16:17:26 +0100209 if (is_dup_page(p)) {
Orit Wasserman0c51f432012-06-19 18:43:14 +0300210 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_COMPRESS);
Blue Swirlad960902010-03-29 19:23:52 +0000211 qemu_put_byte(f, *p);
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200212 bytes_sent = 1;
Blue Swirlad960902010-03-29 19:23:52 +0000213 } else {
Orit Wasserman0c51f432012-06-19 18:43:14 +0300214 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
Blue Swirlad960902010-03-29 19:23:52 +0000215 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200216 bytes_sent = TARGET_PAGE_SIZE;
Blue Swirlad960902010-03-29 19:23:52 +0000217 }
218
Blue Swirlad960902010-03-29 19:23:52 +0000219 break;
220 }
Alex Williamsone44359c2010-06-25 11:09:57 -0600221
222 offset += TARGET_PAGE_SIZE;
223 if (offset >= block->length) {
224 offset = 0;
225 block = QLIST_NEXT(block, next);
226 if (!block)
227 block = QLIST_FIRST(&ram_list.blocks);
228 }
Avi Kivity71c510e2011-12-21 13:11:22 +0200229 } while (block != last_block || offset != last_offset);
Alex Williamsone44359c2010-06-25 11:09:57 -0600230
231 last_block = block;
232 last_offset = offset;
Blue Swirlad960902010-03-29 19:23:52 +0000233
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200234 return bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000235}
236
237static uint64_t bytes_transferred;
238
239static ram_addr_t ram_save_remaining(void)
240{
Alex Williamsone44359c2010-06-25 11:09:57 -0600241 RAMBlock *block;
Blue Swirlad960902010-03-29 19:23:52 +0000242 ram_addr_t count = 0;
243
Alex Williamsone44359c2010-06-25 11:09:57 -0600244 QLIST_FOREACH(block, &ram_list.blocks, next) {
245 ram_addr_t addr;
Avi Kivity71c510e2011-12-21 13:11:22 +0200246 for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) {
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000247 if (memory_region_get_dirty(block->mr, addr, TARGET_PAGE_SIZE,
Avi Kivity71c510e2011-12-21 13:11:22 +0200248 DIRTY_MEMORY_MIGRATION)) {
Alex Williamsone44359c2010-06-25 11:09:57 -0600249 count++;
250 }
Blue Swirlad960902010-03-29 19:23:52 +0000251 }
252 }
253
254 return count;
255}
256
257uint64_t ram_bytes_remaining(void)
258{
259 return ram_save_remaining() * TARGET_PAGE_SIZE;
260}
261
262uint64_t ram_bytes_transferred(void)
263{
264 return bytes_transferred;
265}
266
267uint64_t ram_bytes_total(void)
268{
Alex Williamsond17b5282010-06-25 11:08:38 -0600269 RAMBlock *block;
270 uint64_t total = 0;
271
272 QLIST_FOREACH(block, &ram_list.blocks, next)
273 total += block->length;
274
275 return total;
Blue Swirlad960902010-03-29 19:23:52 +0000276}
277
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200278static int block_compar(const void *a, const void *b)
279{
280 RAMBlock * const *ablock = a;
281 RAMBlock * const *bblock = b;
Avi Kivity8fec98b2011-12-21 13:22:16 +0200282
283 return strcmp((*ablock)->idstr, (*bblock)->idstr);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200284}
285
286static void sort_ram_list(void)
287{
288 RAMBlock *block, *nblock, **blocks;
289 int n;
290 n = 0;
291 QLIST_FOREACH(block, &ram_list.blocks, next) {
292 ++n;
293 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500294 blocks = g_malloc(n * sizeof *blocks);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200295 n = 0;
296 QLIST_FOREACH_SAFE(block, &ram_list.blocks, next, nblock) {
297 blocks[n++] = block;
298 QLIST_REMOVE(block, next);
299 }
300 qsort(blocks, n, sizeof *blocks, block_compar);
301 while (--n >= 0) {
302 QLIST_INSERT_HEAD(&ram_list.blocks, blocks[n], next);
303 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500304 g_free(blocks);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200305}
306
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300307static void migration_end(void)
308{
309 memory_global_dirty_log_stop();
310}
311
Luiz Capitulino539de122011-12-05 14:06:56 -0200312int ram_save_live(QEMUFile *f, int stage, void *opaque)
Blue Swirlad960902010-03-29 19:23:52 +0000313{
314 ram_addr_t addr;
315 uint64_t bytes_transferred_last;
316 double bwidth = 0;
317 uint64_t expected_time = 0;
Juan Quintela29757252011-10-19 15:22:18 +0200318 int ret;
Blue Swirlad960902010-03-29 19:23:52 +0000319
320 if (stage < 0) {
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300321 migration_end();
Blue Swirlad960902010-03-29 19:23:52 +0000322 return 0;
323 }
324
Avi Kivity86e775c2011-12-15 16:24:49 +0200325 memory_global_sync_dirty_bitmap(get_system_memory());
Blue Swirlad960902010-03-29 19:23:52 +0000326
327 if (stage == 1) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600328 RAMBlock *block;
Blue Swirlad960902010-03-29 19:23:52 +0000329 bytes_transferred = 0;
Alex Williamson760e77e2010-08-19 10:18:42 -0300330 last_block = NULL;
331 last_offset = 0;
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200332 sort_ram_list();
Blue Swirlad960902010-03-29 19:23:52 +0000333
334 /* Make sure all dirty bits are set */
Alex Williamsone44359c2010-06-25 11:09:57 -0600335 QLIST_FOREACH(block, &ram_list.blocks, next) {
Avi Kivity71c510e2011-12-21 13:11:22 +0200336 for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) {
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000337 if (!memory_region_get_dirty(block->mr, addr, TARGET_PAGE_SIZE,
Avi Kivity71c510e2011-12-21 13:11:22 +0200338 DIRTY_MEMORY_MIGRATION)) {
Blue Swirlfd4aa972011-10-16 16:04:59 +0000339 memory_region_set_dirty(block->mr, addr, TARGET_PAGE_SIZE);
Alex Williamsone44359c2010-06-25 11:09:57 -0600340 }
Blue Swirlad960902010-03-29 19:23:52 +0000341 }
342 }
343
Avi Kivity8f775582011-12-21 14:14:07 +0200344 memory_global_dirty_log_start();
Blue Swirlad960902010-03-29 19:23:52 +0000345
Alex Williamsone44359c2010-06-25 11:09:57 -0600346 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
Alex Williamson97ab12d2010-06-25 11:09:50 -0600347
348 QLIST_FOREACH(block, &ram_list.blocks, next) {
349 qemu_put_byte(f, strlen(block->idstr));
350 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
351 qemu_put_be64(f, block->length);
352 }
Blue Swirlad960902010-03-29 19:23:52 +0000353 }
354
355 bytes_transferred_last = bytes_transferred;
356 bwidth = qemu_get_clock_ns(rt_clock);
357
Juan Quintela29757252011-10-19 15:22:18 +0200358 while ((ret = qemu_file_rate_limit(f)) == 0) {
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200359 int bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000360
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200361 bytes_sent = ram_save_block(f);
362 bytes_transferred += bytes_sent;
363 if (bytes_sent == 0) { /* no more blocks */
Blue Swirlad960902010-03-29 19:23:52 +0000364 break;
365 }
366 }
367
Juan Quintela29757252011-10-19 15:22:18 +0200368 if (ret < 0) {
369 return ret;
370 }
371
Blue Swirlad960902010-03-29 19:23:52 +0000372 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
373 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
374
375 /* if we haven't transferred anything this round, force expected_time to a
376 * a very high value, but without crashing */
377 if (bwidth == 0) {
378 bwidth = 0.000001;
379 }
380
381 /* try transferring iterative blocks of memory */
382 if (stage == 3) {
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200383 int bytes_sent;
384
Blue Swirlad960902010-03-29 19:23:52 +0000385 /* flush all remaining blocks regardless of rate limiting */
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200386 while ((bytes_sent = ram_save_block(f)) != 0) {
387 bytes_transferred += bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000388 }
Avi Kivity8f775582011-12-21 14:14:07 +0200389 memory_global_dirty_log_stop();
Blue Swirlad960902010-03-29 19:23:52 +0000390 }
391
392 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
393
394 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
395
Orit Wasserman3a697f62012-06-19 18:43:15 +0300396 DPRINTF("ram_save_live: expected(" PRIu64 ") <= max(" PRIu64 ")?\n",
397 expected_time, migrate_max_downtime());
398
Blue Swirlad960902010-03-29 19:23:52 +0000399 return (stage == 2) && (expected_time <= migrate_max_downtime());
400}
401
Alex Williamsona55bbe32010-06-25 11:10:05 -0600402static inline void *host_from_stream_offset(QEMUFile *f,
403 ram_addr_t offset,
404 int flags)
405{
406 static RAMBlock *block = NULL;
407 char id[256];
408 uint8_t len;
409
410 if (flags & RAM_SAVE_FLAG_CONTINUE) {
411 if (!block) {
412 fprintf(stderr, "Ack, bad migration stream!\n");
413 return NULL;
414 }
415
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200416 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600417 }
418
419 len = qemu_get_byte(f);
420 qemu_get_buffer(f, (uint8_t *)id, len);
421 id[len] = 0;
422
423 QLIST_FOREACH(block, &ram_list.blocks, next) {
424 if (!strncmp(id, block->idstr, sizeof(id)))
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200425 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600426 }
427
428 fprintf(stderr, "Can't find block %s!\n", id);
429 return NULL;
430}
431
Blue Swirlad960902010-03-29 19:23:52 +0000432int ram_load(QEMUFile *f, void *opaque, int version_id)
433{
434 ram_addr_t addr;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300435 int flags, ret = 0;
Juan Quintela42802d42011-10-05 01:14:46 +0200436 int error;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300437 static uint64_t seq_iter;
438
439 seq_iter++;
Blue Swirlad960902010-03-29 19:23:52 +0000440
Avi Kivityf09f2182011-12-21 13:37:56 +0200441 if (version_id < 4 || version_id > 4) {
Blue Swirlad960902010-03-29 19:23:52 +0000442 return -EINVAL;
443 }
444
445 do {
446 addr = qemu_get_be64(f);
447
448 flags = addr & ~TARGET_PAGE_MASK;
449 addr &= TARGET_PAGE_MASK;
450
451 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
Avi Kivityf09f2182011-12-21 13:37:56 +0200452 if (version_id == 4) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600453 /* Synchronize RAM block list */
454 char id[256];
455 ram_addr_t length;
456 ram_addr_t total_ram_bytes = addr;
457
458 while (total_ram_bytes) {
459 RAMBlock *block;
460 uint8_t len;
461
462 len = qemu_get_byte(f);
463 qemu_get_buffer(f, (uint8_t *)id, len);
464 id[len] = 0;
465 length = qemu_get_be64(f);
466
467 QLIST_FOREACH(block, &ram_list.blocks, next) {
468 if (!strncmp(id, block->idstr, sizeof(id))) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300469 if (block->length != length) {
470 ret = -EINVAL;
471 goto done;
472 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600473 break;
474 }
475 }
476
477 if (!block) {
Alex Williamsonfb787f82010-07-02 11:13:29 -0600478 fprintf(stderr, "Unknown ramblock \"%s\", cannot "
479 "accept migration\n", id);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300480 ret = -EINVAL;
481 goto done;
Alex Williamson97ab12d2010-06-25 11:09:50 -0600482 }
483
484 total_ram_bytes -= length;
485 }
Blue Swirlad960902010-03-29 19:23:52 +0000486 }
487 }
488
489 if (flags & RAM_SAVE_FLAG_COMPRESS) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600490 void *host;
491 uint8_t ch;
492
Avi Kivityf09f2182011-12-21 13:37:56 +0200493 host = host_from_stream_offset(f, addr, flags);
Michael S. Tsirkin492fb992010-10-17 20:43:40 +0200494 if (!host) {
495 return -EINVAL;
496 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600497
Alex Williamson97ab12d2010-06-25 11:09:50 -0600498 ch = qemu_get_byte(f);
499 memset(host, ch, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +0000500#ifndef _WIN32
501 if (ch == 0 &&
502 (!kvm_enabled() || kvm_has_sync_mmu())) {
Andreas Färbere78815a2010-09-25 11:26:05 +0000503 qemu_madvise(host, TARGET_PAGE_SIZE, QEMU_MADV_DONTNEED);
Blue Swirlad960902010-03-29 19:23:52 +0000504 }
505#endif
506 } else if (flags & RAM_SAVE_FLAG_PAGE) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600507 void *host;
508
Avi Kivityf09f2182011-12-21 13:37:56 +0200509 host = host_from_stream_offset(f, addr, flags);
Orit Wasserman0ff1f9f2012-06-19 11:51:37 +0300510 if (!host) {
511 return -EINVAL;
512 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600513
Alex Williamson97ab12d2010-06-25 11:09:50 -0600514 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +0000515 }
Juan Quintela42802d42011-10-05 01:14:46 +0200516 error = qemu_file_get_error(f);
517 if (error) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300518 ret = error;
519 goto done;
Blue Swirlad960902010-03-29 19:23:52 +0000520 }
521 } while (!(flags & RAM_SAVE_FLAG_EOS));
522
Orit Wasserman3a697f62012-06-19 18:43:15 +0300523done:
524 DPRINTF("Completed load of VM with exit code %d seq iteration " PRIu64 "\n",
525 ret, seq_iter);
526 return ret;
Blue Swirlad960902010-03-29 19:23:52 +0000527}
528
Blue Swirlad960902010-03-29 19:23:52 +0000529#ifdef HAS_AUDIO
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900530struct soundhw {
531 const char *name;
532 const char *descr;
533 int enabled;
534 int isa;
535 union {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100536 int (*init_isa) (ISABus *bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900537 int (*init_pci) (PCIBus *bus);
538 } init;
539};
540
541static struct soundhw soundhw[] = {
Blue Swirlad960902010-03-29 19:23:52 +0000542#ifdef HAS_AUDIO_CHOICE
Hervé Poussineauda128722012-04-14 22:51:33 +0200543#ifdef CONFIG_PCSPK
Blue Swirlad960902010-03-29 19:23:52 +0000544 {
545 "pcspk",
546 "PC speaker",
547 0,
548 1,
549 { .init_isa = pcspk_audio_init }
550 },
551#endif
552
553#ifdef CONFIG_SB16
554 {
555 "sb16",
556 "Creative Sound Blaster 16",
557 0,
558 1,
559 { .init_isa = SB16_init }
560 },
561#endif
562
563#ifdef CONFIG_CS4231A
564 {
565 "cs4231a",
566 "CS4231A",
567 0,
568 1,
569 { .init_isa = cs4231a_init }
570 },
571#endif
572
573#ifdef CONFIG_ADLIB
574 {
575 "adlib",
576#ifdef HAS_YMF262
577 "Yamaha YMF262 (OPL3)",
578#else
579 "Yamaha YM3812 (OPL2)",
580#endif
581 0,
582 1,
583 { .init_isa = Adlib_init }
584 },
585#endif
586
587#ifdef CONFIG_GUS
588 {
589 "gus",
590 "Gravis Ultrasound GF1",
591 0,
592 1,
593 { .init_isa = GUS_init }
594 },
595#endif
596
597#ifdef CONFIG_AC97
598 {
599 "ac97",
600 "Intel 82801AA AC97 Audio",
601 0,
602 0,
603 { .init_pci = ac97_init }
604 },
605#endif
606
607#ifdef CONFIG_ES1370
608 {
609 "es1370",
610 "ENSONIQ AudioPCI ES1370",
611 0,
612 0,
613 { .init_pci = es1370_init }
614 },
615#endif
616
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100617#ifdef CONFIG_HDA
618 {
619 "hda",
620 "Intel HD Audio",
621 0,
622 0,
623 { .init_pci = intel_hda_and_codec_init }
624 },
625#endif
626
Blue Swirlad960902010-03-29 19:23:52 +0000627#endif /* HAS_AUDIO_CHOICE */
628
629 { NULL, NULL, 0, 0, { NULL } }
630};
631
632void select_soundhw(const char *optarg)
633{
634 struct soundhw *c;
635
636 if (*optarg == '?') {
637 show_valid_cards:
638
639 printf("Valid sound card names (comma separated):\n");
640 for (c = soundhw; c->name; ++c) {
641 printf ("%-11s %s\n", c->name, c->descr);
642 }
643 printf("\n-soundhw all will enable all of the above\n");
644 exit(*optarg != '?');
645 }
646 else {
647 size_t l;
648 const char *p;
649 char *e;
650 int bad_card = 0;
651
652 if (!strcmp(optarg, "all")) {
653 for (c = soundhw; c->name; ++c) {
654 c->enabled = 1;
655 }
656 return;
657 }
658
659 p = optarg;
660 while (*p) {
661 e = strchr(p, ',');
662 l = !e ? strlen(p) : (size_t) (e - p);
663
664 for (c = soundhw; c->name; ++c) {
665 if (!strncmp(c->name, p, l) && !c->name[l]) {
666 c->enabled = 1;
667 break;
668 }
669 }
670
671 if (!c->name) {
672 if (l > 80) {
673 fprintf(stderr,
674 "Unknown sound card name (too big to show)\n");
675 }
676 else {
677 fprintf(stderr, "Unknown sound card name `%.*s'\n",
678 (int) l, p);
679 }
680 bad_card = 1;
681 }
682 p += l + (e != NULL);
683 }
684
685 if (bad_card) {
686 goto show_valid_cards;
687 }
688 }
689}
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900690
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100691void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900692{
693 struct soundhw *c;
694
695 for (c = soundhw; c->name; ++c) {
696 if (c->enabled) {
697 if (c->isa) {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100698 if (isa_bus) {
699 c->init.init_isa(isa_bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900700 }
701 } else {
702 if (pci_bus) {
703 c->init.init_pci(pci_bus);
704 }
705 }
706 }
707 }
708}
Blue Swirlad960902010-03-29 19:23:52 +0000709#else
710void select_soundhw(const char *optarg)
711{
712}
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100713void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900714{
715}
Blue Swirlad960902010-03-29 19:23:52 +0000716#endif
717
718int qemu_uuid_parse(const char *str, uint8_t *uuid)
719{
720 int ret;
721
722 if (strlen(str) != 36) {
723 return -1;
724 }
725
726 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
727 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
728 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
729 &uuid[15]);
730
731 if (ret != 16) {
732 return -1;
733 }
734#ifdef TARGET_I386
735 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
736#endif
737 return 0;
738}
739
740void do_acpitable_option(const char *optarg)
741{
742#ifdef TARGET_I386
743 if (acpi_table_add(optarg) < 0) {
744 fprintf(stderr, "Wrong acpi table provided\n");
745 exit(1);
746 }
747#endif
748}
749
750void do_smbios_option(const char *optarg)
751{
752#ifdef TARGET_I386
753 if (smbios_entry_add(optarg) < 0) {
754 fprintf(stderr, "Wrong smbios provided\n");
755 exit(1);
756 }
757#endif
758}
759
760void cpudef_init(void)
761{
762#if defined(cpudef_setup)
763 cpudef_setup(); /* parse cpu definitions in target config file */
764#endif
765}
766
767int audio_available(void)
768{
769#ifdef HAS_AUDIO
770 return 1;
771#else
772 return 0;
773#endif
774}
775
Anthony PERARD303d4e82010-09-21 20:05:31 +0100776int tcg_available(void)
777{
778 return 1;
779}
780
Blue Swirlad960902010-03-29 19:23:52 +0000781int kvm_available(void)
782{
783#ifdef CONFIG_KVM
784 return 1;
785#else
786 return 0;
787#endif
788}
789
790int xen_available(void)
791{
792#ifdef CONFIG_XEN
793 return 1;
794#else
795 return 0;
796#endif
797}