Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: arm64: Support per_cpu_ptr in nVHE hyp code

When compiling with __KVM_NVHE_HYPERVISOR__, redefine per_cpu_offset()
to __hyp_per_cpu_offset() which looks up the base of the nVHE per-CPU
region of the given cpu and computes its offset from the
.hyp.data..percpu section.

This enables use of per_cpu_ptr() helpers in nVHE hyp code. Until now
only this_cpu_ptr() was supported by setting TPIDR_EL2.

Signed-off-by: David Brazdil <dbrazdil@google.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20201202184122.26046-14-dbrazdil@google.com

authored by

David Brazdil and committed by
Marc Zyngier
687413d3 2d7bf218

+35 -1
+6
arch/arm64/include/asm/percpu.h
··· 239 239 #define this_cpu_cmpxchg_8(pcp, o, n) \ 240 240 _pcp_protect_return(cmpxchg_relaxed, pcp, o, n) 241 241 242 + #ifdef __KVM_NVHE_HYPERVISOR__ 243 + extern unsigned long __hyp_per_cpu_offset(unsigned int cpu); 244 + #define __per_cpu_offset 245 + #define per_cpu_offset(cpu) __hyp_per_cpu_offset((cpu)) 246 + #endif 247 + 242 248 #include <asm-generic/percpu.h> 243 249 244 250 /* Redefine macros for nVHE hyp under DEBUG_PREEMPT to avoid its dependencies. */
+3
arch/arm64/kernel/image-vars.h
··· 99 99 KVM_NVHE_ALIAS(__start___kvm_ex_table); 100 100 KVM_NVHE_ALIAS(__stop___kvm_ex_table); 101 101 102 + /* Array containing bases of nVHE per-CPU memory regions. */ 103 + KVM_NVHE_ALIAS(kvm_arm_hyp_percpu_base); 104 + 102 105 #endif /* CONFIG_KVM */ 103 106 104 107 #endif /* __ARM64_KERNEL_IMAGE_VARS_H */
+2 -1
arch/arm64/kvm/hyp/nvhe/Makefile
··· 6 6 asflags-y := -D__KVM_NVHE_HYPERVISOR__ 7 7 ccflags-y := -D__KVM_NVHE_HYPERVISOR__ 8 8 9 - obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o hyp-main.o 9 + obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o host.o \ 10 + hyp-main.o hyp-smp.o 10 11 obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \ 11 12 ../fpsimd.o ../hyp-entry.o 12 13
+24
arch/arm64/kvm/hyp/nvhe/hyp-smp.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Copyright (C) 2020 - Google LLC 4 + * Author: David Brazdil <dbrazdil@google.com> 5 + */ 6 + 7 + #include <asm/kvm_asm.h> 8 + #include <asm/kvm_hyp.h> 9 + #include <asm/kvm_mmu.h> 10 + 11 + unsigned long __hyp_per_cpu_offset(unsigned int cpu) 12 + { 13 + unsigned long *cpu_base_array; 14 + unsigned long this_cpu_base; 15 + unsigned long elf_base; 16 + 17 + if (cpu >= ARRAY_SIZE(kvm_arm_hyp_percpu_base)) 18 + hyp_panic(); 19 + 20 + cpu_base_array = (unsigned long *)hyp_symbol_addr(kvm_arm_hyp_percpu_base); 21 + this_cpu_base = kern_hyp_va(cpu_base_array[cpu]); 22 + elf_base = (unsigned long)hyp_symbol_addr(__per_cpu_start); 23 + return this_cpu_base - elf_base; 24 + }