Hi!

I know next to nothing about LoongArch, but generally, the informal
principle appears to be: make syscalls look a lot like regular
function calls, except pass the syscall number in a register not
normally used for arguments. For AArch64, that's x8, because regular
arguments are passed in x0-x7. On x86_64, that's rax, which is also
not normally used for passing args.

Quickly looking at LoongArch64, it looks like r4-r11 (a0-a7) are all
used by the calling convention to pass values. That would make r12
(t0) the natural syscall number register.

> I've communicate with LoongArch engineer, he advised not to use $t0 because 
> it might be overwritten.

Well, as said, I don't know anything about LoongArch so I can't judge
what he meant exactly. If the point is just that t0 is a temporary
(caller-saved) register, than that should not be a concern. The kernel
can preserve whatever it wants (in fact it works out better for the
kernel to preserve all registers over syscalls except the one used for
the return value, since the thread state may also be saved when a
thread is interrupted involuntarily), and a* registers are also
equally temporary. But a LoongArch engineer would certainly know and
understand that better than I do (and if you're working on a port, you
do too); perhaps there is a different mechanism in LoongArch that
makes t0 unsuitable here.

> On the other hand, if we use $a7, vm_map() would need one additional
> register move: we would have to move the original value of $a7 to the
> stack before putting the syscall number in $a7.

Consider also that syscall bodies (in userland) are not implemented
individually, but instead generated from Mach headers, specifically
<syscall_sw.h>. You could define the kernel_trap macro differently
depending on 'number_args', but it makes things simpler if you don't
have to.

Sergey

Reply via email to