162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2020 Google LLC.
462306a36Sopenharmony_ci * Written by David Brazdil <dbrazdil@google.com>
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#ifndef __ARM64_HYP_IMAGE_H__
862306a36Sopenharmony_ci#define __ARM64_HYP_IMAGE_H__
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#define __HYP_CONCAT(a, b)	a ## b
1162306a36Sopenharmony_ci#define HYP_CONCAT(a, b)	__HYP_CONCAT(a, b)
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#ifndef __KVM_NVHE_HYPERVISOR__
1462306a36Sopenharmony_ci/*
1562306a36Sopenharmony_ci * KVM nVHE code has its own symbol namespace prefixed with __kvm_nvhe_,
1662306a36Sopenharmony_ci * to separate it from the kernel proper.
1762306a36Sopenharmony_ci */
1862306a36Sopenharmony_ci#define kvm_nvhe_sym(sym)	__kvm_nvhe_##sym
1962306a36Sopenharmony_ci#else
2062306a36Sopenharmony_ci#define kvm_nvhe_sym(sym)	sym
2162306a36Sopenharmony_ci#endif
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#ifdef LINKER_SCRIPT
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/*
2662306a36Sopenharmony_ci * KVM nVHE ELF section names are prefixed with .hyp, to separate them
2762306a36Sopenharmony_ci * from the kernel proper.
2862306a36Sopenharmony_ci */
2962306a36Sopenharmony_ci#define HYP_SECTION_NAME(NAME)	.hyp##NAME
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/* Symbol defined at the beginning of each hyp section. */
3262306a36Sopenharmony_ci#define HYP_SECTION_SYMBOL_NAME(NAME) \
3362306a36Sopenharmony_ci	HYP_CONCAT(__hyp_section_, HYP_SECTION_NAME(NAME))
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci/*
3662306a36Sopenharmony_ci * Helper to generate linker script statements starting a hyp section.
3762306a36Sopenharmony_ci *
3862306a36Sopenharmony_ci * A symbol with a well-known name is defined at the first byte. This
3962306a36Sopenharmony_ci * is used as a base for hyp relocations (see gen-hyprel.c). It must
4062306a36Sopenharmony_ci * be defined inside the section so the linker of `vmlinux` cannot
4162306a36Sopenharmony_ci * separate it from the section data.
4262306a36Sopenharmony_ci */
4362306a36Sopenharmony_ci#define BEGIN_HYP_SECTION(NAME)				\
4462306a36Sopenharmony_ci	HYP_SECTION_NAME(NAME) : {			\
4562306a36Sopenharmony_ci		HYP_SECTION_SYMBOL_NAME(NAME) = .;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci/* Helper to generate linker script statements ending a hyp section. */
4862306a36Sopenharmony_ci#define END_HYP_SECTION					\
4962306a36Sopenharmony_ci	}
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/* Defines an ELF hyp section from input section @NAME and its subsections. */
5262306a36Sopenharmony_ci#define HYP_SECTION(NAME)			\
5362306a36Sopenharmony_ci	BEGIN_HYP_SECTION(NAME)			\
5462306a36Sopenharmony_ci		*(NAME NAME##.*)		\
5562306a36Sopenharmony_ci	END_HYP_SECTION
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci/*
5862306a36Sopenharmony_ci * Defines a linker script alias of a kernel-proper symbol referenced by
5962306a36Sopenharmony_ci * KVM nVHE hyp code.
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_ci#define KVM_NVHE_ALIAS(sym)	kvm_nvhe_sym(sym) = sym;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/* Defines a linker script alias for KVM nVHE hyp symbols */
6462306a36Sopenharmony_ci#define KVM_NVHE_ALIAS_HYP(first, sec)	kvm_nvhe_sym(first) = kvm_nvhe_sym(sec);
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci#endif /* LINKER_SCRIPT */
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#endif /* __ARM64_HYP_IMAGE_H__ */
69