[PATCH v2 0/8] arm64: Relocate absolute hyp VAs

David Brazdil
2021-01-05T18:06:49Z

nVHE hyp code is linked into the same kernel binary but executes under
different memory mappings. If the compiler of hyp code chooses absolute
addressing for accessing a symbol, the kernel linker will relocate that
address to a kernel image virtual address, causing a runtime exception.

So far the strategy has been to force PC-relative addressing by wrapping
all symbol references with the hyp_symbol_addr macro. This is error
prone and developer unfriendly.

The series adds a new build-time step for nVHE hyp object file where
positions targeted by R_AARCH64_ABS64 relocations are enumerated and
the information stored in a separate ELF section in the kernel image.
At runtime, the kernel first relocates all absolute addresses to their
actual virtual offset (eg. for KASLR), and then addresses listed in this
section are converted to hyp VAs.

The RFC of this series did not have a build-time step and instead relied
on filtering dynamic relocations at runtime. That approach does not work
if the kernel is built with !CONFIG_RELOCATABLE, hence an always-present
set of relocation positions was added.

The series is based on 5.11-rc2 + kvmarm/next and structured as follows:
* patches 1-2 make sure that all sections referred to by hyp code are
handled by the hyp linker script and prefixed with .hyp so they can
be identified by the build-time tool
* patches 3-5 contain the actual changes to identify and relocate VAs
* patches 6-7 fix existing code that assumes kernel VAs
* patch 8 removes the (now redundant) hyp_symbol_addr

The series is also available at:
https://android-kvm.googlesource.com/linux topic/hyp-reloc_v2

Changes since v1:
* fix for older linkers: declare hyp section symbols in hyp-reloc.S
* fix for older host glibc: define R_AARCH64_ constants if missing
* add generated files to .gitignore

-David

David Brazdil (8):
KVM: arm64: Rename .idmap.text in hyp linker script
KVM: arm64: Set up .hyp.rodata ELF section
KVM: arm64: Add symbol at the beginning of each hyp section
KVM: arm64: Generate hyp relocation data
KVM: arm64: Apply hyp relocations at runtime
KVM: arm64: Fix constant-pool users in hyp
KVM: arm64: Remove patching of fn pointers in hyp
KVM: arm64: Remove hyp_symbol_addr

arch/arm64/include/asm/hyp_image.h | 29 +-
arch/arm64/include/asm/kvm_asm.h | 26 --
arch/arm64/include/asm/kvm_mmu.h | 61 +---
arch/arm64/include/asm/sections.h | 3 +-
arch/arm64/kernel/image-vars.h | 1 -
arch/arm64/kernel/smp.c | 4 +-
arch/arm64/kernel/vmlinux.lds.S | 18 +-
arch/arm64/kvm/arm.c | 7 +-
arch/arm64/kvm/hyp/include/hyp/switch.h | 4 +-
arch/arm64/kvm/hyp/nvhe/.gitignore | 2 +
arch/arm64/kvm/hyp/nvhe/Makefile | 28 +-
arch/arm64/kvm/hyp/nvhe/gen-hyprel.c | 413 +++++++++++++++++++++++
arch/arm64/kvm/hyp/nvhe/host.S | 29 +-
arch/arm64/kvm/hyp/nvhe/hyp-init.S | 4 +-
arch/arm64/kvm/hyp/nvhe/hyp-main.c | 11 +-
arch/arm64/kvm/hyp/nvhe/hyp-smp.c | 4 +-
arch/arm64/kvm/hyp/nvhe/hyp.lds.S | 9 +-
arch/arm64/kvm/hyp/nvhe/psci-relay.c | 24 +-
arch/arm64/kvm/hyp/vgic-v2-cpuif-proxy.c | 2 +-
arch/arm64/kvm/va_layout.c | 34 +-
20 files changed, 578 insertions(+), 135 deletions(-)
create mode 100644 arch/arm64/kvm/hyp/nvhe/gen-hyprel.c

--
2.29.2.729.g45daf8777d-goog