Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a syscall clobber rcx and r11? [duplicate]

In the book Low-Level Programming: C, Assembly, and Program Execution on Intel® 64 Architecture it says,

On system call arguments The arguments for system calls are stored in a different set of registers than those for functions. The fourth argument is stored in r10 , while a function accepts the fourth argument in rcx!

The reason is that syscall instruction implicitly uses rcx. System calls cannot accept more than six arguments.

You can see this also mentioned in this Stack Overflow post,

A system-call is done via the syscall instruction. This clobbers %rcx and %r11, as well as %rax, but other registers are preserved.

I understand clobbering rax to store the return code, but why is rcx, and r11 clobbered in syscall? Is there a list of the specific syscalls that clobber rcx/r11? Is there a convention for the clobbering? Are they assumed safe in any syscalls?

like image 252
NO WAR WITH RUSSIA Avatar asked May 28 '18 17:05

NO WAR WITH RUSSIA


People also ask

How does syscall handle RFLAGS?

SYSCALL also saves RFLAGS into R11 and then masks RFLAGS using the IA32_FMASK MSR (MSR address C0000084H); specifically, the processor clears in RFLAGS every bit corresponding to a bit that is set in the IA32_FMASK MSR. SYSCALL loads the CS and SS selectors with values derived from bits 47:32 of the IA32_STAR MSR.

What is the syscall instruction used for?

The syscall instruction uses rcx to store the address of the next instruction to return to, and r11 to save the value of the rflags register. These values will then be restored by the sysret instruction.

What is the wrmsr instruction in syscall?

(The WRMSR instruction ensures that the IA32_LSTAR MSR always contain a canonical address.) SYSCALL also saves RFLAGS into R11 and then masks RFLAGS using the IA32_FMASK MSR (MSR address C0000084H); specifically, the processor clears in RFLAGS every bit corresponding to a bit that is set in the IA32_FMASK MSR.

Does the syscall instruction save the stack pointer (RSP)?

The SYSCALL instruction does not save the stack pointer (RSP). If the OS system-call handler will change the stack pointer, it is the responsibility of software to save the previous value of the stack pointer.


Video Answer


1 Answers

The syscall instruction uses rcx to store the address of the next instruction to return to, and r11 to save the value of the rflags register. These values will then be restored by the sysret instruction.

This is done by the CPU when executing the CPU instruction, so any OS-specific calling conventions need to avoid using these registers to pass arguments to syscalls.

like image 169
interjay Avatar answered Oct 23 '22 04:10

interjay