minix3 syscall vs interrupt

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
ITchimp
Member
Member
Posts: 134
Joined: Sat Aug 18, 2018 8:44 pm

minix3 syscall vs interrupt

Post by ITchimp »

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)
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: minix3 syscall vs interrupt

Post by Octocontrabass »

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.
ITchimp
Member
Member
Posts: 134
Joined: Sat Aug 18, 2018 8:44 pm

Re: minix3 syscall vs interrupt

Post by ITchimp »

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
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: minix3 syscall vs interrupt

Post by Octocontrabass »

The syscall doesn't need to preserve registers because you can preserve registers.
thewrongchristian
Member
Member
Posts: 426
Joined: Tue Apr 03, 2018 2:44 am

Re: minix3 syscall vs interrupt

Post by thewrongchristian »

ITchimp wrote: Tue Oct 22, 2024 4:39 pm 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
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.
nullplan
Member
Member
Posts: 1789
Joined: Wed Aug 30, 2017 8:24 am

Re: minix3 syscall vs interrupt

Post by nullplan »

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.
Carpe diem!
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: minix3 syscall vs interrupt

Post by iansjack »

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.
nullplan
Member
Member
Posts: 1789
Joined: Wed Aug 30, 2017 8:24 am

Re: minix3 syscall vs interrupt

Post by nullplan »

iansjack wrote: Tue Oct 22, 2024 11:38 pm System calls in x86_64 systems normally use the syscall/sysret instructions, which always clobber the RCX and R11 registers.
Ah, sorry, I had forgotten about that #-o . 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!
Post Reply