Skip to contentSkip to conversation

x86: add hintable NOPs emulation

PATCH Superseded Marcos Del Sol Vives 7 participants 28 messages Aug 20, 2025 – Aug 22, 2025 x86

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: add hintable NOPs emulation

Hintable NOPs are a series of instructions introduced by Intel with the Pentium Pro (i686), and described in US patent US5701442A.

These instructions were reserved to allow backwards-compatible changes in the instruction set possible, by having old processors treat them as variable-length NOPs, while having other semantics in modern processors.

Some modern uses are:
- Multi-byte/long NOPs
- Indirect Branch Tracking (ENDBR32)
- Shadow Stack (part of CET)

Some processors advertising i686 compatibility lack full support for them, which may cause #UD to be incorrectly triggered, crashing software that uses then with an unexpected SIGILL.

One such software is sudo in Debian bookworm, which is compiled with GCC -fcf-protection=branch and contains ENDBR32 instructions. It crashes on my Vortex86DX3 processor and VIA C3 Nehalem processors [1].

This patch is a much simplified version of my previous patch for x86 instruction emulation [2], that only emulates hintable NOPs.

When #UD is raised, it checks if the opcode corresponds to a hintable NOP in user space. If true, it warns the user via the dmesg and advances the instruction pointer, thus emulating its expected NOP behaviour.

[1]: https://lists.debian.org/debian-devel/2023/10/msg00118.html
[2]: https://lore.kernel.org/all/[email protected]/

---
 arch/x86/Kconfig                 | 29 +++++++++++++++++++++++++
 arch/x86/include/asm/processor.h |  4 ++++
 arch/x86/kernel/process.c        |  3 +++
 arch/x86/kernel/traps.c          | 36 ++++++++++++++++++++++++++++++++
 4 files changed, 72 insertions(+)
arch/x86/Kconfig +29 -0
index 58d890fe2100..a6daebdc2573 100644
@@ -1286,6 +1286,35 @@ config X86_IOPL_IOPERM
 	  ability to disable interrupts from user space which would be
 	  granted if the hardware IOPL mechanism would be used.
 
+config X86_HNOP_EMU
+	bool "Hintable NOPs emulation"
+	depends on X86_32
+	default y
+	help
+	  Hintable NOPs are a series of instructions introduced by Intel with
+	  the Pentium Pro (i686), and described in US patent US5701442A.
+
+	  These instructions were reserved to allow backwards-compatible
+	  changes in the instruction set possible, by having old processors
+	  treat them as variable-length NOPs, while having other semantics in
+	  modern processors.
+
+	  Some modern uses are:
+	   - Multi-byte/long NOPs
+	   - Indirect Branch Tracking (ENDBR32)
+	   - Shadow Stack (part of CET)
+
+	  Some processors advertising i686 compatibility (such as Cyrix MII,
+	  VIA C3 Nehalem or DM&P Vortex86DX3) lack full support for them,
+	  which may cause SIGILL to be incorrectly raised in user space when
+	  a hintable NOP is encountered.
+
+	  Say Y here if you want the kernel to emulate them, allowing programs
+	  that make use of them to run transparently on such processors.
+
+	  This emulation has no performance penalty for processors that
+	  properly support them, so if unsure, enable it.
+
 config TOSHIBA
 	tristate "Toshiba Laptop support"
 	depends on X86_32
arch/x86/include/asm/processor.h +4 -0
index bde58f6510ac..c34fb678c4de 100644
@@ -499,6 +499,10 @@ struct thread_struct {
 
 	unsigned int		iopl_warn:1;
 
+#ifdef CONFIG_X86_HNOP_EMU
+	unsigned int		hnop_warn:1;
+#endif
+
 	/*
 	 * Protection Keys Register for Userspace.  Loaded immediately on
 	 * context switch. Store it in thread_struct to avoid a lookup in
arch/x86/kernel/process.c +3 -0
index 1b7960cf6eb0..6ec8021638d0 100644
@@ -178,6 +178,9 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 	p->thread.io_bitmap = NULL;
 	clear_tsk_thread_flag(p, TIF_IO_BITMAP);
 	p->thread.iopl_warn = 0;
+#ifdef CONFIG_X86_HNOP_EMU
+	p->thread.hnop_warn = 0;
+#endif
 	memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
 
 #ifdef CONFIG_X86_64
arch/x86/kernel/traps.c +36 -0
index 36354b470590..2dcb7d7edf8a 100644
@@ -295,12 +295,48 @@ DEFINE_IDTENTRY(exc_overflow)
 	do_error_trap(regs, 0, "overflow", X86_TRAP_OF, SIGSEGV, 0, NULL);
 }
 
+#ifdef CONFIG_X86_HNOP_EMU
+static bool handle_hnop(struct pt_regs *regs)
+{
+	struct thread_struct *t = &current->thread;
+	unsigned char buf[MAX_INSN_SIZE];
+	unsigned long nr_copied;
+	struct insn insn;
+
+	nr_copied = insn_fetch_from_user(regs, buf);
+	if (nr_copied <= 0)
+		return false;
+
+	if (!insn_decode_from_regs(&insn, regs, buf, nr_copied))
+		return false;
+
+	/* Hintable NOPs cover 0F 18 to 0F 1F */
+	if (insn.opcode.bytes[0] != 0x0F ||
+		insn.opcode.bytes[1] < 0x18 || insn.opcode.bytes[1] > 0x1F)
+		return false;
+
+	if (!t->hnop_warn) {
+		pr_warn_ratelimited("%s[%d] emulating hintable NOP, ip:%lx\n",
+		       current->comm, task_pid_nr(current), regs->ip);
+		t->hnop_warn = 1;
+	}
+
+	regs->ip += insn.length;
+	return true;
+}
+#endif
+
 #ifdef CONFIG_X86_F00F_BUG
 void handle_invalid_op(struct pt_regs *regs)
 #else
 static inline void handle_invalid_op(struct pt_regs *regs)
 #endif
 {
+#ifdef CONFIG_X86_HNOP_EMU
+	if (user_mode(regs) && handle_hnop(regs))
+		return;
+#endif
+
 	do_error_trap(regs, 0, "invalid opcode", X86_TRAP_UD, SIGILL,
 		      ILL_ILLOPN, error_get_trap_addr(regs));
 }
Signature · 1 line
2.34.1
Acked-by on v1 1/1
#1 lore raw
Quoted 23 lines · Marcos Del Sol Vives

Hintable NOPs are a series of instructions introduced by Intel with the Pentium Pro (i686), and described in US patent US5701442A.

These instructions were reserved to allow backwards-compatible changes in the instruction set possible, by having old processors treat them as variable-length NOPs, while having other semantics in modern processors.

Some modern uses are:
- Multi-byte/long NOPs
- Indirect Branch Tracking (ENDBR32)
- Shadow Stack (part of CET)

Some processors advertising i686 compatibility lack full support for them, which may cause #UD to be incorrectly triggered, crashing software that uses then with an unexpected SIGILL.

One such software is sudo in Debian bookworm, which is compiled with GCC -fcf-protection=branch and contains ENDBR32 instructions. It crashes on my Vortex86DX3 processor and VIA C3 Nehalem processors [1].

This patch is a much simplified version of my previous patch for x86 instruction emulation [2], that only emulates hintable NOPs.

Quoted 8 lines

