KVM/ARM Implementation
Subsystems (7)
v4 cover KVM/ARM Implementation
The following series implements KVM support for ARM processors, specifically on the Cortex A-15 platform.
The patch series applies to the arm-lpae branch of ARM Ltd's kernel tree. This is Version 4 of the patch series, but the first two versions were reviewed outside of the KVM mailing list. Changes can also be pulled from:
git://git.ncl.cs.columbia.edu/pub/git/linux-kvm-arm kvm-a15-v4
The implementation is broken up into a logical set of patches, the first one containing a skeleton of files, makefile changes, the basic user space interface and KVM architecture specific stubs. Subsequent patches implement parts of the system as listed:
1. Skeleton 2. Identity Mapping for Hyp mode 3. Hypervisor intitalization 4. Hyp mode memory mappings and 2nd stage preparation 5. World-switch implementation and Hyp exception vectors 6. Emulation framework and CP15 emulation 7. Handle guest user memory aborts 8. Handle guest MMIO aborts 9. Handle userspace IRQ/FIQ injection
10. Support guest wait-for-interrupt instructions.
Testing:
Limited testing, but have run GCC inside guest, which compiled a small hellow-world program, which was successfully run. Hardware still unavailable, so all testing has been done on ARM Fast Models.
For a guide on how to set up a testing environment and try out these patches, see:
http://wiki.ncl.cs.columbia.edu/wiki/KVMARM:Guides:Development_Environment
Changes since v3:
- v4 actually works, fully boots a guest
- Support compiling as a module
- Use static inlines instead of macros for vcpu_reg and friends
- Optimize kvm_vcpu_reg function
- Use Ftrace for trace capabilities
- Updated documentation and commenting
- Use KVM_IRQ_LINE instead of KVM_INTERRUPT
- Emulates load/store instructions not supported through HSR syndrome information.
- Frees 2nd stage translation tables on VM teardown
- Handles IRQ/FIQ instructions
- Handles more CP15 accesses
- Support guest WFI calls
- Uses debugfs instead of /proc
- Support compiling in Thumb mode
Changes since v2:
- Performs world-switch code
- Maps guest memory using 2nd stage translation
- Emulates co-processor 15 instructions
- Forwards I/O faults to QEMU.
---
Christoffer Dall (10):
ARM: KVM: Initial skeleton to compile KVM support
ARM: KVM: Hypervisor identity mapping
ARM: KVM: Add hypervisor inititalization
ARM: KVM: Memory virtualization setup
ARM: KVM: Inject IRQs and FIQs from userspace
ARM: KVM: World-switch implementation
ARM: KVM: Emulation framework and CP15 emulation
ARM: KVM: Handle guest faults in KVM
ARM: KVM: Handle I/O aborts
ARM: KVM: Guest wait-for-interrupts (WFI) support
Documentation/kvm/api.txt | 11 arch/arm/Kconfig | 2 arch/arm/Makefile | 1 arch/arm/include/asm/kvm.h | 75 +++ arch/arm/include/asm/kvm_arm.h | 130 +++++ arch/arm/include/asm/kvm_asm.h | 51 ++ arch/arm/include/asm/kvm_emulate.h | 100 ++++ arch/arm/include/asm/kvm_host.h | 105 ++++ arch/arm/include/asm/kvm_mmu.h | 46 ++ arch/arm/include/asm/kvm_para.h | 9 arch/arm/include/asm/pgtable-3level-hwdef.h | 6 arch/arm/include/asm/pgtable-3level.h | 9 arch/arm/include/asm/pgtable.h | 15 + arch/arm/include/asm/unified.h | 12 arch/arm/kernel/armksyms.c | 6 arch/arm/kernel/asm-offsets.c | 33 + arch/arm/kernel/entry-armv.S | 1 arch/arm/kvm/Kconfig | 44 ++ arch/arm/kvm/Makefile | 18 + arch/arm/kvm/arm.c | 701 +++++++++++++++++++++++++++ arch/arm/kvm/arm_emulate.c | 604 +++++++++++++++++++++++ arch/arm/kvm/arm_exports.c | 26 + arch/arm/kvm/arm_guest.c | 150 ++++++ arch/arm/kvm/arm_init.S | 115 ++++ arch/arm/kvm/arm_interrupts.S | 488 +++++++++++++++++++ arch/arm/kvm/arm_mmu.c | 549 +++++++++++++++++++++ arch/arm/kvm/debug.c | 377 +++++++++++++++ arch/arm/kvm/debug.h | 63 ++ arch/arm/kvm/trace.h | 131 +++++ arch/arm/mach-vexpress/Kconfig | 1 arch/arm/mm/Kconfig | 7 arch/arm/mm/idmap.c | 52 ++ arch/arm/mm/mmu.c | 3 include/linux/kvm.h | 1 mm/memory.c | 1 35 files changed, 3939 insertions(+), 4 deletions(-) create mode 100644 arch/arm/include/asm/kvm.h create mode 100644 arch/arm/include/asm/kvm_arm.h create mode 100644 arch/arm/include/asm/kvm_asm.h create mode 100644 arch/arm/include/asm/kvm_emulate.h create mode 100644 arch/arm/include/asm/kvm_host.h create mode 100644 arch/arm/include/asm/kvm_mmu.h create mode 100644 arch/arm/include/asm/kvm_para.h create mode 100644 arch/arm/kvm/Kconfig create mode 100644 arch/arm/kvm/Makefile create mode 100644 arch/arm/kvm/arm.c create mode 100644 arch/arm/kvm/arm_emulate.c create mode 100644 arch/arm/kvm/arm_exports.c create mode 100644 arch/arm/kvm/arm_guest.c create mode 100644 arch/arm/kvm/arm_init.S create mode 100644 arch/arm/kvm/arm_interrupts.S create mode 100644 arch/arm/kvm/arm_mmu.c create mode 100644 arch/arm/kvm/debug.c create mode 100644 arch/arm/kvm/debug.h create mode 100644 arch/arm/kvm/trace.h
PATCH v4 1/10 ARM: KVM: Initial skeleton to compile KVM support
Targets KVM support for Cortex A-15 processors.
Contains no real functionality but all the framework components, make files, header files and some tracing functionality.
Most functionality is in arch/arm/kvm/* or arch/arm/include/asm/kvm_*.h.
--- arch/arm/Kconfig | 2 arch/arm/Makefile | 1 arch/arm/include/asm/kvm.h | 66 ++++++ arch/arm/include/asm/kvm_asm.h | 28 +++ arch/arm/include/asm/kvm_emulate.h | 91 +++++++++ arch/arm/include/asm/kvm_host.h | 93 +++++++++ arch/arm/include/asm/kvm_para.h | 9 + arch/arm/include/asm/unified.h | 12 + arch/arm/kvm/Kconfig | 44 ++++ arch/arm/kvm/Makefile | 18 ++ arch/arm/kvm/arm.c | 272 ++++++++++++++++++++++++++ arch/arm/kvm/arm_emulate.c | 121 ++++++++++++ arch/arm/kvm/arm_exports.c | 16 ++ arch/arm/kvm/arm_guest.c | 148 ++++++++++++++ arch/arm/kvm/arm_init.S | 17 ++ arch/arm/kvm/arm_interrupts.S | 17 ++ arch/arm/kvm/arm_mmu.c | 15 + arch/arm/kvm/debug.c | 377 ++++++++++++++++++++++++++++++++++++ arch/arm/kvm/debug.h | 63 ++++++ arch/arm/kvm/trace.h | 52 +++++ arch/arm/mach-vexpress/Kconfig | 1 arch/arm/mm/Kconfig | 7 + 22 files changed, 1470 insertions(+), 0 deletions(-) create mode 100644 arch/arm/include/asm/kvm.h create mode 100644 arch/arm/include/asm/kvm_asm.h create mode 100644 arch/arm/include/asm/kvm_emulate.h create mode 100644 arch/arm/include/asm/kvm_host.h create mode 100644 arch/arm/include/asm/kvm_para.h create mode 100644 arch/arm/kvm/Kconfig create mode 100644 arch/arm/kvm/Makefile create mode 100644 arch/arm/kvm/arm.c create mode 100644 arch/arm/kvm/arm_emulate.c create mode 100644 arch/arm/kvm/arm_exports.c create mode 100644 arch/arm/kvm/arm_guest.c create mode 100644 arch/arm/kvm/arm_init.S create mode 100644 arch/arm/kvm/arm_interrupts.S create mode 100644 arch/arm/kvm/arm_mmu.c create mode 100644 arch/arm/kvm/debug.c create mode 100644 arch/arm/kvm/debug.h create mode 100644 arch/arm/kvm/trace.h
arch/arm/Kconfig +2 -0
@@ -2049,3 +2049,5 @@ source "security/Kconfig" source "crypto/Kconfig" source "lib/Kconfig" + +source "arch/arm/kvm/Kconfig"
arch/arm/Makefile +1 -0
@@ -258,6 +258,7 @@ core-$(CONFIG_VFP) += arch/arm/vfp/ # If we have a machine-specific directory, then include it in the build. core-y += arch/arm/kernel/ arch/arm/mm/ arch/arm/common/ +core-y += arch/arm/kvm/ core-y += $(machdirs) $(platdirs) drivers-$(CONFIG_OPROFILE) += arch/arm/oprofile/
arch/arm/include/asm/kvm.h new file +66 -0
66 lines not on this page · raw message
arch/arm/include/asm/kvm_asm.h new file +28 -0
@@ -0,0 +1,28 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License, version 2, as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#ifndef __ARM_KVM_ASM_H__ +#define __ARM_KVM_ASM_H__ + +#define ARM_EXCEPTION_RESET 0 +#define ARM_EXCEPTION_UNDEFINED 1 +#define ARM_EXCEPTION_SOFTWARE 2 +#define ARM_EXCEPTION_PREF_ABORT 3 +#define ARM_EXCEPTION_DATA_ABORT 4 +#define ARM_EXCEPTION_IRQ 5 +#define ARM_EXCEPTION_FIQ 6 + +#endif /* __ARM_KVM_ASM_H__ */
arch/arm/include/asm/kvm_emulate.h new file +91 -0
91 lines not on this page · raw message
arch/arm/include/asm/kvm_host.h new file +93 -0
93 lines not on this page · raw message
arch/arm/include/asm/kvm_para.h new file +9 -0
@@ -0,0 +1,9 @@ +#ifndef _ASM_X86_KVM_PARA_H +#define _ASM_X86_KVM_PARA_H + +static inline unsigned int kvm_arch_para_features (void ) +{ +return 0 ; +} + +#endif /* _ASM_X86_KVM_PARA_H */
arch/arm/include/asm/unified.h +12 -0
@@ -54,6 +54,18 @@#endif /* CONFIG_THUMB2_KERNEL */ +#ifdef CONFIG_KVM_ARM_HOST +#ifdef __ASSEMBLY__ +.arch_extension sec +.arch_extension virt +#else +__asm__( +" .arch_extension sec\n" +" .arch_extension virt\n" +); +#endif +#endif +#ifndef CONFIG_ARM_ASM_UNIFIED /*
arch/arm/kvm/Kconfig new file +44 -0
44 lines not on this page · raw message
arch/arm/kvm/Makefile new file +18 -0
@@ -0,0 +1,18 @@ +# +# Makefile for Kernel-based Virtual Machine module +# + +ccflags-y += -Ivirt/kvm -Iarch/arm/kvm +CFLAGS_arm.o := -I. +CFLAGS_arm_mmu.o := -I. + +EXTRA_CFLAGS += -Ivirt/kvm -Iarch/arm/kvm +AFLAGS_arm_interrupts.o := -I$(obj) + +obj-$(CONFIG_KVM_ARM_HOST) += arm_init.o arm_interrupts.o arm_exports.o + +kvm-arm-y += $(addprefix ../../../virt/kvm/, kvm_main.o coalesced_mmio.o) + +kvm-arm-y += arm.o arm_guest.o arm_mmu.o arm_emulate.o debug.o + +obj-$(CONFIG_KVM) += kvm-arm.o
arch/arm/kvm/arm.c new file +272 -0
272 lines not on this page · raw message
arch/arm/kvm/arm_emulate.c new file +121 -0
121 lines not on this page · raw message
arch/arm/kvm/arm_exports.c new file +16 -0
@@ -0,0 +1,16 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License, version 2, as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include <linux/module.h>
arch/arm/kvm/arm_guest.c new file +148 -0
148 lines not on this page · raw message
arch/arm/kvm/arm_init.S new file +17 -0
@@ -0,0 +1,17 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +#include <asm/asm-offsets.h> +#include <asm/kvm_asm.h>
arch/arm/kvm/arm_interrupts.S new file +17 -0
@@ -0,0 +1,17 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ +#include <asm/asm-offsets.h> +#include <asm/kvm_asm.h>
arch/arm/kvm/arm_mmu.c new file +15 -0
@@ -0,0 +1,15 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License, version 2, as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/
arch/arm/kvm/debug.c new file +377 -0
377 lines not on this page · raw message
arch/arm/kvm/debug.h new file +63 -0
63 lines not on this page · raw message
arch/arm/kvm/trace.h new file +52 -0
52 lines not on this page · raw message
arch/arm/mach-vexpress/Kconfig +1 -0
@@ -36,6 +36,7 @@ config ARCH_VEXPRESS_CA15X4 bool "Versatile Express Cortex-A15x4 tile" depends on VEXPRESS_EXTENDED_MEMORY_MAP select CPU_V7 + select ARM_VIRT_EXT select ARM_GIC select HAVE_ARCH_TIMERS
arch/arm/mm/Kconfig +7 -0
@@ -636,6 +636,13 @@ config ARM_LPAE Say Y if you have an ARMv7 processor supporting the LPAE page table format and you would like to access memory beyond the 4GB limit. +config ARM_VIRT_EXT + bool "Support for ARM Virtualization Extensions" + depends on ARM_LPAE + help + Say Y if you have an ARMv7 processor supporting the ARM hardware + Virtualization extensions. + config ARCH_PHYS_ADDR_T_64BIT def_bool ARM_LPAE
PATCH v4 2/10 ARM: KVM: Hypervisor identity mapping
In reply to Christoffer DallAdds support in the identity mapping feature that allows KVM to setup identity mapping for the Hyp mode with the AP[1] bit set as required by the specification and also supports freeing created sub pmd's after finished use.
These two functions:
- hyp_identity_mapping_add(pgd, addr, end);
- hyp_identity_mapping_del(pgd, addr, end);
are essentially calls the same function as the non-hyp versions but with a different argument value. KVM calls these functions to setup and teardown the identity mapping used to initialize the hypervisor.
Note, the hyp-version of the _del function actually frees the pmd's pointed to by the pgd as opposed to the non-hyp version which just clears them.
--- arch/arm/include/asm/pgtable-3level-hwdef.h | 1 + arch/arm/include/asm/pgtable.h | 6 +++ arch/arm/mm/idmap.c | 47 ++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 1 deletions(-)
arch/arm/include/asm/pgtable-3level-hwdef.h +1 -0
@@ -49,6 +49,7 @@#endif #define PMD_SECT_AP_WRITE (_AT(pmdval_t, 0))#define PMD_SECT_AP_READ (_AT(pmdval_t, 0)) +#define PMD_SECT_AP1 (_AT(pmdval_t, 1) << 6)#define PMD_SECT_TEX (x) (_AT(pmdval_t, 0))/*
arch/arm/include/asm/pgtable.h +6 -0
@@ -409,6 +409,12 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)void identity_mapping_add (pgd_t *,unsigned long ,unsigned long );void identity_mapping_del (pgd_t *,unsigned long ,unsigned long ); +#ifdef CONFIG_KVM_ARM_HOST +void hyp_identity_mapping_add (pgd_t *,unsigned long ,unsigned long ); +void hyp_identity_mapping_del (pgd_t *pgd,unsigned long addr, +unsigned long end); +#endif +#endif /* !__ASSEMBLY__ */ #endif /* CONFIG_MMU */
arch/arm/mm/idmap.c +46 -1
69 lines not on this page · raw message
Re: [PATCH v4 02/10] ARM: KVM: Hypervisor identity mapping
Quoted 12 lines · Christoffer Dall
Adds support in the identity mapping feature that allows KVM to setup identity mapping for the Hyp mode with the AP[1] bit set as required by the specification and also supports freeing created sub pmd's after finished use.
These two functions:
-hyp_identity_mapping_add(pgd, addr, end);
-hyp_identity_mapping_del(pgd, addr, end);
are essentially calls the same function as the non-hyp versions but with a different argument value. KVM calls these functions to setup and teardown the identity mapping used to initialize the hypervisor.
Quoted 5 lines
Note, the hyp-version of the
_delfunction actually frees the pmd's pointed to by the pgd as opposed to the non-hyp version which just clears them.
These are for mapping host memory, not guest memory, right?
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v4 02/10] ARM: KVM: Hypervisor identity mapping
Quoted 13 lines · Avi Kivity
Quoted 12 lines · Christoffer Dall
Adds support in the identity mapping feature that allows KVM to setup identity mapping for the Hyp mode with the AP[1] bit set as required by the specification and also supports freeing created sub pmd's after finished use.
These two functions:
-hyp_identity_mapping_add(pgd, addr, end);
-hyp_identity_mapping_del(pgd, addr, end);
are essentially calls the same function as the non-hyp versions but with a different argument value. KVM calls these functions to setup and teardown the identity mapping used to initialize the hypervisor.
Quoted 7 lines
Quoted 5 lines
Note, the hyp-version of the
_delfunction actually frees the pmd's pointed to by the pgd as opposed to the non-hyp version which just clears them.These are for mapping host memory, not guest memory, right?
yes (or to be exact - hypervisor memory). The point is that there are special hardware requirements for translation tables used in Hyp-mode not otherwise satisfied by the normal page tables.
Quoted 4 lines
Signature · 2 lines
-- error compiling committee.c: too many arguments to function
Re: [Android-virt] [PATCH v4 02/10] ARM: KVM: Hypervisor identity mapping
Hi Christoffer,
Quoted 4 lines
Quoted 2 lines
These are for mapping host memory, not guest memory, right?
yes (or to be exact - hypervisor memory). The point is that there are special hardware requirements for translation tables used in Hyp-mode not otherwise satisfied by the normal page tables.
In function init_hyp_memory() you map some memory regions for vectors, vcpu, stack, etc. using function create_hyp_mappings. Just wondering, how do you make sure that guest will never map its own data into these addresses? Since guest is not para-virtualized, it can use any VA it wants, including these addresses.
In your earlier KVM-arm paper you mentioned that such mappings were
write-protected, so whenever guest tried to access them you needed to relocate such shared pages. Is the mechanism the same or you somehow take advantage of virtualization extensions to avoid this problem?
Thanks,
Alexey
Re: [Android-virt] [PATCH v4 02/10] ARM: KVM: Hypervisor identity mapping
Quoted 13 lines · Alexey Smirnov
Hi Christoffer,
Quoted 4 lines
Quoted 2 lines
These are for mapping host memory, not guest memory, right?
yes (or to be exact - hypervisor memory). The point is that there are special hardware requirements for translation tables used in Hyp-mode not otherwise satisfied by the normal page tables.
In function
init_hyp_memory()you map some memory regions for vectors, vcpu, stack, etc. using functioncreate_hyp_mappings. Just wondering, how do you make sure that guest will never map its own data into these addresses? Since guest is not para-virtualized, it can use any VA it wants, including these addresses.
Quoted 4 lines
In your earlier KVM-arm paper you mentioned that such mappings were write-protected, so whenever guest tried to access them you needed to relocate such shared pages. Is the mechanism the same or you somehow take advantage of virtualization extensions to avoid this problem?
I take advantage of the virtualization extensions. These mappings are used only in Hyp-mode, which is only used to handle exceptions from the guest and to perform world-switches. Thus, these mappings are completely orthogonal to the 2nd stage translations used when running the VM.
Re: [PATCH v4 02/10] ARM: KVM: Hypervisor identity mapping
In reply to Avi KivityQuoted 13 lines · Avi Kivity
Quoted 12 lines · Christoffer Dall
Adds support in the identity mapping feature that allows KVM to setup identity mapping for the Hyp mode with the AP[1] bit set as required by the specification and also supports freeing created sub pmd's after finished use.
These two functions:
-hyp_identity_mapping_add(pgd, addr, end);
-hyp_identity_mapping_del(pgd, addr, end);
are essentially calls the same function as the non-hyp versions but with a different argument value. KVM calls these functions to setup and teardown the identity mapping used to initialize the hypervisor.
Quoted 7 lines
Quoted 5 lines
Note, the hyp-version of the
_delfunction actually frees the pmd's pointed to by the pgd as opposed to the non-hyp version which just clears them.These are for mapping host memory, not guest memory, right?
Yes. There is some code that is built into the kernel image (and address space) but it needs to run in Hypervisor mode which has its own MMU translation tables.
Signature · 1 line
Catalin
PATCH v4 3/10 ARM: KVM: Add hypervisor inititalization
In reply to Christoffer DallSets up the required registers to run code in HYP-mode from the kernel. No major controversies, but we should consider how to deal with SMP support for hypervisor stack page.
By setting the HVBAR the kernel can execute code in Hyp-mode with the MMU disabled. The HVBAR initially points to initialization code, which initializes other Hyp-mode registers and enables the MMU for Hyp-mode. Afterwards, the HVBAR is changed to point to KVM Hyp vectors used to catch guest faults and to switch to Hyp mode to perform a world-switch into a KVM guest.
Also provides memory mapping code to map required code pages and data structures accessed in Hyp mode at the same virtual address as the host kernel virtual addresses, but which conforms to the architectural requirements for translations in Hyp mode. This interface is added in arch/arm/kvm/arm_mmu.c and is comprised of:
- create_hyp_mappings(hyp_pgd, start, end);
- remove_hyp_mappings(hyp_pgd, start, end);
- free_hyp_pmds(pgd_hyp);
See the implementation for more details.
--- arch/arm/include/asm/kvm_arm.h | 103 +++++++++++++ arch/arm/include/asm/kvm_asm.h | 23 +++ arch/arm/include/asm/kvm_host.h | 1 arch/arm/include/asm/kvm_mmu.h | 40 +++++ arch/arm/include/asm/pgtable-3level-hwdef.h | 5 + arch/arm/include/asm/pgtable.h | 5 + arch/arm/kvm/arm.c | 169 +++++++++++++++++++++ arch/arm/kvm/arm_exports.c | 10 + arch/arm/kvm/arm_init.S | 98 ++++++++++++ arch/arm/kvm/arm_interrupts.S | 30 ++++ arch/arm/kvm/arm_mmu.c | 213 +++++++++++++++++++++++++++ arch/arm/mm/idmap.c | 7 + mm/memory.c | 1 13 files changed, 704 insertions(+), 1 deletions(-) create mode 100644 arch/arm/include/asm/kvm_arm.h create mode 100644 arch/arm/include/asm/kvm_mmu.h
arch/arm/include/asm/kvm_arm.h new file +103 -0
@@ -0,0 +1,103 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License, version 2, as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#ifndef __KVM_ARM_H__ +#define __KVM_ARM_H__ + +#include <asm/types.h> + +/* Hyp Configuration Register (HCR) bits */ +#define HCR_TGE (1 << 27) +#define HCR_TVM (1 << 26) +#define HCR_TTLB (1 << 25) +#define HCR_TPU (1 << 24) +#define HCR_TPC (1 << 23) +#define HCR_TSW (1 << 22) +#define HCR_TAC (1 << 21) +#define HCR_TIDCP (1 << 20) +#define HCR_TSC (1 << 19) +#define HCR_TID3 (1 << 18) +#define HCR_TID2 (1 << 17) +#define HCR_TID1 (1 << 16) +#define HCR_TID0 (1 << 15) +#define HCR_TWE (1 << 14) +#define HCR_TWI (1 << 13) +#define HCR_DC (1 << 12) +#define HCR_BSU (3 << 10) +#define HCR_FB (1 << 9) +#define HCR_VA (1 << 8) +#define HCR_VI (1 << 7) +#define HCR_VF (1 << 6) +#define HCR_AMO (1 << 5) +#define HCR_IMO (1 << 4) +#define HCR_FMO (1 << 3) +#define HCR_PTW (1 << 2) +#define HCR_SWIO (1 << 1) +#define HCR_VM 1 +#define HCR_GUEST_MASK (HCR_TSC | HCR_TWE | HCR_TWI | HCR_VM | HCR_AMO | \ + HCR_AMO | HCR_IMO | HCR_FMO | HCR_SWIO) + +/* Hyp System Control Register (HSCTLR) bits */ +#define HSCTLR_TE (1 << 30) +#define HSCTLR_EE (1 << 25) +#define HSCTLR_FI (1 << 21) +#define HSCTLR_WXN (1 << 19) +#define HSCTLR_I (1 << 12) +#define HSCTLR_C (1 << 2) +#define HSCTLR_A (1 << 1) +#define HSCTLR_M 1 +#define HSCTLR_MASK (HSCTLR_M | HSCTLR_A | HSCTLR_C | HSCTLR_I | \ + HSCTLR_WXN | HSCTLR_FI | HSCTLR_EE | HSCTLR_TE) + +/* TTBCR and HTCR Registers bits */ +#define TTBCR_EAE (1 << 31) +#define TTBCR_IMP (1 << 30) +#define TTBCR_SH1 (3 << 28) +#define TTBCR_ORGN1 (3 << 26) +#define TTBCR_IRGN1 (3 << 24) +#define TTBCR_EPD1 (1 << 23) +#define TTBCR_A1 (1 << 22) +#define TTBCR_T1SZ (3 << 16) +#define TTBCR_SH0 (3 << 12) +#define TTBCR_ORGN0 (3 << 10) +#define TTBCR_IRGN0 (3 << 8) +#define TTBCR_EPD0 (1 << 7) +#define TTBCR_T0SZ 3 +#define HTCR_MASK (TTBCR_T0SZ | TTBCR_IRGN0 | TTBCR_ORGN0 | TTBCR_SH0) + + +/* Virtualization Translation Control Register (VTCR) bits */ +#define VTCR_SH0 (3 << 12) +#define VTCR_ORGN0 (3 << 10) +#define VTCR_IRGN0 (3 << 8) +#define VTCR_SL0 (3 << 6) +#define VTCR_S (1 << 4) +#define VTCR_T0SZ 3 +#define VTCR_MASK (VTCR_SH0 | VTCR_ORGN0 | VTCR_IRGN0 | VTCR_SL0 | \ + VTCR_S | VTCR_T0SZ | VTCR_MASK) +#define VTCR_HTCR_SH (VTCR_SH0 | VTCR_ORGN0 | VTCR_IRGN0) +#define VTCR_SL_L2 0/* Starting-level: 2 */ +#define VTCR_SL_L1 (1 << 6)/* Starting-level: 1 */ +#define VTCR_GUEST_SL VTCR_SL_L1 +#define VTCR_GUEST_T0SZ 0 +#if VTCR_GUEST_SL ==0 +#define VTTBR_X (14 - VTCR_GUEST_T0SZ) +#else +#define VTTBR_X (5 - VTCR_GUEST_T0SZ) +#endif + + +#endif /* __KVM_ARM_H__ */
arch/arm/include/asm/kvm_asm.h +23 -0
@@ -24,5 +24,28 @@#define ARM_EXCEPTION_DATA_ABORT 4#define ARM_EXCEPTION_IRQ 5#define ARM_EXCEPTION_FIQ 6 +#define ARM_EXCEPTION_HVC 7 + +/* +* SMC Hypervisor API call numbers +*/ +#ifdef __ASSEMBLY__ +.equ SMCHYP_HVBAR_W, 0xfffffff0 +#else /* !__ASSEMBLY__ */ +asm(".equ SMCHYP_HVBAR_W, 0xfffffff0" ); +#endif /* __ASSEMBLY__ */ + +#ifndef __ASSEMBLY__ +struct kvm_vcpu ; + +extern unsigned long __kvm_hyp_init; +extern unsigned long __kvm_hyp_init_end; + +extern unsigned long __kvm_hyp_vector; +extern unsigned long __kvm_hyp_vector_end; + +extern int __kvm_vcpu_run (struct kvm_vcpu *vcpu); +extern unsigned long __kvm_vcpu_run_end; +#endif #endif /* __ARM_KVM_ASM_H__ */
arch/arm/include/asm/kvm_host.h +1 -0
@@ -31,6 +31,7 @@ struct kvm_vcpu;u32 *kvm_vcpu_reg (struct kvm_vcpu *vcpu,u8 reg_num,u32 mode);struct kvm_arch { +pgd_t *pgd;/* 1-level 2nd stage table */ };#define EXCEPTION_NONE 0
arch/arm/include/asm/kvm_mmu.h new file +40 -0
@@ -0,0 +1,40 @@ +/* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License, version 2, as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#ifndef __ARM_KVM_MMU_H__ +#define __ARM_KVM_MMU_H__ + +/* +* The architecture supports 40-bit IPA as input to the 2nd stage translations +* and PTRS_PER_PGD2 could therefore be 1024. +* +* To save a bit of memory and to avoid alignment issues we assume 39-bit IPA +* for now, but remember that the level-1 table must be aligned to its size. +*/ +#define PTRS_PER_PGD2 512 +#define PGD2_ORDER get_order(PTRS_PER_PGD2 * sizeof(pgd_t)) + +extern pgd_t *kvm_hyp_pgd; + +int create_hyp_mappings (pgd_t *hyp_pgd, +unsigned long start, +unsigned long end); +void remove_hyp_mappings (pgd_t *hyp_pgd, +unsigned long start, +unsigned long end); +void free_hyp_pmds (pgd_t *hyp_pgd); + +#endif /* __ARM_KVM_MMU_H__ */
arch/arm/include/asm/pgtable-3level-hwdef.h +5 -0
@@ -32,6 +32,9 @@#define PMD_TYPE_SECT (_AT(pmdval_t, 1) << 0)#define PMD_BIT4 (_AT(pmdval_t, 0))#define PMD_DOMAIN (x) (_AT(pmdval_t, 0)) +#define PMD_APTABLE_SHIFT (61) +#define PMD_APTABLE (_AT(pgdval_t, 3) << PGD_APTABLE_SHIFT) +#define PMD_PXNTABLE (_AT(pgdval_t, 1) << 59)/* * - section
@@ -44,8 +47,10 @@#ifdef __ASSEMBLY__/* avoid 'shift count out of range' warning */ #define PMD_SECT_XN (0) +#define PMD_SECT_PXN (0)#else #define PMD_SECT_XN ((pmdval_t)1 << 54) +#define PMD_SECT_PXN ((pmdval_t)1 << 53)#endif #define PMD_SECT_AP_WRITE (_AT(pmdval_t, 0))#define PMD_SECT_AP_READ (_AT(pmdval_t, 0))
arch/arm/include/asm/pgtable.h +5 -0
@@ -88,6 +88,7 @@ extern pgprot_t pgprot_kernel;#define PAGE_READONLY_EXEC _MOD_PROT(pgprot_user, L_PTE_USER | L_PTE_RDONLY)#define PAGE_KERNEL _MOD_PROT(pgprot_kernel, L_PTE_XN)#define PAGE_KERNEL_EXEC pgprot_kernel +#define PAGE_HYP _MOD_PROT(pgprot_kernel, L_PTE_USER)#define __PAGE_NONE __pgprot(_L_PTE_DEFAULT | L_PTE_RDONLY | L_PTE_XN)#define __PAGE_SHARED __pgprot(_L_PTE_DEFAULT | L_PTE_USER | L_PTE_XN)
@@ -223,6 +224,10 @@ static inline pmd_t *pmd_offset(pud_t *pud, unsigned long addr)#ifdef CONFIG_ARM_LPAE #define pmd_bad (pmd) (!(pmd_val(pmd) & 2)) +#define pmd_table (pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \ + PMD_TYPE_TABLE) +#define pmd_sect (pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \ + PMD_TYPE_SECT)#define copy_pmd (pmdpd,pmdps) \ do { \
arch/arm/kvm/arm.c +169 -0
191 lines not on this page · raw message
arch/arm/kvm/arm_exports.c +10 -0
@@ -14,3 +14,13 @@*/ #include <linux/module.h> +#include <asm/kvm_asm.h> + +EXPORT_SYMBOL_GPL (__kvm_hyp_init); +EXPORT_SYMBOL_GPL (__kvm_hyp_init_end); + +EXPORT_SYMBOL_GPL (__kvm_hyp_vector); +EXPORT_SYMBOL_GPL (__kvm_hyp_vector_end); + +EXPORT_SYMBOL_GPL (__kvm_vcpu_run); +EXPORT_SYMBOL_GPL (__kvm_vcpu_run_end);
arch/arm/kvm/arm_init.S +98 -0
103 lines not on this page · raw message
arch/arm/kvm/arm_interrupts.S +30 -0
@@ -13,5 +13,35 @@ * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * */ + +#include <linux/linkage.h> +#include <asm/unified.h> +#include <asm/page.h> #include <asm/asm-offsets.h> #include <asm/kvm_asm.h> +#include <asm/kvm_arm.h> + +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@ Hypervisor world-switch code +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + + .text + .arm + +ENTRY(__kvm_vcpu_run) +THUMB( orr lr, lr, #1) + mov pc, lr +__kvm_vcpu_run_end: + .globl __kvm_vcpu_run_end + + +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@ Hypervisor exception vector and handlers +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ + + .align 5 +__kvm_hyp_vector: + .globl __kvm_hyp_vector + nop +__kvm_hyp_vector_end: + .globl __kvm_hyp_vector_end
arch/arm/kvm/arm_mmu.c +213 -0
216 lines not on this page · raw message
arch/arm/mm/idmap.c +6 -1
@@ -1,5 +1,6 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/module.h> #include <linux/kernel.h> #include <asm/cputype.h>
@@ -121,12 +122,15 @@ void hyp_identity_mapping_add(pgd_t *pgd, unsigned long addr, unsigned long end) {__identity_mapping_add (pgd, addr, end, true); } +EXPORT_SYMBOL_GPL (hyp_identity_mapping_add);static void hyp_idmap_del_pmd (pgd_t *pgd,unsigned long addr) { +pud_t *pud;pmd_t *pmd; - pmd =pmd_offset (pgd, addr); + pud =pud_offset (pgd, addr); + pmd =pmd_offset (pud, addr);pmd_free (NULL , pmd); }
@@ -148,6 +152,7 @@ void hyp_identity_mapping_del(pgd_t *pgd, unsigned long addr, unsigned long end) } }while (addr = next, addr < end); } +EXPORT_SYMBOL_GPL (hyp_identity_mapping_del);#endif /*
mm/memory.c +1 -0
@@ -204,6 +204,7 @@ void pgd_clear_bad(pgd_t *pgd)pgd_ERROR (*pgd);pgd_clear (pgd); } +EXPORT_SYMBOL_GPL (pgd_clear_bad);void pud_clear_bad (pud_t *pud) {
PATCH v4 4/10 ARM: KVM: Memory virtualization setup
In reply to Christoffer DallThis commit introduces the framework for guest memory management through the use of 2nd stage translation. Each VM has a pointer to a level-1 tabled (the pgd field in struct kvm_arch) which is used for the 2nd stage translations. Entries are added when handling guest faults (later patch) and the table itself can be allocated and freed through the following functions implemented in arch/arm/kvm/arm_mmu.c:
- kvm_alloc_stage2_pgd(struct kvm *kvm);
- kvm_free_stage2_pgd(struct kvm *kvm);
Further, each entry in TLBs and caches are tagged with a VMID identifier in addition to ASIDs. The VMIDs are managed using a bitmap and assigned when creating the VM in kvm_arch_init_vm() where the 2nd stage pgd is also allocated. The table is freed in kvm_arch_destroy_vm(). Both functions are called from the main KVM code.
--- arch/arm/include/asm/kvm_host.h | 4 ++ arch/arm/include/asm/kvm_mmu.h | 5 +++ arch/arm/kvm/arm.c | 62 ++++++++++++++++++++++++++++++++++- arch/arm/kvm/arm_mmu.c | 69 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+), 2 deletions(-)
arch/arm/include/asm/kvm_host.h +3 -1
@@ -31,7 +31,9 @@ struct kvm_vcpu;u32 *kvm_vcpu_reg (struct kvm_vcpu *vcpu,u8 reg_num,u32 mode);struct kvm_arch { -pgd_t *pgd;/* 1-level 2nd stage table */ +u32 vmid;/* The VMID used for the virt. memory system */ +pgd_t *pgd;/* 1-level 2nd stage table */ +u64 vttbr;/* VTTBR value associated with above pgd and vmid */ };#define EXCEPTION_NONE 0
arch/arm/include/asm/kvm_mmu.h +5 -0
@@ -37,4 +37,9 @@ void remove_hyp_mappings(pgd_t *hyp_pgd, unsigned long end);void free_hyp_pmds (pgd_t *hyp_pgd); +int kvm_alloc_stage2_pgd (struct kvm *kvm); +void kvm_free_stage2_pgd (struct kvm *kvm); + +int kvm_handle_guest_abort (struct kvm_vcpu *vcpu,struct kvm_run *run); +#endif /* __ARM_KVM_MMU_H__ */
arch/arm/kvm/arm.c +61 -1
90 lines not on this page · raw message
arch/arm/kvm/arm_mmu.c +69 -0
75 lines not on this page · raw message
Re: [PATCH v4 04/10] ARM: KVM: Memory virtualization setup
Quoted 28 lines · Christoffer Dall
This commit introduces the framework for guest memory management through the use of 2nd stage translation. Each VM has a pointer to a level-1 tabled (the pgd field in struct
kvm_arch) which is used for the 2nd stage translations. Entries are added when handling guest faults (later patch) and the table itself can be allocated and freed through the following functions implemented inarch/arm/kvm/arm_mmu.c:
-kvm_alloc_stage2_pgd(struct kvm *kvm);
-kvm_free_stage2_pgd(struct kvm *kvm);Further, each entry in TLBs and caches are tagged with a VMID identifier in addition to ASIDs. The VMIDs are managed using a bitmap and assigned when creating the VM in
kvm_arch_init_vm()where the 2nd stage pgd is also allocated. The table is freed inkvm_arch_destroy_vm(). Both functions are called from the main KVM code.arch/arm/kvm/arm.c +7
-1+/** +* kvm_arch_init_vm - initializes a VM data structure +* @kvm: pointer to the KVM struct +*/ int kvm_arch_init_vm (struct kvm *kvm) { -return 0 ; +int ret =0 ; +phys_addr_t pgd_phys; +unsigned long vmid;
arch/arm/kvm/arm.c void kvm_arch_sync_events(struct kvm *kvm) · line 93
Quoted 10 lines
arch/arm/kvm/arm.c +10
-0+unsigned long start, end; + + +mutex_lock (&kvm_vmids_mutex); + vmid =find_first_zero_bit (kvm_vmids,VMID_SIZE ); +if (vmid>=VMID_SIZE ) { +mutex_unlock (&kvm_vmids_mutex); +return -EBUSY ; + } +__set_bit (vmid, kvm_vmids);
VMID_SIZE seems to be a bit low for comfort. I guess it's fine for a start, but later on we'll have to recycle VMIDs, like we do for SVM ASIDs.
Is there not a risk of a user starting 255 tiny guests and denying other users the ability to use kvm?
Quoted 25 lines
arch/arm/kvm/arm.c +23
-0+ kvm->arch.vmid = vmid; +mutex_unlock (&kvm_vmids_mutex); + + ret =kvm_alloc_stage2_pgd (kvm); +if (ret) + goto out_fail_alloc; + + pgd_phys =virt_to_phys (kvm->arch.pgd); + kvm->arch.vttbr = pgd_phys& ((1LLU <<40 ) -1 )& ~((2 <<VTTBR_X ) -1 ); + kvm->arch.vttbr |= ((u64 )vmid<<48 ); + + start = (unsigned long )kvm, + end = start +sizeof (struct kvm ); + ret =create_hyp_mappings (kvm_hyp_pgd, start, end); +if (ret) + goto out_fail_hyp_mappings; + +return ret; +out_fail_hyp_mappings: +remove_hyp_mappings (kvm_hyp_pgd, start, end); +out_fail_alloc: +clear_bit (vmid, kvm_vmids); +return ret; }
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [Android-virt] [PATCH v4 04/10] ARM: KVM: Memory virtualization setup
Quoted 43 lines · Avi Kivity
Quoted 28 lines · Christoffer Dall
This commit introduces the framework for guest memory management through the use of 2nd stage translation. Each VM has a pointer to a level-1 tabled (the pgd field in struct
kvm_arch) which is used for the 2nd stage translations. Entries are added when handling guest faults (later patch) and the table itself can be allocated and freed through the following functions implemented inarch/arm/kvm/arm_mmu.c:
-kvm_alloc_stage2_pgd(struct kvm *kvm);
-kvm_free_stage2_pgd(struct kvm *kvm);Further, each entry in TLBs and caches are tagged with a VMID identifier in addition to ASIDs. The VMIDs are managed using a bitmap and assigned when creating the VM in
kvm_arch_init_vm()where the 2nd stage pgd is also allocated. The table is freed inkvm_arch_destroy_vm(). Both functions are called from the main KVM code.arch/arm/kvm/arm.c +7
-1+/** +* kvm_arch_init_vm - initializes a VM data structure +* @kvm: pointer to the KVM struct +*/ int kvm_arch_init_vm (struct kvm *kvm) { -return 0 ; +int ret =0 ; +phys_addr_t pgd_phys; +unsigned long vmid;arch/arm/kvm/arm.c void kvm_arch_sync_events(struct kvm *kvm) · line 93
Quoted 10 lines
arch/arm/kvm/arm.c +10
-0+unsigned long start, end; + + +mutex_lock (&kvm_vmids_mutex); + vmid =find_first_zero_bit (kvm_vmids,VMID_SIZE ); +if (vmid>=VMID_SIZE ) { +mutex_unlock (&kvm_vmids_mutex); +return -EBUSY ; + } +__set_bit (vmid, kvm_vmids);
VMID_SIZEseems to be a bit low for comfort. I guess it's fine for a start, but later on we'll have to recycle VMIDs, like we do for SVM ASIDs.
Quoted 2 lines
Is there not a risk of a user starting 255 tiny guests and denying other users the ability to use kvm?
yes there absolutely is, if that's a valid use case. I wanted something simple for now, but completely agree that with ARM machines with TBs of memory, we need some more VMIDs. I will incorporate it into the next patch series.
Quoted 34 lines
Quoted 26 lines
arch/arm/kvm/arm.c +23
-0+ kvm->arch.vmid = vmid; +mutex_unlock (&kvm_vmids_mutex); + + ret =kvm_alloc_stage2_pgd (kvm); +if (ret) + goto out_fail_alloc; + + pgd_phys =virt_to_phys (kvm->arch.pgd); + kvm->arch.vttbr = pgd_phys& ((1LLU <<40 ) -1 )& ~((2 <<VTTBR_X ) -1 ); + kvm->arch.vttbr |= ((u64 )vmid<<48 ); + + start = (unsigned long )kvm, + end = start +sizeof (struct kvm ); + ret =create_hyp_mappings (kvm_hyp_pgd, start, end); +if (ret) + goto out_fail_hyp_mappings; + +return ret; +out_fail_hyp_mappings: +remove_hyp_mappings (kvm_hyp_pgd, start, end); +out_fail_alloc: +clear_bit (vmid, kvm_vmids); +return ret; }Signature · 6 lines
error compiling committee.c: too many arguments to function _______________________________________________ Android-virt mailing list [email protected] https://lists.cs.columbia.edu/cucslists/listinfo/android-virt
PATCH v4 5/10 ARM: KVM: Inject IRQs and FIQs from userspace
In reply to Christoffer DallUserspace can inject IRQs and FIQs through the KVM_IRQ_LINE VM ioctl. This ioctl is used since the sematics are in fact two lines that can be either raised or lowered on the VCPU - the IRQ and FIQ lines.
KVM needs to know which VCPU it must operate on and whether the FIQ or IRQ line is raised/lowered. Hence both pieces of information is packed in the kvm_irq_level->irq field. The irq fild value will be:
IRQ: vcpu_index * 2
FIQ: (vcpu_index * 2) + 1
This is documented in Documentation/kvm/api.txt.
The effect of the ioctl is simply to simply raise/lower the corresponding virt_irq field on the VCPU struct, which will cause the world-switch code to raise/lower virtual interrupts when running the guest on next switch. The wait_for_interrupt flag is also cleared for raised IRQs causing an idle VCPU to become active again.
--- Documentation/kvm/api.txt | 11 ++++++-- arch/arm/include/asm/kvm.h | 8 ++++++ arch/arm/include/asm/kvm_arm.h | 1 + arch/arm/kvm/arm.c | 54 +++++++++++++++++++++++++++++++++++++++- arch/arm/kvm/trace.h | 21 ++++++++++++++++ include/linux/kvm.h | 1 + 6 files changed, 91 insertions(+), 5 deletions(-)
Documentation/kvm/api.txt +8 -3
@@ -534,15 +534,20 @@ only go to the IOAPIC. On ia64, a IOSAPIC is created. 4.25 KVM_IRQ_LINE Capability: KVM_CAP_IRQCHIP -Architectures: x86, ia64 +Architectures: x86, ia64, arm Type: vm ioctl Parameters: struct kvm_irq_level Returns: 0 on success, -1 on error Sets the level of a GSI input to the interrupt controller model in the kernel. Requires that an interrupt controller model has been previously created with -KVM_CREATE_IRQCHIP. Note that edge-triggered interrupts require the level -to be set to 1 and then back to 0. +KVM_CREATE_IRQCHIP (except for ARM). Note that edge-triggered interrupts +require the level to be set to 1 and then back to 0. + +ARM uses two types of interrupt lines per CPU, ie. IRQ and FIQ. The value of the +irq field should be (VCPU_INDEX * 2) for IRQs and ((VCPU_INDEX * 2) + 1) for +FIQs. Level is used to raise/lower the line. See arch/arm/include/asm/kvm.h for +convenience macros. struct kvm_irq_level { union {
arch/arm/include/asm/kvm.h +8 -0
@@ -20,6 +20,14 @@#include <asm/types.h> /* +* KVM_IRQ_LINE macros to set/read IRQ/FIQ for specific VCPU index. +*/ +enum KVM_ARM_IRQ_LINE_TYPE { +KVM_ARM_IRQ_LINE =0 , +KVM_ARM_FIQ_LINE =1 , +}; + +/* * Modes used for short-hand mode determinition in the world-switch code and * in emulation code. *
arch/arm/include/asm/kvm_arm.h +1 -0
@@ -49,6 +49,7 @@#define HCR_VM 1#define HCR_GUEST_MASK (HCR_TSC | HCR_TWE | HCR_TWI | HCR_VM | HCR_AMO | \ HCR_AMO | HCR_IMO | HCR_FMO | HCR_SWIO) +#define HCR_VIRT_EXCP_MASK (HCR_VA | HCR_VI | HCR_VF)/* Hyp System Control Register (HSCTLR) bits */ #define HSCTLR_TE (1 << 30)
arch/arm/kvm/arm.c +52 -2
66 lines not on this page · raw message
arch/arm/kvm/trace.h +21 -0
@@ -40,6 +40,27 @@ TRACE_EVENT(kvm_exit, ); +TRACE_EVENT (kvm_irq_line, + TP_PROTO(unsigned int type, unsigned int level, unsigned int vcpu_idx), +TP_ARGS (type, level, vcpu_idx), + +TP_STRUCT__entry ( + __field( unsigned int, type ) + __field( unsigned int, level ) + __field( unsigned int, vcpu_idx ) + ), + +TP_fast_assign ( + __entry->type = type; + __entry->level = level; + __entry->vcpu_idx = vcpu_idx; + ), + +TP_printk ("KVM_IRQ_LINE: type: %s, level: %u, vcpu: %u" , + (__entry->type ==KVM_ARM_IRQ_LINE ) ?"IRQ" :"FIQ" , + __entry->level, __entry->vcpu_idx) +); +#endif /* _TRACE_KVM_H */
include/linux/kvm.h +1 -0
@@ -111,6 +111,7 @@ struct kvm_irq_level {* ACPI gsi notion of irq. * For IA-64 (APIC model) IOAPIC0: irq 0-23; IOAPIC1: irq 24-47.. * For X86 (standard AT mode) PIC0/1: irq 0-15. IOAPIC0: 0-23.. +* For ARM: IRQ: irq = (2*vcpu_index). FIQ: irq = (2*vcpu_indx + 1). */ union {__u32 irq;
Re: [PATCH v4 05/10] ARM: KVM: Inject IRQs and FIQs from userspace
Quoted 10 lines · Christoffer Dall
Userspace can inject IRQs and FIQs through the
KVM_IRQ_LINEVM ioctl. This ioctl is used since the sematics are in fact two lines that can be either raised or lowered on the VCPU - the IRQ and FIQ lines.KVM needs to know which VCPU it must operate on and whether the FIQ or IRQ line is raised/lowered. Hence both pieces of information is packed in the
kvm_irq_level->irqfield. The irq fild value will be:
IRQ:vcpu_index* 2
FIQ: (vcpu_index* 2) + 1
Quoted 7 lines
This is documented in
Documentation/kvm/api.txt.The effect of the ioctl is simply to simply raise/lower the corresponding
virt_irqfield on the VCPU struct, which will cause the world-switch code to raise/lower virtual interrupts when running the guest on next switch. Thewait_for_interruptflag is also cleared for raised IRQs causing an idle VCPU to become active again.
Note x86 starts out with a default configuration and allows updating it via KVM_SET_GSI_ROUTING. You may need this in the future if you decide to implement an irq controller in the kernel.
Quoted 17 lines
arch/arm/kvm/arm.c +17
-0+static int kvm_arch_vm_ioctl_irq_line (struct kvm *kvm, +struct kvm_irq_level *irq_level) +{ +u32 mask; +unsigned int vcpu_idx; +struct kvm_vcpu *vcpu; + + vcpu_idx = irq_level->irq /2 ; +if (vcpu_idx>=KVM_MAX_VCPUS ) +return -EINVAL ; + + vcpu =kvm_get_vcpu (kvm, vcpu_idx); +if (!vcpu) +return -EINVAL ; + +switch (irq_level->irq %2 ) { +case KVM_ARM_IRQ_LINE :
arch/arm/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 326
Quoted 10 lines
arch/arm/kvm/arm.c +10
-0+ mask =HCR_VI ; +break ; +case KVM_ARM_FIQ_LINE : + mask =HCR_VF ; +break ; +default : +return -EINVAL ; + } + +trace_kvm_irq_line (irq_level->irq %2 , irq_level->level, vcpu_idx);
Please reuse trace_kvm_set_irq(). You can decode vcpu/type in a trace-cmd plugin.
arch/arm/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 333
Quoted 7 lines
arch/arm/kvm/arm.c +7
-0+ +if (irq_level->level) { + vcpu->arch.virt_irq |= mask; + vcpu->arch.wait_for_interrupts =0 ; + }else + vcpu->arch.virt_irq&= ~mask; +
This seems to be non-smp-safe? Do you need atomic ops and barriers here? And a wakeup?
Unlike KVM_INTERRUPT, KVM_IRQ_LINE is designed to be used asynchronously wrt the vcpu.
Quoted 15 lines
arch/arm/kvm/arm.c +6
-2+return 0 ; +} +long kvm_arch_vcpu_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg) {@@ -312,8 +349,21 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)long kvm_arch_vm_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg) { -printk (KERN_ERR "kvm_arch_vm_ioctl: Unsupported ioctl (%d)\n" , ioctl); -return -EINVAL ; +struct kvm *kvm = filp->private_data; +void __user *argp = (void __user *)arg; +
arch/arm/kvm/arm.c int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) · line 364
Quoted 10 lines
arch/arm/kvm/arm.c +10
-0+switch (ioctl) { +case KVM_IRQ_LINE : { +struct kvm_irq_level irq_event; + +if (copy_from_user (&irq_event, argp,sizeof irq_event)) +return -EFAULT ; +return kvm_arch_vm_ioctl_irq_line (kvm,&irq_event); + } +default : +kvm_err (-EINVAL ,"Unsupported ioctl (%d)" , ioctl);
Please remove for the final code, we don't want a user spamming the kernel log.
Quoted 5 lines
arch/arm/kvm/arm.c +2
-0+return -EINVAL ; + } }
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [Android-virt] [PATCH v4 05/10] ARM: KVM: Inject IRQs and FIQs from userspace
Quoted 19 lines · Avi Kivity
Quoted 10 lines · Christoffer Dall
Userspace can inject IRQs and FIQs through the
KVM_IRQ_LINEVM ioctl. This ioctl is used since the sematics are in fact two lines that can be either raised or lowered on the VCPU - the IRQ and FIQ lines.KVM needs to know which VCPU it must operate on and whether the FIQ or IRQ line is raised/lowered. Hence both pieces of information is packed in the
kvm_irq_level->irqfield. The irq fild value will be:
IRQ:vcpu_index* 2
FIQ: (vcpu_index* 2) + 1Quoted 7 lines
This is documented in
Documentation/kvm/api.txt.The effect of the ioctl is simply to simply raise/lower the corresponding
virt_irqfield on the VCPU struct, which will cause the world-switch code to raise/lower virtual interrupts when running the guest on next switch. Thewait_for_interruptflag is also cleared for raised IRQs causing an idle VCPU to become active again.
Quoted 3 lines
Note x86 starts out with a default configuration and allows updating it via
KVM_SET_GSI_ROUTING. You may need this in the future if you decide to implement an irq controller in the kernel.
Will probably happen some time. Noted.
Quoted 23 lines
Quoted 23 lines
arch/arm/kvm/arm.c +22
-0+static int kvm_arch_vm_ioctl_irq_line (struct kvm *kvm, +struct kvm_irq_level *irq_level) +{ +u32 mask; +unsigned int vcpu_idx; +struct kvm_vcpu *vcpu; + + vcpu_idx = irq_level->irq /2 ; +if (vcpu_idx>=KVM_MAX_VCPUS ) +return -EINVAL ; + + vcpu =kvm_get_vcpu (kvm, vcpu_idx); +if (!vcpu) +return -EINVAL ; + +switch (irq_level->irq %2 ) { +case KVM_ARM_IRQ_LINE : + mask =HCR_VI ; +break ; +case KVM_ARM_FIQ_LINE : + mask =HCR_VF ; +break ;
Quoted 8 lines
arch/arm/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 326
Quoted 5 lines
arch/arm/kvm/arm.c +5
-0+default : +return -EINVAL ; + } + +trace_kvm_irq_line (irq_level->irq %2 , irq_level->level, vcpu_idx);Please reuse
trace_kvm_set_irq(). You can decode vcpu/type in a trace-cmd plugin.
OK
Quoted 11 lines
arch/arm/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 333
Quoted 8 lines
arch/arm/kvm/arm.c +7
-0+ +if (irq_level->level) { + vcpu->arch.virt_irq |= mask; + vcpu->arch.wait_for_interrupts =0 ; + }else + vcpu->arch.virt_irq&= ~mask; +This seems to be non-smp-safe? Do you need atomic ops and barriers here? And a wakeup?
The whole thing is not SMP tested yet, so I took some shortcuts. I only recently got hold of a SMP model and SMP support will be a focus area for the next series. Thanks for pin-pointing this though.
Quoted 24 lines
Unlike
KVM_INTERRUPT,KVM_IRQ_LINEis designed to be used asynchronously wrt the vcpu.Quoted 20 lines
arch/arm/kvm/arm.c +11
-2+return 0 ; +} +long kvm_arch_vcpu_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg) {@@ -312,8 +349,21 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)long kvm_arch_vm_ioctl (struct file *filp,unsigned int ioctl,unsigned long arg) { -printk (KERN_ERR "kvm_arch_vm_ioctl: Unsupported ioctl (%d)\n" , ioctl); -return -EINVAL ; +struct kvm *kvm = filp->private_data; +void __user *argp = (void __user *)arg; + +switch (ioctl) { +case KVM_IRQ_LINE : { +struct kvm_irq_level irq_event; + +if (copy_from_user (&irq_event, argp,sizeof irq_event))
Quoted 8 lines
arch/arm/kvm/arm.c int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) · line 364
Quoted 5 lines
arch/arm/kvm/arm.c +5
-0+return -EFAULT ; +return kvm_arch_vm_ioctl_irq_line (kvm,&irq_event); + } +default : +kvm_err (-EINVAL ,"Unsupported ioctl (%d)" , ioctl);Please remove for the final code, we don't want a user spamming the kernel log.
OK. Good point.
Quoted 14 lines
Quoted 6 lines
arch/arm/kvm/arm.c +2
-0+return -EINVAL ; + } }Signature · 6 lines
error compiling committee.c: too many arguments to function _______________________________________________ Android-virt mailing list [email protected] https://lists.cs.columbia.edu/cucslists/listinfo/android-virt
Re: [Android-virt] [PATCH v4 05/10] ARM: KVM: Inject IRQs and FIQs from userspace
Quoted 9 lines · Christoffer Dall
Quoted 9 lines
Quoted 8 lines
+
+ if (irq_level->level) {
+vcpu->arch.virt_irq|= mask;
+vcpu->arch.wait_for_interrupts= 0;
+ } else
+ vcpu->arch.virt_irq&= ~mask;
+
Quoted 4 lines
Quoted 2 lines
This seems to be non-smp-safe? Do you need atomic ops and barriers here? And a wakeup?
The whole thing is not SMP tested yet, so I took some shortcuts. I only recently got hold of a SMP model and SMP support will be a focus area for the next series. Thanks for pin-pointing this though.
Note even a single vcpu guest on an smp host needs this.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [Android-virt] [PATCH v4 05/10] ARM: KVM: Inject IRQs and FIQs from userspace
Quoted 10 lines · Avi Kivity
Quoted 9 lines · Christoffer Dall
Quoted 9 lines
Quoted 8 lines
+
+ if (irq_level->level) {
+vcpu->arch.virt_irq|= mask;
+vcpu->arch.wait_for_interrupts= 0;
+ } else
+ vcpu->arch.virt_irq&= ~mask;
+
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
This seems to be non-smp-safe? Do you need atomic ops and barriers here? And a wakeup?
The whole thing is not SMP tested yet, so I took some shortcuts. I only recently got hold of a SMP model and SMP support will be a focus area for the next series. Thanks for pin-pointing this though.
Note even a single vcpu guest on an smp host needs this.
yep, I am aware. It's on my to-do list. Thanks.
Quoted 4 lines
Signature · 2 lines
-- error compiling committee.c: too many arguments to function
PATCH v4 6/10 ARM: KVM: World-switch implementation
In reply to Christoffer DallProvides complete world-switch implementation to switch to other guests runinng in non-secure modes. Includes Hyp exception handlers that captures necessary exception information and stores the information on the VCPU and KVM structures.
Switching to Hyp mode is done through a simple HVC instructions. The exception vector code will check that the HVC comes from VMID==0 and if so will store the necessary state on the Hyp stack, which will look like this (see hyp_hvc):
...
Hyp_Sp + 4: lr_usr
Hyp_Sp : spsr (Host-SVC cpsr)
When returning from Hyp mode to SVC mode, another HVC instruction is executed from Hyp mode, which is taken in the Hyp_Svc handler. The Hyp stack pointer should be where it was left from the above initial call, since the values on the stack will be used to restore state (see hyp_svc).
Otherwise, the world-switch is pretty straight-forward. All state that can be modified by the guest is first backed up on the Hyp stack and the VCPU values is loaded onto the hardware. State, which is not loaded, but theoretically modifiable by the guest is protected through the virtualiation features to generate a trap and cause software emulation. Upon guest returns, all state is restored from hardware onto the VCPU struct and the original state is restored from the Hyp-stack onto the hardware.
One controversy may be the back-door call to __irq_svc (the host kernel's own physical IRQ handler) which is called when a physical IRQ exception is taken in Hyp mode while running in the guest.
--- arch/arm/include/asm/kvm.h | 1 arch/arm/include/asm/kvm_arm.h | 26 ++ arch/arm/include/asm/kvm_host.h | 8 + arch/arm/kernel/armksyms.c | 6 + arch/arm/kernel/asm-offsets.c | 33 +++ arch/arm/kernel/entry-armv.S | 1 arch/arm/kvm/arm.c | 55 ++++- arch/arm/kvm/arm_guest.c | 2 arch/arm/kvm/arm_interrupts.S | 443 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 570 insertions(+), 5 deletions(-)
arch/arm/include/asm/kvm.h +1 -0
@@ -51,6 +51,7 @@ struct kvm_regs {__u32 cpsr;__u32 spsr[5 ];/* Banked SPSR, indexed by MODE_ */ struct { +__u32 c0_midr;__u32 c1_sys;__u32 c2_base0;__u32 c2_base1;
arch/arm/include/asm/kvm_arm.h +26 -0
@@ -100,5 +100,31 @@#define VTTBR_X (5 - VTCR_GUEST_T0SZ)#endif +/* Hyp Syndrome Register (HSR) bits */ +#define HSR_EC_SHIFT (26) +#define HSR_EC (0x3fU << HSR_EC_SHIFT) +#define HSR_IL (1U << 25) +#define HSR_ISS (HSR_IL - 1) +#define HSR_ISV_SHIFT (24) +#define HSR_ISV (1U << HSR_ISV_SHIFT) + +#define HSR_EC_UNKNOWN (0x00) +#define HSR_EC_WFI (0x01) +#define HSR_EC_CP15_32 (0x03) +#define HSR_EC_CP15_64 (0x04) +#define HSR_EC_CP14_MR (0x05) +#define HSR_EC_CP14_LS (0x06) +#define HSR_EC_CP_0_13 (0x07) +#define HSR_EC_CP10_ID (0x08) +#define HSR_EC_JAZELLE (0x09) +#define HSR_EC_BXJ (0x0A) +#define HSR_EC_CP14_64 (0x0C) +#define HSR_EC_SVC_HYP (0x11) +#define HSR_EC_HVC (0x12) +#define HSR_EC_SMC (0x13) +#define HSR_EC_IABT (0x20) +#define HSR_EC_IABT_HYP (0x21) +#define HSR_EC_DABT (0x24) +#define HSR_EC_DABT_HYP (0x25)#endif /* __KVM_ARM_H__ */
arch/arm/include/asm/kvm_host.h +8 -0
@@ -62,6 +62,7 @@ struct kvm_vcpu_arch {/* System control coprocessor (cp15) */ struct { +u32 c0_MIDR;/* Main ID Register */ u32 c1_SCTLR;/* System Control Register */ u32 c1_ACTLR;/* Auxilliary Control Register */ u32 c1_CPACR;/* Coprocessor Access Control */
@@ -69,6 +70,12 @@ struct kvm_vcpu_arch {u64 c2_TTBR1;/* Translation Table Base Register 1 */ u32 c2_TTBCR;/* Translation Table Base Control R. */ u32 c3_DACR;/* Domain Access Control Register */ +u32 c10_PRRR;/* Primary Region Remap Register */ +u32 c10_NMRR;/* Normal Memory Remap Register */ +u32 c13_CID;/* Context ID Register */ +u32 c13_TID_URW;/* Thread ID, User R/W */ +u32 c13_TID_URO;/* Thread ID, User R/O */ +u32 c13_TID_PRIV;/* Thread ID, Priveleged */ } cp15;u32 virt_irq;/* HCR exception mask */
@@ -78,6 +85,7 @@ struct kvm_vcpu_arch {u32 hdfar;/* Hyp Data Fault Address Register */ u32 hifar;/* Hyp Inst. Fault Address Register */ u32 hpfar;/* Hyp IPA Fault Address Register */ +u64 pc_ipa;/* IPA for the current PC (VA to PA result) */ /* IO related fields */ u32 mmio_rd;
arch/arm/kernel/armksyms.c +6 -0
@@ -49,6 +49,12 @@ extern void __aeabi_ulcmp(void);extern void fpundefinstr (void ); +#ifdef CONFIG_KVM_ARM_HOST +/* This is needed for KVM */ +extern void __irq_svc (void ); + +EXPORT_SYMBOL_GPL (__irq_svc); +#endif EXPORT_SYMBOL (__backtrace);
arch/arm/kernel/asm-offsets.c +33 -0
@@ -16,6 +16,7 @@#include <asm/cacheflush.h> #include <asm/glue-df.h> #include <asm/glue-pf.h> +#include <linux/kvm_host.h> #include <asm/mach/arch.h> #include <asm/thread_info.h> #include <asm/memory.h>
@@ -129,5 +130,37 @@ int main(void)DEFINE (DMA_BIDIRECTIONAL ,DMA_BIDIRECTIONAL );DEFINE (DMA_TO_DEVICE ,DMA_TO_DEVICE );DEFINE (DMA_FROM_DEVICE ,DMA_FROM_DEVICE ); +#ifdef CONFIG_KVM_ARM_HOST +DEFINE (VCPU_KVM , offsetof(struct kvm_vcpu , kvm)); + DEFINE(VCPU_MIDR, offsetof(struct kvm_vcpu, arch.cp15.c0_MIDR)); + DEFINE(VCPU_SCTLR, offsetof(struct kvm_vcpu, arch.cp15.c1_SCTLR)); + DEFINE(VCPU_CPACR, offsetof(struct kvm_vcpu, arch.cp15.c1_CPACR)); + DEFINE(VCPU_TTBR0, offsetof(struct kvm_vcpu, arch.cp15.c2_TTBR0)); + DEFINE(VCPU_TTBR1, offsetof(struct kvm_vcpu, arch.cp15.c2_TTBR1)); + DEFINE(VCPU_TTBCR, offsetof(struct kvm_vcpu, arch.cp15.c2_TTBCR)); + DEFINE(VCPU_DACR, offsetof(struct kvm_vcpu, arch.cp15.c3_DACR)); + DEFINE(VCPU_PRRR, offsetof(struct kvm_vcpu, arch.cp15.c10_PRRR)); + DEFINE(VCPU_NMRR, offsetof(struct kvm_vcpu, arch.cp15.c10_NMRR)); + DEFINE(VCPU_CID, offsetof(struct kvm_vcpu, arch.cp15.c13_CID)); + DEFINE(VCPU_TID_URW, offsetof(struct kvm_vcpu, arch.cp15.c13_TID_URW)); + DEFINE(VCPU_TID_URO, offsetof(struct kvm_vcpu, arch.cp15.c13_TID_URO)); + DEFINE(VCPU_TID_PRIV, offsetof(struct kvm_vcpu, arch.cp15.c13_TID_PRIV)); + DEFINE(VCPU_REGS, offsetof(struct kvm_vcpu, arch.regs)); + DEFINE(VCPU_USR_REGS, offsetof(struct kvm_vcpu, arch.regs.usr_regs)); + DEFINE(VCPU_SVC_REGS, offsetof(struct kvm_vcpu, arch.regs.svc_regs)); + DEFINE(VCPU_ABT_REGS, offsetof(struct kvm_vcpu, arch.regs.abt_regs)); + DEFINE(VCPU_UND_REGS, offsetof(struct kvm_vcpu, arch.regs.und_regs)); + DEFINE(VCPU_IRQ_REGS, offsetof(struct kvm_vcpu, arch.regs.irq_regs)); + DEFINE(VCPU_FIQ_REGS, offsetof(struct kvm_vcpu, arch.regs.fiq_regs)); + DEFINE(VCPU_PC, offsetof(struct kvm_vcpu, arch.regs.pc)); + DEFINE(VCPU_CPSR, offsetof(struct kvm_vcpu, arch.regs.cpsr)); + DEFINE(VCPU_VIRT_IRQ, offsetof(struct kvm_vcpu, arch.virt_irq)); + DEFINE(VCPU_HSR, offsetof(struct kvm_vcpu, arch.hsr)); + DEFINE(VCPU_HDFAR, offsetof(struct kvm_vcpu, arch.hdfar)); + DEFINE(VCPU_HIFAR, offsetof(struct kvm_vcpu, arch.hifar)); + DEFINE(VCPU_HPFAR, offsetof(struct kvm_vcpu, arch.hpfar)); + DEFINE(VCPU_PC_IPA, offsetof(struct kvm_vcpu, arch.pc_ipa)); + DEFINE(KVM_VTTBR, offsetof(struct kvm, arch.vttbr)); +#endif return 0 ; }
arch/arm/kernel/entry-armv.S +1 -0
@@ -198,6 +198,7 @@ __dabt_svc: ENDPROC(__dabt_svc) .align 5 + .globl __irq_svc __irq_svc: svc_entry
arch/arm/kvm/arm.c +51 -4
76 lines not on this page · raw message
arch/arm/kvm/arm_guest.c +2 -0
@@ -73,6 +73,7 @@ int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)/* * Co-processor registers. */ + regs->cp15.c0_midr = vcpu->arch.cp15.c0_MIDR; regs->cp15.c1_sys = vcpu->arch.cp15.c1_SCTLR; regs->cp15.c2_base0 = vcpu->arch.cp15.c2_TTBR0; regs->cp15.c2_base1 = vcpu->arch.cp15.c2_TTBR1;
@@ -111,6 +112,7 @@ int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)/* * Co-processor registers. */ + vcpu->arch.cp15.c0_MIDR = regs->cp15.c0_midr; vcpu->arch.cp15.c1_SCTLR = regs->cp15.c1_sys; vcpu_regs->pc = regs->reg15;
arch/arm/kvm/arm_interrupts.S +442 -1
466 lines not on this page · raw message
Re: [PATCH v4 06/10] ARM: KVM: World-switch implementation
Quoted 35 lines · Christoffer Dall
Provides complete world-switch implementation to switch to other guests runinng in non-secure modes. Includes Hyp exception handlers that captures necessary exception information and stores the information on the VCPU and KVM structures.
Switching to Hyp mode is done through a simple HVC instructions. The exception vector code will check that the HVC comes from VMID==0 and if so will store the necessary state on the Hyp stack, which will look like this (see
hyp_hvc):... Hyp_Sp + 4: lr_usr Hyp_Sp : spsr (Host-SVC cpsr)When returning from Hyp mode to SVC mode, another HVC instruction is executed from Hyp mode, which is taken in the
Hyp_Svchandler. The Hyp stack pointer should be where it was left from the above initial call, since the values on the stack will be used to restore state (seehyp_svc).Otherwise, the world-switch is pretty straight-forward. All state that can be modified by the guest is first backed up on the Hyp stack and the VCPU values is loaded onto the hardware. State, which is not loaded, but theoretically modifiable by the guest is protected through the virtualiation features to generate a trap and cause software emulation. Upon guest returns, all state is restored from hardware onto the VCPU struct and the original state is restored from the Hyp-stack onto the hardware.
One controversy may be the back-door call to
__irq_svc(the host kernel's own physical IRQ handler) which is called when a physical IRQ exception is taken in Hyp mode while running in the guest.void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) {
arch/arm/kvm/arm.c out: · line 244
Quoted 8 lines
arch/arm/kvm/arm.c +6
-1+unsigned long start, end; + latest_vcpu =NULL ; -KVMARM_NOT_IMPLEMENTED (); + + start = (unsigned long )vcpu, + end = start +sizeof (struct kvm_vcpu ); +remove_hyp_mappings (kvm_hyp_pgd, start, end);
What if vcpu shares a page with another mapped structure?
Quoted 18 lines
arch/arm/kvm/arm.c +13
-0+ +kmem_cache_free (kvm_vcpu_cache, vcpu); }return 0 ; } +/** +* kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code +* @vcpu: The VCPU pointer +* @run: The kvm_run structure pointer used for userspace state exchange +* +* This function is called through the VCPU_RUN ioctl called from user space. It +* will execute VM code in a loop until the time slice for the process is used +* or some emulation is needed from user space in which case the function will +* return with return value 0 and with the kvm_run structure filled in with the +* required data for the requested emulation. +*/
arch/arm/kvm/arm.c int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) · line 332
Quoted 10 lines
arch/arm/kvm/arm.c +6
-2int kvm_arch_vcpu_ioctl_run (struct kvm_vcpu *vcpu,struct kvm_run *run) { -KVMARM_NOT_IMPLEMENTED (); -return -EINVAL ; +unsigned long flags; +int ret; + +for (;;) { +trace_kvm_entry (vcpu->arch.regs.pc); +debug_ws_enter (vcpu->arch.regs.pc);
why both trace_kvm and debug_ws?
arch/arm/kvm/arm.c int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) · line 335
Quoted 3 lines
arch/arm/kvm/arm.c +3
-0+kvm_guest_enter (); + +local_irq_save (flags);
local_irq_disable() is likely sufficient - the call path never changes.
Quoted 12 lines
arch/arm/kvm/arm.c +9
-0+ ret =__kvm_vcpu_run (vcpu); +local_irq_restore (flags); + +kvm_guest_exit (); +debug_ws_exit (vcpu->arch.regs.pc); +trace_kvm_exit (vcpu->arch.regs.pc); + } + +return ret; }
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v4 06/10] ARM: KVM: World-switch implementation
Quoted 34 lines · Avi Kivity
Quoted 33 lines · Christoffer Dall
Provides complete world-switch implementation to switch to other guests runinng in non-secure modes. Includes Hyp exception handlers that captures necessary exception information and stores the information on the VCPU and KVM structures.
Switching to Hyp mode is done through a simple HVC instructions. The exception vector code will check that the HVC comes from VMID==0 and if so will store the necessary state on the Hyp stack, which will look like this (see
hyp_hvc):
...
Hyp_Sp+ 4:lr_usr
Hyp_Sp: spsr (Host-SVC cpsr)When returning from Hyp mode to SVC mode, another HVC instruction is executed from Hyp mode, which is taken in the
Hyp_Svchandler. The Hyp stack pointer should be where it was left from the above initial call, since the values on the stack will be used to restore state (seehyp_svc).Otherwise, the world-switch is pretty straight-forward. All state that can be modified by the guest is first backed up on the Hyp stack and the VCPU values is loaded onto the hardware. State, which is not loaded, but theoretically modifiable by the guest is protected through the virtualiation features to generate a trap and cause software emulation. Upon guest returns, all state is restored from hardware onto the VCPU struct and the original state is restored from the Hyp-stack onto the hardware.
One controversy may be the back-door call to
__irq_svc(the host kernel's own physical IRQ handler) which is called when a physical IRQ exception is taken in Hyp mode while running in the guest.
Quoted 12 lines
arch/arm/kvm/arm.c out: · line 244
Quoted 10 lines
void
kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
{arch/arm/kvm/arm.c +6
-1+unsigned long start, end; + latest_vcpu =NULL ; -KVMARM_NOT_IMPLEMENTED (); + + start = (unsigned long )vcpu, + end = start +sizeof (struct kvm_vcpu ); +remove_hyp_mappings (kvm_hyp_pgd, start, end);What if vcpu shares a page with another mapped structure?
Then we have a problem. I will think about this. Thanks.
Quoted 23 lines
Quoted 23 lines
arch/arm/kvm/arm.c +13
-2+ +kmem_cache_free (kvm_vcpu_cache, vcpu); }return 0 ; } +/** +* kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code +* @vcpu: The VCPU pointer +* @run: The kvm_run structure pointer used for userspace state exchange +* +* This function is called through the VCPU_RUN ioctl called from user space. It +* will execute VM code in a loop until the time slice for the process is used +* or some emulation is needed from user space in which case the function will +* return with return value 0 and with the kvm_run structure filled in with the +* required data for the requested emulation. +*/ int kvm_arch_vcpu_ioctl_run (struct kvm_vcpu *vcpu,struct kvm_run *run) { -KVMARM_NOT_IMPLEMENTED (); -return -EINVAL ;
Quoted 8 lines
arch/arm/kvm/arm.c int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) · line 332
Quoted 6 lines
arch/arm/kvm/arm.c +6
-0+unsigned long flags; +int ret; + +for (;;) { +trace_kvm_entry (vcpu->arch.regs.pc); +debug_ws_enter (vcpu->arch.regs.pc);why both
trace_kvmanddebug_ws?
Because tracepoints was a bit impractical to use for debugging some problems, but this is actually not relevant anymore and I will clean it up for next series. Sorry.
Quoted 6 lines
arch/arm/kvm/arm.c int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) · line 335
Quoted 4 lines
arch/arm/kvm/arm.c +3
-0+kvm_guest_enter (); + +local_irq_save (flags);
local_irq_disable()is likely sufficient - the call path never changes.
good point.
Quoted 17 lines
Quoted 13 lines
arch/arm/kvm/arm.c +9
-0+ ret =__kvm_vcpu_run (vcpu); +local_irq_restore (flags); + +kvm_guest_exit (); +debug_ws_exit (vcpu->arch.regs.pc); +trace_kvm_exit (vcpu->arch.regs.pc); + } + +return ret; }Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH v4 7/10 ARM: KVM: Emulation framework and CP15 emulation
In reply to Christoffer DallAdds a new important function in the main KVM/ARM code called handle_exit() which is called from kvm_arch_vcpu_ioctl_run() on returns from guest execution. This function examines the Hyp-Syndrome-Register (HSR), which contains information telling KVM what caused the exit from the guest.
Some of the reasons for an exit are CP15 accesses, which are not allowed from the guest and this commits handles these exits by emulating the intented operation in software and skip the guest instruction.
--- arch/arm/include/asm/kvm_emulate.h | 7 + arch/arm/kvm/arm.c | 77 ++++++++++++++ arch/arm/kvm/arm_emulate.c | 195 ++++++++++++++++++++++++++++++++++++ arch/arm/kvm/trace.h | 28 +++++ 4 files changed, 307 insertions(+), 0 deletions(-)
arch/arm/include/asm/kvm_emulate.h +7 -0
@@ -40,6 +40,13 @@ static inline unsigned char vcpu_mode(struct kvm_vcpu *vcpu)return modes_table[vcpu->arch.regs.cpsr &0xf ]; } +int kvm_handle_cp10_id (struct kvm_vcpu *vcpu,struct kvm_run *run); +int kvm_handle_cp_0_13_access (struct kvm_vcpu *vcpu,struct kvm_run *run); +int kvm_handle_cp14_load_store (struct kvm_vcpu *vcpu,struct kvm_run *run); +int kvm_handle_cp14_access (struct kvm_vcpu *vcpu,struct kvm_run *run); +int kvm_handle_cp15_access (struct kvm_vcpu *vcpu,struct kvm_run *run); +int kvm_handle_wfi (struct kvm_vcpu *vcpu,struct kvm_run *run); +/* * Return the SPSR for the specified mode of the virtual CPU. */
arch/arm/kvm/arm.c +77 -0
95 lines not on this page · raw message
arch/arm/kvm/arm_emulate.c +195 -0
205 lines not on this page · raw message
arch/arm/kvm/trace.h +28 -0
@@ -39,6 +39,34 @@ TRACE_EVENT(kvm_exit,TP_printk ("PC: 0x%08lx" , __entry->vcpu_pc) ); +TRACE_EVENT (kvm_emulate_cp15_imp, + TP_PROTO(unsigned long Op1, unsigned long Rt1, unsigned long CRn, + unsigned long CRm, unsigned long Op2, bool is_write), +TP_ARGS (Op1, Rt1, CRn, CRm, Op2, is_write), + +TP_STRUCT__entry ( + __field( unsigned int, Op1 ) + __field( unsigned int, Rt1 ) + __field( unsigned int, CRn ) + __field( unsigned int, CRm ) + __field( unsigned int, Op2 ) +__field ( bool, is_write ) + ), + +TP_fast_assign ( + __entry->is_write = is_write; + __entry->Op1 = Op1; + __entry->Rt1 = Rt1; + __entry->CRn = CRn; + __entry->CRm = CRm; + __entry->Op2 = Op2; + ), + +TP_printk ("Implementation defined CP15: %s\tp15, %u, r%u, c%u, c%u, %u" , + (__entry->is_write) ?"mcr" :"mrc" , + __entry->Op1, __entry->Rt1, __entry->CRn, + __entry->CRm, __entry->Op2) +); TRACE_EVENT(kvm_irq_line, TP_PROTO(unsigned int type, unsigned int level, unsigned int vcpu_idx),
Re: [PATCH v4 07/10] ARM: KVM: Emulation framework and CP15 emulation
Quoted 24 lines · Christoffer Dall
Adds a new important function in the main KVM/ARM code called
handle_exit()which is called fromkvm_arch_vcpu_ioctl_run()on returns from guest execution. This function examines the Hyp-Syndrome-Register (HSR), which contains information telling KVM what caused the exit from the guest.Some of the reasons for an exit are CP15 accesses, which are not allowed from the guest and this commits handles these exits by emulating the intented operation in software and skip the guest instruction.
/**
*kvm_arch_vcpu_ioctl_run- the main VCPU run function to execute guest code
* @vcpu: The VCPU pointerarch/arm/kvm/arm.c +5
-0@@ -339,6 +396,26 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)kvm_guest_exit ();debug_ws_exit (vcpu->arch.regs.pc);trace_kvm_exit (vcpu->arch.regs.pc); + + ret =handle_exit (vcpu, run, ret); +if (ret) { +kvm_err (ret,"Error in handle_exit" ); +break ;
arch/arm/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 413
Quoted 10 lines
arch/arm/kvm/arm.c +10
-0+ } + +if (run->exit_reason ==KVM_EXIT_MMIO ) +break ; + +if (need_resched ()) { +vcpu_put (vcpu); +schedule (); +vcpu_load (vcpu); + }
Preempt notifiers mean you don't need vcpu_put()/vcpu_load() - the scheduler will call kvm_arch_vcpu_put/load() automatically during context switch.
arch/arm/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 419
Quoted 6 lines
arch/arm/kvm/arm.c +5
-0+ +if (signal_pending (current)&& !(run->exit_reason)) { + run->exit_reason =KVM_EXIT_IRQ_WINDOW_OPEN ; +break ; + } }
You're supposed to return -EINTR on a signal. run->exit_reason isn't defined in this case, but traditionally we return KVM_EXIT_INTR (which means host signal, not guest signal - yes it's confusing).
Quoted 5 lines
arch/arm/kvm/arm_emulate.c +5
-0+ +/** +* emulate_cp15_c15_access -- emulates cp15 accesses for CRn == 15 +* @vcpu: The VCPU pointer +* @p: The coprocessor parameters struct pointer holding trap inst. details
arch/arm/kvm/arm_emulate.c u32 *kvm_vcpu_reg(struct kvm_vcpu *vcpu, u8 reg_num, u32 mode) · line 239
Quoted 10 lines
arch/arm/kvm/arm_emulate.c +10
-0+* +* The CP15 c15 register is implementation defined, but some guest kernels +* attempt to read/write a diagnostics register here. We always return 0 and +* ignore writes and hope for the best. This may need to be refined. +*/ +static int emulate_cp15_c15_access (struct kvm_vcpu *vcpu, +struct coproc_params *p) +{ +trace_kvm_emulate_cp15_imp (p->Op1, p->Rt1, p->CRn, p->CRm, + p->Op2, p->is_write);
_imp?
Quoted 8 lines
arch/arm/kvm/arm_emulate.c +7
-0+ +if (!p->is_write) + *vcpu_reg (vcpu, p->Rt1) =0 ; + +return 0 ; +} +
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v4 07/10] ARM: KVM: Emulation framework and CP15 emulation
Quoted 29 lines · Avi Kivity
Quoted 28 lines · Christoffer Dall
Adds a new important function in the main KVM/ARM code called
handle_exit()which is called fromkvm_arch_vcpu_ioctl_run()on returns from guest execution. This function examines the Hyp-Syndrome-Register (HSR), which contains information telling KVM what caused the exit from the guest.Some of the reasons for an exit are CP15 accesses, which are not allowed from the guest and this commits handles these exits by emulating the intented operation in software and skip the guest instruction.
/**
*kvm_arch_vcpu_ioctl_run- the main VCPU run function to execute guest code
* @vcpu: The VCPU pointerarch/arm/kvm/arm.c +9
-0@@ -339,6 +396,26 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)kvm_guest_exit ();debug_ws_exit (vcpu->arch.regs.pc);trace_kvm_exit (vcpu->arch.regs.pc); + + ret =handle_exit (vcpu, run, ret); +if (ret) { +kvm_err (ret,"Error in handle_exit" ); +break ; + } + +if (run->exit_reason ==KVM_EXIT_MMIO ) +break ;
Quoted 8 lines
arch/arm/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 413
Quoted 6 lines
arch/arm/kvm/arm.c +6
-0+ +if (need_resched ()) { +vcpu_put (vcpu); +schedule (); +vcpu_load (vcpu); + }Preempt notifiers mean you don't need vcpu_put()/vcpu_load() - the scheduler will call kvm_arch_vcpu_put/load() automatically during context switch.
cool. thanks.
Quoted 9 lines
arch/arm/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 419
Quoted 7 lines
arch/arm/kvm/arm.c +5
-0+ +if (signal_pending (current)&& !(run->exit_reason)) { + run->exit_reason =KVM_EXIT_IRQ_WINDOW_OPEN ; +break ; + } }You're supposed to return
-EINTRon a signal.run->exit_reasonisn't defined in this case, but traditionally we returnKVM_EXIT_INTR(which means host signal, not guest signal - yes it's confusing).
thanks for clearing that up.
Quoted 10 lines
Quoted 10 lines
arch/arm/kvm/arm_emulate.c +9
-0+ +/** +* emulate_cp15_c15_access -- emulates cp15 accesses for CRn == 15 +* @vcpu: The VCPU pointer +* @p: The coprocessor parameters struct pointer holding trap inst. details +* +* The CP15 c15 register is implementation defined, but some guest kernels +* attempt to read/write a diagnostics register here. We always return 0 and +* ignore writes and hope for the best. This may need to be refined.
Quoted 8 lines
arch/arm/kvm/arm_emulate.c u32 *kvm_vcpu_reg(struct kvm_vcpu *vcpu, u8 reg_num, u32 mode) · line 239
Quoted 6 lines
+ */
arch/arm/kvm/arm_emulate.c +5
-0+static int emulate_cp15_c15_access (struct kvm_vcpu *vcpu, +struct coproc_params *p) +{ +trace_kvm_emulate_cp15_imp (p->Op1, p->Rt1, p->CRn, p->CRm, + p->Op2, p->is_write);
_imp?
implementation defined co-processor 15 operations. Took me 10 minutes to dig out from memory, so, ok, this is not super informative or clear:) Will try to come up with something better or the right comment somewhere or something.
Quoted 13 lines
Quoted 9 lines
arch/arm/kvm/arm_emulate.c +7
-0+ +if (!p->is_write) + *vcpu_reg (vcpu, p->Rt1) =0 ; + +return 0 ; +} +Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v4 07/10] ARM: KVM: Emulation framework and CP15 emulation
Quoted 17 lines · Christoffer Dall
Quoted 17 lines
Quoted 16 lines
+
+/**
+ *emulate_cp15_c15_access-- emulates cp15 accesses for CRn == 15
+ * @vcpu: The VCPU pointer
+ * @p: The coprocessor parameters struct pointer holding trap inst. details
+ *
+ * The CP15 c15 register is implementation defined, but some guest kernels
+ * attempt to read/write a diagnostics register here. We always return 0 and
+ * ignore writes and hope for the best. This may need to be refined.
+ */
+static int emulate_cp15_c15_access(structkvm_vcpu*vcpu,
+ structcoproc_params*p)
+{
+ trace_kvm_emulate_cp15_imp(p->Op1,p->Rt1,p->CRn,p->CRm,
+p->Op2,p->is_write);
Quoted 4 lines
Quoted 1 line
_imp?implementation defined co-processor 15 operations. Took me 10 minutes to dig out from memory, so, ok, this is not super informative or clear:) Will try to come up with something better or the right comment somewhere or something.
Ah, okay. It's not related to the kvm implementation, it's architecturally implementation defined.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v4 07/10] ARM: KVM: Emulation framework and CP15 emulation
Quoted 18 lines · Avi Kivity
Quoted 17 lines · Christoffer Dall
Quoted 17 lines
Quoted 16 lines
+
+/**
+ *emulate_cp15_c15_access-- emulates cp15 accesses for CRn == 15
+ * @vcpu: The VCPU pointer
+ * @p: The coprocessor parameters struct pointer holding trap inst. details
+ *
+ * The CP15 c15 register is implementation defined, but some guest kernels
+ * attempt to read/write a diagnostics register here. We always return 0 and
+ * ignore writes and hope for the best. This may need to be refined.
+ */
+static int emulate_cp15_c15_access(structkvm_vcpu*vcpu,
+ structcoproc_params*p)
+{
+ trace_kvm_emulate_cp15_imp(p->Op1,p->Rt1,p->CRn,p->CRm,
+p->Op2,p->is_write);
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
_imp?implementation defined co-processor 15 operations. Took me 10 minutes to dig out from memory, so, ok, this is not super informative or clear:) Will try to come up with something better or the right comment somewhere or something.
Ah, okay. It's not related to the kvm implementation, it's architecturally implementation defined.
exactly.
Quoted 4 lines
Signature · 2 lines
-- error compiling committee.c: too many arguments to function
PATCH v4 8/10 ARM: KVM: Handle guest faults in KVM
In reply to Christoffer DallHandles the guest faults in KVM by mapping in corresponding user pages in the 2nd stage page tables.
Introduces new ARM-specific kernel memory types, PAGE_KVM_GUEST and pgprot_guest variables used to map 2nd stage memory for KVM guests.
--- arch/arm/include/asm/pgtable-3level.h | 9 +++ arch/arm/include/asm/pgtable.h | 4 + arch/arm/kvm/arm_mmu.c | 107 ++++++++++++++++++++++++++++++++- arch/arm/mm/mmu.c | 3 + 4 files changed, 121 insertions(+), 2 deletions(-)
arch/arm/include/asm/pgtable-3level.h +9 -0
@@ -104,4 +104,13 @@*/ #define L_PGD_SWAPPER (_AT(pgdval_t, 1) << 55)/* swapper_pg_dir entry */ +/* +* 2-nd stage PTE definitions for LPAE. +*/ +#define L_PTE2_READ (_AT(pteval_t, 1) << 6)/* HAP[0] */ +#define L_PTE2_WRITE (_AT(pteval_t, 1) << 7)/* HAP[1] */ +#define L_PTE2_NORM_WB (_AT(pteval_t, 3) << 4)/* MemAttr[3:2] */ +#define L_PTE2_INNER_WB (_AT(pteval_t, 3) << 2)/* MemAttr[1:0] */ + +#endif /* _ASM_PGTABLE_3LEVEL_H */
arch/arm/include/asm/pgtable.h +4 -0
@@ -76,6 +76,7 @@ extern void __pgd_error(const char *file, int line, pgd_t);extern pgprot_t pgprot_user;extern pgprot_t pgprot_kernel; +extern pgprot_t pgprot_guest;#define _MOD_PROT (p, b) __pgprot(pgprot_val(p) | (b))
@@ -89,6 +90,9 @@ extern pgprot_t pgprot_kernel;#define PAGE_KERNEL _MOD_PROT(pgprot_kernel, L_PTE_XN)#define PAGE_KERNEL_EXEC pgprot_kernel#define PAGE_HYP _MOD_PROT(pgprot_kernel, L_PTE_USER) +#define PAGE_KVM_GUEST _MOD_PROT(pgprot_guest, L_PTE2_READ | \ + L_PTE2_WRITE | L_PTE2_NORM_WB | \ + L_PTE2_INNER_WB)#define __PAGE_NONE __pgprot(_L_PTE_DEFAULT | L_PTE_RDONLY | L_PTE_XN)#define __PAGE_SHARED __pgprot(_L_PTE_DEFAULT | L_PTE_USER | L_PTE_XN)
arch/arm/kvm/arm_mmu.c +105 -2
113 lines not on this page · raw message
arch/arm/mm/mmu.c +3 -0
@@ -55,9 +55,11 @@ static unsigned int cachepolicy __initdata = CPOLICY_WRITEBACK;static unsigned int ecc_mask__initdata =0 ;pgprot_t pgprot_user;pgprot_t pgprot_kernel; +pgprot_t pgprot_guest;EXPORT_SYMBOL (pgprot_user);EXPORT_SYMBOL (pgprot_kernel); +EXPORT_SYMBOL (pgprot_guest);struct cachepolicy {const char policy[16 ];
@@ -497,6 +499,7 @@ static void __init build_mem_type_table(void) pgprot_user =__pgprot (L_PTE_PRESENT |L_PTE_YOUNG | user_pgprot); pgprot_kernel =__pgprot (L_PTE_PRESENT |L_PTE_YOUNG |L_PTE_DIRTY | kern_pgprot); + pgprot_guest =__pgprot (L_PTE_PRESENT |L_PTE_YOUNG ); mem_types[MT_LOW_VECTORS ].prot_l1 |= ecc_mask; mem_types[MT_HIGH_VECTORS ].prot_l1 |= ecc_mask;
Re: [PATCH v4 08/10] ARM: KVM: Handle guest faults in KVM
Quoted 27 lines · Christoffer Dall
Handles the guest faults in KVM by mapping in corresponding user pages in the 2nd stage page tables.
Introduces new ARM-specific kernel memory types,
PAGE_KVM_GUESTandpgprot_guestvariables used to map 2nd stage memory for KVM guests.arch/arm/kvm/arm_mmu.c +19
-0+static int user_mem_abort (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa, +gfn_t gfn,struct kvm_memory_slot *memslot) +{ +pfn_t pfn; +pgd_t *pgd; +pud_t *pud; +pmd_t *pmd; +pte_t *pte, new_pte; + + pfn =gfn_to_pfn (vcpu->kvm, gfn); + +if (is_error_pfn (pfn)) { +kvm_err (-EFAULT ,"Guest gfn %u (0x%08lx) does not have " +"corresponding host mapping" , + gfn, gfn<<PAGE_SHIFT ); +return -EFAULT ; + } + +/* Create 2nd stage page table mapping - Level 1 */
arch/arm/kvm/arm_mmu.c void kvm_free_stage2_pgd(struct kvm *kvm) · line 321
Quoted 10 lines
arch/arm/kvm/arm_mmu.c +10
-0+ pgd = vcpu->kvm->arch.pgd +pgd_index (fault_ipa); + pud =pud_offset (pgd, fault_ipa); +if (pud_none (*pud)) { + pmd =pmd_alloc_one (NULL , fault_ipa); +if (!pmd) { +kvm_err (-ENOMEM ,"Cannot allocate 2nd stage pmd" ); +return -ENOMEM ; + } +pud_populate (NULL , pud, pmd); + pmd +=pmd_index (fault_ipa);
Don't we need locking here? Another vcpu may have executed pud_populate concurrently.
Quoted 24 lines
arch/arm/kvm/arm_mmu.c +23
-0+ }else + pmd =pmd_offset (pud, fault_ipa); + +/* Create 2nd stage page table mapping - Level 2 */ +if (pmd_none (*pmd)) { + pte =pte_alloc_one_kernel (NULL , fault_ipa); +if (!pte) { +kvm_err (-ENOMEM ,"Cannot allocate 2nd stage pte" ); +return -ENOMEM ; + } +pmd_populate_kernel (NULL , pmd, pte); + pte +=pte_index (fault_ipa); + }else + pte =pte_offset_kernel (pmd, fault_ipa); + +/* Create 2nd stage page table mapping - Level 3 */ + new_pte =pfn_pte (pfn,PAGE_KVM_GUEST ); +set_pte_ext (pte, new_pte,0 ); + +return 0 ; +} + +#define HSR_ABT_FS (0x3f)
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v4 08/10] ARM: KVM: Handle guest faults in KVM
Quoted 32 lines · Avi Kivity
Quoted 31 lines · Christoffer Dall
Handles the guest faults in KVM by mapping in corresponding user pages in the 2nd stage page tables.
Introduces new ARM-specific kernel memory types,
PAGE_KVM_GUESTandpgprot_guestvariables used to map 2nd stage memory for KVM guests.arch/arm/kvm/arm_mmu.c +23
-0+static int user_mem_abort (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa, +gfn_t gfn,struct kvm_memory_slot *memslot) +{ +pfn_t pfn; +pgd_t *pgd; +pud_t *pud; +pmd_t *pmd; +pte_t *pte, new_pte; + + pfn =gfn_to_pfn (vcpu->kvm, gfn); + +if (is_error_pfn (pfn)) { +kvm_err (-EFAULT ,"Guest gfn %u (0x%08lx) does not have " +"corresponding host mapping" , + gfn, gfn<<PAGE_SHIFT ); +return -EFAULT ; + } + +/* Create 2nd stage page table mapping - Level 1 */ + pgd = vcpu->kvm->arch.pgd +pgd_index (fault_ipa); + pud =pud_offset (pgd, fault_ipa); +if (pud_none (*pud)) { + pmd =pmd_alloc_one (NULL , fault_ipa);
Quoted 8 lines
arch/arm/kvm/arm_mmu.c void kvm_free_stage2_pgd(struct kvm *kvm) · line 321
Quoted 6 lines
arch/arm/kvm/arm_mmu.c +6
-0+if (!pmd) { +kvm_err (-ENOMEM ,"Cannot allocate 2nd stage pmd" ); +return -ENOMEM ; + } +pud_populate (NULL , pud, pmd); + pmd +=pmd_index (fault_ipa);Don't we need locking here? Another vcpu may have executed
pud_populateconcurrently.
Absolutely, but there is no SMP support yet and only a single VCPU is supported. Again, focus area in next patch series and thanks for the pinpoint.
Quoted 29 lines
Quoted 25 lines
arch/arm/kvm/arm_mmu.c +23
-0+ }else + pmd =pmd_offset (pud, fault_ipa); + +/* Create 2nd stage page table mapping - Level 2 */ +if (pmd_none (*pmd)) { + pte =pte_alloc_one_kernel (NULL , fault_ipa); +if (!pte) { +kvm_err (-ENOMEM ,"Cannot allocate 2nd stage pte" ); +return -ENOMEM ; + } +pmd_populate_kernel (NULL , pmd, pte); + pte +=pte_index (fault_ipa); + }else + pte =pte_offset_kernel (pmd, fault_ipa); + +/* Create 2nd stage page table mapping - Level 3 */ + new_pte =pfn_pte (pfn,PAGE_KVM_GUEST ); +set_pte_ext (pte, new_pte,0 ); + +return 0 ; +} + +#define HSR_ABT_FS (0x3f)Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH v4 9/10 ARM: KVM: Handle I/O aborts
In reply to Christoffer DallWhen the guest accesses I/O memory this will create data abort exceptions and they are handled by decoding the HSR information (physical address, read/write, length, register) and forwarding reads and writes to QEMU which performs the device emulation.
Certain classes of load/store operations do not support the syndrome information provided in the HSR and we therefore must be able to fetch the offending instruction from guest memory and decode it manually.
This requires changing the general flow somewhat since new calls to run the VCPU must check if there's a pending MMIO load and perform the write after userspace has made the data available.
--- arch/arm/include/asm/kvm_emulate.h | 2 arch/arm/include/asm/kvm_host.h | 1 arch/arm/include/asm/kvm_mmu.h | 1 arch/arm/kvm/arm.c | 8 + arch/arm/kvm/arm_emulate.c | 279 ++++++++++++++++++++++++++++++++++++ arch/arm/kvm/arm_mmu.c | 155 ++++++++++++++++++++ arch/arm/kvm/trace.h | 15 ++ 7 files changed, 457 insertions(+), 4 deletions(-)
arch/arm/include/asm/kvm_emulate.h +2 -0
@@ -46,6 +46,8 @@ int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu, struct kvm_run *run);int kvm_handle_cp14_access (struct kvm_vcpu *vcpu,struct kvm_run *run);int kvm_handle_cp15_access (struct kvm_vcpu *vcpu,struct kvm_run *run);int kvm_handle_wfi (struct kvm_vcpu *vcpu,struct kvm_run *run); +int kvm_emulate_mmio_ls (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa, +unsigned long instr);/* * Return the SPSR for the specified mode of the virtual CPU.
arch/arm/include/asm/kvm_host.h +1 -0
@@ -88,6 +88,7 @@ struct kvm_vcpu_arch {u64 pc_ipa;/* IPA for the current PC (VA to PA result) */ /* IO related fields */ +bool mmio_sign_extend;/* for byte/halfword loads */ u32 mmio_rd;/* Misc. fields */
arch/arm/include/asm/kvm_mmu.h +1 -0
@@ -40,6 +40,7 @@ void free_hyp_pmds(pgd_t *hyp_pgd);int kvm_alloc_stage2_pgd (struct kvm *kvm);void kvm_free_stage2_pgd (struct kvm *kvm); +int kvm_handle_mmio_return (struct kvm_vcpu *vcpu,struct kvm_run *run);int kvm_handle_guest_abort (struct kvm_vcpu *vcpu,struct kvm_run *run);#endif /* __ARM_KVM_MMU_H__ */
arch/arm/kvm/arm.c +8 -0
@@ -385,6 +385,14 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)int ret;for (;;) { +if (run->exit_reason ==KVM_EXIT_MMIO ) { + ret =kvm_handle_mmio_return (vcpu, vcpu->run); +if (ret) +break ; + } + + run->exit_reason =KVM_EXIT_UNKNOWN ; +trace_kvm_entry (vcpu->arch.regs.pc);debug_ws_enter (vcpu->arch.regs.pc);kvm_guest_enter ();
arch/arm/kvm/arm_emulate.c +278 -1
295 lines not on this page · raw message
arch/arm/kvm/arm_mmu.c +152 -3
177 lines not on this page · raw message
arch/arm/kvm/trace.h +15 -0
@@ -39,6 +39,21 @@ TRACE_EVENT(kvm_exit,TP_printk ("PC: 0x%08lx" , __entry->vcpu_pc) ); +TRACE_EVENT (kvm_mmio_emulate, + TP_PROTO(unsigned long vcpu_pc), +TP_ARGS (vcpu_pc), + +TP_STRUCT__entry ( + __field( unsigned long, vcpu_pc ) + ), + +TP_fast_assign ( + __entry->vcpu_pc = vcpu_pc; + ), + +TP_printk ("Emulate MMIO at: 0x%08lx" , __entry->vcpu_pc) +); + TRACE_EVENT(kvm_emulate_cp15_imp, TP_PROTO(unsigned long Op1, unsigned long Rt1, unsigned long CRn, unsigned long CRm, unsigned long Op2, bool is_write),
Re: [PATCH v4 09/10] ARM: KVM: Handle I/O aborts
Quoted 5 lines · Christoffer Dall
When the guest accesses I/O memory this will create data abort exceptions and they are handled by decoding the HSR information (physical address, read/write, length, register) and forwarding reads and writes to QEMU which performs the device emulation.
Quoted 7 lines
Certain classes of load/store operations do not support the syndrome information provided in the HSR and we therefore must be able to fetch the offending instruction from guest memory and decode it manually.
This requires changing the general flow somewhat since new calls to run the VCPU must check if there's a pending MMIO load and perform the write after userspace has made the data available.
We need to move this to arch independent code. Outside the scope of these patches, of course.
Quoted 12 lines
/******************************************************************************
- * Co-processor emulation
+ * Utility functions common for all emulation code
+ *****************************************************************************/arch/arm/kvm/arm_emulate.c +7
-0+ +/* +* This one accepts a matrix where the first element is the +* bits as they must be, and the second element is the bitmask. */ +#define INSTR_NONE -1 +static int kvm_instr_index (u32 instr,u32 table[][2 ],int table_entries) +{
arch/arm/kvm/arm_emulate.c u32 *kvm_vcpu_reg(struct kvm_vcpu *vcpu, u8 reg_num, u32 mode) · line 151
Quoted 10 lines
arch/arm/kvm/arm_emulate.c +10
-0+int i; +u32 mask; + +for (i =0 ; i< table_entries; i++) { + mask = table[i][1 ]; +if ((table[i][0 ]& mask) == (instr& mask)) +return i; + } +return INSTR_NONE ; +}
Seems somewhat inefficient to do this for insn emulation. Is there not a common prefix that can be used to determine the mask?
Quoted 24 lines
arch/arm/kvm/arm_emulate.c +24
-0+ +/* +* Must be ordered with LOADS first and WRITES afterwards +* for easy distinction when doing MMIO. +*/ +#define NUM_LD_INSTR 9 +enum INSTR_LS_INDEXES { +INSTR_LS_LDRBT ,INSTR_LS_LDRT ,INSTR_LS_LDR ,INSTR_LS_LDRB , +INSTR_LS_LDRD ,INSTR_LS_LDREX ,INSTR_LS_LDRH ,INSTR_LS_LDRSB , +INSTR_LS_LDRSH , +INSTR_LS_STRBT ,INSTR_LS_STRT ,INSTR_LS_STR ,INSTR_LS_STRB , +INSTR_LS_STRD ,INSTR_LS_STREX ,INSTR_LS_STRH , +NUM_LS_INSTR +}; + +static u32 ls_instr[NUM_LS_INSTR ][2 ] = { + {0x04700000 ,0x0d700000 },/* LDRBT */ + {0x04300000 ,0x0d700000 },/* LDRT */ + {0x04100000 ,0x0c500000 },/* LDR */ + {0x04500000 ,0x0c500000 },/* LDRB */ + {0x000000d0 ,0x0e1000f0 },/* LDRD */ + {0x01900090 ,0x0ff000f0 },/* LDREX */ + {0x001000b0 ,0x0e1000f0 },/* LDRH */ + {0x001000d0 ,0x0e1000f0 },/* LDRSB */
arch/arm/kvm/arm_emulate.c int kvm_handle_wfi(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 378
Quoted 10 lines
arch/arm/kvm/arm_emulate.c +10
-0+ {0x001000f0, 0x0e1000f0}, /* LDRSH */ + {0x04600000, 0x0d700000}, /* STRBT */ + {0x04200000, 0x0d700000}, /* STRT */ + {0x04000000, 0x0c500000}, /* STR */ + {0x04400000, 0x0c500000}, /* STRB */ + {0x000000f0, 0x0e1000f0}, /* STRD */ + {0x01800090, 0x0ff000f0}, /* STREX */ + {0x000000b0, 0x0e1000f0} /* STRH */ +}; +
Okay, maybe not. But surely there's some clever arithmetic the cpu uses to decode this.
arch/arm/kvm/trace.h TRACE_EVENT(kvm_exit, · line 44
Quoted 11 lines
arch/arm/kvm/trace.h +3
-0@@ -39,6 +39,21 @@ TRACE_EVENT(kvm_exit,TP_printk ("PC: 0x%08lx" , __entry->vcpu_pc) ); +TRACE_EVENT(kvm_mmio_emulate, + TP_PROTO(unsigned long vcpu_pc), + TP_ARGS(vcpu_pc),
Please add the instruction bytes and any other information needed to decode the opcode (e.g. thumb mode). Forx86 we have a trace-cmd plugin that disassembles guest instructions into the trace; it's very useful.
Quoted 17 lines
arch/arm/kvm/trace.h +12
-0+ +TP_STRUCT__entry ( +__field (unsigned long ,vcpu_pc ) + ), + +TP_fast_assign ( + __entry->vcpu_pc = vcpu_pc; + ), + +TP_printk ("Emulate MMIO at: 0x%08lx" , __entry->vcpu_pc) +); + TRACE_EVENT(kvm_emulate_cp15_imp, TP_PROTO(unsigned long Op1, unsigned long Rt1, unsigned long CRn, unsigned long CRm, unsigned long Op2, bool is_write),
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v4 09/10] ARM: KVM: Handle I/O aborts
Quoted 10 lines · Avi Kivity
Quoted 9 lines · Christoffer Dall
When the guest accesses I/O memory this will create data abort exceptions and they are handled by decoding the HSR information (physical address, read/write, length, register) and forwarding reads and writes to QEMU which performs the device emulation.
Certain classes of load/store operations do not support the syndrome information provided in the HSR and we therefore must be able to fetch the offending instruction from guest memory and decode it manually.
Quoted 5 lines
Quoted 3 lines
This requires changing the general flow somewhat since new calls to run the VCPU must check if there's a pending MMIO load and perform the write after userspace has made the data available.
We need to move this to arch independent code. Outside the scope of these patches, of course.
OK, let me know what I can do to make this fit with the ARM implementation nicely.
Quoted 17 lines
Quoted 17 lines
/******************************************************************************
- * Co-processor emulation
+ * Utility functions common for all emulation code
+ *****************************************************************************/arch/arm/kvm/arm_emulate.c +11
-0+ +/* +* This one accepts a matrix where the first element is the +* bits as they must be, and the second element is the bitmask. */ +#define INSTR_NONE -1 +static int kvm_instr_index (u32 instr,u32 table[][2 ],int table_entries) +{ +int i; +u32 mask; + +for (i =0 ; i< table_entries; i++) {
Quoted 8 lines
arch/arm/kvm/arm_emulate.c u32 *kvm_vcpu_reg(struct kvm_vcpu *vcpu, u8 reg_num, u32 mode) · line 151
Quoted 6 lines
arch/arm/kvm/arm_emulate.c +6
-0+ mask = table[i][1 ]; +if ((table[i][0 ]& mask) == (instr& mask)) +return i; + } +return INSTR_NONE ; +}Seems somewhat inefficient to do this for insn emulation. Is there not a common prefix that can be used to determine the mask?
hehe, not so much.
Quoted 29 lines
Quoted 29 lines
arch/arm/kvm/arm_emulate.c +28
-0+ +/* +* Must be ordered with LOADS first and WRITES afterwards +* for easy distinction when doing MMIO. +*/ +#define NUM_LD_INSTR 9 +enum INSTR_LS_INDEXES { +INSTR_LS_LDRBT ,INSTR_LS_LDRT ,INSTR_LS_LDR ,INSTR_LS_LDRB , +INSTR_LS_LDRD ,INSTR_LS_LDREX ,INSTR_LS_LDRH ,INSTR_LS_LDRSB , +INSTR_LS_LDRSH , +INSTR_LS_STRBT ,INSTR_LS_STRT ,INSTR_LS_STR ,INSTR_LS_STRB , +INSTR_LS_STRD ,INSTR_LS_STREX ,INSTR_LS_STRH , +NUM_LS_INSTR +}; + +static u32 ls_instr[NUM_LS_INSTR ][2 ] = { + {0x04700000 ,0x0d700000 },/* LDRBT */ + {0x04300000 ,0x0d700000 },/* LDRT */ + {0x04100000 ,0x0c500000 },/* LDR */ + {0x04500000 ,0x0c500000 },/* LDRB */ + {0x000000d0 ,0x0e1000f0 },/* LDRD */ + {0x01900090 ,0x0ff000f0 },/* LDREX */ + {0x001000b0 ,0x0e1000f0 },/* LDRH */ + {0x001000d0 ,0x0e1000f0 },/* LDRSB */ + {0x001000f0 ,0x0e1000f0 },/* LDRSH */ + {0x04600000 ,0x0d700000 },/* STRBT */ + {0x04200000 ,0x0d700000 },/* STRT */ + {0x04000000 ,0x0c500000 },/* STR */
Quoted 8 lines
arch/arm/kvm/arm_emulate.c int kvm_handle_wfi(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 378
Quoted 6 lines
arch/arm/kvm/arm_emulate.c +6
-0+ {0x04400000, 0x0c500000}, /* STRB */ + {0x000000f0, 0x0e1000f0}, /* STRD */ + {0x01800090, 0x0ff000f0}, /* STREX */ + {0x000000b0, 0x0e1000f0} /* STRH */ +}; +Okay, maybe not. But surely there's some clever arithmetic the cpu uses to decode this.
Probably, but this is only used in the rare case when the virt. extensions doesn't support the fault information. I highly doubt that this is in any critical path for any sane guest OS, but surely one could write a VM that would run very slow. I would like not to spend time on this right now and perhaps get back to it when we have all sorts of other features in place. Or, what do you think?
Quoted 9 lines
Quoted 9 lines
arch/arm/kvm/trace.h +0
-0@@ -39,6 +39,21 @@ TRACE_EVENT(kvm_exit,TP_printk ("PC: 0x%08lx" , __entry->vcpu_pc) );
Quoted 5 lines
arch/arm/kvm/trace.h TRACE_EVENT(kvm_exit, · line 44
Quoted 3 lines
arch/arm/kvm/trace.h +3
-0+TRACE_EVENT(kvm_mmio_emulate, + TP_PROTO(unsigned long vcpu_pc), + TP_ARGS(vcpu_pc),Please add the instruction bytes and any other information needed to decode the opcode (e.g. thumb mode). Forx86 we have a trace-cmd plugin that disassembles guest instructions into the trace; it's very useful.
that's a good idea. I will look into it.
Quoted 22 lines
Quoted 18 lines
arch/arm/kvm/trace.h +12
-0+ +TP_STRUCT__entry ( +__field (unsigned long ,vcpu_pc ) + ), + +TP_fast_assign ( + __entry->vcpu_pc = vcpu_pc; + ), + +TP_printk ("Emulate MMIO at: 0x%08lx" , __entry->vcpu_pc) +); + TRACE_EVENT(kvm_emulate_cp15_imp, TP_PROTO(unsigned long Op1, unsigned long Rt1, unsigned long CRn, unsigned long CRm, unsigned long Op2, bool is_write),Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v4 09/10] ARM: KVM: Handle I/O aborts
Quoted 21 lines · Christoffer Dall
Quoted 21 lines
Quoted 20 lines
+
+static u32 ls_instr[NUM_LS_INSTR][2] = {
+ {0x04700000,0x0d700000}, /* LDRBT */+ {0x04300000, 0x0d700000}, /* LDRT */ + {0x04100000, 0x0c500000}, /* LDR */ + {0x04500000, 0x0c500000}, /* LDRB */ + {0x000000d0, 0x0e1000f0}, /* LDRD */ + {0x01900090, 0x0ff000f0}, /* LDREX */ + {0x001000b0, 0x0e1000f0}, /* LDRH */ + {0x001000d0, 0x0e1000f0}, /* LDRSB */ + {0x001000f0, 0x0e1000f0}, /* LDRSH */ + {0x04600000, 0x0d700000}, /* STRBT */ + {0x04200000, 0x0d700000}, /* STRT */ + {0x04000000, 0x0c500000}, /* STR */ + {0x04400000, 0x0c500000}, /* STRB */ + {0x000000f0, 0x0e1000f0}, /* STRD */+ {
0x01800090,0x0ff000f0}, /* STREX */
+ {0x000000b0,0x0e1000f0} /* STRH */
+};
+
Quoted 3 lines
Quoted 1 line
Okay, maybe not. But surely there's some clever arithmetic the cpu uses to decode this.
Probably, but this is only used in the rare case when the virt. extensions doesn't support the fault information. I highly doubt that this is in any critical path for any sane guest OS, but surely one could write a VM that would run very slow. I would like not to spend time on this right now and perhaps get back to it when we have all sorts of other features in place. Or, what do you think?
It's the ordinary case of premature optimization that afflicts even the best of us. Best to keep it simple.
Signature · 1 line
error compiling committee.c: too many arguments to function
PATCH v4 10/10 ARM: KVM: Guest wait-for-interrupts (WFI) support
In reply to Christoffer DallWhen the guest executes a WFI instruction the operation is trapped to KVM, which emulates the instruction in software. There is no correlation between a guest executing a WFI instruction and actually puttin the hardware into a low-power mode, since a KVM guest is essentially a process and the WFI instruction can be seen as 'sleep' call from this process. Therefore, we flag the VCPU to be in wait_for_interrupts mode and call the main KVM function kvm_vcpu_block() function. This function will put the thread on a wait-queue and call schedule.
When an interrupt comes in through KVM_IRQ_LINE (see previous patch) we signal the VCPU thread and unflag the VCPU to no longer wait for interrupts. All calls to kvm_arch_vcpu_ioctl_run() result in a call to kvm_vcpu_block() as long as the VCPU is in wfi-mode.
--- arch/arm/kvm/arm.c | 20 +++++++++++++++++++- arch/arm/kvm/arm_emulate.c | 11 +++++++++++ arch/arm/kvm/trace.h | 15 +++++++++++++++ 3 files changed, 45 insertions(+), 1 deletions(-)
arch/arm/kvm/arm.c +19 -1
@@ -307,9 +307,18 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,return -EINVAL ; } +/** +* kvm_arch_vcpu_runnable - determine if the vcpu can be scheduled +* @v: The VCPU pointer +* +* If the guest CPU is not waiting for interrupts (or is waiting for interrupts +* but there actually is an incoming interrupt), then it is by definition +* runnable. +*/ int kvm_arch_vcpu_runnable (struct kvm_vcpu *v) { -return 0 ; +return !!v->arch.virt_irq || + !v->arch.wait_for_interrupts; } static inline int handle_exit(struct kvm_vcpu *vcpu, struct kvm_run *run,
@@ -385,6 +394,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)int ret;for (;;) { +if (vcpu->arch.wait_for_interrupts) + goto wait_for_interrupts; +if (run->exit_reason ==KVM_EXIT_MMIO ) { ret =kvm_handle_mmio_return (vcpu, vcpu->run);if (ret)
@@ -420,6 +432,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)vcpu_load (vcpu); } +wait_for_interrupts: +if (vcpu->arch.wait_for_interrupts) +kvm_vcpu_block (vcpu); +if (signal_pending (current) && !(run->exit_reason)) { run->exit_reason =KVM_EXIT_IRQ_WINDOW_OPEN ;break ;
@@ -460,6 +476,8 @@ static int kvm_arch_vm_ioctl_irq_line(struct kvm *kvm,if (irq_level->level) { vcpu->arch.virt_irq |= mask; vcpu->arch.wait_for_interrupts =0 ; +if (waitqueue_active (&vcpu->wq)) +wake_up_interruptible (&vcpu->wq); }else vcpu->arch.virt_irq &= ~mask;
arch/arm/kvm/arm_emulate.c +11 -0
@@ -333,8 +333,19 @@ unsupp_err_out:return -EINVAL ; } +/** +* kvm_handle_wfi - handle a wait-for-interrupts instruction executed by a guest +* @vcpu: the vcpu pointer +* @run: the kvm_run structure pointer +* +* Simply sets the wait_for_interrupts flag on the vcpu structure, which will +* halt execution of world-switches and schedule other host processes until +* there is an incoming IRQ or FIQ to the VM. +*/ int kvm_handle_wfi (struct kvm_vcpu *vcpu,struct kvm_run *run) { +trace_kvm_wfi (vcpu->arch.regs.pc); + vcpu->arch.wait_for_interrupts =1 ;return 0 ; }
arch/arm/kvm/trace.h +15 -0
@@ -104,6 +104,21 @@ TRACE_EVENT(kvm_irq_line, __entry->level, __entry->vcpu_idx) ); +TRACE_EVENT (kvm_wfi, + TP_PROTO(unsigned long vcpu_pc), +TP_ARGS (vcpu_pc), + +TP_STRUCT__entry ( + __field( unsigned long, vcpu_pc ) + ), + +TP_fast_assign ( + __entry->vcpu_pc = vcpu_pc; + ), + +TP_printk ("guest executed wfi at: 0x%08lx" , __entry->vcpu_pc) +); +#endif /* _TRACE_KVM_H */
Quoted 24 lines · Christoffer Dall
The following series implements KVM support for ARM processors, specifically on the Cortex A-15 platform.
The patch series applies to the arm-lpae branch of ARM Ltd's kernel tree. This is Version 4 of the patch series, but the first two versions were reviewed outside of the KVM mailing list. Changes can also be pulled from:
git://git.ncl.cs.columbia.edu/pub/git/linux-kvm-arm kvm-a15-v4The implementation is broken up into a logical set of patches, the first one containing a skeleton of files, makefile changes, the basic user space interface and KVM architecture specific stubs. Subsequent patches implement parts of the system as listed:
1. Skeleton 2. Identity Mapping for Hyp mode 3. Hypervisor intitalization 4. Hyp mode memory mappings and 2nd stage preparation 5. World-switch implementation and Hyp exception vectors 6. Emulation framework and CP15 emulation 7. Handle guest user memory aborts 8. Handle guest MMIO aborts 9. Handle userspace IRQ/FIQ injection10. Support guest wait-for-interrupt instructions.
Quoted 8 lines
Testing:
Limited testing, but have run GCC inside guest, which compiled a small hellow-world program, which was successfully run. Hardware still unavailable, so all testing has been done on ARM Fast Models.For a guide on how to set up a testing environment and try out these patches, see:
http://wiki.ncl.cs.columbia.edu/wiki/KVMARM:Guides:Development_Environment
Pretty nice, and once again I congratulate you for not having to virtualize x86.
I don't know how close you feel you are to merging, but from my point of view things are looking good. We'll need to coordinate trees and acks since this is touching more than just arch/arm/kvm.
Quoted 7 lines
arch/arm/kvm/arm.c | 701 +++++++++++++++++++++++++++ arch/arm/kvm/arm_emulate.c | 604 +++++++++++++++++++++++ arch/arm/kvm/arm_exports.c | 26 + arch/arm/kvm/arm_guest.c | 150 ++++++ arch/arm/kvm/arm_init.S | 115 ++++ arch/arm/kvm/arm_interrupts.S | 488 +++++++++++++++++++ arch/arm/kvm/arm_mmu.c | 549 +++++++++++++++++++++
Suggest eliminating the arm_ prefixes.
Signature · 1 line
error compiling committee.c: too many arguments to function