blob: cec3ead8dec0b1a0be4f4e6c5663bec1b9bedd28 [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"
Orit Wasserman17ad9b32012-08-06 21:42:53 +030046#include "qemu/page_cache.h"
Daniel P. Berrange99afc912012-08-20 15:31:38 +010047#include "qmp-commands.h"
Juan Quintela3c121932012-09-04 13:08:57 +020048#include "trace.h"
Blue Swirlad960902010-03-29 19:23:52 +000049
Orit Wasserman3a697f62012-06-19 18:43:15 +030050#ifdef DEBUG_ARCH_INIT
51#define DPRINTF(fmt, ...) \
52 do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
53#else
54#define DPRINTF(fmt, ...) \
55 do { } while (0)
56#endif
57
Blue Swirlad960902010-03-29 19:23:52 +000058#ifdef TARGET_SPARC
59int graphic_width = 1024;
60int graphic_height = 768;
61int graphic_depth = 8;
62#else
63int graphic_width = 800;
64int graphic_height = 600;
65int graphic_depth = 15;
66#endif
67
Blue Swirlad960902010-03-29 19:23:52 +000068
69#if defined(TARGET_ALPHA)
70#define QEMU_ARCH QEMU_ARCH_ALPHA
71#elif defined(TARGET_ARM)
72#define QEMU_ARCH QEMU_ARCH_ARM
73#elif defined(TARGET_CRIS)
74#define QEMU_ARCH QEMU_ARCH_CRIS
75#elif defined(TARGET_I386)
76#define QEMU_ARCH QEMU_ARCH_I386
77#elif defined(TARGET_M68K)
78#define QEMU_ARCH QEMU_ARCH_M68K
Michael Walle81ea0e12011-02-17 23:45:02 +010079#elif defined(TARGET_LM32)
80#define QEMU_ARCH QEMU_ARCH_LM32
Blue Swirlad960902010-03-29 19:23:52 +000081#elif defined(TARGET_MICROBLAZE)
82#define QEMU_ARCH QEMU_ARCH_MICROBLAZE
83#elif defined(TARGET_MIPS)
84#define QEMU_ARCH QEMU_ARCH_MIPS
Jia Liue67db062012-07-20 15:50:39 +080085#elif defined(TARGET_OPENRISC)
86#define QEMU_ARCH QEMU_ARCH_OPENRISC
Blue Swirlad960902010-03-29 19:23:52 +000087#elif defined(TARGET_PPC)
88#define QEMU_ARCH QEMU_ARCH_PPC
89#elif defined(TARGET_S390X)
90#define QEMU_ARCH QEMU_ARCH_S390X
91#elif defined(TARGET_SH4)
92#define QEMU_ARCH QEMU_ARCH_SH4
93#elif defined(TARGET_SPARC)
94#define QEMU_ARCH QEMU_ARCH_SPARC
Max Filippov23288262011-09-06 03:55:25 +040095#elif defined(TARGET_XTENSA)
96#define QEMU_ARCH QEMU_ARCH_XTENSA
Guan Xuetao4f23a1e2012-08-10 14:42:21 +080097#elif defined(TARGET_UNICORE32)
98#define QEMU_ARCH QEMU_ARCH_UNICORE32
Blue Swirlad960902010-03-29 19:23:52 +000099#endif
100
101const uint32_t arch_type = QEMU_ARCH;
102
103/***********************************************************/
104/* ram save/restore */
105
Yoshiaki Tamurad20878d2010-08-18 13:30:12 +0900106#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
107#define RAM_SAVE_FLAG_COMPRESS 0x02
108#define RAM_SAVE_FLAG_MEM_SIZE 0x04
109#define RAM_SAVE_FLAG_PAGE 0x08
110#define RAM_SAVE_FLAG_EOS 0x10
111#define RAM_SAVE_FLAG_CONTINUE 0x20
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300112#define RAM_SAVE_FLAG_XBZRLE 0x40
Blue Swirlad960902010-03-29 19:23:52 +0000113
Paolo Bonzini86003612011-12-23 16:17:26 +0100114#ifdef __ALTIVEC__
115#include <altivec.h>
116#define VECTYPE vector unsigned char
117#define SPLAT(p) vec_splat(vec_ld(0, p), 0)
118#define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
Andreas Färberf283edc2012-05-27 16:21:02 +0200119/* altivec.h may redefine the bool macro as vector type.
120 * Reset it to POSIX semantics. */
121#undef bool
122#define bool _Bool
Paolo Bonzini86003612011-12-23 16:17:26 +0100123#elif defined __SSE2__
124#include <emmintrin.h>
125#define VECTYPE __m128i
126#define SPLAT(p) _mm_set1_epi8(*(p))
127#define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
128#else
129#define VECTYPE unsigned long
130#define SPLAT(p) (*(p) * (~0UL / 255))
131#define ALL_EQ(v1, v2) ((v1) == (v2))
132#endif
133
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300134
Eduardo Habkost756557d2012-05-02 13:07:27 -0300135static struct defconfig_file {
136 const char *filename;
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300137 /* Indicates it is an user config file (disabled by -no-user-config) */
138 bool userconfig;
Eduardo Habkost756557d2012-05-02 13:07:27 -0300139} default_config_files[] = {
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300140 { CONFIG_QEMU_CONFDIR "/qemu.conf", true },
141 { CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf", true },
Eduardo Habkost756557d2012-05-02 13:07:27 -0300142 { NULL }, /* end of list */
143};
144
145
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300146int qemu_read_default_config_files(bool userconfig)
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300147{
148 int ret;
Eduardo Habkost756557d2012-05-02 13:07:27 -0300149 struct defconfig_file *f;
150
151 for (f = default_config_files; f->filename; f++) {
Eduardo Habkostf29a5612012-05-02 13:07:29 -0300152 if (!userconfig && f->userconfig) {
153 continue;
154 }
Eduardo Habkost756557d2012-05-02 13:07:27 -0300155 ret = qemu_read_config_file(f->filename);
156 if (ret < 0 && ret != -ENOENT) {
157 return ret;
158 }
159 }
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300160
Eduardo Habkostb5a8fe52012-05-02 13:07:25 -0300161 return 0;
162}
163
Paolo Bonzini86003612011-12-23 16:17:26 +0100164static int is_dup_page(uint8_t *page)
Blue Swirlad960902010-03-29 19:23:52 +0000165{
Paolo Bonzini86003612011-12-23 16:17:26 +0100166 VECTYPE *p = (VECTYPE *)page;
167 VECTYPE val = SPLAT(page);
Blue Swirlad960902010-03-29 19:23:52 +0000168 int i;
169
Paolo Bonzini86003612011-12-23 16:17:26 +0100170 for (i = 0; i < TARGET_PAGE_SIZE / sizeof(VECTYPE); i++) {
171 if (!ALL_EQ(val, p[i])) {
Blue Swirlad960902010-03-29 19:23:52 +0000172 return 0;
173 }
174 }
175
176 return 1;
177}
178
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300179/* struct contains XBZRLE cache and a static page
180 used by the compression */
181static struct {
182 /* buffer used for XBZRLE encoding */
183 uint8_t *encoded_buf;
184 /* buffer for storing page content */
185 uint8_t *current_buf;
186 /* buffer used for XBZRLE decoding */
187 uint8_t *decoded_buf;
188 /* Cache for XBZRLE */
189 PageCache *cache;
190} XBZRLE = {
191 .encoded_buf = NULL,
192 .current_buf = NULL,
193 .decoded_buf = NULL,
194 .cache = NULL,
195};
196
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300197
198int64_t xbzrle_cache_resize(int64_t new_size)
199{
200 if (XBZRLE.cache != NULL) {
201 return cache_resize(XBZRLE.cache, new_size / TARGET_PAGE_SIZE) *
202 TARGET_PAGE_SIZE;
203 }
204 return pow2floor(new_size);
205}
206
Orit Wasserman004d4c12012-08-06 21:42:56 +0300207/* accounting for migration statistics */
208typedef struct AccountingInfo {
209 uint64_t dup_pages;
210 uint64_t norm_pages;
211 uint64_t iterations;
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300212 uint64_t xbzrle_bytes;
213 uint64_t xbzrle_pages;
214 uint64_t xbzrle_cache_miss;
215 uint64_t xbzrle_overflows;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300216} AccountingInfo;
217
218static AccountingInfo acct_info;
219
220static void acct_clear(void)
221{
222 memset(&acct_info, 0, sizeof(acct_info));
223}
224
225uint64_t dup_mig_bytes_transferred(void)
226{
227 return acct_info.dup_pages * TARGET_PAGE_SIZE;
228}
229
230uint64_t dup_mig_pages_transferred(void)
231{
232 return acct_info.dup_pages;
233}
234
235uint64_t norm_mig_bytes_transferred(void)
236{
237 return acct_info.norm_pages * TARGET_PAGE_SIZE;
238}
239
240uint64_t norm_mig_pages_transferred(void)
241{
242 return acct_info.norm_pages;
243}
244
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300245uint64_t xbzrle_mig_bytes_transferred(void)
246{
247 return acct_info.xbzrle_bytes;
248}
249
250uint64_t xbzrle_mig_pages_transferred(void)
251{
252 return acct_info.xbzrle_pages;
253}
254
255uint64_t xbzrle_mig_pages_cache_miss(void)
256{
257 return acct_info.xbzrle_cache_miss;
258}
259
260uint64_t xbzrle_mig_pages_overflow(void)
261{
262 return acct_info.xbzrle_overflows;
263}
264
Orit Wasserman0c51f432012-06-19 18:43:14 +0300265static void save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
266 int cont, int flag)
267{
268 qemu_put_be64(f, offset | cont | flag);
269 if (!cont) {
270 qemu_put_byte(f, strlen(block->idstr));
271 qemu_put_buffer(f, (uint8_t *)block->idstr,
272 strlen(block->idstr));
273 }
274
275}
276
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300277#define ENCODING_FLAG_XBZRLE 0x1
278
279static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
280 ram_addr_t current_addr, RAMBlock *block,
Juan Quinteladd051c72012-08-06 21:42:58 +0300281 ram_addr_t offset, int cont, bool last_stage)
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300282{
283 int encoded_len = 0, bytes_sent = -1;
284 uint8_t *prev_cached_page;
285
286 if (!cache_is_cached(XBZRLE.cache, current_addr)) {
Juan Quinteladd051c72012-08-06 21:42:58 +0300287 if (!last_stage) {
288 cache_insert(XBZRLE.cache, current_addr,
289 g_memdup(current_data, TARGET_PAGE_SIZE));
290 }
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300291 acct_info.xbzrle_cache_miss++;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300292 return -1;
293 }
294
295 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
296
297 /* save current buffer into memory */
298 memcpy(XBZRLE.current_buf, current_data, TARGET_PAGE_SIZE);
299
300 /* XBZRLE encoding (if there is no overflow) */
301 encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
302 TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
303 TARGET_PAGE_SIZE);
304 if (encoded_len == 0) {
305 DPRINTF("Skipping unmodified page\n");
306 return 0;
307 } else if (encoded_len == -1) {
308 DPRINTF("Overflow\n");
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300309 acct_info.xbzrle_overflows++;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300310 /* update data in the cache */
311 memcpy(prev_cached_page, current_data, TARGET_PAGE_SIZE);
312 return -1;
313 }
314
315 /* we need to update the data in the cache, in order to get the same data */
Juan Quinteladd051c72012-08-06 21:42:58 +0300316 if (!last_stage) {
317 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
318 }
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300319
320 /* Send XBZRLE based compressed page */
321 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_XBZRLE);
322 qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
323 qemu_put_be16(f, encoded_len);
324 qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
325 bytes_sent = encoded_len + 1 + 2;
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300326 acct_info.xbzrle_pages++;
327 acct_info.xbzrle_bytes += bytes_sent;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300328
329 return bytes_sent;
330}
331
Alex Williamson760e77e2010-08-19 10:18:42 -0300332static RAMBlock *last_block;
333static ram_addr_t last_offset;
334
Juan Quintela69268cd2012-07-20 10:36:12 +0200335static inline bool migration_bitmap_test_and_reset_dirty(MemoryRegion *mr,
336 ram_addr_t offset)
337{
338 bool ret = memory_region_get_dirty(mr, offset, TARGET_PAGE_SIZE,
339 DIRTY_MEMORY_MIGRATION);
340
341 if (ret) {
342 memory_region_reset_dirty(mr, offset, TARGET_PAGE_SIZE,
343 DIRTY_MEMORY_MIGRATION);
344 }
345 return ret;
346}
347
Juan Quintelae44d26c2012-07-20 10:16:08 +0200348static inline void migration_bitmap_set_dirty(MemoryRegion *mr, int length)
349{
350 ram_addr_t addr;
351
352 for (addr = 0; addr < length; addr += TARGET_PAGE_SIZE) {
353 if (!memory_region_get_dirty(mr, addr, TARGET_PAGE_SIZE,
354 DIRTY_MEMORY_MIGRATION)) {
355 memory_region_set_dirty(mr, addr, TARGET_PAGE_SIZE);
356 }
357 }
358}
359
Juan Quinteladd2df732012-07-20 10:52:51 +0200360static void migration_bitmap_sync(void)
361{
Juan Quintela3c121932012-09-04 13:08:57 +0200362 uint64_t num_dirty_pages_init = ram_list.dirty_pages;
363
364 trace_migration_bitmap_sync_start();
Juan Quinteladd2df732012-07-20 10:52:51 +0200365 memory_global_sync_dirty_bitmap(get_system_memory());
Juan Quintela3c121932012-09-04 13:08:57 +0200366 trace_migration_bitmap_sync_end(ram_list.dirty_pages
367 - num_dirty_pages_init);
Juan Quinteladd2df732012-07-20 10:52:51 +0200368}
369
370
Orit Wasserman6c779f22012-07-10 12:37:13 +0300371/*
372 * ram_save_block: Writes a page of memory to the stream f
373 *
374 * Returns: 0: if the page hasn't changed
375 * -1: if there are no more dirty pages
376 * n: the amount of bytes written in other case
377 */
378
Juan Quinteladd051c72012-08-06 21:42:58 +0300379static int ram_save_block(QEMUFile *f, bool last_stage)
Blue Swirlad960902010-03-29 19:23:52 +0000380{
Alex Williamsone44359c2010-06-25 11:09:57 -0600381 RAMBlock *block = last_block;
382 ram_addr_t offset = last_offset;
Orit Wasserman6c779f22012-07-10 12:37:13 +0300383 int bytes_sent = -1;
Avi Kivity71c510e2011-12-21 13:11:22 +0200384 MemoryRegion *mr;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300385 ram_addr_t current_addr;
Blue Swirlad960902010-03-29 19:23:52 +0000386
Alex Williamsone44359c2010-06-25 11:09:57 -0600387 if (!block)
388 block = QLIST_FIRST(&ram_list.blocks);
389
Alex Williamsone44359c2010-06-25 11:09:57 -0600390 do {
Avi Kivity71c510e2011-12-21 13:11:22 +0200391 mr = block->mr;
Juan Quintela69268cd2012-07-20 10:36:12 +0200392 if (migration_bitmap_test_and_reset_dirty(mr, offset)) {
Blue Swirlad960902010-03-29 19:23:52 +0000393 uint8_t *p;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600394 int cont = (block == last_block) ? RAM_SAVE_FLAG_CONTINUE : 0;
Blue Swirlad960902010-03-29 19:23:52 +0000395
Avi Kivity71c510e2011-12-21 13:11:22 +0200396 p = memory_region_get_ram_ptr(mr) + offset;
Blue Swirlad960902010-03-29 19:23:52 +0000397
Paolo Bonzini86003612011-12-23 16:17:26 +0100398 if (is_dup_page(p)) {
Orit Wasserman004d4c12012-08-06 21:42:56 +0300399 acct_info.dup_pages++;
Orit Wasserman0c51f432012-06-19 18:43:14 +0300400 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_COMPRESS);
Blue Swirlad960902010-03-29 19:23:52 +0000401 qemu_put_byte(f, *p);
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200402 bytes_sent = 1;
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300403 } else if (migrate_use_xbzrle()) {
404 current_addr = block->offset + offset;
405 bytes_sent = save_xbzrle_page(f, p, current_addr, block,
Juan Quinteladd051c72012-08-06 21:42:58 +0300406 offset, cont, last_stage);
407 if (!last_stage) {
408 p = get_cached_data(XBZRLE.cache, current_addr);
409 }
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300410 }
411
412 /* either we didn't send yet (we may have had XBZRLE overflow) */
413 if (bytes_sent == -1) {
Orit Wasserman0c51f432012-06-19 18:43:14 +0300414 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
Blue Swirlad960902010-03-29 19:23:52 +0000415 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200416 bytes_sent = TARGET_PAGE_SIZE;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300417 acct_info.norm_pages++;
Blue Swirlad960902010-03-29 19:23:52 +0000418 }
419
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300420 /* if page is unmodified, continue to the next */
421 if (bytes_sent != 0) {
422 break;
423 }
Blue Swirlad960902010-03-29 19:23:52 +0000424 }
Alex Williamsone44359c2010-06-25 11:09:57 -0600425
426 offset += TARGET_PAGE_SIZE;
427 if (offset >= block->length) {
428 offset = 0;
429 block = QLIST_NEXT(block, next);
430 if (!block)
431 block = QLIST_FIRST(&ram_list.blocks);
432 }
Avi Kivity71c510e2011-12-21 13:11:22 +0200433 } while (block != last_block || offset != last_offset);
Alex Williamsone44359c2010-06-25 11:09:57 -0600434
435 last_block = block;
436 last_offset = offset;
Blue Swirlad960902010-03-29 19:23:52 +0000437
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200438 return bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000439}
440
441static uint64_t bytes_transferred;
442
443static ram_addr_t ram_save_remaining(void)
444{
Juan Quintela45f33f02012-06-22 15:21:07 +0200445 return ram_list.dirty_pages;
Blue Swirlad960902010-03-29 19:23:52 +0000446}
447
448uint64_t ram_bytes_remaining(void)
449{
450 return ram_save_remaining() * TARGET_PAGE_SIZE;
451}
452
453uint64_t ram_bytes_transferred(void)
454{
455 return bytes_transferred;
456}
457
458uint64_t ram_bytes_total(void)
459{
Alex Williamsond17b5282010-06-25 11:08:38 -0600460 RAMBlock *block;
461 uint64_t total = 0;
462
463 QLIST_FOREACH(block, &ram_list.blocks, next)
464 total += block->length;
465
466 return total;
Blue Swirlad960902010-03-29 19:23:52 +0000467}
468
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200469static int block_compar(const void *a, const void *b)
470{
471 RAMBlock * const *ablock = a;
472 RAMBlock * const *bblock = b;
Avi Kivity8fec98b2011-12-21 13:22:16 +0200473
474 return strcmp((*ablock)->idstr, (*bblock)->idstr);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200475}
476
477static void sort_ram_list(void)
478{
479 RAMBlock *block, *nblock, **blocks;
480 int n;
481 n = 0;
482 QLIST_FOREACH(block, &ram_list.blocks, next) {
483 ++n;
484 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500485 blocks = g_malloc(n * sizeof *blocks);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200486 n = 0;
487 QLIST_FOREACH_SAFE(block, &ram_list.blocks, next, nblock) {
488 blocks[n++] = block;
489 QLIST_REMOVE(block, next);
490 }
491 qsort(blocks, n, sizeof *blocks, block_compar);
492 while (--n >= 0) {
493 QLIST_INSERT_HEAD(&ram_list.blocks, blocks[n], next);
494 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500495 g_free(blocks);
Michael S. Tsirkinb2e0a132010-11-22 19:52:34 +0200496}
497
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300498static void migration_end(void)
499{
500 memory_global_dirty_log_stop();
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300501
502 if (migrate_use_xbzrle()) {
503 cache_fini(XBZRLE.cache);
504 g_free(XBZRLE.cache);
505 g_free(XBZRLE.encoded_buf);
506 g_free(XBZRLE.current_buf);
507 g_free(XBZRLE.decoded_buf);
508 XBZRLE.cache = NULL;
509 }
Orit Wasserman8e21cd32012-06-19 18:43:17 +0300510}
511
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200512static void ram_migration_cancel(void *opaque)
513{
514 migration_end();
515}
516
Juan Quintela5a170772012-07-17 17:02:24 +0200517
518static void reset_ram_globals(void)
519{
520 last_block = NULL;
521 last_offset = 0;
522 sort_ram_list();
523}
524
Juan Quintela4508bd92012-05-22 16:27:59 +0200525#define MAX_WAIT 50 /* ms, half buffered_file limit */
526
Juan Quintelad1315aa2012-06-28 15:11:57 +0200527static int ram_save_setup(QEMUFile *f, void *opaque)
Blue Swirlad960902010-03-29 19:23:52 +0000528{
Juan Quintelad1315aa2012-06-28 15:11:57 +0200529 RAMBlock *block;
Blue Swirlad960902010-03-29 19:23:52 +0000530
Juan Quintelad1315aa2012-06-28 15:11:57 +0200531 bytes_transferred = 0;
Juan Quintela5a170772012-07-17 17:02:24 +0200532 reset_ram_globals();
Blue Swirlad960902010-03-29 19:23:52 +0000533
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300534 if (migrate_use_xbzrle()) {
535 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
536 TARGET_PAGE_SIZE,
537 TARGET_PAGE_SIZE);
538 if (!XBZRLE.cache) {
539 DPRINTF("Error creating cache\n");
540 return -1;
541 }
542 XBZRLE.encoded_buf = g_malloc0(TARGET_PAGE_SIZE);
543 XBZRLE.current_buf = g_malloc(TARGET_PAGE_SIZE);
Orit Wasserman004d4c12012-08-06 21:42:56 +0300544 acct_clear();
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300545 }
546
Juan Quintelad1315aa2012-06-28 15:11:57 +0200547 /* Make sure all dirty bits are set */
548 QLIST_FOREACH(block, &ram_list.blocks, next) {
Juan Quintelae44d26c2012-07-20 10:16:08 +0200549 migration_bitmap_set_dirty(block->mr, block->length);
Blue Swirlad960902010-03-29 19:23:52 +0000550 }
551
Juan Quintelad1315aa2012-06-28 15:11:57 +0200552 memory_global_dirty_log_start();
Juan Quintela79536f42012-08-03 12:58:16 +0200553 memory_global_sync_dirty_bitmap(get_system_memory());
Juan Quintelad1315aa2012-06-28 15:11:57 +0200554
555 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
556
557 QLIST_FOREACH(block, &ram_list.blocks, next) {
558 qemu_put_byte(f, strlen(block->idstr));
559 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
560 qemu_put_be64(f, block->length);
561 }
562
Juan Quintelad1315aa2012-06-28 15:11:57 +0200563 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
564
565 return 0;
566}
567
Juan Quintela16310a32012-06-28 15:31:37 +0200568static int ram_save_iterate(QEMUFile *f, void *opaque)
Juan Quintelad1315aa2012-06-28 15:11:57 +0200569{
Blue Swirlad960902010-03-29 19:23:52 +0000570 uint64_t bytes_transferred_last;
571 double bwidth = 0;
572 int ret;
573 int i;
Juan Quintelac00012f2012-08-13 09:36:36 +0200574 uint64_t expected_downtime;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200575 MigrationState *s = migrate_get_current();
Blue Swirlad960902010-03-29 19:23:52 +0000576
577 bytes_transferred_last = bytes_transferred;
578 bwidth = qemu_get_clock_ns(rt_clock);
579
Juan Quintela4508bd92012-05-22 16:27:59 +0200580 i = 0;
Juan Quintela29757252011-10-19 15:22:18 +0200581 while ((ret = qemu_file_rate_limit(f)) == 0) {
Pierre Riteau3fc250b2010-05-12 15:12:44 +0200582 int bytes_sent;
Blue Swirlad960902010-03-29 19:23:52 +0000583
Juan Quinteladd051c72012-08-06 21:42:58 +0300584 bytes_sent = ram_save_block(f, false);
Orit Wasserman6c779f22012-07-10 12:37:13 +0300585 /* no more blocks to sent */
586 if (bytes_sent < 0) {
Blue Swirlad960902010-03-29 19:23:52 +0000587 break;
588 }
Orit Wasserman6c779f22012-07-10 12:37:13 +0300589 bytes_transferred += bytes_sent;
Orit Wasserman004d4c12012-08-06 21:42:56 +0300590 acct_info.iterations++;
Juan Quintela4508bd92012-05-22 16:27:59 +0200591 /* we want to check in the 1st loop, just in case it was the 1st time
592 and we had to sync the dirty bitmap.
593 qemu_get_clock_ns() is a bit expensive, so we only check each some
594 iterations
595 */
596 if ((i & 63) == 0) {
597 uint64_t t1 = (qemu_get_clock_ns(rt_clock) - bwidth) / 1000000;
598 if (t1 > MAX_WAIT) {
Igor Mitsyankoef37a692012-09-05 13:04:56 +0400599 DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n",
Juan Quintela4508bd92012-05-22 16:27:59 +0200600 t1, i);
601 break;
602 }
603 }
604 i++;
Blue Swirlad960902010-03-29 19:23:52 +0000605 }
606
Juan Quintela29757252011-10-19 15:22:18 +0200607 if (ret < 0) {
608 return ret;
609 }
610
Blue Swirlad960902010-03-29 19:23:52 +0000611 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
612 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
613
Juan Quintelac00012f2012-08-13 09:36:36 +0200614 /* if we haven't transferred anything this round, force
615 * expected_downtime to a very high value, but without
616 * crashing */
Blue Swirlad960902010-03-29 19:23:52 +0000617 if (bwidth == 0) {
618 bwidth = 0.000001;
619 }
620
Blue Swirlad960902010-03-29 19:23:52 +0000621 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
622
Juan Quintelac00012f2012-08-13 09:36:36 +0200623 expected_downtime = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
624 DPRINTF("ram_save_live: expected(%" PRIu64 ") <= max(" PRIu64 ")?\n",
625 expected_downtime, migrate_max_downtime());
Blue Swirlad960902010-03-29 19:23:52 +0000626
Juan Quintelac00012f2012-08-13 09:36:36 +0200627 if (expected_downtime <= migrate_max_downtime()) {
Juan Quinteladd2df732012-07-20 10:52:51 +0200628 migration_bitmap_sync();
Juan Quintelac00012f2012-08-13 09:36:36 +0200629 expected_downtime = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200630 s->expected_downtime = expected_downtime / 1000000; /* ns -> ms */
Orit Wasserman3a697f62012-06-19 18:43:15 +0300631
Juan Quintelac00012f2012-08-13 09:36:36 +0200632 return expected_downtime <= migrate_max_downtime();
Juan Quintela5b3c9632012-05-22 00:44:24 +0200633 }
634 return 0;
Blue Swirlad960902010-03-29 19:23:52 +0000635}
636
Juan Quintela16310a32012-06-28 15:31:37 +0200637static int ram_save_complete(QEMUFile *f, void *opaque)
638{
Juan Quinteladd2df732012-07-20 10:52:51 +0200639 migration_bitmap_sync();
Juan Quintela16310a32012-06-28 15:31:37 +0200640
Juan Quintela16310a32012-06-28 15:31:37 +0200641 /* try transferring iterative blocks of memory */
642
643 /* flush all remaining blocks regardless of rate limiting */
Orit Wasserman6c779f22012-07-10 12:37:13 +0300644 while (true) {
645 int bytes_sent;
646
Juan Quinteladd051c72012-08-06 21:42:58 +0300647 bytes_sent = ram_save_block(f, true);
Orit Wasserman6c779f22012-07-10 12:37:13 +0300648 /* no more blocks to sent */
649 if (bytes_sent < 0) {
650 break;
651 }
Juan Quintela16310a32012-06-28 15:31:37 +0200652 bytes_transferred += bytes_sent;
653 }
654 memory_global_dirty_log_stop();
655
Blue Swirlad960902010-03-29 19:23:52 +0000656 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
657
Alex Williamsona55bbe32010-06-25 11:10:05 -0600658 return 0;
659}
660
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300661static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
662{
663 int ret, rc = 0;
664 unsigned int xh_len;
665 int xh_flags;
666
667 if (!XBZRLE.decoded_buf) {
668 XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE);
669 }
670
671 /* extract RLE header */
672 xh_flags = qemu_get_byte(f);
673 xh_len = qemu_get_be16(f);
674
675 if (xh_flags != ENCODING_FLAG_XBZRLE) {
676 fprintf(stderr, "Failed to load XBZRLE page - wrong compression!\n");
677 return -1;
678 }
679
680 if (xh_len > TARGET_PAGE_SIZE) {
681 fprintf(stderr, "Failed to load XBZRLE page - len overflow!\n");
682 return -1;
683 }
684 /* load data and decode */
685 qemu_get_buffer(f, XBZRLE.decoded_buf, xh_len);
686
687 /* decode RLE */
688 ret = xbzrle_decode_buffer(XBZRLE.decoded_buf, xh_len, host,
689 TARGET_PAGE_SIZE);
690 if (ret == -1) {
691 fprintf(stderr, "Failed to load XBZRLE page - decode error!\n");
692 rc = -1;
693 } else if (ret > TARGET_PAGE_SIZE) {
694 fprintf(stderr, "Failed to load XBZRLE page - size %d exceeds %d!\n",
695 ret, TARGET_PAGE_SIZE);
696 abort();
697 }
698
699 return rc;
700}
701
Alex Williamsona55bbe32010-06-25 11:10:05 -0600702static inline void *host_from_stream_offset(QEMUFile *f,
703 ram_addr_t offset,
704 int flags)
705{
706 static RAMBlock *block = NULL;
707 char id[256];
708 uint8_t len;
709
710 if (flags & RAM_SAVE_FLAG_CONTINUE) {
711 if (!block) {
712 fprintf(stderr, "Ack, bad migration stream!\n");
713 return NULL;
714 }
715
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200716 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600717 }
718
719 len = qemu_get_byte(f);
720 qemu_get_buffer(f, (uint8_t *)id, len);
721 id[len] = 0;
722
723 QLIST_FOREACH(block, &ram_list.blocks, next) {
724 if (!strncmp(id, block->idstr, sizeof(id)))
Avi Kivitydc94a7e2011-12-21 13:54:33 +0200725 return memory_region_get_ram_ptr(block->mr) + offset;
Alex Williamsona55bbe32010-06-25 11:10:05 -0600726 }
727
728 fprintf(stderr, "Can't find block %s!\n", id);
729 return NULL;
730}
731
Juan Quintela7908c782012-06-26 18:46:10 +0200732static int ram_load(QEMUFile *f, void *opaque, int version_id)
Blue Swirlad960902010-03-29 19:23:52 +0000733{
734 ram_addr_t addr;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300735 int flags, ret = 0;
Juan Quintela42802d42011-10-05 01:14:46 +0200736 int error;
Orit Wasserman3a697f62012-06-19 18:43:15 +0300737 static uint64_t seq_iter;
738
739 seq_iter++;
Blue Swirlad960902010-03-29 19:23:52 +0000740
Avi Kivityf09f2182011-12-21 13:37:56 +0200741 if (version_id < 4 || version_id > 4) {
Blue Swirlad960902010-03-29 19:23:52 +0000742 return -EINVAL;
743 }
744
745 do {
746 addr = qemu_get_be64(f);
747
748 flags = addr & ~TARGET_PAGE_MASK;
749 addr &= TARGET_PAGE_MASK;
750
751 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
Avi Kivityf09f2182011-12-21 13:37:56 +0200752 if (version_id == 4) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600753 /* Synchronize RAM block list */
754 char id[256];
755 ram_addr_t length;
756 ram_addr_t total_ram_bytes = addr;
757
758 while (total_ram_bytes) {
759 RAMBlock *block;
760 uint8_t len;
761
762 len = qemu_get_byte(f);
763 qemu_get_buffer(f, (uint8_t *)id, len);
764 id[len] = 0;
765 length = qemu_get_be64(f);
766
767 QLIST_FOREACH(block, &ram_list.blocks, next) {
768 if (!strncmp(id, block->idstr, sizeof(id))) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300769 if (block->length != length) {
770 ret = -EINVAL;
771 goto done;
772 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600773 break;
774 }
775 }
776
777 if (!block) {
Alex Williamsonfb787f82010-07-02 11:13:29 -0600778 fprintf(stderr, "Unknown ramblock \"%s\", cannot "
779 "accept migration\n", id);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300780 ret = -EINVAL;
781 goto done;
Alex Williamson97ab12d2010-06-25 11:09:50 -0600782 }
783
784 total_ram_bytes -= length;
785 }
Blue Swirlad960902010-03-29 19:23:52 +0000786 }
787 }
788
789 if (flags & RAM_SAVE_FLAG_COMPRESS) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600790 void *host;
791 uint8_t ch;
792
Avi Kivityf09f2182011-12-21 13:37:56 +0200793 host = host_from_stream_offset(f, addr, flags);
Michael S. Tsirkin492fb992010-10-17 20:43:40 +0200794 if (!host) {
795 return -EINVAL;
796 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600797
Alex Williamson97ab12d2010-06-25 11:09:50 -0600798 ch = qemu_get_byte(f);
799 memset(host, ch, TARGET_PAGE_SIZE);
Blue Swirlad960902010-03-29 19:23:52 +0000800#ifndef _WIN32
801 if (ch == 0 &&
802 (!kvm_enabled() || kvm_has_sync_mmu())) {
Andreas Färbere78815a2010-09-25 11:26:05 +0000803 qemu_madvise(host, TARGET_PAGE_SIZE, QEMU_MADV_DONTNEED);
Blue Swirlad960902010-03-29 19:23:52 +0000804 }
805#endif
806 } else if (flags & RAM_SAVE_FLAG_PAGE) {
Alex Williamson97ab12d2010-06-25 11:09:50 -0600807 void *host;
808
Avi Kivityf09f2182011-12-21 13:37:56 +0200809 host = host_from_stream_offset(f, addr, flags);
Orit Wasserman0ff1f9f2012-06-19 11:51:37 +0300810 if (!host) {
811 return -EINVAL;
812 }
Alex Williamson97ab12d2010-06-25 11:09:50 -0600813
Alex Williamson97ab12d2010-06-25 11:09:50 -0600814 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
Orit Wasserman17ad9b32012-08-06 21:42:53 +0300815 } else if (flags & RAM_SAVE_FLAG_XBZRLE) {
816 if (!migrate_use_xbzrle()) {
817 return -EINVAL;
818 }
819 void *host = host_from_stream_offset(f, addr, flags);
820 if (!host) {
821 return -EINVAL;
822 }
823
824 if (load_xbzrle(f, addr, host) < 0) {
825 ret = -EINVAL;
826 goto done;
827 }
Blue Swirlad960902010-03-29 19:23:52 +0000828 }
Juan Quintela42802d42011-10-05 01:14:46 +0200829 error = qemu_file_get_error(f);
830 if (error) {
Orit Wasserman3a697f62012-06-19 18:43:15 +0300831 ret = error;
832 goto done;
Blue Swirlad960902010-03-29 19:23:52 +0000833 }
834 } while (!(flags & RAM_SAVE_FLAG_EOS));
835
Orit Wasserman3a697f62012-06-19 18:43:15 +0300836done:
Igor Mitsyankoef37a692012-09-05 13:04:56 +0400837 DPRINTF("Completed load of VM with exit code %d seq iteration "
838 "%" PRIu64 "\n", ret, seq_iter);
Orit Wasserman3a697f62012-06-19 18:43:15 +0300839 return ret;
Blue Swirlad960902010-03-29 19:23:52 +0000840}
841
Juan Quintela7908c782012-06-26 18:46:10 +0200842SaveVMHandlers savevm_ram_handlers = {
Juan Quintelad1315aa2012-06-28 15:11:57 +0200843 .save_live_setup = ram_save_setup,
Juan Quintela16310a32012-06-28 15:31:37 +0200844 .save_live_iterate = ram_save_iterate,
845 .save_live_complete = ram_save_complete,
Juan Quintela7908c782012-06-26 18:46:10 +0200846 .load_state = ram_load,
Juan Quintela9b5bfab2012-06-26 19:26:41 +0200847 .cancel = ram_migration_cancel,
Juan Quintela7908c782012-06-26 18:46:10 +0200848};
849
Blue Swirlad960902010-03-29 19:23:52 +0000850#ifdef HAS_AUDIO
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900851struct soundhw {
852 const char *name;
853 const char *descr;
854 int enabled;
855 int isa;
856 union {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100857 int (*init_isa) (ISABus *bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +0900858 int (*init_pci) (PCIBus *bus);
859 } init;
860};
861
862static struct soundhw soundhw[] = {
Blue Swirlad960902010-03-29 19:23:52 +0000863#ifdef HAS_AUDIO_CHOICE
Hervé Poussineauda128722012-04-14 22:51:33 +0200864#ifdef CONFIG_PCSPK
Blue Swirlad960902010-03-29 19:23:52 +0000865 {
866 "pcspk",
867 "PC speaker",
868 0,
869 1,
870 { .init_isa = pcspk_audio_init }
871 },
872#endif
873
874#ifdef CONFIG_SB16
875 {
876 "sb16",
877 "Creative Sound Blaster 16",
878 0,
879 1,
880 { .init_isa = SB16_init }
881 },
882#endif
883
884#ifdef CONFIG_CS4231A
885 {
886 "cs4231a",
887 "CS4231A",
888 0,
889 1,
890 { .init_isa = cs4231a_init }
891 },
892#endif
893
894#ifdef CONFIG_ADLIB
895 {
896 "adlib",
897#ifdef HAS_YMF262
898 "Yamaha YMF262 (OPL3)",
899#else
900 "Yamaha YM3812 (OPL2)",
901#endif
902 0,
903 1,
904 { .init_isa = Adlib_init }
905 },
906#endif
907
908#ifdef CONFIG_GUS
909 {
910 "gus",
911 "Gravis Ultrasound GF1",
912 0,
913 1,
914 { .init_isa = GUS_init }
915 },
916#endif
917
918#ifdef CONFIG_AC97
919 {
920 "ac97",
921 "Intel 82801AA AC97 Audio",
922 0,
923 0,
924 { .init_pci = ac97_init }
925 },
926#endif
927
928#ifdef CONFIG_ES1370
929 {
930 "es1370",
931 "ENSONIQ AudioPCI ES1370",
932 0,
933 0,
934 { .init_pci = es1370_init }
935 },
936#endif
937
Gerd Hoffmannd61a4ce2010-11-01 13:05:32 +0100938#ifdef CONFIG_HDA
939 {
940 "hda",
941 "Intel HD Audio",
942 0,
943 0,
944 { .init_pci = intel_hda_and_codec_init }
945 },
946#endif
947
Blue Swirlad960902010-03-29 19:23:52 +0000948#endif /* HAS_AUDIO_CHOICE */
949
950 { NULL, NULL, 0, 0, { NULL } }
951};
952
953void select_soundhw(const char *optarg)
954{
955 struct soundhw *c;
956
Peter Maydellc8057f92012-08-02 13:45:54 +0100957 if (is_help_option(optarg)) {
Blue Swirlad960902010-03-29 19:23:52 +0000958 show_valid_cards:
959
Peter Maydell55d4fd32012-09-19 14:51:38 +0100960#ifdef HAS_AUDIO_CHOICE
Blue Swirlad960902010-03-29 19:23:52 +0000961 printf("Valid sound card names (comma separated):\n");
962 for (c = soundhw; c->name; ++c) {
963 printf ("%-11s %s\n", c->name, c->descr);
964 }
965 printf("\n-soundhw all will enable all of the above\n");
Peter Maydell55d4fd32012-09-19 14:51:38 +0100966#else
967 printf("Machine has no user-selectable audio hardware "
968 "(it may or may not have always-present audio hardware).\n");
969#endif
Peter Maydellc8057f92012-08-02 13:45:54 +0100970 exit(!is_help_option(optarg));
Blue Swirlad960902010-03-29 19:23:52 +0000971 }
972 else {
973 size_t l;
974 const char *p;
975 char *e;
976 int bad_card = 0;
977
978 if (!strcmp(optarg, "all")) {
979 for (c = soundhw; c->name; ++c) {
980 c->enabled = 1;
981 }
982 return;
983 }
984
985 p = optarg;
986 while (*p) {
987 e = strchr(p, ',');
988 l = !e ? strlen(p) : (size_t) (e - p);
989
990 for (c = soundhw; c->name; ++c) {
991 if (!strncmp(c->name, p, l) && !c->name[l]) {
992 c->enabled = 1;
993 break;
994 }
995 }
996
997 if (!c->name) {
998 if (l > 80) {
999 fprintf(stderr,
1000 "Unknown sound card name (too big to show)\n");
1001 }
1002 else {
1003 fprintf(stderr, "Unknown sound card name `%.*s'\n",
1004 (int) l, p);
1005 }
1006 bad_card = 1;
1007 }
1008 p += l + (e != NULL);
1009 }
1010
1011 if (bad_card) {
1012 goto show_valid_cards;
1013 }
1014 }
1015}
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001016
Hervé Poussineau4a0f0312011-12-15 22:10:01 +01001017void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001018{
1019 struct soundhw *c;
1020
1021 for (c = soundhw; c->name; ++c) {
1022 if (c->enabled) {
1023 if (c->isa) {
Hervé Poussineau4a0f0312011-12-15 22:10:01 +01001024 if (isa_bus) {
1025 c->init.init_isa(isa_bus);
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001026 }
1027 } else {
1028 if (pci_bus) {
1029 c->init.init_pci(pci_bus);
1030 }
1031 }
1032 }
1033 }
1034}
Blue Swirlad960902010-03-29 19:23:52 +00001035#else
1036void select_soundhw(const char *optarg)
1037{
1038}
Hervé Poussineau4a0f0312011-12-15 22:10:01 +01001039void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
Isaku Yamahata0dfa5ef2011-01-21 19:53:45 +09001040{
1041}
Blue Swirlad960902010-03-29 19:23:52 +00001042#endif
1043
1044int qemu_uuid_parse(const char *str, uint8_t *uuid)
1045{
1046 int ret;
1047
1048 if (strlen(str) != 36) {
1049 return -1;
1050 }
1051
1052 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
1053 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
1054 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
1055 &uuid[15]);
1056
1057 if (ret != 16) {
1058 return -1;
1059 }
1060#ifdef TARGET_I386
1061 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
1062#endif
1063 return 0;
1064}
1065
1066void do_acpitable_option(const char *optarg)
1067{
1068#ifdef TARGET_I386
1069 if (acpi_table_add(optarg) < 0) {
1070 fprintf(stderr, "Wrong acpi table provided\n");
1071 exit(1);
1072 }
1073#endif
1074}
1075
1076void do_smbios_option(const char *optarg)
1077{
1078#ifdef TARGET_I386
1079 if (smbios_entry_add(optarg) < 0) {
1080 fprintf(stderr, "Wrong smbios provided\n");
1081 exit(1);
1082 }
1083#endif
1084}
1085
1086void cpudef_init(void)
1087{
1088#if defined(cpudef_setup)
1089 cpudef_setup(); /* parse cpu definitions in target config file */
1090#endif
1091}
1092
1093int audio_available(void)
1094{
1095#ifdef HAS_AUDIO
1096 return 1;
1097#else
1098 return 0;
1099#endif
1100}
1101
Anthony PERARD303d4e82010-09-21 20:05:31 +01001102int tcg_available(void)
1103{
1104 return 1;
1105}
1106
Blue Swirlad960902010-03-29 19:23:52 +00001107int kvm_available(void)
1108{
1109#ifdef CONFIG_KVM
1110 return 1;
1111#else
1112 return 0;
1113#endif
1114}
1115
1116int xen_available(void)
1117{
1118#ifdef CONFIG_XEN
1119 return 1;
1120#else
1121 return 0;
1122#endif
1123}
Daniel P. Berrange99afc912012-08-20 15:31:38 +01001124
1125
1126TargetInfo *qmp_query_target(Error **errp)
1127{
1128 TargetInfo *info = g_malloc0(sizeof(*info));
1129
1130 info->arch = TARGET_TYPE;
1131
1132 return info;
1133}