What is better “int 0x80” or “syscall”?

2019-01-06 10:39发布

I study the Linux Kernel and found out that for x86_64 architecture the interrupt int 0x80 doesn't work as calling for system call.

The question is: in case of x86 architecture what is more preferable syscall or int 0x80 and why?

EDIT: I use the kernel 3.4

4条回答
三岁会撩人
2楼-- · 2019-01-06 11:02

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.

查看更多
劳资没心,怎么记你
3楼-- · 2019-01-06 11:03

int 0x80 is a better terminology to indicate its a system call to the kernel to tell it to do something.

The meaning and interpretation are interchangeble, 'make a syscall' or 'issue int 80h'.

It is no different to the days of DOS:

  • invoke int 21h to get DOS to do something depedning on the AX register and optionally ES:DX register pair,
  • int 13h is the BIOS hard disk handler.
  • int 10h is the EGA/VGA screen.
  • int 09h is the keyboard handler.

What is the common theme here is this, when a interrupt/syscall is invoked, the kernel checks the state of the registers to see what type of system call is required. By looking at for example, eax register, for example, and determine what to perform, internally context switches into kernel space, carry out the procedure and context switch back to user-space, with an option to return back the results of the call, i.e. was it successful or was it in failure.

查看更多
看我几分像从前
4楼-- · 2019-01-06 11:07
  • syscall is default way of entering kernel mode on x86-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 to syscall, a bit more difficult to use though, but that is kernel's concern.
  • int 0x80 is a legacy way to invoke a system call and should be avoided.

Preferable way to invoke a system call is to use VDSO, a part of memory mapped in each process address space that allow 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.

查看更多
我只想做你的唯一
5楼-- · 2019-01-06 11:10

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

查看更多
登录 后发表回答