When #UD is raised, it checks if the opcode corresponds to a hintable NOP in user space. If true, it warns the user via the dmesg and advances the instruction pointer, thus emulating its expected NOP behaviour.

[1]: https://lists.debian.org/debian-devel/2023/10/msg00118.html
[2]: https://lore.kernel.org/all/[email protected]/

This is going to be terribly slow if there's a significant number of traps (like with endbr32), but yeah, this ought to work.

One indenting fail below, other than that:

Quoted 92 lines
---
 arch/x86/Kconfig                 | 29 +++++++++++++++++++++++++
 arch/x86/include/asm/processor.h |  4 ++++
 arch/x86/kernel/process.c        |  3 +++
 arch/x86/kernel/traps.c          | 36 ++++++++++++++++++++++++++++++++
 4 files changed, 72 insertions(+)
arch/x86/Kconfig +29 -0
index 58d890fe2100..a6daebdc2573 100644
@@ -1286,6 +1286,35 @@ config X86_IOPL_IOPERM
 	  ability to disable interrupts from user space which would be
 	  granted if the hardware IOPL mechanism would be used.
 
+config X86_HNOP_EMU
+	bool "Hintable NOPs emulation"
+	depends on X86_32
+	default y
+	help
+	  Hintable NOPs are a series of instructions introduced by Intel with
+	  the Pentium Pro (i686), and described in US patent US5701442A.
+
+	  These instructions were reserved to allow backwards-compatible
+	  changes in the instruction set possible, by having old processors
+	  treat them as variable-length NOPs, while having other semantics in
+	  modern processors.
+
+	  Some modern uses are:
+	   - Multi-byte/long NOPs
+	   - Indirect Branch Tracking (ENDBR32)
+	   - Shadow Stack (part of CET)
+
+	  Some processors advertising i686 compatibility (such as Cyrix MII,
+	  VIA C3 Nehalem or DM&P Vortex86DX3) lack full support for them,
+	  which may cause SIGILL to be incorrectly raised in user space when
+	  a hintable NOP is encountered.
+
+	  Say Y here if you want the kernel to emulate them, allowing programs
+	  that make use of them to run transparently on such processors.
+
+	  This emulation has no performance penalty for processors that
+	  properly support them, so if unsure, enable it.
+
 config TOSHIBA
 	tristate "Toshiba Laptop support"
 	depends on X86_32
