Skip to contentSkip to conversation

x86/alternatives: Adjust all insn-relative fields

PATCH In review Andrew Cooper 3 participants 10 messages Oct 15 – Nov 2

lore
Share & tools
lore
#0 lore raw

v4 cover x86/alternatives: Adjust all insn-relative fields

Alternatives have had a reasonably severe restriction since their introduction. This has been the source of several bugs, and several inefficiencies particularly in the speculative safety paths.

https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/2952556080

Andrew Cooper (5):
x86/emul: Introduce x86_decode_lite()
tests/x86: Introduce a userspace test harness for x86_decode_lite() x86/alternative: Walk all replacements during self tests x86/alternative: Relocate all insn-relative fields x86/spec-ctrl: Introduce and use DO_COND_BHB_SEQ

 tools/tests/Makefile                      |   1 +
 tools/tests/x86-decode-lite/.gitignore    |   1 +
 tools/tests/x86-decode-lite/Makefile      |  56 ++
 tools/tests/x86-decode-lite/insns.S       | 711 ++++++++++++++++++++++
 tools/tests/x86-decode-lite/macro-magic.h |  62 ++
 tools/tests/x86-decode-lite/main.c        | 113 ++++
 tools/tests/x86-decode-lite/x86-emulate.h |  29 +
 xen/arch/x86/alternative.c                | 102 +++-
 xen/arch/x86/hvm/vmx/entry.S              |  12 +-
 xen/arch/x86/include/asm/spec_ctrl_asm.h  |  43 +-
 xen/arch/x86/x86_emulate/Makefile         |   6 +
 xen/arch/x86/x86_emulate/decode-lite.c    | 331 ++++++++++
 xen/arch/x86/x86_emulate/x86_emulate.h    |  14 +
 13 files changed, 1447 insertions(+), 34 deletions(-)
 create mode 100644 tools/tests/x86-decode-lite/.gitignore
 create mode 100644 tools/tests/x86-decode-lite/Makefile
 create mode 100644 tools/tests/x86-decode-lite/insns.S
 create mode 100644 tools/tests/x86-decode-lite/macro-magic.h
 create mode 100644 tools/tests/x86-decode-lite/main.c
 create mode 100644 tools/tests/x86-decode-lite/x86-emulate.h
 create mode 100644 xen/arch/x86/x86_emulate/decode-lite.c
Signature · 1 line
2.39.5
#1 lore raw

PATCH v4 1/5 x86/emul: Introduce x86_decode_lite()

In order to relocate all IP-relative fields in an alternative replacement block, we need to decode the instructions enough to obtain their length and any relative fields.

Full x86_decode() is far too heavyweight, so introduce a minimal form which can make several simplifying assumptions.

This a mostly-complete decoder for integer instruction in the onebyte and twobyte maps. Some instructions are intentionally unrecognised, as finding them in an alternative is more likely to be a bug than intentional. Some instruction groups and prefixes are unimplemented to reduce decode complexity, as this will be a fasthpath during patching.

This logic can decode all alternative blocks that exist in Xen right now.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>

v4:
* Optimise switch ( osize )
* Optimise SIB
* Don't mention "replacement block" in function comment.

v3:
* Rearrange decode tables to satisfy comment requests without splitting
* Recognise UDB now it's used by Xen
* Fix MISRA violations
* Misc other changes

v2:
* Switch to 0 on failure, rel_sz in bytes
* Mostly complete the integer instructions; paird with userspace harness
* Put in .init when !CONFIG_LIVEPATCH

---
 xen/arch/x86/x86_emulate/Makefile      |   6 +
 xen/arch/x86/x86_emulate/decode-lite.c | 331 +++++++++++++++++++++++++
 xen/arch/x86/x86_emulate/x86_emulate.h |  14 ++
 3 files changed, 351 insertions(+)
 create mode 100644 xen/arch/x86/x86_emulate/decode-lite.c
xen/arch/x86/x86_emulate/Makefile +6 -0
index 295e602f6b86..679bddbb1584 100644
@@ -17,3 +17,9 @@ obj-y += decode.o
 obj-$(CONFIG_HVM) += fpu.o
 obj-y += util.o
 obj-y += util-xen.o
+
+ifeq ($(CONFIG_LIVEPATCH),y)
+obj-y += decode-lite.o
+else
+obj-bin-y += decode-lite.init.o
+endif
xen/arch/x86/x86_emulate/decode-lite.c new file +331 -0

331 lines not on this page · raw message

xen/arch/x86/x86_emulate/x86_emulate.h +14 -0
index 4539b14c1534..a2f9b5e2a2cf 100644
@@ -769,4 +769,18 @@ static inline void x86_emul_reset_event(struct x86_emulate_ctxt *ctxt)
     ctxt->event = (struct x86_event){};
 }
 
+/*
+ * x86_decode_lite().  Very minimal decoder for managing alternatives.
+ *
+ * @len is 0 on error, or nonzero on success.  If the instruction has a
+ * relative field, @rel_sz is nonzero, and @rel points at the field.
+ */
+typedef struct {
+    uint8_t len;
+    uint8_t rel_sz; /* bytes: 0, 1 or 4 */
+    void *rel;
+} x86_decode_lite_t;
+
+x86_decode_lite_t x86_decode_lite(void *ip, const void *end);
+
 #endif /* __X86_EMULATE_H__ */
Signature · 1 line
2.39.5
Reviewed-by on v4 1/5
#2 lore raw

Re: [PATCH v4 1/5] x86/emul: Introduce x86_decode_lite()

Quoted 4 lines · Andrew Cooper

In order to relocate all IP-relative fields in an alternative replacement block, we need to decode the instructions enough to obtain their length and any relative fields.

