9ae3a8
From 8140691df300c06f99981832cf2f19a2e5e5d0b9 Mon Sep 17 00:00:00 2001
9ae3a8
From: Laszlo Ersek <lersek@redhat.com>
9ae3a8
Date: Sat, 11 Jan 2014 17:59:57 +0100
9ae3a8
Subject: [PATCH 07/22] kvm: support using KVM_MEM_READONLY flag for regions
9ae3a8
9ae3a8
RH-Author: Laszlo Ersek <lersek@redhat.com>
9ae3a8
Message-id: <1389463208-6278-8-git-send-email-lersek@redhat.com>
9ae3a8
Patchwork-id: 56619
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH 07/18] kvm: support using KVM_MEM_READONLY flag for regions
9ae3a8
Bugzilla: 1032346
9ae3a8
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
RH-Acked-by: Amos Kong <akong@redhat.com>
9ae3a8
RH-Acked-by: Andrew Jones <drjones@redhat.com>
9ae3a8
9ae3a8
From: Jordan Justen <jordan.l.justen@intel.com>
9ae3a8
9ae3a8
For readonly memory regions and rom devices in romd_mode,
9ae3a8
we make use of the KVM_MEM_READONLY. A slot that uses
9ae3a8
KVM_MEM_READONLY can be read from and code can execute from the
9ae3a8
region, but writes will exit to qemu.
9ae3a8
9ae3a8
For rom devices with !romd_mode, we force the slot to be
9ae3a8
removed so reads or writes to the region will exit to qemu.
9ae3a8
(Note that a memory region in this state is not executable
9ae3a8
within kvm.)
9ae3a8
9ae3a8
v7:
9ae3a8
 * Update for readable => romd_mode rename (5f9a5ea1)
9ae3a8
9ae3a8
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
9ae3a8
Reviewed-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com> (v4)
9ae3a8
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> (v5)
9ae3a8
Message-id: 1369816047-16384-4-git-send-email-jordan.l.justen@intel.com
9ae3a8
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
9ae3a8
(cherry picked from commit 235e8982ad393e5611cb892df54881c872eea9e1)
9ae3a8
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
---
9ae3a8
 kvm-all.c | 37 ++++++++++++++++++++++++++++---------
9ae3a8
 1 file changed, 28 insertions(+), 9 deletions(-)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 kvm-all.c |   37 ++++++++++++++++++++++++++++---------
9ae3a8
 1 files changed, 28 insertions(+), 9 deletions(-)
9ae3a8
9ae3a8
diff --git a/kvm-all.c b/kvm-all.c
9ae3a8
index 0a70ca1..42c71b9 100644
9ae3a8
--- a/kvm-all.c
9ae3a8
+++ b/kvm-all.c
9ae3a8
@@ -203,12 +203,18 @@ static int kvm_set_user_memory_region(KVMState *s, KVMSlot *slot)
9ae3a8
 
9ae3a8
     mem.slot = slot->slot;
9ae3a8
     mem.guest_phys_addr = slot->start_addr;
9ae3a8
-    mem.memory_size = slot->memory_size;
9ae3a8
     mem.userspace_addr = (unsigned long)slot->ram;
9ae3a8
     mem.flags = slot->flags;
9ae3a8
     if (s->migration_log) {
9ae3a8
         mem.flags |= KVM_MEM_LOG_DIRTY_PAGES;
9ae3a8
     }
9ae3a8
+    if (mem.flags & KVM_MEM_READONLY) {
9ae3a8
+        /* Set the slot size to 0 before setting the slot to the desired
9ae3a8
+         * value. This is needed based on KVM commit 75d61fbc. */
9ae3a8
+        mem.memory_size = 0;
9ae3a8
+        kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem;;
9ae3a8
+    }
9ae3a8
+    mem.memory_size = slot->memory_size;
9ae3a8
     return kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem;;
9ae3a8
 }
9ae3a8
 
9ae3a8
@@ -270,9 +276,14 @@ err:
9ae3a8
  * dirty pages logging control
9ae3a8
  */
9ae3a8
 
9ae3a8
-static int kvm_mem_flags(KVMState *s, bool log_dirty)
9ae3a8
+static int kvm_mem_flags(KVMState *s, bool log_dirty, bool readonly)
9ae3a8
 {
9ae3a8
-    return log_dirty ? KVM_MEM_LOG_DIRTY_PAGES : 0;
9ae3a8
+    int flags = 0;
9ae3a8
+    flags = log_dirty ? KVM_MEM_LOG_DIRTY_PAGES : 0;
9ae3a8
+    if (readonly && kvm_readonly_mem_allowed) {
9ae3a8
+        flags |= KVM_MEM_READONLY;
9ae3a8
+    }
9ae3a8
+    return flags;
9ae3a8
 }
