I was reading the minix3 source code, the interrupt handling saves every register while the syscall did not save any registers, why not? What is the consideration behind the decision of whether or not reg value on saved on the kernel stack?
clarification: by syscall, I meant minix3's way of doing rendezvous messaging (send/receive/sendrec/notify)
minix3 syscall vs interrupt
-
- Member
- Posts: 5560
- Joined: Mon Mar 25, 2013 7:01 pm
Re: minix3 syscall vs interrupt
Why does a syscall need to preserve registers? Interrupts can happen at any time, so the interrupt handler needs to preserve the registers so it can return to the running program exactly as it was before the interrupt. Syscalls only happen when the program wants them, so the program won't be surprised if the syscall doesn't preserve the registers.
Re: minix3 syscall vs interrupt
there can be cases where syscall need to preserve registers or things will not work
consider I want to repeat syscall in a loop
movl $100, %ecx
loop1:
syscall
loop loop1
if the %ecx value is modified in a syscall, this will not work correctly
consider I want to repeat syscall in a loop
movl $100, %ecx
loop1:
syscall
loop loop1
if the %ecx value is modified in a syscall, this will not work correctly
-
- Member
- Posts: 5560
- Joined: Mon Mar 25, 2013 7:01 pm
Re: minix3 syscall vs interrupt
The syscall doesn't need to preserve registers because you can preserve registers.
-
- Member
- Posts: 426
- Joined: Tue Apr 03, 2018 2:44 am
Re: minix3 syscall vs interrupt
Then it is incumbent on the libc syscall wrapper to preserve the registers required by the ABI at the syscall C function boundary. But the syscall mechanism itself is not beholden to the C ABI, it is wrapped and exported to C via a C wrapper.
The user level wrapper knows what registers are and are not preserved.
Re: minix3 syscall vs interrupt
FWIW, on Linux, different architectures will handle this differently. Most architectures actually preserve all parameters except the return value register (obviously). PowerPC and PowerPC64 on the other hand clobber all call-clobbered registers (r0, r3-r12) and the condition reg (because cr0.so is used as error flag).
So ITchimp's code would work on Linux. But in general, it is up to the OS to define the syscall ABI.
So ITchimp's code would work on Linux. But in general, it is up to the OS to define the syscall ABI.
Carpe diem!
Re: minix3 syscall vs interrupt
System calls in x86_64 systems normally use the syscall/sysret instructions, which always clobber the RCX and R11 registers. It’s the programmer’s responsibility to preserve these registers if necessary. As has been said, the wrapper will do this.
Re: minix3 syscall vs interrupt
Ah, sorry, I had forgotten about that . Yes, RCX and R11 are clobbered. But the other registers are preserved.
Which, because he chose the loop instruction, means that ITchimp's code actually wouldn't work on Linux, except on i386, where it would work until you need to give the syscall a second argument (because ECX is the second argument register). Linux on i386 does support the syscall instruction, but only through the VDSO, which reshuffles the arguments as necessary.
Carpe diem!