From patchwork Thu Oct 25 09:22:48 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Guangrong X-Patchwork-Id: 194084 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 781822C009A for ; Thu, 25 Oct 2012 20:23:59 +1100 (EST) Received: from localhost ([::1]:36383 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TRJfR-0001fR-Ju for incoming@patchwork.ozlabs.org; Thu, 25 Oct 2012 05:23:57 -0400 Received: from eggs.gnu.org ([208.118.235.92]:55629) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TRJfD-0001eK-92 for qemu-devel@nongnu.org; Thu, 25 Oct 2012 05:23:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TRJf7-0007xO-BQ for qemu-devel@nongnu.org; Thu, 25 Oct 2012 05:23:43 -0400 Received: from e23smtp05.au.ibm.com ([202.81.31.147]:53498) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TRJey-0007sJ-J2 for qemu-devel@nongnu.org; Thu, 25 Oct 2012 05:23:37 -0400 Received: from /spool/local by e23smtp05.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 25 Oct 2012 19:21:23 +1000 Received: from d23relay03.au.ibm.com (202.81.31.245) by e23smtp05.au.ibm.com (202.81.31.211) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 25 Oct 2012 19:21:14 +1000 Received: from d23av01.au.ibm.com (d23av01.au.ibm.com [9.190.234.96]) by d23relay03.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q9P9Mt7K37158980 for ; Thu, 25 Oct 2012 20:22:55 +1100 Received: from d23av01.au.ibm.com (loopback [127.0.0.1]) by d23av01.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q9P9Mpmg023494 for ; Thu, 25 Oct 2012 20:22:51 +1100 Received: from localhost.localdomain ([9.123.236.99]) by d23av01.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q9P9Mmi1023396; Thu, 25 Oct 2012 20:22:49 +1100 Message-ID: <508904E8.3010809@linux.vnet.ibm.com> Date: Thu, 25 Oct 2012 17:22:48 +0800 From: Xiao Guangrong User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120911 Thunderbird/15.0.1 MIME-Version: 1.0 To: Xiao Guangrong References: <50890462.5010307@linux.vnet.ibm.com> In-Reply-To: <50890462.5010307@linux.vnet.ibm.com> x-cbid: 12102509-1396-0000-0000-0000020B042D X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 202.81.31.147 Cc: KVM , Jan Kiszka , Marcelo Tosatti , qemu-devel@nongnu.org, Kevin O'Connor , Avi Kivity , Anthony Liguori , Liu Sheng Subject: [Qemu-devel] [PATCH v2 4/5] Qemu: implement readonly memory X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org As readonly memory is support in kvm, this patch supports this feature in qemu, mainly pluging the memory region with KVM_MEM_READONLY flag to kvm Signed-off-by: Xiao Guangrong --- kvm-all.c | 24 +++++++++++++++++------- 1 files changed, 17 insertions(+), 7 deletions(-) diff --git a/kvm-all.c b/kvm-all.c index 92a7137..6b9395a 100644 --- a/kvm-all.c +++ b/kvm-all.c @@ -101,6 +101,7 @@ struct KVMState QTAILQ_HEAD(msi_hashtab, KVMMSIRoute) msi_hashtab[KVM_MSI_HASHTAB_SIZE]; bool direct_msi; #endif + bool readonly_mem; }; KVMState *kvm_state; @@ -265,9 +266,16 @@ err: * dirty pages logging control */ -static int kvm_mem_flags(KVMState *s, bool log_dirty) +static int kvm_mem_flags(KVMState *s, MemoryRegion *mr) { - return log_dirty ? KVM_MEM_LOG_DIRTY_PAGES : 0; + int flags; + + flags = memory_region_is_logging(mr) ? KVM_MEM_LOG_DIRTY_PAGES : 0; + + if (s->readonly_mem && mr->readonly) { + flags |= KVM_MEM_READONLY; + } + return flags; } static int kvm_slot_dirty_pages_log_change(KVMSlot *mem, bool log_dirty) @@ -278,7 +286,7 @@ static int kvm_slot_dirty_pages_log_change(KVMSlot *mem, bool log_dirty) old_flags = mem->flags; - flags = (mem->flags & ~mask) | kvm_mem_flags(s, log_dirty); + flags = (mem->flags & ~mask) | (log_dirty ? mask : 0); mem->flags = flags; /* If nothing changed effectively, no need to issue ioctl */ @@ -626,7 +634,7 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add) mem->memory_size = old.memory_size; mem->start_addr = old.start_addr; mem->ram = old.ram; - mem->flags = kvm_mem_flags(s, log_dirty); + mem->flags = kvm_mem_flags(s, mr); err = kvm_set_user_memory_region(s, mem); if (err) { @@ -647,7 +655,7 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add) mem->memory_size = start_addr - old.start_addr; mem->start_addr = old.start_addr; mem->ram = old.ram; - mem->flags = kvm_mem_flags(s, log_dirty); + mem->flags = kvm_mem_flags(s, mr); err = kvm_set_user_memory_region(s, mem); if (err) { @@ -671,7 +679,7 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add) size_delta = mem->start_addr - old.start_addr; mem->memory_size = old.memory_size - size_delta; mem->ram = old.ram + size_delta; - mem->flags = kvm_mem_flags(s, log_dirty); + mem->flags = kvm_mem_flags(s, mr); err = kvm_set_user_memory_region(s, mem); if (err) { @@ -693,7 +701,7 @@ static void kvm_set_phys_mem(MemoryRegionSection *section, bool add) mem->memory_size = size; mem->start_addr = start_addr; mem->ram = ram; - mem->flags = kvm_mem_flags(s, log_dirty); + mem->flags = kvm_mem_flags(s, mr); err = kvm_set_user_memory_region(s, mem); if (err) { @@ -1390,6 +1398,8 @@ int kvm_init(void) s->irq_set_ioctl = KVM_IRQ_LINE_STATUS; } + s->readonly_mem = kvm_check_extension(s, KVM_CAP_READONLY_MEM); + ret = kvm_arch_init(s); if (ret < 0) { goto err;