Nopl: Difference between revisions

From JookWiki
(More writing wow)
(Finish glibc fallout)
Line 52: Line 52:


== Linux fallout ==
== Linux fallout ==
In 2006 [https://sourceware.org/pipermail/binutils/2006-June/047692.html PATCH: Add "nop memory" for i386/x86-64] was committed to the GNU Assembler. It added support for the 'nopl' and 'nopw' assembly instructions that map to multi-byte NOP code.
In 2006 [https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff;h=1596541188b1a4080ab7bce6578c09626193dfd0 PATCH: Add "nop memory" for i386/x86-64] was committed to the GNU Assembler. It added support for the 'nopl' and 'nopw' assembly instructions that map to multi-byte NOP code.


In 2007 [http://lkml.iu.edu/hypermail/linux/kernel/0709.2/2726.html x86: multi-byte single instruction NOPs] was committed to Linux. This added a set of 'P6 NOPs' that used the multi-byte NOP opcodes and used them for i686 or newer x86 CPUs. Which type of NOPs to use were decided at runtime, so running an i686 kernel on an i586 machine would not cause any issues with this. Strangely on 64-bit systems the NOPs were only used if your CPU vendor was Intel.  
In 2007 [http://lkml.iu.edu/hypermail/linux/kernel/0709.2/2726.html x86: multi-byte single instruction NOPs] was committed to Linux. This added a set of 'P6 NOPs' that used the multi-byte NOP opcodes and used them for i686 or newer x86 CPUs. Which type of NOPs to use were decided at runtime, so running an i686 kernel on an i586 machine would not cause any issues with this. Strangely on 64-bit systems the NOPs were only used if your CPU vendor was Intel.  
Line 119: Line 119:


== glibc fallout ==
== glibc fallout ==
In 2010 [https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=01f1f5ee Pass -mtune=i686 to assembler when compiling for i686] was committed to glibc. This told GNU Assembler to not just generate code that worked on i686, but to tune for the i686 architecture. This
In 2010 [https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=01f1f5ee Pass -mtune=i686 to assembler when compiling for i686] was committed to glibc. This told GNU Assembler to optimize for i686 CPUs (Pentium Pro), and as I mentioned in the previous section, this used multi-byte NOPs.  


In 2010 the Arch Linux bug [https://bugs.archlinux.org/task/19733 Update to glibc 2.12-2 on VIA C3 Nehemia makes system unusable] and Fedora bug [https://bugzilla.redhat.com/show_bug.cgi?id=579838 glibc not compatible with AMD Geode LX] were reported.
A month later the Arch Linux bug [https://bugs.archlinux.org/task/19733 Update to glibc 2.12-2 on VIA C3 Nehemia makes system unusable] and Fedora bug [https://bugzilla.redhat.com/show_bug.cgi?id=579838 glibc not compatible with AMD Geode LX] were reported. glibc being a core component of most GNU systems meant updating completely crashed people's machines. Oops.
 
Unlike the Linux and GNU Assembler discussions, the Arch Linux and Fedora discussions were from the perspective of people building and packaging software. Finding out what was broken was a little tricky.
 
* Was it GNU Assembler for adding nopls to code?
* Was it glibc for tuning for i686 CPUs?
* Was it the Linux distros for running i686 binaries on non-i686 CPUs?
 
Things were a little tricky for Fedora here as they explicitly supported the AMD Geode LX800 as it was used in millions of laptops for the One Laptop per Child project. While the LX800 isn't i686, it ran i686 binaries fine. They would have to support not just i686 but i586 too for their entire distribution just to support this laptop.
 
Around this time the GNU Assembler committed [https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff;h=2210942396dab942a86cb6777c705554b84ebb0e Don't generate multi-byte NOPs for i686.] This patch restricted generating multi-byte NOPs to Intel and AMD CPUs. Strangely enough the i586 AMD K6-2 CPU was marked as supporting multi-byte NOPs, which was fixed in the 2013 commit [https://sourceware.org/git/?p=binutils-gdb.git;a=commitdiff;h=d56da83e58816c45a4bc70503776a6e62a66bf89 Remove CpuNop from CPU_K6_2_FLAGS].
 
After a few months of discussion and without a new GNU Assembler release, Arch and Fedora decided to just revert glibc's change. This at least fixed things and made i686 builds of their distributions run on CPUs they supported.

Revision as of 12:02, 27 January 2022

During research for my Geode Repair projects, I found an amazing saga based around a CPU instruction. Nobody else has written this up from what I can see, so here's my take.

Background

The AMD Geode LX800 CPU is an incredibly strange CPU. Despite being branded AMD, it's a descendant of the Cyrix MediaGX with a ton of features stapled on.

It implements the following instruction sets:

  • Pentium (i586)
  • Pentium Pro (i686)
  • MMX
  • Extended MMX
  • 3DNow!
  • Enhanced 3DNow!
  • Extra AMD Geode instructions

Notably the machine does not support PAE (Physical Address Extension). This may be why the CPUID identifies as family 5 (Pentium), not family 6 (Pentium Pro). This is all according to my reading of the AMD Geode LX Processors Data Book.

In contrast, some CPUs like the VIA C3 marked their CPU as family 6 without implementing the instruction set specified in the Pentium Pro Family Developer's Manual Volume 2. Most notably the 'CMOV' instruction.

But in theory, you could run an i686 system on the Geode without PAE. In practice it's a bit more complicated.

Undocumented i686 instructions

In 1995 Intel released the Pentium Pro and its developer manual documenting the entire instruction set.

By 1997 Christian Ludloff had created a map of 2 byte x86 opcodes. This confirmed Intel's documentation, but included a few unknown opcodes: 0F 34, 0F 35, as well as 0F 18 through 0F 1F.

In 1997 those first two opcodes 0F 34 and 0F 35 were determined to be Pentium II SYSENTER and SYSEXIT instructions despite Intel only documenting this later in 1999. It turns out the instructions were available on the Pentium Pro but broken. See SYSENTER, Where Are you? for a good summary of this situation.

Hilariously enough, the Geode LX800 identifies as a Pentium but supports the SYSENTER and SYSEXIT instructions introduced in the Pentium Pro and finally made useful in the Pentium II. Weird.

In 1998 Christian Ludloff documented in his updated map of 2 byte x86 opcodes that the 0F 18 through 0F 1F range of opcodes were hinting NOPs. The first being the 0F 18 opcode which maps to PREFETCHh instructions. I believe this information was documented first in the Intel Architecture Optimization Reference Manual.

Later in 2003 Christian Ludloff clarified in an email thread Undocumented opcodes (HINT_NOP) that these hinting NOPs were declared by Intel in their 1995 patent US5701442. The idea behind this patent from my reading is that you can encode a program written in another ISA as a series of opcodes that are run as NOPs on older machines and the new ISA on a newer machine.

I'm not sure why, but third party x86 CPUs aside from AMD didn't implement these NOPs. Perhaps Intel kept this patent close to their heart? Or maybe it's just not worth spending silicon and research on NOPs that nobody used?

The birth of multi-byte NOP

In 2006 Intel released an updated IA-32 Intel Architecture Software Developer's Manual. In addition to the standard one byte NOP op code, the multi-byte opcode was documented. This NOP could be from 2 to 9 bytes long, much longer than a single byte NOP. This is a pretty useful instruction for aligning code and data in memory. The only thing weird about this instruction is that it was marked as available on Pentium Pro and newer machines despite being documented in 2006. It turned out that Intel had recycled one of their hinting NOPs (0F 1F) as a new instruction.

Someone pointed this out on the Intel forum in the thread Multi-byte NOP opcode made official. They had tested and verified the feature and even pointed out that it works on AMD processors despite this instruction not being documented anywhere. They asked a few hard hitting questions to Intel:

  • Why was this opcode secret?
  • Why does it work on AMD CPUs?
  • Why does AMD recommend an opcode of 66 66 66 90 for multi-byte NOPs?

Intel got back to them with a 'this information is Intel Confidential and would require an NDA to discuss' reply. NOPs are definitely serious business.

I'm going to just go out and guess that AMD recommended the 66 90 opcode series because their CPUs optimized it and it worked on older machines. While with Intel their solution seems to be to recycle their trash.

In 2007 Symantec wrote a blog post x86 Fetch-Decode Anomalies showing that Intel's hinting NOP opcode they assigned to the multi-byte NOP actually will attempt to fetch memory (and even page fault) if you instruct it to. This isn't a problem in practice, but it gives more evidence this opcode isn't strictly a NOP.

Linux fallout

In 2006 PATCH: Add "nop memory" for i386/x86-64 was committed to the GNU Assembler. It added support for the 'nopl' and 'nopw' assembly instructions that map to multi-byte NOP code.

In 2007 x86: multi-byte single instruction NOPs was committed to Linux. This added a set of 'P6 NOPs' that used the multi-byte NOP opcodes and used them for i686 or newer x86 CPUs. Which type of NOPs to use were decided at runtime, so running an i686 kernel on an i586 machine would not cause any issues with this. Strangely on 64-bit systems the NOPs were only used if your CPU vendor was Intel.

In 2008 the Debian bugs Linux 2.6.24 fails to boot on MS Virtual PC 2007 and -686 build uses long noops, that are unsupported by Transmeta Crusoe, immediate crash on boot were reported, as well as the Linux. After a lot of discussion, the following CPUs were reported to not support multi-byte NOPs:

  • VIA C3 Nehemiah
  • Transmeta Crusoe TM5800
  • Microsoft Virtual PC 2007
  • QEMU 0.9.1
  • AMD Geode LX800

Interestingly enough the TM5800 reports its CPUID as family 5 like the LX800 does. But for the TM5800 the kernel promoted its status to i686 by changing the reported family at runtime.

The LX800 reports its family as 5, so shouldn't have failed due to the patch. I looked up other reports from the time and found the bug report 2.6.24-rc8 hangs at mfgpt-timer which seems to fit better, especially since the reporter didn't post any logs.

In parallel [BUG] x86 kenel won't boot under Virtual PC was reported to the Linux mailing list. This has a bit of a more focused discussion about how to address this problem. To summarize the discussion:

  • GNU Assembler shouldn't generate multi-byte NOPs if not all i686 CPUs support it
  • This is all too much effort for NOPs

A flurry of fixes appeared in Linux 2.6.25 to stop the bleeding:

This decided Transmeta Crusoe CPUs weren't i686, and limited use of multi-byte NOPs to i686 non-generic kernels.

A little while later in Linux 2.6.27 this amazing chain of patches happened:

Instead of only adding multi-byte NOPs to i686 and better machines during the kernel build, the plan was to only use multi-byte NOPs at runtime based on whether the CPU supported running multi-byte NOPs.

Unfortunately the developers found the GNU Assembler would add multi-byte NOPs all the time for i686 and Virtual PC would freeze during detection of multi-byte NOP support. In the end they just threw their arms up and said 'no multi-byte NOPs at all on 32-bit x86.

As a result, the 'nopl' flag found in /proc/cpuinfo which would have shown if the CPU supported multi-byte NOPs is always hidden on 32-bit x86 and always shown on 64-bit x86, making it effectively useless.

GNU Assembler confusion

In 2008 in the midst of the kernel fallout the GNU Assembler bug i386 NOPs must be derived from march not mtune.

When compiling code you can specify which CPU family to support and which specific CPU to optimize for. The kernel developers found that the GNU Assembler wouldn't add multi-byte NOPs if you targeted the i686 family, instead it only added them if you targeted the i686 CPU (the Pentium Pro). This was confusing for a few reasons:

  • Multi-byte NOPs weren't used on all CPUs that supported them by default
  • Optimizing for a specific CPU could break compatibility with the family

Reading the bug report, you can see two schools of thought on what the problem is here and how to fix it.

The bug reporters believed:

  • Multi-byte NOPs are not part of the i686 family
  • Optimizing for the i686 CPU is adding Pentium Pro-only instructions
  • The i686 architecture should not emit multi-byte NOPs

The GNU Assembler developers believed:

  • Multi-byte NOPs are indeed part of the i686 family
  • Optimizing for the i686 CPU is using i686 instructions
  • Developers should build against the i586 architecture if they need wider compatibility

Something important to note here is that most software projects didn't ask GNU Assembler to target the i686 CPU, so this bug didn't really affect many projects in practice. A workaround was to optimize for the 'generic32' CPU which didn't use the nopl instructions.

glibc fallout

In 2010 Pass -mtune=i686 to assembler when compiling for i686 was committed to glibc. This told GNU Assembler to optimize for i686 CPUs (Pentium Pro), and as I mentioned in the previous section, this used multi-byte NOPs.

A month later the Arch Linux bug Update to glibc 2.12-2 on VIA C3 Nehemia makes system unusable and Fedora bug glibc not compatible with AMD Geode LX were reported. glibc being a core component of most GNU systems meant updating completely crashed people's machines. Oops.

Unlike the Linux and GNU Assembler discussions, the Arch Linux and Fedora discussions were from the perspective of people building and packaging software. Finding out what was broken was a little tricky.

  • Was it GNU Assembler for adding nopls to code?
  • Was it glibc for tuning for i686 CPUs?
  • Was it the Linux distros for running i686 binaries on non-i686 CPUs?

Things were a little tricky for Fedora here as they explicitly supported the AMD Geode LX800 as it was used in millions of laptops for the One Laptop per Child project. While the LX800 isn't i686, it ran i686 binaries fine. They would have to support not just i686 but i586 too for their entire distribution just to support this laptop.

Around this time the GNU Assembler committed Don't generate multi-byte NOPs for i686. This patch restricted generating multi-byte NOPs to Intel and AMD CPUs. Strangely enough the i586 AMD K6-2 CPU was marked as supporting multi-byte NOPs, which was fixed in the 2013 commit Remove CpuNop from CPU_K6_2_FLAGS.

After a few months of discussion and without a new GNU Assembler release, Arch and Fedora decided to just revert glibc's change. This at least fixed things and made i686 builds of their distributions run on CPUs they supported.