Quoted 8 lines

Full x86_decode() is far too heavyweight, so introduce a minimal form which can make several simplifying assumptions.

This a mostly-complete decoder for integer instruction in the onebyte and twobyte maps. Some instructions are intentionally unrecognised, as finding them in an alternative is more likely to be a bug than intentional. Some instruction groups and prefixes are unimplemented to reduce decode complexity, as this will be a fasthpath during patching.

Just that patching isn't really something where performance matters very much. It's done during boot (or livepatch load), and then not anymore. (I still agree that reduction of complexity is a worthwhile goal, or course.)

Quoted 57 lines
xen/arch/x86/x86_emulate/decode-lite.c new file +54 -0
@@ -0,0 +1,331 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifdef __XEN__
+# include <xen/init.h>
+#endif
+
+#include "private.h"
+
+#undef ModRM
+
+/*
+ * Bare minimum x86 instruction decoder to parse the alternative replacement
+ * instructions and locate the IP-relative references that may need updating.
+ *
+ * These are:
+ *  - disp8/32 from near direct branches
+ *  - RIP-relative memory references
+ *
+ * The following simplifications are used:
+ *  - All code is 64bit, the instruction stream is well formed and safe to
+ *    read.
+ *  - Instruction groups and prefixes not used by Xen's current alternatives
+ *    are not implemented in order to reduce the decode complexity.
+ *  - Certain instructions are intentionally not recognised, when it is more
+ *    likely for their presence to be an error than intentional.
+ *
+ * Inputs:
+ *  @ip  The position to start decoding from.
+ *  @end End of the code block.  Exceeding this is considered an error.
+ *
+ * Returns: x86_decode_lite_t
+ *  - On failure, length of 0.
+ *  - On success, length > 0.  For rel_sz > 0, rel points at the relative
+ *    field in the instruction stream.
+ */
+x86_decode_lite_t init_or_livepatch x86_decode_lite(void *ip, const void *end)
+{
+#define Imm8   (1 << 0)
+#define Imm    (1 << 1)
+#define Moffs  (1 << 2)
+#define Branch (1 << 5) /* Near direct branches, which have a displacement */
+#define ModRM  (1 << 6)
+#define Known  (1 << 7)
+
+    static const uint8_t init_or_livepatch_const onebyte[256] = {
+
+#define ALU_OPS(x)                              \
+        [(x) + 0] = (Known|ModRM),              \
+        [(x) + 1] = (Known|ModRM),              \
+        [(x) + 2] = (Known|ModRM),              \
+        [(x) + 3] = (Known|ModRM),              \
+        [(x) + 4] = (Known|Imm8),               \
+        [(x) + 5] = (Known|Imm)
+

xen/arch/x86/x86_emulate/decode-lite.c line 64

Quoted 10 lines
xen/arch/x86/x86_emulate/decode-lite.c new file +10 -0
+        ALU_OPS(0x00) /* ADD */, ALU_OPS(0x08) /* OR  */,
+        ALU_OPS(0x10) /* ADC */, ALU_OPS(0x18) /* SBB */,
+        ALU_OPS(0x20) /* AND */, ALU_OPS(0x28) /* SUB */,
+        ALU_OPS(0x30) /* XOR */, ALU_OPS(0x38) /* CMP */,
+
+#undef ALU_OPS
+
+        [0x50 ... 0x5f] = (Known),             /* PUSH/POP %reg */
+
+        [0x62]          = 0,                   /* BOUND, but also EVEX prefix, not implemented. */

Given the purpose, BOUND isn't relevant here at all: We don't mean to use this decoder on 32- or 16-bit code. Hence I'd suggest to put emphasis on EVEX, and name BOUND secondarily. See how ...

xen/arch/x86/x86_emulate/decode-lite.c line 65

Quoted 1 line
xen/arch/x86/x86_emulate/decode-lite.c new file +1 -0
+        [0x63]          = (Known|ModRM),       /* MOVSxd */

... here you don't mention ARPL at all. (Also - why xd in lower case?)

xen/arch/x86/x86_emulate/decode-lite.c line 78

Quoted 12 lines
xen/arch/x86/x86_emulate/decode-lite.c new file +12 -0
+        [0x68]          = (Known|Imm),         /* PUSH $imm */
+        [0x69]          = (Known|ModRM|Imm),   /* IMUL $imm */
+        [0x6a]          = (Known|Imm8),        /* PUSH $imm8 */
+        [0x6b]          = (Known|ModRM|Imm8),  /* PUSH $imm8 */
+        [0x6c ... 0x6f] = (Known),             /* INS/OUTS */
+        [0x70 ... 0x7f] = (Known|Branch|Imm8), /* Jcc disp8 */
+        [0x80]          = (Known|ModRM|Imm8),  /* Grp1 */
+        [0x81]          = (Known|ModRM|Imm),   /* Grp1 */
+
+        [0x83]          = (Known|ModRM|Imm8),  /* Grp1 */
+        [0x84 ... 0x8e] = (Known|ModRM),       /* TEST/XCHG/MOV/MOV-SREG/LEA */
+        [0x8f]          = 0,                   /* Grp1A - POP but also XOP prefix, not implemented. */

I continue thinking that the asymmetry between PUSH r/m and POP r/m being deliberate wants at least mentioning in the description.

Quoted 8 lines
xen/arch/x86/x86_emulate/decode-lite.c new file +8 -0
+        [0x90 ... 0x99] = (Known),             /* NOP/XCHG %rAX/CLTQ/CQTO */
+
+        [0x9b ... 0x9f] = (Known),             /* FWAIT/PUSHF/POPF/SAHF/LAHF */
+        [0xa0 ... 0xa3] = (Known|Moffs),       /* MOVABS */
+        [0xa4 ... 0xa7] = (Known),             /* MOVS/CMPS */
+        [0xa8]          = (Known|Imm8),        /* TEST %al */
+        [0xa9]          = (Known|Imm),         /* TEST %rAX */
+        [0xaa ... 0xaf] = (Known),             /* STOS/LODS/SCAS */

xen/arch/x86/x86_emulate/decode-lite.c line 96

Quoted 10 lines
xen/arch/x86/x86_emulate/decode-lite.c new file +10 -0
+        [0xb0 ... 0xb7] = (Known|Imm8),        /* MOV $imm8, %reg */
+        [0xb8 ... 0xbf] = (Known|Imm),         /* MOV $imm{16,32,64}, %reg */
+        [0xc0 ... 0xc1] = (Known|ModRM|Imm8),  /* Grp2 (ROL..SAR $imm8, %reg) */
+
+        [0xc3]          = (Known),             /* RET */
+        [0xc4 ... 0xc5] = 0,                   /* LES/LDS but also VEX prefixes, not implemented. */
+        [0xc6]          = (Known|ModRM|Imm8),  /* Grp11, Further ModRM decode */
+        [0xc7]          = (Known|ModRM|Imm),   /* Grp11, Further ModRM decode */
+
+        [0xcb ... 0xcc] = (Known),             /* LRET/INT3 */

Here and for RET above: I think it's fine to entirely omit entries for opcodes not defined in 64-bit mode (e.g. 9A, EA), and for SIMD ones. At the same time I think we'd be better off having explicit (zero) entries for (integer) opcodes we deliberately mean to not support (RET/LRET with imm16, ENTER, LEAVE; more further down).

Quoted 39 lines
xen/arch/x86/x86_emulate/decode-lite.c new file +39 -0
+        [0xcd]          = (Known|Imm8),        /* INT $imm8 */
+
+        [0xd0 ... 0xd3] = (Known|ModRM),       /* Grp2 (ROL..SAR {$1,%cl}, %reg) */
+
+        [0xd6]          = (Known),             /* UDB */
+
+        [0xe4 ... 0xe7] = (Known|Imm8),        /* IN/OUT $imm8 */
+        [0xe8 ... 0xe9] = (Known|Branch|Imm),  /* CALL/JMP disp32 */
+
+        [0xeb]          = (Known|Branch|Imm8), /* JMP disp8 */
+        [0xec ... 0xef] = (Known),             /* IN/OUT %dx */
+
+        [0xf1]          = (Known),             /* ICEBP */
+
+        [0xf4]          = (Known),             /* HLT */
+        [0xf5]          = (Known),             /* CMC */
+        [0xf6 ... 0xf7] = (Known|ModRM),       /* Grp3, Further ModRM decode */
+        [0xf8 ... 0xfd] = (Known),             /* CLC ... STD */
+        [0xfe ... 0xff] = (Known|ModRM),       /* Grp4 */
+    };
+    static const uint8_t init_or_livepatch_const twobyte[256] = {
+        [0x00 ... 0x03] = (Known|ModRM),       /* Grp6/Grp7/LAR/LSL */
+
+        [0x0b]          = (Known),             /* UD2 */
+
+        [0x18 ... 0x1f] = (Known|ModRM),       /* Grp16 (Hint Nop) */
+        [0x20 ... 0x23] = (Known|ModRM),       /* MOV %cr/%dr */
+
+        [0x30 ... 0x33] = (Known),             /* WRMSR/RDTSC/RDMSR/RDPMC */
+
+        [0x40 ... 0x4f] = (Known|ModRM),       /* CMOVcc */
+
+        [0x80 ... 0x8f] = (Known|Branch|Imm),  /* Jcc disp32 */
+        [0x90 ... 0x9f] = (Known|ModRM),       /* SETcc */
+
+        [0xa0 ... 0xa2] = (Known),             /* PUSH/POP %fs/CPUID */
+        [0xa3]          = (Known|ModRM),       /* BT */
+        [0xa4]          = (Known|ModRM|Imm8),  /* SHLD $imm8 */
+        [0xa5]          = (Known|ModRM),       /* SHLD %cl */

xen/arch/x86/x86_emulate/decode-lite.c line 145

Quoted 10 lines
xen/arch/x86/x86_emulate/decode-lite.c new file +10 -0
+
+        [0xa8 ... 0xa9] = (Known),             /* PUSH/POP %gs */
+
+        [0xab]          = (Known|ModRM),       /* BTS */
+        [0xac]          = (Known|ModRM|Imm8),  /* SHRD $imm8 */
+        [0xad ... 0xaf] = (Known|ModRM),       /* SHRD %cl/Grp15/IMUL */
+
+        [0xb0 ... 0xb9] = (Known|ModRM),       /* CMPXCHG/LSS/BTR/LFS/LGS/MOVZxx/POPCNT/UD1 */
+        [0xba]          = (Known|ModRM|Imm8),  /* Grp8 */
+        [0xbb ... 0xbf] = (Known|ModRM),       /* BTC/BSF/BSR/MOVSX */

With it being MOVSX here, perhaps MOVZX above? Or the other way around, MOVSxx here?

xen/arch/x86/x86_emulate/decode-lite.c line 146

Quoted 1 line
xen/arch/x86/x86_emulate/decode-lite.c new file +1 -0
+        [0xc0 ... 0xc1] = (Known|ModRM),       /* XADD */

Is there a reason to permit XADD but not CMPXCHG?

Preferably with the above addressed (but explicitly also with some / all left as is, yet then with the expectation that I may send an adjustment patch on top, which I'd then expect to not be outright refused/ignored):

Jan

#3 lore raw

Re: [PATCH v4 1/5] x86/emul: Introduce x86_decode_lite()

Quoted 11 lines · Jan Beulich

xen/arch/x86/x86_emulate/decode-lite.c line 145

Quoted 3 lines · Andrew Cooper
xen/arch/x86/x86_emulate/decode-lite.c new file +3 -0
+        [0xb0 ... 0xb9] = (Known|ModRM),       /* CMPXCHG/LSS/BTR/LFS/LGS/MOVZxx/POPCNT/UD1 */
+        [0xba]          = (Known|ModRM|Imm8),  /* Grp8 */
+        [0xbb ... 0xbf] = (Known|ModRM),       /* BTC/BSF/BSR/MOVSX */

With it being MOVSX here, perhaps MOVZX above? Or the other way around, MOVSxx here?

Quoted 1 line

+ [0xc0 ... 0xc1] = (Known|ModRM), /* XADD */

Is there a reason to permit XADD but not CMPXCHG?

Oh, I'm sorry - CMPXCHG is there, just that I overlooked it in the longish comment.

Jan

#4 lore raw

PATCH v4 2/5 tests/x86: Introduce a userspace test harness for x86_decode_lite()

In reply to Andrew Cooper

All the interesting behaviour is in insns.S.

There are 4 interesting cases; "not an instruction we tolerate", and one we do tolerate, split by no relation, disp8 or disp32. The DECL()/END() macros start and terminate the tests_*[] arrays used by C.

Between DECL()/END(), a macro named _ adds an entry into the array, including a name and the length of the instruction according to the assembler, while being as visually unintrusive as possible.

Plain labels are ad-hoc and there to aid legibility during disassembly. In a couple of cases, the macro named n (for name) allows for choosing a name manually, and is used for cases where the assembler doesn't like the mnemonic.

Clang IAS doesn't like the convenience macro, and Binutils of around 2.30 don't like sysexitl or movsxd with a 32bit operand. As it's only Ubuntu 18.04 affected by this, skip building the harness in old environments.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>

v4:
* Fix %dl/%bh confusion in ModRM testing
* Rebase over header rework
* Uppercase some identifiers

v3:
* Force disable Clang IAS. It doesn't like the _ macro.
* Support 32bit builds

v2:
* New

---
 tools/tests/Makefile                      |   1 +
 tools/tests/x86-decode-lite/.gitignore    |   1 +
 tools/tests/x86-decode-lite/Makefile      |  56 ++
 tools/tests/x86-decode-lite/insns.S       | 711 ++++++++++++++++++++++
 tools/tests/x86-decode-lite/macro-magic.h |  62 ++
 tools/tests/x86-decode-lite/main.c        | 113 ++++
 tools/tests/x86-decode-lite/x86-emulate.h |  29 +
 7 files changed, 973 insertions(+)
 create mode 100644 tools/tests/x86-decode-lite/.gitignore
 create mode 100644 tools/tests/x86-decode-lite/Makefile
 create mode 100644 tools/tests/x86-decode-lite/insns.S
 create mode 100644 tools/tests/x86-decode-lite/macro-magic.h
 create mode 100644 tools/tests/x86-decode-lite/main.c
 create mode 100644 tools/tests/x86-decode-lite/x86-emulate.h
tools/tests/Makefile +1 -0
index fc0ed8091510..ca4f0c707638 100644
@@ -14,6 +14,7 @@ SUBDIRS-y += xenstore
 
 SUBDIRS-$(CONFIG_X86) += cpu-policy
 SUBDIRS-$(CONFIG_X86) += tsx
+SUBDIRS-$(CONFIG_X86) += x86-decode-lite
 ifneq ($(clang),y)
 SUBDIRS-$(CONFIG_X86) += x86_emulator
 endif
tools/tests/x86-decode-lite/.gitignore new file +1 -0
new file mode 100644
index 000000000000..e726b493c993
@@ -0,0 +1 @@
+test-x86-decode-lite
tools/tests/x86-decode-lite/Makefile new file +56 -0

56 lines not on this page · raw message

tools/tests/x86-decode-lite/insns.S new file +711 -0

711 lines not on this page · raw message

tools/tests/x86-decode-lite/macro-magic.h new file +62 -0

62 lines not on this page · raw message

tools/tests/x86-decode-lite/main.c new file +113 -0

113 lines not on this page · raw message

tools/tests/x86-decode-lite/x86-emulate.h new file +29 -0
new file mode 100644
index 000000000000..337718869435
@@ -0,0 +1,29 @@
+#ifndef X86_EMULATE_H
+#define X86_EMULATE_H
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <xen/asm/x86-defns.h>
+#include <xen/asm/x86-event.h>
+#include <xen/asm/x86-types.h>
+#include <xen/asm/x86-vendors.h>
+
+#include <xen-tools/common-macros.h>
+
+#define ASSERT assert
+
+#define printk(...)
+
+#define likely
+#define unlikely
+#define cf_check
+#define init_or_livepatch
+#define init_or_livepatch_const
+
+#include "x86_emulate/x86_emulate.h"
+
+#endif /* X86_EMULATE_H */
Signature · 1 line
2.39.5
Acked-by on v4 2/5
#5 lore raw

Re: [PATCH v4 2/5] tests/x86: Introduce a userspace test harness for x86_decode_lite()

Quoted 4 lines · Andrew Cooper

All the interesting behaviour is in insns.S.

There are 4 interesting cases; "not an instruction we tolerate", and one we do tolerate, split by no relation, disp8 or disp32. The DECL()/END() macros

DYM "relocation" here?

Quoted 4 lines
tools/tests/x86-decode-lite/Makefile new file +1 -0
@@ -0,0 +1,56 @@
+XEN_ROOT = $(CURDIR)/../../..

tools/tests/x86-decode-lite/Makefile line 11

Quoted 10 lines
tools/tests/x86-decode-lite/Makefile new file +10 -0
+include $(XEN_ROOT)/tools/Rules.mk
+
+TARGET :=
+
+# Clang IAS doesn't like the convenience macros we use
+$(call cc-option-add,CFLAGS,CC,-no-integrated-as)
+
+# Binutils around 2.30 have mutually exclusive expectations of instruction
+# suffix validities compared to later versions.  Among the distro we test,
+# this only excludes Ubuntu 18.04.

This is liable to go stale; 18.04's lifetime can't be all that much anymore. I'd suggest to keep just the first sentence.

Quoted 105 lines
tools/tests/x86-decode-lite/insns.S new file +101 -0
@@ -0,0 +1,711 @@
[...]
+sib:
+        /* Mod=0, Reg=0, RM=4, SIB S=3, I=0, B {0..f} */
+        _ add %al, (%rax, %rax, 8)
+        _ add %al, (%rcx, %rax, 8)
+        _ add %al, (%rdx, %rax, 8)
+        _ add %al, (%rbx, %rax, 8)
+        _ add %al, (%rsp, %rax, 8)
+        _ add %al, (    , %rax, 8) /* "none", %rbp encoded with mod=1/2 */
+        _ add %al, (%rsi, %rax, 8)
+        _ add %al, (%rdi, %rax, 8)
+        _ add %al, (%r8,  %rax, 8)
+        _ add %al, (%r9,  %rax, 8)
+        _ add %al, (%r10, %rax, 8)
+        _ add %al, (%r11, %rax, 8)
+        _ add %al, (%r12, %rax, 8)
+        _ rex.b add %al,(,%rax, 8) /* "none", %r13 encoded with mod=1/2 */
+        _ add %al, (%r14, %rax, 8)
+        _ add %al, (%r15, %rax, 8)
+
+        /* Mod=1, Reg=0, RM=4, SIB S=3, I=0, B {0..f} */
+        _ add %al, 0x01(%rax, %rax, 8)
+        _ add %al, 0x01(%rcx, %rax, 8)
+        _ add %al, 0x01(%rdx, %rax, 8)
+        _ add %al, 0x01(%rbx, %rax, 8)
+        _ add %al, 0x01(%rsp, %rax, 8)
+        _ add %al, 0x01(%rbp, %rax, 8)
+        _ add %al, 0x01(%rsi, %rax, 8)
+        _ add %al, 0x01(%rdi, %rax, 8)
+        _ add %al, 0x01(%r8,  %rax, 8)
+        _ add %al, 0x01(%r9,  %rax, 8)
+        _ add %al, 0x01(%r10, %rax, 8)
+        _ add %al, 0x01(%r11, %rax, 8)
+        _ add %al, 0x01(%r12, %rax, 8)
+        _ add %al, 0x01(%r13, %rax, 8)
+        _ add %al, 0x01(%r14, %rax, 8)
+        _ add %al, 0x01(%r15, %rax, 8)
+
+        /* Mod=2, Reg=0, RM=4, SIB S=3, I=0, B {0..f} */
+        _ add %al, 0x7f000001(%rax, %rax, 8)
+        _ add %al, 0x7f000001(%rcx, %rax, 8)
+        _ add %al, 0x7f000001(%rdx, %rax, 8)
+        _ add %al, 0x7f000001(%rbx, %rax, 8)
+        _ add %al, 0x7f000001(%rsp, %rax, 8)
+        _ add %al, 0x7f000001(%rbp, %rax, 8)
+        _ add %al, 0x7f000001(%rsi, %rax, 8)
+        _ add %al, 0x7f000001(%rdi, %rax, 8)
+        _ add %al, 0x7f000001(%r8,  %rax, 8)
+        _ add %al, 0x7f000001(%r9,  %rax, 8)
+        _ add %al, 0x7f000001(%r10, %rax, 8)
+        _ add %al, 0x7f000001(%r11, %rax, 8)
+        _ add %al, 0x7f000001(%r12, %rax, 8)
+        _ add %al, 0x7f000001(%r13, %rax, 8)
+        _ add %al, 0x7f000001(%r14, %rax, 8)
+        _ add %al, 0x7f000001(%r15, %rax, 8)
+
+        /* Mod=0, Reg=0, RM=4, SIB S=3, I=4, B {0..f} */
+        _ add %al, (%rax, %riz, 8)
+        _ add %al, (%rcx, %riz, 8)
+        _ add %al, (%rdx, %riz, 8)
+        _ add %al, (%rbx, %riz, 8)
+        _ add %al, (%rsp, %riz, 8)
+        _ add %al, (    , %riz, 8) /* %rbp encoded with mod=1/2 */
+        _ add %al, (%rsi, %riz, 8)
+        _ add %al, (%rdi, %riz, 8)
+        _ add %al, (%r8,  %riz, 8)
+        _ add %al, (%r9,  %riz, 8)
+        _ add %al, (%r10, %riz, 8)
+        _ add %al, (%r11, %riz, 8)
+        _ add %al, (%r12, %riz, 8)
+        _ rex.b add %al,(,%riz, 8) /* %r13 encoded with mod=1/2 */
+        _ add %al, (%r14, %riz, 8)
+        _ add %al, (%r15, %riz, 8)
+
+        /* Mod=1, Reg=0, RM=4, SIB S=3, I=4, B {0..f} */
+        _ add %al, 0x01(%rax, %riz, 8)
+        _ add %al, 0x01(%rcx, %riz, 8)
+        _ add %al, 0x01(%rdx, %riz, 8)
+        _ add %al, 0x01(%rbx, %riz, 8)
+        _ add %al, 0x01(%rsp, %riz, 8)
+        _ add %al, 0x01(%rbp, %riz, 8)
+        _ add %al, 0x01(%rsi, %riz, 8)
+        _ add %al, 0x01(%rdi, %riz, 8)
+        _ add %al, 0x01(%r8,  %riz, 8)
+        _ add %al, 0x01(%r9,  %riz, 8)
+        _ add %al, 0x01(%r10, %riz, 8)
+        _ add %al, 0x01(%r11, %riz, 8)
+        _ add %al, 0x01(%r12, %riz, 8)
+        _ add %al, 0x01(%r13, %riz, 8)
+        _ add %al, 0x01(%r14, %riz, 8)
+        _ add %al, 0x01(%r15, %riz, 8)
+
+        /* Mod=2, Reg=0, RM=4, SIB S=3, I=4, B {0..f} */
+        _ add %al, 0x7f000001(%rax, %riz, 8)
+        _ add %al, 0x7f000001(%rcx, %riz, 8)
+        _ add %al, 0x7f000001(%rdx, %riz, 8)
+        _ add %al, 0x7f000001(%rbx, %riz, 8)
+        _ add %al, 0x7f000001(%rsp, %riz, 8)
+        _ add %al, 0x7f000001(%rbp, %riz, 8)
+        _ add %al, 0x7f000001(%rsi, %riz, 8)
+        _ add %al, 0x7f000001(%rdi, %riz, 8)
+        _ add %al, 0x7f000001(%r8,  %riz, 8)

tools/tests/x86-decode-lite/insns.S line 193

Quoted 10 lines
tools/tests/x86-decode-lite/insns.S new file +10 -0
+        _ add %al, 0x7f000001(%r9,  %riz, 8)
+        _ add %al, 0x7f000001(%r10, %riz, 8)
+        _ add %al, 0x7f000001(%r11, %riz, 8)
+        _ add %al, 0x7f000001(%r12, %riz, 8)
+        _ add %al, 0x7f000001(%r13, %riz, 8)
+        _ add %al, 0x7f000001(%r14, %riz, 8)
+        _ add %al, 0x7f000001(%r15, %riz, 8)
+
+
+.macro alu_ops op

Nit: Can we avoid double blank lines, please?

tools/tests/x86-decode-lite/insns.S line 258

Quoted 4 lines
tools/tests/x86-decode-lite/insns.S new file +4 -0
+onebyte_row_6x:
+        /*pusha,popa,bound --> not supported */
+        _ movsxd (%rax), %eax
+        _ movslq (%rax), %rax

What about the (odd) 16-bit form? Decoding wise it's identical between AMD and Intel.

tools/tests/x86-decode-lite/insns.S line 265

Quoted 7 lines
tools/tests/x86-decode-lite/insns.S new file +7 -0
+        _ fs nop
+        _ gs nop
+        _ data16 nop
+        /* addr32 --> not supported */
+        _ pushq $0x7f000001
+        _ pushw $0x7f01
+        _ rex.w pushq $0x7f000001

Maybe also

        _ data16 rex.w pushq $0x7f000001

?

tools/tests/x86-decode-lite/insns.S line 271

Quoted 6 lines
tools/tests/x86-decode-lite/insns.S new file +6 -0
+        _ imul $0x7f01, %ax, %ax
+        _ imul $0x7f000001, %eax, %eax
+        _ imul $0x7f000001, %rax, %rax
+        _ pushq $0
+        _ pushw $0
+        _ rex.w pushq $0

Same here then.

Quoted 15 lines
tools/tests/x86-decode-lite/insns.S new file +15 -0
+onebyte_row_Fx:
+        _ lock addb $0, (%rax)
+        n "icebp" .byte 0xf1
+        _ repne nop
+        _ repe nop
+        _ hlt
+        _ cmc
+        _ test $0, %cl /* Grp3, /0 has extra Imm{8,} */
+        _ not %cl
+        _ test $0x7f01, %cx
+        _ not %cx
+        _ test $0x7f000001, %ecx
+        _ not %ecx
+        _ test $0x7f000001, %rcx
+        _ not %rcx

tools/tests/x86-decode-lite/insns.S line 425

Quoted 10 lines
tools/tests/x86-decode-lite/insns.S new file +10 -0
+        _ clc
+        _ stc
+        _ cli
+        _ sti
+        _ cld
+        _ std
+        _ inc %cl /* Grp4 */
+        _ dec %cl
+        _ inc %ecx /* Grp5 */
+        _ dec %ecx

Any reason you use register operands up to here, but then ...

Quoted 1 line

+ _ call *(%rax)

... a memory one here and ...

tools/tests/x86-decode-lite/insns.S line 428

Quoted 2 lines
tools/tests/x86-decode-lite/insns.S new file +2 -0
+        _ lcall *(%rax)
+        _ jmp *(%rax)

... here?

tools/tests/x86-decode-lite/insns.S line 430

Quoted 2 lines
tools/tests/x86-decode-lite/insns.S new file +2 -0
+        _ ljmp *(%rax)
+        _ push (%rax)

(I realize the assembler can't easily be told to encode this with a ModR/M byte when used with a register operand.)

Again preferably with the above addressed (but explicitly also with some / all left as is, except perhaps the double blank line one):

tools/tests/x86-decode-lite/insns.S line 447

Quoted 5 lines
tools/tests/x86-decode-lite/insns.S new file +5 -0
+twobyte_row_2x:
+        _ mov %cr0, %rax
+        _ mov %dr0, %rax
+        _ mov %rax, %cr0
+        _ mov %rax, %dr0

I guess as we don't mean to use odd encodings of these (ModR/M.mod != 3), there's no value in trying to test (and support) such.

tools/tests/x86-decode-lite/insns.S line 459

Quoted 5 lines
tools/tests/x86-decode-lite/insns.S new file +5 -0
+twobyte_row_4x:
+        _ cmovo (%rax), %eax
+        _ cmovg (%rax), %eax
+
+/* twobyte_row_8x: --> Jcc disp32 */

Hmm, noticing only here: You do not foresee a need to support VMREAD/VMWRITE?

Jan

#6 lore raw

PATCH v4 3/5 x86/alternative: Walk all replacements during self tests

In reply to Andrew Cooper

When self tests are active, walk all alternative replacements with x86_decode_lite().

This checks that we can decode all instructions, and also lets us check that disp8's don't leave the replacement block as such a case will definitely malfunction.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>

v2:
* Rebase over API changes in patch 1
* Use +%lu and drop casts
* Swap to CONFIG_SELF_TESTS

---
 xen/arch/x86/alternative.c | 52 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
xen/arch/x86/alternative.c +52 -0
index a4cadd9b0b79..6b5f9c94f6dd 100644
@@ -16,6 +16,7 @@
 #include <asm/stubs.h>
 #include <asm/system.h>
 #include <asm/traps.h>
+#include <asm/x86_emulate.h>
 
 #define MAX_PATCH_LEN (255-1)
 
@@ -578,6 +579,57 @@ static void __init _alternative_instructions(unsigned int what)
 void __init alternative_instructions(void)
 {
     arch_init_ideal_nops();
+
+    /*
+     * Walk all replacement instructions with x86_decode_lite().  This checks
+     * both that we can decode all instructions within the replacement, and
+     * that any near branch with a disp8 stays within the alternative itself.
+     */
+    if ( IS_ENABLED(CONFIG_SELF_TESTS) )
+    {
+        struct alt_instr *a;
+
+        for ( a = __alt_instructions;
+              a < __alt_instructions_end; ++a )
+        {
+            void *repl = ALT_REPL_PTR(a);
+            void *ip = repl, *end = ip + a->repl_len;
+
+            if ( !a->repl_len )
+                continue;
+
+            for ( x86_decode_lite_t res; ip < end; ip += res.len )
+            {
+                const int8_t *d8;
+                const void *target;
+
+                res = x86_decode_lite(ip, end);
+
+                if ( res.len == 0 )
+                {
+                    printk("Alt for %ps [%*ph]\n",
+                           ALT_ORIG_PTR(a), a->repl_len, repl);
+                    panic("  Unable to decode instruction at +%lu in alternative\n",
+                          ip - repl);
+                }
+
+                if ( res.rel_sz != 1 )
+                    continue;
+
+                d8 = res.rel;
+                target = ip + res.len + *d8;
+
+                if ( target < repl || target > end )
+                {
+                    printk("Alt for %ps [%*ph]\n",
+                           ALT_ORIG_PTR(a), a->repl_len, repl);
+                    panic("  'JMP/Jcc disp8' at +%lu leaves alternative block\n",
+                          ip - repl);
+                }
+            }
+        }
+    }
+
     _alternative_instructions(ALT_INSNS);
 }
 
Signature · 1 line
2.39.5
#7 lore raw

PATCH v4 4/5 x86/alternative: Relocate all insn-relative fields

In reply to Andrew Cooper

Right now, relocation of displacements is restricted to finding 0xe8/e9 as the first byte of the replacement, but this is overly restrictive.

Use x86_decode_lite() to find and adjust all insn-relative fields.

As with disp8's not leaving the replacemnet block, some disp32's don't either. e.g. the RSB stuffing loop. These stay unmodified.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>

v4:
* Rebase

v3:
* Rebase over the split-out of altcall. Substantially simpler.

---
 xen/arch/x86/alternative.c | 50 +++++++++++++++++++++++++++++++-------
 1 file changed, 41 insertions(+), 9 deletions(-)
xen/arch/x86/alternative.c +41 -9
index 6b5f9c94f6dd..aa7da97f8548 100644
@@ -341,15 +341,47 @@ static int init_or_livepatch _apply_alternatives(struct alt_instr *start,
 
         memcpy(buf, repl, a->repl_len);
 
-        /* 0xe8/0xe9 are relative branches; fix the offset. */
-        if ( a->repl_len >= 5 && (*buf & 0xfe) == 0xe8 )
-            *(int32_t *)(buf + 1) += (long)repl - (long)orig;
-        else if ( IS_ENABLED(CONFIG_RETURN_THUNK) &&
-                  a->repl_len > 5 && buf[a->repl_len - 5] == 0xe9 &&
-                  ((long)repl + a->repl_len +
-                   *(int32_t *)(buf + a->repl_len - 4) ==
-                   (long)__x86_return_thunk) )
-            *(int32_t *)(buf + a->repl_len - 4) += (long)repl - (long)orig;
+        /*
+         * Walk buf[] and adjust any insn-relative operands which leave the
+         * replacement block.
+         */
+        if ( a->repl_len )
+        {
+            uint8_t *ip = buf, *repl_end = ip + a->repl_len;
+
+            for ( x86_decode_lite_t res; ip < repl_end; ip += res.len )
+            {
+                int32_t *d32;
+                const uint8_t *target;
+
+                res = x86_decode_lite(ip, repl_end);
+
+                if ( res.len == 0 )
+                {
+                    printk("Alt for %ps [%*ph]\n"
+                           "  Unable to decode instruction at +%lu in alternative\n",
+                           ALT_ORIG_PTR(a), a->repl_len, repl, ip - repl);
+                    return -EINVAL;
+                }
+
+                if ( res.rel_sz != 4 )
+                    continue;
+
+                d32 = res.rel;
+                target = ip + res.len + *d32;
+
+                if ( target >= buf && target <= repl_end )
+                {
+                    /*
+                     * Target doesn't leave the replacement block.  e.g. RSB
+                     * stuffing.  Leave it unmodified.
+                     */
+                    continue;
+                }
+
+                *d32 += (long)repl - (long)orig;
+            }
+        }
 
         a->priv = 1;
 
Signature · 1 line
2.39.5
#8 lore raw

PATCH v4 5/5 x86/spec-ctrl: Introduce and use DO_COND_BHB_SEQ

In reply to Andrew Cooper

Now that alternatives can fix up call displacements even when they're not the first instruction of the replacement, move the SCF_entry_bhb conditional inside the replacement block.

This removes a conditional branch from the fastpaths of BHI-unaffected hardware.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Teddy Astie <[email protected]>

---
 xen/arch/x86/hvm/vmx/entry.S             | 12 +++----
 xen/arch/x86/include/asm/spec_ctrl_asm.h | 43 +++++++++++++-----------
 2 files changed, 30 insertions(+), 25 deletions(-)
xen/arch/x86/hvm/vmx/entry.S +6 -6
index cebc70064048..76508c0de2f3 100644
@@ -59,12 +59,12 @@ FUNC(vmx_asm_vmexit_handler)
          * Clear the BHB to mitigate BHI.  Used on eIBRS parts, and uses RETs
          * itself so must be after we've perfomed all the RET-safety we can.
          */
-        testb $SCF_entry_bhb, CPUINFO_scf(%rsp)
-        jz .L_skip_bhb
-        ALTERNATIVE_2 "",                                    \
-            "call clear_bhb_loops", X86_SPEC_BHB_LOOPS,      \
-            "call clear_bhb_tsx", X86_SPEC_BHB_TSX
-.L_skip_bhb:
+        .macro VMX_BHB_SEQ fn:req
+            DO_COND_BHB_SEQ \fn scf=CPUINFO_scf(%rsp)
+        .endm
+        ALTERNATIVE_2 "",                                         \
+            "VMX_BHB_SEQ fn=clear_bhb_loops", X86_SPEC_BHB_LOOPS, \
+            "VMX_BHB_SEQ fn=clear_bhb_tsx",   X86_SPEC_BHB_TSX
 
         ALTERNATIVE "lfence", "", X86_SPEC_NO_LFENCE_ENTRY_VMX
         /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */
xen/arch/x86/include/asm/spec_ctrl_asm.h +24 -19

67 lines not on this page · raw message

Signature · 1 line
2.39.5
Tested-by on the v4 series
#9 lore raw
In reply to Andrew Cooper
Quoted 27 lines · Andrew Cooper

Alternatives have had a reasonably severe restriction since their introduction. This has been the source of several bugs, and several inefficiencies particularly in the speculative safety paths.

https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/2952556080

Andrew Cooper (5):
x86/emul: Introduce x86_decode_lite()
tests/x86: Introduce a userspace test harness for x86_decode_lite() x86/alternative: Walk all replacements during self tests x86/alternative: Relocate all insn-relative fields x86/spec-ctrl: Introduce and use DO_COND_BHB_SEQ

  tools/tests/Makefile                      |   1 +
  tools/tests/x86-decode-lite/.gitignore    |   1 +
  tools/tests/x86-decode-lite/Makefile      |  56 ++
  tools/tests/x86-decode-lite/insns.S       | 711 ++++++++++++++++++++++
  tools/tests/x86-decode-lite/macro-magic.h |  62 ++
  tools/tests/x86-decode-lite/main.c        | 113 ++++
  tools/tests/x86-decode-lite/x86-emulate.h |  29 +
  xen/arch/x86/alternative.c                | 102 +++-
  xen/arch/x86/hvm/vmx/entry.S              |  12 +-
  xen/arch/x86/include/asm/spec_ctrl_asm.h  |  43 +-
  xen/arch/x86/x86_emulate/Makefile         |   6 +
  xen/arch/x86/x86_emulate/decode-lite.c    | 331 ++++++++++
  xen/arch/x86/x86_emulate/x86_emulate.h    |  14 +
  13 files changed, 1447 insertions(+), 34 deletions(-)
Quoted 8 lines

create mode 100644 tools/tests/x86-decode-lite/.gitignore create mode 100644 tools/tests/x86-decode-lite/Makefile create mode 100644 tools/tests/x86-decode-lite/insns.S create mode 100644 tools/tests/x86-decode-lite/macro-magic.h create mode 100644 tools/tests/x86-decode-lite/main.c create mode 100644 tools/tests/x86-decode-lite/x86-emulate.h create mode 100644 xen/arch/x86/x86_emulate/decode-lite.c

I tested this series on top of staging/unstable.

Hardware: Intel Core Ultra 5 226V
Config:   CONFIG_SELF_TESTS=y, spec-ctrl=bhb-entry,bhb-seq=short

(BHB-entry shown for both HVM and PV in the boot log)
Dom0: PVH

Result:
* x86-decode-lite userspace test: passed
* Boot with the alternatives self-test: no decode failures or disp8 escapes reported.
* dom0 and an HVM guest ran normally with the BHB sequence active, including a guest cpuid loop to generate VM exits.
* PV guest not tested

Note: the CPU doesn't select the BHB sequence by default, so I forced it on the command line.

~Thomas

--
Thomas Odin | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech