]> Git Repo - qemu.git/blob - arch_init.c
migration: rename expected_time to expected_downtime
[qemu.git] / arch_init.c
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include <stdint.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #ifndef _WIN32
28 #include <sys/types.h>
29 #include <sys/mman.h>
30 #endif
31 #include "config.h"
32 #include "monitor.h"
33 #include "sysemu.h"
34 #include "arch_init.h"
35 #include "audio/audio.h"
36 #include "hw/pc.h"
37 #include "hw/pci.h"
38 #include "hw/audiodev.h"
39 #include "kvm.h"
40 #include "migration.h"
41 #include "net.h"
42 #include "gdbstub.h"
43 #include "hw/smbios.h"
44 #include "exec-memory.h"
45 #include "hw/pcspk.h"
46 #include "qemu/page_cache.h"
47 #include "qmp-commands.h"
48
49 #ifdef DEBUG_ARCH_INIT
50 #define DPRINTF(fmt, ...) \
51     do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
52 #else
53 #define DPRINTF(fmt, ...) \
54     do { } while (0)
55 #endif
56
57 #ifdef TARGET_SPARC
58 int graphic_width = 1024;
59 int graphic_height = 768;
60 int graphic_depth = 8;
61 #else
62 int graphic_width = 800;
63 int graphic_height = 600;
64 int graphic_depth = 15;
65 #endif
66
67
68 #if defined(TARGET_ALPHA)
69 #define QEMU_ARCH QEMU_ARCH_ALPHA
70 #elif defined(TARGET_ARM)
71 #define QEMU_ARCH QEMU_ARCH_ARM
72 #elif defined(TARGET_CRIS)
73 #define QEMU_ARCH QEMU_ARCH_CRIS
74 #elif defined(TARGET_I386)
75 #define QEMU_ARCH QEMU_ARCH_I386
76 #elif defined(TARGET_M68K)
77 #define QEMU_ARCH QEMU_ARCH_M68K
78 #elif defined(TARGET_LM32)
79 #define QEMU_ARCH QEMU_ARCH_LM32
80 #elif defined(TARGET_MICROBLAZE)
81 #define QEMU_ARCH QEMU_ARCH_MICROBLAZE
82 #elif defined(TARGET_MIPS)
83 #define QEMU_ARCH QEMU_ARCH_MIPS
84 #elif defined(TARGET_OPENRISC)
85 #define QEMU_ARCH QEMU_ARCH_OPENRISC
86 #elif defined(TARGET_PPC)
87 #define QEMU_ARCH QEMU_ARCH_PPC
88 #elif defined(TARGET_S390X)
89 #define QEMU_ARCH QEMU_ARCH_S390X
90 #elif defined(TARGET_SH4)
91 #define QEMU_ARCH QEMU_ARCH_SH4
92 #elif defined(TARGET_SPARC)
93 #define QEMU_ARCH QEMU_ARCH_SPARC
94 #elif defined(TARGET_XTENSA)
95 #define QEMU_ARCH QEMU_ARCH_XTENSA
96 #elif defined(TARGET_UNICORE32)
97 #define QEMU_ARCH QEMU_ARCH_UNICORE32
98 #endif
99
100 const uint32_t arch_type = QEMU_ARCH;
101
102 /***********************************************************/
103 /* ram save/restore */
104
105 #define RAM_SAVE_FLAG_FULL     0x01 /* Obsolete, not used anymore */
106 #define RAM_SAVE_FLAG_COMPRESS 0x02
107 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
108 #define RAM_SAVE_FLAG_PAGE     0x08
109 #define RAM_SAVE_FLAG_EOS      0x10
110 #define RAM_SAVE_FLAG_CONTINUE 0x20
111 #define RAM_SAVE_FLAG_XBZRLE   0x40
112
113 #ifdef __ALTIVEC__
114 #include <altivec.h>
115 #define VECTYPE        vector unsigned char
116 #define SPLAT(p)       vec_splat(vec_ld(0, p), 0)
117 #define ALL_EQ(v1, v2) vec_all_eq(v1, v2)
118 /* altivec.h may redefine the bool macro as vector type.
119  * Reset it to POSIX semantics. */
120 #undef bool
121 #define bool _Bool
122 #elif defined __SSE2__
123 #include <emmintrin.h>
124 #define VECTYPE        __m128i
125 #define SPLAT(p)       _mm_set1_epi8(*(p))
126 #define ALL_EQ(v1, v2) (_mm_movemask_epi8(_mm_cmpeq_epi8(v1, v2)) == 0xFFFF)
127 #else
128 #define VECTYPE        unsigned long
129 #define SPLAT(p)       (*(p) * (~0UL / 255))
130 #define ALL_EQ(v1, v2) ((v1) == (v2))
131 #endif
132
133
134 static struct defconfig_file {
135     const char *filename;
136     /* Indicates it is an user config file (disabled by -no-user-config) */
137     bool userconfig;
138 } default_config_files[] = {
139     { CONFIG_QEMU_CONFDIR "/qemu.conf",                   true },
140     { CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf", true },
141     { NULL }, /* end of list */
142 };
143
144
145 int qemu_read_default_config_files(bool userconfig)
146 {
147     int ret;
148     struct defconfig_file *f;
149
150     for (f = default_config_files; f->filename; f++) {
151         if (!userconfig && f->userconfig) {
152             continue;
153         }
154         ret = qemu_read_config_file(f->filename);
155         if (ret < 0 && ret != -ENOENT) {
156             return ret;
157         }
158     }
159     
160     return 0;
161 }
162
163 static int is_dup_page(uint8_t *page)
164 {
165     VECTYPE *p = (VECTYPE *)page;
166     VECTYPE val = SPLAT(page);
167     int i;
168
169     for (i = 0; i < TARGET_PAGE_SIZE / sizeof(VECTYPE); i++) {
170         if (!ALL_EQ(val, p[i])) {
171             return 0;
172         }
173     }
174
175     return 1;
176 }
177
178 /* struct contains XBZRLE cache and a static page
179    used by the compression */
180 static struct {
181     /* buffer used for XBZRLE encoding */
182     uint8_t *encoded_buf;
183     /* buffer for storing page content */
184     uint8_t *current_buf;
185     /* buffer used for XBZRLE decoding */
186     uint8_t *decoded_buf;
187     /* Cache for XBZRLE */
188     PageCache *cache;
189 } XBZRLE = {
190     .encoded_buf = NULL,
191     .current_buf = NULL,
192     .decoded_buf = NULL,
193     .cache = NULL,
194 };
195
196
197 int64_t xbzrle_cache_resize(int64_t new_size)
198 {
199     if (XBZRLE.cache != NULL) {
200         return cache_resize(XBZRLE.cache, new_size / TARGET_PAGE_SIZE) *
201             TARGET_PAGE_SIZE;
202     }
203     return pow2floor(new_size);
204 }
205
206 /* accounting for migration statistics */
207 typedef struct AccountingInfo {
208     uint64_t dup_pages;
209     uint64_t norm_pages;
210     uint64_t iterations;
211     uint64_t xbzrle_bytes;
212     uint64_t xbzrle_pages;
213     uint64_t xbzrle_cache_miss;
214     uint64_t xbzrle_overflows;
215 } AccountingInfo;
216
217 static AccountingInfo acct_info;
218
219 static void acct_clear(void)
220 {
221     memset(&acct_info, 0, sizeof(acct_info));
222 }
223
224 uint64_t dup_mig_bytes_transferred(void)
225 {
226     return acct_info.dup_pages * TARGET_PAGE_SIZE;
227 }
228
229 uint64_t dup_mig_pages_transferred(void)
230 {
231     return acct_info.dup_pages;
232 }
233
234 uint64_t norm_mig_bytes_transferred(void)
235 {
236     return acct_info.norm_pages * TARGET_PAGE_SIZE;
237 }
238
239 uint64_t norm_mig_pages_transferred(void)
240 {
241     return acct_info.norm_pages;
242 }
243
244 uint64_t xbzrle_mig_bytes_transferred(void)
245 {
246     return acct_info.xbzrle_bytes;
247 }
248
249 uint64_t xbzrle_mig_pages_transferred(void)
250 {
251     return acct_info.xbzrle_pages;
252 }
253
254 uint64_t xbzrle_mig_pages_cache_miss(void)
255 {
256     return acct_info.xbzrle_cache_miss;
257 }
258
259 uint64_t xbzrle_mig_pages_overflow(void)
260 {
261     return acct_info.xbzrle_overflows;
262 }
263
264 static void save_block_hdr(QEMUFile *f, RAMBlock *block, ram_addr_t offset,
265         int cont, int flag)
266 {
267         qemu_put_be64(f, offset | cont | flag);
268         if (!cont) {
269                 qemu_put_byte(f, strlen(block->idstr));
270                 qemu_put_buffer(f, (uint8_t *)block->idstr,
271                                 strlen(block->idstr));
272         }
273
274 }
275
276 #define ENCODING_FLAG_XBZRLE 0x1
277
278 static int save_xbzrle_page(QEMUFile *f, uint8_t *current_data,
279                             ram_addr_t current_addr, RAMBlock *block,
280                             ram_addr_t offset, int cont, bool last_stage)
281 {
282     int encoded_len = 0, bytes_sent = -1;
283     uint8_t *prev_cached_page;
284
285     if (!cache_is_cached(XBZRLE.cache, current_addr)) {
286         if (!last_stage) {
287             cache_insert(XBZRLE.cache, current_addr,
288                          g_memdup(current_data, TARGET_PAGE_SIZE));
289         }
290         acct_info.xbzrle_cache_miss++;
291         return -1;
292     }
293
294     prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
295
296     /* save current buffer into memory */
297     memcpy(XBZRLE.current_buf, current_data, TARGET_PAGE_SIZE);
298
299     /* XBZRLE encoding (if there is no overflow) */
300     encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
301                                        TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
302                                        TARGET_PAGE_SIZE);
303     if (encoded_len == 0) {
304         DPRINTF("Skipping unmodified page\n");
305         return 0;
306     } else if (encoded_len == -1) {
307         DPRINTF("Overflow\n");
308         acct_info.xbzrle_overflows++;
309         /* update data in the cache */
310         memcpy(prev_cached_page, current_data, TARGET_PAGE_SIZE);
311         return -1;
312     }
313
314     /* we need to update the data in the cache, in order to get the same data */
315     if (!last_stage) {
316         memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
317     }
318
319     /* Send XBZRLE based compressed page */
320     save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_XBZRLE);
321     qemu_put_byte(f, ENCODING_FLAG_XBZRLE);
322     qemu_put_be16(f, encoded_len);
323     qemu_put_buffer(f, XBZRLE.encoded_buf, encoded_len);
324     bytes_sent = encoded_len + 1 + 2;
325     acct_info.xbzrle_pages++;
326     acct_info.xbzrle_bytes += bytes_sent;
327
328     return bytes_sent;
329 }
330
331 static RAMBlock *last_block;
332 static ram_addr_t last_offset;
333
334 /*
335  * ram_save_block: Writes a page of memory to the stream f
336  *
337  * Returns:  0: if the page hasn't changed
338  *          -1: if there are no more dirty pages
339  *           n: the amount of bytes written in other case
340  */
341
342 static int ram_save_block(QEMUFile *f, bool last_stage)
343 {
344     RAMBlock *block = last_block;
345     ram_addr_t offset = last_offset;
346     int bytes_sent = -1;
347     MemoryRegion *mr;
348     ram_addr_t current_addr;
349
350     if (!block)
351         block = QLIST_FIRST(&ram_list.blocks);
352
353     do {
354         mr = block->mr;
355         if (memory_region_get_dirty(mr, offset, TARGET_PAGE_SIZE,
356                                     DIRTY_MEMORY_MIGRATION)) {
357             uint8_t *p;
358             int cont = (block == last_block) ? RAM_SAVE_FLAG_CONTINUE : 0;
359
360             memory_region_reset_dirty(mr, offset, TARGET_PAGE_SIZE,
361                                       DIRTY_MEMORY_MIGRATION);
362
363             p = memory_region_get_ram_ptr(mr) + offset;
364
365             if (is_dup_page(p)) {
366                 acct_info.dup_pages++;
367                 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_COMPRESS);
368                 qemu_put_byte(f, *p);
369                 bytes_sent = 1;
370             } else if (migrate_use_xbzrle()) {
371                 current_addr = block->offset + offset;
372                 bytes_sent = save_xbzrle_page(f, p, current_addr, block,
373                                               offset, cont, last_stage);
374                 if (!last_stage) {
375                     p = get_cached_data(XBZRLE.cache, current_addr);
376                 }
377             }
378
379             /* either we didn't send yet (we may have had XBZRLE overflow) */
380             if (bytes_sent == -1) {
381                 save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);
382                 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
383                 bytes_sent = TARGET_PAGE_SIZE;
384                 acct_info.norm_pages++;
385             }
386
387             /* if page is unmodified, continue to the next */
388             if (bytes_sent != 0) {
389                 break;
390             }
391         }
392
393         offset += TARGET_PAGE_SIZE;
394         if (offset >= block->length) {
395             offset = 0;
396             block = QLIST_NEXT(block, next);
397             if (!block)
398                 block = QLIST_FIRST(&ram_list.blocks);
399         }
400     } while (block != last_block || offset != last_offset);
401
402     last_block = block;
403     last_offset = offset;
404
405     return bytes_sent;
406 }
407
408 static uint64_t bytes_transferred;
409
410 static ram_addr_t ram_save_remaining(void)
411 {
412     return ram_list.dirty_pages;
413 }
414
415 uint64_t ram_bytes_remaining(void)
416 {
417     return ram_save_remaining() * TARGET_PAGE_SIZE;
418 }
419
420 uint64_t ram_bytes_transferred(void)
421 {
422     return bytes_transferred;
423 }
424
425 uint64_t ram_bytes_total(void)
426 {
427     RAMBlock *block;
428     uint64_t total = 0;
429
430     QLIST_FOREACH(block, &ram_list.blocks, next)
431         total += block->length;
432
433     return total;
434 }
435
436 static int block_compar(const void *a, const void *b)
437 {
438     RAMBlock * const *ablock = a;
439     RAMBlock * const *bblock = b;
440
441     return strcmp((*ablock)->idstr, (*bblock)->idstr);
442 }
443
444 static void sort_ram_list(void)
445 {
446     RAMBlock *block, *nblock, **blocks;
447     int n;
448     n = 0;
449     QLIST_FOREACH(block, &ram_list.blocks, next) {
450         ++n;
451     }
452     blocks = g_malloc(n * sizeof *blocks);
453     n = 0;
454     QLIST_FOREACH_SAFE(block, &ram_list.blocks, next, nblock) {
455         blocks[n++] = block;
456         QLIST_REMOVE(block, next);
457     }
458     qsort(blocks, n, sizeof *blocks, block_compar);
459     while (--n >= 0) {
460         QLIST_INSERT_HEAD(&ram_list.blocks, blocks[n], next);
461     }
462     g_free(blocks);
463 }
464
465 static void migration_end(void)
466 {
467     memory_global_dirty_log_stop();
468
469     if (migrate_use_xbzrle()) {
470         cache_fini(XBZRLE.cache);
471         g_free(XBZRLE.cache);
472         g_free(XBZRLE.encoded_buf);
473         g_free(XBZRLE.current_buf);
474         g_free(XBZRLE.decoded_buf);
475         XBZRLE.cache = NULL;
476     }
477 }
478
479 static void ram_migration_cancel(void *opaque)
480 {
481     migration_end();
482 }
483
484 #define MAX_WAIT 50 /* ms, half buffered_file limit */
485
486 static int ram_save_setup(QEMUFile *f, void *opaque)
487 {
488     ram_addr_t addr;
489     RAMBlock *block;
490
491     bytes_transferred = 0;
492     last_block = NULL;
493     last_offset = 0;
494     sort_ram_list();
495
496     if (migrate_use_xbzrle()) {
497         XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
498                                   TARGET_PAGE_SIZE,
499                                   TARGET_PAGE_SIZE);
500         if (!XBZRLE.cache) {
501             DPRINTF("Error creating cache\n");
502             return -1;
503         }
504         XBZRLE.encoded_buf = g_malloc0(TARGET_PAGE_SIZE);
505         XBZRLE.current_buf = g_malloc(TARGET_PAGE_SIZE);
506         acct_clear();
507     }
508
509     /* Make sure all dirty bits are set */
510     QLIST_FOREACH(block, &ram_list.blocks, next) {
511         for (addr = 0; addr < block->length; addr += TARGET_PAGE_SIZE) {
512             if (!memory_region_get_dirty(block->mr, addr, TARGET_PAGE_SIZE,
513                                          DIRTY_MEMORY_MIGRATION)) {
514                 memory_region_set_dirty(block->mr, addr, TARGET_PAGE_SIZE);
515             }
516         }
517     }
518
519     memory_global_dirty_log_start();
520     memory_global_sync_dirty_bitmap(get_system_memory());
521
522     qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
523
524     QLIST_FOREACH(block, &ram_list.blocks, next) {
525         qemu_put_byte(f, strlen(block->idstr));
526         qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
527         qemu_put_be64(f, block->length);
528     }
529
530     qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
531
532     return 0;
533 }
534
535 static int ram_save_iterate(QEMUFile *f, void *opaque)
536 {
537     uint64_t bytes_transferred_last;
538     double bwidth = 0;
539     int ret;
540     int i;
541     uint64_t expected_downtime;
542
543     bytes_transferred_last = bytes_transferred;
544     bwidth = qemu_get_clock_ns(rt_clock);
545
546     i = 0;
547     while ((ret = qemu_file_rate_limit(f)) == 0) {
548         int bytes_sent;
549
550         bytes_sent = ram_save_block(f, false);
551         /* no more blocks to sent */
552         if (bytes_sent < 0) {
553             break;
554         }
555         bytes_transferred += bytes_sent;
556         acct_info.iterations++;
557         /* we want to check in the 1st loop, just in case it was the 1st time
558            and we had to sync the dirty bitmap.
559            qemu_get_clock_ns() is a bit expensive, so we only check each some
560            iterations
561         */
562         if ((i & 63) == 0) {
563             uint64_t t1 = (qemu_get_clock_ns(rt_clock) - bwidth) / 1000000;
564             if (t1 > MAX_WAIT) {
565                 DPRINTF("big wait: %" PRIu64 " milliseconds, %d iterations\n",
566                         t1, i);
567                 break;
568             }
569         }
570         i++;
571     }
572
573     if (ret < 0) {
574         return ret;
575     }
576
577     bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
578     bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
579
580     /* if we haven't transferred anything this round, force
581      * expected_downtime to a very high value, but without
582      * crashing */
583     if (bwidth == 0) {
584         bwidth = 0.000001;
585     }
586
587     qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
588
589     expected_downtime = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
590     DPRINTF("ram_save_live: expected(%" PRIu64 ") <= max(" PRIu64 ")?\n",
591             expected_downtime, migrate_max_downtime());
592
593     if (expected_downtime <= migrate_max_downtime()) {
594         memory_global_sync_dirty_bitmap(get_system_memory());
595         expected_downtime = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
596
597         return expected_downtime <= migrate_max_downtime();
598     }
599     return 0;
600 }
601
602 static int ram_save_complete(QEMUFile *f, void *opaque)
603 {
604     memory_global_sync_dirty_bitmap(get_system_memory());
605
606     /* try transferring iterative blocks of memory */
607
608     /* flush all remaining blocks regardless of rate limiting */
609     while (true) {
610         int bytes_sent;
611
612         bytes_sent = ram_save_block(f, true);
613         /* no more blocks to sent */
614         if (bytes_sent < 0) {
615             break;
616         }
617         bytes_transferred += bytes_sent;
618     }
619     memory_global_dirty_log_stop();
620
621     qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
622
623     return 0;
624 }
625
626 static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
627 {
628     int ret, rc = 0;
629     unsigned int xh_len;
630     int xh_flags;
631
632     if (!XBZRLE.decoded_buf) {
633         XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE);
634     }
635
636     /* extract RLE header */
637     xh_flags = qemu_get_byte(f);
638     xh_len = qemu_get_be16(f);
639
640     if (xh_flags != ENCODING_FLAG_XBZRLE) {
641         fprintf(stderr, "Failed to load XBZRLE page - wrong compression!\n");
642         return -1;
643     }
644
645     if (xh_len > TARGET_PAGE_SIZE) {
646         fprintf(stderr, "Failed to load XBZRLE page - len overflow!\n");
647         return -1;
648     }
649     /* load data and decode */
650     qemu_get_buffer(f, XBZRLE.decoded_buf, xh_len);
651
652     /* decode RLE */
653     ret = xbzrle_decode_buffer(XBZRLE.decoded_buf, xh_len, host,
654                                TARGET_PAGE_SIZE);
655     if (ret == -1) {
656         fprintf(stderr, "Failed to load XBZRLE page - decode error!\n");
657         rc = -1;
658     } else  if (ret > TARGET_PAGE_SIZE) {
659         fprintf(stderr, "Failed to load XBZRLE page - size %d exceeds %d!\n",
660                 ret, TARGET_PAGE_SIZE);
661         abort();
662     }
663
664     return rc;
665 }
666
667 static inline void *host_from_stream_offset(QEMUFile *f,
668                                             ram_addr_t offset,
669                                             int flags)
670 {
671     static RAMBlock *block = NULL;
672     char id[256];
673     uint8_t len;
674
675     if (flags & RAM_SAVE_FLAG_CONTINUE) {
676         if (!block) {
677             fprintf(stderr, "Ack, bad migration stream!\n");
678             return NULL;
679         }
680
681         return memory_region_get_ram_ptr(block->mr) + offset;
682     }
683
684     len = qemu_get_byte(f);
685     qemu_get_buffer(f, (uint8_t *)id, len);
686     id[len] = 0;
687
688     QLIST_FOREACH(block, &ram_list.blocks, next) {
689         if (!strncmp(id, block->idstr, sizeof(id)))
690             return memory_region_get_ram_ptr(block->mr) + offset;
691     }
692
693     fprintf(stderr, "Can't find block %s!\n", id);
694     return NULL;
695 }
696
697 static int ram_load(QEMUFile *f, void *opaque, int version_id)
698 {
699     ram_addr_t addr;
700     int flags, ret = 0;
701     int error;
702     static uint64_t seq_iter;
703
704     seq_iter++;
705
706     if (version_id < 4 || version_id > 4) {
707         return -EINVAL;
708     }
709
710     do {
711         addr = qemu_get_be64(f);
712
713         flags = addr & ~TARGET_PAGE_MASK;
714         addr &= TARGET_PAGE_MASK;
715
716         if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
717             if (version_id == 4) {
718                 /* Synchronize RAM block list */
719                 char id[256];
720                 ram_addr_t length;
721                 ram_addr_t total_ram_bytes = addr;
722
723                 while (total_ram_bytes) {
724                     RAMBlock *block;
725                     uint8_t len;
726
727                     len = qemu_get_byte(f);
728                     qemu_get_buffer(f, (uint8_t *)id, len);
729                     id[len] = 0;
730                     length = qemu_get_be64(f);
731
732                     QLIST_FOREACH(block, &ram_list.blocks, next) {
733                         if (!strncmp(id, block->idstr, sizeof(id))) {
734                             if (block->length != length) {
735                                 ret =  -EINVAL;
736                                 goto done;
737                             }
738                             break;
739                         }
740                     }
741
742                     if (!block) {
743                         fprintf(stderr, "Unknown ramblock \"%s\", cannot "
744                                 "accept migration\n", id);
745                         ret = -EINVAL;
746                         goto done;
747                     }
748
749                     total_ram_bytes -= length;
750                 }
751             }
752         }
753
754         if (flags & RAM_SAVE_FLAG_COMPRESS) {
755             void *host;
756             uint8_t ch;
757
758             host = host_from_stream_offset(f, addr, flags);
759             if (!host) {
760                 return -EINVAL;
761             }
762
763             ch = qemu_get_byte(f);
764             memset(host, ch, TARGET_PAGE_SIZE);
765 #ifndef _WIN32
766             if (ch == 0 &&
767                 (!kvm_enabled() || kvm_has_sync_mmu())) {
768                 qemu_madvise(host, TARGET_PAGE_SIZE, QEMU_MADV_DONTNEED);
769             }
770 #endif
771         } else if (flags & RAM_SAVE_FLAG_PAGE) {
772             void *host;
773
774             host = host_from_stream_offset(f, addr, flags);
775             if (!host) {
776                 return -EINVAL;
777             }
778
779             qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
780         } else if (flags & RAM_SAVE_FLAG_XBZRLE) {
781             if (!migrate_use_xbzrle()) {
782                 return -EINVAL;
783             }
784             void *host = host_from_stream_offset(f, addr, flags);
785             if (!host) {
786                 return -EINVAL;
787             }
788
789             if (load_xbzrle(f, addr, host) < 0) {
790                 ret = -EINVAL;
791                 goto done;
792             }
793         }
794         error = qemu_file_get_error(f);
795         if (error) {
796             ret = error;
797             goto done;
798         }
799     } while (!(flags & RAM_SAVE_FLAG_EOS));
800
801 done:
802     DPRINTF("Completed load of VM with exit code %d seq iteration "
803             "%" PRIu64 "\n", ret, seq_iter);
804     return ret;
805 }
806
807 SaveVMHandlers savevm_ram_handlers = {
808     .save_live_setup = ram_save_setup,
809     .save_live_iterate = ram_save_iterate,
810     .save_live_complete = ram_save_complete,
811     .load_state = ram_load,
812     .cancel = ram_migration_cancel,
813 };
814
815 #ifdef HAS_AUDIO
816 struct soundhw {
817     const char *name;
818     const char *descr;
819     int enabled;
820     int isa;
821     union {
822         int (*init_isa) (ISABus *bus);
823         int (*init_pci) (PCIBus *bus);
824     } init;
825 };
826
827 static struct soundhw soundhw[] = {
828 #ifdef HAS_AUDIO_CHOICE
829 #ifdef CONFIG_PCSPK
830     {
831         "pcspk",
832         "PC speaker",
833         0,
834         1,
835         { .init_isa = pcspk_audio_init }
836     },
837 #endif
838
839 #ifdef CONFIG_SB16
840     {
841         "sb16",
842         "Creative Sound Blaster 16",
843         0,
844         1,
845         { .init_isa = SB16_init }
846     },
847 #endif
848
849 #ifdef CONFIG_CS4231A
850     {
851         "cs4231a",
852         "CS4231A",
853         0,
854         1,
855         { .init_isa = cs4231a_init }
856     },
857 #endif
858
859 #ifdef CONFIG_ADLIB
860     {
861         "adlib",
862 #ifdef HAS_YMF262
863         "Yamaha YMF262 (OPL3)",
864 #else
865         "Yamaha YM3812 (OPL2)",
866 #endif
867         0,
868         1,
869         { .init_isa = Adlib_init }
870     },
871 #endif
872
873 #ifdef CONFIG_GUS
874     {
875         "gus",
876         "Gravis Ultrasound GF1",
877         0,
878         1,
879         { .init_isa = GUS_init }
880     },
881 #endif
882
883 #ifdef CONFIG_AC97
884     {
885         "ac97",
886         "Intel 82801AA AC97 Audio",
887         0,
888         0,
889         { .init_pci = ac97_init }
890     },
891 #endif
892
893 #ifdef CONFIG_ES1370
894     {
895         "es1370",
896         "ENSONIQ AudioPCI ES1370",
897         0,
898         0,
899         { .init_pci = es1370_init }
900     },
901 #endif
902
903 #ifdef CONFIG_HDA
904     {
905         "hda",
906         "Intel HD Audio",
907         0,
908         0,
909         { .init_pci = intel_hda_and_codec_init }
910     },
911 #endif
912
913 #endif /* HAS_AUDIO_CHOICE */
914
915     { NULL, NULL, 0, 0, { NULL } }
916 };
917
918 void select_soundhw(const char *optarg)
919 {
920     struct soundhw *c;
921
922     if (is_help_option(optarg)) {
923     show_valid_cards:
924
925 #ifdef HAS_AUDIO_CHOICE
926         printf("Valid sound card names (comma separated):\n");
927         for (c = soundhw; c->name; ++c) {
928             printf ("%-11s %s\n", c->name, c->descr);
929         }
930         printf("\n-soundhw all will enable all of the above\n");
931 #else
932         printf("Machine has no user-selectable audio hardware "
933                "(it may or may not have always-present audio hardware).\n");
934 #endif
935         exit(!is_help_option(optarg));
936     }
937     else {
938         size_t l;
939         const char *p;
940         char *e;
941         int bad_card = 0;
942
943         if (!strcmp(optarg, "all")) {
944             for (c = soundhw; c->name; ++c) {
945                 c->enabled = 1;
946             }
947             return;
948         }
949
950         p = optarg;
951         while (*p) {
952             e = strchr(p, ',');
953             l = !e ? strlen(p) : (size_t) (e - p);
954
955             for (c = soundhw; c->name; ++c) {
956                 if (!strncmp(c->name, p, l) && !c->name[l]) {
957                     c->enabled = 1;
958                     break;
959                 }
960             }
961
962             if (!c->name) {
963                 if (l > 80) {
964                     fprintf(stderr,
965                             "Unknown sound card name (too big to show)\n");
966                 }
967                 else {
968                     fprintf(stderr, "Unknown sound card name `%.*s'\n",
969                             (int) l, p);
970                 }
971                 bad_card = 1;
972             }
973             p += l + (e != NULL);
974         }
975
976         if (bad_card) {
977             goto show_valid_cards;
978         }
979     }
980 }
981
982 void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
983 {
984     struct soundhw *c;
985
986     for (c = soundhw; c->name; ++c) {
987         if (c->enabled) {
988             if (c->isa) {
989                 if (isa_bus) {
990                     c->init.init_isa(isa_bus);
991                 }
992             } else {
993                 if (pci_bus) {
994                     c->init.init_pci(pci_bus);
995                 }
996             }
997         }
998     }
999 }
1000 #else
1001 void select_soundhw(const char *optarg)
1002 {
1003 }
1004 void audio_init(ISABus *isa_bus, PCIBus *pci_bus)
1005 {
1006 }
1007 #endif
1008
1009 int qemu_uuid_parse(const char *str, uint8_t *uuid)
1010 {
1011     int ret;
1012
1013     if (strlen(str) != 36) {
1014         return -1;
1015     }
1016
1017     ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
1018                  &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
1019                  &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14],
1020                  &uuid[15]);
1021
1022     if (ret != 16) {
1023         return -1;
1024     }
1025 #ifdef TARGET_I386
1026     smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
1027 #endif
1028     return 0;
1029 }
1030
1031 void do_acpitable_option(const char *optarg)
1032 {
1033 #ifdef TARGET_I386
1034     if (acpi_table_add(optarg) < 0) {
1035         fprintf(stderr, "Wrong acpi table provided\n");
1036         exit(1);
1037     }
1038 #endif
1039 }
1040
1041 void do_smbios_option(const char *optarg)
1042 {
1043 #ifdef TARGET_I386
1044     if (smbios_entry_add(optarg) < 0) {
1045         fprintf(stderr, "Wrong smbios provided\n");
1046         exit(1);
1047     }
1048 #endif
1049 }
1050
1051 void cpudef_init(void)
1052 {
1053 #if defined(cpudef_setup)
1054     cpudef_setup(); /* parse cpu definitions in target config file */
1055 #endif
1056 }
1057
1058 int audio_available(void)
1059 {
1060 #ifdef HAS_AUDIO
1061     return 1;
1062 #else
1063     return 0;
1064 #endif
1065 }
1066
1067 int tcg_available(void)
1068 {
1069     return 1;
1070 }
1071
1072 int kvm_available(void)
1073 {
1074 #ifdef CONFIG_KVM
1075     return 1;
1076 #else
1077     return 0;
1078 #endif
1079 }
1080
1081 int xen_available(void)
1082 {
1083 #ifdef CONFIG_XEN
1084     return 1;
1085 #else
1086     return 0;
1087 #endif
1088 }
1089
1090
1091 TargetInfo *qmp_query_target(Error **errp)
1092 {
1093     TargetInfo *info = g_malloc0(sizeof(*info));
1094
1095     info->arch = TARGET_TYPE;
1096
1097     return info;
1098 }
This page took 0.088759 seconds and 4 git commands to generate.