lkml.org 
[lkml]   [2021]   [Oct]   [13]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
Subject[PATCH 6/9] x86/alternative: Try inline spectre_v2=retpoline,amd
Try and replace retpoline thunk calls with:

lfence
call *%\reg

for spectre_v2=retpoline,amd.

Specifically, the sequence above is 5 bytes for the low 8 registers,
but 6 bytes for the high 8 registers. This means that unless the
compilers prefix stuff the call with higher registers this replacement
will fail.

Luckily GCC strongly favours RAX for the indirect calls and most (95%+
for defconfig-x86_64) will be converted. OTOH clang strongly favours
R11 and almost nothing gets converted.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
arch/x86/kernel/alternative.c | 24 +++++++++++++++++++-----
1 file changed, 19 insertions(+), 5 deletions(-)

--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -389,15 +389,13 @@ static int emit_indirect(int op, int reg
*
* CALL *%\reg
*
+ * It also tries to inline spectre_v2=retpoline,amd when size permits.
*/
static int patch_retpoline(void *addr, struct insn *insn, u8 *bytes)
{
+ u8 cc, op = insn->opcode.bytes[0];
void (*target)(void);
int reg, ret, i = 0;
- u8 op, cc;
-
- if (cpu_feature_enabled(X86_FEATURE_RETPOLINE))
- return -1;

target = addr + insn->length + insn->immediate.value;
reg = (target - &__x86_indirect_thunk_rax) /
@@ -406,7 +404,22 @@ static int patch_retpoline(void *addr, s
if (WARN_ON_ONCE(reg & ~0xf))
return -1;

- op = insn->opcode.bytes[0];
+ if (cpu_feature_enabled(X86_FEATURE_RETPOLINE_AMD)) {
+ /*
+ * Can't do nothing about the Jcc case here.
+ */
+ if (op != JMP32_INSN_OPCODE && op != CALL_INSN_OPCODE)
+ return -1;
+
+ bytes[i++] = 0x0f;
+ bytes[i++] = 0xae;
+ bytes[i++] = 0xe8; /* lfence */
+
+ goto indirect;
+ }
+
+ if (cpu_feature_enabled(X86_FEATURE_RETPOLINE))
+ return -1;

/*
* Convert:
@@ -430,6 +443,7 @@ static int patch_retpoline(void *addr, s
op = JMP32_INSN_OPCODE;
}

+indirect:
ret = emit_indirect(op, reg, bytes + i);
if (ret < 0)
return ret;

\
 
 \ /
  Last update: 2021-10-13 14:41    [W:0.284 / U:0.052 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site