blob: 7b65c4888b7151dfb6a7f8e0cd226df3f86037be [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
Guan Xuetao4f23a1e2012-08-10 14:42:21 +080094#elif defined(TARGET_UNICORE32)
95#define QEMU_ARCH QEMU_ARCH_UNICORE32
Blue Swirlad960902010-03-29 19:23:52 +000096#endif
97
98const uint32_t arch_type = QEMU_ARCH;
99
100/***********************************************************/
101/* ram save/restore */
102
Yoshiaki Tamurad20878d2010-08-18 13:30:12 +0900103#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
Blue Swirlad960902010-03-29 19:23:52 +0000109
Paolo Bonzini86003612011-12-23 16:17:26 +0100110#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)
Andreas Färberf283edc2012-05-27 16:21:02 +0200115/* altivec.h may redefine the bool macro as vector type.
116 * Reset it to POSIX semantics. */
117#undef bool
118#define bool _Bool
Paolo Bonzini86003612011-12-23 16:17:26 +0100119#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
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300130
Eduardo Habkost756557d2012-05-02 13:07:27 -0300131static struct defconfig_file {
132 const char *filename;
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300133 /* Indicates it is an user config file (disabled by -no-user-config) */
134 bool userconfig;
Eduardo Habkost756557d2012-05-02 13:07:27 -0300135} default_config_files[] = {
Eduardo Habkoste2d87bf2012-05-02 13:07:30 -0300136 { CONFIG_QEMU_DATADIR "/cpus-" TARGET_ARCH ".conf", false },
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300137 { CONFIG_QEMU_CONFDIR "/qemu.conf", true },
138 { CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf", true },
Eduardo Habkost756557d2012-05-02 13:07:27 -0300139 { NULL }, /* end of list */
140};
141
142
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300143int qemu_read_default_config_files(bool userconfig)
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300144{
145 int ret;
Eduardo Habkost756557d2012-05-02 13:07:27 -0300146 struct defconfig_file *f;
147
148 for (f = default_config_files; f->filename; f++) {
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300149 if (!userconfig && f->userconfig) {
150 continue;
151 }
Eduardo Habkost756557d2012-05-02 13:07:27 -0300152 ret = qemu_read_config_file(f->filename);
153 if (ret < 0 && ret != -ENOENT) {
154 return ret;
155 }
156 }
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300157
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300158 return 0;
159}
160
Paolo Bonzini86003612011-12-23 16:17:26 +0100161static int is_dup_page(uint8_t *page)
Blue Swirlad960902010-03-29 19:23:52 +0000162{
Paolo Bonzini86003612011-12-23 16:17:26 +0100163 VECTYPE *p = (VECTYPE *)page;
164 VECTYPE val = SPLAT(page);
Blue Swirlad960902010-03-29 19:23:52 +0000165 int i;
166
Paolo Bonzini86003612011-12-23 16:17:26 +0100167 for (i = 0; i < TARGET_PAGE_SIZE / sizeof(VECTYPE); i++) {
168 if (!ALL_EQ(val, p[i])) {
Blue Swirlad960902010-03-29 19:23:52 +0000169 return 0;
170 }
171 }
172
173 return 1;
174}
175
Orit Wasserman0c51f432012-06-19 18:43:14 +0300176static 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
Alex Williamson760e77e2010-08-19 10:18:42 -0300188static RAMBlock *last_block;
189static ram_addr_t last_offset;
190
Orit Wasserman6c779f22012-07-10 12:37:13 +0300191/*
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
Blue Swirlad960902010-03-29 19:23:52 +0000199static int ram_save_block(QEMUFile *f)
200{
Alex Williamsone44359c2010-06-25 11:09:57 -0600201 RAMBlock *block = last_block;
202 ram_addr_t offset = last_offset;
Orit Wasserman6c779f22012-07-10 12:37:13 +0300203 int bytes_sent = -1;
Avi Kivity71c510e2011-12-21 13:11:22 +0200204 MemoryRegion *mr;
Blue Swirlad960902010-03-29 19:23:52 +0000205
Alex Williamsone44359c2010-06-25 11:09:57 -0600206 if (!block)
207 block = QLIST_FIRST(&ram_list.blocks);
208
Alex Williamsone44359c2010-06-25 11:09:57 -0600209 do {
Avi Kivity71c510e2011-12-21 13:11:22 +0200210 mr = block->mr;
Blue Swirlcd7a45c2012-01-22 16:38:21 +0000211 if (memory_region_get_dirty(mr, offset, TARGET_PAGE_SIZE,
212 DIRTY_MEMORY_MIGRATION)) {
Blue Swirlad960902010-03-29 19:23:52 +0000213 uint8_t *p;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600214 int cont = (block == last_block) ? RAM_SAVE_FLAG_CONTINUE : 0;
Blue Swirlad960902010-03-29 19:23:52 +0000215
Avi Kivity71c510e2011-12-21 13:11:22 +0200216 memory_region_reset_dirty(mr, offset, TARGET_PAGE_SIZE,
217 DIRTY_MEMORY_MIGRATION);
Blue Swirlad960902010-03-29 19:23:52 +0000218
Avi Kivity71c510e2011-12-21 13:11:22 +0200219 p = memory_region_get_ram_ptr(mr) + offset;
Blue Swirlad960902010-03-29 19:23:52 +0000220
Paolo Bonzini86003612011-12-23 16:17:26 +0100221 if (is_dup_page(p)) {
Orit Wasserman0c51f432012-06-19 18:43:14 +0300222 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_COMPRESS);
Blue Swirlad960902010-03-29 19:23:52 +0000223 qemu_put_byte(f, *p);
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200224 bytes_sent = 1;
Blue Swirlad960902010-03-29 19:23:52 +0000225 } else {
Orit Wasserman0c51f432012-06-19 18:43:14 +0300226 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
Blue Swirlad960902010-03-29 19:23:52 +0000227 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200228 bytes_sent = TARGET_PAGE_SIZE;
Blue Swirlad960902010-03-29 19:23:52 +0000229 }
230
Blue Swirlad960902010-03-29 19:23:52 +0000231 break;
232 }
Alex Williamsone44359c2010-06-25 11:09:57 -0600233
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 }
Avi Kivity71c510e2011-12-21 13:11:22 +0200241 } while (block != last_block || offset != last_offset);
Alex Williamsone44359c2010-06-25 11:09:57 -0600242
243 last_block = block;
244 last_offset = offset;
Blue Swirlad960902010-03-29 19:23:52 +0000245
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200246 return bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000247}
248
249static uint64_t bytes_transferred;
250
251static ram_addr_t ram_save_remaining(void)
252{
Juan Quintela45f33f02012-06-22 15:21:07 +0200253 return ram_list.dirty_pages;
Blue Swirlad960902010-03-29 19:23:52 +0000254}
255
256uint64_t ram_bytes_remaining(void)
257{
258 return ram_save_remaining() * TARGET_PAGE_SIZE;
259}
260
261uint64_t ram_bytes_transferred(void)
262{
263 return bytes_transferred;
264}
265
266uint64_t ram_bytes_total(void)
267{
Alex Williamsond17b5282010-06-25 11:08:38 -0600268 RAMBlock *block;
269 uint64_t total = 0;
270
271 QLIST_FOREACH(block, &ram_list.blocks, next)
272 total += block->length;
273
274 return total;
Blue Swirlad960902010-03-29 19:23:52 +0000275}
276
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200277static int block_compar(const void *a, const void *b)
278{
279 RAMBlock * const *ablock = a;
280 RAMBlock * const *bblock = b;
Avi Kivity8fec98b2011-12-21 13:22:16 +0200281
282 return strcmp((*ablock)->idstr, (*bblock)->idstr);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200283}
284
285static 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 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500293 blocks = g_malloc(n * sizeof *blocks);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200294 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 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500303 g_free(blocks);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200304}
305
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300306static void migration_end(void)
307{
308 memory_global_dirty_log_stop();
309}
310
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200311static void ram_migration_cancel(void *opaque)
312{
313 migration_end();
314}
315
Juan Quintela4508bd92012-05-22 16:27:59 +0200316#define MAX_WAIT 50 /* ms, half buffered_file limit */
317
Juan Quintelad1315aa2012-06-28 15:11:57 +0200318static int ram_save_setup(QEMUFile *f, void *opaque)
Blue Swirlad960902010-03-29 19:23:52 +0000319{
320 ram_addr_t addr;
Juan Quintelad1315aa2012-06-28 15:11:57 +0200321 RAMBlock *block;
Blue Swirlad960902010-03-29 19:23:52 +0000322
Juan Quintelad1315aa2012-06-28 15:11:57 +0200323 bytes_transferred = 0;
324 last_block = NULL;
325 last_offset = 0;
326 sort_ram_list();
Blue Swirlad960902010-03-29 19:23:52 +0000327
Juan Quintelad1315aa2012-06-28 15:11:57 +0200328 /* 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);
Blue Swirlad960902010-03-29 19:23:52 +0000334 }
335 }
Blue Swirlad960902010-03-29 19:23:52 +0000336 }
337
Juan Quintelad1315aa2012-06-28 15:11:57 +0200338 memory_global_dirty_log_start();
339
340 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
341
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);
346 }
347
Juan Quintelad1315aa2012-06-28 15:11:57 +0200348 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
349
350 return 0;
351}
352
Juan Quintela16310a32012-06-28 15:31:37 +0200353static int ram_save_iterate(QEMUFile *f, void *opaque)
Juan Quintelad1315aa2012-06-28 15:11:57 +0200354{
Blue Swirlad960902010-03-29 19:23:52 +0000355 uint64_t bytes_transferred_last;
356 double bwidth = 0;
357 int ret;
358 int i;
Juan Quintela16310a32012-06-28 15:31:37 +0200359 uint64_t expected_time;
Blue Swirlad960902010-03-29 19:23:52 +0000360
361 bytes_transferred_last = bytes_transferred;
362 bwidth = qemu_get_clock_ns(rt_clock);
363
Juan Quintela4508bd92012-05-22 16:27:59 +0200364 i = 0;
Juan Quintela29757252011-10-19 15:22:18 +0200365 while ((ret = qemu_file_rate_limit(f)) == 0) {
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200366 int bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000367
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200368 bytes_sent = ram_save_block(f);
Orit Wasserman6c779f22012-07-10 12:37:13 +0300369 /* no more blocks to sent */
370 if (bytes_sent < 0) {
Blue Swirlad960902010-03-29 19:23:52 +0000371 break;
372 }
Orit Wasserman6c779f22012-07-10 12:37:13 +0300373 bytes_transferred += bytes_sent;
Juan Quintela4508bd92012-05-22 16:27:59 +0200374 /* 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++;
Blue Swirlad960902010-03-29 19:23:52 +0000388 }
389
Juan Quintela29757252011-10-19 15:22:18 +0200390 if (ret < 0) {
391 return ret;
392 }
393
Blue Swirlad960902010-03-29 19:23:52 +0000394 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
Blue Swirlad960902010-03-29 19:23:52 +0000403 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
404
Juan Quintela16310a32012-06-28 15:31:37 +0200405 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
Blue Swirlad960902010-03-29 19:23:52 +0000406
Juan Quintela16310a32012-06-28 15:31:37 +0200407 DPRINTF("ram_save_live: expected(" PRIu64 ") <= max(" PRIu64 ")?\n",
408 expected_time, migrate_max_downtime());
409
Juan Quintela00d94f32012-06-28 19:51:17 +0200410 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;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300413
Juan Quintela5b3c9632012-05-22 00:44:24 +0200414 return expected_time <= migrate_max_downtime();
415 }
416 return 0;
Blue Swirlad960902010-03-29 19:23:52 +0000417}
418
Juan Quintela16310a32012-06-28 15:31:37 +0200419static int ram_save_complete(QEMUFile *f, void *opaque)
420{
Juan Quintela16310a32012-06-28 15:31:37 +0200421 memory_global_sync_dirty_bitmap(get_system_memory());
422
Juan Quintela16310a32012-06-28 15:31:37 +0200423 /* try transferring iterative blocks of memory */
424
425 /* flush all remaining blocks regardless of rate limiting */
Orit Wasserman6c779f22012-07-10 12:37:13 +0300426 while (true) {
427 int bytes_sent;
428
429 bytes_sent = ram_save_block(f);
430 /* no more blocks to sent */
431 if (bytes_sent < 0) {
432 break;
433 }
Juan Quintela16310a32012-06-28 15:31:37 +0200434 bytes_transferred += bytes_sent;
435 }
436 memory_global_dirty_log_stop();
437
Blue Swirlad960902010-03-29 19:23:52 +0000438 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
439
Alex Williamsona55bbe32010-06-25 11:10:05 -0600440 return 0;
441}
442
443static 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");
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200454 return NULL;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600455 }
456
457 return memory_region_get_ram_ptr(block->mr) + offset;
458 }
459
460 len = qemu_get_byte(f);
461 qemu_get_buffer(f, (uint8_t *)id, len);
462 id[len] = 0;
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200463
Alex Williamsona55bbe32010-06-25 11:10:05 -0600464 QLIST_FOREACH(block, &ram_list.blocks, next) {
465 if (!strncmp(id, block->idstr, sizeof(id)))
466 return memory_region_get_ram_ptr(block->mr) + offset;
467 }
468
469 fprintf(stderr, "Can't find block %s!\n", id);
Blue Swirlad960902010-03-29 19:23:52 +0000470 return NULL;
471}
472
Juan Quintela7908c782012-06-26 18:46:10 +0200473static int ram_load(QEMUFile *f, void *opaque, int version_id)
Blue Swirlad960902010-03-29 19:23:52 +0000474{
475 ram_addr_t addr;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300476 int flags, ret = 0;
Juan Quintela42802d42011-10-05 01:14:46 +0200477 int error;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300478 static uint64_t seq_iter;
479
480 seq_iter++;
Blue Swirlad960902010-03-29 19:23:52 +0000481
Avi Kivityf09f2182011-12-21 13:37:56 +0200482 if (version_id < 4 || version_id > 4) {
Blue Swirlad960902010-03-29 19:23:52 +0000483 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) {
Avi Kivityf09f2182011-12-21 13:37:56 +0200493 if (version_id == 4) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600494 /* 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))) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300510 if (block->length != length) {
511 ret = -EINVAL;
512 goto done;
513 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600514 break;
515 }
516 }
517
518 if (!block) {
Alex Williamsonfb787f82010-07-02 11:13:29 -0600519 fprintf(stderr, "Unknown ramblock \"%s\", cannot "
520 "accept migration\n", id);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300521 ret = -EINVAL;
522 goto done;
Alex Williamson97ab12d2010-06-25 11:09:50 -0600523 }
524
525 total_ram_bytes -= length;
526 }
Blue Swirlad960902010-03-29 19:23:52 +0000527 }
528 }
529
530 if (flags & RAM_SAVE_FLAG_COMPRESS) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600531 void *host;
532 uint8_t ch;
533
Avi Kivityf09f2182011-12-21 13:37:56 +0200534 host = host_from_stream_offset(f, addr, flags);
Michael S. Tsirkin492fb992010-10-17 20:43:40 +0200535 if (!host) {
536 return -EINVAL;
537 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600538
Alex Williamson97ab12d2010-06-25 11:09:50 -0600539 ch = qemu_get_byte(f);
540 memset(host, ch, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +0000541#ifndef _WIN32
542 if (ch == 0 &&
543 (!kvm_enabled() || kvm_has_sync_mmu())) {
Andreas Färbere78815a2010-09-25 11:26:05 +0000544 qemu_madvise(host, TARGET_PAGE_SIZE, QEMU_MADV_DONTNEED);
Blue Swirlad960902010-03-29 19:23:52 +0000545 }
546#endif
547 } else if (flags & RAM_SAVE_FLAG_PAGE) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600548 void *host;
549
Avi Kivityf09f2182011-12-21 13:37:56 +0200550 host = host_from_stream_offset(f, addr, flags);
Orit Wasserman0ff1f9f2012-06-19 11:51:37 +0300551 if (!host) {
552 return -EINVAL;
553 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600554
Alex Williamson97ab12d2010-06-25 11:09:50 -0600555 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +0000556 }
Juan Quintela42802d42011-10-05 01:14:46 +0200557 error = qemu_file_get_error(f);
558 if (error) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300559 ret = error;
560 goto done;
Blue Swirlad960902010-03-29 19:23:52 +0000561 }
562 } while (!(flags & RAM_SAVE_FLAG_EOS));
563
Orit Wasserman3a697f62012-06-19 18:43:15 +0300564done:
565 DPRINTF("Completed load of VM with exit code %d seq iteration " PRIu64 "\n",
566 ret, seq_iter);
567 return ret;
Blue Swirlad960902010-03-29 19:23:52 +0000568}
569
Juan Quintela7908c782012-06-26 18:46:10 +0200570SaveVMHandlers savevm_ram_handlers = {
Juan Quintelad1315aa2012-06-28 15:11:57 +0200571 .save_live_setup = ram_save_setup,
Juan Quintela16310a32012-06-28 15:31:37 +0200572 .save_live_iterate = ram_save_iterate,
573 .save_live_complete = ram_save_complete,
Juan Quintela7908c782012-06-26 18:46:10 +0200574 .load_state = ram_load,
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200575 .cancel = ram_migration_cancel,
Juan Quintela7908c782012-06-26 18:46:10 +0200576};
577
Blue Swirlad960902010-03-29 19:23:52 +0000578#ifdef HAS_AUDIO
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900579struct soundhw {
580 const char *name;
581 const char *descr;
582 int enabled;
583 int isa;
584 union {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100585 int (*init_isa) (ISABus *bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900586 int (*init_pci) (PCIBus *bus);
587 } init;
588};
589
590static struct soundhw soundhw[] = {
Blue Swirlad960902010-03-29 19:23:52 +0000591#ifdef HAS_AUDIO_CHOICE
Hervé Poussineauda128722012-04-14 22:51:33 +0200592#ifdef CONFIG_PCSPK
Blue Swirlad960902010-03-29 19:23:52 +0000593 {
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
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100666#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
Blue Swirlad960902010-03-29 19:23:52 +0000676#endif /* HAS_AUDIO_CHOICE */
677
678 { NULL, NULL, 0, 0, { NULL } }
679};
680
681void select_soundhw(const char *optarg)
682{
683 struct soundhw *c;
684
Peter Maydellc8057f92012-08-02 13:45:54 +0100685 if (is_help_option(optarg)) {
Blue Swirlad960902010-03-29 19:23:52 +0000686 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");
Peter Maydellc8057f92012-08-02 13:45:54 +0100693 exit(!is_help_option(optarg));
Blue Swirlad960902010-03-29 19:23:52 +0000694 }
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}
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900739
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100740void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900741{
742 struct soundhw *c;
743
744 for (c = soundhw; c->name; ++c) {
745 if (c->enabled) {
746 if (c->isa) {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100747 if (isa_bus) {
748 c->init.init_isa(isa_bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900749 }
750 } else {
751 if (pci_bus) {
752 c->init.init_pci(pci_bus);
753 }
754 }
755 }
756 }
757}
Blue Swirlad960902010-03-29 19:23:52 +0000758#else
759void select_soundhw(const char *optarg)
760{
761}
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100762void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900763{
764}
Blue Swirlad960902010-03-29 19:23:52 +0000765#endif
766
767int 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
789void 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
799void 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
809void cpudef_init(void)
810{
811#if defined(cpudef_setup)
812 cpudef_setup(); /* parse cpu definitions in target config file */
813#endif
814}
815
816int audio_available(void)
817{
818#ifdef HAS_AUDIO
819 return 1;
820#else
821 return 0;
822#endif
823}
824
Anthony PERARD303d4e82010-09-21 20:05:31 +0100825int tcg_available(void)
826{
827 return 1;
828}
829
Blue Swirlad960902010-03-29 19:23:52 +0000830int kvm_available(void)
831{
832#ifdef CONFIG_KVM
833 return 1;
834#else
835 return 0;
836#endif
837}
838
839int xen_available(void)
840{
841#ifdef CONFIG_XEN
842 return 1;
843#else
844 return 0;
845#endif
846}