r/programming • u/mttd • Aug 19 '19
Sushi Roll: A CPU research kernel with minimal noise for cycle-by-cycle micro-architectural introspection
https://gamozolabs.github.io/metrology/2019/08/19/sushi_roll.html89
u/HighRelevancy Aug 19 '19
The processor then holds the results and makes sure they “appear” to other cores in the correct order, as x86 is a strongly-ordered architecture
x86 is a strongly-ordered architecture
Which gets me thinking: "As opposed to what?"
20
u/IJzerbaard Aug 19 '19
I thought that paragraph was a bit weird, because it appears to throw out-of-order execution and memory order on one heap, but they are different things and governed by different mechanisms.
11
u/HighRelevancy Aug 19 '19
I think they're very much linked though. You can't reorder things in a way that would change the effect of a single thread of code. Memory interactions are the only time any of the reordering is exposed, and so memory fencing is the only time you need to exercise control over it. Other reorderings are just to make better use of the ALU and such, which should be entirely transparent.
5
u/MakesGamesForFun Aug 20 '19
The above article even has a line discussing this:
It’s true that a x86/64 processor can execute instructions out-of-order, but that’s a hardware implementation detail – what matters is that it still keeps its memory interactionsin-order, so in a multicore environment, we can still consider it strongly-ordered.
1
u/HighRelevancy Aug 20 '19
Well there ye go. I only got to read half the article last night before I was WAY PAST MY BED TIME but it's nice to have my intuition confirmed.
56
u/kvigor Aug 19 '19
Sorry, no plans to make this tool public for a while.
Well, that certainly makes the whole thing much less interesting.
2
u/danny54670 Aug 19 '19
Yes, it would be nice to see the implementations of the pseudo-code setup/teardown functions. We'll have to make do for now with the author's description of the core technique of obtaining graphs of performance counter values as a function of CPU cycle (using the
IA32_DEBUGCTL.Freeze_PerfMon_On_PMIfeature). That seems like a brilliant idea.0
Aug 19 '19
[deleted]
18
u/gamozolabs Aug 19 '19
Awhh sorry. No company owns the rights to this code and I could open source it if I wanted. Unfortunately the code is riddled with exploits and PoCs for CPU bugs and I don't really have the time to clean up the code for release. It's also such a sensitive tool that even remotely supporting this for multiple users (read: multiple micro-architectures) would be a pain. Right now it makes some hefty assumptions that performance counters v4 are supported, that I'm running on Skylake, etc.
Definitely possible with a bit of work, but I just don't have the time right at this moment to make the changes for going public. The environment of a whole OS also isn't the most welcoming experience for researchers, so I thought getting the concept/technique out there was much more important. I'm sure we'll see people make some drivers and other implementations of this technique but in a more standard environment.
8
u/dimitriye98 Aug 19 '19 edited Nov 05 '25
reel days anna iron shed sole jail voip
5
u/gamozolabs Aug 19 '19
Because many of the things in it are not ready for public release. The kernel does a lot more than just make graphs.
7
u/dimitriye98 Aug 19 '19 edited Nov 05 '25
but hand pike goto dome blah fat mess
32
u/gamozolabs Aug 19 '19 edited Aug 19 '19
This kernel has been actively used to find 0-day in software and hardware for about 2 years (and still references non-public and un-patched vulnerabilities). It contains emulators and hypervisors successfully used for fuzzing, and IPC mechanisms to manage fuzzing and sharing results and inputs between the whole system.
Since it's a personal project that I never intended to make public, there are references to specific bugs, fuzzers, generators, exploits, etc which live all over the code base and it's not a trivial task to remove all of these features for release.
I have made Sushi Roll available to certain researchers who I trust the tech with, and who I know will use responsible disclosures if they were to come across some CPU vulnerability.
It would honestly be less work for me to start with a new kernel (like my https://github.com/gamozolabs/orange_slice kernel, which is a stripped down version of sushi roll) and add this tech to it, than clean up what I have now.
Would make for a good 2 hour live stream. Wouldn't be too hard to get this performance counting tech into it.
10
u/dimitriye98 Aug 19 '19 edited Nov 05 '25
yard path soft thus cool alan ph ict
7
u/gamozolabs Aug 19 '19
No problem! I'll try to see if I can fit a stream in my schedule this week. That'd get this functionality out there for the public to use :)
13
u/danny54670 Aug 19 '19 edited Aug 22 '19
Here I am thinking that I understood "basically" how an Intel processor works. And then I encounter this, and realize that there are a lot of things I don't know. There's something mesmerizing about this blog post. I have already read it three times, yet I have a strange desire to read it again, because each time I pick up on something that I didn't consciously observe or comprehend before.
What are the uOP "ports" that the author keeps mentioning?
I vaguely recall reading about an application that could compile a program as an operating system, sort of like boot to metal. Is that what Sushi Roll is?
EDIT - I was thinking of IncludeOS.
6
u/aa93 Aug 19 '19
Ports are basically the entry points to each of the independent physical execution pipelines.
Each port can accept one uop per cycle, but the pipelines those ports feed have different capabilities.
Some of the ports have pipelines with integer and floating point math units, some can do a floating point fused-multiply-add, some can do vectorized operations, and some can be paired up to work on 512bits of data at a time shared across their hardware (as in AVX512 instructions). Only two of the ports, however, are able to execute load instructions, and only one can execute store instructions.
5
u/Ravek Aug 19 '19
What are the uOP "ports" that the author keeps mentioning?
A CPU has a bunch of 'execution units', which are pieces of hardware that can handle a specific type of operation. E.g. doing integer arithmetic, logic, floating point arithmetic, memory stores, memory loads, memory address computations, etc. The execution units aren't all individually accessible, but instead are grouped behind 'ports'. So one port could have units for integer arithmetic + logic + floating point addition/subtraction behind it, or only a unit for memory stores, etc.
Every instruction needs one or more execution units to run, depending on which type of instruction it is. A simple one like 'XOR two registers' will need only one execution unit, but a more complex one which does arithmetic and stores to memory all in one go might need three (arithmetic, address computation, and storying to memory). All of these parts to executing an instruction will be scheduled to separate ports on the CPU.
A modern CPU will have multiple execution units for things like arithmetic and logic operations behind separate ports, so can execute several instructions that need these units in the same cycle. An important part of optimising for a CPU is trying to keep as many ports occupied as possible to make maximum use of the parallelism inherent in the design.
If you're interested in more detail, Agner Fog has some good resources: https://www.agner.org/optimize/microarchitecture.pdf, https://www.agner.org/optimize/instruction_tables.pdf
3
u/danny54670 Aug 19 '19
Do AMD processors support the same performance monitoring features that are used by the author?
5
u/gamozolabs Aug 19 '19
AMD does have performance monitors (programmed in a very similar way as well). I don't know if they support all of the features here (like freeze on PMI), so I don't know if this technique would work. The events you can monitor on AMD are quite a bit different, but there's enough overlap to get similar information!
Section 3.22 of https://www.amd.com/system/files/TechDocs/48751_16h_bkdg.pdf will show you some of the things you can monitor (this is specifically for family 0x16 AMD processors)
1
u/ryp3gridId Aug 21 '19
This is super interesting. Thanks for the share. /u/gamozolabs: I'd love to read more posts about this topic (in case you uncover more funny microarchitecture details)
14
u/claytonkb Aug 19 '19
Correction:
Every macro-instruction that is not being macro-fused is executed as one or more microcode operations in the execution engine (EE). The decoder distinguishes between different classes of macro-ops by their complexity and the complexity of the macro-op determines whether its associated microcode will be issued by a simple decoder, complex decoder or the MS. The MS is only invoked for macro-instructions or other events that are going to require more than four uops.
Source: me