4 * Copyright (C) 2006-2008 Qumranet Technologies
6 * Licensed under the terms of the GNU GPL version 2 or higher.
9 #include "config-host.h"
19 #include "qemu-common.h"
27 #include <sys/utsname.h>
28 #include <sys/syscall.h>
34 extern void perror(const char *s
);
36 kvm_context_t kvm_context
;
40 pthread_mutex_t qemu_mutex
= PTHREAD_MUTEX_INITIALIZER
;
41 pthread_cond_t qemu_vcpu_cond
= PTHREAD_COND_INITIALIZER
;
42 pthread_cond_t qemu_system_cond
= PTHREAD_COND_INITIALIZER
;
43 pthread_cond_t qemu_pause_cond
= PTHREAD_COND_INITIALIZER
;
44 pthread_cond_t qemu_work_cond
= PTHREAD_COND_INITIALIZER
;
45 __thread
struct CPUState
*current_env
;
47 static int qemu_system_ready
;
49 #define SIG_IPI (SIGRTMIN+4)
52 static int io_thread_fd
= -1;
53 static int io_thread_sigfd
= -1;
55 static int kvm_debug_stop_requested
;
57 /* The list of ioperm_data */
58 static LIST_HEAD(, ioperm_data
) ioperm_head
;
60 static inline unsigned long kvm_get_thread_id(void)
62 return syscall(SYS_gettid
);
65 static void qemu_cond_wait(pthread_cond_t
*cond
)
67 CPUState
*env
= cpu_single_env
;
68 static const struct timespec ts
= {
73 pthread_cond_timedwait(cond
, &qemu_mutex
, &ts
);
77 static void sig_ipi_handler(int n
)
81 static void on_vcpu(CPUState
*env
, void (*func
)(void *data
), void *data
)
83 struct qemu_work_item wi
;
85 if (env
== current_env
) {
92 if (!env
->kvm_cpu_state
.queued_work_first
)
93 env
->kvm_cpu_state
.queued_work_first
= &wi
;
95 env
->kvm_cpu_state
.queued_work_last
->next
= &wi
;
96 env
->kvm_cpu_state
.queued_work_last
= &wi
;
100 pthread_kill(env
->kvm_cpu_state
.thread
, SIG_IPI
);
102 qemu_cond_wait(&qemu_work_cond
);
105 static void inject_interrupt(void *data
)
107 cpu_interrupt(current_env
, (int)data
);
110 void kvm_inject_interrupt(CPUState
*env
, int mask
)
112 on_vcpu(env
, inject_interrupt
, (void *)mask
);
115 void kvm_update_interrupt_request(CPUState
*env
)
120 if (!current_env
|| !current_env
->kvm_cpu_state
.created
)
123 * Testing for created here is really redundant
125 if (current_env
&& current_env
->kvm_cpu_state
.created
&&
126 env
!= current_env
&& !env
->kvm_cpu_state
.signalled
)
130 env
->kvm_cpu_state
.signalled
= 1;
131 if (env
->kvm_cpu_state
.thread
)
132 pthread_kill(env
->kvm_cpu_state
.thread
, SIG_IPI
);
137 void kvm_update_after_sipi(CPUState
*env
)
139 env
->kvm_cpu_state
.sipi_needed
= 1;
140 kvm_update_interrupt_request(env
);
143 void kvm_apic_init(CPUState
*env
)
145 if (env
->cpu_index
!= 0)
146 env
->kvm_cpu_state
.init
= 1;
147 kvm_update_interrupt_request(env
);
152 static int try_push_interrupts(void *opaque
)
154 return kvm_arch_try_push_interrupts(opaque
);
157 static void push_nmi(void *opaque
)
159 kvm_arch_push_nmi(opaque
);
162 static void post_kvm_run(void *opaque
, void *data
)
164 CPUState
*env
= (CPUState
*)data
;
166 pthread_mutex_lock(&qemu_mutex
);
167 kvm_arch_post_kvm_run(opaque
, env
);
170 static int pre_kvm_run(void *opaque
, void *data
)
172 CPUState
*env
= (CPUState
*)data
;
174 kvm_arch_pre_kvm_run(opaque
, env
);
176 if (env
->interrupt_request
& CPU_INTERRUPT_EXIT
)
178 pthread_mutex_unlock(&qemu_mutex
);
182 static void kvm_do_load_registers(void *_env
)
184 CPUState
*env
= _env
;
186 kvm_arch_load_regs(env
);
189 void kvm_load_registers(CPUState
*env
)
191 if (kvm_enabled() && qemu_system_ready
)
192 on_vcpu(env
, kvm_do_load_registers
, env
);
195 static void kvm_do_save_registers(void *_env
)
197 CPUState
*env
= _env
;
199 kvm_arch_save_regs(env
);
202 void kvm_save_registers(CPUState
*env
)
205 on_vcpu(env
, kvm_do_save_registers
, env
);
208 int kvm_cpu_exec(CPUState
*env
)
212 r
= kvm_run(kvm_context
, env
->cpu_index
, env
);
214 printf("kvm_run returned %d\n", r
);
221 extern int vm_running
;
223 static int has_work(CPUState
*env
)
225 if (!vm_running
|| (env
&& env
->kvm_cpu_state
.stopped
))
229 return kvm_arch_has_work(env
);
232 static void flush_queued_work(CPUState
*env
)
234 struct qemu_work_item
*wi
;
236 if (!env
->kvm_cpu_state
.queued_work_first
)
239 while ((wi
= env
->kvm_cpu_state
.queued_work_first
)) {
240 env
->kvm_cpu_state
.queued_work_first
= wi
->next
;
244 env
->kvm_cpu_state
.queued_work_last
= NULL
;
245 pthread_cond_broadcast(&qemu_work_cond
);
248 static void kvm_main_loop_wait(CPUState
*env
, int timeout
)
255 pthread_mutex_unlock(&qemu_mutex
);
257 ts
.tv_sec
= timeout
/ 1000;
258 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
259 sigemptyset(&waitset
);
260 sigaddset(&waitset
, SIG_IPI
);
262 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
265 pthread_mutex_lock(&qemu_mutex
);
267 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
268 printf("sigtimedwait: %s\n", strerror(e
));
272 cpu_single_env
= env
;
273 flush_queued_work(env
);
275 if (env
->kvm_cpu_state
.stop
) {
276 env
->kvm_cpu_state
.stop
= 0;
277 env
->kvm_cpu_state
.stopped
= 1;
278 pthread_cond_signal(&qemu_pause_cond
);
281 env
->kvm_cpu_state
.signalled
= 0;
284 static int all_threads_paused(void)
286 CPUState
*penv
= first_cpu
;
289 if (penv
->kvm_cpu_state
.stop
)
291 penv
= (CPUState
*)penv
->next_cpu
;
297 static void pause_all_threads(void)
299 CPUState
*penv
= first_cpu
;
301 assert(!cpu_single_env
);
304 penv
->kvm_cpu_state
.stop
= 1;
305 pthread_kill(penv
->kvm_cpu_state
.thread
, SIG_IPI
);
306 penv
= (CPUState
*)penv
->next_cpu
;
309 while (!all_threads_paused())
310 qemu_cond_wait(&qemu_pause_cond
);
313 static void resume_all_threads(void)
315 CPUState
*penv
= first_cpu
;
317 assert(!cpu_single_env
);
320 penv
->kvm_cpu_state
.stop
= 0;
321 penv
->kvm_cpu_state
.stopped
= 0;
322 pthread_kill(penv
->kvm_cpu_state
.thread
, SIG_IPI
);
323 penv
= (CPUState
*)penv
->next_cpu
;
327 static void kvm_vm_state_change_handler(void *context
, int running
)
330 resume_all_threads();
335 static void update_regs_for_sipi(CPUState
*env
)
337 kvm_arch_update_regs_for_sipi(env
);
338 env
->kvm_cpu_state
.sipi_needed
= 0;
341 static void update_regs_for_init(CPUState
*env
)
344 SegmentCache cs
= env
->segs
[R_CS
];
350 /* restore SIPI vector */
351 if(env
->kvm_cpu_state
.sipi_needed
)
352 env
->segs
[R_CS
] = cs
;
355 env
->kvm_cpu_state
.init
= 0;
356 kvm_arch_load_regs(env
);
359 static void setup_kernel_sigmask(CPUState
*env
)
364 sigaddset(&set
, SIGUSR2
);
365 sigaddset(&set
, SIGIO
);
366 sigaddset(&set
, SIGALRM
);
367 sigprocmask(SIG_BLOCK
, &set
, NULL
);
369 sigprocmask(SIG_BLOCK
, NULL
, &set
);
370 sigdelset(&set
, SIG_IPI
);
372 kvm_set_signal_mask(kvm_context
, env
->cpu_index
, &set
);
375 void qemu_kvm_system_reset(void)
377 CPUState
*penv
= first_cpu
;
384 kvm_arch_cpu_reset(penv
);
385 penv
= (CPUState
*)penv
->next_cpu
;
388 resume_all_threads();
391 static int kvm_main_loop_cpu(CPUState
*env
)
393 setup_kernel_sigmask(env
);
395 pthread_mutex_lock(&qemu_mutex
);
396 if (kvm_irqchip_in_kernel(kvm_context
))
399 kvm_qemu_init_env(env
);
401 kvm_tpr_vcpu_start(env
);
404 cpu_single_env
= env
;
405 kvm_load_registers(env
);
408 while (!has_work(env
))
409 kvm_main_loop_wait(env
, 1000);
410 if (env
->interrupt_request
& (CPU_INTERRUPT_HARD
| CPU_INTERRUPT_NMI
))
412 if (!kvm_irqchip_in_kernel(kvm_context
)) {
413 if (env
->kvm_cpu_state
.init
)
414 update_regs_for_init(env
);
415 if (env
->kvm_cpu_state
.sipi_needed
)
416 update_regs_for_sipi(env
);
418 if (!env
->halted
&& !env
->kvm_cpu_state
.init
)
420 env
->interrupt_request
&= ~CPU_INTERRUPT_EXIT
;
421 kvm_main_loop_wait(env
, 0);
423 pthread_mutex_unlock(&qemu_mutex
);
427 static void *ap_main_loop(void *_env
)
429 CPUState
*env
= _env
;
431 struct ioperm_data
*data
= NULL
;
434 env
->thread_id
= kvm_get_thread_id();
435 sigfillset(&signals
);
436 sigprocmask(SIG_BLOCK
, &signals
, NULL
);
437 kvm_create_vcpu(kvm_context
, env
->cpu_index
);
438 kvm_qemu_init_env(env
);
440 #ifdef USE_KVM_DEVICE_ASSIGNMENT
441 /* do ioperm for io ports of assigned devices */
442 LIST_FOREACH(data
, &ioperm_head
, entries
)
443 on_vcpu(env
, kvm_arch_do_ioperm
, data
);
446 /* signal VCPU creation */
447 pthread_mutex_lock(&qemu_mutex
);
448 current_env
->kvm_cpu_state
.created
= 1;
449 pthread_cond_signal(&qemu_vcpu_cond
);
451 /* and wait for machine initialization */
452 while (!qemu_system_ready
)
453 qemu_cond_wait(&qemu_system_cond
);
454 pthread_mutex_unlock(&qemu_mutex
);
456 kvm_main_loop_cpu(env
);
460 void kvm_init_vcpu(CPUState
*env
)
462 int cpu
= env
->cpu_index
;
463 pthread_create(&env
->kvm_cpu_state
.thread
, NULL
, ap_main_loop
, env
);
465 while (env
->kvm_cpu_state
.created
== 0)
466 qemu_cond_wait(&qemu_vcpu_cond
);
469 int kvm_init_ap(void)
474 qemu_add_vm_change_state_handler(kvm_vm_state_change_handler
, NULL
);
476 signal(SIG_IPI
, sig_ipi_handler
);
480 void qemu_kvm_notify_work(void)
486 if (io_thread_fd
== -1)
489 memcpy(buffer
, &value
, sizeof(value
));
494 len
= write(io_thread_fd
, buffer
+ offset
, 8 - offset
);
495 if (len
== -1 && errno
== EINTR
)
505 fprintf(stderr
, "failed to notify io thread\n");
508 /* If we have signalfd, we mask out the signals we want to handle and then
509 * use signalfd to listen for them. We rely on whatever the current signal
510 * handler is to dispatch the signals when we receive them.
513 static void sigfd_handler(void *opaque
)
515 int fd
= (unsigned long)opaque
;
516 struct qemu_signalfd_siginfo info
;
517 struct sigaction action
;
522 len
= read(fd
, &info
, sizeof(info
));
523 } while (len
== -1 && errno
== EINTR
);
525 if (len
== -1 && errno
== EAGAIN
)
528 if (len
!= sizeof(info
)) {
529 printf("read from sigfd returned %ld: %m\n", len
);
533 sigaction(info
.ssi_signo
, NULL
, &action
);
534 if (action
.sa_handler
)
535 action
.sa_handler(info
.ssi_signo
);
540 /* Used to break IO thread out of select */
541 static void io_thread_wakeup(void *opaque
)
543 int fd
= (unsigned long)opaque
;
550 len
= read(fd
, buffer
+ offset
, 8 - offset
);
551 if (len
== -1 && errno
== EINTR
)
561 int kvm_main_loop(void)
567 io_thread
= pthread_self();
568 qemu_system_ready
= 1;
570 if (qemu_eventfd(fds
) == -1) {
571 fprintf(stderr
, "failed to create eventfd\n");
575 qemu_set_fd_handler2(fds
[0], NULL
, io_thread_wakeup
, NULL
,
576 (void *)(unsigned long)fds
[0]);
578 io_thread_fd
= fds
[1];
581 sigaddset(&mask
, SIGIO
);
582 sigaddset(&mask
, SIGALRM
);
583 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
585 sigfd
= qemu_signalfd(&mask
);
587 fprintf(stderr
, "failed to create signalfd\n");
591 fcntl(sigfd
, F_SETFL
, O_NONBLOCK
);
593 qemu_set_fd_handler2(sigfd
, NULL
, sigfd_handler
, NULL
,
594 (void *)(unsigned long)sigfd
);
596 pthread_cond_broadcast(&qemu_system_cond
);
598 io_thread_sigfd
= sigfd
;
599 cpu_single_env
= NULL
;
602 main_loop_wait(1000);
603 if (qemu_shutdown_requested())
605 else if (qemu_powerdown_requested())
606 qemu_system_powerdown();
607 else if (qemu_reset_requested())
608 qemu_kvm_system_reset();
609 else if (kvm_debug_stop_requested
) {
611 kvm_debug_stop_requested
= 0;
616 pthread_mutex_unlock(&qemu_mutex
);
621 static int kvm_debug(void *opaque
, void *data
)
623 struct CPUState
*env
= (struct CPUState
*)data
;
625 kvm_debug_stop_requested
= 1;
626 env
->kvm_cpu_state
.stopped
= 1;
630 static int kvm_inb(void *opaque
, uint16_t addr
, uint8_t *data
)
632 *data
= cpu_inb(0, addr
);
636 static int kvm_inw(void *opaque
, uint16_t addr
, uint16_t *data
)
638 *data
= cpu_inw(0, addr
);
642 static int kvm_inl(void *opaque
, uint16_t addr
, uint32_t *data
)
644 *data
= cpu_inl(0, addr
);
648 #define PM_IO_BASE 0xb000
650 static int kvm_outb(void *opaque
, uint16_t addr
, uint8_t data
)
655 cpu_outb(0, 0xb3, 0);
662 x
= cpu_inw(0, PM_IO_BASE
+ 4);
664 cpu_outw(0, PM_IO_BASE
+ 4, x
);
671 x
= cpu_inw(0, PM_IO_BASE
+ 4);
673 cpu_outw(0, PM_IO_BASE
+ 4, x
);
681 cpu_outb(0, addr
, data
);
685 static int kvm_outw(void *opaque
, uint16_t addr
, uint16_t data
)
687 cpu_outw(0, addr
, data
);
691 static int kvm_outl(void *opaque
, uint16_t addr
, uint32_t data
)
693 cpu_outl(0, addr
, data
);
697 static int kvm_mmio_read(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
699 cpu_physical_memory_rw(addr
, data
, len
, 0);
703 static int kvm_mmio_write(void *opaque
, uint64_t addr
, uint8_t *data
, int len
)
705 cpu_physical_memory_rw(addr
, data
, len
, 1);
709 static int kvm_io_window(void *opaque
)
715 static int kvm_halt(void *opaque
, int vcpu
)
717 return kvm_arch_halt(opaque
, vcpu
);
720 static int kvm_shutdown(void *opaque
, void *data
)
722 struct CPUState
*env
= (struct CPUState
*)data
;
724 /* stop the current vcpu from going back to guest mode */
725 env
->kvm_cpu_state
.stopped
= 1;
727 qemu_system_reset_request();
731 static struct kvm_callbacks qemu_kvm_ops
= {
739 .mmio_read
= kvm_mmio_read
,
740 .mmio_write
= kvm_mmio_write
,
742 .shutdown
= kvm_shutdown
,
743 .io_window
= kvm_io_window
,
744 .try_push_interrupts
= try_push_interrupts
,
745 .push_nmi
= push_nmi
,
746 .post_kvm_run
= post_kvm_run
,
747 .pre_kvm_run
= pre_kvm_run
,
749 .tpr_access
= handle_tpr_access
,
752 .powerpc_dcr_read
= handle_powerpc_dcr_read
,
753 .powerpc_dcr_write
= handle_powerpc_dcr_write
,
759 /* Try to initialize kvm */
760 kvm_context
= kvm_init(&qemu_kvm_ops
, cpu_single_env
);
764 pthread_mutex_lock(&qemu_mutex
);
769 static int destroy_region_works
= 0;
771 int kvm_qemu_create_context(void)
775 kvm_disable_irqchip_creation(kvm_context
);
778 kvm_disable_pit_creation(kvm_context
);
780 if (kvm_create(kvm_context
, phys_ram_size
, (void**)&phys_ram_base
) < 0) {
784 r
= kvm_arch_qemu_create_context();
787 destroy_region_works
= kvm_destroy_memory_region_works(kvm_context
);
791 void kvm_qemu_destroy(void)
793 kvm_finalize(kvm_context
);
796 static int must_use_aliases_source(target_phys_addr_t addr
)
798 if (destroy_region_works
)
800 if (addr
== 0xa0000 || addr
== 0xa8000)
805 static int must_use_aliases_target(target_phys_addr_t addr
)
807 if (destroy_region_works
)
809 if (addr
>= 0xe000000 && addr
< 0x100000000ull
)
814 static struct mapping
{
815 target_phys_addr_t phys
;
819 static int nr_mappings
;
821 static struct mapping
*find_ram_mapping(ram_addr_t ram_addr
)
825 for (p
= mappings
; p
< mappings
+ nr_mappings
; ++p
) {
826 if (p
->ram
<= ram_addr
&& ram_addr
< p
->ram
+ p
->len
) {
833 static struct mapping
*find_mapping(target_phys_addr_t start_addr
)
837 for (p
= mappings
; p
< mappings
+ nr_mappings
; ++p
) {
838 if (p
->phys
<= start_addr
&& start_addr
< p
->phys
+ p
->len
) {
845 static void drop_mapping(target_phys_addr_t start_addr
)
847 struct mapping
*p
= find_mapping(start_addr
);
850 *p
= mappings
[--nr_mappings
];
853 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr
,
855 unsigned long phys_offset
)
858 unsigned long area_flags
= phys_offset
& ~TARGET_PAGE_MASK
;
861 phys_offset
&= ~IO_MEM_ROM
;
863 if (area_flags
== IO_MEM_UNASSIGNED
) {
864 if (must_use_aliases_source(start_addr
)) {
865 kvm_destroy_memory_alias(kvm_context
, start_addr
);
868 if (must_use_aliases_target(start_addr
))
870 kvm_unregister_memory_area(kvm_context
, start_addr
, size
);
874 r
= kvm_is_containing_region(kvm_context
, start_addr
, size
);
878 if (area_flags
>= TLB_MMIO
)
881 if (must_use_aliases_source(start_addr
)) {
882 p
= find_ram_mapping(phys_offset
);
884 kvm_create_memory_alias(kvm_context
, start_addr
, size
,
885 p
->phys
+ (phys_offset
- p
->ram
));
890 r
= kvm_register_phys_mem(kvm_context
, start_addr
,
891 phys_ram_base
+ phys_offset
,
894 printf("kvm_cpu_register_physical_memory: failed\n");
898 drop_mapping(start_addr
);
899 p
= &mappings
[nr_mappings
++];
900 p
->phys
= start_addr
;
901 p
->ram
= phys_offset
;
907 void kvm_cpu_unregister_physical_memory(target_phys_addr_t start_addr
,
908 target_phys_addr_t size
,
909 unsigned long phys_offset
)
911 kvm_unregister_memory_area(kvm_context
, start_addr
, size
);
914 int kvm_setup_guest_memory(void *area
, unsigned long size
)
919 if (kvm_enabled() && !kvm_has_sync_mmu())
920 ret
= madvise(area
, size
, MADV_DONTFORK
);
929 int kvm_qemu_check_extension(int ext
)
931 return kvm_check_extension(kvm_context
, ext
);
934 int kvm_qemu_init_env(CPUState
*cenv
)
936 return kvm_arch_qemu_init_env(cenv
);
939 struct kvm_guest_debug_data
{
940 struct kvm_debug_guest dbg
;
944 void kvm_invoke_guest_debug(void *data
)
946 struct kvm_guest_debug_data
*dbg_data
= data
;
948 dbg_data
->err
= kvm_guest_debug(kvm_context
, cpu_single_env
->cpu_index
,
952 int kvm_update_debugger(CPUState
*env
)
954 struct kvm_guest_debug_data data
;
958 memset(data
.dbg
.breakpoints
, 0, sizeof(data
.dbg
.breakpoints
));
960 data
.dbg
.enabled
= 0;
961 if (!TAILQ_EMPTY(&env
->breakpoints
) || env
->singlestep_enabled
) {
962 bp
= TAILQ_FIRST(&env
->breakpoints
);
963 data
.dbg
.enabled
= 1;
964 for (i
= 0; i
< 4; ++i
) {
965 data
.dbg
.breakpoints
[i
].enabled
= bp
!= NULL
;
967 data
.dbg
.breakpoints
[i
].address
= bp
->pc
;
968 bp
= TAILQ_NEXT(bp
, entry
);
971 data
.dbg
.singlestep
= env
->singlestep_enabled
;
973 on_vcpu(env
, kvm_invoke_guest_debug
, &data
);
979 * dirty pages logging
981 /* FIXME: use unsigned long pointer instead of unsigned char */
982 unsigned char *kvm_dirty_bitmap
= NULL
;
983 int kvm_physical_memory_set_dirty_tracking(int enable
)
991 if (!kvm_dirty_bitmap
) {
992 unsigned bitmap_size
= BITMAP_SIZE(phys_ram_size
);
993 kvm_dirty_bitmap
= qemu_malloc(bitmap_size
);
994 if (kvm_dirty_bitmap
== NULL
) {
995 perror("Failed to allocate dirty pages bitmap");
999 r
= kvm_dirty_pages_log_enable_all(kvm_context
);
1004 if (kvm_dirty_bitmap
) {
1005 r
= kvm_dirty_pages_log_reset(kvm_context
);
1006 qemu_free(kvm_dirty_bitmap
);
1007 kvm_dirty_bitmap
= NULL
;
1013 /* get kvm's dirty pages bitmap and update qemu's */
1014 int kvm_get_dirty_pages_log_range(unsigned long start_addr
,
1015 unsigned char *bitmap
,
1016 unsigned int offset
,
1017 unsigned long mem_size
)
1019 unsigned int i
, j
, n
=0;
1021 unsigned long page_number
, addr
, addr1
;
1022 ram_addr_t ram_addr
;
1023 unsigned int len
= ((mem_size
/TARGET_PAGE_SIZE
) + 7) / 8;
1026 * bitmap-traveling is faster than memory-traveling (for addr...)
1027 * especially when most of the memory is not dirty.
1029 for (i
=0; i
<len
; i
++) {
1034 page_number
= i
* 8 + j
;
1035 addr1
= page_number
* TARGET_PAGE_SIZE
;
1036 addr
= offset
+ addr1
;
1037 ram_addr
= cpu_get_physical_page_desc(addr
);
1038 cpu_physical_memory_set_dirty(ram_addr
);
1044 int kvm_get_dirty_bitmap_cb(unsigned long start
, unsigned long len
,
1045 void *bitmap
, void *opaque
)
1047 return kvm_get_dirty_pages_log_range(start
, bitmap
, start
, len
);
1051 * get kvm's dirty pages bitmap and update qemu's
1052 * we only care about physical ram, which resides in slots 0 and 3
1054 int kvm_update_dirty_pages_log(void)
1059 r
= kvm_get_dirty_pages_range(kvm_context
, 0, phys_ram_size
,
1060 kvm_dirty_bitmap
, NULL
,
1061 kvm_get_dirty_bitmap_cb
);
1065 void kvm_qemu_log_memory(target_phys_addr_t start
, target_phys_addr_t size
,
1069 kvm_dirty_pages_log_enable_slot(kvm_context
, start
, size
);
1071 if (must_use_aliases_target(start
))
1073 kvm_dirty_pages_log_disable_slot(kvm_context
, start
, size
);
1077 int kvm_get_phys_ram_page_bitmap(unsigned char *bitmap
)
1079 unsigned int bsize
= BITMAP_SIZE(phys_ram_size
);
1080 unsigned int brsize
= BITMAP_SIZE(ram_size
);
1081 unsigned int extra_pages
= (phys_ram_size
- ram_size
) / TARGET_PAGE_SIZE
;
1082 unsigned int extra_bytes
= (extra_pages
+7)/8;
1083 unsigned int hole_start
= BITMAP_SIZE(0xa0000);
1084 unsigned int hole_end
= BITMAP_SIZE(0xc0000);
1086 memset(bitmap
, 0xFF, brsize
+ extra_bytes
);
1087 memset(bitmap
+ hole_start
, 0, hole_end
- hole_start
);
1088 memset(bitmap
+ brsize
+ extra_bytes
, 0, bsize
- brsize
- extra_bytes
);
1093 #ifdef KVM_CAP_IRQCHIP
1095 int kvm_set_irq(int irq
, int level
)
1097 return kvm_set_irq_level(kvm_context
, irq
, level
);
1102 int qemu_kvm_get_dirty_pages(unsigned long phys_addr
, void *buf
)
1104 return kvm_get_dirty_pages(kvm_context
, phys_addr
, buf
);
1107 void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr
,
1108 unsigned long size
, int log
, int writable
)
1110 return kvm_create_phys_mem(kvm_context
, start_addr
, size
, log
, writable
);
1113 void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr
,
1116 kvm_destroy_phys_mem(kvm_context
, start_addr
, size
);
1119 void kvm_mutex_unlock(void)
1121 assert(!cpu_single_env
);
1122 pthread_mutex_unlock(&qemu_mutex
);
1125 void kvm_mutex_lock(void)
1127 pthread_mutex_lock(&qemu_mutex
);
1128 cpu_single_env
= NULL
;
1131 int qemu_kvm_register_coalesced_mmio(target_phys_addr_t addr
, unsigned int size
)
1133 return kvm_register_coalesced_mmio(kvm_context
, addr
, size
);
1136 int qemu_kvm_unregister_coalesced_mmio(target_phys_addr_t addr
,
1139 return kvm_unregister_coalesced_mmio(kvm_context
, addr
, size
);
1142 #ifdef USE_KVM_DEVICE_ASSIGNMENT
1143 void kvm_add_ioperm_data(struct ioperm_data
*data
)
1145 LIST_INSERT_HEAD(&ioperm_head
, data
, entries
);
1148 void kvm_ioperm(CPUState
*env
, void *data
)
1150 if (kvm_enabled() && qemu_system_ready
)
1151 on_vcpu(env
, kvm_arch_do_ioperm
, data
);
1156 void kvm_physical_sync_dirty_bitmap(target_phys_addr_t start_addr
, target_phys_addr_t end_addr
)
1160 if (must_use_aliases_source(start_addr
))
1163 buf
= qemu_malloc((end_addr
- start_addr
) / 8 + 2);
1164 kvm_get_dirty_pages_range(kvm_context
, start_addr
, end_addr
- start_addr
,
1165 buf
, NULL
, kvm_get_dirty_bitmap_cb
);
1169 int kvm_log_start(target_phys_addr_t phys_addr
, target_phys_addr_t len
)
1172 if (must_use_aliases_source(phys_addr
))
1174 kvm_qemu_log_memory(phys_addr
, len
, 1);
1179 int kvm_log_stop(target_phys_addr_t phys_addr
, target_phys_addr_t len
)
1182 if (must_use_aliases_source(phys_addr
))
1184 kvm_qemu_log_memory(phys_addr
, len
, 0);
1189 /* hack: both libkvm and upstream qemu define kvm_has_sync_mmu(), differently */
1190 #undef kvm_has_sync_mmu
1191 int qemu_kvm_has_sync_mmu(void)
1193 return kvm_has_sync_mmu(kvm_context
);