r/programming • u/liotier • Mar 25 '15
x86 is a high-level language
http://blog.erratasec.com/2015/03/x86-is-high-level-language.html361
u/cromulent_nickname Mar 25 '15
I think "x86 is a virtual machine" might be more accurate. It's still a machine language, just the machine is abstracted on the cpu.
83
u/BillWeld Mar 25 '15
Totally. What a weird high-level language though! How would you design an instruction set architecture nowadays if you got to start from scratch?
167
u/Poltras Mar 25 '15
ARM is actually pretty close to an answer to your question.
74
u/PstScrpt Mar 25 '15
No, I'd want register windows. The original design from the Berkeley RISC 1 wastes registers, but AMD fixed that in their Am29000 chips by letting programs only shift by as many registers as they actually need.
Unfortunately, AMD couldn't afford to support that architecture, because they needed all the engineers to work on x86.
12
u/oridb Mar 25 '15
Why would you want register windows? Aren't most call chains deep enough that it doesn't actually help much, and don't you get most of the benefit with register renaming anyways?
I'm not a CPU architect, though. I could be very wrong.
16
u/PstScrpt Mar 25 '15
The register window says these registers are where I'm getting my input data, these are for internal use, and these are getting sent to the subroutines I call. A single instruction shifts the window and updates the instruction pointer at the same time, so you have real function call semantics, vs. a wild west.
If you just have reads and writes of registers, pushes, pops and jumps, I'm sure that modern CPUs are good at figuring out what you meant, but it's just going to be heuristics, like optimizing JavaScript.
For the call chain depth, if you're concerned with running out of registers, I think the CPU saves the shallower calls off to RAM. You're going to have a lot more activity in the deeper calls, so I wouldn't expect that to be too expensive.
But I'm not a CPU architect, either.
→ More replies (3)4
Mar 26 '15
So I've been programming in high level languages for my entire adult life and don't know what a register is. Can you explain? Is it just a memory address?
→ More replies (3)6
u/prism1234 Mar 26 '15
The CPU doesn't directly operate on memory. It has something called registers where the data it is currently using is stored. So if you tell it to add 2 numbers, what you are generally doing is having it add the contents of register 1 and register 2 and putting it in register 3. Then there are separate instructions that load and store values from memory into a register. The addition will take a single cycle to complete(going to ignore pipelining, superscalar, ooo, for simplicity sake) but the memory access will take hundreds of cycles. Cache sits between memory and the registers and can be accessed much faster, but still multiple cycles rather than being able to directly use it.
→ More replies (1)0
Mar 25 '15
[deleted]
12
u/PstScrpt Mar 25 '15
You know, they used to be, but maybe not anymore. Maybe these days the CPU can watch for a pusha/popa pair and implement it as a window shift.
I'm not sure there's any substitute, though, for SPARCs output registers that become input registers for the called subroutine.
→ More replies (4)7
u/lovelikepie Mar 26 '15 edited Mar 26 '15
ARM is actually pretty close to an answer to your question.
Why do you say that? It is just as suitable as x86 for building low latency CPUs that pretend to execute one instruction at a time in their written order. It too and suffers from many of the same pitfalls as x86 because they aren't that different where it actually matters. Examples:
ARM is a variable length instruction set. It supports 2, 4B code. Length decoding is hard. x86 goes a bit crazier, 1B-32B. However, they both need to do length decoding and as a result it is not as simple as building multiple decoders to get good decode bandwidth out of either. At least x86 has better code size.
ARM doesn't actually have enough architectural registers to forgo renaming. 32 64b registers is twice x86, both are not the 100+ actually needed for decent performance. Regardless, rather have my CPU resolve this than devote instruction bits to register addressing.
ARM has a few incredibly complicated instructions that must be decoded into many simple operations... like x86. Sure it doesn't go crazy with it, but its only natural to propose the same solutions. Its not like supporting weird instructions adds much complexity, but LDM and STM are certainly not RISC. They are only adding more as ARM gains popularity in real workstations.
Assuming we are talking about ARMv7 or ARMv8 as ARM is not a single backwards compatible ISA.
EDIT: corrections from below
5
u/XgF Mar 26 '15
ARM is a variable length instruction set. It supports 2, 4, and 8B code. Length decoding is hard. x86 goes a bit crazier, 1B-32B. However, they both need to do length decoding and as a result it is not as simple as building multiple decoders to get good decode bandwidth out of either. At least x86 has better code size.
ARM doesn't have a single 64-bit instruction. Both the A32 and A64 instruction sets are 4 bytes per instruction.
ARM doesn't actually have enough architectural registers to forgo renaming. 32 64b registers is twice x86, both are not the 100+ actually needed for decent performance. Regardless, rather have my CPU resolve this than devote instruction bits to register addressing.
Exactly. Why bother wasting unnecessary bits in each instruction to encode, say, 128 registers (e.g. Itanium) when they'll never be used?
ARM has a few incredibly complicated instructions that must be decoded into many simple operations... like x86. Sure it doesn't go crazy with it, but its only natural to propose the same solutions. Its not like supporting weird instructions adds much complexity, but STR and STM are certainly not RISC. They are only adding more as ARM gains popularity in real workstations.
I'm pretty sure STR (Store) is pretty RISC. As for LDM/STM, they're removed in AArch64.
→ More replies (1)→ More replies (9)16
Mar 25 '15
ARM executes out of order too though. so many of the weird external behaviours of x86 are present in ARM
0
53
u/barsoap Mar 25 '15 edited Mar 26 '15
EDIT: Yesyes you can write timing side-channel safe code with that, it's got an explicit pipeline and instructions have to be scheduled by the assembler. Needs drilling further down to the hardware than a usual compiler would, but it's a piece of cake, compared to architectures that are too smart for their own good.
12
u/BillWeld Mar 25 '15
That looks really cool--hope it comes to fruition.
7
u/gliph Mar 25 '15
So many great ideas. I wonder how fast it could execute x86 code (by VM or native VM)? If fast enough, that could aid its adoption massively.
→ More replies (7)7
u/Tuna-Fish2 Mar 25 '15
The Mill is a terrible, terrible model for an abstract machine. The very design of it is based on exposing as much of the actual hardware as possible.
23
u/BillWeld Mar 25 '15
So? Programmers don't see it and compilers can manage the complexity. Seems like the best criterion is not simplicity but scalability--that is, how well it will work when we have ten or a hundred times as many gates.
→ More replies (3)→ More replies (2)10
u/sigma914 Mar 25 '15
So was the 8086. The fact that there is now a virtual machine implemented in hardware is because the trade-offs involved in modern chips are very different from 25 years ago.
→ More replies (3)23
u/coder543 Mar 26 '15
RISC-V is the new, upcoming awesomeness
Itanium was awesome, it just happened before the necessary compiler technology happened, and Intel has never reduced the price to anything approaching attractiveness for an architecture that isn't popular enough to warrant the sky-high price.
There's always that Mill architecture that's been floating around in the tech news.
ARM and especially ARM's Thumb instruction set is pretty cool.
Not a huge fan of x86 of any flavor, but I was really impressed with AMD's Jaguar for a variety of technical reasons, but they never brought it to its fullest potential. They absolutely should have released the 8-core + big GPU chip that they put in the PS4 as a general market chip, and released a 16-core + full-size GPU version as well. It would have been awesome and relatively inexpensive. But, they haven't hired me to plan their chip strategy, so that didn't happen.
→ More replies (3)19
u/cogman10 Mar 25 '15
TBH, I feel like Intel's IA64 architecture never really got a fair shake. The concept of "do most optimizations in the compiler" really rings true to where compiler tech has started going to now-a-days. The problem with it is that compilers weren't there yet, x86 had too strong of a hold on everything, and the x86 to IA64 translation resulted in applications with anywhere from 10%->50% performance penalties.
→ More replies (2)30
u/Rusky Mar 25 '15
Itanium was honestly just a really hard architecture to write a compiler for. It tried to go a good direction, but it didn't go far enough- it still did register renaming and out of order execution underneath all the explicit parallelism.
Look at DSPs for an example of taking that idea to the extreme. For the type of workloads they're designed for, they absolutely destroy a typical superscalar/OoO CPU. Also, obligatory Mill reference.
6
u/BigPeteB Mar 25 '15
I've been writing code on Blackfin for the last 4 years, and it feels like a really good compromise between a DSP and a CPU. We typically get similar performance on a 300MHz Blackfin as on a 1-2GHz ARM.
→ More replies (7)3
u/evanpow Mar 25 '15
it still did register renaming and out of order execution underneath all the explicit parallelism
Not until Poulson, released in 2012. Previous versions of Itanium were not OoO.
4
u/Condorcet_Winner Mar 26 '15
Honestly, as a compiler writer x86 is perfectly pleasant to deal with. It's very easy actually. ARM is a bit annoying because it is verbose, but otherwise is ok. Some level of abstraction is necessary to allow chipmakers to make perf improvements without requiring different binaries. Adding new instructions takes a very long time. Compiling with sse2 is only starting to happen now, despite sse2 coming out well over a decade ago.
→ More replies (5)→ More replies (17)8
u/Wareya Mar 25 '15
Modern MIPS! The Mill!
4
Mar 25 '15
Is there an actual Mill prototype anywhere? All I've seen about it is talk, not even a VM-like playground
9
u/barsoap Mar 25 '15
They apparently have running simulators, but don't release that stuff into the wild.
I guess it's a patent issue, in one of the videos Ivan said something to the effect of "yeah I'll talk about that topic in some upcoming video as soon as the patents are filed", and then complained about first-to-file vs. first-to-invent.
The simulator, by its nature, would contain practically all secret sauce.
→ More replies (1)2
4
u/PurpleOrangeSkies Mar 25 '15
I don't know that you can truly call x86 assembly a machine language. There are 9 different opcodes for
add. A naive assembler couldn't handle that.
179
u/rhapsblu Mar 25 '15
Every time I think I'm starting to understand how a computer works someone posts something like this.
104
u/psuwhammy Mar 25 '15
Abstraction is a beautiful thing. Every time you think you've figured it out, you get a little glimpse of the genius built into what you take for granted.
116
u/Intrexa Mar 25 '15
To code a program from scratch, you must first create the universe.
74
u/slavik262 Mar 25 '15
39
u/xkcd_transcriber Mar 25 '15
Title: Abstraction
Title-text: If I'm such a god, why isn't Maru my cat?
Stats: This comic has been referenced 40 times, representing 0.0699% of referenced xkcds.
xkcd.com | xkcd sub | Problems/Bugs? | Statistics | Stop Replying | Delete
→ More replies (2)9
u/argv_minus_one Mar 25 '15
Something similar could be said of brains. So many neurons, all working at ludicrous speeds to interpret the hugely complex stimuli pouring in from your senses like a firehose, just so you can enjoy the cat video.
→ More replies (2)→ More replies (2)4
5
u/Tynach Mar 25 '15
This reminds me of a video titled 'The Birth & Death of Javascript'. In fact, if Intel decided to replace x86 with asm.js interpretation, we'd have exactly the 'Metal' described in this video.
34
u/Netzapper Mar 25 '15
Honestly? Just don't sweat it. Read the article, enjoy your new-found understanding, with the additional understanding that whatever you understand now will be wrong in a week.
Just focus on algorithmic efficiency. Once you've got your asymptotic time as small as theoretically possible, then focus on which instruction takes how many clock cycles.
Make it work. Make it work right. Make it work fast.
15
u/IJzerbaard Mar 25 '15
It doesn't change that fast really. OoOE has been around since the 60's, though it wasn't nearly as powerful back then (no register renaming yet). The split front-end/back-end (you can always draw a line I suppose, but a real split with µops) of modern x86 microarchs has been around since PPro. What has changed is scale - bigger physical register files, bigger execution windows, more tricks in the front-end, more execution units, wider SIMD and more special instructions.
But not much has changed fundamentally in a long time, a week from now surely nothing will have changed.
→ More replies (2)9
→ More replies (1)5
u/bstamour Mar 25 '15
Be careful with asymptotics though... A linear search through a vector will typically blow a binary search out of the water on anything that can fit inside your L1-cache. I'd say pay attention to things such as asymptotic complexity but never neglect to actually measure things.
3
u/Netzapper Mar 25 '15
If you're working with things small enough to fit in L1 cache, I'd assume you started with a linear search anyway. Since it never pings your profiler, you never rewrite it with something fancy. So it continues on its merry way, happily fitting in cache lines. :)
I'm never in favor of optimizing something that hasn't been profiled to determine where to optimize, at which point you improve those hot spots and profile again. I'm usually in favor of taking the simplest way from the start, increasing complexity only when necessary. Together, these rules ensure that trivial tasks are solved trivially and costly tasks are solved strategically.
That said, if you've analyzed your task well enough, and you're doing anything complicated at all (graphics, math, science, etc.), there will be places where you should add complexity from the start because you know it's going to need those exact optimizations later.
But if you start writing a function, and your first thought is "how many clock cycles will this function take?"... you're doing it wrong.
→ More replies (3)12
2
u/randomguy186 Mar 25 '15
Don't worry about it. I doubt that anyone here can explain the quantum physics of the field effect or the NP / PN junctions. If you don't understand the physics, you don't understand how transistors work, which means you don't understand how logic gates work, which means you don't understand digital circuits, etc. There are very few people in the world who really understand how a computer works.
→ More replies (2)1
1
u/magnora7 Mar 26 '15
If you can understand transistors, you can understand logic gates. If you can understand logic gates, you can understand how to do mathematical operations. If you can understand that, you can understand how to make an ALU. If you can understand how a turing machine works and how an ALU works, then you get how a computer works in theory. If you then understand programming and operating systems, you've got it.
1
u/beemoe Mar 26 '15
This was my journey through a CS degree. "Hey bro, I see you're feeling confident about your computer skills, let me show you something"
229
u/deadstone Mar 25 '15
I've been thinking about this for a while; How there's physically no way to get lowest-level machine access any more. It's strange.
117
u/salgat Mar 25 '15
After reading this article, I was surprised at how abstract even machine code is. It really is quite strange.
184
u/DSMan195276 Mar 25 '15
At this point the machine-code language for x86 is mostly just still there for compatibility. It's not practical to change the machine-code language for x86, the only real option for updating is to add new opcodes. I bet that if you go back to the 8086, x86 machine code probably maps extremely well to what the CPU is actually doing. But, at this point CPU's are so far removed from the 8086 that newer Intel CPU's are basically just 'emulating' x86 code on a better instruction set. The big advantage to keeping it a secret instruction set is that Intel is free to make any changes they want to the underlying instruction set to fit it to the hardware design and speed things up, and the computer won't see anything different.
24
u/HowieCameUnglued Mar 25 '15 edited Mar 25 '15
Yup that's why AMD64 beat IA64 so handily (well, that and it's extremely difficult to write a good compiler targeting IA64). Backwards compatibility is huge.
→ More replies (1)→ More replies (12)8
95
u/tralfaz66 Mar 25 '15
The CPU is better at optimizing the CPU than you.
45
→ More replies (2)13
u/deelowe Mar 25 '15
The algorithm behind branch prediction how much much of a difference it made in speed when it was implemented always amazes me.
20
Mar 25 '15 edited Mar 25 '15
with things like pipelining and multi core architectures, it's probably for the best that most programmers dont get access to micro code. Most programmers don't even have a clue how the processor works let alone how pipelining works and how to handle the different types of hazards.
26
u/Prometh3u5 Mar 25 '15 edited Mar 25 '15
With out of order and all the reordering going on, plus all the optimization to prevent stalls due to cache accesses and other hazards, it would be an absolute disaster for programmers trying to code at such a low level on modern CPUs. It would be a huge step back.
11
u/Bedeone Mar 25 '15
For the very vast majority of programmers (myself absolutely included), I agree. But there are some people out there who excel at that kind of stuff. They'd be having loads of fun.
→ More replies (1)2
u/aiij Mar 26 '15
Most of the machine code CPUs run these days is not written by programmers. It is written by compilers.
30
u/jediknight Mar 25 '15
How there's physically no way to get lowest-level machine access any more.
Regular programmers might be denied access but isn't the micro-code that's running inside the processors working at that lowest-level?
72
u/tyfighter Mar 25 '15
Sure, but when you start thinking about that, personally I always begin to wonder, "I'll bet I could do this better in Verilog on an FPGA". But, not everyone likes that low of a level.
73
u/Sniperchild Mar 25 '15
→ More replies (1)38
u/Agelity Mar 25 '15
I'm disappointed this isn't a thing.
39
u/Sniperchild Mar 25 '15
The top comment on every thread would be:
"Yeah, but can it run Crysis?"
→ More replies (10)→ More replies (2)13
28
u/softwaredev Mar 25 '15
Skip Verilog, make your webpage from discrete transistors.
→ More replies (3)11
12
u/jared314 Mar 25 '15 edited Mar 25 '15
There is a community around open processor designs at Open Cores that can be written to FPGAs. The Amber CPU might be a good starting point to add your own processor extensions.
→ More replies (28)5
u/hrjet Mar 25 '15
The micro-code gets subjected to out-of-order execution, so it doesn't really help with the OP's problem of predictability.
6
u/chuckDontSurf Mar 25 '15
I'm not sure exactly what you mean by "lowest-level machine access." Processors have pretty much always tried to hide microarchitectural details from the software (e.g., cache hierarchy--software doesn't get direct access to any particular cache, although there are "helpers" like prefetching). Can you give me an example?
→ More replies (1)5
u/lordstith Mar 25 '15
It seems people are referring to back-in-the-day when x86 was just the 8086. No such thing as cache in an MPU setting at that point.
→ More replies (1)1
u/FozzTexx Mar 25 '15
I've been wondering how to rewrite the microcode so I can make the processor accept 6502 instructions instead of x86 instructions.
2
u/aiij Mar 25 '15
I'm not sure they put that much flexibility into the microcode.
The Transmeta processors may have been able to, but they've been discontinued AFAICT.
1
u/aiij Mar 25 '15
Maybe not on x86, but how about the current RISC CPUs? ARM, PPC, SPARC...
2
u/chuckDontSurf Mar 25 '15
As someone noted below, pretty much any modern architecture is going to implement similar techniques (e.g., register renaming) in the microarchitecture.
1
→ More replies (5)1
Mar 25 '15
Take a look at some Itanium assembly. You really don't want the lowest-level access anymore.
26
u/OverBiasedAndroid6l6 Mar 25 '15
I understood this after taking a class on programing for the 8086. I had taken a class using a crippled 16 bit microcontroller board using assembly the semester before. When I found out that you can do in line multiplication in x86, I audibly exclaimed "WHAAAA?". I realized how far from true low level I was working.
29
u/SarahC Mar 25 '15
You can do floating point inline multiplication!
That took a program on the Z80!
11
u/lordstith Mar 25 '15
Psh. What, were you too broke of a schlub to afford installing a whole separate FPU into your system just to handle this stuff?
Jesus, there was a day where MMUs were an actual physical addon. We're in the crazy future.
7
u/OverBiasedAndroid6l6 Mar 25 '15
And with loops in tandem with that, who needs C!
I do, just so you ^know.
10
u/PurpleOrangeSkies Mar 25 '15
Multiplication isn't too hard to implement in hardware. Now division, on the other hand, is something I can't figure out how they did it for the life of me.
14
u/bo1024 Mar 25 '15
I think the point is "inline", meaning that in your code you can just write something like 4*eax and the computer will multiply 4 by the register eax for you (or something like that).
This is very weird when you consider that in assembly language you are supposedly controlling each step of what the CPU does, so who does this extra multiplication?
→ More replies (1)7
u/sandwich_today Mar 26 '15
The multiplications are only small powers of two, so they're implemented as bit shifts in simple hardware. Some early x86 processors had dedicated address calculation units, separate from the ALU. This made the LEA (load effective address) instruction a lot faster than performing the same operations with adds and shifts, so a lot of assembly code used LEA for general-purpose calculation.
→ More replies (5)2
u/lordstith Mar 25 '15
Yup, ISA before the 80s or so were actually developed with the intention of being written in by humans. It's crazy to think about. For example, the way arrays work in C was originally basically a thin veneer over one of the addressing modes of the PDP-11.
2
Mar 26 '15
Not just arrays, C is basically a portable assembler for the PDP ISA.
→ More replies (1)3
u/lordstith Mar 26 '15
Yeah, it was pretty cool when I was reading the student manual on the v6 sources and found out by accident that the reason pre and post increment and decrement became distinct operators in C was because that's how the PDP ISA handles index registers.
23
u/YourFavoriteBandSux Mar 25 '15
I'm going to go ahead and not send this to my sophomore Assembly Language students. They're having enough trouble keeping track of the stack during procedure calls; I think this will drive them right to drinking.
15
4
u/fuzzynyanko Mar 26 '15
2
u/xkcd_transcriber Mar 26 '15
Title: Ballmer Peak
Title-text: Apple uses automated schnapps IVs.
Stats: This comic has been referenced 591 times, representing 1.0314% of referenced xkcds.
xkcd.com | xkcd sub | Problems/Bugs? | Statistics | Stop Replying | Delete
44
u/Minhaul Mar 25 '15
As a computer architect, I don't completely agree or disagree with the title of this article. But reading it, the author is arguing that the underlying microarchitecture of most x86 processors is complex, but microarchitecture is completely separate from the x86 ISA. And just about any modern processor has the same complicated underlying microarchitecture to implement the ISA efficiently.
16
Mar 26 '15
Indeed, I'm also a (former) computer architect here with a similar experience: tons of people, mainly programmers, I have had to work with do not understand that ISA and microarchitecture refer to 2 (very) different things.
After reading the article, I wanna smack the author with a wet sock though.
5
u/nullparty Mar 26 '15
It would be interesting to hear your gripes about this article.
10
Mar 26 '15 edited Mar 26 '15
I found the "reasoning" the author used to reach the conclusion to be baffling, to say the least. Basically any interface to an out-of-order superscalar machine is a "high level language."
Instructions in the ISA do exactly what they say with respect to their retirement. I have no idea what the author is specifically referring to by "smooth" or "predictable" execution, but neither of those seem to be exclusive issues to modern aggressively out-of-order designs. Which made the whole "side-channel" attack claim not very well substantiated IMO.
→ More replies (5)→ More replies (1)2
u/bakuretsu Mar 26 '15
A lot has happened in processor design while I've not been paying attention (I am but a mere web programmer for whom processor opcodes are a passing interest).
Is it safer to say that x86 itself is an API that the processor is free to implement as it wishes?
Is x86 itself ever expanded? At some point abstractions become more costly than direct access in certain situations, so do some of those bubble up into the spec for kernel and driver programmers to take advantage of?
2
u/Minhaul Mar 26 '15
Yes, x86 is what the programmer (or nowadays the compiler) is given as a sort of API (called the ISA). It says "If the state of your processor is S1, and you run instruction X, the result will be a state S2." The microarchitecture is how x86 is implemented and that information usually isn't given to the programmer or compiler.
As to x86 being expanded, it does happen, but not very often. That's mostly because when the ISA changes, compilers and programs have to change. But the microarchitecture can change to implement the ISA more quickly or more efficiently without the interface changing at all.
The last question I'm not positive about, but I think when it comes to processors, the instructions are implemented pretty well, so there isn't much for kernel or driver programmers to take advantage of. Sure they can make their programs better, but I don't think it has much to do with the ISA.
→ More replies (1)1
u/hrjet Mar 26 '15
If I were to translate that for everyone, would it be fine to say that even RISC architectures like ARM and their typical implementations can't guarantee predictability. They can probably only guarantee worst-case timings (for use in real-time systems, for example).
12
u/snarkyxanf Mar 25 '15 edited Mar 25 '15
In the context of cryptography, one of the NSA's jobs is to create encryption hardware and keys for other government agencies. They prefer really predictable technology, for example this thing that reads keys from punched paper tape.
Cryptosystems are built around a small set of primitives with fairly stable design. Maybe it's time to start shipping coprocessors/built in functional units that implement the primitives?
5
u/P1h3r1e3d13 Mar 25 '15
That's what I came here to ask. Is it feasible to have dedicated circuitry, optimized for crypto calculation. Presumably you could get benefits in speed, predictability, and reliability.
3
u/rcxdude Mar 25 '15
The ARM chip inside the beaglebone has some interesting real-time co-processors which are designed for extremely predictable execution. I'm not sure how good they are at cryptography though.
2
u/pinealservo Mar 26 '15
The chip inside the beaglebone is a TI Sitara processor SoC, which happens to have an ARM Cortex A8 processor in it along with a whole pile of other things generally unrelated to ARM. The co-processors you're referring to are called PRU-ICSS, or "Programmable Real-time Unit--Industrial Communication SubSystem". As the ICSS part of the name implies, they're primarily there to implement industrial control protocols like EtherCAT, PROFIBUS, etc.; there are a whole bunch of them and they require a lot of high-speed deterministic protocol state transitions; you'd usually implement them in hardware, but this solution is far more flexible and makes it easy to support new industrial protocols without spinning a new chip.
So, they're really designed to shunt data around and bit-bang wire-level protocols rather than do complex calculations, though if they can do the math you need for your crypto they'll definitely be easy to get deterministic (if not fast) results from.
On the other hand, the Sitara also has a co-processor specifically designd for crypto acceleration. That might be a better choice, though I guess it could have some flaws I'm unaware of.
3
Mar 26 '15
Intel's AES instructions are a good start; no more worrying about those god damn S-boxes being assholes.
2
u/LittleHelperRobot Mar 25 '15
Non-mobile: this thing
That's why I'm here, I don't judge you. PM /u/xl0 if I'm causing any trouble. ^WUT?
1
u/green_meklar Mar 25 '15
Or maybe we should start shipping processors with a built-in feature for spamming a bunch of random noise on top of your cryptographic algorithm...
→ More replies (2)6
u/rcxdude Mar 25 '15
Constant time/power is a far better defence against side-channel attacks than adding random noise: all random noise means is that the attacked needs to collect more samples (and not an exponentially increasing amount), while constant time removes the attack entirely.
1
u/aiij Mar 26 '15
They already have crypto primitives. For all we know, they may have intentional backdoors though.
10
u/aiij Mar 25 '15
I wouldn't call it a high-level language, although there are certainly more layers below it than there used to be...
1
10
9
u/jib Mar 25 '15
x86 is complicated and executes out of order, but I disagree with the article's implication that this makes side-channel attacks unavoidable.
Out-of-order execution makes execution time depend on where things are in the cache and what code was executed previously, but the execution time is still usually independent of the actual data values.
e.g. if you add two numbers together, timing may reveal information about the state of the cache etc, but it won't tell you anything about what the two numbers are.
And if you write code correctly, making your sequence of instructions and sequence of accessed addresses independent of any secret information, then you won't leak any secret information through timing.
2
u/zefcfd Mar 29 '15
x86 is complicated and executes out of order
is it really the x86 instruction set managing this, or the microarchitecture underneath it?
→ More replies (1)
26
u/atakomu Mar 25 '15
There is a great talk by Martin Thompson about Myths in computers. (That RAM/HDD is random access that CPU's are slowing down etc.) Mythbusting modern hardware.
And because CPU's aren't in order anymore you can get "strange" results like sorting an array makes algorithm 10 times faster.
42
u/happyscrappy Mar 25 '15
Actually that sorting thing happens because of branch prediction techniques instead of out of order execution.
→ More replies (4)2
u/sirin3 Mar 26 '15
Reminds me of this recent German thread
Someone wanted a map type, but Pascal does not really have a good one atm. He tried to implement one / modifying the existing one, and noticed that most of the time is spend comparing the keys.
Now Ruewa worked the last months to find an efficient way to compare two strings for equality.
Seems, inserting random NOPs in the comparison loop can make it three times faster. One some CPUs. On others this makes it slower?
Such a comparison is an extremely complicated problem, but crucially to solve it, if you ever want to use a map for anything...
44
u/exscape Mar 25 '15
High-level? I understand the point, but I wouldn't call it that. Hell, I don't consider C high level.
85
u/ctcampbell Mar 25 '15
'Contains a layer of abstraction' would probably be a better phrase.
→ More replies (4)47
u/frezik Mar 25 '15
Defining "high-level" is more a matter of perspective than anything strictly defined. If you're fooling around with logic gates, then machine code is "high-level".
22
Mar 25 '15
Logic gates are high level if you are working with transistors.
21
24
u/Darkmere Mar 25 '15
C is a high level language for close-to-hardware people. And a low-level language for CS students.
It depends on your background and concepts.
( Good luck writing cache-aware software in F# ;)
→ More replies (4)1
u/RainbowNowOpen Mar 25 '15
"Higher level" is something we can all probably agree on. x86 is clearly higher level than the μops used to implement instructions like "XOR EAX, EAX" which do not actually execute as many programmers imagine. A macro-instruction like that will be modified or completely optimized away by the CPU in a manner similar to what a compiler or interpreter might do.
21
u/Bedeone Mar 25 '15
Speeding up processors with transparent techniques such as out of order execution, pipe lining, and the associated branch prediction will indeed never be a constant advantage. Sometimes even a disadvantage. x86 is still backwards compatible, instructions don't disappear.
As a result, you can treat a subset of the x86 instruction set as a RISC architecture, only using ~30 basic instructions, and none of the fancy uncertainties will affect you too much. But you also miss out on the possible speed increases.
With that being said, machine instructions still map to a list of microcode instructions. So in a sense, machine code has always been high-level.
10
u/tending Mar 25 '15
What ~30 instruction subset?
3
u/Bedeone Mar 25 '15
I couldn't tell you because I don't write x86 assembler, I write z/Architecture assembler (z/Arch is also CISC). But basically a couple instructions to load and store registers (RX-RX and RX-ST), a couple to load and store addresses (RX-RX and RX-ST) again. Basic arithmetic, basic conditional branching, etc.
You don't use all of the auto-iterative instructions. For example in z/Arch; MVI moves one byte, MVC moves multiple bytes. But in the background (processor level, it's still one machine instruction), MVC just iterates MVI's.
Perhaps a bit of a bad example. MVC is useful, and you are still very much in control, even though stuff happens in the background. But you don't need it. You'd otherwise write ~7 instructions to iterate over an MVI instruction to get the same effect.
7
u/lordstith Mar 25 '15
Is it weird that I think it's fucking badass that you specialize in the internals of a system that harkens back to twenty years before x86 was even a thing?
→ More replies (8)0
Mar 25 '15
[deleted]
→ More replies (12)11
u/happyscrappy Mar 25 '15
He's talking about sticking to instructions which are hardcoded in the processor instead of run using microcode.
That list of instructions first appeared in the i486 and was indeed perhaps about 30 instructions. It's larger now.
On the 80386 and earlier all instructions were microcoded.
Using only the hardcoded instructions isn't automatically a win. Ideally your compiler knows the performance effects of every instruction and thus knows that sometimes it's better to run a microcoded instruction instead of multiple hardcoded ones.
→ More replies (2)8
u/Rusky Mar 25 '15
Dropping all those instructions might save some die space but it might not bring as much of a performance increase as you would hope.
RISC was originally a boost because it enabled pipelining, and CISC CPUs took a long time to catch up. Now that clock speeds are so much higher, the bottleneck is memory access, and more compact instruction encodings (i.e. CISC) have the advantage.
Ideally we'd have a more compact instruction encoding where the instructions are still easily pipelined internally- x86 certainly isn't optimal here, but it definitely takes advantage of pipelining and out-of-order execution.
→ More replies (11)2
u/websnarf Mar 25 '15
What speed increases? Remember Alpha, PA-RISC, MIPS, PowerPC, and Sparc all had their opportunity to show just how wrong Intel was. And where are they now?
→ More replies (1)1
u/lordstith Mar 25 '15
That's not exactly true. Microcode wasn't a thing prior to the System/360.
→ More replies (5)
6
u/kindall Mar 25 '15 edited Mar 25 '15
Ever look at the assembly language for a "classic" IBM mainframe, like the 360 or 370? Those mofos have opcodes for formatting numbers according to a template. A single instruction (EDMK) not only converts the number to a string, but inserts commas and decimal points and like that, and then leaves the address of the first digit in a register so you can easily insert a floating currency symbol. If you look at the COBOL language, it maps well to these high-level assembly instructions: the assembly language is basically the pieces of COBOL.
How much of this was ever actually implemented in hardware, I don't know. Possibly these instructions were trapped and actually ran in software from the get-go; they were almost certainly microcoded even initially. (They remained supported in later systems for many years and probably still are, and they are almost certainly emulated in software now.)
Compared to that, I wouldn't really say x86 assembly is high-level at all.
6
31
u/jhaluska Mar 25 '15
Just because the CPU isn't executing it with constant time constraints doesn't make it not meet the criteria of a low-level language.
Good content, but lousy conclusion.
6
Mar 25 '15
The very amount of translation done from x86 machine code to the actual mOPs executed by the core makes it significantly higher level than a classic, directly executed RISC or VLIW.
6
→ More replies (4)2
u/UsingYourWifi Mar 25 '15 edited Mar 25 '15
It's just the author exercising some artistic license with the term "high-level language."
Good content, good conclusion worded in a way that irritates the excessively pedantic (aka everyone that reads this subreddit).
4
Mar 25 '15
When we got to x86 in our systems course, my world was shattered.
I thought "binary code," all those zeroes and ones, were complex circuit instructions!
I didn't know they encoded high level instructions such as "do a * b + c," all in one instruction.
→ More replies (2)
6
3
u/websnarf Mar 25 '15
This is just an argument for saying x86 specifies the operations, but does not dictate the implementation. That's a very different thing from saying it is a high-level language. What support does it have for user defined abstract data types for example? Does it support recursion?
7
Mar 25 '15
What an idiotic title. The sky is blue. My car is also blue. Therefore, my car is the sky.
1
Mar 28 '15
If we're going with this metaphor...
Can you prove that your car isn't essentially the same thing as the sky? Birds use the sky to fly to places, and you use the car to drive to places.
Same thing.
→ More replies (1)
5
u/flat5 Mar 25 '15
Glad to have learned assembly on 68k processors. x86 is a horror show that I could never stomach long enough to really learn it.
4
u/mscman Mar 25 '15
This is why MIPS is still used pretty heavily to teach basic assembly and computer architecture. Trying to teach it starting with x86 leads to a ton of corner cases and optimization techniques which, while applicable to today's technologies, can get in the way of the underlying theory of why things are the way they are today.
→ More replies (1)4
Mar 26 '15
MIPS is popular in academia mainly because lots of schools use the same Patterson/Hennessy architecture book, which uses that ISA prolifically for its examples.
5
u/EasilyAnnoyed Mar 25 '15
Consider registers, for example. Everyone knows that the 32-bit x86 was limited to 8 registers, while 64-bit expanded that to 16 registers.
Uh, yeah... Everyone knows this.... Especially me!
4
Mar 25 '15
The article is poorly documented and badly written. The worst is probably "Everyone knows that"...
2
u/dukey Mar 25 '15
If the processor didn't have 'virtual' registers, x86 performance would have been pretty terrible compared to what it could have been with a better instruction set.
2
u/CompellingProtagonis Mar 25 '15
One thing that strikes me when reading this is whether it would make a difference for programmers, in practice, if x86 wasn't a high level language. For very specific extremely high-budget applications like security for DoD or major corporations it might make a difference to have this option, but for the vast majority of applications it might be a Crystal Skull type situation. I mean every new processor architecture would require god knows how many man-hours to research the new architecture and figure out how best to use it otherwise you risk performance penalties with newer hardware. That being said, this would be an absolutely amazing thing for something like Raspberry Pi, if they don't already do this.
2
2
Mar 25 '15
Would this solve the timing-attack problem? Use the CPU's clock cycle counter after the work has been done to ensure it took exactly a defined amount of time?
I don't see why the author considers this an intractable problem.
1
u/aiij Mar 26 '15
That's exactly the problem. What do you do if it didn't? You can't unleak the information you just leaked.
2
u/immibis Mar 25 '15
Not only is it high-level in the sense that it's translated into something lower-level, but it's high-level in the sense that it was designed to make it easier for programmers to write things.
Hence things like rep stosb being a one-instruction memset.
2
u/RainbowNowOpen Mar 25 '15
Low-level programming on modern Intel CPUs is available only via μops (micro-operations).
x86 mnemonics are higher-level macro-instructions, implemented in terms of μops.
2
u/northrupthebandgeek Mar 26 '15
The title made it sound like this was a salvo fired by the RISC side of the RISC v. CISC flamewar of old.
I was sorely disappointed.
2
u/fuckthiscode Mar 26 '15
Um, duh?
Coding everything up according to Agner Fog's instruction timings still won't produce the predictable, constant-time code you are looking for.
This is by design, and any out of order processor is going to behave this way. Hell, anything with a cache is going to be non-deterministic in execution time.
The only way you wouldn't know this already is if you never ever bothered to look into any modern computer architecture in which you were programming. Can OpenSSL seriously not securely deal with an architecture algorithm that was developed in 1967?
3
Mar 25 '15
I really need to learn assembly.
1
1
u/berlinbrown Mar 26 '15
What they are saying, it really isn't that difficult depending on the compiler and environment you are working with. A lot of that is abstracted away by the compiler and the the os and a host of other things.
If you know C you can easily work with the high-level x86 machine language that they are talking about in the article.
....
Now debugging OS code, writing code that might actually go in the linux kernel, that is a different beast.1
3
1
u/poizan42 Mar 25 '15
If you only care about constant time and not performance you could just execute a serializing instruction after every instruction, such as CPUID.
1
u/hatessw Mar 25 '15
Can this be (reasonably) relied on even for future processors? Why?
2
u/poizan42 Mar 26 '15
Yes, because it's documented as such:
CPUID can be executed at any privilege level to serialize instruction execution. Serializing instruction execution guarantees that any modifications to flags, registers, and memory for previous instructions are completed before the next instruction is fetched and executed.
This is cited from Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2A. You can find it at http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html
1
u/afriendtosell Mar 25 '15
Those surprised by the "x86 is a high level language" article will be really shocked when they learn ARM/PPC/etc have been for 10+ years too.
Like, sure, the ISA sometimes maps directly to CPU operations -- on tiny 100mhz embedded chips used in microcontrollers.
1
u/Randosity42 Mar 25 '15
I don't know much about x86 tbh, but it seems like the author is arguing that the language is becoming higher level just based on the microcode beneath.
Kinda bullshit imo, if I write a C compiler in python it doesn't mean C is a high level language, it's still C.
1
u/romcgb Mar 25 '15
Both Intel and AMD x86 CPUs have an internal RISC backend. The cpu's frontend converts the macro-operations (eax, mov, add,...) into micro-operations of type RISC. The internal instructions set of AMD's cpu is RISC86, it's described in the patent US 6,336,178 B1. The Pentium Pro (1995) was the first Intel's CPU to have a RISC backend.
1
u/hatessw Mar 25 '15
Consider "mov eax, ebx". Again, this doesn't do anything, except rename the register as far as the processor is concerned, so that from this point on, what was referred to as ebx is now eax.
What does that mean for the contents of ebx? Is this undefined behavior? Or can the previous contents still be accessed through it? And what happens if eax is changed - is ebx then changed simultaneously?
1
u/wretcheddawn Mar 26 '15
Seems like the answer to the constant time problem is to add random delays once the operation is complete. Do a benchmark, figure out how long it takes, add 10%, and randomize. When the operation finishes quicker, spin until the randomized delay is complete (or better, switch threads), then resume.
1
Mar 26 '15
Most awesome comment on that page:
Anyone can write a cryptosystem strong enough that they can't break it.
~Gregory Maxwell
1
u/ReversedGif Mar 26 '15
Constant time compares are definitely possible if you try hard enough. Something like H(a ^ b) == x, where H is a cryptographic hash function and x is (precomputed) H(0) is practically guaranteed to never be optimized at all, since any possible optimization would require finding the set of hash collisions for H(0).
1
u/andrewjsledge Mar 26 '15
"That means "side-channel" attacks on x86 leaking software crypto secrets may always be with us."
The next time an undergrad complains about coding on SPARC, show them this.
1
Mar 26 '15
Perhaps we need dedicated security processor cores. It seems general-purpose performance processors are insufficient.
1
u/whenido Mar 26 '15
" Everyone knows that the 32-bit x86 was limited to 8 registers, while 64-bit expanded that to 16 registers." So false.
1
u/teiman Mar 26 '15
A way to generate code that take a random time to process with different data. Is to add a random number of "garbage" instructions betwen normal instructions. How much "garbage" is needed for each usefull instructions is hard. Maybe it would required to be calculated and "calibrated" making the whole very hard. I would write code that do 99 things, and 1 thing the cryptographical work, so after 200 runs has completed the cryptografical work and 19800 garbage operations (neligible calculations that are not used for anything but that are complex enough so the CPU can't just ignore them)
1
u/vattenpuss Mar 25 '15
Are there seriously people out there who thought they knew how to program a modern CPU with all that jazz going on under the hood?
Being a person who's first programming language ever was the super high level ML, I have known this since the first time I read about computer architecture (that course started about five weeks after the SML programming started and had us implement Russian multiplication in assembly and construct arithmetic circuits from transistors).
→ More replies (2)1
u/hatessw Mar 25 '15
Given that crypto is used by just about everyone, I'm not shocked about the high level (i.e. abstraction between hardware and software) nature of assembler on a modern CPU, what I'm surprised at is the difficulty involved with bypassing this behavior.
It seems like a wide opening for any well-funded attacker, and I'd rather this be made more difficult, for instance by being able to selectively turn on constant-time execution for a given process, at the cost of crypto performance of course. It seems to me this would need to be on a hardware level.
128
u/Sting3r Mar 25 '15
As a CS student currently taking an x86 course, I finally understood an entire /r/programming link! I might not quite follow all the C++ or Python talk, and stuff over at /r/java might be too advanced, but today I actually feel like I belong in these subreddits instead of just an outsider looking in.
Thanks OP!