KVM/ARM Implementation
Subsystems (3)
v6 cover KVM/ARM Implementation
The following series implements KVM support for ARM processors, specifically on the Cortex A-15 platform.
Work is done in collaboration between Columbia University, Virtual Open Systems and ARM/Linaro.
The patch series applies to Linux 3.8-rc3.
The series relies on two additional patches in Will Deacon's perf tree:
ARM: Define CPU part numbers and implementors
ARM: Use implementor and part defines from cputype.h
This is Version 16 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-v16
branch: kvm-arm-v16-vgic
branch: kvm-arm-v16-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
There is also WIP QEMU patches to support virtio on ARM:
git://github.com/virtualopensystems/qemu.git kvm-arm-virtio
There is also a rebasing WIP branch with support for huge pages:
git://github.com/virtualopensystems/linux-kvm-arm.git kvm-arm-hugetlb
Finally there is kvmtool support available for the mach-virt machine:
git://git.kernel.org/pub/scm/linux/kernel/git/will/kvmtool.git
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
2. ARM: Section based HYP idmaps
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:
3. Skeleton and reset hooks
4. Hypervisor initialization
5. Memory virtualization setup (hyp mode mappings and 2nd stage)
6. Inject IRQs and FIQs from userspace
7. World-switch implementation and Hyp exception vectors
8. Emulation framework and coproc emulation
9. Coproc user space API
10. Demux multiplexed coproc registers
11. User spac API to get/set VFP registers
12. Handle guest user memory aborts
13. Handle guest MMIO aborts
14. Support PSCI interface
15. Add an entry in the MAINTAINERS file
Testing:
Tested on the Versatile Express TC2 devboard and on the Arndale board. running 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 swapping on the host. Fully boots both Ubuntu (user space Thumb-2) and Debian (user space ARM) guests each of which can run a number of worloads like apache, mysql, kernel compile, network tests, and more.
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 v15:
- Removed load/store MMIO instruction decoding
- Remove unused memory slot parameter from io_mem_abort
- Grab kvm->srcu lock when needed
- Minor fixes to world-switch code
- Minor fixes and updates from reviewers
- Reuse kvm_call_hyp in vcpu_init_hyp_mode
- Enable dcaches in hyp mode
- Add PSCI support
- Move one-time init to its own function in the run loop
- Add trace event for HVC calls
Changes since v14:
- Fixed permission fault handling by correctly retrieving the IPA on Stage-2 permission faults
- Fix compile error when !CONFIG_KVM_ARM_HOST
- Support building into separate object directory
- Fixed the Vodoo Bug (see
https://github.com/virtualopensystems/linux-kvm-arm/wiki/Voodoo-Bug)
- Improved some tracepoint debugs
- Improved and cleaned up VTCR and VTTBR initialization
- Clarified and unified Stage-2 page table clearing
- Addressed a large number of concerns from Will Deacon's review, including fixing a race condition and removing unused exports.
- Be a little more verbose when something goes wrong during the init process.
Changes since v13:
- Fix VTTBR mask bug
- Change KVM_MAX_VCPUS to config option (defualt 4)
- Go back to struct pt_regs in kvm_regs struct
- Factor out mmio instruction decoding to a separate file with non kvm-specific data structures as the interface.
- Update kvm_device_address struct to use 64-bit fields
- Various cleanups and compile fixes
Changes since v12:
- Documentation updates
- Change Hyp-ABI to function call based paradigm
- Cleanup world-switch code
- Unify HIFAR/HDFAR on the vcpu struct
- Simplify vcpu register access in sofware
- Enforce use of vcpu field accessors
- Factor out mmio handling into separate file
- Check for overlaps in mmio address mappings
- Bugfix in mmio decoding
- Complete rework of ARM mmio load/store instruction
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 v6:
- 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.
---
Christoffer Dall (13):
ARM: Add page table and page defines needed by KVM
ARM: Section based HYP idmap
KVM: ARM: Initial skeleton to compile KVM support
KVM: ARM: Hypervisor initialization
KVM: ARM: Memory virtualization setup
KVM: ARM: Inject IRQs and FIQs from userspace
KVM: ARM: World-switch implementation
KVM: ARM: Emulation framework and CP15 emulation
trom: Christoffer Dall <[email protected]>
KVM: ARM: Demux CCSIDR in the userspace API
KVM: ARM: Handle guest faults in KVM
KVM: ARM: Handle I/O aborts
KVM: ARM: Add maintainer entry for KVM/ARM
Marc Zyngier (1):
KVM: ARM: Power State Coordination Interface implementation
Rusty Russell (1):
KVM: ARM: VFP userspace interface
Documentation/virtual/kvm/api.txt | 99 ++- MAINTAINERS | 8 arch/arm/Kconfig | 2 arch/arm/Makefile | 1 arch/arm/include/asm/idmap.h | 1 arch/arm/include/asm/kvm_arm.h | 214 ++++++ arch/arm/include/asm/kvm_asm.h | 84 ++ arch/arm/include/asm/kvm_coproc.h | 47 + arch/arm/include/asm/kvm_emulate.h | 67 ++ arch/arm/include/asm/kvm_host.h | 161 ++++ arch/arm/include/asm/kvm_mmio.h | 56 + arch/arm/include/asm/kvm_mmu.h | 50 + arch/arm/include/asm/kvm_psci.h | 23 + arch/arm/include/asm/pgtable-3level-hwdef.h | 5 arch/arm/include/asm/pgtable-3level.h | 18 arch/arm/include/asm/pgtable.h | 7 arch/arm/include/uapi/asm/kvm.h | 164 ++++ arch/arm/kernel/asm-offsets.c | 25 + arch/arm/kernel/vmlinux.lds.S | 6 arch/arm/kvm/Kconfig | 56 + arch/arm/kvm/Makefile | 21 + arch/arm/kvm/arm.c | 1010 ++++++++++++++++++++++++++ arch/arm/kvm/coproc.c | 1046 +++++++++++++++++++++++++++ arch/arm/kvm/coproc.h | 153 ++++ arch/arm/kvm/coproc_a15.c | 162 ++++ arch/arm/kvm/emulate.c | 373 ++++++++++ arch/arm/kvm/guest.c | 222 ++++++ arch/arm/kvm/init.S | 114 +++ arch/arm/kvm/interrupts.S | 493 +++++++++++++ arch/arm/kvm/interrupts_head.S | 441 +++++++++++ arch/arm/kvm/mmio.c | 153 ++++ arch/arm/kvm/mmu.c | 787 ++++++++++++++++++++ arch/arm/kvm/psci.c | 105 +++ arch/arm/kvm/reset.c | 74 ++ arch/arm/kvm/trace.h | 235 ++++++ arch/arm/mm/idmap.c | 55 + arch/arm/mm/mmu.c | 22 + include/uapi/linux/kvm.h | 9 38 files changed, 6549 insertions(+), 20 deletions(-) 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_coproc.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_mmio.h create mode 100644 arch/arm/include/asm/kvm_mmu.h create mode 100644 arch/arm/include/asm/kvm_psci.h create mode 100644 arch/arm/include/uapi/asm/kvm.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/coproc.c create mode 100644 arch/arm/kvm/coproc.h create mode 100644 arch/arm/kvm/coproc_a15.c create mode 100644 arch/arm/kvm/emulate.c create mode 100644 arch/arm/kvm/guest.c create mode 100644 arch/arm/kvm/init.S create mode 100644 arch/arm/kvm/interrupts.S create mode 100644 arch/arm/kvm/interrupts_head.S create mode 100644 arch/arm/kvm/mmio.c create mode 100644 arch/arm/kvm/mmu.c create mode 100644 arch/arm/kvm/psci.c create mode 100644 arch/arm/kvm/reset.c create mode 100644 arch/arm/kvm/trace.h
PATCH v6 1/15 ARM: Add page table and page defines needed by KVM
KVM uses the stage-2 page tables and the Hyp page table format, so we define the fields and page protection flags needed by KVM.
The nomenclature is this:
- page_hyp: PL2 code/data mappings
- page_hyp_device: PL2 device mappings (vgic access)
- page_s2: Stage-2 code/data page mappings
- page_s2_device: Stage-2 device mappings (vgic access)
Christoffer Dall <[email protected]>
--- arch/arm/include/asm/pgtable-3level.h | 18 ++++++++++++++++++ arch/arm/include/asm/pgtable.h | 7 +++++++ arch/arm/mm/mmu.c | 22 ++++++++++++++++++++++ 3 files changed, 47 insertions(+)
arch/arm/include/asm/pgtable-3level.h +18 -0
@@ -104,11 +104,29 @@*/ #define L_PGD_SWAPPER (_AT(pgdval_t, 1) << 55)/* swapper_pg_dir entry */ +/* +* 2nd stage PTE definitions for LPAE. +*/ +#define L_PTE_S2_MT_UNCACHED (_AT(pteval_t, 0x5) << 2)/* MemAttr[3:0] */ +#define L_PTE_S2_MT_WRITETHROUGH (_AT(pteval_t, 0xa) << 2)/* MemAttr[3:0] */ +#define L_PTE_S2_MT_WRITEBACK (_AT(pteval_t, 0xf) << 2)/* MemAttr[3:0] */ +#define L_PTE_S2_RDONLY (_AT(pteval_t, 1) << 6)/* HAP[1] */ +#define L_PTE_S2_RDWR (_AT(pteval_t, 2) << 6)/* HAP[2:1] */ + +/* +* Hyp-mode PL2 PTE definitions for LPAE. +*/ +#define L_PTE_HYP L_PTE_USER +#ifndef __ASSEMBLY__#define pud_none (pud) (!pud_val(pud))#define pud_bad (pud) (!(pud_val(pud) & 2))#define pud_present (pud) (pud_val(pud)) +#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 pud_clear (pudp) \ do { \
arch/arm/include/asm/pgtable.h +7 -0
@@ -70,6 +70,9 @@ 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_hyp_device; +extern pgprot_t pgprot_s2; +extern pgprot_t pgprot_s2_device;#define _MOD_PROT (p, b) __pgprot(pgprot_val(p) | (b))
@@ -82,6 +85,10 @@ 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_HYP) +#define PAGE_HYP_DEVICE _MOD_PROT(pgprot_hyp_device, L_PTE_HYP) +#define PAGE_S2 _MOD_PROT(pgprot_s2, L_PTE_S2_RDONLY) +#define PAGE_S2_DEVICE _MOD_PROT(pgprot_s2_device, L_PTE_USER | L_PTE_S2_RDONLY)#define __PAGE_NONE __pgprot(_L_PTE_DEFAULT | L_PTE_RDONLY | L_PTE_XN | L_PTE_NONE)#define __PAGE_SHARED __pgprot(_L_PTE_DEFAULT | L_PTE_USER | L_PTE_XN)
arch/arm/mm/mmu.c +22 -0
@@ -57,6 +57,9 @@ 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_hyp_device; +pgprot_t pgprot_s2; +pgprot_t pgprot_s2_device;EXPORT_SYMBOL (pgprot_user);EXPORT_SYMBOL (pgprot_kernel);
@@ -66,34 +69,46 @@ struct cachepolicy {unsigned int cr_mask;pmdval_t pmd;pteval_t pte; +pteval_t pte_s2; }; +#ifdef CONFIG_ARM_LPAE +#define s2_policy (policy) policy +#else +#define s2_policy (policy) 0 +#endif +static struct cachepolicy cache_policies[]__initdata = { { .policy ="uncached" , .cr_mask =CR_W |CR_C , .pmd =PMD_SECT_UNCACHED , .pte =L_PTE_MT_UNCACHED , + .pte_s2 =s2_policy (L_PTE_S2_MT_UNCACHED ), }, { .policy ="buffered" , .cr_mask =CR_C , .pmd =PMD_SECT_BUFFERED , .pte =L_PTE_MT_BUFFERABLE , + .pte_s2 =s2_policy (L_PTE_S2_MT_UNCACHED ), }, { .policy ="writethrough" , .cr_mask =0 , .pmd =PMD_SECT_WT , .pte =L_PTE_MT_WRITETHROUGH , + .pte_s2 =s2_policy (L_PTE_S2_MT_WRITETHROUGH ), }, { .policy ="writeback" , .cr_mask =0 , .pmd =PMD_SECT_WB , .pte =L_PTE_MT_WRITEBACK , + .pte_s2 =s2_policy (L_PTE_S2_MT_WRITEBACK ), }, { .policy ="writealloc" , .cr_mask =0 , .pmd =PMD_SECT_WBWA , .pte =L_PTE_MT_WRITEALLOC , + .pte_s2 =s2_policy (L_PTE_S2_MT_WRITEBACK ), } };
@@ -310,6 +325,7 @@ static void __init build_mem_type_table(void)struct cachepolicy *cp;unsigned int cr =get_cr ();pteval_t user_pgprot, kern_pgprot, vecs_pgprot; +pteval_t hyp_device_pgprot, s2_pgprot, s2_device_pgprot;int cpu_arch =cpu_architecture ();int i;
@@ -421,6 +437,8 @@ static void __init build_mem_type_table(void)*/ cp = &cache_policies[cachepolicy]; vecs_pgprot = kern_pgprot = user_pgprot = cp->pte; + s2_pgprot = cp->pte_s2; + hyp_device_pgprot = s2_device_pgprot = mem_types[MT_DEVICE ].prot_pte;/* * ARMv6 and above have extended page tables.
@@ -444,6 +462,7 @@ static void __init build_mem_type_table(void) user_pgprot |=L_PTE_SHARED ; kern_pgprot |=L_PTE_SHARED ; vecs_pgprot |=L_PTE_SHARED ; + s2_pgprot |=L_PTE_SHARED ; mem_types[MT_DEVICE_WC ].prot_sect |=PMD_SECT_S ; mem_types[MT_DEVICE_WC ].prot_pte |=L_PTE_SHARED ; mem_types[MT_DEVICE_CACHED ].prot_sect |=PMD_SECT_S ;
@@ -498,6 +517,9 @@ 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_s2 =__pgprot (L_PTE_PRESENT |L_PTE_YOUNG | s2_pgprot); + pgprot_s2_device =__pgprot (s2_device_pgprot); + pgprot_hyp_device =__pgprot (hyp_device_pgprot); mem_types[MT_LOW_VECTORS ].prot_l1 |= ecc_mask; mem_types[MT_HIGH_VECTORS ].prot_l1 |= ecc_mask;
Re: [PATCH v6 01/15] ARM: Add page table and page defines needed by KVM
Hi Christoffer,
Quoted 6 lines · Christoffer Dall
arch/arm/include/asm/pgtable-3level.h +0
-0@@ -104,11 +104,29 @@*/
arch/arm/include/asm/pgtable-3level.h line 114
Quoted 10 lines
arch/arm/include/asm/pgtable-3level.h +8
-0#define L_PGD_SWAPPER (_AT(pgdval_t, 1) << 55)/* swapper_pg_dir entry */ +/* +* 2nd stage PTE definitions for LPAE. +*/ +#define L_PTE_S2_MT_UNCACHED (_AT(pteval_t, 0x5) << 2)/* MemAttr[3:0] */ +#define L_PTE_S2_MT_WRITETHROUGH (_AT(pteval_t, 0xa) << 2)/* MemAttr[3:0] */ +#define L_PTE_S2_MT_WRITEBACK (_AT(pteval_t, 0xf) << 2)/* MemAttr[3:0] */ +#define L_PTE_S2_RDONLY (_AT(pteval_t, 1) << 6)/* HAP[1] */ +#define L_PTE_S2_RDWR (_AT(pteval_t, 2) << 6)/* HAP[2:1] */
Why is this called L_PTE_S2_RDWR? The ARM ARM shows HAP[2:1] as 0b11 for read/write and 0b10 for WRONLY.
Quoted 13 lines
arch/arm/include/asm/pgtable.h +3
-0@@ -70,6 +70,9 @@ 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_hyp_device; +extern pgprot_t pgprot_s2; +extern pgprot_t pgprot_s2_device;#define _MOD_PROT (p, b) __pgprot(pgprot_val(p) | (b))
arch/arm/include/asm/pgtable.h extern pgprot_t pgprot_kernel; · line 91
Quoted 7 lines
arch/arm/include/asm/pgtable.h +4
-0@@ -82,6 +85,10 @@ 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_HYP) +#define PAGE_HYP_DEVICE _MOD_PROT(pgprot_hyp_device, L_PTE_HYP) +#define PAGE_S2 _MOD_PROT(pgprot_s2, L_PTE_S2_RDONLY) +#define PAGE_S2_DEVICE _MOD_PROT(pgprot_s2_device, L_PTE_USER | L_PTE_S2_RDONLY)
Are these S2 read-only by default?
Signature · 1 line
Catalin
Re: [PATCH v6 01/15] ARM: Add page table and page defines needed by KVM
Quoted 11 lines · Catalin Marinas
Hi Christoffer,
Quoted 8 lines · Christoffer Dall
arch/arm/include/asm/pgtable-3level.h +0
-0@@ -104,11 +104,29 @@*/ #define L_PGD_SWAPPER (_AT(pgdval_t, 1) << 55)/* swapper_pg_dir entry */
Quoted 12 lines
arch/arm/include/asm/pgtable-3level.h line 114
Quoted 8 lines
arch/arm/include/asm/pgtable-3level.h +8
-0+/* +* 2nd stage PTE definitions for LPAE. +*/ +#define L_PTE_S2_MT_UNCACHED (_AT(pteval_t, 0x5) << 2)/* MemAttr[3:0] */ +#define L_PTE_S2_MT_WRITETHROUGH (_AT(pteval_t, 0xa) << 2)/* MemAttr[3:0] */ +#define L_PTE_S2_MT_WRITEBACK (_AT(pteval_t, 0xf) << 2)/* MemAttr[3:0] */ +#define L_PTE_S2_RDONLY (_AT(pteval_t, 1) << 6)/* HAP[1] */ +#define L_PTE_S2_RDWR (_AT(pteval_t, 2) << 6)/* HAP[2:1] */ Why is this called
L_PTE_S2_RDWR? The ARM ARM shows HAP[2:1] as 0b11 for read/write and 0b10 for WRONLY.
it should be
+#define L_PTE_S2_RDWR (_AT(pteval_t, 3) << 6) /* HAP[2:1] */
Quoted 12 lines
Quoted 12 lines
arch/arm/include/asm/pgtable.h +3
-0@@ -70,6 +70,9 @@ 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_hyp_device; +extern pgprot_t pgprot_s2; +extern pgprot_t pgprot_s2_device;#define _MOD_PROT (p, b) __pgprot(pgprot_val(p) | (b))
Quoted 11 lines
arch/arm/include/asm/pgtable.h extern pgprot_t pgprot_kernel; · line 91
Quoted 8 lines
arch/arm/include/asm/pgtable.h +4
-0@@ -82,6 +85,10 @@ 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_HYP) +#define PAGE_HYP_DEVICE _MOD_PROT(pgprot_hyp_device, L_PTE_HYP) +#define PAGE_S2 _MOD_PROT(pgprot_s2, L_PTE_S2_RDONLY) +#define PAGE_S2_DEVICE _MOD_PROT(pgprot_s2_device, L_PTE_USER | L_PTE_S2_RDONLY)Are these S2 read-only by default?
not sure I understand what you're asking here?
Re: [PATCH v6 01/15] ARM: Add page table and page defines needed by KVM
Quoted 23 lines · Christoffer Dall
Quoted 9 lines · Catalin Marinas
Quoted 8 lines · Christoffer Dall
arch/arm/include/asm/pgtable-3level.h +0
-0@@ -104,11 +104,29 @@*/ #define L_PGD_SWAPPER (_AT(pgdval_t, 1) << 55)/* swapper_pg_dir entry */ Quoted 11 lines
arch/arm/include/asm/pgtable-3level.h line 114
Quoted 8 lines
arch/arm/include/asm/pgtable-3level.h +8
-0+/* +* 2nd stage PTE definitions for LPAE. +*/ +#define L_PTE_S2_MT_UNCACHED (_AT(pteval_t, 0x5) << 2)/* MemAttr[3:0] */ +#define L_PTE_S2_MT_WRITETHROUGH (_AT(pteval_t, 0xa) << 2)/* MemAttr[3:0] */ +#define L_PTE_S2_MT_WRITEBACK (_AT(pteval_t, 0xf) << 2)/* MemAttr[3:0] */ +#define L_PTE_S2_RDONLY (_AT(pteval_t, 1) << 6)/* HAP[1] */ +#define L_PTE_S2_RDWR (_AT(pteval_t, 2) << 6)/* HAP[2:1] */ Why is this called
L_PTE_S2_RDWR? The ARM ARM shows HAP[2:1] as 0b11 for read/write and 0b10 for WRONLY.
Quoted 3 lines
it should be
+#define
L_PTE_S2_RDWR(_AT(pteval_t, 3)<< 6) /* HAP[2:1] */
OK.
Quoted 24 lines
Quoted 13 lines
Quoted 13 lines
arch/arm/include/asm/pgtable.h +3
-0@@ -70,6 +70,9 @@ 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_hyp_device; +extern pgprot_t pgprot_s2; +extern pgprot_t pgprot_s2_device;#define _MOD_PROT (p, b) __pgprot(pgprot_val(p) | (b))Quoted 10 lines
arch/arm/include/asm/pgtable.h extern pgprot_t pgprot_kernel; · line 91
Quoted 8 lines
arch/arm/include/asm/pgtable.h +4
-0@@ -82,6 +85,10 @@ 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_HYP) +#define PAGE_HYP_DEVICE _MOD_PROT(pgprot_hyp_device, L_PTE_HYP) +#define PAGE_S2 _MOD_PROT(pgprot_s2, L_PTE_S2_RDONLY) +#define PAGE_S2_DEVICE _MOD_PROT(pgprot_s2_device, L_PTE_USER | L_PTE_S2_RDONLY)Are these S2 read-only by default?
Quoted 1 line
not sure I understand what you're asking here?
Why do you pass L_PTE_S2_RDONLY? Do you need to restrict guest writes via the stage 2 attributes?
Signature · 1 line
Catalin
Re: [PATCH v6 01/15] ARM: Add page table and page defines needed by KVM
Quoted 54 lines · Catalin Marinas
Quoted 23 lines · Christoffer Dall
Quoted 9 lines · Catalin Marinas
Quoted 8 lines · Christoffer Dall
arch/arm/include/asm/pgtable-3level.h +0
-0@@ -104,11 +104,29 @@*/ #define L_PGD_SWAPPER (_AT(pgdval_t, 1) << 55)/* swapper_pg_dir entry */ Quoted 11 lines
arch/arm/include/asm/pgtable-3level.h line 114
Quoted 8 lines
arch/arm/include/asm/pgtable-3level.h +8
-0+/* +* 2nd stage PTE definitions for LPAE. +*/ +#define L_PTE_S2_MT_UNCACHED (_AT(pteval_t, 0x5) << 2)/* MemAttr[3:0] */ +#define L_PTE_S2_MT_WRITETHROUGH (_AT(pteval_t, 0xa) << 2)/* MemAttr[3:0] */ +#define L_PTE_S2_MT_WRITEBACK (_AT(pteval_t, 0xf) << 2)/* MemAttr[3:0] */ +#define L_PTE_S2_RDONLY (_AT(pteval_t, 1) << 6)/* HAP[1] */ +#define L_PTE_S2_RDWR (_AT(pteval_t, 2) << 6)/* HAP[2:1] */ Why is this called
L_PTE_S2_RDWR? The ARM ARM shows HAP[2:1] as 0b11 for read/write and 0b10 for WRONLY.Quoted 3 lines
it should be
+#define
L_PTE_S2_RDWR(_AT(pteval_t, 3)<< 6) /* HAP[2:1] */OK.
Quoted 24 lines
Quoted 23 lines
Quoted 14 lines
arch/arm/include/asm/pgtable.h +3
-0@@ -70,6 +70,9 @@ 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_hyp_device; +extern pgprot_t pgprot_s2; +extern pgprot_t pgprot_s2_device;#define _MOD_PROT (p, b) __pgprot(pgprot_val(p) | (b))arch/arm/include/asm/pgtable.h extern pgprot_t pgprot_kernel; · line 91
Quoted 7 lines
arch/arm/include/asm/pgtable.h +4
-0@@ -82,6 +85,10 @@ 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_HYP) +#define PAGE_HYP_DEVICE _MOD_PROT(pgprot_hyp_device, L_PTE_HYP) +#define PAGE_S2 _MOD_PROT(pgprot_s2, L_PTE_S2_RDONLY) +#define PAGE_S2_DEVICE _MOD_PROT(pgprot_s2_device, L_PTE_USER | L_PTE_S2_RDONLY)Are these S2 read-only by default?
Quoted 5 lines
Quoted 1 line
not sure I understand what you're asking here?
Why do you pass
L_PTE_S2_RDONLY? Do you need to restrict guest writes via the stage 2 attributes?
yes, for example when KSM merges two pages, then it must be mapped read-only to the guest so that we catch writes and perform COW.
Re: [PATCH v6 01/15] ARM: Add page table and page defines needed by KVM
Quoted 17 lines · Christoffer Dall
Quoted 15 lines · Catalin Marinas
Quoted 14 lines · Christoffer Dall
Quoted 11 lines · Catalin Marinas
arch/arm/include/asm/pgtable.h extern pgprot_t pgprot_kernel; · line 91
Quoted 8 lines · Christoffer Dall
arch/arm/include/asm/pgtable.h +4
-0@@ -82,6 +85,10 @@ 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_HYP) +#define PAGE_HYP_DEVICE _MOD_PROT(pgprot_hyp_device, L_PTE_HYP) +#define PAGE_S2 _MOD_PROT(pgprot_s2, L_PTE_S2_RDONLY) +#define PAGE_S2_DEVICE _MOD_PROT(pgprot_s2_device, L_PTE_USER | L_PTE_S2_RDONLY)Are these S2 read-only by default?
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
not sure I understand what you're asking here?
Why do you pass
L_PTE_S2_RDONLY? Do you need to restrict guest writes via the stage 2 attributes?yes, for example when KSM merges two pages, then it must be mapped read-only to the guest so that we catch writes and perform COW.
OK, so you want the default to be read-only unless write access is required and you perform an 'or' with L_PTE_S2_RDWR. Sounds fine.
Signature · 1 line
Catalin
Re: [PATCH v6 01/15] ARM: Add page table and page defines needed by KVM
Quoted 20 lines · Catalin Marinas
Quoted 19 lines · Christoffer Dall
Quoted 17 lines · Catalin Marinas
Quoted 15 lines · Christoffer Dall
Quoted 11 lines · Catalin Marinas
arch/arm/include/asm/pgtable.h extern pgprot_t pgprot_kernel; · line 91
Quoted 8 lines · Christoffer Dall
arch/arm/include/asm/pgtable.h +4
-0@@ -82,6 +85,10 @@ 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_HYP) +#define PAGE_HYP_DEVICE _MOD_PROT(pgprot_hyp_device, L_PTE_HYP) +#define PAGE_S2 _MOD_PROT(pgprot_s2, L_PTE_S2_RDONLY) +#define PAGE_S2_DEVICE _MOD_PROT(pgprot_s2_device, L_PTE_USER | L_PTE_S2_RDONLY)Are these S2 read-only by default?
not sure I understand what you're asking here?
Quoted 9 lines
Quoted 5 lines
Quoted 3 lines
Why do you pass
L_PTE_S2_RDONLY? Do you need to restrict guest writes via the stage 2 attributes?yes, for example when KSM merges two pages, then it must be mapped read-only to the guest so that we catch writes and perform COW.
OK, so you want the default to be read-only unless write access is required and you perform an 'or' with
L_PTE_S2_RDWR. Sounds fine.
exactly, the specific pointer to the code is in line ~562 in arch/arm/kvm/mmu.c.
-Christoffer
PATCH v6 2/15 ARM: Section based HYP idmap
In reply to Christoffer DallAdd a method (hyp_idmap_setup) to populate a hyp pgd with an identity mapping of the code contained in the .hyp.idmap.text section.
Offer a method to drop this identity mapping through hyp_idmap_teardown.
Make all the above depend on CONFIG_ARM_VIRT_EXT and CONFIG_ARM_LPAE.
--- arch/arm/include/asm/idmap.h | 1 arch/arm/include/asm/pgtable-3level-hwdef.h | 1 arch/arm/kernel/vmlinux.lds.S | 6 ++- arch/arm/mm/idmap.c | 55 ++++++++++++++++++++++----- 4 files changed, 51 insertions(+), 12 deletions(-)
arch/arm/include/asm/idmap.h +1 -0
@@ -8,6 +8,7 @@#define __idmap __section(.idmap.text) noinline notraceextern pgd_t *idmap_pgd; +extern pgd_t *hyp_pgd;void setup_mm_for_reboot (void );
arch/arm/include/asm/pgtable-3level-hwdef.h +1 -0
@@ -44,6 +44,7 @@#define PMD_SECT_XN (_AT(pmdval_t, 1) << 54)#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/kernel/vmlinux.lds.S +5 -1
@@ -19,7 +19,11 @@ ALIGN_FUNCTION(); \ VMLINUX_SYMBOL(__idmap_text_start) = .; \ *(.idmap.text) \ - VMLINUX_SYMBOL(__idmap_text_end) = .; + VMLINUX_SYMBOL(__idmap_text_end) = .; \ + ALIGN_FUNCTION(); \ + VMLINUX_SYMBOL(__hyp_idmap_text_start) = .; \ + *(.hyp.idmap.text) \ + VMLINUX_SYMBOL(__hyp_idmap_text_end) = .; #ifdef CONFIG_HOTPLUG_CPU #define ARM_CPU_DISCARD(x)
arch/arm/mm/idmap.c +44 -11
93 lines not on this page · raw message
Re: [PATCH v6 02/15] ARM: Section based HYP idmap
arch/arm/mm/idmap.c line 1
Quoted 4 lines · Christoffer Dall
arch/arm/mm/idmap.c +1
-0@@ -1,4 +1,6 @@ +#include <linux/module.h>
Minor thing - do you need to include linux/module.h here?
arch/arm/mm/idmap.c static void identity_mapping_add(pgd_t *pgd, unsigned long addr, unsigned long e · line 100
Quoted 2 lines
arch/arm/mm/idmap.c +2
-0+identity_mapping_add (hyp_pgd, __hyp_idmap_text_start, + __hyp_idmap_text_end,PMD_SECT_AP1 );
It would be more consistent if you define PMD_SECT_HYP or something like that. I think you have a L_PTE_HYP bit as well.
Signature · 1 line
Catalin
Re: [PATCH v6 02/15] ARM: Section based HYP idmap
Quoted 8 lines · Catalin Marinas
arch/arm/mm/idmap.c line 1
Quoted 4 lines · Christoffer Dall
arch/arm/mm/idmap.c +1
-0@@ -1,4 +1,6 @@ +#include <linux/module.h> Minor thing - do you need to include
linux/module.hhere?
there was once a time when kvm/arm could be compiled as a module; my best guess is that it's a leftover from that.
Quoted 6 lines
arch/arm/mm/idmap.c line 3
Quoted 2 lines
arch/arm/mm/idmap.c +2
-0+identity_mapping_add (hyp_pgd, __hyp_idmap_text_start, + __hyp_idmap_text_end,PMD_SECT_AP1 );It would be more consistent if you define
PMD_SECT_HYPor something like that. I think you have aL_PTE_HYPbit as well.
Is this along the lines of what you had in mind?
(quoted diff) +0 -0
b/arch/arm/include/asm/pgtable-3level-hwdef.h
index 18f5cef..432fa12 100644
arch/arm/include/asm/pgtable-3level-hwdef.h +1 -0
@@ -50,6 +50,7 @@#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)) +#define PMD_SECT_HYP PMD_SECT_AP1/* * AttrIndx[2:0] encoding (mapping attributes defined in the MAIR* registers).
(quoted diff) +0 -0
b/arch/arm/include/asm/pgtable-3level.h
index 6ef8afd..a7e6532 100644
arch/arm/include/asm/pgtable-3level.h +1 -0
@@ -116,6 +116,7 @@/* * Hyp-mode PL2 PTE definitions for LPAE. */ +#define PMD_HYP PMD_SECT_AP1#define L_PTE_HYP L_PTE_USER#ifndef __ASSEMBLY__
arch/arm/mm/idmap.c +1 -2
@@ -1,4 +1,3 @@ -#include <linux/module.h> #include <linux/kernel.h> #include <linux/slab.h>
@@ -97,7 +96,7 @@ static int __init init_static_idmap_hyp(void)pr_info ("Setting up static HYP identity map for 0x%p - 0x%p\n" , __hyp_idmap_text_start, __hyp_idmap_text_end);identity_mapping_add (hyp_pgd, __hyp_idmap_text_start, - __hyp_idmap_text_end,PMD_SECT_AP1 ); + __hyp_idmap_text_end,PMD_SECT_HYP );return 0 ; }
Re: [PATCH v6 02/15] ARM: Section based HYP idmap
Quoted 41 lines · Christoffer Dall
Quoted 8 lines · Catalin Marinas
arch/arm/mm/idmap.c line 1
Quoted 4 lines · Christoffer Dall
arch/arm/mm/idmap.c +1
-0@@ -1,4 +1,6 @@ +#include <linux/module.h> Minor thing - do you need to include
linux/module.hhere?there was once a time when kvm/arm could be compiled as a module; my best guess is that it's a leftover from that.
Quoted 6 lines
arch/arm/mm/idmap.c line 3
Quoted 2 lines
arch/arm/mm/idmap.c +2
-0+identity_mapping_add (hyp_pgd, __hyp_idmap_text_start, + __hyp_idmap_text_end,PMD_SECT_AP1 );It would be more consistent if you define
PMD_SECT_HYPor something like that. I think you have aL_PTE_HYPbit as well.Is this along the lines of what you had in mind?
arch/arm/include/asm/pgtable-3level-hwdef.h +1
-0@@ -50,6 +50,7 @@#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)) +#define PMD_SECT_HYP PMD_SECT_AP1/* * AttrIndx[2:0] encoding (mapping attributes defined in the MAIR* registers). arch/arm/include/asm/pgtable-3level.h +0
-0
arch/arm/include/asm/pgtable-3level.h line 119
Quoted 4 lines
arch/arm/include/asm/pgtable-3level.h +1
-0@@ -116,6 +116,7 @@/* * Hyp-mode PL2 PTE definitions for LPAE. */ +#define PMD_HYP PMD_SECT_AP1
Yes, but only one definition in is probably enough (PMD_SECT_HYP?).
Signature · 1 line
Catalin
Re: [PATCH v6 02/15] ARM: Section based HYP idmap
Quoted 34 lines · Catalin Marinas
Quoted 33 lines · Christoffer Dall
Quoted 8 lines · Catalin Marinas
arch/arm/mm/idmap.c line 1
Quoted 4 lines · Christoffer Dall
arch/arm/mm/idmap.c +1
-0@@ -1,4 +1,6 @@ +#include <linux/module.h> Minor thing - do you need to include
linux/module.hhere?there was once a time when kvm/arm could be compiled as a module; my best guess is that it's a leftover from that.
Quoted 6 lines
arch/arm/mm/idmap.c line 3
Quoted 2 lines
arch/arm/mm/idmap.c +2
-0+identity_mapping_add (hyp_pgd, __hyp_idmap_text_start, + __hyp_idmap_text_end,PMD_SECT_AP1 );It would be more consistent if you define
PMD_SECT_HYPor something like that. I think you have aL_PTE_HYPbit as well.Is this along the lines of what you had in mind?
arch/arm/include/asm/pgtable-3level-hwdef.h +1
-0@@ -50,6 +50,7 @@#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)) +#define PMD_SECT_HYP PMD_SECT_AP1
Quoted 15 lines
arch/arm/include/asm/pgtable-3level.h line 119
Quoted 12 lines
arch/arm/include/asm/pgtable-3level-hwdef.h +0
-0/* * AttrIndx[2:0] encoding (mapping attributes defined in the MAIR* registers). arch/arm/include/asm/pgtable-3level.h +1
-0@@ -116,6 +116,7 @@/* * Hyp-mode PL2 PTE definitions for LPAE. */ +#define PMD_HYP PMD_SECT_AP1Yes, but only one definition in is probably enough (
PMD_SECT_HYP?).
yeah, that one was not supposed to be included in my git add command, whoops:
(quoted diff) +0 -0
b/arch/arm/include/asm/pgtable-3level-hwdef.h
index 18f5cef..432fa12 100644
arch/arm/include/asm/pgtable-3level-hwdef.h +1 -0
@@ -50,6 +50,7 @@#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)) +#define PMD_SECT_HYP PMD_SECT_AP1/* * AttrIndx[2:0] encoding (mapping attributes defined in the MAIR* registers).
arch/arm/mm/idmap.c +1 -2
@@ -1,4 +1,3 @@ -#include <linux/module.h> #include <linux/kernel.h> #include <linux/slab.h>
@@ -97,7 +96,7 @@ static int __init init_static_idmap_hyp(void)pr_info ("Setting up static HYP identity map for 0x%p - 0x%p\n" , __hyp_idmap_text_start, __hyp_idmap_text_end);identity_mapping_add (hyp_pgd, __hyp_idmap_text_start, - __hyp_idmap_text_end,PMD_SECT_AP1 ); + __hyp_idmap_text_end,PMD_SECT_HYP );return 0 ; }
--
Thanks, I'll queue these and send them after the other patch series has been merged.
-Christoffer
PATCH v6 3/15 KVM: ARM: Initial skeleton to compile KVM support
In reply to Christoffer DallTargets KVM support for Cortex A-15 processors.
Contains all the framework components, make files, header files, some tracing functionality, and basic user space API.
Only supported core is Cortex-A15 for now.
Most functionality is in arch/arm/kvm/* or arch/arm/include/asm/kvm_*.h.
--- Documentation/virtual/kvm/api.txt | 57 +++++- arch/arm/Kconfig | 2 arch/arm/Makefile | 1 arch/arm/include/asm/kvm_arm.h | 24 ++ arch/arm/include/asm/kvm_asm.h | 58 ++++++ arch/arm/include/asm/kvm_coproc.h | 24 ++ arch/arm/include/asm/kvm_emulate.h | 50 +++++ arch/arm/include/asm/kvm_host.h | 114 ++++++++++++ arch/arm/include/uapi/asm/kvm.h | 106 +++++++++++ arch/arm/kvm/Kconfig | 55 ++++++ arch/arm/kvm/Makefile | 21 ++ arch/arm/kvm/arm.c | 350 ++++++++++++++++++++++++++++++++++++ arch/arm/kvm/coproc.c | 23 ++ arch/arm/kvm/emulate.c | 155 ++++++++++++++++ arch/arm/kvm/guest.c | 221 +++++++++++++++++++++++ arch/arm/kvm/init.S | 19 ++ arch/arm/kvm/interrupts.S | 19 ++ arch/arm/kvm/mmu.c | 17 ++ arch/arm/kvm/reset.c | 74 ++++++++ arch/arm/kvm/trace.h | 52 +++++ include/uapi/linux/kvm.h | 7 + 21 files changed, 1445 insertions(+), 4 deletions(-) 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_coproc.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/uapi/asm/kvm.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/coproc.c create mode 100644 arch/arm/kvm/emulate.c create mode 100644 arch/arm/kvm/guest.c create mode 100644 arch/arm/kvm/init.S create mode 100644 arch/arm/kvm/interrupts.S create mode 100644 arch/arm/kvm/mmu.c create mode 100644 arch/arm/kvm/reset.c create mode 100644 arch/arm/kvm/trace.h
Documentation/virtual/kvm/api.txt +53 -4
@@ -293,7 +293,7 @@ kvm_run' (see below). 4.11 KVM_GET_REGS Capability: basic -Architectures: all +Architectures: all except ARM Type: vcpu ioctl Parameters: struct kvm_regs (out) Returns: 0 on success, -1 on error
@@ -314,7 +314,7 @@ struct kvm_regs { 4.12 KVM_SET_REGS Capability: basic -Architectures: all +Architectures: all except ARM Type: vcpu ioctl Parameters: struct kvm_regs (in) Returns: 0 on success, -1 on error
@@ -600,7 +600,7 @@ struct kvm_fpu { 4.24 KVM_CREATE_IRQCHIP Capability: KVM_CAP_IRQCHIP -Architectures: x86, ia64 +Architectures: x86, ia64, ARM Type: vm ioctl Parameters: none Returns: 0 on success, -1 on error
@@ -608,7 +608,8 @@ Returns: 0 on success, -1 on error Creates an interrupt controller model in the kernel. On x86, creates a virtual ioapic, a virtual PIC (two PICs, nested), and sets up future vcpus to have a local APIC. IRQ routing for GSIs 0-15 is set to both PIC and IOAPIC; GSI 16-23 -only go to the IOAPIC. On ia64, a IOSAPIC is created. +only go to the IOAPIC. On ia64, a IOSAPIC is created. On ARM, a GIC is +created. 4.25 KVM_IRQ_LINE
@@ -1775,6 +1776,14 @@ registers, find a list below: PPC | KVM_REG_PPC_VPA_DTL | 128 PPC | KVM_REG_PPC_EPCR | 32 +ARM registers are mapped using the lower 32 bits. The upper 16 of that +is the register group type, or coprocessor number: + +ARM core registers have the following id bit patterns: + 0x4002 0000 0010 <index into the kvm_regs struct:16> + + + 4.69 KVM_GET_ONE_REG Capability: KVM_CAP_ONE_REG
@@ -2127,6 +2136,46 @@ written, then `n_invalid' invalid entries, invalidating any previously valid entries found. +4.77 KVM_ARM_VCPU_INIT + +Capability: basic +Architectures: arm +Type: vcpu ioctl +Parameters: struct struct kvm_vcpu_init (in) +Returns: 0 on success; -1 on error +Errors: + ?EINVAL: ???the target is unknown, or the combination of features is invalid. + ?ENOENT: ???a features bit specified is unknown. + +This tells KVM what type of CPU to present to the guest, and what +optional features it should have. ?This will cause a reset of the cpu +registers to their initial values. ?If this is not called, KVM_RUN will +return ENOEXEC for that vcpu. + +Note that because some registers reflect machine topology, all vcpus +should be created before this ioctl is invoked. + + +4.78 KVM_GET_REG_LIST + +Capability: basic +Architectures: arm +Type: vcpu ioctl +Parameters: struct kvm_reg_list (in/out) +Returns: 0 on success; -1 on error +Errors: + ?E2BIG: ????the reg index list is too big to fit in the array specified by + ????????????the user (the number required will be written into n). + +struct kvm_reg_list { + __u64 n; /* number of registers in reg[] */ + __u64 reg[0]; +}; + +This ioctl returns the guest registers that are supported for the +KVM_GET_ONE_REG/KVM_SET_ONE_REG calls. + + 5. The kvm_run structure ------------------------
arch/arm/Kconfig +2 -0
@@ -2322,3 +2322,5 @@ source "security/Kconfig" source "crypto/Kconfig" source "lib/Kconfig" + +source "arch/arm/kvm/Kconfig"
arch/arm/Makefile +1 -0
@@ -252,6 +252,7 @@ core-$(CONFIG_FPE_NWFPE) += arch/arm/nwfpe/ core-$(CONFIG_FPE_FASTFPE) += $(FASTFPE_OBJ) core-$(CONFIG_VFP) += arch/arm/vfp/ core-$(CONFIG_XEN) += arch/arm/xen/ +core-$(CONFIG_KVM_ARM_HOST) += arch/arm/kvm/ # If we have a machine-specific directory, then include it in the build. core-y += arch/arm/kernel/ arch/arm/mm/ arch/arm/common/
arch/arm/include/asm/kvm_arm.h new file +24 -0
@@ -0,0 +1,24 @@ +/* +* Copyright (C) 2012 - Virtual Open Systems and Columbia University +* Author: Christoffer Dall <[email protected]> +* +* 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_ARM_H__ +#define __ARM_KVM_ARM_H__ + +#include <linux/types.h> + +#endif /* __ARM_KVM_ARM_H__ */
arch/arm/include/asm/kvm_asm.h new file +58 -0
58 lines not on this page · raw message
arch/arm/include/asm/kvm_coproc.h new file +24 -0
@@ -0,0 +1,24 @@ +/* +* Copyright (C) 2012 Rusty Russell IBM Corporation +* +* 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_COPROC_H__ +#define __ARM_KVM_COPROC_H__ +#include <linux/kvm_host.h> + +void kvm_reset_coprocs (struct kvm_vcpu *vcpu); + +#endif /* __ARM_KVM_COPROC_H__ */
arch/arm/include/asm/kvm_emulate.h new file +50 -0
50 lines not on this page · raw message
arch/arm/include/asm/kvm_host.h new file +114 -0
114 lines not on this page · raw message
arch/arm/include/uapi/asm/kvm.h new file +106 -0
106 lines not on this page · raw message
arch/arm/kvm/Kconfig new file +55 -0
55 lines not on this page · raw message
arch/arm/kvm/Makefile new file +21 -0
@@ -0,0 +1,21 @@ +# +# Makefile for Kernel-based Virtual Machine module +# + +plus_virt := $(call as-instr,.arch_extension virt,+virt) +ifeq ($(plus_virt),+virt) + plus_virt_def := -DREQUIRES_VIRT=1 +endif + +ccflags-y += -Ivirt/kvm -Iarch/arm/kvm +CFLAGS_arm.o := -I. $(plus_virt_def) +CFLAGS_mmu.o := -I. + +AFLAGS_init.o := -Wa,-march=armv7-a$(plus_virt) +AFLAGS_interrupts.o := -Wa,-march=armv7-a$(plus_virt) + +kvm-arm-y = $(addprefix ../../../virt/kvm/, kvm_main.o coalesced_mmio.o) + +obj-y += kvm-arm.o init.o interrupts.o +obj-y += arm.o guest.o mmu.o emulate.o reset.o +obj-y += coproc.o
arch/arm/kvm/arm.c new file +350 -0
350 lines not on this page · raw message
arch/arm/kvm/coproc.c new file +23 -0
@@ -0,0 +1,23 @@ +/* +* Copyright (C) 2012 - Virtual Open Systems and Columbia University +* Authors: Rusty Russell <[email protected]> +* Christoffer Dall <[email protected]> +* +* 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/kvm_host.h> + +void kvm_reset_coprocs (struct kvm_vcpu *vcpu) +{ +}
arch/arm/kvm/emulate.c new file +155 -0
155 lines not on this page · raw message
arch/arm/kvm/guest.c new file +221 -0
221 lines not on this page · raw message
arch/arm/kvm/init.S new file +19 -0
@@ -0,0 +1,19 @@ +/* + * Copyright (C) 2012 - Virtual Open Systems and Columbia University + * Author: Christoffer Dall <[email protected]> + * + * 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/interrupts.S new file +19 -0
@@ -0,0 +1,19 @@ +/* + * Copyright (C) 2012 - Virtual Open Systems and Columbia University + * Author: Christoffer Dall <[email protected]> + * + * 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/mmu.c new file +17 -0
@@ -0,0 +1,17 @@ +/* +* Copyright (C) 2012 - Virtual Open Systems and Columbia University +* Author: Christoffer Dall <[email protected]> +* +* 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/reset.c new file +74 -0
74 lines not on this page · raw message
arch/arm/kvm/trace.h new file +52 -0
52 lines not on this page · raw message
include/uapi/linux/kvm.h +7 -0
@@ -764,6 +764,11 @@ struct kvm_dirty_tlb {#define KVM_REG_SIZE_U512 0x0060000000000000ULL#define KVM_REG_SIZE_U1024 0x0070000000000000ULL +struct kvm_reg_list { +__u64 n;/* number of regs */ +__u64 reg[0 ]; +}; +struct kvm_one_reg {__u64 id;__u64 addr;
@@ -932,6 +937,8 @@ struct kvm_s390_ucas_mapping {#define KVM_SET_ONE_REG _IOW(KVMIO, 0xac, struct kvm_one_reg)/* VM is being stopped by host */ #define KVM_KVMCLOCK_CTRL _IO(KVMIO, 0xad) +#define KVM_ARM_VCPU_INIT _IOW(KVMIO, 0xae, struct kvm_vcpu_init) +#define KVM_GET_REG_LIST _IOWR(KVMIO, 0xb0, struct kvm_reg_list)#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)
PATCH v6 4/15 KVM: ARM: Hypervisor initialization
In reply to Christoffer DallSets up KVM code to handle all exceptions taken to Hyp mode.
When the kernel is booted in Hyp mode, calling an hvc instruction with r0 pointing to the new vectors, the HVBAR is changed to the the vector pointers. This allows subsystems (like KVM here) to execute code in Hyp-mode with the MMU disabled.
We initialize other Hyp-mode registers and enables the MMU for Hyp-mode from the id-mapped hyp initialization code. 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, data structures, and I/O regions 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 comprises:
- create_hyp_mappings(from, to);
- create_hyp_io_mappings(from, to, phys_addr);
- free_hyp_pmds();
--- arch/arm/include/asm/kvm_arm.h | 124 ++++++++++++++ arch/arm/include/asm/kvm_asm.h | 20 ++ arch/arm/include/asm/kvm_host.h | 1 arch/arm/include/asm/kvm_mmu.h | 29 +++ arch/arm/include/asm/pgtable-3level-hwdef.h | 4 arch/arm/kvm/arm.c | 172 +++++++++++++++++++ arch/arm/kvm/init.S | 95 ++++++++++ arch/arm/kvm/interrupts.S | 40 ++++ arch/arm/kvm/mmu.c | 248 +++++++++++++++++++++++++++ 9 files changed, 733 insertions(+) create mode 100644 arch/arm/include/asm/kvm_mmu.h
arch/arm/include/asm/kvm_arm.h +124 -0
@@ -21,4 +21,128 @@#include <linux/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_BSU_IS (1 << 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 + +/* +* The bits we set in HCR: +* TAC: Trap ACTLR +* TSC: Trap SMC +* TSW: Trap cache operations by set/way +* TWI: Trap WFI +* TIDCP: Trap L2CTLR/L2ECTLR +* BSU_IS: Upgrade barriers to the inner shareable domain +* FB: Force broadcast of all maintainance operations +* AMO: Override CPSR.A and enable signaling with VA +* IMO: Override CPSR.I and enable signaling with VI +* FMO: Override CPSR.F and enable signaling with VF +* SWIO: Turn set/way invalidates into set/way clean+invalidate +*/ +#define HCR_GUEST_MASK (HCR_TSC | HCR_TSW | HCR_TWI | HCR_VM | HCR_BSU_IS | \ + HCR_FB | HCR_TAC | HCR_AMO | HCR_IMO | HCR_FMO | \ + HCR_SWIO | HCR_TIDCP) + +/* 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) + +/* Hyp Debug Configuration Register bits */ +#define HDCR_TDRA (1 << 11) +#define HDCR_TDOSA (1 << 10) +#define HDCR_TDA (1 << 9) +#define HDCR_TDE (1 << 8) +#define HDCR_HPME (1 << 7) +#define HDCR_TPM (1 << 6) +#define HDCR_TPMCR (1 << 5) +#define HDCR_HPMN_MASK (0x1F) + +/* +* The architecture supports 40-bit IPA as input to the 2nd stage translations +* and PTRS_PER_S2_PGD becomes 1024, because each entry covers 1GB of address +* space. +*/ +#define KVM_PHYS_SHIFT (40) +#define KVM_PHYS_SIZE (1ULL << KVM_PHYS_SHIFT) +#define KVM_PHYS_MASK (KVM_PHYS_SIZE - 1ULL) +#define PTRS_PER_S2_PGD (1ULL << (KVM_PHYS_SHIFT - 30)) +#define S2_PGD_ORDER get_order(PTRS_PER_S2_PGD * sizeof(pgd_t)) +#define S2_PGD_SIZE (1 << S2_PGD_ORDER) + +/* 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 (0xf) +#define VTCR_MASK (VTCR_SH0 | VTCR_ORGN0 | VTCR_IRGN0 | VTCR_SL0 | \ + VTCR_S | VTCR_T0SZ) +#define VTCR_HTCR_SH (VTCR_SH0 | VTCR_ORGN0 | VTCR_IRGN0) +#define VTCR_SL_L2 (0 << 6)/* Starting-level: 2 */ +#define VTCR_SL_L1 (1 << 6)/* Starting-level: 1 */ +#define KVM_VTCR_SL0 VTCR_SL_L1 +/* stage-2 input address range defined as 2^(32-T0SZ) */ +#define KVM_T0SZ (32 - KVM_PHYS_SHIFT) +#define KVM_VTCR_T0SZ (KVM_T0SZ & VTCR_T0SZ) +#define KVM_VTCR_S ((KVM_VTCR_T0SZ << 1) & VTCR_S) + +/* Virtualization Translation Table Base Register (VTTBR) bits */ +#if KVM_VTCR_SL0 ==VTCR_SL_L2 /* see ARM DDI 0406C: B4-1720 */ +#define VTTBR_X (14 - KVM_T0SZ) +#else +#define VTTBR_X (5 - KVM_T0SZ) +#endif + +#endif /* __ARM_KVM_ARM_H__ */
arch/arm/include/asm/kvm_asm.h +20 -0
@@ -54,5 +54,25 @@#define ARM_EXCEPTION_DATA_ABORT 4#define ARM_EXCEPTION_IRQ 5#define ARM_EXCEPTION_FIQ 6 +#define ARM_EXCEPTION_HVC 7 + +#ifndef __ASSEMBLY__ +struct kvm_vcpu ; + +extern char __kvm_hyp_init[]; +extern char __kvm_hyp_init_end[]; + +extern char __kvm_hyp_exit[]; +extern char __kvm_hyp_exit_end[]; + +extern char __kvm_hyp_vector[]; + +extern char __kvm_hyp_code_start[]; +extern char __kvm_hyp_code_end[]; + +extern void __kvm_flush_vm_context (void ); + +extern int __kvm_vcpu_run (struct kvm_vcpu *vcpu); +#endif #endif /* __ARM_KVM_ASM_H__ */
arch/arm/include/asm/kvm_host.h +1 -0
@@ -111,4 +111,5 @@ int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);struct kvm_one_reg ;int kvm_arm_get_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg);int kvm_arm_set_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg); +u64 kvm_call_hyp (void *hypfn, ...);#endif /* __ARM_KVM_HOST_H__ */
arch/arm/include/asm/kvm_mmu.h new file +29 -0
@@ -0,0 +1,29 @@ +/* +* Copyright (C) 2012 - Virtual Open Systems and Columbia University +* Author: Christoffer Dall <[email protected]> +* +* 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__ + +int create_hyp_mappings (void *from,void *to); +int create_hyp_io_mappings (void *from,void *to,phys_addr_t ); +void free_hyp_pmds (void ); + +phys_addr_t kvm_mmu_get_httbr (void ); +int kvm_mmu_init (void ); +void kvm_clear_hyp_idmap (void ); +#endif /* __ARM_KVM_MMU_H__ */
arch/arm/include/asm/pgtable-3level-hwdef.h +4 -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
@@ -41,6 +44,7 @@#define PMD_SECT_S (_AT(pmdval_t, 3) << 8)#define PMD_SECT_AF (_AT(pmdval_t, 1) << 10)#define PMD_SECT_nG (_AT(pmdval_t, 1) << 11) +#define PMD_SECT_PXN (_AT(pmdval_t, 1) << 53)#define PMD_SECT_XN (_AT(pmdval_t, 1) << 54)#define PMD_SECT_AP_WRITE (_AT(pmdval_t, 0))#define PMD_SECT_AP_READ (_AT(pmdval_t, 0))
arch/arm/kvm/arm.c +172 -0
192 lines not on this page · raw message
arch/arm/kvm/init.S +95 -0
100 lines not on this page · raw message
arch/arm/kvm/interrupts.S +40 -0
@@ -15,5 +15,45 @@ * along with this program; if not, write to the Free Software * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include <linux/linkage.h> +#include <linux/const.h> +#include <asm/unified.h> +#include <asm/page.h> #include <asm/asm-offsets.h> #include <asm/kvm_asm.h> +#include <asm/kvm_arm.h> + + .text + +__kvm_hyp_code_start: + .globl __kvm_hyp_code_start + +/******************************************************************** + * Flush per-VMID TLBs + */ +ENTRY(__kvm_flush_vm_context) + bx lr +ENDPROC(__kvm_flush_vm_context) + +/******************************************************************** + * Hypervisor world-switch code + */ +ENTRY(__kvm_vcpu_run) + bx lr + +ENTRY(kvm_call_hyp) + hvc #0 + bx lr + +/******************************************************************** + * Hypervisor exception vector and handlers + */ + + .align 5 +__kvm_hyp_vector: + .globl __kvm_hyp_vector + nop + +__kvm_hyp_code_end: + .globl __kvm_hyp_code_end
arch/arm/kvm/mmu.c +248 -0
251 lines not on this page · raw message
Re: [PATCH v6 04/15] KVM: ARM: Hypervisor initialization
arch/arm/include/asm/pgtable-3level-hwdef.h line 37
Quoted 9 lines · Christoffer Dall
arch/arm/include/asm/pgtable-3level-hwdef.h +3
-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)
Are these used anywhere? I grep'ed but couldn't find. While I'm fine with keeping some hardware definitions for future use, I think here you need to qualify these bits with some suffix to PMD_APTABLE. The APTable are just a group of bits with different meaning (no write access etc.). If you want to keep them as group of bits, you can add a _MASK suffix.
arch/arm/include/asm/pgtable-3level-hwdef.h
Quoted 12 lines
arch/arm/include/asm/pgtable-3level-hwdef.h +12
-0+static int init_hyp_mode (void ) +{ +phys_addr_t init_phys_addr; +int cpu; +int err =0 ; + +/* +* Allocate Hyp PGD and setup Hyp identity mapping +*/ + err =kvm_mmu_init (); +if (err) + goto out_err;
The comment is a bit misleading since kvm_mmu_init() simply checks whether hyp_pgd has been initialised. The actual initialisation is done by the idmap code.
Signature · 1 line
Catalin
Re: [PATCH v6 04/15] KVM: ARM: Hypervisor initialization
Quoted 17 lines · Catalin Marinas
arch/arm/include/asm/pgtable-3level-hwdef.h line 37
Quoted 9 lines · Christoffer Dall
arch/arm/include/asm/pgtable-3level-hwdef.h +3
-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)Are these used anywhere? I grep'ed but couldn't find. While I'm fine with keeping some hardware definitions for future use, I think here you need to qualify these bits with some suffix to
PMD_APTABLE. The APTable are just a group of bits with different meaning (no write access etc.). If you want to keep them as group of bits, you can add a_MASKsuffix.
Quoted 17 lines
arch/arm/include/asm/pgtable-3level-hwdef.h
Quoted 12 lines
arch/arm/include/asm/pgtable-3level-hwdef.h +12
-0+static int init_hyp_mode (void ) +{ +phys_addr_t init_phys_addr; +int cpu; +int err =0 ; + +/* +* Allocate Hyp PGD and setup Hyp identity mapping +*/ + err =kvm_mmu_init (); +if (err) + goto out_err;The comment is a bit misleading since
kvm_mmu_init()simply checks whetherhyp_pgdhas been initialised. The actual initialisation is done by the idmap code.
This should fix it, thanks:
(quoted diff) +0 -0
b/arch/arm/include/asm/pgtable-3level-hwdef.h
index 432fa12..30fb12a 100644
arch/arm/include/asm/pgtable-3level-hwdef.h +1 -1
@@ -33,7 +33,7 @@#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_APTABLE_MASK (_AT(pgdval_t, 3) << PGD_APTABLE_SHIFT)#define PMD_PXNTABLE (_AT(pgdval_t, 1) << 59)/*
arch/arm/kvm/arm.c +1 -1
@@ -871,7 +871,7 @@ static int init_hyp_mode(void)int err =0 ;/* -* Allocate Hyp PGD and setup Hyp identity mapping +* Make sure the Hyp identity map is ready for us. */ err =kvm_mmu_init ();if (err)
--
PATCH v6 5/15 KVM: ARM: 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 table (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);
Each entry in TLBs and caches are tagged with a VMID identifier in addition to ASIDs. The VMIDs are assigned consecutively to VMs in the order that VMs are executed, and caches and tlbs are invalidated when the VMID space has been used to allow for more than 255 simultaenously running guests.
The 2nd stage pgd is allocated in kvm_arch_init_vm(). The table is freed in kvm_arch_destroy_vm(). Both functions are called from the main KVM code.
We pre-allocate page table memory to be able to synchronize using a spinlock and be called under rcu_read_lock from the MMU notifiers. We steal the mmu_memory_cache implementation from x86 and adapt for our specific usage.
We support MMU notifiers (thanks to Marc Zyngier) through kvm_unmap_hva and kvm_set_spte_hva.
Finally, define kvm_phys_addr_ioremap() to map a device at a guest IPA, which is used by VGIC support to map the virtual CPU interface registers to the guest. This support is added by Marc Zyngier.
--- arch/arm/include/asm/kvm_asm.h | 2 arch/arm/include/asm/kvm_host.h | 18 ++ arch/arm/include/asm/kvm_mmu.h | 9 + arch/arm/kvm/Kconfig | 1 arch/arm/kvm/arm.c | 37 ++++ arch/arm/kvm/interrupts.S | 7 + arch/arm/kvm/mmu.c | 370 +++++++++++++++++++++++++++++++++++++++ arch/arm/kvm/trace.h | 46 +++++ 8 files changed, 488 insertions(+), 2 deletions(-)
arch/arm/include/asm/kvm_asm.h +2 -0
@@ -57,6 +57,7 @@#define ARM_EXCEPTION_HVC 7#ifndef __ASSEMBLY__ +struct kvm ;struct kvm_vcpu ;extern char __kvm_hyp_init[];
@@ -71,6 +72,7 @@ extern char __kvm_hyp_code_start[];extern char __kvm_hyp_code_end[];extern void __kvm_flush_vm_context (void ); +extern void __kvm_tlb_flush_vmid (struct kvm *kvm);extern int __kvm_vcpu_run (struct kvm_vcpu *vcpu);#endif
arch/arm/include/asm/kvm_host.h +18 -0
@@ -112,4 +112,22 @@ struct kvm_one_reg;int kvm_arm_get_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg);int kvm_arm_set_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg);u64 kvm_call_hyp (void *hypfn, ...); + +#define KVM_ARCH_WANT_MMU_NOTIFIER +struct kvm ; +int kvm_unmap_hva (struct kvm *kvm,unsigned long hva); +int kvm_unmap_hva_range (struct kvm *kvm, +unsigned long start,unsigned long end); +void kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte); + +/* We do not have shadow page tables, hence the empty hooks */ +static inline int kvm_age_hva (struct kvm *kvm,unsigned long hva) +{ +return 0 ; +} + +static inline int kvm_test_age_hva (struct kvm *kvm,unsigned long hva) +{ +return 0 ; +}#endif /* __ARM_KVM_HOST_H__ */
arch/arm/include/asm/kvm_mmu.h +9 -0
@@ -23,6 +23,15 @@ int create_hyp_mappings(void *from, void *to);int create_hyp_io_mappings (void *from,void *to,phys_addr_t );void free_hyp_pmds (void ); +int kvm_alloc_stage2_pgd (struct kvm *kvm); +void kvm_free_stage2_pgd (struct kvm *kvm); +int kvm_phys_addr_ioremap (struct kvm *kvm,phys_addr_t guest_ipa, +phys_addr_t pa,unsigned long size); + +int kvm_handle_guest_abort (struct kvm_vcpu *vcpu,struct kvm_run *run); + +void kvm_mmu_free_memory_caches (struct kvm_vcpu *vcpu); +phys_addr_t kvm_mmu_get_httbr (void );int kvm_mmu_init (void );void kvm_clear_hyp_idmap (void );
arch/arm/kvm/Kconfig +1 -0
@@ -36,6 +36,7 @@ config KVM_ARM_HOST bool "KVM host support for ARM cpus." depends on KVM depends on MMU + select MMU_NOTIFIER ---help--- Provides host support for ARM processors.
arch/arm/kvm/arm.c +36 -1
@@ -81,12 +81,33 @@ void kvm_arch_sync_events(struct kvm *kvm) { } +/** +* kvm_arch_init_vm - initializes a VM data structure +* @kvm: pointer to the KVM struct +*/ int kvm_arch_init_vm (struct kvm *kvm,unsigned long type) { +int ret =0 ; +if (type)return -EINVAL ; -return 0 ; + ret =kvm_alloc_stage2_pgd (kvm); +if (ret) + goto out_fail_alloc; + + ret =create_hyp_mappings (kvm, kvm +1 ); +if (ret) + goto out_free_stage2_pgd; + +/* Mark the initial VMID generation invalid */ + kvm->arch.vmid_gen =0 ; + +return ret; +out_free_stage2_pgd: +kvm_free_stage2_pgd (kvm); +out_fail_alloc: +return ret; }int kvm_arch_vcpu_fault (struct kvm_vcpu *vcpu,struct vm_fault *vmf)
@@ -104,10 +125,16 @@ int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)return 0 ; } +/** +* kvm_arch_destroy_vm - destroy the VM data structure +* @kvm: pointer to the KVM struct +*/ void kvm_arch_destroy_vm (struct kvm *kvm) {int i; +kvm_free_stage2_pgd (kvm); +for (i =0 ; i <KVM_MAX_VCPUS ; ++i) {if (kvm->vcpus[i]) {kvm_arch_vcpu_free (kvm->vcpus[i]);
@@ -196,7 +223,13 @@ struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)if (err) goto free_vcpu; + err =create_hyp_mappings (vcpu, vcpu +1 ); +if (err) + goto vcpu_uninit; +return vcpu; +vcpu_uninit: +kvm_vcpu_uninit (vcpu); free_vcpu:kmem_cache_free (kvm_vcpu_cache, vcpu); out:
@@ -210,6 +243,8 @@ int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)void kvm_arch_vcpu_free (struct kvm_vcpu *vcpu) { +kvm_mmu_free_memory_caches (vcpu); +kmem_cache_free (kvm_vcpu_cache, vcpu); }void kvm_arch_vcpu_destroy (struct kvm_vcpu *vcpu)
arch/arm/kvm/interrupts.S +7 -0
@@ -32,6 +32,13 @@ __kvm_hyp_code_start: /******************************************************************** * Flush per-VMID TLBs */ +ENTRY(__kvm_tlb_flush_vmid) + bx lr +ENDPROC(__kvm_tlb_flush_vmid) + +/******************************************************************** + * Flush TLBs and instruction caches of current CPU for all VMIDs + */ ENTRY(__kvm_flush_vm_context) bx lr ENDPROC(__kvm_flush_vm_context)
arch/arm/kvm/mmu.c +369 -1
405 lines not on this page · raw message
arch/arm/kvm/trace.h +46 -0
53 lines not on this page · raw message
PATCH v6 6/15 KVM: ARM: Inject IRQs and FIQs from userspace
In reply to Christoffer DallFrom: Christoffer Dall <[email protected]>
All interrupt injection is now based on the VM ioctl KVM_IRQ_LINE. This works semantically well for the GIC as we in fact raise/lower a line on a machine component (the gic). The IOCTL uses the follwing struct.
struct kvm_irq_level {
union {
__u32 irq; /* GSI */
__s32 status; /* not used for KVM_IRQ_LEVEL */
};
__u32 level; /* 0 or 1 */
};ARM can signal an interrupt either at the CPU level, or at the in-kernel irqchip (GIC), and for in-kernel irqchip can tell the GIC to use PPIs designated for specific cpus. The irq field is interpreted like this:
?bits: | 31 ... 24 | 23 ... 16 | 15 ... 0 | field: | irq_type | vcpu_index | irq_number |
The irq_type field has the following values:
- irq_type[0]: out-of-kernel GIC: irq_number 0 is IRQ, irq_number 1 is FIQ
- irq_type[1]: in-kernel GIC: SPI, irq_number between 32 and 1019 (incl.) (the vcpu_index field is ignored)
- irq_type[2]: in-kernel GIC: PPI, irq_number between 16 and 31 (incl.)
The irq_number thus corresponds to the irq ID in as in the GICv2 specs.
This is documented in Documentation/kvm/api.txt.
--- Documentation/virtual/kvm/api.txt | 25 ++++++++++++-- arch/arm/include/asm/kvm_arm.h | 1 + arch/arm/include/uapi/asm/kvm.h | 21 ++++++++++++ arch/arm/kvm/arm.c | 65 +++++++++++++++++++++++++++++++++++++ arch/arm/kvm/trace.h | 25 ++++++++++++++ include/uapi/linux/kvm.h | 1 + 6 files changed, 134 insertions(+), 4 deletions(-)
Documentation/virtual/kvm/api.txt +21 -4
@@ -615,15 +615,32 @@ 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. +On some architectures it is required 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. + +ARM can signal an interrupt either at the CPU level, or at the in-kernel irqchip +(GIC), and for in-kernel irqchip can tell the GIC to use PPIs designated for +specific cpus. The irq field is interpreted like this: + + ?bits: | 31 ... 24 | 23 ... 16 | 15 ... 0 | + field: | irq_type | vcpu_index | irq_id | + +The irq_type field has the following values: +- irq_type[0]: out-of-kernel GIC: irq_id 0 is IRQ, irq_id 1 is FIQ +- irq_type[1]: in-kernel GIC: SPI, irq_id between 32 and 1019 (incl.) + (the vcpu_index field is ignored) +- irq_type[2]: in-kernel GIC: PPI, irq_id between 16 and 31 (incl.) + +(The irq_id field thus corresponds nicely to the IRQ ID in the ARM GIC specs) + +In both cases, level is used to raise/lower the line. struct kvm_irq_level { union {
arch/arm/include/asm/kvm_arm.h +1 -0
@@ -68,6 +68,7 @@#define HCR_GUEST_MASK (HCR_TSC | HCR_TSW | HCR_TWI | HCR_VM | HCR_BSU_IS | \ HCR_FB | HCR_TAC | HCR_AMO | HCR_IMO | HCR_FMO | \ HCR_SWIO | HCR_TIDCP) +#define HCR_VIRT_EXCP_MASK (HCR_VA | HCR_VI | HCR_VF)/* Hyp System Control Register (HSCTLR) bits */ #define HSCTLR_TE (1 << 30)
arch/arm/include/uapi/asm/kvm.h +21 -0
@@ -23,6 +23,7 @@#include <asm/ptrace.h> #define __KVM_HAVE_GUEST_DEBUG +#define __KVM_HAVE_IRQ_LINE#define KVM_REG_SIZE (id) \ (1U << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
@@ -103,4 +104,24 @@ struct kvm_arch_memory_slot {#define KVM_REG_ARM_CORE (0x0010 << KVM_REG_ARM_COPROC_SHIFT)#define KVM_REG_ARM_CORE_REG (name) (offsetof(struct kvm_regs, name) / 4) +/* KVM_IRQ_LINE irq field index values */ +#define KVM_ARM_IRQ_TYPE_SHIFT 24 +#define KVM_ARM_IRQ_TYPE_MASK 0xff +#define KVM_ARM_IRQ_VCPU_SHIFT 16 +#define KVM_ARM_IRQ_VCPU_MASK 0xff +#define KVM_ARM_IRQ_NUM_SHIFT 0 +#define KVM_ARM_IRQ_NUM_MASK 0xffff + +/* irq_type field */ +#define KVM_ARM_IRQ_TYPE_CPU 0 +#define KVM_ARM_IRQ_TYPE_SPI 1 +#define KVM_ARM_IRQ_TYPE_PPI 2 + +/* out-of-kernel GIC cpu interrupt injection irq_number field */ +#define KVM_ARM_IRQ_CPU_IRQ 0 +#define KVM_ARM_IRQ_CPU_FIQ 1 + +/* Highest supported SPI, from VGIC_NR_IRQS */ +#define KVM_ARM_IRQ_GIC_MAX 127 +#endif /* __ARM_KVM_H__ */
arch/arm/kvm/arm.c +65 -0
83 lines not on this page · raw message
arch/arm/kvm/trace.h +25 -0
@@ -39,6 +39,31 @@ TRACE_EVENT(kvm_exit,TP_printk ("PC: 0x%08lx" , __entry->vcpu_pc) ); +TRACE_EVENT (kvm_irq_line, + TP_PROTO(unsigned int type, int vcpu_idx, int irq_num, int level), +TP_ARGS (type, vcpu_idx, irq_num, level), + +TP_STRUCT__entry ( + __field( unsigned int, type ) + __field( int, vcpu_idx ) + __field( int, irq_num ) +__field ( int, level ) + ), + +TP_fast_assign ( + __entry->type = type; + __entry->vcpu_idx = vcpu_idx; + __entry->irq_num = irq_num; + __entry->level = level; + ), + +TP_printk ("Inject %s interrupt (%d), vcpu->idx: %d, num: %d, level: %d" , + (__entry->type ==KVM_ARM_IRQ_TYPE_CPU ) ?"CPU" : + (__entry->type ==KVM_ARM_IRQ_TYPE_PPI ) ?"VGIC PPI" : + (__entry->type ==KVM_ARM_IRQ_TYPE_SPI ) ?"VGIC SPI" :"UNKNOWN" , + __entry->type, __entry->vcpu_idx, __entry->irq_num, __entry->level) +); + TRACE_EVENT(kvm_unmap_hva, TP_PROTO(unsigned long hva), TP_ARGS(hva),
include/uapi/linux/kvm.h +1 -0
@@ -115,6 +115,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: See Documentation/virtual/kvm/api.txt */ union {__u32 irq;
PATCH v6 7/15 KVM: ARM: World-switch implementation
In reply to Christoffer DallProvides complete world-switch implementation to switch to other guests running in non-secure modes. Includes Hyp exception handlers that capture necessary exception information and stores the information on the VCPU and KVM structures.
The following Hyp-ABI is also documented in the code:
Hyp-ABI: Calling HYP-mode functions from host (in SVC mode):
Switching to Hyp mode is done through a simple HVC #0 instruction. The exception vector code will check that the HVC comes from VMID==0 and if so will push the necessary state (SPSR, lr_usr) on the Hyp stack.
- r0 contains a pointer to a HYP function
- r1, r2, and r3 contain arguments to the above function.
- The HYP function will be called with its arguments in r0, r1 and r2. On HYP function return, we return directly to SVC.
A call to a function executing in Hyp mode is performed like the following:
<svc code>
ldr r0, =BSYM(my_hyp_fn)
ldr r1, =my_param
hvc #0 ; Call my_hyp_fn(my_param) from HYP mode <svc code>
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.
SMP support using the VMPIDR calculated on the basis of the host MPIDR and overriding the low bits with KVM vcpu_id contributed by Marc Zyngier.
Reuse of VMIDs has been implemented by Antonios Motakis and adapated from a separate patch into the appropriate patches introducing the functionality. Note that the VMIDs are stored per VM as required by the ARM architecture reference manual.
To support VFP/NEON we trap those instructions using the HPCTR. When we trap, we switch the FPU. After a guest exit, the VFP state is returned to the host. When disabling access to floating point instructions, we also mask FPEXC_EN in order to avoid the guest receiving Undefined instruction exceptions before we have a chance to switch back the floating point state. We are reusing vfp_hard_struct, so we depend on VFPv3 being enabled in the host kernel, if not, we still trap cp10 and cp11 in order to inject an undefined instruction exception whenever the guest tries to use VFP/NEON. VFP/NEON developed by Antionios Motakis and Rusty Russell.
Aborts that are permission faults, and not stage-1 page table walk, do not report the faulting address in the HPFAR. We have to resolve the IPA, and store it just like the HPFAR register on the VCPU struct. If the IPA cannot be resolved, it means another CPU is playing with the page tables, and we simply restart the guest. This quirk was fixed by Marc Zyngier.
--- arch/arm/include/asm/kvm_arm.h | 51 +++++ arch/arm/include/asm/kvm_host.h | 13 + arch/arm/kernel/asm-offsets.c | 25 ++ arch/arm/kvm/arm.c | 200 ++++++++++++++++++ arch/arm/kvm/interrupts.S | 395 +++++++++++++++++++++++++++++++++++ arch/arm/kvm/interrupts_head.S | 441 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 1121 insertions(+), 4 deletions(-) create mode 100644 arch/arm/kvm/interrupts_head.S
arch/arm/include/asm/kvm_arm.h +51 -0
@@ -98,6 +98,18 @@#define TTBCR_T0SZ 3#define HTCR_MASK (TTBCR_T0SZ | TTBCR_IRGN0 | TTBCR_ORGN0 | TTBCR_SH0) +/* Hyp System Trap Register */ +#define HSTR_T (x) (1 << x) +#define HSTR_TTEE (1 << 16) +#define HSTR_TJDBX (1 << 17) + +/* Hyp Coprocessor Trap Register */ +#define HCPTR_TCP (x) (1 << x) +#define HCPTR_TCP_MASK (0x3fff) +#define HCPTR_TASE (1 << 15) +#define HCPTR_TTA (1 << 20) +#define HCPTR_TCPAC (1 << 31) +/* Hyp Debug Configuration Register bits */ #define HDCR_TDRA (1 << 11)#define HDCR_TDOSA (1 << 10)
@@ -144,6 +156,45 @@#else #define VTTBR_X (5 - KVM_T0SZ)#endif +#define VTTBR_BADDR_SHIFT (VTTBR_X - 1) +#define VTTBR_BADDR_MASK (((1LLU << (40 - VTTBR_X)) - 1) << VTTBR_BADDR_SHIFT) +#define VTTBR_VMID_SHIFT (48LLU) +#define VTTBR_VMID_MASK (0xffLLU << VTTBR_VMID_SHIFT) + +/* 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_FSC (0x3f) +#define HSR_FSC_TYPE (0x3c) +#define HSR_WNR (1 << 6) + +#define FSC_FAULT (0x04) +#define FSC_PERM (0x0c) + +/* Hyp Prefetch Fault Address Register (HPFAR/HDFAR) */ +#define HPFAR_MASK (~0xf) +#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 /* __ARM_KVM_ARM_H__ */
arch/arm/include/asm/kvm_host.h +13 -0
@@ -21,6 +21,7 @@#include <asm/kvm.h> #include <asm/kvm_asm.h> +#include <asm/fpstate.h> #define KVM_MAX_VCPUS CONFIG_KVM_ARM_MAX_VCPUS#define KVM_MEMORY_SLOTS 32
@@ -85,6 +86,14 @@ struct kvm_vcpu_arch {u32 hxfar;/* Hyp Data/Inst Fault Address Register */ u32 hpfar;/* Hyp IPA Fault Address Register */ +/* Floating point registers (VFP and Advanced SIMD/NEON) */ +struct vfp_hard_struct vfp_guest; +struct vfp_hard_struct *vfp_host; + +/* +* Anything that is not used directly from assembly code goes +* here. +*/ /* Interrupt related fields */ u32 irq_lines;/* IRQ and FIQ levels */
@@ -93,6 +102,9 @@ struct kvm_vcpu_arch {/* Cache some mmu pages needed inside spinlock regions */ struct kvm_mmu_memory_cache mmu_page_cache; + +/* Detect first run of a vcpu */ +bool has_run_once; };struct kvm_vm_stat {
@@ -112,6 +124,7 @@ struct kvm_one_reg;int kvm_arm_get_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg);int kvm_arm_set_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg);u64 kvm_call_hyp (void *hypfn, ...); +void force_vm_exit (const cpumask_t *mask);#define KVM_ARCH_WANT_MMU_NOTIFIER struct kvm ;
arch/arm/kernel/asm-offsets.c +25 -0
@@ -13,6 +13,9 @@#include <linux/sched.h> #include <linux/mm.h> #include <linux/dma-mapping.h> +#ifdef CONFIG_KVM_ARM_HOST +#include <linux/kvm_host.h> +#endif #include <asm/cacheflush.h> #include <asm/glue-df.h> #include <asm/glue-pf.h>
@@ -146,5 +149,27 @@ 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.midr)); + DEFINE(VCPU_CP15, offsetof(struct kvm_vcpu, arch.cp15)); + DEFINE(VCPU_VFP_GUEST, offsetof(struct kvm_vcpu, arch.vfp_guest)); + DEFINE(VCPU_VFP_HOST, offsetof(struct kvm_vcpu, arch.vfp_host)); + 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.usr_regs.ARM_pc)); + DEFINE(VCPU_CPSR, offsetof(struct kvm_vcpu, arch.regs.usr_regs.ARM_cpsr)); + DEFINE(VCPU_IRQ_LINES, offsetof(struct kvm_vcpu, arch.irq_lines)); + DEFINE(VCPU_HSR, offsetof(struct kvm_vcpu, arch.hsr)); + DEFINE(VCPU_HxFAR, offsetof(struct kvm_vcpu, arch.hxfar)); + DEFINE(VCPU_HPFAR, offsetof(struct kvm_vcpu, arch.hpfar)); + DEFINE(VCPU_HYP_PC, offsetof(struct kvm_vcpu, arch.hyp_pc)); + DEFINE(KVM_VTTBR, offsetof(struct kvm, arch.vttbr)); +#endif return 0 ; }
arch/arm/kvm/arm.c +199 -1
232 lines not on this page · raw message
arch/arm/kvm/interrupts.S +392 -3
436 lines not on this page · raw message
arch/arm/kvm/interrupts_head.S new file +441 -0
441 lines not on this page · raw message
PATCH v6 8/15 KVM: ARM: 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 commit handles these exits by emulating the intended operation in software and skipping the guest instruction.
Minor notes about the coproc register reset:
1) We reserve a value of 0 as an invalid cp15 offset, to catch bugs in our table, at cost of 4 bytes per vcpu.
2) Added comments on the table indicating how we handle each register, for simplicity of understanding.
--- arch/arm/include/asm/kvm_arm.h | 11 + arch/arm/include/asm/kvm_coproc.h | 14 + arch/arm/include/asm/kvm_emulate.h | 6 + arch/arm/include/asm/kvm_host.h | 4 arch/arm/kvm/Makefile | 2 arch/arm/kvm/arm.c | 168 +++++++++++++++++ arch/arm/kvm/coproc.c | 360 ++++++++++++++++++++++++++++++++++++ arch/arm/kvm/coproc.h | 153 +++++++++++++++ arch/arm/kvm/coproc_a15.c | 162 ++++++++++++++++ arch/arm/kvm/emulate.c | 218 ++++++++++++++++++++++ arch/arm/kvm/trace.h | 65 +++++++ 11 files changed, 1159 insertions(+), 4 deletions(-) create mode 100644 arch/arm/kvm/coproc.h create mode 100644 arch/arm/kvm/coproc_a15.c
arch/arm/include/asm/kvm_arm.h +11 -0
@@ -70,6 +70,11 @@ HCR_SWIO | HCR_TIDCP)#define HCR_VIRT_EXCP_MASK (HCR_VA | HCR_VI | HCR_VF) +/* System Control Register (SCTLR) bits */ +#define SCTLR_TE (1 << 30) +#define SCTLR_EE (1 << 25) +#define SCTLR_V (1 << 13) +/* Hyp System Control Register (HSCTLR) bits */ #define HSCTLR_TE (1 << 30)#define HSCTLR_EE (1 << 25)
@@ -171,6 +176,10 @@#define HSR_FSC (0x3f)#define HSR_FSC_TYPE (0x3c)#define HSR_WNR (1 << 6) +#define HSR_CV_SHIFT (24) +#define HSR_CV (1U << HSR_CV_SHIFT) +#define HSR_COND_SHIFT (20) +#define HSR_COND (0xfU << HSR_COND_SHIFT)#define FSC_FAULT (0x04)#define FSC_PERM (0x0c)
@@ -197,4 +206,6 @@#define HSR_EC_DABT (0x24)#define HSR_EC_DABT_HYP (0x25) +#define HSR_HVC_IMM_MASK ((1UL << 16) - 1) +#endif /* __ARM_KVM_ARM_H__ */
arch/arm/include/asm/kvm_coproc.h +14 -0
@@ -21,4 +21,18 @@void kvm_reset_coprocs (struct kvm_vcpu *vcpu); +struct kvm_coproc_target_table { +unsigned target; +const struct coproc_reg *table; +size_t num; +}; +void kvm_register_target_coproc_table (struct kvm_coproc_target_table *table); + +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_32 (struct kvm_vcpu *vcpu,struct kvm_run *run); +int kvm_handle_cp15_64 (struct kvm_vcpu *vcpu,struct kvm_run *run); +void kvm_coproc_table_init (void );#endif /* __ARM_KVM_COPROC_H__ */
arch/arm/include/asm/kvm_emulate.h +6 -0
@@ -25,6 +25,12 @@u32 *vcpu_reg (struct kvm_vcpu *vcpu,u8 reg_num);u32 *vcpu_spsr (struct kvm_vcpu *vcpu); +int kvm_handle_wfi (struct kvm_vcpu *vcpu,struct kvm_run *run); +void kvm_skip_instr (struct kvm_vcpu *vcpu,bool is_wide_instr); +void kvm_inject_undefined (struct kvm_vcpu *vcpu); +void kvm_inject_dabt (struct kvm_vcpu *vcpu,unsigned long addr); +void kvm_inject_pabt (struct kvm_vcpu *vcpu,unsigned long addr); +static inline u32 *vcpu_pc (struct kvm_vcpu *vcpu) {return (u32 *)&vcpu->arch.regs.usr_regs.ARM_pc;
arch/arm/include/asm/kvm_host.h +4 -0
@@ -94,6 +94,10 @@ struct kvm_vcpu_arch {* Anything that is not used directly from assembly code goes * here. */ +/* dcache set/way operation pending */ +int last_pcpu; +cpumask_t require_dcache_flush; +/* Interrupt related fields */ u32 irq_lines;/* IRQ and FIQ levels */
arch/arm/kvm/Makefile +1 -1
@@ -18,4 +18,4 @@ kvm-arm-y = $(addprefix ../../../virt/kvm/, kvm_main.o coalesced_mmio.o) obj-y += kvm-arm.o init.o interrupts.o obj-y += arm.o guest.o mmu.o emulate.o reset.o -obj-y += coproc.o +obj-y += coproc.o coproc_a15.o
arch/arm/kvm/arm.c +165 -3
217 lines not on this page · raw message
arch/arm/kvm/coproc.c +360 -0
368 lines not on this page · raw message
arch/arm/kvm/coproc.h new file +153 -0
153 lines not on this page · raw message
arch/arm/kvm/coproc_a15.c new file +162 -0
162 lines not on this page · raw message
arch/arm/kvm/emulate.c +218 -0
228 lines not on this page · raw message
arch/arm/kvm/trace.h +65 -0
77 lines not on this page · raw message
PATCH v6 9/15 trom: Christoffer Dall <[email protected]>
In reply to Christoffer DallKVM: ARM: User space API for getting/setting co-proc registers
The following three ioctls are implemented:
- KVM_GET_REG_LIST - KVM_GET_ONE_REG - KVM_SET_ONE_REG
Now we have a table for all the cp15 registers, we can drive a generic API.
The register IDs carry the following encoding:
ARM registers are mapped using the lower 32 bits. The upper 16 of that is the register group type, or coprocessor number:
ARM 32-bit CP15 registers have the following id bit patterns:
0x4002 0000 000F <zero:1> <crn:4> <crm:4> <opc1:4> <opc2:3>
ARM 64-bit CP15 registers have the following id bit patterns:
0x4003 0000 000F <zero:1> <zero:4> <crm:4> <opc1:4> <zero:3>
For futureproofing, we need to tell QEMU about the CP15 registers the host lets the guest access.
It will need this information to restore a current guest on a future CPU or perhaps a future KVM which allow some of these to be changed.
We use a separate table for these, as they're only for the userspace API.
--- Documentation/virtual/kvm/api.txt | 5 + arch/arm/include/asm/kvm_coproc.h | 9 + arch/arm/include/asm/kvm_host.h | 4 arch/arm/kvm/coproc.c | 327 +++++++++++++++++++++++++++++++++++++ arch/arm/kvm/guest.c | 9 + 5 files changed, 350 insertions(+), 4 deletions(-)
Documentation/virtual/kvm/api.txt +5 -0
@@ -1799,6 +1799,11 @@ is the register group type, or coprocessor number: ARM core registers have the following id bit patterns: 0x4002 0000 0010 <index into the kvm_regs struct:16> +ARM 32-bit CP15 registers have the following id bit patterns: + 0x4002 0000 000F <zero:1> <crn:4> <crm:4> <opc1:4> <opc2:3> + +ARM 64-bit CP15 registers have the following id bit patterns: + 0x4003 0000 000F <zero:1> <zero:4> <crm:4> <opc1:4> <zero:3> 4.69 KVM_GET_ONE_REG
arch/arm/include/asm/kvm_coproc.h +9 -0
@@ -34,5 +34,14 @@ 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_32 (struct kvm_vcpu *vcpu,struct kvm_run *run);int kvm_handle_cp15_64 (struct kvm_vcpu *vcpu,struct kvm_run *run); + +unsigned long kvm_arm_num_guest_msrs (struct kvm_vcpu *vcpu); +int kvm_arm_copy_msrindices (struct kvm_vcpu *vcpu,u64 __user *uindices);void kvm_coproc_table_init (void ); + +struct kvm_one_reg ; +int kvm_arm_copy_coproc_indices (struct kvm_vcpu *vcpu,u64 __user *uindices); +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 *); +unsigned long kvm_arm_num_coproc_regs (struct kvm_vcpu *vcpu);#endif /* __ARM_KVM_COPROC_H__ */
arch/arm/include/asm/kvm_host.h +4 -0
@@ -27,6 +27,7 @@#define KVM_MEMORY_SLOTS 32#define KVM_PRIVATE_MEM_SLOTS 4#define KVM_COALESCED_MMIO_PAGE_OFFSET 1 +#define KVM_HAVE_ONE_REG #define KVM_VCPU_MAX_FEATURES 0
@@ -137,6 +138,9 @@ int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end);void kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte); +unsigned long kvm_arm_num_regs (struct kvm_vcpu *vcpu); +int kvm_arm_copy_reg_indices (struct kvm_vcpu *vcpu,u64 __user *indices); +/* We do not have shadow page tables, hence the empty hooks */ static inline int kvm_age_hva (struct kvm *kvm,unsigned long hva) {
arch/arm/kvm/coproc.c +327 -0
345 lines not on this page · raw message
arch/arm/kvm/guest.c +5 -4
@@ -26,6 +26,7 @@#include <asm/kvm.h> #include <asm/kvm_asm.h> #include <asm/kvm_emulate.h> +#include <asm/kvm_coproc.h> #define VM_STAT (x) { #x, offsetof(struct kvm, stat.x), KVM_STAT_VM }#define VCPU_STAT (x) { #x, offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU }
@@ -119,7 +120,7 @@ static unsigned long num_core_regs(void)*/ unsigned long kvm_arm_num_regs (struct kvm_vcpu *vcpu) { -return num_core_regs (); +return num_core_regs () +kvm_arm_num_coproc_regs (vcpu); }/**
@@ -138,7 +139,7 @@ int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) uindices++; } -return 0 ; +return kvm_arm_copy_coproc_indices (vcpu, uindices); }int kvm_arm_get_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg)
@@ -151,7 +152,7 @@ int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)if ((reg->id &KVM_REG_ARM_COPROC_MASK ) ==KVM_REG_ARM_CORE )return get_core_reg (vcpu, reg); -return -EINVAL ; +return kvm_arm_coproc_get_reg (vcpu, reg); }int kvm_arm_set_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg)
@@ -164,7 +165,7 @@ int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)if ((reg->id &KVM_REG_ARM_COPROC_MASK ) ==KVM_REG_ARM_CORE )return set_core_reg (vcpu, reg); -return -EINVAL ; +return kvm_arm_coproc_set_reg (vcpu, reg); } int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
PATCH RESEND v6 9/15 KVM: ARM: User space API for getting/setting co-proc registers
The following three ioctls are implemented:
- KVM_GET_REG_LIST - KVM_GET_ONE_REG - KVM_SET_ONE_REG
Now we have a table for all the cp15 registers, we can drive a generic API.
The register IDs carry the following encoding:
ARM registers are mapped using the lower 32 bits. The upper 16 of that is the register group type, or coprocessor number:
ARM 32-bit CP15 registers have the following id bit patterns:
0x4002 0000 000F <zero:1> <crn:4> <crm:4> <opc1:4> <opc2:3>
ARM 64-bit CP15 registers have the following id bit patterns:
0x4003 0000 000F <zero:1> <zero:4> <crm:4> <opc1:4> <zero:3>
For futureproofing, we need to tell QEMU about the CP15 registers the host lets the guest access.
It will need this information to restore a current guest on a future CPU or perhaps a future KVM which allow some of these to be changed.
We use a separate table for these, as they're only for the userspace API.
--- Documentation/virtual/kvm/api.txt | 5 + arch/arm/include/asm/kvm_coproc.h | 9 + arch/arm/include/asm/kvm_host.h | 4 arch/arm/kvm/coproc.c | 327 +++++++++++++++++++++++++++++++++++++ arch/arm/kvm/guest.c | 9 + 5 files changed, 350 insertions(+), 4 deletions(-)
Documentation/virtual/kvm/api.txt +5 -0
@@ -1799,6 +1799,11 @@ is the register group type, or coprocessor number: ARM core registers have the following id bit patterns: 0x4002 0000 0010 <index into the kvm_regs struct:16> +ARM 32-bit CP15 registers have the following id bit patterns: + 0x4002 0000 000F <zero:1> <crn:4> <crm:4> <opc1:4> <opc2:3> + +ARM 64-bit CP15 registers have the following id bit patterns: + 0x4003 0000 000F <zero:1> <zero:4> <crm:4> <opc1:4> <zero:3> 4.69 KVM_GET_ONE_REG
arch/arm/include/asm/kvm_coproc.h +9 -0
@@ -34,5 +34,14 @@ 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_32 (struct kvm_vcpu *vcpu,struct kvm_run *run);int kvm_handle_cp15_64 (struct kvm_vcpu *vcpu,struct kvm_run *run); + +unsigned long kvm_arm_num_guest_msrs (struct kvm_vcpu *vcpu); +int kvm_arm_copy_msrindices (struct kvm_vcpu *vcpu,u64 __user *uindices);void kvm_coproc_table_init (void ); + +struct kvm_one_reg ; +int kvm_arm_copy_coproc_indices (struct kvm_vcpu *vcpu,u64 __user *uindices); +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 *); +unsigned long kvm_arm_num_coproc_regs (struct kvm_vcpu *vcpu);#endif /* __ARM_KVM_COPROC_H__ */
arch/arm/include/asm/kvm_host.h +4 -0
@@ -27,6 +27,7 @@#define KVM_MEMORY_SLOTS 32#define KVM_PRIVATE_MEM_SLOTS 4#define KVM_COALESCED_MMIO_PAGE_OFFSET 1 +#define KVM_HAVE_ONE_REG #define KVM_VCPU_MAX_FEATURES 0
@@ -137,6 +138,9 @@ int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end);void kvm_set_spte_hva (struct kvm *kvm,unsigned long hva,pte_t pte); +unsigned long kvm_arm_num_regs (struct kvm_vcpu *vcpu); +int kvm_arm_copy_reg_indices (struct kvm_vcpu *vcpu,u64 __user *indices); +/* We do not have shadow page tables, hence the empty hooks */ static inline int kvm_age_hva (struct kvm *kvm,unsigned long hva) {
arch/arm/kvm/coproc.c +327 -0
345 lines not on this page · raw message
arch/arm/kvm/guest.c +5 -4
@@ -26,6 +26,7 @@#include <asm/kvm.h> #include <asm/kvm_asm.h> #include <asm/kvm_emulate.h> +#include <asm/kvm_coproc.h> #define VM_STAT (x) { #x, offsetof(struct kvm, stat.x), KVM_STAT_VM }#define VCPU_STAT (x) { #x, offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU }
@@ -119,7 +120,7 @@ static unsigned long num_core_regs(void)*/ unsigned long kvm_arm_num_regs (struct kvm_vcpu *vcpu) { -return num_core_regs (); +return num_core_regs () +kvm_arm_num_coproc_regs (vcpu); }/**
@@ -138,7 +139,7 @@ int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) uindices++; } -return 0 ; +return kvm_arm_copy_coproc_indices (vcpu, uindices); }int kvm_arm_get_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg)
@@ -151,7 +152,7 @@ int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)if ((reg->id &KVM_REG_ARM_COPROC_MASK ) ==KVM_REG_ARM_CORE )return get_core_reg (vcpu, reg); -return -EINVAL ; +return kvm_arm_coproc_get_reg (vcpu, reg); }int kvm_arm_set_reg (struct kvm_vcpu *vcpu,const struct kvm_one_reg *reg)
@@ -164,7 +165,7 @@ int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)if ((reg->id &KVM_REG_ARM_COPROC_MASK ) ==KVM_REG_ARM_CORE )return set_core_reg (vcpu, reg); -return -EINVAL ; +return kvm_arm_coproc_set_reg (vcpu, reg); } int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
PATCH v6 10/15 KVM: ARM: Demux CCSIDR in the userspace API
In reply to Christoffer DallThe Cache Size Selection Register (CSSELR) selects the current Cache Size ID Register (CCSIDR). You write which cache you are interested in to CSSELR, and read the information out of CCSIDR.
Which cache numbers are valid is known by reading the Cache Level ID Register (CLIDR).
To export this state to userspace, we add a KVM_REG_ARM_DEMUX numberspace (17), which uses 8 bits to represent which register is being demultiplexed (0 for CCSIDR), and the lower 8 bits to represent this demultiplexing (in our case, the CSSELR value, which is 4 bits).
--- Documentation/virtual/kvm/api.txt | 2 arch/arm/include/uapi/asm/kvm.h | 9 ++ arch/arm/kvm/coproc.c | 164 ++++++++++++++++++++++++++++++++++++- 3 files changed, 172 insertions(+), 3 deletions(-)
Documentation/virtual/kvm/api.txt +2 -0
@@ -1805,6 +1805,8 @@ ARM 32-bit CP15 registers have the following id bit patterns: ARM 64-bit CP15 registers have the following id bit patterns: 0x4003 0000 000F <zero:1> <zero:4> <crm:4> <opc1:4> <zero:3> +ARM CCSIDR registers are demultiplexed by CSSELR value: + 0x4002 0000 0011 00 <csselr:8> 4.69 KVM_GET_ONE_REG
arch/arm/include/uapi/asm/kvm.h +9 -0
@@ -104,6 +104,15 @@ struct kvm_arch_memory_slot {#define KVM_REG_ARM_CORE (0x0010 << KVM_REG_ARM_COPROC_SHIFT)#define KVM_REG_ARM_CORE_REG (name) (offsetof(struct kvm_regs, name) / 4) +/* Some registers need more space to represent values. */ +#define KVM_REG_ARM_DEMUX (0x0011 << KVM_REG_ARM_COPROC_SHIFT) +#define KVM_REG_ARM_DEMUX_ID_MASK 0x000000000000FF00 +#define KVM_REG_ARM_DEMUX_ID_SHIFT 8 +#define KVM_REG_ARM_DEMUX_ID_CCSIDR (0x00 << KVM_REG_ARM_DEMUX_ID_SHIFT) +#define KVM_REG_ARM_DEMUX_VAL_MASK 0x00000000000000FF +#define KVM_REG_ARM_DEMUX_VAL_SHIFT 0 + +/* KVM_IRQ_LINE irq field index values */ #define KVM_ARM_IRQ_TYPE_SHIFT 24#define KVM_ARM_IRQ_TYPE_MASK 0xff
arch/arm/kvm/coproc.c +161 -3
211 lines not on this page · raw message
PATCH v6 11/15 KVM: ARM: VFP userspace interface
In reply to Christoffer DallFrom: Rusty Russell <[email protected]>
We use space #18 for floating point regs.
--- Documentation/virtual/kvm/api.txt | 6 + arch/arm/include/uapi/asm/kvm.h | 12 ++ arch/arm/kvm/coproc.c | 178 +++++++++++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+)
Documentation/virtual/kvm/api.txt +6 -0
@@ -1808,6 +1808,12 @@ ARM 64-bit CP15 registers have the following id bit patterns: ARM CCSIDR registers are demultiplexed by CSSELR value: 0x4002 0000 0011 00 <csselr:8> +ARM 32-bit VFP control registers have the following id bit patterns: + 0x4002 0000 0012 1 <regno:12> + +ARM 64-bit FP registers have the following id bit patterns: + 0x4002 0000 0012 0 <regno:12> + 4.69 KVM_GET_ONE_REG Capability: KVM_CAP_ONE_REG
arch/arm/include/uapi/asm/kvm.h +12 -0
@@ -112,6 +112,18 @@ struct kvm_arch_memory_slot {#define KVM_REG_ARM_DEMUX_VAL_MASK 0x00000000000000FF#define KVM_REG_ARM_DEMUX_VAL_SHIFT 0 +/* VFP registers: we could overload CP10 like ARM does, but that's ugly. */ +#define KVM_REG_ARM_VFP (0x0012 << KVM_REG_ARM_COPROC_SHIFT) +#define KVM_REG_ARM_VFP_MASK 0x000000000000FFFF +#define KVM_REG_ARM_VFP_BASE_REG 0x0 +#define KVM_REG_ARM_VFP_FPSID 0x1000 +#define KVM_REG_ARM_VFP_FPSCR 0x1001 +#define KVM_REG_ARM_VFP_MVFR1 0x1006 +#define KVM_REG_ARM_VFP_MVFR0 0x1007 +#define KVM_REG_ARM_VFP_FPEXC 0x1008 +#define KVM_REG_ARM_VFP_FPINST 0x1009 +#define KVM_REG_ARM_VFP_FPINST2 0x100A +/* KVM_IRQ_LINE irq field index values */ #define KVM_ARM_IRQ_TYPE_SHIFT 24
arch/arm/kvm/coproc.c +178 -0
214 lines not on this page · raw message
PATCH v6 12/15 KVM: ARM: 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.
We invalidate the instruction cache by MVA whenever we map a page to the guest (no, we cannot only do it when we have an iabt because the guest may happily read/write a page before hitting the icache) if the hardware uses VIPT or PIPT. In the latter case, we can invalidate only that physical page. In the first case, all bets are off and we simply must invalidate the whole affair. Not that VIVT icaches are tagged with vmids, and we are out of the woods on that one. Alexander Graf was nice enough to remind us of this massive pain.
--- arch/arm/include/asm/kvm_asm.h | 2 + arch/arm/include/asm/kvm_mmu.h | 12 +++ arch/arm/kvm/mmu.c | 153 ++++++++++++++++++++++++++++++++++++++++ arch/arm/kvm/trace.h | 26 +++++++ 4 files changed, 192 insertions(+), 1 deletion(-)
arch/arm/include/asm/kvm_asm.h +2 -0
@@ -71,6 +71,8 @@ extern char __kvm_hyp_vector[];extern char __kvm_hyp_code_start[];extern char __kvm_hyp_code_end[]; +extern void __kvm_tlb_flush_vmid (struct kvm *kvm); +extern void __kvm_flush_vm_context (void );extern void __kvm_tlb_flush_vmid (struct kvm *kvm);
arch/arm/include/asm/kvm_mmu.h +12 -0
@@ -35,4 +35,16 @@ void kvm_mmu_free_memory_caches(struct kvm_vcpu *vcpu);phys_addr_t kvm_mmu_get_httbr (void );int kvm_mmu_init (void );void kvm_clear_hyp_idmap (void ); + +static inline bool kvm_is_write_fault (unsigned long hsr) +{ +unsigned long hsr_ec = hsr >>HSR_EC_SHIFT ; +if (hsr_ec ==HSR_EC_IABT ) +return false; +else if ((hsr &HSR_ISV ) && !(hsr &HSR_WNR )) +return false; +else +return true; +} +#endif /* __ARM_KVM_MMU_H__ */
arch/arm/kvm/mmu.c +152 -1
170 lines not on this page · raw message
arch/arm/kvm/trace.h +26 -0
@@ -39,6 +39,32 @@ TRACE_EVENT(kvm_exit,TP_printk ("PC: 0x%08lx" , __entry->vcpu_pc) ); +TRACE_EVENT (kvm_guest_fault, + TP_PROTO(unsigned long vcpu_pc, unsigned long hsr, + unsigned long hxfar, + unsigned long long ipa), +TP_ARGS (vcpu_pc, hsr, hxfar, ipa), + +TP_STRUCT__entry ( + __field( unsigned long, vcpu_pc ) + __field( unsigned long, hsr ) + __field( unsigned long, hxfar ) + __field( unsigned long long, ipa ) + ), + +TP_fast_assign ( + __entry->vcpu_pc = vcpu_pc; + __entry->hsr = hsr; + __entry->hxfar = hxfar; + __entry->ipa = ipa; + ), + +TP_printk ("guest fault at PC %#08lx (hxfar %#08lx, " +"ipa %#16llx, hsr %#08lx" , + __entry->vcpu_pc, __entry->hxfar, + __entry->ipa, __entry->hsr) +); + TRACE_EVENT(kvm_irq_line, TP_PROTO(unsigned int type, int vcpu_idx, int irq_num, int level), TP_ARGS(type, vcpu_idx, irq_num, level),
PATCH v6 13/15 KVM: ARM: 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. We don't support decoding these (patches are available elsewhere), so we report an error to user space in this case.
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_arm.h | 3 + arch/arm/include/asm/kvm_asm.h | 2 arch/arm/include/asm/kvm_emulate.h | 6 + arch/arm/include/asm/kvm_host.h | 4 + arch/arm/include/asm/kvm_mmio.h | 56 +++++++++++++ arch/arm/kvm/Makefile | 2 arch/arm/kvm/arm.c | 6 + arch/arm/kvm/interrupts.S | 38 +++++++++ arch/arm/kvm/mmio.c | 153 ++++++++++++++++++++++++++++++++++++ arch/arm/kvm/mmu.c | 7 +- arch/arm/kvm/trace.h | 21 +++++ 11 files changed, 295 insertions(+), 3 deletions(-) create mode 100644 arch/arm/include/asm/kvm_mmio.h create mode 100644 arch/arm/kvm/mmio.c
arch/arm/include/asm/kvm_arm.h +3 -0
@@ -173,8 +173,11 @@#define HSR_ISS (HSR_IL - 1)#define HSR_ISV_SHIFT (24)#define HSR_ISV (1U << HSR_ISV_SHIFT) +#define HSR_SRT_SHIFT (16) +#define HSR_SRT_MASK (0xf << HSR_SRT_SHIFT)#define HSR_FSC (0x3f)#define HSR_FSC_TYPE (0x3c) +#define HSR_SSE (1 << 21)#define HSR_WNR (1 << 6)#define HSR_CV_SHIFT (24)#define HSR_CV (1U << HSR_CV_SHIFT)
arch/arm/include/asm/kvm_asm.h +2 -0
@@ -77,6 +77,8 @@ extern void __kvm_flush_vm_context(void);extern void __kvm_tlb_flush_vmid (struct kvm *kvm);extern int __kvm_vcpu_run (struct kvm_vcpu *vcpu); + +extern u64 __kvm_va_to_pa (struct kvm_vcpu *vcpu,u32 va,bool priv);#endif #endif /* __ARM_KVM_ASM_H__ */
arch/arm/include/asm/kvm_emulate.h +6 -0
@@ -21,6 +21,7 @@#include <linux/kvm_host.h> #include <asm/kvm_asm.h> +#include <asm/kvm_mmio.h> u32 *vcpu_reg (struct kvm_vcpu *vcpu,u8 reg_num);u32 *vcpu_spsr (struct kvm_vcpu *vcpu);
@@ -53,4 +54,9 @@ static inline bool vcpu_mode_priv(struct kvm_vcpu *vcpu) return cpsr_mode > USR_MODE;; } +static inline bool kvm_vcpu_reg_is_pc (struct kvm_vcpu *vcpu,int reg) +{ +return reg ==15 ; +} +#endif /* __ARM_KVM_EMULATE_H__ */
arch/arm/include/asm/kvm_host.h +4 -0
@@ -21,6 +21,7 @@#include <asm/kvm.h> #include <asm/kvm_asm.h> +#include <asm/kvm_mmio.h> #include <asm/fpstate.h> #define KVM_MAX_VCPUS CONFIG_KVM_ARM_MAX_VCPUS
@@ -99,6 +100,9 @@ struct kvm_vcpu_arch {int last_pcpu;cpumask_t require_dcache_flush; +/* IO related fields */ +struct kvm_decode mmio_decode; +/* Interrupt related fields */ u32 irq_lines;/* IRQ and FIQ levels */
arch/arm/include/asm/kvm_mmio.h new file +56 -0
56 lines not on this page · raw message
arch/arm/kvm/Makefile +1 -1
@@ -18,4 +18,4 @@ kvm-arm-y = $(addprefix ../../../virt/kvm/, kvm_main.o coalesced_mmio.o) obj-y += kvm-arm.o init.o interrupts.o obj-y += arm.o guest.o mmu.o emulate.o reset.o -obj-y += coproc.o coproc_a15.o +obj-y += coproc.o coproc_a15.o mmio.o
arch/arm/kvm/arm.c +6 -0
@@ -615,6 +615,12 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)if (ret)return ret; +if (run->exit_reason ==KVM_EXIT_MMIO ) { + ret =kvm_handle_mmio_return (vcpu, vcpu->run); +if (ret) +return ret; + } +if (vcpu->sigset_active)sigprocmask (SIG_SETMASK , &vcpu->sigset, &sigsaved);
arch/arm/kvm/interrupts.S +38 -0
@@ -192,6 +192,44 @@ after_vfp_restore: mov r0, r1 @ Return the return code bx lr @ return to IOCTL + +/******************************************************************** + * Translate VA to PA + * + * u64 __kvm_va_to_pa(struct kvm_vcpu *vcpu, u32 va, bool priv) + * + * Arguments: + * r0: pointer to vcpu struct + * r1: virtual address to map (rounded to page) + * r2: 1 = P1 (read) mapping, 0 = P0 (read) mapping. + * Returns 64 bit PAR value. + */ +ENTRY(__kvm_va_to_pa) + push {r4-r12} + + @ Fold flag into r1, easier than using stack. + cmp r2, #0 + movne r2, #1 + orr r1, r1, r2 + + @ This swaps too many registers, but we're in the slow path anyway. + read_cp15_state store_to_vcpu = 0 + write_cp15_state read_from_vcpu = 1 + + ands r2, r1, #1 + bic r1, r1, r2 + mcrne p15, 0, r1, c7, c8, 0 @ VA to PA, ATS1CPR + mcreq p15, 0, r1, c7, c8, 2 @ VA to PA, ATS1CUR + isb + + @ Restore host state. + read_cp15_state store_to_vcpu = 1 + write_cp15_state read_from_vcpu = 0 + + mrrc p15, 0, r0, r1, c7 @ PAR + pop {r4-r12} + bx lr + ENTRY(kvm_call_hyp) hvc #0 bx lr
arch/arm/kvm/mmio.c new file +153 -0
153 lines not on this page · raw message
arch/arm/kvm/mmu.c +5 -2
@@ -19,11 +19,13 @@#include <linux/mman.h> #include <linux/kvm_host.h> #include <linux/io.h> +#include <trace/events/kvm.h> #include <asm/idmap.h> #include <asm/pgalloc.h> #include <asm/cacheflush.h> #include <asm/kvm_arm.h> #include <asm/kvm_mmu.h> +#include <asm/kvm_mmio.h> #include <asm/kvm_asm.h> #include <asm/kvm_emulate.h> #include <asm/mach/map.h>
@@ -624,8 +626,9 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu, struct kvm_run *run) goto out_unlock; } -kvm_pr_unimpl ("I/O address abort..." ); - ret =0 ; +/* Adjust page offset */ + fault_ipa |= vcpu->arch.hxfar & ~PAGE_MASK ; + ret =io_mem_abort (vcpu, run, fault_ipa); goto out_unlock; }
arch/arm/kvm/trace.h +21 -0
@@ -90,6 +90,27 @@ TRACE_EVENT(kvm_irq_line, __entry->type, __entry->vcpu_idx, __entry->irq_num, __entry->level) ); +TRACE_EVENT (kvm_mmio_emulate, + TP_PROTO(unsigned long vcpu_pc, unsigned long instr, + unsigned long cpsr), +TP_ARGS (vcpu_pc, instr, cpsr), + +TP_STRUCT__entry ( + __field( unsigned long, vcpu_pc ) + __field( unsigned long, instr ) + __field( unsigned long, cpsr ) + ), + +TP_fast_assign ( + __entry->vcpu_pc = vcpu_pc; + __entry->instr = instr; + __entry->cpsr = cpsr; + ), + +TP_printk ("Emulate MMIO at: 0x%08lx (instr: %08lx, cpsr: %08lx)" , + __entry->vcpu_pc, __entry->instr, __entry->cpsr) +); +/* Architecturally implementation defined CP15 register access */ TRACE_EVENT(kvm_emulate_cp15_imp, TP_PROTO(unsigned long Op1, unsigned long Rt1, unsigned long CRn,
Re: [PATCH v6 13/15] KVM: ARM: Handle I/O aborts
Quoted 18 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. We don't support decoding these (patches are available elsewhere), so we report an error to user space in this case.
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.
Reviewed-by Will Deacon <[email protected]>Reviewed-by Marcelo Tosatti <[email protected]>Signed-off-by Rusty Russell <[email protected]>Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Christoffer Dall <[email protected]>
[...]
Quoted 36 lines
arch/arm/kvm/interrupts.S +28
-0@@ -192,6 +192,44 @@ after_vfp_restore: mov r0, r1 @ Return the return code bx lr @ return to IOCTL + +/******************************************************************** + * Translate VA to PA + * + * u64 __kvm_va_to_pa(struct kvm_vcpu *vcpu, u32 va, bool priv) + * + * Arguments: + * r0: pointer to vcpu struct + * r1: virtual address to map (rounded to page) + * r2: 1 = P1 (read) mapping, 0 = P0 (read) mapping. + * Returns 64 bit PAR value. + */ +ENTRY(__kvm_va_to_pa) + push {r4-r12} + + @ Fold flag into r1, easier than using stack. + cmp r2, #0 + movne r2, #1 + orr r1, r1, r2 + + @ This swaps too many registers, but we're in the slow path anyway. + read_cp15_state store_to_vcpu = 0 + write_cp15_state read_from_vcpu = 1 + + ands r2, r1, #1 + bic r1, r1, r2 + mcrne p15, 0, r1, c7, c8, 0 @ VA to PA, ATS1CPR + mcreq p15, 0, r1, c7, c8, 2 @ VA to PA, ATS1CUR
arch/arm/kvm/interrupts.S after_vfp_restore: · line 232
Quoted 10 lines
arch/arm/kvm/interrupts.S +10
-0+ isb + + @ Restore host state. + read_cp15_state store_to_vcpu = 1 + write_cp15_state read_from_vcpu = 0 + + mrrc p15, 0, r0, r1, c7 @ PAR + pop {r4-r12} + bx lr +
Do we still need this function? Now that the MMIO emulation is gone, there should be no need to perform a manual translation.
I can't even find a caller for it either.
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v6 13/15] KVM: ARM: Handle I/O aborts
Quoted 72 lines · Marc Zyngier
Quoted 18 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. We don't support decoding these (patches are available elsewhere), so we report an error to user space in this case.
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.
Reviewed-by Will Deacon <[email protected]>Reviewed-by Marcelo Tosatti <[email protected]>Signed-off-by Rusty Russell <[email protected]>Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Christoffer Dall <[email protected]>[...]
Quoted 36 lines
arch/arm/kvm/interrupts.S +28
-0@@ -192,6 +192,44 @@ after_vfp_restore: mov r0, r1 @ Return the return code bx lr @ return to IOCTL + +/******************************************************************** + * Translate VA to PA + * + * u64 __kvm_va_to_pa(struct kvm_vcpu *vcpu, u32 va, bool priv) + * + * Arguments: + * r0: pointer to vcpu struct + * r1: virtual address to map (rounded to page) + * r2: 1 = P1 (read) mapping, 0 = P0 (read) mapping. + * Returns 64 bit PAR value. + */ +ENTRY(__kvm_va_to_pa) + push {r4-r12} + + @ Fold flag into r1, easier than using stack. + cmp r2, #0 + movne r2, #1 + orr r1, r1, r2 + + @ This swaps too many registers, but we're in the slow path anyway. + read_cp15_state store_to_vcpu = 0 + write_cp15_state read_from_vcpu = 1 + + ands r2, r1, #1 + bic r1, r1, r2 + mcrne p15, 0, r1, c7, c8, 0 @ VA to PA, ATS1CPR + mcreq p15, 0, r1, c7, c8, 2 @ VA to PA, ATS1CURarch/arm/kvm/interrupts.S after_vfp_restore: · line 232
Quoted 10 lines
arch/arm/kvm/interrupts.S +10
-0+ isb + + @ Restore host state. + read_cp15_state store_to_vcpu = 1 + write_cp15_state read_from_vcpu = 0 + + mrrc p15, 0, r0, r1, c7 @ PAR + pop {r4-r12} + bx lr +Do we still need this function? Now that the MMIO emulation is gone, there should be no need to perform a manual translation.
Quoted 2 lines
I can't even find a caller for it either.
I'm trying to leave bits and pieces of the emulation code in here in hope that it will spawn itself into a glorious unified solution that decodes everything and that everybody will be happy with :)
I'll remove it. Thanks.
-Christoffer
PATCH v6 14/15 KVM: ARM: Power State Coordination Interface implementation
In reply to Christoffer DallFrom: Marc Zyngier <[email protected]>
Implement the PSCI specification (ARM DEN 0022A) to control virtual CPUs being "powered" on or off.
PSCI/KVM is detected using the KVM_CAP_ARM_PSCI capability.
A virtual CPU can now be initialized in a "powered off" state, using the KVM_ARM_VCPU_POWER_OFF feature flag.
The guest can use either SMC or HVC to execute a PSCI function.
--- Documentation/virtual/kvm/api.txt | 4 + arch/arm/include/asm/kvm_emulate.h | 5 ++ arch/arm/include/asm/kvm_host.h | 5 +- arch/arm/include/asm/kvm_psci.h | 23 ++++++++ arch/arm/include/uapi/asm/kvm.h | 16 +++++ arch/arm/kvm/Makefile | 2 - arch/arm/kvm/arm.c | 28 +++++++++- arch/arm/kvm/psci.c | 105 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 1 9 files changed, 184 insertions(+), 5 deletions(-) create mode 100644 arch/arm/include/asm/kvm_psci.h create mode 100644 arch/arm/kvm/psci.c
Documentation/virtual/kvm/api.txt +4 -0
@@ -2185,6 +2185,10 @@ return ENOEXEC for that vcpu. Note that because some registers reflect machine topology, all vcpus should be created before this ioctl is invoked. +Possible features: + - KVM_ARM_VCPU_POWER_OFF: Starts the CPU in a power-off state. + Depends on KVM_CAP_ARM_PSCI. + 4.78 KVM_GET_REG_LIST
arch/arm/include/asm/kvm_emulate.h +5 -0
@@ -32,6 +32,11 @@ void kvm_inject_undefined(struct kvm_vcpu *vcpu);void kvm_inject_dabt (struct kvm_vcpu *vcpu,unsigned long addr);void kvm_inject_pabt (struct kvm_vcpu *vcpu,unsigned long addr); +static inline bool vcpu_mode_is_32bit (struct kvm_vcpu *vcpu) +{ +return 1 ; +} +static inline u32 *vcpu_pc (struct kvm_vcpu *vcpu) {return (u32 *)&vcpu->arch.regs.usr_regs.ARM_pc;
arch/arm/include/asm/kvm_host.h +4 -1
@@ -30,7 +30,7 @@#define KVM_COALESCED_MMIO_PAGE_OFFSET 1#define KVM_HAVE_ONE_REG -#define KVM_VCPU_MAX_FEATURES 0 +#define KVM_VCPU_MAX_FEATURES 1/* We don't currently support large pages. */ #define KVM_HPAGE_GFN_SHIFT (x) 0
@@ -100,6 +100,9 @@ struct kvm_vcpu_arch {int last_pcpu;cpumask_t require_dcache_flush; +/* Don't run the guest: see copy_current_insn() */ +bool pause; +/* IO related fields */ struct kvm_decode mmio_decode;
arch/arm/include/asm/kvm_psci.h new file +23 -0
@@ -0,0 +1,23 @@ +/* +* Copyright (C) 2012 - ARM Ltd +* Author: Marc Zyngier <[email protected]> +* +* 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, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef __ARM_KVM_PSCI_H__ +#define __ARM_KVM_PSCI_H__ + +bool kvm_psci_call (struct kvm_vcpu *vcpu); + +#endif /* __ARM_KVM_PSCI_H__ */
arch/arm/include/uapi/asm/kvm.h +16 -0
@@ -65,6 +65,8 @@ struct kvm_regs {#define KVM_ARM_TARGET_CORTEX_A15 0#define KVM_ARM_NUM_TARGETS 1 +#define KVM_ARM_VCPU_POWER_OFF 0/* CPU is started in OFF state */ +struct kvm_vcpu_init {__u32 target;__u32 features[7 ];
@@ -145,4 +147,18 @@ struct kvm_arch_memory_slot {/* Highest supported SPI, from VGIC_NR_IRQS */ #define KVM_ARM_IRQ_GIC_MAX 127 +/* PSCI interface */ +#define KVM_PSCI_FN_BASE 0x95c1ba5e +#define KVM_PSCI_FN (n) (KVM_PSCI_FN_BASE + (n)) + +#define KVM_PSCI_FN_CPU_SUSPEND KVM_PSCI_FN(0) +#define KVM_PSCI_FN_CPU_OFF KVM_PSCI_FN(1) +#define KVM_PSCI_FN_CPU_ON KVM_PSCI_FN(2) +#define KVM_PSCI_FN_MIGRATE KVM_PSCI_FN(3) + +#define KVM_PSCI_RET_SUCCESS 0 +#define KVM_PSCI_RET_NI ((unsigned long)-1) +#define KVM_PSCI_RET_INVAL ((unsigned long)-2) +#define KVM_PSCI_RET_DENIED ((unsigned long)-3) +#endif /* __ARM_KVM_H__ */
arch/arm/kvm/Makefile +1 -1
@@ -18,4 +18,4 @@ kvm-arm-y = $(addprefix ../../../virt/kvm/, kvm_main.o coalesced_mmio.o) obj-y += kvm-arm.o init.o interrupts.o obj-y += arm.o guest.o mmu.o emulate.o reset.o -obj-y += coproc.o coproc_a15.o mmio.o +obj-y += coproc.o coproc_a15.o mmio.o psci.o
arch/arm/kvm/arm.c +25 -3
@@ -43,6 +43,7 @@#include <asm/kvm_mmu.h> #include <asm/kvm_emulate.h> #include <asm/kvm_coproc.h> +#include <asm/kvm_psci.h> #include <asm/opcodes.h> #ifdef REQUIRES_VIRT
@@ -160,6 +161,7 @@ int kvm_dev_ioctl_check_extension(long ext)case KVM_CAP_SYNC_MMU :case KVM_CAP_DESTROY_MEMORY_REGION_WORKS :case KVM_CAP_ONE_REG : +case KVM_CAP_ARM_PSCI : r =1 ;break ;case KVM_CAP_COALESCED_MMIO :
@@ -443,13 +445,17 @@ static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run)trace_kvm_hvc (*vcpu_pc (vcpu), *vcpu_reg (vcpu,0 ), vcpu->arch.hsr &HSR_HVC_IMM_MASK ); +if (kvm_psci_call (vcpu)) +return 1 ; +return 1 ; }static int handle_smc (struct kvm_vcpu *vcpu,struct kvm_run *run) { -/* We don't support SMC; don't do that. */ -kvm_debug ("smc: at %08x" , *vcpu_pc (vcpu)); +if (!kvm_psci_call (vcpu)) +return 1 ; +kvm_inject_undefined (vcpu);return 1 ; }
@@ -588,6 +594,16 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)return 0 ; vcpu->arch.has_run_once = true; + +/* +* Handle the "start in power-off" case by calling into the +* PSCI code. +*/ +if (test_and_clear_bit (KVM_ARM_VCPU_POWER_OFF , vcpu->arch.features)) { + *vcpu_reg (vcpu,0 ) =KVM_PSCI_FN_CPU_OFF ; +kvm_psci_call (vcpu); + } +return 0 ; }
@@ -656,7 +672,13 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)kvm_guest_enter (); vcpu->mode =IN_GUEST_MODE ; - ret =kvm_call_hyp (__kvm_vcpu_run, vcpu); +smp_mb ();/* set mode before reading vcpu->arch.pause */ +if (unlikely (vcpu->arch.pause)) { +/* This means ignore, try again. */ + ret =ARM_EXCEPTION_IRQ ; + }else { + ret =kvm_call_hyp (__kvm_vcpu_run, vcpu); + } vcpu->mode =OUTSIDE_GUEST_MODE ; vcpu->arch.last_pcpu =smp_processor_id ();
arch/arm/kvm/psci.c new file +105 -0
105 lines not on this page · raw message
include/uapi/linux/kvm.h +1 -0
@@ -636,6 +636,7 @@ struct kvm_ppc_smmu_info {#define KVM_CAP_IRQFD_RESAMPLE 82#define KVM_CAP_PPC_BOOKE_WATCHDOG 83#define KVM_CAP_PPC_HTAB_FD 84 +#define KVM_CAP_ARM_PSCI 86#ifdef KVM_CAP_IRQ_ROUTING
Re: [PATCH v6 14/15] KVM: ARM: Power State Coordination Interface implementation
Quoted 7 lines · Christoffer Dall
From: Marc Zyngier <[email protected]>
Implement the PSCI specification (ARM DEN 0022A) to control virtual CPUs being "powered" on or off.
PSCI/KVM is detected using the
KVM_CAP_ARM_PSCIcapability.
Quoted 8 lines
A virtual CPU can now be initialized in a "powered off" state, using the
KVM_ARM_VCPU_POWER_OFFfeature flag.The guest can use either SMC or HVC to execute a PSCI function.
Reviewed-by Will Deacon <[email protected]>Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Christoffer Dall <[email protected]>
A few bits went wrong when you reworked this patch. See below.
Quoted 59 lines
--- Documentation/virtual/kvm/api.txt | 4 + arch/arm/include/asm/kvm_emulate.h | 5 ++ arch/arm/include/asm/kvm_host.h | 5 +- arch/arm/include/asm/kvm_psci.h | 23 ++++++++ arch/arm/include/uapi/asm/kvm.h | 16 +++++ arch/arm/kvm/Makefile | 2 - arch/arm/kvm/arm.c | 28 +++++++++- arch/arm/kvm/psci.c | 105 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 1 9 files changed, 184 insertions(+), 5 deletions(-) create mode 100644 arch/arm/include/asm/kvm_psci.h create mode 100644 arch/arm/kvm/psci.cDocumentation/virtual/kvm/api.txt +4
-0@@ -2185,6 +2185,10 @@ return ENOEXEC for that vcpu. Note that because some registers reflect machine topology, all vcpus should be created before this ioctl is invoked. +Possible features: + - KVM_ARM_VCPU_POWER_OFF: Starts the CPU in a power-off state. + Depends on KVM_CAP_ARM_PSCI. + 4.78 KVM_GET_REG_LISTarch/arm/include/asm/kvm_emulate.h +5
-0@@ -32,6 +32,11 @@ void kvm_inject_undefined(struct kvm_vcpu *vcpu);void kvm_inject_dabt (struct kvm_vcpu *vcpu,unsigned long addr);void kvm_inject_pabt (struct kvm_vcpu *vcpu,unsigned long addr); +static inline bool vcpu_mode_is_32bit (struct kvm_vcpu *vcpu) +{ +return 1 ; +} +static inline u32 *vcpu_pc (struct kvm_vcpu *vcpu) {return (u32 *)&vcpu->arch.regs.usr_regs.ARM_pc;arch/arm/include/asm/kvm_host.h +1
-1@@ -30,7 +30,7 @@#define KVM_COALESCED_MMIO_PAGE_OFFSET 1#define KVM_HAVE_ONE_REG -#define KVM_VCPU_MAX_FEATURES 0 +#define KVM_VCPU_MAX_FEATURES 1/* We don't currently support large pages. */ #define KVM_HPAGE_GFN_SHIFT (x) 0
arch/arm/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 103
Quoted 4 lines
arch/arm/include/asm/kvm_host.h +1
-0@@ -100,6 +100,9 @@ struct kvm_vcpu_arch {int last_pcpu;cpumask_t require_dcache_flush; +/* Don't run the guest: see copy_current_insn() */
Now that you made this field PSCI-specific, how about rewording the comment?
Quoted 97 lines
arch/arm/include/asm/kvm_host.h +2
-0+bool pause; +/* IO related fields */ struct kvm_decode mmio_decode;arch/arm/include/asm/kvm_psci.h new file +23
-0@@ -0,0 +1,23 @@ +/* +* Copyright (C) 2012 - ARM Ltd +* Author: Marc Zyngier <[email protected]> +* +* 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, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef __ARM_KVM_PSCI_H__ +#define __ARM_KVM_PSCI_H__ + +bool kvm_psci_call (struct kvm_vcpu *vcpu); + +#endif /* __ARM_KVM_PSCI_H__ */ arch/arm/include/uapi/asm/kvm.h +16
-0@@ -65,6 +65,8 @@ struct kvm_regs {#define KVM_ARM_TARGET_CORTEX_A15 0#define KVM_ARM_NUM_TARGETS 1 +#define KVM_ARM_VCPU_POWER_OFF 0/* CPU is started in OFF state */ +struct kvm_vcpu_init {__u32 target;__u32 features[7 ];@@ -145,4 +147,18 @@ struct kvm_arch_memory_slot {/* Highest supported SPI, from VGIC_NR_IRQS */ #define KVM_ARM_IRQ_GIC_MAX 127 +/* PSCI interface */ +#define KVM_PSCI_FN_BASE 0x95c1ba5e +#define KVM_PSCI_FN (n) (KVM_PSCI_FN_BASE + (n)) + +#define KVM_PSCI_FN_CPU_SUSPEND KVM_PSCI_FN(0) +#define KVM_PSCI_FN_CPU_OFF KVM_PSCI_FN(1) +#define KVM_PSCI_FN_CPU_ON KVM_PSCI_FN(2) +#define KVM_PSCI_FN_MIGRATE KVM_PSCI_FN(3) + +#define KVM_PSCI_RET_SUCCESS 0 +#define KVM_PSCI_RET_NI ((unsigned long)-1) +#define KVM_PSCI_RET_INVAL ((unsigned long)-2) +#define KVM_PSCI_RET_DENIED ((unsigned long)-3) +#endif /* __ARM_KVM_H__ */ arch/arm/kvm/Makefile +1
-1@@ -18,4 +18,4 @@ kvm-arm-y = $(addprefix ../../../virt/kvm/, kvm_main.o coalesced_mmio.o) obj-y += kvm-arm.o init.o interrupts.o obj-y += arm.o guest.o mmu.o emulate.o reset.o -obj-y += coproc.o coproc_a15.o mmio.o +obj-y += coproc.o coproc_a15.o mmio.o psci.oarch/arm/kvm/arm.c +2
-0@@ -43,6 +43,7 @@#include <asm/kvm_mmu.h> #include <asm/kvm_emulate.h> #include <asm/kvm_coproc.h> +#include <asm/kvm_psci.h> #include <asm/opcodes.h> #ifdef REQUIRES_VIRT @@ -160,6 +161,7 @@ int kvm_dev_ioctl_check_extension(long ext)case KVM_CAP_SYNC_MMU :case KVM_CAP_DESTROY_MEMORY_REGION_WORKS :case KVM_CAP_ONE_REG : +case KVM_CAP_ARM_PSCI : r =1 ;break ;case KVM_CAP_COALESCED_MMIO :
arch/arm/kvm/arm.c static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 451
Quoted 7 lines
arch/arm/kvm/arm.c +3
-0@@ -443,13 +445,17 @@ static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run)trace_kvm_hvc (*vcpu_pc (vcpu), *vcpu_reg (vcpu,0 ), vcpu->arch.hsr &HSR_HVC_IMM_MASK ); +if (kvm_psci_call (vcpu)) +return 1 ; +return 1 ;
No undef injection if there is no PSCI match?
arch/arm/kvm/arm.c static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 456
Quoted 7 lines
arch/arm/kvm/arm.c +1
-2}static int handle_smc (struct kvm_vcpu *vcpu,struct kvm_run *run) { -/* We don't support SMC; don't do that. */ -kvm_debug ("smc: at %08x" , *vcpu_pc (vcpu)); +if (!kvm_psci_call (vcpu))
Looks like you got the return value backward here.
arch/arm/kvm/arm.c static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 461
Quoted 5 lines
arch/arm/kvm/arm.c +2
-0+return 1 ; +kvm_inject_undefined (vcpu);return 1 ; }
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v6 14/15] KVM: ARM: Power State Coordination Interface implementation
Quoted 81 lines · Marc Zyngier
Quoted 7 lines · Christoffer Dall
From: Marc Zyngier <[email protected]>
Implement the PSCI specification (ARM DEN 0022A) to control virtual CPUs being "powered" on or off.
PSCI/KVM is detected using the
KVM_CAP_ARM_PSCIcapability.Quoted 8 lines
A virtual CPU can now be initialized in a "powered off" state, using the
KVM_ARM_VCPU_POWER_OFFfeature flag.The guest can use either SMC or HVC to execute a PSCI function.
Reviewed-by Will Deacon <[email protected]>Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Christoffer Dall <[email protected]>A few bits went wrong when you reworked this patch. See below.
Quoted 62 lines
--- Documentation/virtual/kvm/api.txt | 4 + arch/arm/include/asm/kvm_emulate.h | 5 ++ arch/arm/include/asm/kvm_host.h | 5 +- arch/arm/include/asm/kvm_psci.h | 23 ++++++++ arch/arm/include/uapi/asm/kvm.h | 16 +++++ arch/arm/kvm/Makefile | 2 - arch/arm/kvm/arm.c | 28 +++++++++- arch/arm/kvm/psci.c | 105 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 1 9 files changed, 184 insertions(+), 5 deletions(-) create mode 100644 arch/arm/include/asm/kvm_psci.h create mode 100644 arch/arm/kvm/psci.cDocumentation/virtual/kvm/api.txt +4
-0@@ -2185,6 +2185,10 @@ return ENOEXEC for that vcpu. Note that because some registers reflect machine topology, all vcpus should be created before this ioctl is invoked. +Possible features: + - KVM_ARM_VCPU_POWER_OFF: Starts the CPU in a power-off state. + Depends on KVM_CAP_ARM_PSCI. + 4.78 KVM_GET_REG_LISTarch/arm/include/asm/kvm_emulate.h +5
-0@@ -32,6 +32,11 @@ void kvm_inject_undefined(struct kvm_vcpu *vcpu);void kvm_inject_dabt (struct kvm_vcpu *vcpu,unsigned long addr);void kvm_inject_pabt (struct kvm_vcpu *vcpu,unsigned long addr); +static inline bool vcpu_mode_is_32bit (struct kvm_vcpu *vcpu) +{ +return 1 ; +} +static inline u32 *vcpu_pc (struct kvm_vcpu *vcpu) {return (u32 *)&vcpu->arch.regs.usr_regs.ARM_pc;arch/arm/include/asm/kvm_host.h +1
-1@@ -30,7 +30,7 @@#define KVM_COALESCED_MMIO_PAGE_OFFSET 1#define KVM_HAVE_ONE_REG -#define KVM_VCPU_MAX_FEATURES 0 +#define KVM_VCPU_MAX_FEATURES 1/* We don't currently support large pages. */ #define KVM_HPAGE_GFN_SHIFT (x) 0@@ -100,6 +100,9 @@ struct kvm_vcpu_arch {int last_pcpu;cpumask_t require_dcache_flush;
Quoted 4 lines
arch/arm/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 103
Quoted 1 line
arch/arm/include/asm/kvm_host.h +1
-0+/* Don't run the guest: see copy_current_insn() */ Now that you made this field PSCI-specific, how about rewording the comment?
yeah, that would work. And actually it's kind of broken, because if we sent a paused VCPU a signal and that VCPU is never woken up using the PSCI call we'll busy spin in the kernel, which is sad :(
This should eventually be moved to the vcpu requests infrastructure, but I'll send some preparatory patches to the kvm first, so let's fix it in the right way later on (requires reworking the run loop quite a bit), and for now simply fix the pause clause, see patch below.
Quoted 109 lines
Quoted 97 lines
arch/arm/include/asm/kvm_host.h +2
-0+bool pause; +/* IO related fields */ struct kvm_decode mmio_decode;arch/arm/include/asm/kvm_psci.h new file +23
-0@@ -0,0 +1,23 @@ +/* +* Copyright (C) 2012 - ARM Ltd +* Author: Marc Zyngier <[email protected]> +* +* 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, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef __ARM_KVM_PSCI_H__ +#define __ARM_KVM_PSCI_H__ + +bool kvm_psci_call (struct kvm_vcpu *vcpu); + +#endif /* __ARM_KVM_PSCI_H__ */ arch/arm/include/uapi/asm/kvm.h +16
-0@@ -65,6 +65,8 @@ struct kvm_regs {#define KVM_ARM_TARGET_CORTEX_A15 0#define KVM_ARM_NUM_TARGETS 1 +#define KVM_ARM_VCPU_POWER_OFF 0/* CPU is started in OFF state */ +struct kvm_vcpu_init {__u32 target;__u32 features[7 ];@@ -145,4 +147,18 @@ struct kvm_arch_memory_slot {/* Highest supported SPI, from VGIC_NR_IRQS */ #define KVM_ARM_IRQ_GIC_MAX 127 +/* PSCI interface */ +#define KVM_PSCI_FN_BASE 0x95c1ba5e +#define KVM_PSCI_FN (n) (KVM_PSCI_FN_BASE + (n)) + +#define KVM_PSCI_FN_CPU_SUSPEND KVM_PSCI_FN(0) +#define KVM_PSCI_FN_CPU_OFF KVM_PSCI_FN(1) +#define KVM_PSCI_FN_CPU_ON KVM_PSCI_FN(2) +#define KVM_PSCI_FN_MIGRATE KVM_PSCI_FN(3) + +#define KVM_PSCI_RET_SUCCESS 0 +#define KVM_PSCI_RET_NI ((unsigned long)-1) +#define KVM_PSCI_RET_INVAL ((unsigned long)-2) +#define KVM_PSCI_RET_DENIED ((unsigned long)-3) +#endif /* __ARM_KVM_H__ */ arch/arm/kvm/Makefile +1
-1@@ -18,4 +18,4 @@ kvm-arm-y = $(addprefix ../../../virt/kvm/, kvm_main.o coalesced_mmio.o) obj-y += kvm-arm.o init.o interrupts.o obj-y += arm.o guest.o mmu.o emulate.o reset.o -obj-y += coproc.o coproc_a15.o mmio.o +obj-y += coproc.o coproc_a15.o mmio.o psci.oarch/arm/kvm/arm.c +2
-0@@ -43,6 +43,7 @@#include <asm/kvm_mmu.h> #include <asm/kvm_emulate.h> #include <asm/kvm_coproc.h> +#include <asm/kvm_psci.h> #include <asm/opcodes.h> #ifdef REQUIRES_VIRT @@ -160,6 +161,7 @@ int kvm_dev_ioctl_check_extension(long ext)case KVM_CAP_SYNC_MMU :case KVM_CAP_DESTROY_MEMORY_REGION_WORKS :case KVM_CAP_ONE_REG : +case KVM_CAP_ARM_PSCI : r =1 ;break ;case KVM_CAP_COALESCED_MMIO :arch/arm/kvm/arm.c static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 451
Quoted 7 lines
arch/arm/kvm/arm.c +3
-0@@ -443,13 +445,17 @@ static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run)trace_kvm_hvc (*vcpu_pc (vcpu), *vcpu_reg (vcpu,0 ), vcpu->arch.hsr &HSR_HVC_IMM_MASK ); +if (kvm_psci_call (vcpu)) +return 1 ; +return 1 ;No undef injection if there is no PSCI match?
Quoted 2 lines
arch/arm/kvm/arm.c +0
-0}
Quoted 8 lines
arch/arm/kvm/arm.c static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 456
Quoted 5 lines
arch/arm/kvm/arm.c +1
-2static int handle_smc (struct kvm_vcpu *vcpu,struct kvm_run *run) { -/* We don't support SMC; don't do that. */ -kvm_debug ("smc: at %08x" , *vcpu_pc (vcpu)); +if (!kvm_psci_call (vcpu))Looks like you got the return value backward here.
yeah, I missed that call completely.
Quoted 6 lines
arch/arm/kvm/arm.c static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 461
Quoted 5 lines
arch/arm/kvm/arm.c +2
-0+return 1 ; +kvm_inject_undefined (vcpu);return 1 ; }
Thanks, see this patch:
arch/arm/include/asm/kvm_host.h +1 -1
@@ -113,7 +113,7 @@ struct kvm_vcpu_arch {int last_pcpu;cpumask_t require_dcache_flush; -/* Don't run the guest: see copy_current_insn() */ +/* Don't run the guest on this vcpu */ bool pause;/* IO related fields */
arch/arm/kvm/arm.c +0 -0
@@ -502,7 +502,7 @@ static int handle_hvc(struct kvm_vcpu *vcpu,
struct kvm_run *run)
static int handle_smc(struct kvm_vcpu *vcpu, struct kvm_run *run) {
- if (!kvm_psci_call(vcpu))
+ if (kvm_psci_call(vcpu))
return 1;
kvm_inject_undefined(vcpu);
@@ -667,6 +667,13 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
return 0;
}
+static void vcpu_pause(struct kvm_vcpu *vcpu)
+{
+ wait_queue_head_t *wq = kvm_arch_vcpu_wq(vcpu);
+
+ wait_event_interruptible(*wq, !vcpu->arch.pause);
+}
+
/**
* kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
* @vcpu: The VCPU pointer
@@ -710,6 +717,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
update_vttbr(vcpu->kvm);
+ if (vcpu->arch.pause)
+ vcpu_pause(vcpu);
+
kvm_vgic_flush_hwstate(vcpu);
kvm_timer_flush_hwstate(vcpu);
@@ -737,13 +747,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
kvm_guest_enter();
vcpu->mode = IN_GUEST_MODE;
- smp_mb(); /* set mode before reading vcpu->arch.pause */
- if (unlikely(vcpu->arch.pause)) {
- /* This means ignore, try again. */
- ret = ARM_EXCEPTION_IRQ;
- } else {
- ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
- }
+ ret = kvm_call_hyp(__kvm_vcpu_run, vcpu);
vcpu->mode = OUTSIDE_GUEST_MODE; vcpu->arch.last_pcpu = smp_processor_id();
arch/arm/kvm/psci.c +0 -4
@@ -28,11 +28,7 @@static void kvm_psci_vcpu_off (struct kvm_vcpu *vcpu) { -wait_queue_head_t *wq =kvm_arch_vcpu_wq (vcpu); - vcpu->arch.pause = true; - -wait_event_interruptible (*wq, !vcpu->arch.pause); }static unsigned long kvm_psci_vcpu_on (struct kvm_vcpu *source_vcpu)
Signature · 1 line
1.7.9.5
Re: [kvmarm] [PATCH v6 14/15] KVM: ARM: Power State Coordination Interface implementation
Quoted 20 lines · Christoffer Dall
Quoted 18 lines · Marc Zyngier
Quoted 7 lines · Christoffer Dall
From: Marc Zyngier <[email protected]>
Implement the PSCI specification (ARM DEN 0022A) to control virtual CPUs being "powered" on or off.
PSCI/KVM is detected using the
KVM_CAP_ARM_PSCIcapability.Quoted 8 lines
A virtual CPU can now be initialized in a "powered off" state, using the
KVM_ARM_VCPU_POWER_OFFfeature flag.The guest can use either SMC or HVC to execute a PSCI function.
Reviewed-by Will Deacon <[email protected]>Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Christoffer Dall <[email protected]>A few bits went wrong when you reworked this patch. See below.
[...]
Quoted 11 lines
Quoted 11 lines
Review static int handle_hvc(struct kvm_vcpu *vcpu, · line 452
Quoted 9 lines
(quoted diff) +3
-0@@ -443,13 +445,17 @@ static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) trace_kvm_hvc(*vcpu_pc(vcpu), *vcpu_reg(vcpu, 0), vcpu->arch.hsr & HSR_HVC_IMM_MASK); + if (kvm_psci_call(vcpu)) + return 1; + return 1;No undef injection if there is no PSCI match?
You haven't addressed this issue in you patch.
[...]
Quoted 34 lines
Thanks, see this patch:
arch/arm/include/asm/kvm_host.h +1
-1@@ -113,7 +113,7 @@ struct kvm_vcpu_arch {int last_pcpu;cpumask_t require_dcache_flush; -/* Don't run the guest: see copy_current_insn() */ +/* Don't run the guest on this vcpu */ bool pause;/* IO related fields */ arch/arm/kvm/arm.c +1
-1@@ -502,7 +502,7 @@ static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run)static int handle_smc (struct kvm_vcpu *vcpu,struct kvm_run *run) { -if (!kvm_psci_call (vcpu)) +if (kvm_psci_call (vcpu))return 1 ;arch/arm/kvm/arm.c +0
-0kvm_inject_undefined (vcpu);@@ -667,6 +667,13 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)return 0 ; }
arch/arm/kvm/arm.c static int kvm_vcpu_first_run_init(struct kvm_vcpu · line 679
Quoted 10 lines
arch/arm/kvm/arm.c +7
-0+static void vcpu_pause (struct kvm_vcpu *vcpu) +{ +wait_queue_head_t *wq =kvm_arch_vcpu_wq (vcpu); + +wait_event_interruptible (*wq, !vcpu->arch.pause); +} +/** * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute
guest
Quoted 35 lines
code
* @vcpu: The VCPU pointerarch/arm/kvm/arm.c +4
-7@@ -710,6 +717,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) update_vttbr(vcpu->kvm); +if (vcpu->arch.pause) +vcpu_pause (vcpu); +kvm_vgic_flush_hwstate (vcpu);kvm_timer_flush_hwstate (vcpu);@@ -737,13 +747,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)kvm_guest_enter (); vcpu->mode =IN_GUEST_MODE ; -smp_mb ();/* set mode before reading vcpu->arch.pause */ -if (unlikely (vcpu->arch.pause)) { -/* This means ignore, try again. */ - ret =ARM_EXCEPTION_IRQ ; - }else { - ret =kvm_call_hyp (__kvm_vcpu_run, vcpu); - } + ret =kvm_call_hyp (__kvm_vcpu_run, vcpu); vcpu->mode =OUTSIDE_GUEST_MODE ;arch/arm/kvm/arm.c +0
-0vcpu->arch.last_pcpu =smp_processor_id ();arch/arm/kvm/psci.c +0
-0@@ -28,11 +28,7 @@
arch/arm/kvm/psci.c line 34
Quoted 10 lines
arch/arm/kvm/psci.c +0
-4static void kvm_psci_vcpu_off (struct kvm_vcpu *vcpu) { -wait_queue_head_t *wq =kvm_arch_vcpu_wq (vcpu); - vcpu->arch.pause = true; - -wait_event_interruptible (*wq, !vcpu->arch.pause); }static unsigned long kvm_psci_vcpu_on (struct kvm_vcpu *source_vcpu)
Assuming you fix the above, it looks OK to me.
M.
Signature · 1 line
Fast, cheap, reliable. Pick two.
Re: [kvmarm] [PATCH v6 14/15] KVM: ARM: Power State Coordination Interface implementation
Quoted 30 lines · Marc Zyngier
Quoted 20 lines · Christoffer Dall
Quoted 18 lines · Marc Zyngier
Quoted 7 lines · Christoffer Dall
From: Marc Zyngier <[email protected]>
Implement the PSCI specification (ARM DEN 0022A) to control virtual CPUs being "powered" on or off.
PSCI/KVM is detected using the
KVM_CAP_ARM_PSCIcapability.Quoted 8 lines
A virtual CPU can now be initialized in a "powered off" state, using the
KVM_ARM_VCPU_POWER_OFFfeature flag.The guest can use either SMC or HVC to execute a PSCI function.
Reviewed-by Will Deacon <[email protected]>Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Christoffer Dall <[email protected]>A few bits went wrong when you reworked this patch. See below.
[...]
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
(quoted diff) +0
-0@@ -443,13 +445,17 @@ static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) trace_kvm_hvc(*vcpu_pc(vcpu), *vcpu_reg(vcpu, 0), vcpu->arch.hsr & HSR_HVC_IMM_MASK);
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines
Review static int handle_hvc(struct kvm_vcpu *vcpu, · line 452
Quoted 4 lines
(quoted diff) +3
-0+ if (kvm_psci_call(vcpu)) + return 1; + return 1;No undef injection if there is no PSCI match?
You haven't addressed this issue in you patch.
right, well, it's actually quite nice not having it give you an undefined exception when it logs the trace event. The psci protocol relies on a confirmation in form of a return value anyhow, so it was actually on purpose to remove it, so you can do things like easily measure exit times or probe places in the guest.
Let me know what you think.
Re: [kvmarm] [PATCH v6 14/15] KVM: ARM: Power State Coordination Interface implementation
Quoted 1 line · Christoffer Dall
On Mon, Jan 21, 2013 at 5:04 AM, Marc Zyngier <[email protected]>
Quoted 37 lines · wrote:
Quoted 37 lines
Quoted 20 lines · Christoffer Dall
Quoted 18 lines · Marc Zyngier
Quoted 7 lines · Christoffer Dall
From: Marc Zyngier <[email protected]>
Implement the PSCI specification (ARM DEN 0022A) to control virtual CPUs being "powered" on or off.
PSCI/KVM is detected using the
KVM_CAP_ARM_PSCIcapability.Quoted 8 lines
A virtual CPU can now be initialized in a "powered off" state, using the
KVM_ARM_VCPU_POWER_OFFfeature flag.The guest can use either SMC or HVC to execute a PSCI function.
Reviewed-by Will Deacon <[email protected]>Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Christoffer Dall <[email protected]>A few bits went wrong when you reworked this patch. See below.
[...]
Quoted 11 lines
Quoted 11 lines
Review static int handle_hvc(struct kvm_vcpu *vcpu, · line 452
Quoted 9 lines
(quoted diff) +3
-0@@ -443,13 +445,17 @@ static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) trace_kvm_hvc(*vcpu_pc(vcpu), *vcpu_reg(vcpu, 0), vcpu->arch.hsr & HSR_HVC_IMM_MASK); + if (kvm_psci_call(vcpu)) + return 1; + return 1;No undef injection if there is no PSCI match?
Quoted 7 lines
Quoted 2 lines
You haven't addressed this issue in you patch.
right, well, it's actually quite nice not having it give you an undefined exception when it logs the trace event. The psci protocol relies on a confirmation in form of a return value anyhow, so it was actually on purpose to remove it, so you can do things like easily measure exit times or probe places in the guest.
If that's for tracing purpose, why don't you allocate another hypercall? Returning to the guest without signalling that nothing was executed seems very wrong to me.
M.
Signature · 1 line
Fast, cheap, reliable. Pick two.
Re: [kvmarm] [PATCH v6 14/15] KVM: ARM: Power State Coordination Interface implementation
Quoted 49 lines · Marc Zyngier
Quoted 1 line · Christoffer Dall
On Mon, Jan 21, 2013 at 5:04 AM, Marc Zyngier <[email protected]>
Quoted 37 lines · wrote:
Quoted 37 lines
Quoted 20 lines · Christoffer Dall
Quoted 18 lines · Marc Zyngier
Quoted 7 lines · Christoffer Dall
From: Marc Zyngier <[email protected]>
Implement the PSCI specification (ARM DEN 0022A) to control virtual CPUs being "powered" on or off.
PSCI/KVM is detected using the
KVM_CAP_ARM_PSCIcapability.Quoted 8 lines
A virtual CPU can now be initialized in a "powered off" state, using the
KVM_ARM_VCPU_POWER_OFFfeature flag.The guest can use either SMC or HVC to execute a PSCI function.
Reviewed-by Will Deacon <[email protected]>Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Christoffer Dall <[email protected]>A few bits went wrong when you reworked this patch. See below.
[...]
Quoted 11 lines
Quoted 11 lines
Review static int handle_hvc(struct kvm_vcpu *vcpu, · line 452
Quoted 9 lines
(quoted diff) +3
-0@@ -443,13 +445,17 @@ static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) trace_kvm_hvc(*vcpu_pc(vcpu), *vcpu_reg(vcpu, 0), vcpu->arch.hsr & HSR_HVC_IMM_MASK); + if (kvm_psci_call(vcpu)) + return 1; + return 1;No undef injection if there is no PSCI match?
Quoted 7 lines
Quoted 2 lines
You haven't addressed this issue in you patch.
right, well, it's actually quite nice not having it give you an undefined exception when it logs the trace event. The psci protocol relies on a confirmation in form of a return value anyhow, so it was actually on purpose to remove it, so you can do things like easily measure exit times or probe places in the guest.
Quoted 4 lines
If that's for tracing purpose, why don't you allocate another hypercall? Returning to the guest without signalling that nothing was executed seems very wrong to me.
hmm, yeah, maybe you're right.
I was just debating with myself whether an undefined isn't too rough. It made sense when we didn't have any kind of handling of hvc, but now an hvc isn't really an undefined instruction, and if we assume that we have a series of hypercalls, multiplexed by a number in r0, but you don't really know what's available on your VM host, it also seems very wrong to have an ABI that says, try it, and if it's not implemented handle the undefined exception.... Know what I mean? perhaps we should return -1 in r0 instead or something.
We can of course argue that we should have an HVC discovery API or that everything should be set in device tree, but the latter doesn't really fit well with the way we do things now with qemu at least.
Re: [kvmarm] [PATCH v6 14/15] KVM: ARM: Power State Coordination Interface implementation
Quoted 9 lines · Christoffer Dall
Quoted 7 lines · Marc Zyngier
Quoted 1 line · Christoffer Dall
On Mon, Jan 21, 2013 at 5:04 AM, Marc Zyngier <[email protected]>
Quoted 3 lines · wrote:
Quoted 3 lines
Quoted 1 line · Christoffer Dall
On Thu, Jan 17, 2013 at 10:55 AM, Marc Zyngier
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Quoted 19 lines
Quoted 18 lines · wrote:
Quoted 7 lines · Christoffer Dall
From: Marc Zyngier <[email protected]>
Implement the PSCI specification (ARM DEN 0022A) to control virtual CPUs being "powered" on or off.
PSCI/KVM is detected using the
KVM_CAP_ARM_PSCIcapability.Quoted 8 lines
A virtual CPU can now be initialized in a "powered off" state, using the
KVM_ARM_VCPU_POWER_OFFfeature flag.The guest can use either SMC or HVC to execute a PSCI function.
Reviewed-by Will Deacon <[email protected]>Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Christoffer Dall <[email protected]>A few bits went wrong when you reworked this patch. See below.
[...]
Quoted 11 lines
Quoted 11 lines
Review static int handle_hvc(struct kvm_vcpu *vcpu, · line 452
Quoted 9 lines
(quoted diff) +3
-0@@ -443,13 +445,17 @@ static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) trace_kvm_hvc(*vcpu_pc(vcpu), *vcpu_reg(vcpu, 0), vcpu->arch.hsr & HSR_HVC_IMM_MASK); + if (kvm_psci_call(vcpu)) + return 1; + return 1;No undef injection if there is no PSCI match?
You haven't addressed this issue in you patch.
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
right, well, it's actually quite nice not having it give you an undefined exception when it logs the trace event. The psci protocol relies on a confirmation in form of a return value anyhow, so it was actually on purpose to remove it, so you can do things like easily measure exit times or probe places in the guest.
If that's for tracing purpose, why don't you allocate another
hypercall?
Quoted 1 line
Quoted 1 line
Returning to the guest without signalling that nothing was executed
seems
Quoted 4 lines
Quoted 2 lines
very wrong to me.
hmm, yeah, maybe you're right.
Quoted 8 lines
I was just debating with myself whether an undefined isn't too rough. It made sense when we didn't have any kind of handling of hvc, but now an hvc isn't really an undefined instruction, and if we assume that we have a series of hypercalls, multiplexed by a number in r0, but you don't really know what's available on your VM host, it also seems very wrong to have an ABI that says, try it, and if it's not implemented handle the undefined exception.... Know what I mean? perhaps we should return -1 in r0 instead or something.
Aside from the API discovery discussion, my thoughts on the HVC semantics:
The way I see it, HVC effectively becomes an undefined instruction if the upper layer doesn't know about the requested service. This is very different from "I know what you mean, but I can't do it now". We should really tell the guest "I have no clue what you're talking about", because something is utterly wrong. This is a case of not being able to give the VM the semantics it expects, and trying to paper over it.
Now, returning something in r0 could have been a possibility if it was specified in the ARM ARM, and it is not. So we can already forget about it.
Quoted 3 lines
We can of course argue that we should have an HVC discovery API or that everything should be set in device tree, but the latter doesn't really fit well with the way we do things now with qemu at least.
I still have hope for QEMU to move forward and eventually generate a DT based on the host capabilities.
M.
Signature · 1 line
Fast, cheap, reliable. Pick two.
Re: [kvmarm] [PATCH v6 14/15] KVM: ARM: Power State Coordination Interface implementation
Quoted 2 lines · Marc Zyngier
I still have hope for QEMU to move forward and eventually generate a DT based on the host capabilities.
We might improve our "modify the DT the user passed us" code, but I don't currently anticipate "generate a DT from scratch". In my experience using anything other that the DT which came from the same tree as the kernel itself is just asking for the kernel to crash with some really obscure and unhelpful error message, so QEMU generating a DT is just going to result in a pile of bugs on the QEMU list where users say "new kernel doesn't boot"...
-- PMM
Re: [kvmarm] [PATCH v6 14/15] KVM: ARM: Power State Coordination Interface implementation
In reply to Marc ZyngierQuoted 79 lines · Marc Zyngier
Quoted 9 lines · Christoffer Dall
Quoted 7 lines · Marc Zyngier
Quoted 1 line · Christoffer Dall
On Mon, Jan 21, 2013 at 5:04 AM, Marc Zyngier <[email protected]>
Quoted 3 lines · wrote:
Quoted 3 lines
Quoted 1 line · Christoffer Dall
On Thu, Jan 17, 2013 at 10:55 AM, Marc Zyngier
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Quoted 19 lines
Quoted 18 lines · wrote:
Quoted 7 lines · Christoffer Dall
From: Marc Zyngier <[email protected]>
Implement the PSCI specification (ARM DEN 0022A) to control virtual CPUs being "powered" on or off.
PSCI/KVM is detected using the
KVM_CAP_ARM_PSCIcapability.Quoted 8 lines
A virtual CPU can now be initialized in a "powered off" state, using the
KVM_ARM_VCPU_POWER_OFFfeature flag.The guest can use either SMC or HVC to execute a PSCI function.
Reviewed-by Will Deacon <[email protected]>Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Christoffer Dall <[email protected]>A few bits went wrong when you reworked this patch. See below.
[...]
Quoted 11 lines
Quoted 11 lines
Review static int handle_hvc(struct kvm_vcpu *vcpu, · line 452
Quoted 9 lines
(quoted diff) +3
-0@@ -443,13 +445,17 @@ static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) trace_kvm_hvc(*vcpu_pc(vcpu), *vcpu_reg(vcpu, 0), vcpu->arch.hsr & HSR_HVC_IMM_MASK); + if (kvm_psci_call(vcpu)) + return 1; + return 1;No undef injection if there is no PSCI match?
You haven't addressed this issue in you patch.
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
right, well, it's actually quite nice not having it give you an undefined exception when it logs the trace event. The psci protocol relies on a confirmation in form of a return value anyhow, so it was actually on purpose to remove it, so you can do things like easily measure exit times or probe places in the guest.
If that's for tracing purpose, why don't you allocate another
hypercall?
Quoted 1 line
Quoted 1 line
Returning to the guest without signalling that nothing was executed
seems
Quoted 4 lines
Quoted 2 lines
very wrong to me.
hmm, yeah, maybe you're right.
Quoted 8 lines
I was just debating with myself whether an undefined isn't too rough. It made sense when we didn't have any kind of handling of hvc, but now an hvc isn't really an undefined instruction, and if we assume that we have a series of hypercalls, multiplexed by a number in r0, but you don't really know what's available on your VM host, it also seems very wrong to have an ABI that says, try it, and if it's not implemented handle the undefined exception.... Know what I mean? perhaps we should return -1 in r0 instead or something.
Aside from the API discovery discussion, my thoughts on the HVC semantics:
The way I see it, HVC effectively becomes an undefined instruction if the upper layer doesn't know about the requested service. This is very different from "I know what you mean, but I can't do it now". We should really tell the guest "I have no clue what you're talking about", because something is utterly wrong. This is a case of not being able to give the VM the semantics it expects, and trying to paper over it.
Quoted 3 lines
Now, returning something in r0 could have been a possibility if it was specified in the ARM ARM, and it is not. So we can already forget about it.
well if you use an HVC instruction, you're no longer relying on the ARM ARM concerning the effects of such an instruction, you're relying on the assumption of a hyper call ABI that we're discussing right now. And I think that's an important discussion.
If they way forward is, tell the guest which HVCs are available and if you do anything else than this we're going to shoot you with an undef, ok, but for an ABI to inform the guest of an unavailable hypercall, even though the call was valid on some other host, I think that's equally awful and something we wish to avoid.
Anyway, in no way high priority and at this point it's probably safer that we yell than ignore it in silence, so I'm going to put the undef back right away, I just figured the discussion was important, as I said.
-Christoffer
Re: [PATCH v6 14/15] KVM: ARM: Power State Coordination Interface implementation
In reply to Christoffer DallQuoted 266 lines · Christoffer Dall
Quoted 81 lines · Marc Zyngier
Quoted 7 lines · Christoffer Dall
From: Marc Zyngier <[email protected]>
Implement the PSCI specification (ARM DEN 0022A) to control virtual CPUs being "powered" on or off.
PSCI/KVM is detected using the
KVM_CAP_ARM_PSCIcapability.Quoted 8 lines
A virtual CPU can now be initialized in a "powered off" state, using the
KVM_ARM_VCPU_POWER_OFFfeature flag.The guest can use either SMC or HVC to execute a PSCI function.
Reviewed-by Will Deacon <[email protected]>Signed-off-by Marc Zyngier <[email protected]>Signed-off-by Christoffer Dall <[email protected]>A few bits went wrong when you reworked this patch. See below.
Quoted 62 lines
--- Documentation/virtual/kvm/api.txt | 4 + arch/arm/include/asm/kvm_emulate.h | 5 ++ arch/arm/include/asm/kvm_host.h | 5 +- arch/arm/include/asm/kvm_psci.h | 23 ++++++++ arch/arm/include/uapi/asm/kvm.h | 16 +++++ arch/arm/kvm/Makefile | 2 - arch/arm/kvm/arm.c | 28 +++++++++- arch/arm/kvm/psci.c | 105 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 1 9 files changed, 184 insertions(+), 5 deletions(-) create mode 100644 arch/arm/include/asm/kvm_psci.h create mode 100644 arch/arm/kvm/psci.cDocumentation/virtual/kvm/api.txt +4
-0@@ -2185,6 +2185,10 @@ return ENOEXEC for that vcpu. Note that because some registers reflect machine topology, all vcpus should be created before this ioctl is invoked. +Possible features: + - KVM_ARM_VCPU_POWER_OFF: Starts the CPU in a power-off state. + Depends on KVM_CAP_ARM_PSCI. + 4.78 KVM_GET_REG_LISTarch/arm/include/asm/kvm_emulate.h +5
-0@@ -32,6 +32,11 @@ void kvm_inject_undefined(struct kvm_vcpu *vcpu);void kvm_inject_dabt (struct kvm_vcpu *vcpu,unsigned long addr);void kvm_inject_pabt (struct kvm_vcpu *vcpu,unsigned long addr); +static inline bool vcpu_mode_is_32bit (struct kvm_vcpu *vcpu) +{ +return 1 ; +} +static inline u32 *vcpu_pc (struct kvm_vcpu *vcpu) {return (u32 *)&vcpu->arch.regs.usr_regs.ARM_pc;arch/arm/include/asm/kvm_host.h +1
-1@@ -30,7 +30,7 @@#define KVM_COALESCED_MMIO_PAGE_OFFSET 1#define KVM_HAVE_ONE_REG -#define KVM_VCPU_MAX_FEATURES 0 +#define KVM_VCPU_MAX_FEATURES 1/* We don't currently support large pages. */ #define KVM_HPAGE_GFN_SHIFT (x) 0@@ -100,6 +100,9 @@ struct kvm_vcpu_arch {int last_pcpu;cpumask_t require_dcache_flush;Quoted 4 lines
arch/arm/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 103
Quoted 1 line
arch/arm/include/asm/kvm_host.h +1
-0+/* Don't run the guest: see copy_current_insn() */ Now that you made this field PSCI-specific, how about rewording the comment?
yeah, that would work. And actually it's kind of broken, because if we sent a paused VCPU a signal and that VCPU is never woken up using the PSCI call we'll busy spin in the kernel, which is sad :(
This should eventually be moved to the vcpu requests infrastructure, but I'll send some preparatory patches to the kvm first, so let's fix it in the right way later on (requires reworking the run loop quite a bit), and for now simply fix the pause clause, see patch below.
Quoted 109 lines
Quoted 97 lines
arch/arm/include/asm/kvm_host.h +2
-0+bool pause; +/* IO related fields */ struct kvm_decode mmio_decode;arch/arm/include/asm/kvm_psci.h new file +23
-0@@ -0,0 +1,23 @@ +/* +* Copyright (C) 2012 - ARM Ltd +* Author: Marc Zyngier <[email protected]> +* +* 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, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef __ARM_KVM_PSCI_H__ +#define __ARM_KVM_PSCI_H__ + +bool kvm_psci_call (struct kvm_vcpu *vcpu); + +#endif /* __ARM_KVM_PSCI_H__ */ arch/arm/include/uapi/asm/kvm.h +16
-0@@ -65,6 +65,8 @@ struct kvm_regs {#define KVM_ARM_TARGET_CORTEX_A15 0#define KVM_ARM_NUM_TARGETS 1 +#define KVM_ARM_VCPU_POWER_OFF 0/* CPU is started in OFF state */ +struct kvm_vcpu_init {__u32 target;__u32 features[7 ];@@ -145,4 +147,18 @@ struct kvm_arch_memory_slot {/* Highest supported SPI, from VGIC_NR_IRQS */ #define KVM_ARM_IRQ_GIC_MAX 127 +/* PSCI interface */ +#define KVM_PSCI_FN_BASE 0x95c1ba5e +#define KVM_PSCI_FN (n) (KVM_PSCI_FN_BASE + (n)) + +#define KVM_PSCI_FN_CPU_SUSPEND KVM_PSCI_FN(0) +#define KVM_PSCI_FN_CPU_OFF KVM_PSCI_FN(1) +#define KVM_PSCI_FN_CPU_ON KVM_PSCI_FN(2) +#define KVM_PSCI_FN_MIGRATE KVM_PSCI_FN(3) + +#define KVM_PSCI_RET_SUCCESS 0 +#define KVM_PSCI_RET_NI ((unsigned long)-1) +#define KVM_PSCI_RET_INVAL ((unsigned long)-2) +#define KVM_PSCI_RET_DENIED ((unsigned long)-3) +#endif /* __ARM_KVM_H__ */ arch/arm/kvm/Makefile +1
-1@@ -18,4 +18,4 @@ kvm-arm-y = $(addprefix ../../../virt/kvm/, kvm_main.o coalesced_mmio.o) obj-y += kvm-arm.o init.o interrupts.o obj-y += arm.o guest.o mmu.o emulate.o reset.o -obj-y += coproc.o coproc_a15.o mmio.o +obj-y += coproc.o coproc_a15.o mmio.o psci.oarch/arm/kvm/arm.c +2
-0@@ -43,6 +43,7 @@#include <asm/kvm_mmu.h> #include <asm/kvm_emulate.h> #include <asm/kvm_coproc.h> +#include <asm/kvm_psci.h> #include <asm/opcodes.h> #ifdef REQUIRES_VIRT @@ -160,6 +161,7 @@ int kvm_dev_ioctl_check_extension(long ext)case KVM_CAP_SYNC_MMU :case KVM_CAP_DESTROY_MEMORY_REGION_WORKS :case KVM_CAP_ONE_REG : +case KVM_CAP_ARM_PSCI : r =1 ;break ;case KVM_CAP_COALESCED_MMIO :arch/arm/kvm/arm.c static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 451
Quoted 7 lines
arch/arm/kvm/arm.c +3
-0@@ -443,13 +445,17 @@ static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run)trace_kvm_hvc (*vcpu_pc (vcpu), *vcpu_reg (vcpu,0 ), vcpu->arch.hsr &HSR_HVC_IMM_MASK ); +if (kvm_psci_call (vcpu)) +return 1 ; +return 1 ;No undef injection if there is no PSCI match?
Quoted 2 lines
arch/arm/kvm/arm.c +0
-0}Quoted 8 lines
arch/arm/kvm/arm.c static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 456
Quoted 5 lines
arch/arm/kvm/arm.c +1
-2static int handle_smc (struct kvm_vcpu *vcpu,struct kvm_run *run) { -/* We don't support SMC; don't do that. */ -kvm_debug ("smc: at %08x" , *vcpu_pc (vcpu)); +if (!kvm_psci_call (vcpu))Looks like you got the return value backward here.
yeah, I missed that call completely.
Quoted 6 lines
arch/arm/kvm/arm.c static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run) · line 461
Quoted 5 lines
arch/arm/kvm/arm.c +2
-0+return 1 ; +kvm_inject_undefined (vcpu);return 1 ; }Thanks, see this patch:
arch/arm/include/asm/kvm_host.h +1
-1@@ -113,7 +113,7 @@ struct kvm_vcpu_arch {int last_pcpu;cpumask_t require_dcache_flush; -/* Don't run the guest: see copy_current_insn() */ +/* Don't run the guest on this vcpu */ bool pause;/* IO related fields */ arch/arm/kvm/arm.c +1
-1@@ -502,7 +502,7 @@ static int handle_hvc(struct kvm_vcpu *vcpu, struct kvm_run *run)static int handle_smc (struct kvm_vcpu *vcpu,struct kvm_run *run) { -if (!kvm_psci_call (vcpu)) +if (kvm_psci_call (vcpu))return 1 ;arch/arm/kvm/arm.c +7
-0kvm_inject_undefined (vcpu);@@ -667,6 +667,13 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)return 0 ; } +static void vcpu_pause (struct kvm_vcpu *vcpu) +{ +wait_queue_head_t *wq =kvm_arch_vcpu_wq (vcpu); + +wait_event_interruptible (*wq, !vcpu->arch.pause); +} +/** * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code * @vcpu: The VCPU pointer
arch/arm/kvm/arm.c int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, · line 723
Quoted 7 lines
arch/arm/kvm/arm.c +3
-0@@ -710,6 +717,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) update_vttbr(vcpu->kvm); +if (vcpu->arch.pause) +vcpu_pause (vcpu); +
If you check vcpu->arch.pause in kvm_arch_vcpu_runnable() you can use kvm_vcpu_block() here.
Quoted 41 lines
arch/arm/kvm/arm.c +1
-7kvm_vgic_flush_hwstate (vcpu);kvm_timer_flush_hwstate (vcpu);@@ -737,13 +747,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)kvm_guest_enter (); vcpu->mode =IN_GUEST_MODE ; -smp_mb ();/* set mode before reading vcpu->arch.pause */ -if (unlikely (vcpu->arch.pause)) { -/* This means ignore, try again. */ - ret =ARM_EXCEPTION_IRQ ; - }else { - ret =kvm_call_hyp (__kvm_vcpu_run, vcpu); - } + ret =kvm_call_hyp (__kvm_vcpu_run, vcpu); vcpu->mode =OUTSIDE_GUEST_MODE ;arch/arm/kvm/arm.c +0
-0vcpu->arch.last_pcpu =smp_processor_id ();arch/arm/kvm/psci.c +0
-4@@ -28,11 +28,7 @@static void kvm_psci_vcpu_off (struct kvm_vcpu *vcpu) { -wait_queue_head_t *wq =kvm_arch_vcpu_wq (vcpu); - vcpu->arch.pause = true; - -wait_event_interruptible (*wq, !vcpu->arch.pause); }static unsigned long kvm_psci_vcpu_on (struct kvm_vcpu *source_vcpu)Signature · 5 lines
1.7.9.5 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Gleb.
PATCH v6 15/15 KVM: ARM: Add maintainer entry for KVM/ARM
In reply to Christoffer DallAdd an entry in the MAINTAINERS file for KVM/ARM.
--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
@@ -4474,6 +4474,14 @@ F: arch/s390/include/asm/kvm* F: arch/s390/kvm/ F: drivers/s390/kvm/ +KERNEL VIRTUAL MACHINE (KVM) FOR ARM +M: Christoffer Dall <[email protected]> +L: kvmarm at lists.cs.columbia.edu +W: http://http://systems.cs.columbia.edu/projects/kvm-arm +S: Supported +F: arch/arm/include/asm/kvm* +F: arch/arm/kvm/ + KEXEC M: Eric Biederman <[email protected]> W: http://kernel.org/pub/linux/utils/kernel/kexec/
Re: [PATCH v6 15/15] KVM: ARM: Add maintainer entry for KVM/ARM
Quoted 14 lines · Christoffer Dall
Add an entry in the
MAINTAINERSfile for KVM/ARM.Cc Russell King <[email protected]>Reviewed-by Will Deacon <[email protected]>Signed-off-by Christoffer Dall <[email protected]>--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)MAINTAINERS +0
-0
MAINTAINERS F: arch/s390/include/asm/kvm* · line 4480
Quoted 7 lines
MAINTAINERS +4
-0@@ -4474,6 +4474,14 @@ F: arch/s390/include/asm/kvm* F: arch/s390/kvm/ F: drivers/s390/kvm/ +KERNEL VIRTUAL MACHINE (KVM) FOR ARM +M: Christoffer Dall <[email protected]> +L: kvmarm at lists.cs.columbia.edu +W: http://http://systems.cs.columbia.edu/projects/kvm-arm
Oops, I missed that this URL has two http:// prefixes.
MAINTAINERS F: arch/s390/include/asm/kvm* · line 4481
Quoted 1 line
MAINTAINERS +1
-0+S: Supported
This means that you're paid to look after KVM. If so, using a gmail address seems a little odd...
MAINTAINERS F: arch/s390/include/asm/kvm* · line 4482
Quoted 1 line
MAINTAINERS +1
-0+F: arch/arm/include/asm/kvm*
uapi?
MAINTAINERS F: arch/s390/include/asm/kvm* · line 4483
Quoted 1 line
MAINTAINERS +1
-0+F: arch/arm/kvm/
Will
Re: [PATCH v6 15/15] KVM: ARM: Add maintainer entry for KVM/ARM
Quoted 25 lines · Will Deacon
Quoted 14 lines · Christoffer Dall
Add an entry in the
MAINTAINERSfile for KVM/ARM.Cc Russell King <[email protected]>Reviewed-by Will Deacon <[email protected]>Signed-off-by Christoffer Dall <[email protected]>--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)MAINTAINERS +0
-0MAINTAINERS F: arch/s390/include/asm/kvm* · line 4480
Quoted 7 lines
MAINTAINERS +4
-0@@ -4474,6 +4474,14 @@ F: arch/s390/include/asm/kvm* F: arch/s390/kvm/ F: drivers/s390/kvm/ +KERNEL VIRTUAL MACHINE (KVM) FOR ARM +M: Christoffer Dall <[email protected]> +L: kvmarm at lists.cs.columbia.edu +W: http://http://systems.cs.columbia.edu/projects/kvm-armOops, I missed that this URL has two http:// prefixes.
Quoted 11 lines
MAINTAINERS F: arch/s390/include/asm/kvm* · line 4481
Quoted 1 line
MAINTAINERS +1
-0+S: SupportedThis means that you're paid to look after KVM. If so, using a gmail address seems a little odd...
MAINTAINERS F: arch/s390/include/asm/kvm* · line 4482
Quoted 1 line
MAINTAINERS +1
-0+F: arch/arm/include/asm/kvm*uapi?
Quoted 1 line
MAINTAINERS +1
-0+F: arch/arm/kvm/
This was obviously flawed.
This is the fix, thanks:
MAINTAINERS +4 -3
@@ -4475,10 +4475,11 @@ F: arch/s390/kvm/ F: drivers/s390/kvm/ KERNEL VIRTUAL MACHINE (KVM) FOR ARM -M: Christoffer Dall <[email protected]> +M: Christoffer Dall <[email protected]> L: kvmarm at lists.cs.columbia.edu -W: http://http://systems.cs.columbia.edu/projects/kvm-arm -S: Supported +W: http://systems.cs.columbia.edu/projects/kvm-arm +S: Maintained +F: arch/arm/include/uapi/asm/kvm* F: arch/arm/include/asm/kvm* F: arch/arm/kvm/
--
Hi Christoffer,
Quoted 2 lines · Christoffer Dall
The following series implements KVM support for ARM processors, specifically on the Cortex A-15 platform.
I went through the KVM patches (just this series, not the vgic or timers) and apart from a few minor clean-up issues (which can be done later as an additional patch) they look fine to me.
Quoted 11 lines · Catalin Marinas
Hi Christoffer,
Quoted 2 lines · Christoffer Dall
The following series implements KVM support for ARM processors, specifically on the Cortex A-15 platform.
I went through the KVM patches (just this series, not the vgic or timers) and apart from a few minor clean-up issues (which can be done later as an additional patch) they look fine to me.
Reviewed-by Catalin Marinas <[email protected]>
Thanks a lot!
-Christoffer
Gleb Natapov
Reviewed-byQuoted 11 lines · Catalin Marinas
Hi Christoffer,
Quoted 2 lines · Christoffer Dall
The following series implements KVM support for ARM processors, specifically on the Cortex A-15 platform.
I went through the KVM patches (just this series, not the vgic or timers) and apart from a few minor clean-up issues (which can be done later as an additional patch) they look fine to me.
Reviewed-by Catalin Marinas <[email protected]>
Same here. Reviewed the patch series from KVM perspective. There are improvements that can be done to vcpu run loop, but nothing that should prevent this series from going in.
--
Gleb.