Skip to content

Commit 23a4d7f

Browse files
author
Martin Schwidefsky
committed
s390/ftrace: use expoline for indirect branches
The return from the ftrace_stub, _mcount, ftrace_caller and return_to_handler functions is done with "br %r14" and "br %r1". These are indirect branches as well and need to use execute trampolines for CONFIG_EXPOLINE=y. The ftrace_caller function is a special case as it returns to the start of a function and may only use %r0 and %r1. For a pre z10 machine the standard execute trampoline uses a LARL + EX to do this, but this requires *two* registers in the range %r1..%r15. To get around this the 'br %r1' located in the lowcore is used, then the EX instruction does not need an address register. But the lowcore trick may only be used for pre z14 machines, with noexec=on the mapping for the first page may not contain instructions. The solution for that is an ALTERNATIVE in the expoline THUNK generated by 'GEN_BR_THUNK %r1' to switch to EXRL, this relies on the fact that a machine that supports noexec=on has EXRL as well. Cc: [email protected] # 4.16 Fixes: f19fbd5 ("s390: introduce execute-trampolines for branches") Signed-off-by: Martin Schwidefsky <[email protected]>
1 parent 97489e0 commit 23a4d7f

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

arch/s390/include/asm/nospec-insn.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
#ifndef _ASM_S390_NOSPEC_ASM_H
33
#define _ASM_S390_NOSPEC_ASM_H
44

5+
#include <asm/alternative-asm.h>
6+
#include <asm/asm-offsets.h>
57
#include <asm/dwarf.h>
68

79
#ifdef __ASSEMBLY__
810

911
#ifdef CONFIG_EXPOLINE
1012

13+
_LC_BR_R1 = __LC_BR_R1
14+
1115
/*
1216
* The expoline macros are used to create thunks in the same format
1317
* as gcc generates them. The 'comdat' section flag makes sure that
@@ -78,13 +82,21 @@
7882
.endm
7983

8084
.macro __THUNK_EX_BR reg,ruse
85+
# Be very careful when adding instructions to this macro!
86+
# The ALTERNATIVE replacement code has a .+10 which targets
87+
# the "br \reg" after the code has been patched.
8188
#ifdef CONFIG_HAVE_MARCH_Z10_FEATURES
8289
exrl 0,555f
8390
j .
8491
#else
92+
.ifc \reg,%r1
93+
ALTERNATIVE "ex %r0,_LC_BR_R1", ".insn ril,0xc60000000000,0,.+10", 35
94+
j .
95+
.else
8596
larl \ruse,555f
8697
ex 0,0(\ruse)
8798
j .
99+
.endif
88100
#endif
89101
555: br \reg
90102
.endm

arch/s390/kernel/asm-offsets.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ int main(void)
181181
OFFSET(__LC_MACHINE_FLAGS, lowcore, machine_flags);
182182
OFFSET(__LC_PREEMPT_COUNT, lowcore, preempt_count);
183183
OFFSET(__LC_GMAP, lowcore, gmap);
184+
OFFSET(__LC_BR_R1, lowcore, br_r1_trampoline);
184185
/* software defined ABI-relevant lowcore locations 0xe00 - 0xe20 */
185186
OFFSET(__LC_DUMP_REIPL, lowcore, ipib);
186187
/* hardware defined lowcore locations 0x1000 - 0x18ff */

arch/s390/kernel/mcount.S

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,25 @@
99
#include <linux/linkage.h>
1010
#include <asm/asm-offsets.h>
1111
#include <asm/ftrace.h>
12+
#include <asm/nospec-insn.h>
1213
#include <asm/ptrace.h>
1314
#include <asm/export.h>
1415

16+
GEN_BR_THUNK %r1
17+
GEN_BR_THUNK %r14
18+
1519
.section .kprobes.text, "ax"
1620

1721
ENTRY(ftrace_stub)
18-
br %r14
22+
BR_EX %r14
1923

2024
#define STACK_FRAME_SIZE (STACK_FRAME_OVERHEAD + __PT_SIZE)
2125
#define STACK_PTREGS (STACK_FRAME_OVERHEAD)
2226
#define STACK_PTREGS_GPRS (STACK_PTREGS + __PT_GPRS)
2327
#define STACK_PTREGS_PSW (STACK_PTREGS + __PT_PSW)
2428

2529
ENTRY(_mcount)
26-
br %r14
30+
BR_EX %r14
2731

2832
EXPORT_SYMBOL(_mcount)
2933

@@ -53,7 +57,7 @@ ENTRY(ftrace_caller)
5357
#endif
5458
lgr %r3,%r14
5559
la %r5,STACK_PTREGS(%r15)
56-
basr %r14,%r1
60+
BASR_EX %r14,%r1
5761
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5862
# The j instruction gets runtime patched to a nop instruction.
5963
# See ftrace_enable_ftrace_graph_caller.
@@ -68,7 +72,7 @@ ftrace_graph_caller_end:
6872
#endif
6973
lg %r1,(STACK_PTREGS_PSW+8)(%r15)
7074
lmg %r2,%r15,(STACK_PTREGS_GPRS+2*8)(%r15)
71-
br %r1
75+
BR_EX %r1
7276

7377
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
7478

@@ -81,6 +85,6 @@ ENTRY(return_to_handler)
8185
aghi %r15,STACK_FRAME_OVERHEAD
8286
lgr %r14,%r2
8387
lmg %r2,%r5,32(%r15)
84-
br %r14
88+
BR_EX %r14
8589

8690
#endif

0 commit comments

Comments
 (0)