Skip to content

Commit ab25464

Browse files
David BrazdilMarc Zyngier
authored andcommitted
kvm: arm64: Partially link nVHE hyp code, simplify HYPCOPY
Relying on objcopy to prefix the ELF section names of the nVHE hyp code is brittle and prevents us from using wildcards to match specific section names. Improve the build rules by partially linking all '.nvhe.o' files and prefixing their ELF section names using a linker script. Continue using objcopy for prefixing ELF symbol names. One immediate advantage of this approach is that all subsections matching a pattern can be merged into a single prefixed section, eg. .text and .text.* can be linked into a single '.hyp.text'. This removes the need for -fno-reorder-functions on GCC and will be useful in the future too: LTO builds use .text subsections, compilers routinely generate .rodata subsections, etc. Partially linking all hyp code into a single object file also makes it easier to analyze. Signed-off-by: David Brazdil <dbrazdil@google.com> Signed-off-by: Marc Zyngier <maz@kernel.org> Acked-by: Will Deacon <will@kernel.org> Link: https://lore.kernel.org/r/20200922204910.7265-2-dbrazdil@google.com
1 parent f4d51df commit ab25464

4 files changed

Lines changed: 72 additions & 27 deletions

File tree

‎arch/arm64/include/asm/hyp_image.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2020 Google LLC.
4+
* Written by David Brazdil <dbrazdil@google.com>
5+
*/
6+
7+
#ifndef __ARM64_HYP_IMAGE_H__
8+
#define __ARM64_HYP_IMAGE_H__
9+
10+
#ifdef LINKER_SCRIPT
11+
12+
/*
13+
* KVM nVHE ELF section names are prefixed with .hyp, to separate them
14+
* from the kernel proper.
15+
*/
16+
#define HYP_SECTION_NAME(NAME) .hyp##NAME
17+
18+
/* Defines an ELF hyp section from input section @NAME and its subsections. */
19+
#define HYP_SECTION(NAME) \
20+
HYP_SECTION_NAME(NAME) : { *(NAME NAME##.*) }
21+
22+
#endif /* LINKER_SCRIPT */
23+
24+
#endif /* __ARM64_HYP_IMAGE_H__ */

‎arch/arm64/kvm/hyp/nvhe/.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-License-Identifier: GPL-2.0-only
2+
hyp.lds

‎arch/arm64/kvm/hyp/nvhe/Makefile‎

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,46 @@ obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o hyp-init.o
1010
obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
1111
../fpsimd.o ../hyp-entry.o
1212

13-
obj-y := $(patsubst %.o,%.hyp.o,$(obj-y))
14-
extra-y := $(patsubst %.hyp.o,%.hyp.tmp.o,$(obj-y))
13+
##
14+
## Build rules for compiling nVHE hyp code
15+
## Output of this folder is `kvm_nvhe.o`, a partially linked object
16+
## file containing all nVHE hyp code and data.
17+
##
1518

16-
$(obj)/%.hyp.tmp.o: $(src)/%.c FORCE
19+
hyp-obj := $(patsubst %.o,%.nvhe.o,$(obj-y))
20+
obj-y := kvm_nvhe.o
21+
extra-y := $(hyp-obj) kvm_nvhe.tmp.o hyp.lds
22+
23+
# 1) Compile all source files to `.nvhe.o` object files. The file extension
24+
# avoids file name clashes for files shared with VHE.
25+
$(obj)/%.nvhe.o: $(src)/%.c FORCE
1726
$(call if_changed_rule,cc_o_c)
18-
$(obj)/%.hyp.tmp.o: $(src)/%.S FORCE
27+
$(obj)/%.nvhe.o: $(src)/%.S FORCE
1928
$(call if_changed_rule,as_o_S)
20-
$(obj)/%.hyp.o: $(obj)/%.hyp.tmp.o FORCE
21-
$(call if_changed,hypcopy)
2229

23-
# Disable reordering functions by GCC (enabled at -O2).
24-
# This pass puts functions into '.text.*' sections to aid the linker
25-
# in optimizing ELF layout. See HYPCOPY comment below for more info.
26-
ccflags-y += $(call cc-option,-fno-reorder-functions)
30+
# 2) Compile linker script.
31+
$(obj)/hyp.lds: $(src)/hyp.lds.S FORCE
32+
$(call if_changed_dep,cpp_lds_S)
33+
34+
# 3) Partially link all '.nvhe.o' files and apply the linker script.
35+
# Prefixes names of ELF sections with '.hyp', eg. '.hyp.text'.
36+
# Note: The following rule assumes that the 'ld' rule puts LDFLAGS before
37+
# the list of dependencies to form '-T $(obj)/hyp.lds'. This is to
38+
# keep the dependency on the target while avoiding an error from
39+
# GNU ld if the linker script is passed to it twice.
40+
LDFLAGS_kvm_nvhe.tmp.o := -r -T
41+
$(obj)/kvm_nvhe.tmp.o: $(obj)/hyp.lds $(addprefix $(obj)/,$(hyp-obj)) FORCE
42+
$(call if_changed,ld)
43+
44+
# 4) Produce the final 'kvm_nvhe.o', ready to be linked into 'vmlinux'.
45+
# Prefixes names of ELF symbols with '__kvm_nvhe_'.
46+
$(obj)/kvm_nvhe.o: $(obj)/kvm_nvhe.tmp.o FORCE
47+
$(call if_changed,hypcopy)
2748

2849
# The HYPCOPY command uses `objcopy` to prefix all ELF symbol names
29-
# and relevant ELF section names to avoid clashes with VHE code/data.
30-
#
31-
# Hyp code is assumed to be in the '.text' section of the input object
32-
# files (with the exception of specialized sections such as
33-
# '.hyp.idmap.text'). This assumption may be broken by a compiler that
34-
# divides code into sections like '.text.unlikely' so as to optimize
35-
# ELF layout. HYPCOPY checks that no such sections exist in the input
36-
# using `objdump`, otherwise they would be linked together with other
37-
# kernel code and not memory-mapped correctly at runtime.
50+
# to avoid clashes with VHE code/data.
3851
quiet_cmd_hypcopy = HYPCOPY $@
39-
cmd_hypcopy = \
40-
if $(OBJDUMP) -h $< | grep -F '.text.'; then \
41-
echo "$@: function reordering not supported in nVHE hyp code" >&2; \
42-
/bin/false; \
43-
fi; \
44-
$(OBJCOPY) --prefix-symbols=__kvm_nvhe_ \
45-
--rename-section=.text=.hyp.text \
46-
$< $@
52+
cmd_hypcopy = $(OBJCOPY) --prefix-symbols=__kvm_nvhe_ $< $@
4753

4854
# Remove ftrace and Shadow Call Stack CFLAGS.
4955
# This is equivalent to the 'notrace' and '__noscs' annotations.

‎arch/arm64/kvm/hyp/nvhe/hyp.lds.S‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2020 Google LLC.
4+
* Written by David Brazdil <dbrazdil@google.com>
5+
*
6+
* Linker script used for partial linking of nVHE EL2 object files.
7+
*/
8+
9+
#include <asm/hyp_image.h>
10+
11+
SECTIONS {
12+
HYP_SECTION(.text)
13+
}

0 commit comments

Comments
 (0)