Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Linux follow Unix syscall conventions?

I'm teaching myself Linux assembly language and I've come across an interesting difference between BSD and Linux. In Unix, you push your syscall parameters onto the stack before calling an 80h interrupt; by contrast, in Linux, you pass your parameters in registers.

Does anyone know what the rationale was for the Linux developers to go with registers instead of the stack?

Note: Here's a good page detailing this difference: FreeBSD Developer's Handbook:System Calls without explaining the rationale.

like image 763
clay Avatar asked Nov 01 '22 09:11

clay


2 Answers

The syscall convention is different because the standard function calling sequence is different. Im assuming you're talking about the difference between the x86-32 calling convention and the AMD64 calling convention. You can check out the AMD64 ABI here.

But if you want to get to the point quickly check this post. Basically it's about speed. By changing the calling convention and using registers instead of the stack you can shave off instructions in the prologue and the epilogue of a call.

like image 129
red-E Avatar answered Nov 10 '22 12:11

red-E


You can use some registers with 32 bit code as well. There are several calling conventions for 32-bit code: cdecl, stdcall, pascal and fastcall. Windows and Linux use the same calling conventions for 32-bit code. With fastcall (__attribute((fastcall) in GCC) the first two integer parameters (3 with some compilers) can be registers. The other calling conventions use the stack.

For 64-bit code Windows and Linux use different calling conventions. Linux can use up to 14 registers for calls and Windows only six. Using registers can make the code faster. That could be part of the reason some 64-bit code with many function calls runs O(10%) faster than the same 32-bit code.

like image 29
Z boson Avatar answered Nov 10 '22 13:11

Z boson