9ae3a8
 
9ae3a8
 static int kvm_slot_dirty_pages_log_change(KVMSlot *mem, bool log_dirty)
9ae3a8
@@ -283,7 +294,7 @@ static int kvm_slot_dirty_pages_log_change(KVMSlot *mem, bool log_dirty)
9ae3a8
 
9ae3a8
     old_flags = mem->flags;
9ae3a8
 
9ae3a8
-    flags = (mem->flags & ~mask) | kvm_mem_flags(s, log_dirty);
9ae3a8
+    flags = (mem->flags & ~mask) | kvm_mem_flags(s, log_dirty, false);
9ae3a8
     mem->flags = flags;
9ae3a8
 
9ae3a8
     /* If nothing changed effectively, no need to issue ioctl */
9ae3a8
@@ -601,6 +612,8 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
9ae3a8
     int err;
9ae3a8
     MemoryRegion *mr = section->mr;
9ae3a8
     bool log_dirty = memory_region_is_logging(mr);
9ae3a8
+    bool writeable = !mr->readonly && !mr->rom_device;
9ae3a8
+    bool readonly_flag = mr->readonly || memory_region_is_romd(mr);
9ae3a8
     hwaddr start_addr = section->offset_within_address_space;
9ae3a8
     ram_addr_t size = section->size;
9ae3a8
     void *ram = NULL;
9ae3a8
@@ -620,7 +633,13 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
9ae3a8
     }
9ae3a8
 
9ae3a8
     if (!memory_region_is_ram(mr)) {
9ae3a8
-        return;
9ae3a8
+        if (writeable || !kvm_readonly_mem_allowed) {
9ae3a8
+            return;
9ae3a8
+        } else if (!mr->romd_mode) {
9ae3a8
+            /* If the memory device is not in romd_mode, then we actually want
9ae3a8
+             * to remove the kvm memory slot so all accesses will trap. */
9ae3a8
+            add = false;
9ae3a8
+        }
9ae3a8
     }
9ae3a8
 
9ae3a8
     ram = memory_region_get_ram_ptr(mr) + section->offset_within_region + delta;
9ae3a8
@@ -669,7 +688,7 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
9ae3a8
             mem->memory_size = old.memory_size;
9ae3a8
             mem->start_addr = old.start_addr;
9ae3a8
             mem->ram = old.ram;
9ae3a8
-            mem->flags = kvm_mem_flags(s, log_dirty);
9ae3a8
+            mem->flags = kvm_mem_flags(s, log_dirty, readonly_flag);
9ae3a8
 
9ae3a8
             err = kvm_set_user_memory_region(s, mem);
9ae3a8
             if (err) {
9ae3a8
@@ -690,7 +709,7 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
9ae3a8
             mem->memory_size = start_addr - old.start_addr;
9ae3a8
             mem->start_addr = old.start_addr;
9ae3a8
             mem->ram = old.ram;
9ae3a8
-            mem->flags =  kvm_mem_flags(s, log_dirty);
9ae3a8
+            mem->flags =  kvm_mem_flags(s, log_dirty, readonly_flag);
9ae3a8
 
9ae3a8
             err = kvm_set_user_memory_region(s, mem);
9ae3a8
             if (err) {
9ae3a8
@@ -714,7 +733,7 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
9ae3a8
             size_delta = mem->start_addr - old.start_addr;
9ae3a8
             mem->memory_size = old.memory_size - size_delta;
9ae3a8
             mem->ram = old.ram + size_delta;
9ae3a8
-            mem->flags = kvm_mem_flags(s, log_dirty);
9ae3a8
+            mem->flags = kvm_mem_flags(s, log_dirty, readonly_flag);
9ae3a8
 
9ae3a8
             err = kvm_set_user_memory_region(s, mem);
9ae3a8
             if (err) {
9ae3a8
@@ -736,7 +755,7 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add)
9ae3a8
     mem->memory_size = size;
9ae3a8
     mem->start_addr = start_addr;
9ae3a8
     mem->ram = ram;
9ae3a8
-    mem->flags = kvm_mem_flags(s, log_dirty);
9ae3a8
+    mem->flags = kvm_mem_flags(s, log_dirty, readonly_flag);
9ae3a8
 
9ae3a8
     err = kvm_set_user_memory_region(s, mem);
9ae3a8
     if (err) {
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8