arch/x86/include/asm/processor.h +4 -0
index bde58f6510ac..c34fb678c4de 100644
@@ -499,6 +499,10 @@ struct thread_struct {
 
 	unsigned int		iopl_warn:1;
 
+#ifdef CONFIG_X86_HNOP_EMU
+	unsigned int		hnop_warn:1;
+#endif
+
 	/*
 	 * Protection Keys Register for Userspace.  Loaded immediately on
 	 * context switch. Store it in thread_struct to avoid a lookup in
arch/x86/kernel/process.c +3 -0
index 1b7960cf6eb0..6ec8021638d0 100644
@@ -178,6 +178,9 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 	p->thread.io_bitmap = NULL;
 	clear_tsk_thread_flag(p, TIF_IO_BITMAP);
 	p->thread.iopl_warn = 0;
+#ifdef CONFIG_X86_HNOP_EMU
+	p->thread.hnop_warn = 0;
+#endif
 	memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
 
 #ifdef CONFIG_X86_64
arch/x86/kernel/traps.c +8 -0
index 36354b470590..2dcb7d7edf8a 100644
@@ -295,12 +295,48 @@ DEFINE_IDTENTRY(exc_overflow)
 	do_error_trap(regs, 0, "overflow", X86_TRAP_OF, SIGSEGV, 0, NULL);
 }
 
+#ifdef CONFIG_X86_HNOP_EMU
+static bool handle_hnop(struct pt_regs *regs)
+{
+	struct thread_struct *t = &current->thread;
+	unsigned char buf[MAX_INSN_SIZE];
+	unsigned long nr_copied;
+	struct insn insn;
+

arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_overflow) · line 315

Quoted 10 lines
arch/x86/kernel/traps.c +10 -0
+	nr_copied = insn_fetch_from_user(regs, buf);
+	if (nr_copied <= 0)
+		return false;
+
+	if (!insn_decode_from_regs(&insn, regs, buf, nr_copied))
+		return false;
+
+	/* Hintable NOPs cover 0F 18 to 0F 1F */
+	if (insn.opcode.bytes[0] != 0x0F ||
+		insn.opcode.bytes[1] < 0x18 || insn.opcode.bytes[1] > 0x1F)

That continuation wants to be aligned at (, not tab-width.

Quoted 30 lines
arch/x86/kernel/traps.c +18 -0
+		return false;
+
+	if (!t->hnop_warn) {
+		pr_warn_ratelimited("%s[%d] emulating hintable NOP, ip:%lx\n",
+		       current->comm, task_pid_nr(current), regs->ip);
+		t->hnop_warn = 1;
+	}
+
+	regs->ip += insn.length;
+	return true;
+}
+#endif
+
 #ifdef CONFIG_X86_F00F_BUG
 void handle_invalid_op(struct pt_regs *regs)
 #else
 static inline void handle_invalid_op(struct pt_regs *regs)
 #endif
 {
+#ifdef CONFIG_X86_HNOP_EMU
+	if (user_mode(regs) && handle_hnop(regs))
+		return;
+#endif
+
 	do_error_trap(regs, 0, "invalid opcode", X86_TRAP_UD, SIGILL,
 		      ILL_ILLOPN, error_get_trap_addr(regs));
 }
Signature · 1 line
2.34.1
#2 lore raw
Quoted 28 lines · On Wed, 20 Aug 2025 11:07:33 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 27 lines · Marcos Del Sol Vives

Hintable NOPs are a series of instructions introduced by Intel with the Pentium Pro (i686), and described in US patent US5701442A.

These instructions were reserved to allow backwards-compatible changes in the instruction set possible, by having old processors treat them as variable-length NOPs, while having other semantics in modern processors.

Some modern uses are:
- Multi-byte/long NOPs
- Indirect Branch Tracking (ENDBR32)
- Shadow Stack (part of CET)

Some processors advertising i686 compatibility lack full support for them, which may cause #UD to be incorrectly triggered, crashing software that uses then with an unexpected SIGILL.

One such software is sudo in Debian bookworm, which is compiled with GCC -fcf-protection=branch and contains ENDBR32 instructions. It crashes on my Vortex86DX3 processor and VIA C3 Nehalem processors [1].

This patch is a much simplified version of my previous patch for x86 instruction emulation [2], that only emulates hintable NOPs.

When #UD is raised, it checks if the opcode corresponds to a hintable NOP in user space. If true, it warns the user via the dmesg and advances the instruction pointer, thus emulating its expected NOP behaviour.

Quoted 7 lines
Quoted 4 lines

[1]: https://lists.debian.org/debian-devel/2023/10/msg00118.html
[2]: https://lore.kernel.org/all/[email protected]/

This is going to be terribly slow if there's a significant number of traps (like with endbr32), but yeah, this ought to work.

Could you patch the memory resident page to contain a supported nop? (without marking it 'dirty')
Then the same function wouldn't trap until the code page was reloaded from the source file.

David

#3 lore raw
Quoted 36 lines · David Laight
Quoted 33 lines · On Wed, 20 Aug 2025 11:07:33 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 31 lines · Marcos Del Sol Vives

Hintable NOPs are a series of instructions introduced by Intel with the Pentium Pro (i686), and described in US patent US5701442A.

These instructions were reserved to allow backwards-compatible changes in the instruction set possible, by having old processors treat them as variable-length NOPs, while having other semantics in modern processors.

Some modern uses are:
- Multi-byte/long NOPs
- Indirect Branch Tracking (ENDBR32)
- Shadow Stack (part of CET)

Some processors advertising i686 compatibility lack full support for them, which may cause #UD to be incorrectly triggered, crashing software that uses then with an unexpected SIGILL.

One such software is sudo in Debian bookworm, which is compiled with GCC -fcf-protection=branch and contains ENDBR32 instructions. It crashes on my Vortex86DX3 processor and VIA C3 Nehalem processors [1].

This patch is a much simplified version of my previous patch for x86 instruction emulation [2], that only emulates hintable NOPs.

When #UD is raised, it checks if the opcode corresponds to a hintable NOP in user space. If true, it warns the user via the dmesg and advances the instruction pointer, thus emulating its expected NOP behaviour.

[1]: https://lists.debian.org/debian-devel/2023/10/msg00118.html
[2]: https://lore.kernel.org/all/[email protected]/

Quoted 7 lines
Quoted 2 lines

This is going to be terribly slow if there's a significant number of traps (like with endbr32), but yeah, this ought to work.

Could you patch the memory resident page to contain a supported nop? (without marking it 'dirty')
Then the same function wouldn't trap until the code page was reloaded from the source file.

It would mean cloning the page as private. Yes you can do it, uprobes has all the code for this. But it has non-trivial memory overhead.

#4 lore raw
Quoted 40 lines · On Thu, 21 Aug 2025 14:46:59 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 39 lines · David Laight
Quoted 35 lines · On Wed, 20 Aug 2025 11:07:33 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 23 lines · Marcos Del Sol Vives

Hintable NOPs are a series of instructions introduced by Intel with the Pentium Pro (i686), and described in US patent US5701442A.

These instructions were reserved to allow backwards-compatible changes in the instruction set possible, by having old processors treat them as variable-length NOPs, while having other semantics in modern processors.

Some modern uses are:
- Multi-byte/long NOPs
- Indirect Branch Tracking (ENDBR32)
- Shadow Stack (part of CET)

Some processors advertising i686 compatibility lack full support for them, which may cause #UD to be incorrectly triggered, crashing software that uses then with an unexpected SIGILL.

One such software is sudo in Debian bookworm, which is compiled with GCC -fcf-protection=branch and contains ENDBR32 instructions. It crashes on my Vortex86DX3 processor and VIA C3 Nehalem processors [1].

This patch is a much simplified version of my previous patch for x86 instruction emulation [2], that only emulates hintable NOPs.

Quoted 8 lines

When #UD is raised, it checks if the opcode corresponds to a hintable NOP in user space. If true, it warns the user via the dmesg and advances the instruction pointer, thus emulating its expected NOP behaviour.

[1]: https://lists.debian.org/debian-devel/2023/10/msg00118.html
[2]: https://lore.kernel.org/all/[email protected]/

This is going to be terribly slow if there's a significant number of traps (like with endbr32), but yeah, this ought to work.

Quoted 7 lines
Quoted 4 lines

Could you patch the memory resident page to contain a supported nop? (without marking it 'dirty')
Then the same function wouldn't trap until the code page was reloaded from the source file.

It would mean cloning the page as private. Yes you can do it, uprobes has all the code for this. But it has non-trivial memory overhead.

I was thinking it would be safe to do this change without cloning the page. After all the change is needed for all processes executing the code. That might only be easy on UP systems - but I doubt the affected CPU are SMP.

David

#5 lore raw
Quoted 11 lines · David Laight
Quoted 2 lines · On Thu, 21 Aug 2025 14:46:59 +0200 Peter Zijlstra <[email protected]> wrote:

It would mean cloning the page as private. Yes you can do it, uprobes has all the code for this. But it has non-trivial memory overhead.

I was thinking it would be safe to do this change without cloning the page. After all the change is needed for all processes executing the code. That might only be easy on UP systems - but I doubt the affected CPU are SMP.

David

Actually...

marcos@vdx3:~$ nproc
2
marcos@vdx3:~$ cat /proc/cpuinfo
processor       : 0
vendor_id       : Vortex86 SoC
cpu family      : 6
model           : 1
model name      : Vortex86DX3
stepping        : 1
cpu MHz         : 1000.017
physical id     : 0
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 0
initial apicid  : 0
fdiv_bug        : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 3
wp              : yes
flags           : fpu pse tsc msr cx8 apic sep pge cmov mmx fxsr sse cpuid
bugs            : itlb_multihit
bogomips        : 2000.03
clflush size    : 32
cache_alignment : 32
address sizes   : 32 bits physical, 32 bits virtual
power management:

processor : 1
vendor_id : Vortex86 SoC
cpu family : 6
model : 1
model name : Vortex86DX3
stepping : 1
cpu MHz : 1000.017
physical id : 1
siblings : 1
core id : 0
cpu cores : 1
apicid : 1
initial apicid : 1
fdiv_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 3
wp : yes
flags : fpu pse tsc msr cx8 apic sep pge cmov mmx fxsr sse cpuid
bugs : itlb_multihit
bogomips : 2000.03
clflush size : 32
cache_alignment : 32
address sizes : 32 bits physical, 32 bits virtual
power management:

Vortex SoCs are true oddballs, aren't they?

#6 lore raw
In reply to David Laight
Quoted 8 lines · David Laight
Quoted 2 lines

This is going to be terribly slow if there's a significant number of traps (like with endbr32), but yeah, this ought to work.

Could you patch the memory resident page to contain a supported nop? (without marking it 'dirty')
Then the same function wouldn't trap until the code page was reloaded from the source file.

While I had thought of that, to be honest I'm not knowledgeable enough in kernel development to pull that off without being 100% sure I did not introduce any compatibility or security issues, so I preferred for the time being to play it safe.

Anyhow, I made a simple benchmark running "sudo" with NOPASSWD as provided by Debian bookworm i686, and another version compiled without ENDBR32s, and the overhead does not seem to be too high:

# time for i in {1..100}; do ./sudo-nocet echo "" >/dev/null; done
real    0m6,001s
user    0m3,664s
sys     0m1,576s
# time for i in {1..100}; do sudo echo "" >/dev/null; done
real    0m5,983s
user    0m3,546s
sys     0m1,717s

The original binary has 203 "endbr32"s, and they result in a 0.3% slowdown for this binary in particular, which is totally acceptable IMO.

Greetings,
Marcos

#7 lore raw
In reply to Marcos Del Sol Vives

Hi Marcos,

On Wed, 20 Aug 2025, Marcos Del Sol Vives wrote:
...

Quoted 11 lines
arch/x86/include/asm/processor.h +4 -0
@@ -499,6 +499,10 @@ struct thread_struct {
 
 	unsigned int		iopl_warn:1;
 
+#ifdef CONFIG_X86_HNOP_EMU
+	unsigned int		hnop_warn:1;
+#endif
+

...

Quoted 9 lines
arch/x86/kernel/process.c +3 -0
@@ -178,6 +178,9 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 	p->thread.io_bitmap = NULL;
 	clear_tsk_thread_flag(p, TIF_IO_BITMAP);
 	p->thread.iopl_warn = 0;
+#ifdef CONFIG_X86_HNOP_EMU
+	p->thread.hnop_warn = 0;
+#endif

...

arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_overflow) · line 322

Quoted 8 lines
arch/x86/kernel/traps.c +6 -0
+
+	if (!t->hnop_warn) {
+		pr_warn_ratelimited("%s[%d] emulating hintable NOP, ip:%lx\n",
+		       current->comm, task_pid_nr(current), regs->ip);
+		t->hnop_warn = 1;
+	}

Can we please remove all this 'hnop_warn' trickery? Removing it will simplifiy the code and avoid complicating 'thread_struct' further.

It's just the kernel doing its normal job.

And if the system is full of binaries with hintable NOPs, ratelimiting will not save you much. I got hit recently by a 'ratelimited' correctible error PCI subsystem warning, and it still overflows the log buffers of my Thinkpad laptop, in just 4 to 5 days :(

arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_overflow) · line 339

Quoted 9 lines

static inline void handle_invalid_op(struct pt_regs *regs) {

arch/x86/kernel/traps.c +5 -0
+#ifdef CONFIG_X86_HNOP_EMU
+	if (user_mode(regs) && handle_hnop(regs))
+		return;
+#endif
+

CPP conditionals within C function code are ugly. Please do instead:

    static bool handle_hnop(struct pt_regs *regs)
    {
	if (!IS_ENABLED(CONFIG_X86_HNOP_EMU))
		return false;
	...
    }

Thanks for your contribution!

--
Ahmed S. Darwish
Linutronix GmbH

#8 lore raw

Hi Ahmed,

Quoted 9 lines · Ahmed S. Darwish

Can we please remove all this 'hnop_warn' trickery? Removing it will simplifiy the code and avoid complicating 'thread_struct' further.

It's just the kernel doing its normal job.

And if the system is full of binaries with hintable NOPs, ratelimiting will not save you much. I got hit recently by a 'ratelimited' correctible error PCI subsystem warning, and it still overflows the log buffers of my Thinkpad laptop, in just 4 to 5 days :(

But I think the kernel should let the user know the binaries they're running are having some performance penalty due to this emulation, in case they want to recompile without the offending flags.

Without the logging, they'd be in the dark and might get confused on why their programs are running slower than on other machines.

Quoted 12 lines

arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_overflow) · line 339

Quoted 9 lines

static inline void handle_invalid_op(struct pt_regs *regs) {

arch/x86/kernel/traps.c +5 -0
+#ifdef CONFIG_X86_HNOP_EMU
+	if (user_mode(regs) && handle_hnop(regs))
+		return;
+#endif
+

CPP conditionals within C function code are ugly. Please do instead:

Quoted 8 lines
    static bool handle_hnop(struct pt_regs *regs)
    {
	if (!IS_ENABLED(CONFIG_X86_HNOP_EMU))
		return false;
	...
    }

Thanks for your contribution!

I originally did that, but then realized it was not possible due to "handle_hnop" depending on the conditionally-available "hnop_warn" flag.

Greetings,
Marcos

maintainer
#9 lore raw
Quoted 3 lines · Marcos Del Sol Vives

But I think the kernel should let the user know the binaries they're running are having some performance penalty due to this emulation, in case they want to recompile without the offending flags.

Sure, once perhaps.

Do you want to let the user know for each binary?

And how many users do you really think will look at dmesg and recompile their binaries?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#10 lore raw
Quoted 11 lines · Borislav Petkov
Quoted 3 lines · Marcos Del Sol Vives

But I think the kernel should let the user know the binaries they're running are having some performance penalty due to this emulation, in case they want to recompile without the offending flags.

Sure, once perhaps.

Do you want to let the user know for each binary?

And how many users do you really think will look at dmesg and recompile their binaries?

I mean, they should know what they need to recompile if they want to, not just that their machine is having a bug triggered by some binary.

A global flag would mean they'd need to reboot to see if there is any other binary triggering it too.

Whether they look or not at the dmesg I cannot tell, but if IOPL emulation does logging this way, I assumed this should too. Otherwise, would that mean IOPL emulation logging is also too verbose?

maintainer
#11 lore raw
Quoted 2 lines · Marcos Del Sol Vives

I mean, they should know what they need to recompile if they want to, not just that their machine is having a bug triggered by some binary.

And what's stopping you from writing a proper error message explaining that?

And issuing that error message *exactly once* instead of flooding dmesg for no good reason?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#12 lore raw
Quoted 8 lines · Borislav Petkov
Quoted 2 lines · Marcos Del Sol Vives

I mean, they should know what they need to recompile if they want to, not just that their machine is having a bug triggered by some binary.

And what's stopping you from writing a proper error message explaining that?

And issuing that error message *exactly once* instead of flooding dmesg for no good reason?

Please define "once". Once per what? Per boot? Per executable? Per process?

Once per boot would mean they'd need to reboot to see if any other executables are affected. Per executable AFAIK there are no facilities to do that, and the closest is per process which is what it's currently being done (again, like IOPL emulation which was already deemed okay a couple years ago and merged into the kernel)

maintainer
#13 lore raw
Quoted 1 line · Marcos Del Sol Vives

Please define "once". Once per what? Per boot? Per executable? Per process?

pr_err_once(). Per boot.

Quoted 2 lines

Once per boot would mean they'd need to reboot to see if any other executables are affected.

We'll cross that bridge when we get to it.

Quoted 2 lines

(again, like IOPL emulation which was already deemed okay a couple years ago and merged into the kernel)

When it is flooding dmesg for no good reason, I wouldn't mind toning that down too.

IOW, I wanna see a real, actual use case which justifies flooding dmesg. If you don't have one, then we'll do it only when it is really warranted.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#14 lore raw
Quoted 4 lines · Borislav Petkov
Quoted 1 line · Marcos Del Sol Vives

Please define "once". Once per what? Per boot? Per executable? Per process?

pr_err_once(). Per boot.

Would a simple:

Quoted 2 lines

pr_warn_once("Your processor does not correctly handle hintable NOPs.\n"); pr_warn_once("The kernel will emulate them, but the performance will be impacted!\n");

work for you, then? With no thread information, as that might make the user think there is only once binary impacted.

maintainer
#15 lore raw
Quoted 7 lines · Marcos Del Sol Vives

Would a simple:

Quoted 2 lines

pr_warn_once("Your processor does not correctly handle hintable NOPs.\n"); pr_warn_once("The kernel will emulate them, but the performance will be impacted!\n");

work for you, then? With no thread information, as that might make the user think there is only once binary impacted.

I don't mind if you make the warning message as helpful as possible and even dump current->comm and whatever else is needed to help the user address the issue.

What I mind is flooding dmesg unnecessarily with the same stanzas over and over again.

If the user can do something about it, then she should be able to find out also which executables need to be recompiled.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#16 lore raw
In reply to Marcos Del Sol Vives
Quoted 10 lines · Marcos Del Sol Vives
Quoted 8 lines · Borislav Petkov
Quoted 2 lines · Marcos Del Sol Vives

I mean, they should know what they need to recompile if they want to, not just that their machine is having a bug triggered by some binary.

And what's stopping you from writing a proper error message explaining that?

And issuing that error message *exactly once* instead of flooding dmesg for no good reason?

Quoted 7 lines

Please define "once". Once per what? Per boot? Per executable? Per process?

Once per boot would mean they'd need to reboot to see if any other executables are affected. Per executable AFAIK there are no facilities to do that, and the closest is per process which is what it's currently being done (again, like IOPL emulation which was already deemed okay a couple years ago and merged into the kernel)

If they want to see them again, just:

echo 1 >/sys/kernel/debug/clear_warn_once

Signature · 1 line
Kees Cook
#17 lore raw
In reply to Marcos Del Sol Vives
Quoted 8 lines · Marcos Del Sol Vives

But I think the kernel should let the user know the binaries they're running are having some performance penalty due to this emulation, in case they want to recompile without the offending flags.

Without the logging, they'd be in the dark and might get confused on why their programs are running slower than on other machines.

Not convinced; especially all the extra 'thread_struct' noise.

Quoted 4 lines

I originally did that, but then realized it was not possible due to "handle_hnop" depending on the conditionally-available "hnop_warn" flag.

Please do:

    #ifdef CONFIG_X86_HNOP_EMU
    static bool handle_hnop(struct pt_regs *regs)
    {
	// Reference 'hnop_warn' as much as you like
    }
    #else
    static bool handle_hnop(struct pt_regs *regs)
    {
	return false;
    }
    # endif

Then this ugliness:

    static inline void handle_invalid_op(struct pt_regs *regs)
    {
    #ifdef CONFIG_X86_HNOP_EMU
     	if (user_mode(regs) && handle_hnop(regs))
     		return;
    #endif
	...
    }

can become normal code:

    static inline void handle_invalid_op(struct pt_regs *regs)
    {
     	if (user_mode(regs) && handle_hnop(regs))
     		return;
	...
    }

Good luck,

--
Ahmed S. Darwish
Linutronix GmbH

#18 lore raw
Quoted 15 lines · Ahmed S. Darwish

Please do:

    #ifdef CONFIG_X86_HNOP_EMU
    static bool handle_hnop(struct pt_regs *regs)
    {
	// Reference 'hnop_warn' as much as you like
    }
    #else
    static bool handle_hnop(struct pt_regs *regs)
    {
	return false;
    }
    # endif

And as previously suggested: remove the ugly hnop_warn stuff from 'thread_struct', then you can even just do:

    static bool handle_hnop(struct pt_regs *regs)
    {
	if (!IS_ENABLED(CONFIG_X86_HNOP_EMU))
		return false;

	pr_warn_once("%s[%d] Emulating hintable NOP\n"
		     "This warning will not be repeated; even for other binaries\n"
		     current->comm, task_pid_nr(current));

	...
    }

And everything else will fit quietly in place.

--
Ahmed S. Darwish
Linutronix GmbH

#19 lore raw
In reply to Marcos Del Sol Vives
Quoted 150 lines · Marcos Del Sol Vives

Hintable NOPs are a series of instructions introduced by Intel with the Pentium Pro (i686), and described in US patent US5701442A.

These instructions were reserved to allow backwards-compatible changes in the instruction set possible, by having old processors treat them as variable-length NOPs, while having other semantics in modern processors.

Some modern uses are:
- Multi-byte/long NOPs
- Indirect Branch Tracking (ENDBR32)
- Shadow Stack (part of CET)

Some processors advertising i686 compatibility lack full support for them, which may cause #UD to be incorrectly triggered, crashing software that uses then with an unexpected SIGILL.

One such software is sudo in Debian bookworm, which is compiled with GCC -fcf-protection=branch and contains ENDBR32 instructions. It crashes on my Vortex86DX3 processor and VIA C3 Nehalem processors [1].

This patch is a much simplified version of my previous patch for x86 instruction emulation [2], that only emulates hintable NOPs.

When #UD is raised, it checks if the opcode corresponds to a hintable NOP in user space. If true, it warns the user via the dmesg and advances the instruction pointer, thus emulating its expected NOP behaviour.

[1]: https://lists.debian.org/debian-devel/2023/10/msg00118.html
[2]: https://lore.kernel.org/all/[email protected]/

---

arch/x86/Kconfig                 | 29 +++++++++++++++++++++++++
arch/x86/include/asm/processor.h |  4 ++++
arch/x86/kernel/process.c        |  3 +++
arch/x86/kernel/traps.c          | 36 ++++++++++++++++++++++++++++++++
4 files changed, 72 insertions(+)
arch/x86/Kconfig +29 -0
index 58d890fe2100..a6daebdc2573 100644
@@ -1286,6 +1286,35 @@ config X86_IOPL_IOPERM
 	  ability to disable interrupts from user space which would be
 	  granted if the hardware IOPL mechanism would be used.
 
+config X86_HNOP_EMU
+	bool "Hintable NOPs emulation"
+	depends on X86_32
+	default y
+	help
+	  Hintable NOPs are a series of instructions introduced by Intel with
+	  the Pentium Pro (i686), and described in US patent US5701442A.
+
+	  These instructions were reserved to allow backwards-compatible
+	  changes in the instruction set possible, by having old processors
+	  treat them as variable-length NOPs, while having other semantics in
+	  modern processors.
+
+	  Some modern uses are:
+	   - Multi-byte/long NOPs
+	   - Indirect Branch Tracking (ENDBR32)
+	   - Shadow Stack (part of CET)
+
+	  Some processors advertising i686 compatibility (such as Cyrix MII,
+	  VIA C3 Nehalem or DM&P Vortex86DX3) lack full support for them,
+	  which may cause SIGILL to be incorrectly raised in user space when
+	  a hintable NOP is encountered.
+
+	  Say Y here if you want the kernel to emulate them, allowing programs
+	  that make use of them to run transparently on such processors.
+
+	  This emulation has no performance penalty for processors that
+	  properly support them, so if unsure, enable it.
+

config TOSHIBA

	tristate "Toshiba Laptop support"
	depends on X86_32
arch/x86/include/asm/processor.h +4 -0
index bde58f6510ac..c34fb678c4de 100644
@@ -499,6 +499,10 @@ struct thread_struct {
 
 	unsigned int		iopl_warn:1;
 
+#ifdef CONFIG_X86_HNOP_EMU
+	unsigned int		hnop_warn:1;
+#endif
+
 	/*
 	 * Protection Keys Register for Userspace.  Loaded immediately on
 	 * context switch. Store it in thread_struct to avoid a lookup in
arch/x86/kernel/process.c +3 -0
index 1b7960cf6eb0..6ec8021638d0 100644
@@ -178,6 +178,9 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 	p->thread.io_bitmap = NULL;
 	clear_tsk_thread_flag(p, TIF_IO_BITMAP);
 	p->thread.iopl_warn = 0;
+#ifdef CONFIG_X86_HNOP_EMU
+	p->thread.hnop_warn = 0;
+#endif
 	memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
 
 #ifdef CONFIG_X86_64
arch/x86/kernel/traps.c +31 -0
index 36354b470590..2dcb7d7edf8a 100644
@@ -295,12 +295,48 @@ DEFINE_IDTENTRY(exc_overflow)
 	do_error_trap(regs, 0, "overflow", X86_TRAP_OF, SIGSEGV, 0, NULL);
 }
 
+#ifdef CONFIG_X86_HNOP_EMU
+static bool handle_hnop(struct pt_regs *regs)
+{
+	struct thread_struct *t = &current->thread;
+	unsigned char buf[MAX_INSN_SIZE];
+	unsigned long nr_copied;
+	struct insn insn;
+
+	nr_copied = insn_fetch_from_user(regs, buf);
+	if (nr_copied <= 0)
+		return false;
+
+	if (!insn_decode_from_regs(&insn, regs, buf, nr_copied))
+		return false;
+
+	/* Hintable NOPs cover 0F 18 to 0F 1F */
+	if (insn.opcode.bytes[0] != 0x0F ||
+		insn.opcode.bytes[1] < 0x18 || insn.opcode.bytes[1] > 0x1F)
+		return false;
+
+	if (!t->hnop_warn) {
+		pr_warn_ratelimited("%s[%d] emulating hintable NOP, ip:%lx\n",
+		       current->comm, task_pid_nr(current), regs->ip);
+		t->hnop_warn = 1;
+	}
+
+	regs->ip += insn.length;
+	return true;
+}
+#endif
+
 #ifdef CONFIG_X86_F00F_BUG
 void handle_invalid_op(struct pt_regs *regs)
 #else
 static inline void handle_invalid_op(struct pt_regs *regs)

arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_overflow) · line 342

Quoted 10 lines
arch/x86/kernel/traps.c +5 -0
 #endif
 {
+#ifdef CONFIG_X86_HNOP_EMU
+	if (user_mode(regs) && handle_hnop(regs))
+		return;
+#endif
+
 	do_error_trap(regs, 0, "invalid opcode", X86_TRAP_UD, SIGILL,
 		      ILL_ILLOPN, error_get_trap_addr(regs));
 }

Do those processors support FCOMI?

#20 lore raw
Quoted 5 lines · H. Peter Anvin
Quoted 4 lines · Marcos Del Sol Vives

One such software is sudo in Debian bookworm, which is compiled with GCC -fcf-protection=branch and contains ENDBR32 instructions. It crashes on my Vortex86DX3 processor and VIA C3 Nehalem processors [1].

Quoted 7 lines
Quoted 5 lines

This patch is a much simplified version of my previous patch for x86 instruction emulation [2], that only emulates hintable NOPs.

[1]: https://lists.debian.org/debian-devel/2023/10/msg00118.html
[2]: https://lore.kernel.org/all/[email protected]/

Do those processors support FCOMI?

The Vortex86DX3, at the very least, does. Unlike the Vortex86MX I wrote the previous patch for, this one seems to support all standard i686 instructions. It's modern enough to support SSE1, even.

I have tested it using the program I wrote for testing it on the -MX last time: https://lore.kernel.org/all/[email protected]/.

Quoted 4 lines
marcos@vdx3:~$ LANG=C gcc -Wall fucomi-test.c -o fucomi-test
marcos@vdx3:~$ ./fucomi-test
Float value: 6.210000
FPU status: 3800 EFLAGS: 00000242

Which matches 1:1 my current desktop Ryzen 8645H.

Greetings,
Marcos

#21 lore raw
In reply to Marcos Del Sol Vives
Quoted 150 lines · Marcos Del Sol Vives

Hintable NOPs are a series of instructions introduced by Intel with the Pentium Pro (i686), and described in US patent US5701442A.

These instructions were reserved to allow backwards-compatible changes in the instruction set possible, by having old processors treat them as variable-length NOPs, while having other semantics in modern processors.

Some modern uses are:
- Multi-byte/long NOPs
- Indirect Branch Tracking (ENDBR32)
- Shadow Stack (part of CET)

Some processors advertising i686 compatibility lack full support for them, which may cause #UD to be incorrectly triggered, crashing software that uses then with an unexpected SIGILL.

One such software is sudo in Debian bookworm, which is compiled with GCC -fcf-protection=branch and contains ENDBR32 instructions. It crashes on my Vortex86DX3 processor and VIA C3 Nehalem processors [1].

This patch is a much simplified version of my previous patch for x86 instruction emulation [2], that only emulates hintable NOPs.

When #UD is raised, it checks if the opcode corresponds to a hintable NOP in user space. If true, it warns the user via the dmesg and advances the instruction pointer, thus emulating its expected NOP behaviour.

[1]: https://lists.debian.org/debian-devel/2023/10/msg00118.html
[2]: https://lore.kernel.org/all/[email protected]/

---

arch/x86/Kconfig                 | 29 +++++++++++++++++++++++++
arch/x86/include/asm/processor.h |  4 ++++
arch/x86/kernel/process.c        |  3 +++
arch/x86/kernel/traps.c          | 36 ++++++++++++++++++++++++++++++++
4 files changed, 72 insertions(+)
arch/x86/Kconfig +29 -0
index 58d890fe2100..a6daebdc2573 100644
@@ -1286,6 +1286,35 @@ config X86_IOPL_IOPERM
 	  ability to disable interrupts from user space which would be
 	  granted if the hardware IOPL mechanism would be used.
 
+config X86_HNOP_EMU
+	bool "Hintable NOPs emulation"
+	depends on X86_32
+	default y
+	help
+	  Hintable NOPs are a series of instructions introduced by Intel with
+	  the Pentium Pro (i686), and described in US patent US5701442A.
+
+	  These instructions were reserved to allow backwards-compatible
+	  changes in the instruction set possible, by having old processors
+	  treat them as variable-length NOPs, while having other semantics in
+	  modern processors.
+
+	  Some modern uses are:
+	   - Multi-byte/long NOPs
+	   - Indirect Branch Tracking (ENDBR32)
+	   - Shadow Stack (part of CET)
+
+	  Some processors advertising i686 compatibility (such as Cyrix MII,
+	  VIA C3 Nehalem or DM&P Vortex86DX3) lack full support for them,
+	  which may cause SIGILL to be incorrectly raised in user space when
+	  a hintable NOP is encountered.
+
+	  Say Y here if you want the kernel to emulate them, allowing programs
+	  that make use of them to run transparently on such processors.
+
+	  This emulation has no performance penalty for processors that
+	  properly support them, so if unsure, enable it.
+

config TOSHIBA

	tristate "Toshiba Laptop support"
	depends on X86_32
arch/x86/include/asm/processor.h +4 -0
index bde58f6510ac..c34fb678c4de 100644
@@ -499,6 +499,10 @@ struct thread_struct {
 
 	unsigned int		iopl_warn:1;
 
+#ifdef CONFIG_X86_HNOP_EMU
+	unsigned int		hnop_warn:1;
+#endif
+
 	/*
 	 * Protection Keys Register for Userspace.  Loaded immediately on
 	 * context switch. Store it in thread_struct to avoid a lookup in
arch/x86/kernel/process.c +3 -0
index 1b7960cf6eb0..6ec8021638d0 100644
@@ -178,6 +178,9 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 	p->thread.io_bitmap = NULL;
 	clear_tsk_thread_flag(p, TIF_IO_BITMAP);
 	p->thread.iopl_warn = 0;
+#ifdef CONFIG_X86_HNOP_EMU
+	p->thread.hnop_warn = 0;
+#endif
 	memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
 
 #ifdef CONFIG_X86_64
arch/x86/kernel/traps.c +31 -0
index 36354b470590..2dcb7d7edf8a 100644
@@ -295,12 +295,48 @@ DEFINE_IDTENTRY(exc_overflow)
 	do_error_trap(regs, 0, "overflow", X86_TRAP_OF, SIGSEGV, 0, NULL);
 }
 
+#ifdef CONFIG_X86_HNOP_EMU
+static bool handle_hnop(struct pt_regs *regs)
+{
+	struct thread_struct *t = &current->thread;
+	unsigned char buf[MAX_INSN_SIZE];
+	unsigned long nr_copied;
+	struct insn insn;
+
+	nr_copied = insn_fetch_from_user(regs, buf);
+	if (nr_copied <= 0)
+		return false;
+
+	if (!insn_decode_from_regs(&insn, regs, buf, nr_copied))
+		return false;
+
+	/* Hintable NOPs cover 0F 18 to 0F 1F */
+	if (insn.opcode.bytes[0] != 0x0F ||
+		insn.opcode.bytes[1] < 0x18 || insn.opcode.bytes[1] > 0x1F)
+		return false;
+
+	if (!t->hnop_warn) {
+		pr_warn_ratelimited("%s[%d] emulating hintable NOP, ip:%lx\n",
+		       current->comm, task_pid_nr(current), regs->ip);
+		t->hnop_warn = 1;
+	}
+
+	regs->ip += insn.length;
+	return true;
+}
+#endif
+
 #ifdef CONFIG_X86_F00F_BUG
 void handle_invalid_op(struct pt_regs *regs)
 #else
 static inline void handle_invalid_op(struct pt_regs *regs)

arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_overflow) · line 342

Quoted 10 lines
arch/x86/kernel/traps.c +5 -0
 #endif
 {
+#ifdef CONFIG_X86_HNOP_EMU
+	if (user_mode(regs) && handle_hnop(regs))
+		return;
+#endif
+
 	do_error_trap(regs, 0, "invalid opcode", X86_TRAP_UD, SIGILL,
 		      ILL_ILLOPN, error_get_trap_addr(regs));
 }

Why are they using -fcf-protection=branch on 32 buts when the kernel vdso doesn't even (currently) support it?

#22 lore raw
In reply to Marcos Del Sol Vives
Quoted 124 lines · On Wed, 20 Aug 2025 03:34:46 +0200 Marcos Del Sol Vives <[email protected]> wrote:

Hintable NOPs are a series of instructions introduced by Intel with the Pentium Pro (i686), and described in US patent US5701442A.

These instructions were reserved to allow backwards-compatible changes in the instruction set possible, by having old processors treat them as variable-length NOPs, while having other semantics in modern processors.

Some modern uses are:
- Multi-byte/long NOPs
- Indirect Branch Tracking (ENDBR32)
- Shadow Stack (part of CET)

Some processors advertising i686 compatibility lack full support for them, which may cause #UD to be incorrectly triggered, crashing software that uses then with an unexpected SIGILL.

One such software is sudo in Debian bookworm, which is compiled with GCC -fcf-protection=branch and contains ENDBR32 instructions. It crashes on my Vortex86DX3 processor and VIA C3 Nehalem processors [1].

This patch is a much simplified version of my previous patch for x86 instruction emulation [2], that only emulates hintable NOPs.

When #UD is raised, it checks if the opcode corresponds to a hintable NOP in user space. If true, it warns the user via the dmesg and advances the instruction pointer, thus emulating its expected NOP behaviour.

[1]: https://lists.debian.org/debian-devel/2023/10/msg00118.html
[2]: https://lore.kernel.org/all/[email protected]/

---
 arch/x86/Kconfig                 | 29 +++++++++++++++++++++++++
 arch/x86/include/asm/processor.h |  4 ++++
 arch/x86/kernel/process.c        |  3 +++
 arch/x86/kernel/traps.c          | 36 ++++++++++++++++++++++++++++++++
 4 files changed, 72 insertions(+)
arch/x86/Kconfig +29 -0
index 58d890fe2100..a6daebdc2573 100644
@@ -1286,6 +1286,35 @@ config X86_IOPL_IOPERM
 	  ability to disable interrupts from user space which would be
 	  granted if the hardware IOPL mechanism would be used.
 
+config X86_HNOP_EMU
+	bool "Hintable NOPs emulation"
+	depends on X86_32
+	default y
+	help
+	  Hintable NOPs are a series of instructions introduced by Intel with
+	  the Pentium Pro (i686), and described in US patent US5701442A.
+
+	  These instructions were reserved to allow backwards-compatible
+	  changes in the instruction set possible, by having old processors
+	  treat them as variable-length NOPs, while having other semantics in
+	  modern processors.
+
+	  Some modern uses are:
+	   - Multi-byte/long NOPs
+	   - Indirect Branch Tracking (ENDBR32)
+	   - Shadow Stack (part of CET)
+
+	  Some processors advertising i686 compatibility (such as Cyrix MII,
+	  VIA C3 Nehalem or DM&P Vortex86DX3) lack full support for them,
+	  which may cause SIGILL to be incorrectly raised in user space when
+	  a hintable NOP is encountered.
+
+	  Say Y here if you want the kernel to emulate them, allowing programs
+	  that make use of them to run transparently on such processors.
+
+	  This emulation has no performance penalty for processors that
+	  properly support them, so if unsure, enable it.
+
 config TOSHIBA
 	tristate "Toshiba Laptop support"
 	depends on X86_32
arch/x86/include/asm/processor.h +4 -0
index bde58f6510ac..c34fb678c4de 100644
@@ -499,6 +499,10 @@ struct thread_struct {
 
 	unsigned int		iopl_warn:1;
 
+#ifdef CONFIG_X86_HNOP_EMU
+	unsigned int		hnop_warn:1;
+#endif
+
 	/*
 	 * Protection Keys Register for Userspace.  Loaded immediately on
 	 * context switch. Store it in thread_struct to avoid a lookup in
arch/x86/kernel/process.c +3 -0
index 1b7960cf6eb0..6ec8021638d0 100644
@@ -178,6 +178,9 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 	p->thread.io_bitmap = NULL;
 	clear_tsk_thread_flag(p, TIF_IO_BITMAP);
 	p->thread.iopl_warn = 0;
+#ifdef CONFIG_X86_HNOP_EMU
+	p->thread.hnop_warn = 0;
+#endif
 	memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
 
 #ifdef CONFIG_X86_64
arch/x86/kernel/traps.c +9 -0
index 36354b470590..2dcb7d7edf8a 100644
@@ -295,12 +295,48 @@ DEFINE_IDTENTRY(exc_overflow)
 	do_error_trap(regs, 0, "overflow", X86_TRAP_OF, SIGSEGV, 0, NULL);
 }
 
+#ifdef CONFIG_X86_HNOP_EMU
+static bool handle_hnop(struct pt_regs *regs)
+{
+	struct thread_struct *t = &current->thread;
+	unsigned char buf[MAX_INSN_SIZE];
+	unsigned long nr_copied;
+	struct insn insn;
+
+	nr_copied = insn_fetch_from_user(regs, buf);

arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_overflow) · line 316

Quoted 10 lines
arch/x86/kernel/traps.c +10 -0
+	if (nr_copied <= 0)
+		return false;
+
+	if (!insn_decode_from_regs(&insn, regs, buf, nr_copied))
+		return false;
+
+	/* Hintable NOPs cover 0F 18 to 0F 1F */
+	if (insn.opcode.bytes[0] != 0x0F ||
+		insn.opcode.bytes[1] < 0x18 || insn.opcode.bytes[1] > 0x1F)
+		return false;

Can you swap the order of those tests?
Looks like the 'decode' is only needed for the length.

Quoted 11 lines
arch/x86/kernel/traps.c +11 -0
+
+	if (!t->hnop_warn) {
+		pr_warn_ratelimited("%s[%d] emulating hintable NOP, ip:%lx\n",
+		       current->comm, task_pid_nr(current), regs->ip);
+		t->hnop_warn = 1;
+	}
+
+	regs->ip += insn.length;
+	return true;
+}
+#endif

arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_overflow) · line 337

