Re: [PATCH v2 1/4] tools/nolibc: sys.h: add __syscall() and __sysret() helpers

From: Thomas Weißschuh
Date: Thu Jun 08 2023 - 12:06:25 EST


Hi David,

On 2023-06-08 14:35:49+0000, David Laight wrote:
> From: Zhangjin Wu
> > Sent: 06 June 2023 09:10
> >
> > most of the library routines share the same code model, let's add two
> > helpers to simplify the coding and shrink the code lines too.
> >
> ...
> > +/* Syscall return helper, set errno as -ret when ret < 0 */
> > +static inline __attribute__((always_inline)) long __sysret(long ret)
> > +{
> > + if (ret < 0) {
> > + SET_ERRNO(-ret);
> > + ret = -1;
> > + }
> > + return ret;
> > +}
>
> If that right?
> I thought that that only the first few (1024?) negative values
> got used as errno values.
>
> Do all Linux architectures even use negatives for error?
> I thought at least some used the carry flag.
> (It is the historic method of indicating a system call failure.)

I guess you are thinking about the architectures native systemcall ABI.

In nolibc these are abstracted away in the architecture-specific
assembly wrappers: my_syscall0 to my_syscall6.
(A good example would be arch-mips.h)

These normalize the architecture systemcall ABI to negative errornumbers
which then are returned from the sys_* wrapper functions.

The sys_* wrapper functions in turn are used by the libc function which
translate the negative error number to the libc-style
"return -1 and set errno" mechanism.
At this point the new __sysret function is used.

Returning negative error numbers in between has the advantage that it
can be used without having to set up a global/threadlocal errno
variable.

In hope this helped,
Thomas