KVM/ARM Implementation
Subsystems (2)
v2 cover KVM/ARM Implementation
The following series implements KVM support for ARM processors, specifically on the Cortex A-15 platform. We feel this is ready to be merged.
Work is done in collaboration between Columbia University, Virtual Open Systems and ARM/Linaro.
The patch series applies to Linux 3.6 with a number of merges:
1. git://git.kernel.org/pub/scm/linux/kernel/git/maz/arm-platforms.git
branch: hyp-mode-boot-next (e5a04cb0b4a)
2. git://git.kernel.org/pub/scm/linux/kernel/git/maz/arm-platforms.git
branch: timers-next (437814c44c)
3. git://git.kernel.org/pub/scm/virt/kvm/kvm.git
branch: next (1e08ec4a)This is Version 12 of the patch series, the first 10 versions were reviewed on the KVM/ARM and KVM mailing lists. Changes can also be pulled from:
git://github.com/virtualopensystems/linux-kvm-arm.git
branch: kvm-arm-v12
branch: kvm-arm-v12-vgic
branch: kvm-arm-v12-vgic-timersA non-flattened edition of the patch series, which can always be merged, can be found at:
git://github.com/virtualopensystems/linux-kvm-arm.git kvm-arm-master
This patch series requires QEMU compatibility. Use the branch
git://github.com/virtualopensystems/qemu.git kvm-arm
Following this patch series, which implements core KVM support are two other patch series implementing Virtual Generic Interrupt Controller (VGIC) support and Architected Generic Timers. All three patch series should be applied for full QEMU compatibility.
The implementation is broken up into a logical set of patches, the first are preparatory patches:
1. ARM: Add page table defines for KVM
3. ARM: Section based HYP idmaps
3. ARM: Factor out cpuid implementor and part_number fields
The main implementation is broken up into separate patches, the first 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:
4. Skeleton and reset hooks
5. Hypervisor initialization
6. Memory virtualization setup (hyp mode mappings and 2nd stage)
7. Inject IRQs and FIQs from userspace
8. World-switch implementation and Hyp exception vectors
9. Emulation framework and coproc emulation
10. Coproc user space API
11. Demux multiplexed coproc registers
12. User spac API to get/set VFP registers
13. Handle guest user memory aborts
14. Handle guest MMIO aborts
Testing:
Tested on FAST Models and Versatile Express test-chip2. Tested by running three simultaenous VMs, all running SMP, on an SMP host, each VM running hackbench and cyclictest and with extreme memory pressure applied to the host with swapping enabled to provoke page eviction. Also tested KSM merging and GCC inside VMs. Fully boots both Ubuntu (user space Thumb-2) and Debian (user space ARM) guests.
For a guide on how to set up a testing environment and try out these patches, see:
http://www.virtualopensystems.com/media/pdf/kvm-arm-guide.pdf
Changes since v11:
- Memory setup and page table defines reworked
- We do not export unused perf bitfields anymore
- No module support anymore and following cleanup
- Hide vcpu register accessors
- Fix unmap range mmu notifier race condition
- Factored out A15 coprocs in separate file
- Factored out world-switch assembly macros to separate file
- Add dmux of multiplexed coprocs to user space
- Add VFP get/set interface to user space
- Addressed various cleanup comments from reviewers
Changes since v10:
- Boot in Hyp mode and user HVC to initialize HVBAR
- Support VGIC
- Support Arch timers
- Support Thumb-2 mmio instruction decoding
- Transition to GET_ONE/SET_ONE register API
- Added KVM_VCPU_GET_REG_LIST
- New interrupt injection API
- Don't pin guest pages anymore
- Fix race condition in page fault handler
- Cleanup guest instruction copying.
- Fix race when copying SMP guest instructions
- Inject data/prefetch aborts when guest does something strange
Changes since v9:
- Addressed reviewer comments (see mailing list archive)
- Limit the user of .arch_extensiion sec/virt for compilers that need them
- VFP/Neon Support (Antonios Motakis)
- Run exit handling under preemption and still handle guest cache ops
- Add support for IO mapping at Hyp level (VGIC prep)
- Add support for IO mapping at Guest level (VGIC prep)
- Remove backdoor call to irq_svc
- Complete rework of CP15 handling and register reset (Rusty Russell)
- Don't use HSTR for anything else than CR 15
- New ioctl to set emulation target core (only A15 supported for now)
- Support KVM_GET_MSRS / KVM_SET_MSRS
- Add page accounting and page table eviction
- Change pgd lock to spinlock and fix sleeping in atomic bugs
- Check kvm_condition_valid for HVC traps of undefs
- Added a naive implementation of kvm_unmap_hva_range
Changes since v8:
- Support cache maintenance on SMP through set/way
- Hyp mode idmaps are now section based and happen at kernel init
- Handle aborts in Hyp mode
- Inject undefined exceptions into the guest on error
- Kernel-side reset of all crucial registers
- Specifically state which target CPU is being virtualized
- Exit statistics in debugfs
- Some L2CTLR cp15 emulation cleanups
- Support spte_hva for MMU notifiers and take write faults
- FIX: Race condition in VMID generation
- BUG: Run exit handling code with disabled preemption
- Save/Restore abort fault register during world switch
Changes since v7:
- Traps accesses to ACTLR
- Do not trap WFE execution
- Upgrade barriers and TLB operations to inner-shareable domain
- Restrucure hyp_pgd related code to be more opaque
- Random SMP fixes
- Random BUG fixes
- Improve commenting
- Support module loading/unloading of KVM/ARM
- Thumb-2 support for host kernel and KVM
- Unaligned cross-page wide guest Thumb instruction fetching
- Support ITSTATE fields in CPSR for Thumb guests
- Document HCR settings
Changes since v6:
- Support for MMU notifiers to not pin user pages in memory
- Suport build with log debugging
- Bugfix: v6 clobbered r7 in init code
- Simplify hyp code mapping
- Cleanup of register access code
- Table-based CP15 emulation from Rusty Russell
- Various other bug fixes and cleanups
Changes since v5:
- General bugfixes and nit fixes from reviews
- Implemented re-use of VMIDs
- Cleaned up the Hyp-mapping code to be readable by non-mm hackers (including myself)
- Integrated preliminary SMP support in base patches
- Lock-less interrupt injection and WFI support
- Fixed signal-handling in while in guest (increases overall stability)
Changes since v4:
- Addressed reviewer comments from v4
* cleanup debug and trace code
* remove printks
* fixup kvm_arch_vcpu_ioctl_run
* add trace details to mmio emulation
- Fix from Marc Zyngier: Move kvm_guest_enter/exit into non-preemptible section (squashed into world-switch patch)
- Cleanup create_hyp_mappings/remove_hyp_mappings from Marc Zyngier (squashed into hypervisor initialization patch)
- Removed the remove_hyp_mappings feature. Removing hypervisor mappings could potentially unmap other important data shared in the same page.
- Removed the arm_ prefix from the arch-specific files.
- Initial SMP host/guest support
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.
---
Marc Zyngier (10):
ARM: KVM: Keep track of currently running vcpus
ARM: KVM: Initial VGIC infrastructure support
ARM: KVM: Initial VGIC MMIO support code
ARM: KVM: VGIC distributor handling
ARM: KVM: VGIC virtual CPU interface management
ARM: KVM: VGIC interrupt injection
ARM: KVM: VGIC control interface world switch
ARM: KVM: VGIC initialisation code
ARM: KVM: vgic: reduce the number of vcpu kick
ARM: KVM: Add VGIC configuration option
arch/arm/include/asm/kvm_arm.h | 12 arch/arm/include/asm/kvm_host.h | 16 + arch/arm/include/asm/kvm_vgic.h | 301 +++++++++++ arch/arm/kernel/asm-offsets.c | 12 arch/arm/kvm/Kconfig | 7 arch/arm/kvm/Makefile | 1 arch/arm/kvm/arm.c | 101 +++- arch/arm/kvm/interrupts.S | 4 arch/arm/kvm/interrupts_head.S | 68 ++ arch/arm/kvm/mmu.c | 3 arch/arm/kvm/vgic.c | 1115 +++++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 5 12 files changed, 1640 insertions(+), 5 deletions(-) create mode 100644 arch/arm/include/asm/kvm_vgic.h create mode 100644 arch/arm/kvm/vgic.c
PATCH v2 1/10 ARM: KVM: Keep track of currently running vcpus
From: Marc Zyngier <[email protected]>
When an interrupt occurs for the guest, it is sometimes necessary to find out which vcpu was running at that point.
Keep track of which vcpu is being tun in kvm_arch_vcpu_ioctl_run(), and allow the data to be retrived using either:
- kvm_arm_get_running_vcpu(): returns the vcpu running at this point on the current CPU. Can only be used in a non-preemptable context.
- kvm_arm_get_running_vcpus(): returns the per-CPU variable holding the the running vcpus, useable for per-CPU interrupts.
--- arch/arm/include/asm/kvm_host.h | 9 +++++++++ arch/arm/kvm/arm.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+)
arch/arm/include/asm/kvm_host.h +9 -0
@@ -151,4 +151,13 @@ static inline int kvm_test_age_hva(struct kvm *kvm, unsigned long hva) {return 0 ; } + +struct kvm_vcpu *kvm_arm_get_running_vcpu (void ); +struct kvm_vcpu __percpu **kvm_get_running_vcpus (void ); + +int kvm_arm_copy_coproc_indices (struct kvm_vcpu *vcpu,u64 __user *uindices); +unsigned long kvm_arm_num_coproc_regs (struct kvm_vcpu *vcpu); +struct kvm_one_reg ; +int kvm_arm_coproc_get_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *); +int kvm_arm_coproc_set_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *);#endif /* __ARM_KVM_HOST_H__ */
arch/arm/kvm/arm.c +30 -0
@@ -53,11 +53,38 @@ static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page);static struct vfp_hard_struct __percpu *kvm_host_vfp_state;static unsigned long hyp_default_vectors; +/* Per-CPU variable containing the currently running vcpu. */ +static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_arm_running_vcpu); +/* The VMID used in the VTTBR */ static atomic64_t kvm_vmid_gen =ATOMIC64_INIT (1 );static u8 kvm_next_vmid;static DEFINE_SPINLOCK (kvm_vmid_lock); +static void kvm_arm_set_running_vcpu (struct kvm_vcpu *vcpu) +{ +BUG_ON (preemptible ()); +__get_cpu_var (kvm_arm_running_vcpu) = vcpu; +} + +/** +* kvm_arm_get_running_vcpu - get the vcpu running on the current CPU. +* Must be called from non-preemptible context +*/ +struct kvm_vcpu *kvm_arm_get_running_vcpu (void ) +{ +BUG_ON (preemptible ()); +return __get_cpu_var (kvm_arm_running_vcpu); +} + +/** +* kvm_arm_get_running_vcpus - get the per-CPU array on currently running vcpus. +*/ +struct kvm_vcpu __percpu **kvm_get_running_vcpus (void ) +{ +return &kvm_arm_running_vcpu; +} +int kvm_arch_hardware_enable (void *garbage) {return 0 ;
@@ -296,10 +323,13 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)cpumask_clear_cpu (cpu, &vcpu->arch.require_dcache_flush);flush_cache_all ();/* We'd really want v7_flush_dcache_all() */ } + +kvm_arm_set_running_vcpu (vcpu); }void kvm_arch_vcpu_put (struct kvm_vcpu *vcpu) { +kvm_arm_set_running_vcpu (NULL ); } int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
PATCH v2 2/10 ARM: KVM: Initial VGIC infrastructure support
In reply to Christoffer DallFrom: Marc Zyngier <[email protected]>
Wire the basic framework code for VGIC support. Nothing to enable yet.
--- arch/arm/include/asm/kvm_host.h | 7 ++++ arch/arm/include/asm/kvm_vgic.h | 65 +++++++++++++++++++++++++++++++++++++++ arch/arm/kvm/arm.c | 21 ++++++++++++- arch/arm/kvm/interrupts.S | 4 ++ arch/arm/kvm/mmu.c | 3 ++ virt/kvm/kvm_main.c | 5 ++- 6 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 arch/arm/include/asm/kvm_vgic.h
arch/arm/include/asm/kvm_host.h +7 -0
@@ -22,6 +22,7 @@#include <asm/kvm.h> #include <asm/kvm_asm.h> #include <asm/fpstate.h> +#include <asm/kvm_vgic.h> #define KVM_MAX_VCPUS NR_CPUS#define KVM_MEMORY_SLOTS 32
@@ -52,6 +53,9 @@ struct kvm_arch {/* VTTBR value associated with above pgd and vmid */ u64 vttbr; + +/* Interrupt controller */ +struct vgic_dist vgic; };#define KVM_NR_MEM_OBJS 40
@@ -87,6 +91,9 @@ struct kvm_vcpu_arch {struct vfp_hard_struct vfp_guest;struct vfp_hard_struct *vfp_host; +/* VGIC state */ +struct vgic_cpu vgic_cpu; +/* * Anything that is not used directly from assembly code goes * here.
arch/arm/include/asm/kvm_vgic.h new file +65 -0
65 lines not on this page · raw message
arch/arm/kvm/arm.c +20 -1
@@ -183,6 +183,9 @@ int kvm_dev_ioctl_check_extension(long ext) {int r;switch (ext) { +#ifdef CONFIG_KVM_ARM_VGIC +case KVM_CAP_IRQCHIP : +#endif case KVM_CAP_USER_MEMORY :case KVM_CAP_DESTROY_MEMORY_REGION_WORKS :case KVM_CAP_ONE_REG :
@@ -301,6 +304,10 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) {/* Force users to call KVM_ARM_VCPU_INIT */ vcpu->arch.target =-1 ; + +/* Set up VGIC */ +kvm_vgic_vcpu_init (vcpu); +return 0 ; }
@@ -360,7 +367,7 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,*/ int kvm_arch_vcpu_runnable (struct kvm_vcpu *v) { -return !!v->arch.irq_lines; +return !!v->arch.irq_lines ||kvm_vgic_vcpu_pending_irq (v); }int kvm_arch_vcpu_in_guest_mode (struct kvm_vcpu *v)
@@ -629,6 +636,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)update_vttbr (vcpu->kvm); +kvm_vgic_sync_to_cpu (vcpu); +local_irq_disable ();/*
@@ -641,6 +650,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)if (ret <=0 ||need_new_vmid_gen (vcpu->kvm)) {local_irq_enable (); +kvm_vgic_sync_from_cpu (vcpu);continue ; }
@@ -679,6 +689,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)* Back from guest *************************************************************/ +kvm_vgic_sync_from_cpu (vcpu); + ret =handle_exit (vcpu, run, ret); }
@@ -942,6 +954,13 @@ static int init_hyp_mode(void) } } +/* +* Init HYP view of VGIC +*/ + err =kvm_vgic_hyp_init (); +if (err) + goto out_free_mappings; +return 0 ; out_free_vfp:free_percpu (kvm_host_vfp_state);
arch/arm/kvm/interrupts.S +4 -0
@@ -104,6 +104,8 @@ ENTRY(__kvm_vcpu_run) store_mode_state sp, irq store_mode_state sp, fiq + restore_vgic_state r0 + @ Store hardware CP15 state and load guest state read_cp15_state write_cp15_state 1, r0
@@ -221,6 +223,8 @@ after_vfp_restore: read_cp15_state 1, r1 write_cp15_state + save_vgic_state r1 + load_mode_state sp, fiq load_mode_state sp, irq load_mode_state sp, und
arch/arm/kvm/mmu.c +3 -0
@@ -830,6 +830,9 @@ static int io_mem_abort(struct kvm_vcpu *vcpu, struct kvm_run *run,if (mmio.is_write)memcpy (mmio.data,vcpu_reg (vcpu, rd), mmio.len); +if (vgic_handle_mmio (vcpu, run, &mmio)) +return 1 ; +kvm_prepare_mmio (run, &mmio);return 0 ; }
virt/kvm/kvm_main.c +3 -2
@@ -1883,12 +1883,13 @@ static long kvm_vcpu_ioctl(struct file *filp,if (vcpu->kvm->mm != current->mm)return -EIO ; -#if defined(CONFIG_S390 ) || defined(CONFIG_PPC ) +#if defined(CONFIG_S390 ) || defined(CONFIG_PPC ) || defined(CONFIG_ARM )/* * Special cases: vcpu ioctls that are asynchronous to vcpu execution, * so vcpu_load() would break it. */ -if (ioctl ==KVM_S390_INTERRUPT || ioctl ==KVM_INTERRUPT ) +if (ioctl ==KVM_S390_INTERRUPT || ioctl ==KVM_INTERRUPT || + ioctl ==KVM_IRQ_LINE )return kvm_arch_vcpu_ioctl (filp, ioctl, arg);#endif
PATCH v2 3/10 ARM: KVM: Initial VGIC MMIO support code
In reply to Christoffer DallFrom: Marc Zyngier <[email protected]>
Wire the initial in-kernel MMIO support code for the VGIC, used for the distributor emulation.
--- arch/arm/include/asm/kvm_vgic.h | 6 +- arch/arm/kvm/Makefile | 1 arch/arm/kvm/vgic.c | 138 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 arch/arm/kvm/vgic.c
arch/arm/include/asm/kvm_vgic.h +5 -1
@@ -30,7 +30,11 @@ struct kvm_vcpu;struct kvm_run ;struct kvm_exit_mmio ; -#ifndef CONFIG_KVM_ARM_VGIC +#ifdef CONFIG_KVM_ARM_VGIC +bool vgic_handle_mmio (struct kvm_vcpu *vcpu,struct kvm_run *run, +struct kvm_exit_mmio *mmio); + +#else static inline int kvm_vgic_hyp_init (void ) {return 0 ;
arch/arm/kvm/Makefile +1 -0
@@ -20,3 +20,4 @@ obj-$(CONFIG_KVM_ARM_HOST) += $(addprefix ../../../virt/kvm/, kvm_main.o coalesc obj-$(CONFIG_KVM_ARM_HOST) += arm.o guest.o mmu.o emulate.o reset.o obj-$(CONFIG_KVM_ARM_HOST) += coproc.o coproc_a15.o +obj-$(CONFIG_KVM_ARM_VGIC) += vgic.o
arch/arm/kvm/vgic.c new file +138 -0
138 lines not on this page · raw message
PATCH v2 4/10 ARM: KVM: VGIC distributor handling
In reply to Christoffer DallFrom: Marc Zyngier <[email protected]>
Add the GIC distributor emulation code. A number of the GIC features are simply ignored as they are not required to boot a Linux guest.
--- arch/arm/include/asm/kvm_vgic.h | 170 ++++++++++++++ arch/arm/kvm/vgic.c | 475 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 644 insertions(+), 1 deletion(-)
arch/arm/include/asm/kvm_vgic.h +170 -0
@@ -19,7 +19,177 @@#ifndef __ASM_ARM_KVM_VGIC_H#define __ASM_ARM_KVM_VGIC_H +#include <linux/kernel.h> +#include <linux/kvm.h> +#include <linux/kvm_host.h> +#include <linux/irqreturn.h> +#include <linux/spinlock.h> +#include <linux/types.h> + +#define VGIC_NR_IRQS 128 +#define VGIC_NR_SHARED_IRQS (VGIC_NR_IRQS - 32) +#define VGIC_MAX_CPUS NR_CPUS + +/* Sanity checks... */ +#if (VGIC_MAX_CPUS >8 ) +#error Invalid number of CPU interfaces +#endif + +#if (VGIC_NR_IRQS &31 ) +#error "VGIC_NR_IRQS must be a multiple of 32" +#endif + +#if (VGIC_NR_IRQS >1024 ) +#error "VGIC_NR_IRQS must be <= 1024" +#endif + +/* +* The GIC distributor registers describing interrupts have two parts: +* - 32 per-CPU interrupts (SGI + PPI) +* - a bunch of shared interrups (SPI) +*/ +struct vgic_bitmap { +union { +u32 reg[1 ]; +unsigned long reg_ul[0 ]; + } percpu[VGIC_MAX_CPUS ]; +union { +u32 reg[VGIC_NR_SHARED_IRQS /32 ]; +unsigned long reg_ul[0 ]; + } shared; +}; + +static inline u32 *vgic_bitmap_get_reg (struct vgic_bitmap *x, +int cpuid,u32 offset) +{ + offset >>=2 ; +BUG_ON (offset > (VGIC_NR_IRQS /32 )); +if (!offset) +return x->percpu[cpuid].reg; +else +return x->shared.reg + offset -1 ; +} + +static inline int vgic_bitmap_get_irq_val (struct vgic_bitmap *x, +int cpuid,int irq) +{ +if (irq <32 ) +return test_bit (irq, x->percpu[cpuid].reg_ul); + +return test_bit (irq -32 , x->shared.reg_ul); +} + +static inline void vgic_bitmap_set_irq_val (struct vgic_bitmap *x, +int cpuid,int irq,int val) +{ +unsigned long *reg; + +if (irq <32 ) + reg = x->percpu[cpuid].reg_ul; +else { + reg = x->shared.reg_ul; + irq -=32 ; + } + +if (val) +set_bit (irq, reg); +else +clear_bit (irq, reg); +} + +static inline unsigned long *vgic_bitmap_get_cpu_map (struct vgic_bitmap *x, +int cpuid) +{ +if (unlikely (cpuid >=VGIC_MAX_CPUS )) +return NULL ; +return x->percpu[cpuid].reg_ul; +} + +static inline unsigned long *vgic_bitmap_get_shared_map (struct vgic_bitmap *x) +{ +return x->shared.reg_ul; +} + +struct vgic_bytemap { +union { +u32 reg[8 ]; +unsigned long reg_ul[0 ]; + } percpu[VGIC_MAX_CPUS ]; +union { +u32 reg[VGIC_NR_SHARED_IRQS /4 ]; +unsigned long reg_ul[0 ]; + } shared; +}; + +static inline u32 *vgic_bytemap_get_reg (struct vgic_bytemap *x, +int cpuid,u32 offset) +{ + offset >>=2 ; +BUG_ON (offset > (VGIC_NR_IRQS /4 )); +if (offset <4 ) +return x->percpu[cpuid].reg + offset; +else +return x->shared.reg + offset -8 ; +} + +static inline int vgic_bytemap_get_irq_val (struct vgic_bytemap *x, +int cpuid,int irq) +{ +u32 *reg, shift; + shift = (irq &3 ) *8 ; + reg =vgic_bytemap_get_reg (x, cpuid, irq); +return (*reg >> shift) &0xff ; +} + +static inline void vgic_bytemap_set_irq_val (struct vgic_bytemap *x, +int cpuid,int irq,int val) +{ +u32 *reg, shift; + shift = (irq &3 ) *8 ; + reg =vgic_bytemap_get_reg (x, cpuid, irq); + *reg &= ~(0xff << shift); + *reg |= (val &0xff ) << shift; +} +struct vgic_dist { +#ifdef CONFIG_KVM_ARM_VGIC +spinlock_t lock; + +/* Virtual control interface mapping */ +void __iomem *vctrl_base; + +/* Distributor mapping in the guest */ +unsigned long vgic_dist_base; +unsigned long vgic_dist_size; + +/* Distributor enabled */ +u32 enabled; + +/* Interrupt enabled (one bit per IRQ) */ +struct vgic_bitmap irq_enabled; + +/* Interrupt 'pin' level */ +struct vgic_bitmap irq_state; + +/* Level-triggered interrupt in progress */ +struct vgic_bitmap irq_active; + +/* Interrupt priority. Not used yet. */ +struct vgic_bytemap irq_priority; + +/* Level/edge triggered */ +struct vgic_bitmap irq_cfg; + +/* Source CPU per SGI and target CPU */ +u8 irq_sgi_sources[VGIC_MAX_CPUS ][16 ]; + +/* Target CPU for each IRQ */ +u8 irq_spi_cpu[VGIC_NR_SHARED_IRQS ]; +struct vgic_bitmap irq_spi_target[VGIC_MAX_CPUS ]; + +/* Bitmap indicating which CPU has something pending */ +unsigned long irq_pending_on_cpu; +#endif }; struct vgic_cpu {
arch/arm/kvm/vgic.c +474 -1
503 lines not on this page · raw message
PATCH v2 5/10 ARM: KVM: VGIC virtual CPU interface management
In reply to Christoffer DallFrom: Marc Zyngier <[email protected]>
Add VGIC virtual CPU interface code, picking pending interrupts from the distributor and stashing them in the VGIC control interface list registers.
--- arch/arm/include/asm/kvm_vgic.h | 41 +++++++ arch/arm/kvm/vgic.c | 224 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 264 insertions(+), 1 deletion(-)
arch/arm/include/asm/kvm_vgic.h +41 -0
@@ -193,17 +193,58 @@ struct vgic_dist { };struct vgic_cpu { +#ifdef CONFIG_KVM_ARM_VGIC +/* per IRQ to LR mapping */ +u8 vgic_irq_lr_map[VGIC_NR_IRQS ]; + +/* Pending interrupts on this VCPU */ + DECLARE_BITMAP( pending, VGIC_NR_IRQS); + +/* Bitmap of used/free list registers */ + DECLARE_BITMAP( lr_used, 64); + +/* Number of list registers on this CPU */ +int nr_lr; + +/* CPU vif control registers for world switch */ +u32 vgic_hcr; +u32 vgic_vmcr; +u32 vgic_misr;/* Saved only */ +u32 vgic_eisr[2 ];/* Saved only */ +u32 vgic_elrsr[2 ];/* Saved only */ +u32 vgic_apr; +u32 vgic_lr[64 ];/* A15 has only 4... */ +#endif }; +#define VGIC_HCR_EN (1 << 0) +#define VGIC_HCR_UIE (1 << 1) + +#define VGIC_LR_VIRTUALID (0x3ff << 0) +#define VGIC_LR_PHYSID_CPUID (7 << 10) +#define VGIC_LR_STATE (3 << 28) +#define VGIC_LR_PENDING_BIT (1 << 28) +#define VGIC_LR_ACTIVE_BIT (1 << 29) +#define VGIC_LR_EOI (1 << 19) + +#define VGIC_MISR_EOI (1 << 0) +#define VGIC_MISR_U (1 << 1) + +#define LR_EMPTY 0xff +struct kvm ;struct kvm_vcpu ;struct kvm_run ;struct kvm_exit_mmio ;#ifdef CONFIG_KVM_ARM_VGIC +void kvm_vgic_sync_to_cpu (struct kvm_vcpu *vcpu); +void kvm_vgic_sync_from_cpu (struct kvm_vcpu *vcpu); +int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu);bool vgic_handle_mmio (struct kvm_vcpu *vcpu,struct kvm_run *run,struct kvm_exit_mmio *mmio); +#define irqchip_in_kernel (k) (!!((k)->arch.vgic.vctrl_base))#else static inline int kvm_vgic_hyp_init (void ) {
arch/arm/kvm/vgic.c +223 -1
233 lines not on this page · raw message
Quoted 227 lines · Christoffer Dall
The following series implements KVM support for ARM processors, specifically on the Cortex A-15 platform. We feel this is ready to be merged.
Work is done in collaboration between Columbia University, Virtual Open Systems and ARM/Linaro.
The patch series applies to Linux 3.6 with a number of merges:
1.git://git.kernel.org/pub/scm/linux/kernel/git/maz/arm-platforms.gitbranch: hyp-mode-boot-next (e5a04cb0b4a) 2. git://git.kernel.org/pub/scm/linux/kernel/git/maz/arm-platforms.git branch: timers-next (437814c44c) 3. git://git.kernel.org/pub/scm/virt/kvm/kvm.git branch: next (1e08ec4a)This is Version 12 of the patch series, the first 10 versions were reviewed on the KVM/ARM and KVM mailing lists. Changes can also be pulled from:
git://github.com/virtualopensystems/linux-kvm-arm.git branch: kvm-arm-v12 branch: kvm-arm-v12-vgic branch: kvm-arm-v12-vgic-timersA non-flattened edition of the patch series, which can always be merged, can be found at:
git://github.com/virtualopensystems/linux-kvm-arm.gitkvm-arm-masterThis patch series requires QEMU compatibility. Use the branch
git://github.com/virtualopensystems/qemu.gitkvm-armFollowing this patch series, which implements core KVM support are two other patch series implementing Virtual Generic Interrupt Controller (VGIC) support and Architected Generic Timers. All three patch series should be applied for full QEMU compatibility.
The implementation is broken up into a logical set of patches, the first are preparatory patches:
1. ARM: Add page table defines for KVM
3. ARM: Section based HYP idmaps
3. ARM: Factor out cpuid implementor andpart_numberfieldsThe main implementation is broken up into separate patches, the first 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:
4. Skeleton and reset hooks
5. Hypervisor initialization
6. Memory virtualization setup (hyp mode mappings and 2nd stage)
7. Inject IRQs and FIQs from userspace
8. World-switch implementation and Hyp exception vectors
9. Emulation framework and coproc emulation
10. Coproc user space API
11. Demux multiplexed coproc registers
12. User spac API to get/set VFP registers
13. Handle guest user memory aborts
14. Handle guest MMIO abortsTesting:
Tested on FAST Models and Versatile Express test-chip2. Tested by running three simultaenous VMs, all running SMP, on an SMP host, each VM running hackbench and cyclictest and with extreme memory pressure applied to the host with swapping enabled to provoke page eviction. Also tested KSM merging and GCC inside VMs. Fully boots both Ubuntu (user space Thumb-2) and Debian (user space ARM) guests.For a guide on how to set up a testing environment and try out these patches, see:
http://www.virtualopensystems.com/media/pdf/kvm-arm-guide.pdfChanges since v11:
- Memory setup and page table defines reworked
- We do not export unused perf bitfields anymore
- No module support anymore and following cleanup
- Hide vcpu register accessors
- Fix unmap range mmu notifier race condition
- Factored out A15 coprocs in separate file
- Factored out world-switch assembly macros to separate file
- Add dmux of multiplexed coprocs to user space
- Add VFP get/set interface to user space
- Addressed various cleanup comments from reviewersChanges since v10:
- Boot in Hyp mode and user HVC to initialize HVBAR
- Support VGIC
- Support Arch timers
- Support Thumb-2 mmio instruction decoding
- Transition to GET_ONE/SET_ONE register API
- AddedKVM_VCPU_GET_REG_LIST
- New interrupt injection API
- Don't pin guest pages anymore
- Fix race condition in page fault handler
- Cleanup guest instruction copying.
- Fix race when copying SMP guest instructions
- Inject data/prefetch aborts when guest does something strangeChanges since v9:
- Addressed reviewer comments (see mailing list archive)
- Limit the user of .arch_extensiion sec/virt for compilers that need them
- VFP/Neon Support (Antonios Motakis)
- Run exit handling under preemption and still handle guest cache ops
- Add support for IO mapping at Hyp level (VGIC prep)
- Add support for IO mapping at Guest level (VGIC prep)
- Remove backdoor call toirq_svc
- Complete rework of CP15 handling and register reset (Rusty Russell)
- Don't use HSTR for anything else than CR 15
- New ioctl to set emulation target core (only A15 supported for now)
- SupportKVM_GET_MSRS/KVM_SET_MSRS
- Add page accounting and page table eviction
- Change pgd lock to spinlock and fix sleeping in atomic bugs
- Checkkvm_condition_validfor HVC traps of undefs
- Added a naive implementation ofkvm_unmap_hva_rangeChanges since v8:
- Support cache maintenance on SMP through set/way
- Hyp mode idmaps are now section based and happen at kernel init
- Handle aborts in Hyp mode
- Inject undefined exceptions into the guest on error
- Kernel-side reset of all crucial registers
- Specifically state which target CPU is being virtualized
- Exit statistics in debugfs
- Some L2CTLR cp15 emulation cleanups
- Supportspte_hvafor MMU notifiers and take write faults
- FIX: Race condition in VMID generation
- BUG: Run exit handling code with disabled preemption
- Save/Restore abort fault register during world switchChanges since v7:
- Traps accesses to ACTLR
- Do not trap WFE execution
- Upgrade barriers and TLB operations to inner-shareable domain
- Restrucurehyp_pgdrelated code to be more opaque
- Random SMP fixes
- Random BUG fixes
- Improve commenting
- Support module loading/unloading of KVM/ARM
- Thumb-2 support for host kernel and KVM
- Unaligned cross-page wide guest Thumb instruction fetching
- Support ITSTATE fields in CPSR for Thumb guests
- Document HCR settingsChanges since v6:
- Support for MMU notifiers to not pin user pages in memory
- Suport build with log debugging
- Bugfix: v6 clobbered r7 in init code
- Simplify hyp code mapping
- Cleanup of register access code
- Table-based CP15 emulation from Rusty Russell
- Various other bug fixes and cleanupsChanges since v5:
- General bugfixes and nit fixes from reviews
- Implemented re-use of VMIDs
- Cleaned up the Hyp-mapping code to be readable by non-mm hackers (including myself)
- Integrated preliminary SMP support in base patches
- Lock-less interrupt injection and WFI support
- Fixed signal-handling in while in guest (increases overall stability)Changes since v4:
- Addressed reviewer comments from v4
* cleanup debug and trace code
* remove printks
* fixupkvm_arch_vcpu_ioctl_run
* add trace details to mmio emulation
- Fix from Marc Zyngier: Move kvm_guest_enter/exit into non-preemptible section (squashed into world-switch patch)
- Cleanup create_hyp_mappings/remove_hyp_mappings from Marc Zyngier (squashed into hypervisor initialization patch)
- Removed theremove_hyp_mappingsfeature. Removing hypervisor mappings could potentially unmap other important data shared in the same page.
- Removed thearm_prefix from the arch-specific files.
- Initial SMP host/guest supportChanges since v3:
- v4 actually works, fully boots a guest
- Support compiling as a module
- Use static inlines instead of macros forvcpu_regand friends
- Optimizekvm_vcpu_regfunction
- Use Ftrace for trace capabilities
- Updated documentation and commenting
- UseKVM_IRQ_LINEinstead ofKVM_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 modeChanges since v2:
- Performs world-switch code
- Maps guest memory using 2nd stage translation
- Emulates co-processor 15 instructions
- Forwards I/O faults to QEMU.---
Marc Zyngier (10):
ARM: KVM: Keep track of currently running vcpus
ARM: KVM: Initial VGIC infrastructure support
ARM: KVM: Initial VGIC MMIO support code
ARM: KVM: VGIC distributor handling
ARM: KVM: VGIC virtual CPU interface management
ARM: KVM: VGIC interrupt injection
ARM: KVM: VGIC control interface world switch
ARM: KVM: VGIC initialisation code
ARM: KVM: vgic: reduce the number of vcpu kick
ARM: KVM: Add VGIC configuration optionarch/arm/include/asm/kvm_arm.h | 12 arch/arm/include/asm/kvm_host.h | 16 + arch/arm/include/asm/kvm_vgic.h | 301 +++++++++++ arch/arm/kernel/asm-offsets.c | 12 arch/arm/kvm/Kconfig | 7 arch/arm/kvm/Makefile | 1 arch/arm/kvm/arm.c | 101 +++- arch/arm/kvm/interrupts.S | 4 arch/arm/kvm/interrupts_head.S | 68 ++ arch/arm/kvm/mmu.c | 3 arch/arm/kvm/vgic.c | 1115 +++++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 5 12 files changed, 1640 insertions(+), 5 deletions(-) create mode 100644 arch/arm/include/asm/kvm_vgic.h create mode 100644 arch/arm/kvm/vgic.c
Quoted 1 line
--
Please disregard this first mail-out, I messed up my stgit config.
-Christoffer