Quoted 10 lines
arch/x86/kernel/traps.c +4 -0
+
 #ifdef CONFIG_X86_F00F_BUG
 void handle_invalid_op(struct pt_regs *regs)
 #else
 static inline void handle_invalid_op(struct pt_regs *regs)
 #endif
 {
+#ifdef CONFIG_X86_HNOP_EMU
+	if (user_mode(regs) && handle_hnop(regs))
+		return;

Why not move the user_mode() test into handle_hnop() ? Should make the config tests easier.

David

Quoted 5 lines
arch/x86/kernel/traps.c +2 -0
+#endif
+
 	do_error_trap(regs, 0, "invalid opcode", X86_TRAP_UD, SIGILL,
 		      ILL_ILLOPN, error_get_trap_addr(regs));
 }
#23 lore raw
Quoted 12 lines · David Laight

arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_overflow) · line 316

Quoted 7 lines · Marcos Del Sol Vives
arch/x86/kernel/traps.c +7 -0
+	if (!insn_decode_from_regs(&insn, regs, buf, nr_copied))
+		return false;
+
+	/* Hintable NOPs cover 0F 18 to 0F 1F */
+	if (insn.opcode.bytes[0] != 0x0F ||
+		insn.opcode.bytes[1] < 0x18 || insn.opcode.bytes[1] > 0x1F)
+		return false;

