[Top] [All Lists]

Re: Is r25 saved across syscalls?

To: Rich Felker <>
Subject: Re: Is r25 saved across syscalls?
From: Ralf Baechle <>
Date: Tue, 11 Sep 2012 10:48:04 +0200
In-reply-to: <>
List-archive: <>
List-help: <>
List-id: linux-mips <>
List-owner: <>
List-post: <>
List-software: Ecartis version 1.0.0
List-subscribe: <>
List-unsubscribe: <>
References: <> <> <> <> <>
User-agent: Mutt/1.5.21 (2010-09-15)
On Mon, Sep 10, 2012 at 02:37:20PM -0400, Rich Felker wrote:

> When a restartable system call is interrupted by a signal, the kernel
> must arrange for it to restart after the signal handler returns.
> While some other obscure variants with trampolines are conceivable,
> the canonical way to do this is to set PC back to the syscall
> instruction with all the relevant registers preserved. MIPS is a bit
> peculiar in that the kernel sets PC back to the _previous_ instruction
> and requires that instruction to reload $2. This requirement is part
> of the syscall ABI in that failure of the application to properly
> reload $2 in this slot will cause unpredictable behavior when a
> syscall needs to be resumed after a signal.
> While I asked about preserving $25 in general, my actual concern is
> about the syscall restarting situation. I don't care if the value of
> $25 (or $7 in my alternate version) is lost once the syscall returns;
> I only care that the value is still there if the kernel decides to
> point PC back at the instruction before syscall in order to implement
> restarting.

Yes.  The kernel keeps a backup copy of $a3 around and uses it to restore
the old content of $a3 before returning to userland, even in old kernels.

A recent signal.c contains:

        if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
                regs->regs[2] = current->thread.abi->restart;
                regs->regs[7] = regs->regs[26];
                regs->cp0_epc -= 4;

Note that c0_epc is made to point back to the SYSCALL instruction,
not the one preceeding the SYSCALL instructions since 8f5a00eb4 [MIPS:
Sanitize restart logics] which went in for 2.6.36.

Relying on userland to reload $v0 was something ugly that Linux inherited
from god knows where and I'm happy to have gotten rid of that.

> The code I'm looking at seems to match what you cited from glibc.
> > >These are
> > >requirements beyond the normal function call convention (which does
> > >not require the callee preserve the values of r4-r7).
> > 
> > I would assume these are clobbered (from glibc sources
> > ports/sysdeps/unix/sysv/linux/mips/mips64/n64/sysdep.h):
> > 
> > "$1", "$3", "$10", "$11", "$12", "$13", "$14", "$15", "$24", "$25",
> > "hi", "lo"

Which is correct but also means that the _syscallX() macros that were in
<asm/unistd.h> up to 2.6.19 were broken; the were lacking clobbers for
$25, $hi and $lo.  Unfortunately these macros were copied into many
libraries and applications.


<Prev in Thread] Current Thread [Next in Thread>