blob: ebdad3a57df072300e479e170ea84e3df4f3c137 [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
Jia Liue67db062012-07-20 15:50:39 +080082#elif defined(TARGET_OPENRISC)
83#define QEMU_ARCH QEMU_ARCH_OPENRISC
Blue Swirlad960902010-03-29 19:23:52 +000084#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
Max Filippov23288262011-09-06 03:55:25 +040092#elif defined(TARGET_XTENSA)
93#define QEMU_ARCH QEMU_ARCH_XTENSA
Blue Swirlad960902010-03-29 19:23:52 +000094#endif
95
96const uint32_t arch_type = QEMU_ARCH;
97
98/***********************************************************/
99/* ram save/restore */
100
Yoshiaki Tamurad20878d2010-08-18 13:30:12 +0900101#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
102#define RAM_SAVE_FLAG_COMPRESS 0x02
103#define RAM_SAVE_FLAG_MEM_SIZE 0x04
104#define RAM_SAVE_FLAG_PAGE 0x08
105#define RAM_SAVE_FLAG_EOS 0x10
106#define RAM_SAVE_FLAG_CONTINUE 0x20
Blue Swirlad960902010-03-29 19:23:52 +0000107
Paolo Bonzini86003612011-12-23 16:17:26 +0100108#ifdef __ALTIVEC__
109#include <altivec.h>
110#define VECTYPE vector unsigned char
111#define SPLAT(p) vec_splat(vec_ld(0, p), 0)
112#define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
Andreas Färberf283edc2012-05-27 16:21:02 +0200113/* altivec.h may redefine the bool macro as vector type.
114 * Reset it to POSIX semantics. */
115#undef bool
116#define bool _Bool
Paolo Bonzini86003612011-12-23 16:17:26 +0100117#elif defined __SSE2__
118#include <emmintrin.h>
119#define VECTYPE __m128i
120#define SPLAT(p) _mm_set1_epi8(*(p))
121#define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
122#else
123#define VECTYPE unsigned long
124#define SPLAT(p) (*(p) * (~0UL / 255))
125#define ALL_EQ(v1, v2) ((v1) == (v2))
126#endif
127
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300128
Eduardo Habkost756557d2012-05-02 13:07:27 -0300129static struct defconfig_file {
130 const char *filename;
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300131 /* Indicates it is an user config file (disabled by -no-user-config) */
132 bool userconfig;
Eduardo Habkost756557d2012-05-02 13:07:27 -0300133} default_config_files[] = {
Eduardo Habkoste2d87bf2012-05-02 13:07:30 -0300134 { CONFIG_QEMU_DATADIR "/cpus-" TARGET_ARCH ".conf", false },
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300135 { CONFIG_QEMU_CONFDIR "/qemu.conf", true },
136 { CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf", true },
Eduardo Habkost756557d2012-05-02 13:07:27 -0300137 { NULL }, /* end of list */
138};
139
140
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300141int qemu_read_default_config_files(bool userconfig)
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300142{
143 int ret;
Eduardo Habkost756557d2012-05-02 13:07:27 -0300144 struct defconfig_file *f;
145
146 for (f = default_config_files; f->filename; f++) {
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300147 if (!userconfig && f->userconfig) {
148 continue;
149 }
Eduardo Habkost756557d2012-05-02 13:07:27 -0300150 ret = qemu_read_config_file(f->filename);
151 if (ret < 0 && ret != -ENOENT) {
152 return ret;
153 }
154 }
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300155
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300156 return 0;
157}
158
Paolo Bonzini86003612011-12-23 16:17:26 +0100159static int is_dup_page(uint8_t *page)
Blue Swirlad960902010-03-29 19:23:52 +0000160{
Paolo Bonzini86003612011-12-23 16:17:26 +0100161 VECTYPE *p = (VECTYPE *)page;
162 VECTYPE val = SPLAT(page);
Blue Swirlad960902010-03-29 19:23:52 +0000163 int i;
164
Paolo Bonzini86003612011-12-23 16:17:26 +0100165 for (i = 0; i < TARGET_PAGE_SIZE / sizeof(VECTYPE); i++) {
166 if (!ALL_EQ(val, p[i])) {
Blue Swirlad960902010-03-29 19:23:52 +0000167 return 0;
168 }
169 }
170
171 return 1;
172}
173
Orit Wasserman0c51f432012-06-19 18:43:14 +0300174static void save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
175 int cont, int flag)
176{
177 qemu_put_be64(f, offset | cont | flag);
178 if (!cont) {
179 qemu_put_byte(f, strlen(block->idstr));
180 qemu_put_buffer(f, (uint8_t *)block->idstr,
181 strlen(block->idstr));
182 }
183
184}
185
Alex Williamson760e77e2010-08-19 10:18:42 -0300186static RAMBlock *last_block;
187static ram_addr_t last_offset;
188
Blue Swirlad960902010-03-29 19:23:52 +0000189static int ram_save_block(QEMUFile *f)
190{
Alex Williamsone44359c2010-06-25 11:09:57 -0600191 RAMBlock *block = last_block;
192 ram_addr_t offset = last_offset;
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200193 int bytes_sent = 0;
Avi Kivity71c510e2011-12-21 13:11:22 +0200194 MemoryRegion *mr;
Blue Swirlad960902010-03-29 19:23:52 +0000195
Alex Williamsone44359c2010-06-25 11:09:57 -0600196 if (!block)
197 block = QLIST_FIRST(&ram_list.blocks);
198
Alex Williamsone44359c2010-06-25 11:09:57 -0600199 do {
Avi Kivity71c510e2011-12-21 13:11:22 +0200200 mr = block->mr;
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000201 if (memory_region_get_dirty(mr, offset, TARGET_PAGE_SIZE,
202 DIRTY_MEMORY_MIGRATION)) {
Blue Swirlad960902010-03-29 19:23:52 +0000203 uint8_t *p;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600204 int cont = (block == last_block) ? RAM_SAVE_FLAG_CONTINUE : 0;
Blue Swirlad960902010-03-29 19:23:52 +0000205
Avi Kivity71c510e2011-12-21 13:11:22 +0200206 memory_region_reset_dirty(mr, offset, TARGET_PAGE_SIZE,
207 DIRTY_MEMORY_MIGRATION);
Blue Swirlad960902010-03-29 19:23:52 +0000208
Avi Kivity71c510e2011-12-21 13:11:22 +0200209 p = memory_region_get_ram_ptr(mr) + offset;
Blue Swirlad960902010-03-29 19:23:52 +0000210
Paolo Bonzini86003612011-12-23 16:17:26 +0100211 if (is_dup_page(p)) {
Orit Wasserman0c51f432012-06-19 18:43:14 +0300212 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_COMPRESS);
Blue Swirlad960902010-03-29 19:23:52 +0000213 qemu_put_byte(f, *p);
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200214 bytes_sent = 1;
Blue Swirlad960902010-03-29 19:23:52 +0000215 } else {
Orit Wasserman0c51f432012-06-19 18:43:14 +0300216 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
Blue Swirlad960902010-03-29 19:23:52 +0000217 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200218 bytes_sent = TARGET_PAGE_SIZE;
Blue Swirlad960902010-03-29 19:23:52 +0000219 }
220
Blue Swirlad960902010-03-29 19:23:52 +0000221 break;
222 }
Alex Williamsone44359c2010-06-25 11:09:57 -0600223
224 offset += TARGET_PAGE_SIZE;
225 if (offset >= block->length) {
226 offset = 0;
227 block = QLIST_NEXT(block, next);
228 if (!block)
229 block = QLIST_FIRST(&ram_list.blocks);
230 }
Avi Kivity71c510e2011-12-21 13:11:22 +0200231 } while (block != last_block || offset != last_offset);
Alex Williamsone44359c2010-06-25 11:09:57 -0600232
233 last_block = block;
234 last_offset = offset;
Blue Swirlad960902010-03-29 19:23:52 +0000235
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200236 return bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000237}
238
239static uint64_t bytes_transferred;
240
241static ram_addr_t ram_save_remaining(void)
242{
Juan Quintela45f33f02012-06-22 15:21:07 +0200243 return ram_list.dirty_pages;
Blue Swirlad960902010-03-29 19:23:52 +0000244}
245
246uint64_t ram_bytes_remaining(void)
247{
248 return ram_save_remaining() * TARGET_PAGE_SIZE;
249}
250
251uint64_t ram_bytes_transferred(void)
252{
253 return bytes_transferred;
254}
255
256uint64_t ram_bytes_total(void)
257{
Alex Williamsond17b5282010-06-25 11:08:38 -0600258 RAMBlock *block;
259 uint64_t total = 0;
260
261 QLIST_FOREACH(block, &ram_list.blocks, next)
262 total += block->length;
263
264 return total;
Blue Swirlad960902010-03-29 19:23:52 +0000265}
266
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200267static int block_compar(const void *a, const void *b)
268{
269 RAMBlock * const *ablock = a;
270 RAMBlock * const *bblock = b;
Avi Kivity8fec98b2011-12-21 13:22:16 +0200271
272 return strcmp((*ablock)->idstr, (*bblock)->idstr);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200273}
274
275static void sort_ram_list(void)
276{
277 RAMBlock *block, *nblock, **blocks;
278 int n;
279 n = 0;
280 QLIST_FOREACH(block, &ram_list.blocks, next) {
281 ++n;
282 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500283 blocks = g_malloc(n * sizeof *blocks);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200284 n = 0;
285 QLIST_FOREACH_SAFE(block, &ram_list.blocks, next, nblock) {
286 blocks[n++] = block;
287 QLIST_REMOVE(block, next);
288 }
289 qsort(blocks, n, sizeof *blocks, block_compar);
290 while (--n >= 0) {
291 QLIST_INSERT_HEAD(&ram_list.blocks, blocks[n], next);
292 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500293 g_free(blocks);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200294}
295
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300296static void migration_end(void)
297{
298 memory_global_dirty_log_stop();
299}
300
Juan Quintela4508bd92012-05-22 16:27:59 +0200301#define MAX_WAIT 50 /* ms, half buffered_file limit */
302
Luiz Capitulino539de122011-12-05 14:06:56 -0200303int ram_save_live(QEMUFile *f, int stage, void *opaque)
Blue Swirlad960902010-03-29 19:23:52 +0000304{
305 ram_addr_t addr;
306 uint64_t bytes_transferred_last;
307 double bwidth = 0;
Juan Quintela29757252011-10-19 15:22:18 +0200308 int ret;
Juan Quintela4508bd92012-05-22 16:27:59 +0200309 int i;
Blue Swirlad960902010-03-29 19:23:52 +0000310
311 if (stage < 0) {
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300312 migration_end();
Blue Swirlad960902010-03-29 19:23:52 +0000313 return 0;
314 }
315
Avi Kivity86e775c2011-12-15 16:24:49 +0200316 memory_global_sync_dirty_bitmap(get_system_memory());
Blue Swirlad960902010-03-29 19:23:52 +0000317
318 if (stage == 1) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600319 RAMBlock *block;
Blue Swirlad960902010-03-29 19:23:52 +0000320 bytes_transferred = 0;
Alex Williamson760e77e2010-08-19 10:18:42 -0300321 last_block = NULL;
322 last_offset = 0;
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200323 sort_ram_list();
Blue Swirlad960902010-03-29 19:23:52 +0000324
325 /* Make sure all dirty bits are set */
Alex Williamsone44359c2010-06-25 11:09:57 -0600326 QLIST_FOREACH(block, &ram_list.blocks, next) {
Avi Kivity71c510e2011-12-21 13:11:22 +0200327 for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) {
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000328 if (!memory_region_get_dirty(block->mr, addr, TARGET_PAGE_SIZE,
Avi Kivity71c510e2011-12-21 13:11:22 +0200329 DIRTY_MEMORY_MIGRATION)) {
Blue Swirlfd4aa972011-10-16 16:04:59 +0000330 memory_region_set_dirty(block->mr, addr, TARGET_PAGE_SIZE);
Alex Williamsone44359c2010-06-25 11:09:57 -0600331 }
Blue Swirlad960902010-03-29 19:23:52 +0000332 }
333 }
334
Avi Kivity8f775582011-12-21 14:14:07 +0200335 memory_global_dirty_log_start();
Blue Swirlad960902010-03-29 19:23:52 +0000336
Alex Williamsone44359c2010-06-25 11:09:57 -0600337 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
Alex Williamson97ab12d2010-06-25 11:09:50 -0600338
339 QLIST_FOREACH(block, &ram_list.blocks, next) {
340 qemu_put_byte(f, strlen(block->idstr));
341 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
342 qemu_put_be64(f, block->length);
343 }
Blue Swirlad960902010-03-29 19:23:52 +0000344 }
345
346 bytes_transferred_last = bytes_transferred;
347 bwidth = qemu_get_clock_ns(rt_clock);
348
Juan Quintela4508bd92012-05-22 16:27:59 +0200349 i = 0;
Juan Quintela29757252011-10-19 15:22:18 +0200350 while ((ret = qemu_file_rate_limit(f)) == 0) {
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200351 int bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000352
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200353 bytes_sent = ram_save_block(f);
354 bytes_transferred += bytes_sent;
355 if (bytes_sent == 0) { /* no more blocks */
Blue Swirlad960902010-03-29 19:23:52 +0000356 break;
357 }
Juan Quintela4508bd92012-05-22 16:27:59 +0200358 /* we want to check in the 1st loop, just in case it was the 1st time
359 and we had to sync the dirty bitmap.
360 qemu_get_clock_ns() is a bit expensive, so we only check each some
361 iterations
362 */
363 if ((i & 63) == 0) {
364 uint64_t t1 = (qemu_get_clock_ns(rt_clock) - bwidth) / 1000000;
365 if (t1 > MAX_WAIT) {
366 DPRINTF("big wait: " PRIu64 " milliseconds, %d iterations\n",
367 t1, i);
368 break;
369 }
370 }
371 i++;
Blue Swirlad960902010-03-29 19:23:52 +0000372 }
373
Juan Quintela29757252011-10-19 15:22:18 +0200374 if (ret < 0) {
375 return ret;
376 }
377
Blue Swirlad960902010-03-29 19:23:52 +0000378 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
379 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
380
381 /* if we haven't transferred anything this round, force expected_time to a
382 * a very high value, but without crashing */
383 if (bwidth == 0) {
384 bwidth = 0.000001;
385 }
386
387 /* try transferring iterative blocks of memory */
388 if (stage == 3) {
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200389 int bytes_sent;
390
Blue Swirlad960902010-03-29 19:23:52 +0000391 /* flush all remaining blocks regardless of rate limiting */
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200392 while ((bytes_sent = ram_save_block(f)) != 0) {
393 bytes_transferred += bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000394 }
Avi Kivity8f775582011-12-21 14:14:07 +0200395 memory_global_dirty_log_stop();
Blue Swirlad960902010-03-29 19:23:52 +0000396 }
397
398 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
399
Juan Quintela5b3c9632012-05-22 00:44:24 +0200400 if (stage == 2) {
401 uint64_t expected_time;
402 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
Blue Swirlad960902010-03-29 19:23:52 +0000403
Juan Quintela5b3c9632012-05-22 00:44:24 +0200404 DPRINTF("ram_save_live: expected(" PRIu64 ") <= max(" PRIu64 ")?\n",
405 expected_time, migrate_max_downtime());
Orit Wasserman3a697f62012-06-19 18:43:15 +0300406
Juan Quintela5b3c9632012-05-22 00:44:24 +0200407 return expected_time <= migrate_max_downtime();
408 }
409 return 0;
Blue Swirlad960902010-03-29 19:23:52 +0000410}
411
Alex Williamsona55bbe32010-06-25 11:10:05 -0600412static inline void *host_from_stream_offset(QEMUFile *f,
413 ram_addr_t offset,
414 int flags)
415{
416 static RAMBlock *block = NULL;
417 char id[256];
418 uint8_t len;
419
420 if (flags & RAM_SAVE_FLAG_CONTINUE) {
421 if (!block) {
422 fprintf(stderr, "Ack, bad migration stream!\n");
423 return NULL;
424 }
425
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200426 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600427 }
428
429 len = qemu_get_byte(f);
430 qemu_get_buffer(f, (uint8_t *)id, len);
431 id[len] = 0;
432
433 QLIST_FOREACH(block, &ram_list.blocks, next) {
434 if (!strncmp(id, block->idstr, sizeof(id)))
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200435 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600436 }
437
438 fprintf(stderr, "Can't find block %s!\n", id);
439 return NULL;
440}
441
Blue Swirlad960902010-03-29 19:23:52 +0000442int ram_load(QEMUFile *f, void *opaque, int version_id)
443{
444 ram_addr_t addr;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300445 int flags, ret = 0;
Juan Quintela42802d42011-10-05 01:14:46 +0200446 int error;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300447 static uint64_t seq_iter;
448
449 seq_iter++;
Blue Swirlad960902010-03-29 19:23:52 +0000450
Avi Kivityf09f2182011-12-21 13:37:56 +0200451 if (version_id < 4 || version_id > 4) {
Blue Swirlad960902010-03-29 19:23:52 +0000452 return -EINVAL;
453 }
454
455 do {
456 addr = qemu_get_be64(f);
457
458 flags = addr & ~TARGET_PAGE_MASK;
459 addr &= TARGET_PAGE_MASK;
460
461 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
Avi Kivityf09f2182011-12-21 13:37:56 +0200462 if (version_id == 4) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600463 /* Synchronize RAM block list */
464 char id[256];
465 ram_addr_t length;
466 ram_addr_t total_ram_bytes = addr;
467
468 while (total_ram_bytes) {
469 RAMBlock *block;
470 uint8_t len;
471
472 len = qemu_get_byte(f);
473 qemu_get_buffer(f, (uint8_t *)id, len);
474 id[len] = 0;
475 length = qemu_get_be64(f);
476
477 QLIST_FOREACH(block, &ram_list.blocks, next) {
478 if (!strncmp(id, block->idstr, sizeof(id))) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300479 if (block->length != length) {
480 ret = -EINVAL;
481 goto done;
482 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600483 break;
484 }
485 }
486
487 if (!block) {
Alex Williamsonfb787f82010-07-02 11:13:29 -0600488 fprintf(stderr, "Unknown ramblock \"%s\", cannot "
489 "accept migration\n", id);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300490 ret = -EINVAL;
491 goto done;
Alex Williamson97ab12d2010-06-25 11:09:50 -0600492 }
493
494 total_ram_bytes -= length;
495 }
Blue Swirlad960902010-03-29 19:23:52 +0000496 }
497 }
498
499 if (flags & RAM_SAVE_FLAG_COMPRESS) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600500 void *host;
501 uint8_t ch;
502
Avi Kivityf09f2182011-12-21 13:37:56 +0200503 host = host_from_stream_offset(f, addr, flags);
Michael S. Tsirkin492fb992010-10-17 20:43:40 +0200504 if (!host) {
505 return -EINVAL;
506 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600507
Alex Williamson97ab12d2010-06-25 11:09:50 -0600508 ch = qemu_get_byte(f);
509 memset(host, ch, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +0000510#ifndef _WIN32
511 if (ch == 0 &&
512 (!kvm_enabled() || kvm_has_sync_mmu())) {
Andreas Färbere78815a2010-09-25 11:26:05 +0000513 qemu_madvise(host, TARGET_PAGE_SIZE, QEMU_MADV_DONTNEED);
Blue Swirlad960902010-03-29 19:23:52 +0000514 }
515#endif
516 } else if (flags & RAM_SAVE_FLAG_PAGE) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600517 void *host;
518
Avi Kivityf09f2182011-12-21 13:37:56 +0200519 host = host_from_stream_offset(f, addr, flags);
Orit Wasserman0ff1f9f2012-06-19 11:51:37 +0300520 if (!host) {
521 return -EINVAL;
522 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600523
Alex Williamson97ab12d2010-06-25 11:09:50 -0600524 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +0000525 }
Juan Quintela42802d42011-10-05 01:14:46 +0200526 error = qemu_file_get_error(f);
527 if (error) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300528 ret = error;
529 goto done;
Blue Swirlad960902010-03-29 19:23:52 +0000530 }
531 } while (!(flags & RAM_SAVE_FLAG_EOS));
532
Orit Wasserman3a697f62012-06-19 18:43:15 +0300533done:
534 DPRINTF("Completed load of VM with exit code %d seq iteration " PRIu64 "\n",
535 ret, seq_iter);
536 return ret;
Blue Swirlad960902010-03-29 19:23:52 +0000537}
538
Blue Swirlad960902010-03-29 19:23:52 +0000539#ifdef HAS_AUDIO
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900540struct soundhw {
541 const char *name;
542 const char *descr;
543 int enabled;
544 int isa;
545 union {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100546 int (*init_isa) (ISABus *bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900547 int (*init_pci) (PCIBus *bus);
548 } init;
549};
550
551static struct soundhw soundhw[] = {
Blue Swirlad960902010-03-29 19:23:52 +0000552#ifdef HAS_AUDIO_CHOICE
Hervé Poussineauda128722012-04-14 22:51:33 +0200553#ifdef CONFIG_PCSPK
Blue Swirlad960902010-03-29 19:23:52 +0000554 {
555 "pcspk",
556 "PC speaker",
557 0,
558 1,
559 { .init_isa = pcspk_audio_init }
560 },
561#endif
562
563#ifdef CONFIG_SB16
564 {
565 "sb16",
566 "Creative Sound Blaster 16",
567 0,
568 1,
569 { .init_isa = SB16_init }
570 },
571#endif
572
573#ifdef CONFIG_CS4231A
574 {
575 "cs4231a",
576 "CS4231A",
577 0,
578 1,
579 { .init_isa = cs4231a_init }
580 },
581#endif
582
583#ifdef CONFIG_ADLIB
584 {
585 "adlib",
586#ifdef HAS_YMF262
587 "Yamaha YMF262 (OPL3)",
588#else
589 "Yamaha YM3812 (OPL2)",
590#endif
591 0,
592 1,
593 { .init_isa = Adlib_init }
594 },
595#endif
596
597#ifdef CONFIG_GUS
598 {
599 "gus",
600 "Gravis Ultrasound GF1",
601 0,
602 1,
603 { .init_isa = GUS_init }
604 },
605#endif
606
607#ifdef CONFIG_AC97
608 {
609 "ac97",
610 "Intel 82801AA AC97 Audio",
611 0,
612 0,
613 { .init_pci = ac97_init }
614 },
615#endif
616
617#ifdef CONFIG_ES1370
618 {
619 "es1370",
620 "ENSONIQ AudioPCI ES1370",
621 0,
622 0,
623 { .init_pci = es1370_init }
624 },
625#endif
626
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100627#ifdef CONFIG_HDA
628 {
629 "hda",
630 "Intel HD Audio",
631 0,
632 0,
633 { .init_pci = intel_hda_and_codec_init }
634 },
635#endif
636
Blue Swirlad960902010-03-29 19:23:52 +0000637#endif /* HAS_AUDIO_CHOICE */
638
639 { NULL, NULL, 0, 0, { NULL } }
640};
641
642void select_soundhw(const char *optarg)
643{
644 struct soundhw *c;
645
646 if (*optarg == '?') {
647 show_valid_cards:
648
649 printf("Valid sound card names (comma separated):\n");
650 for (c = soundhw; c->name; ++c) {
651 printf ("%-11s %s\n", c->name, c->descr);
652 }
653 printf("\n-soundhw all will enable all of the above\n");
654 exit(*optarg != '?');
655 }
656 else {
657 size_t l;
658 const char *p;
659 char *e;
660 int bad_card = 0;
661
662 if (!strcmp(optarg, "all")) {
663 for (c = soundhw; c->name; ++c) {
664 c->enabled = 1;
665 }
666 return;
667 }
668
669 p = optarg;
670 while (*p) {
671 e = strchr(p, ',');
672 l = !e ? strlen(p) : (size_t) (e - p);
673
674 for (c = soundhw; c->name; ++c) {
675 if (!strncmp(c->name, p, l) && !c->name[l]) {
676 c->enabled = 1;
677 break;
678 }
679 }
680
681 if (!c->name) {
682 if (l > 80) {
683 fprintf(stderr,
684 "Unknown sound card name (too big to show)\n");
685 }
686 else {
687 fprintf(stderr, "Unknown sound card name `%.*s'\n",
688 (int) l, p);
689 }
690 bad_card = 1;
691 }
692 p += l + (e != NULL);
693 }
694
695 if (bad_card) {
696 goto show_valid_cards;
697 }
698 }
699}
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900700
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100701void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900702{
703 struct soundhw *c;
704
705 for (c = soundhw; c->name; ++c) {
706 if (c->enabled) {
707 if (c->isa) {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100708 if (isa_bus) {
709 c->init.init_isa(isa_bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900710 }
711 } else {
712 if (pci_bus) {
713 c->init.init_pci(pci_bus);
714 }
715 }
716 }
717 }
718}
Blue Swirlad960902010-03-29 19:23:52 +0000719#else
720void select_soundhw(const char *optarg)
721{
722}
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100723void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900724{
725}
Blue Swirlad960902010-03-29 19:23:52 +0000726#endif
727
728int qemu_uuid_parse(const char *str, uint8_t *uuid)
729{
730 int ret;
731
732 if (strlen(str) != 36) {
733 return -1;
734 }
735
736 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
737 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
738 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
739 &uuid[15]);
740
741 if (ret != 16) {
742 return -1;
743 }
744#ifdef TARGET_I386
745 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
746#endif
747 return 0;
748}
749
750void do_acpitable_option(const char *optarg)
751{
752#ifdef TARGET_I386
753 if (acpi_table_add(optarg) < 0) {
754 fprintf(stderr, "Wrong acpi table provided\n");
755 exit(1);
756 }
757#endif
758}
759
760void do_smbios_option(const char *optarg)
761{
762#ifdef TARGET_I386
763 if (smbios_entry_add(optarg) < 0) {
764 fprintf(stderr, "Wrong smbios provided\n");
765 exit(1);
766 }
767#endif
768}
769
770void cpudef_init(void)
771{
772#if defined(cpudef_setup)
773 cpudef_setup(); /* parse cpu definitions in target config file */
774#endif
775}
776
777int audio_available(void)
778{
779#ifdef HAS_AUDIO
780 return 1;
781#else
782 return 0;
783#endif
784}
785
Anthony PERARD303d4e82010-09-21 20:05:31 +0100786int tcg_available(void)
787{
788 return 1;
789}
790
Blue Swirlad960902010-03-29 19:23:52 +0000791int kvm_available(void)
792{
793#ifdef CONFIG_KVM
794 return 1;
795#else
796 return 0;
797#endif
798}
799
800int xen_available(void)
801{
802#ifdef CONFIG_XEN
803 return 1;
804#else
805 return 0;
806#endif
807}