Can you swap the order of those tests?
Looks like the 'decode' is only needed for the length.

Not really. The opcodes may have prefixes that are "removed" by the decode function. ENDBR32 for instance is actually "F3 0F 1E FB", with a REP prefix.

Quoted 7 lines

arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_overflow) · line 337

Quoted 3 lines
arch/x86/kernel/traps.c +3 -0
+#ifdef CONFIG_X86_HNOP_EMU
+	if (user_mode(regs) && handle_hnop(regs))
+		return;

Why not move the user_mode() test into handle_hnop() ? Should make the config tests easier.

Other code I saw did this in the calling function itself (eg handle_bug) so I did it here too.

#24 lore raw
In reply to Marcos Del Sol Vives
Quoted 8 lines · Marcos Del Sol Vives
arch/x86/kernel/traps.c +8 -0
+static bool handle_hnop(struct pt_regs *regs)
+{
+	struct thread_struct *t = &current->thread;
+	unsigned char buf[MAX_INSN_SIZE];
+	unsigned long nr_copied;
+	struct insn insn;
+
+	nr_copied = insn_fetch_from_user(regs, buf);

arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_overflow) · line 316

Quoted 10 lines
arch/x86/kernel/traps.c +10 -0
+	if (nr_copied <= 0)
+		return false;
+
+	if (!insn_decode_from_regs(&insn, regs, buf, nr_copied))
+		return false;
+
+	/* Hintable NOPs cover 0F 18 to 0F 1F */
+	if (insn.opcode.bytes[0] != 0x0F ||
+		insn.opcode.bytes[1] < 0x18 || insn.opcode.bytes[1] > 0x1F)
+		return false;

