@@ -1445,6 +1445,38 @@ out:
kvm_slots_unlock();
}
+static int kvm_map_private_memory(hwaddr start, hwaddr size)
+{
+ return 0;
+}
+
+static int kvm_map_shared_memory(hwaddr start, hwaddr size)
+{
+ MemoryRegionSection section;
+ void *addr;
+ RAMBlock *rb;
+ ram_addr_t offset;
+
+ /* Punch a hole in private memory. */
+ section = memory_region_find(get_system_private_memory(), start, size);
+ if (section.mr) {
+ addr = memory_region_get_ram_ptr(section.mr) +
+ section.offset_within_region;
+ rb = qemu_ram_block_from_host(addr, false, &offset);
+ ram_block_discard_range(rb, offset, size);
+ memory_region_unref(section.mr);
+ }
+
+ /* Create new shared memory. */
+ section = memory_region_find(get_system_memory(), start, size);
+ if (section.mr) {
+ memory_region_unref(section.mr);
+ return -1; /*Already existed. */
+ }
+
+ return kvm_arch_map_shared_memory(start, size);
+}
+
static void *kvm_dirty_ring_reaper_thread(void *data)
{
KVMState *s = data;
@@ -2957,6 +2989,23 @@ int kvm_cpu_exec(CPUState *cpu)
break;
}
break;
+ case KVM_EXIT_MEMORY_ERROR:
+ switch (run->mem.type) {
+ case KVM_EXIT_MEM_MAP_PRIVATE:
+ ret = kvm_map_private_memory(run->mem.u.map.gpa,
+ run->mem.u.map.size);
+ break;
+ case KVM_EXIT_MEM_MAP_SHARE:
+ ret = kvm_map_shared_memory(run->mem.u.map.gpa,
+ run->mem.u.map.size);
+ break;
+ default:
+ DPRINTF("kvm_arch_handle_exit\n");
+ ret = kvm_arch_handle_exit(cpu, run);
+ break;
+ }
+ break;
+
default:
DPRINTF("kvm_arch_handle_exit\n");
ret = kvm_arch_handle_exit(cpu, run);
@@ -547,4 +547,5 @@ bool kvm_cpu_check_are_resettable(void);
bool kvm_arch_cpu_check_are_resettable(void);
+int kvm_arch_map_shared_memory(hwaddr start, hwaddr size);
#endif
@@ -1051,3 +1051,8 @@ bool kvm_arch_cpu_check_are_resettable(void)
{
return true;
}
+
+int kvm_arch_map_shared_memory(hwaddr start, hwaddr size)
+{
+ return 0;
+}
@@ -4925,3 +4925,30 @@ bool kvm_arch_cpu_check_are_resettable(void)
{
return !sev_es_enabled();
}
+
+int kvm_arch_map_shared_memory(hwaddr start, hwaddr size)
+{
+ MachineState *pcms = current_machine;
+ X86MachineState *x86ms = X86_MACHINE(pcms);
+ MemoryRegion *system_memory = get_system_memory();
+ MemoryRegion *region;
+ char name[134];
+ hwaddr offset;
+
+ if (start + size < x86ms->below_4g_mem_size) {
+ sprintf(name, "0x%lx@0x%lx", size, start);
+ region = g_malloc(sizeof(*region));
+ memory_region_init_alias(region, NULL, name, pcms->ram, start, size);
+ memory_region_add_subregion(system_memory, start, region);
+ return 0;
+ } else if (start > 0x100000000ULL){
+ sprintf(name, "0x%lx@0x%lx", size, start);
+ offset = start - 0x100000000ULL + x86ms->below_4g_mem_size;
+ region = g_malloc(sizeof(*region));
+ memory_region_init_alias(region, NULL, name, pcms->ram, offset, size);
+ memory_region_add_subregion(system_memory, start, region);
+ return 0;
+ }
+
+ return -1;
+}
@@ -1295,3 +1295,8 @@ bool kvm_arch_cpu_check_are_resettable(void)
{
return true;
}
+
+int kvm_arch_map_shared_memory(hwaddr start, hwaddr size)
+{
+ return 0;
+}
@@ -2959,3 +2959,8 @@ bool kvm_arch_cpu_check_are_resettable(void)
{
return true;
}
+
+int kvm_arch_map_shared_memory(hwaddr start, hwaddr size)
+{
+ return 0;
+}
@@ -2562,3 +2562,8 @@ bool kvm_arch_cpu_check_are_resettable(void)
{
return true;
}
+
+int kvm_arch_map_shared_memory(hwaddr start, hwaddr size)
+{
+ return 0;
+}
Signed-off-by: Chao Peng <chao.p.peng@linux.intel.com> --- accel/kvm/kvm-all.c | 49 ++++++++++++++++++++++++++++++++++++++++++ include/sysemu/kvm.h | 1 + target/arm/kvm.c | 5 +++++ target/i386/kvm/kvm.c | 27 +++++++++++++++++++++++ target/mips/kvm.c | 5 +++++ target/ppc/kvm.c | 5 +++++ target/s390x/kvm/kvm.c | 5 +++++ 7 files changed, 97 insertions(+)