What is better "int 0x80" or "syscall" in 32-bit code on Linux?
Solution 1:
-
syscall
is the default way of entering kernel mode onx86-64
. This instruction is not available in 32 bit modes of operation on Intel processors. -
sysenter
is an instruction most frequently used to invoke system calls in 32 bit modes of operation. It is similar tosyscall
, a bit more difficult to use though, but that is the kernel's concern. -
int 0x80
is a legacy way to invoke a system call and should be avoided.
The preferred way to invoke a system call is to use vDSO, a part of memory mapped in each process address space that allows to use system calls more efficiently (for example, by not entering kernel mode in some cases at all). vDSO also takes care of more difficult, in comparison to the legacy int 0x80
way, handling of syscall
or sysenter
instructions.
Also, see this and this.
Solution 2:
My answer here covers your question.
In practice, recent kernels are implementing a VDSO, notably to dynamically optimize system calls (the kernel sets the VDSO to some code best for the current processor). So you should use the VDSO, and you'll better use, for existing syscalls, the interface provided by the libc.
Notice that, AFAIK, a significant part of the cost of simple syscalls is going from user-space to kernel and back. Hence, for some syscalls (probably gettimeofday
, getpid
...) the VDSO might avoid even that (and technically might avoid doing a real syscall). For most syscalls (like open
, read
, send
, mmap
....) the kernel cost of the syscall is large enough to make any improvement of the user-space to kernel space transition (e.g. using SYSENTER
or SYSCALL
machine instructions instead of INT
) insignificant.
Solution 3:
Beware of this before changing : system call numbers differ when doing 0x80 or syscall, e.g sys_write is 4 with 0x80 and 1 with syscall.
http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html for 32 bits or 0x80 http://blog.rchapman.org/post/36801038863/linux-system-call-table-for-x86-64 for syscall