FWIW, you need to check for insn.opcode.nbytes == 2.

Quoted 9 lines
arch/x86/kernel/traps.c +9 -0
+	if (!t->hnop_warn) {
+		pr_warn_ratelimited("%s[%d] emulating hintable NOP, ip:%lx\n",
+		       current->comm, task_pid_nr(current), regs->ip);
+		t->hnop_warn = 1;
+	}
+
+	regs->ip += insn.length;
+	return true;
+}
#25 lore raw
Quoted 8 lines · Peter Zijlstra

arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_overflow) · line 316

Quoted 4 lines · Marcos Del Sol Vives
arch/x86/kernel/traps.c +4 -0
+	/* Hintable NOPs cover 0F 18 to 0F 1F */
+	if (insn.opcode.bytes[0] != 0x0F ||
+		insn.opcode.bytes[1] < 0x18 || insn.opcode.bytes[1] > 0x1F)
+		return false;

FWIW, you need to check for insn.opcode.nbytes == 2.

I can add it no problem for clarity, but would it be really necessary?

All opcodes in that range will have that length by the Intel SDM, so it seems somewhat redundant, and if the opcode couldn't be read in full the decode would've failed earlier.

insn_decode_mmio for example which I used as an example of software parsing of instructions does not check any length if the prefix was 0x0f.

