From: Andre Przywara <andre.przywara@arm.com> To: Will Deacon <will@kernel.org>, Julien Thierry <julien.thierry.kdev@gmail.com> Cc: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, Raphael Gault <raphael.gault@arm.com>, Sami Mujawar <sami.mujawar@arm.com>, Alexandru Elisei <Alexandru.Elisei@arm.com>, Ard Biesheuvel <ardb@kernel.org> Subject: [PATCH kvmtool v4 4/5] memslot: Add support for READONLY mappings Date: Thu, 23 Apr 2020 18:38:43 +0100 [thread overview] Message-ID: <20200423173844.24220-5-andre.przywara@arm.com> (raw) In-Reply-To: <20200423173844.24220-1-andre.przywara@arm.com> A KVM memslot has a flags field, which allows to mark a region as read-only. Add another memory type bit to allow kvmtool-internal users to map a write-protected region. Write access would trap and can be handled by the MMIO emulation, which should register on the same guest address region. Signed-off-by: Andre Przywara <andre.przywara@arm.com> --- include/kvm/kvm.h | 12 ++++++++---- kvm.c | 5 +++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h index 9428f57a..53373b08 100644 --- a/include/kvm/kvm.h +++ b/include/kvm/kvm.h @@ -40,10 +40,12 @@ enum kvm_mem_type { KVM_MEM_TYPE_RAM = 1 << 0, KVM_MEM_TYPE_DEVICE = 1 << 1, KVM_MEM_TYPE_RESERVED = 1 << 2, + KVM_MEM_TYPE_READONLY = 1 << 3, KVM_MEM_TYPE_ALL = KVM_MEM_TYPE_RAM | KVM_MEM_TYPE_DEVICE | KVM_MEM_TYPE_RESERVED + | KVM_MEM_TYPE_READONLY }; struct kvm_ext { @@ -158,17 +160,19 @@ u64 host_to_guest_flat(struct kvm *kvm, void *ptr); bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd, const char *kernel_cmdline); +#define add_read_only(type, str) \ + (((type) & KVM_MEM_TYPE_READONLY) ? str " (read-only)" : str) static inline const char *kvm_mem_type_to_string(enum kvm_mem_type type) { - switch (type) { + switch (type & ~KVM_MEM_TYPE_READONLY) { case KVM_MEM_TYPE_ALL: return "(all)"; case KVM_MEM_TYPE_RAM: - return "RAM"; + return add_read_only(type, "RAM"); case KVM_MEM_TYPE_DEVICE: - return "device"; + return add_read_only(type, "device"); case KVM_MEM_TYPE_RESERVED: - return "reserved"; + return add_read_only(type, "reserved"); } return "???"; diff --git a/kvm.c b/kvm.c index 26f6b9bc..e327541d 100644 --- a/kvm.c +++ b/kvm.c @@ -242,6 +242,7 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, struct kvm_mem_bank *bank; struct list_head *prev_entry; u32 slot; + u32 flags = 0; int ret; mutex_lock(&kvm->mem_banks_lock); @@ -313,9 +314,13 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, bank->type = type; bank->slot = slot; + if (type & KVM_MEM_TYPE_READONLY) + flags |= KVM_MEM_READONLY; + if (type != KVM_MEM_TYPE_RESERVED) { mem = (struct kvm_userspace_memory_region) { .slot = slot, + .flags = flags, .guest_phys_addr = guest_phys, .memory_size = size, .userspace_addr = (unsigned long)userspace_addr, -- 2.17.1
WARNING: multiple messages have this Message-ID (diff)
From: Andre Przywara <andre.przywara@arm.com> To: Will Deacon <will@kernel.org>, Julien Thierry <julien.thierry.kdev@gmail.com> Cc: kvm@vger.kernel.org, Ard Biesheuvel <ardb@kernel.org>, Raphael Gault <raphael.gault@arm.com>, Sami Mujawar <sami.mujawar@arm.com>, kvmarm@lists.cs.columbia.edu Subject: [PATCH kvmtool v4 4/5] memslot: Add support for READONLY mappings Date: Thu, 23 Apr 2020 18:38:43 +0100 [thread overview] Message-ID: <20200423173844.24220-5-andre.przywara@arm.com> (raw) In-Reply-To: <20200423173844.24220-1-andre.przywara@arm.com> A KVM memslot has a flags field, which allows to mark a region as read-only. Add another memory type bit to allow kvmtool-internal users to map a write-protected region. Write access would trap and can be handled by the MMIO emulation, which should register on the same guest address region. Signed-off-by: Andre Przywara <andre.przywara@arm.com> --- include/kvm/kvm.h | 12 ++++++++---- kvm.c | 5 +++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h index 9428f57a..53373b08 100644 --- a/include/kvm/kvm.h +++ b/include/kvm/kvm.h @@ -40,10 +40,12 @@ enum kvm_mem_type { KVM_MEM_TYPE_RAM = 1 << 0, KVM_MEM_TYPE_DEVICE = 1 << 1, KVM_MEM_TYPE_RESERVED = 1 << 2, + KVM_MEM_TYPE_READONLY = 1 << 3, KVM_MEM_TYPE_ALL = KVM_MEM_TYPE_RAM | KVM_MEM_TYPE_DEVICE | KVM_MEM_TYPE_RESERVED + | KVM_MEM_TYPE_READONLY }; struct kvm_ext { @@ -158,17 +160,19 @@ u64 host_to_guest_flat(struct kvm *kvm, void *ptr); bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd, const char *kernel_cmdline); +#define add_read_only(type, str) \ + (((type) & KVM_MEM_TYPE_READONLY) ? str " (read-only)" : str) static inline const char *kvm_mem_type_to_string(enum kvm_mem_type type) { - switch (type) { + switch (type & ~KVM_MEM_TYPE_READONLY) { case KVM_MEM_TYPE_ALL: return "(all)"; case KVM_MEM_TYPE_RAM: - return "RAM"; + return add_read_only(type, "RAM"); case KVM_MEM_TYPE_DEVICE: - return "device"; + return add_read_only(type, "device"); case KVM_MEM_TYPE_RESERVED: - return "reserved"; + return add_read_only(type, "reserved"); } return "???"; diff --git a/kvm.c b/kvm.c index 26f6b9bc..e327541d 100644 --- a/kvm.c +++ b/kvm.c @@ -242,6 +242,7 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, struct kvm_mem_bank *bank; struct list_head *prev_entry; u32 slot; + u32 flags = 0; int ret; mutex_lock(&kvm->mem_banks_lock); @@ -313,9 +314,13 @@ int kvm__register_mem(struct kvm *kvm, u64 guest_phys, u64 size, bank->type = type; bank->slot = slot; + if (type & KVM_MEM_TYPE_READONLY) + flags |= KVM_MEM_READONLY; + if (type != KVM_MEM_TYPE_RESERVED) { mem = (struct kvm_userspace_memory_region) { .slot = slot, + .flags = flags, .guest_phys_addr = guest_phys, .memory_size = size, .userspace_addr = (unsigned long)userspace_addr, -- 2.17.1 _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
next prev parent reply other threads:[~2020-04-23 17:39 UTC|newest] Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-04-23 17:38 [PATCH kvmtool v4 0/5] Add CFI flash emulation Andre Przywara 2020-04-23 17:38 ` Andre Przywara 2020-04-23 17:38 ` [PATCH kvmtool v4 1/5] virtio-mmio: Assign IRQ line directly before registering device Andre Przywara 2020-04-23 17:38 ` Andre Przywara 2020-04-24 8:41 ` Will Deacon 2020-04-24 8:41 ` Will Deacon 2020-04-24 8:50 ` André Przywara 2020-04-24 8:50 ` André Przywara 2020-04-24 9:51 ` Will Deacon 2020-04-24 9:51 ` Will Deacon 2020-04-23 17:38 ` [PATCH kvmtool v4 2/5] Add emulation for CFI compatible flash memory Andre Przywara 2020-04-23 17:38 ` Andre Przywara 2020-04-23 17:38 ` [PATCH kvmtool v4 3/5] vfio: Destroy memslot when unmapping the associated VAs Andre Przywara 2020-04-23 17:38 ` Andre Przywara 2020-04-23 17:38 ` Andre Przywara [this message] 2020-04-23 17:38 ` [PATCH kvmtool v4 4/5] memslot: Add support for READONLY mappings Andre Przywara 2020-04-24 8:41 ` Will Deacon 2020-04-24 8:41 ` Will Deacon 2020-04-23 17:38 ` [PATCH kvmtool v4 5/5] cfi-flash: Add support for mapping flash into guest Andre Przywara 2020-04-23 17:38 ` Andre Przywara 2020-04-23 17:55 ` [PATCH kvmtool v4 0/5] Add CFI flash emulation Ard Biesheuvel 2020-04-23 17:55 ` Ard Biesheuvel 2020-04-23 20:43 ` Ard Biesheuvel 2020-04-23 20:43 ` Ard Biesheuvel 2020-04-23 21:31 ` André Przywara 2020-04-23 21:31 ` André Przywara 2020-04-24 6:45 ` Ard Biesheuvel 2020-04-24 6:45 ` Ard Biesheuvel 2020-04-24 12:08 ` André Przywara 2020-04-24 12:08 ` André Przywara 2020-04-24 12:25 ` Ard Biesheuvel 2020-04-24 12:25 ` Ard Biesheuvel 2020-04-24 8:40 ` Will Deacon 2020-04-24 8:40 ` Will Deacon 2020-04-24 17:03 ` Will Deacon 2020-04-24 17:03 ` Will Deacon 2020-04-25 15:16 ` Ard Biesheuvel 2020-04-25 15:16 ` Ard Biesheuvel
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20200423173844.24220-5-andre.przywara@arm.com \ --to=andre.przywara@arm.com \ --cc=Alexandru.Elisei@arm.com \ --cc=ardb@kernel.org \ --cc=julien.thierry.kdev@gmail.com \ --cc=kvm@vger.kernel.org \ --cc=kvmarm@lists.cs.columbia.edu \ --cc=raphael.gault@arm.com \ --cc=sami.mujawar@arm.com \ --cc=will@kernel.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.