#26 lore raw
Quoted 10 lines · Marcos Del Sol Vives
Quoted 8 lines · Peter Zijlstra

arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_overflow) · line 316

Quoted 4 lines · Marcos Del Sol Vives
arch/x86/kernel/traps.c +4 -0
+	/* Hintable NOPs cover 0F 18 to 0F 1F */
+	if (insn.opcode.bytes[0] != 0x0F ||
+		insn.opcode.bytes[1] < 0x18 || insn.opcode.bytes[1] > 0x1F)
+		return false;

FWIW, you need to check for insn.opcode.nbytes == 2.

Quoted 8 lines

I can add it no problem for clarity, but would it be really necessary?

All opcodes in that range will have that length by the Intel SDM, so it seems somewhat redundant, and if the opcode couldn't be read in full the decode would've failed earlier.

insn_decode_mmio for example which I used as an example of software parsing of instructions does not check any length if the prefix was 0x0f.

Yeah, I suppose you're right.

#27 lore raw
In reply to Peter Zijlstra
Quoted 22 lines · Peter Zijlstra
Quoted 8 lines · Marcos Del Sol Vives
arch/x86/kernel/traps.c +8 -0
+static bool handle_hnop(struct pt_regs *regs)
+{
+	struct thread_struct *t = &current->thread;
+	unsigned char buf[MAX_INSN_SIZE];
+	unsigned long nr_copied;
+	struct insn insn;
+
+	nr_copied = insn_fetch_from_user(regs, buf);

arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_overflow) · line 316

Quoted 10 lines
arch/x86/kernel/traps.c +10 -0
+	if (nr_copied <= 0)
+		return false;
+
+	if (!insn_decode_from_regs(&insn, regs, buf, nr_copied))
+		return false;
+
+	/* Hintable NOPs cover 0F 18 to 0F 1F */
+	if (insn.opcode.bytes[0] != 0x0F ||
+		insn.opcode.bytes[1] < 0x18 || insn.opcode.bytes[1] > 0x1F)
+		return false;

FWIW, you need to check for insn.opcode.nbytes == 2.

Quoted 9 lines

arch/x86/kernel/traps.c DEFINE_IDTENTRY(exc_overflow) · line 326

Quoted 9 lines
arch/x86/kernel/traps.c +9 -0
+	if (!t->hnop_warn) {
+		pr_warn_ratelimited("%s[%d] emulating hintable NOP, ip:%lx\n",
+		       current->comm, task_pid_nr(current), regs->ip);
+		t->hnop_warn = 1;
+	}
+
+	regs->ip += insn.length;
+	return true;
+}

No, hintable noops apply to any modr/m.