9

Context: I am trying to write a small C program with inline asm that should run under Linux on an x86_64 system and being compiled with gcc in order to better understand how syscalls work under Linux.

My question is: How are error numbers returned from a syscall (e.g. write) in this environment? I understand that when I use a library such as glibc, it takes care of saving the resulting error code in the global errno variable. But where is the error number stored when I call a syscall directly through inline assembler? Will it be stored inside a separate register, or will it be encoded in %rax?

Let take the write syscall on linux as an example:

When call write then after the syscall returns I find it stores 0xfffffffffffffff2 inside %rax, do I need to somehow extract the error code from that?

If I have the error code number, where should I look to identify the actual error that occured? Lets say I get the number 5 returned, which header file do I need to consult to find the corresponding symbolic error name.

I am calling the write syscall like this:

asm ("mov $1,%%rax;"
     "mov $1,%%rdi;"
     "mov %1,%%rsi;"
     "mov %2,%%rdx;"
     "syscall;"
     "mov %%rax,%0;"
     : "=r" (result)
     : "r" (msg), "r" (len)
     : "%rdx", "%rsi", "%rax", "%rdi" /* EDIT: this is needed or else the registers will be overwritten */
    );

with result, msg and len defined like so:

long result = 0;
char* msg = "Hello World\n";
long len = 12;
lanoxx
  • 12,249
  • 13
  • 87
  • 142
  • after the asm call to syscall, check the content of errno, it should still be in it. – Pierre Emmanuel Lallemant May 11 '16 at 15:32
  • @PierreEmmanuelLallemant No `errno` is defined by glibc or what ever C standard library is being used. If I am not using such a library then I need to manually get the error number from a register. Trying to use `errno` gives me the following compile error: **error: ‘errno’ undeclared (first use in this function)** – lanoxx May 11 '16 at 15:36
  • 2
    I have very (20 years) old knowledge of this, so this might not be true anymore but Linux used to (and still sometimes does) treat any negative return value as an error and the absolute value of that is the errno. – Art May 11 '16 at 15:55
  • 1
    The posted error is EFAULT – stark May 11 '16 at 17:28
  • @Art: you are still right. – Pierre Emmanuel Lallemant May 11 '16 at 18:55
  • But the "errno" used in the kernel may be different that the errno wrapped by the C wrap function. – Pierre Emmanuel Lallemant May 11 '16 at 18:57
  • @Art: IIRC, it was any number between -1 and -4095 – ninjalj May 11 '16 at 18:58
  • @PierreEmmanuelLallemant: any example besides `close()` `EINPROGRESS/EINTR` ? – ninjalj May 11 '16 at 19:01
  • @ninjalj: i don't understand, can you explain what i'm wrong with ? :/ – Pierre Emmanuel Lallemant May 11 '16 at 20:06
  • 1
    You said the "errno" used in the kernel may be different that the errno wrapped by the C wrap function, but (after trivial conversion) the only case I can think of is `close()`, which in Linux returns `EINTR` for a failure case that now POSIX says should be `EINPROGRESS`: http://ewontfix.com/4/ – ninjalj May 11 '16 at 20:52

4 Answers4

11

The Linux syscall's convention is that they encode both the possible error code and the return value for successful call in the return value. It's just glibc or other C libraries's wrappers that they will set errno to the error code returned by the underlying syscall, and the wrapper will return -1. Taking the write as an example, the kernel does the error processing similar to this:

ssize_t write(int fd, ...) {
    if (fd is not valid)
         return -EBADF;
    return do_write(...);
}

So as you can see, the error code is just in the return value, and depending on the semantics, there is always a way to check if the syscall succeeded or not by comparing it to a value not possible for successful operation. For most syscalls, like write, that means check if it is negative.

Laurel
  • 5,965
  • 14
  • 31
  • 57
fluter
  • 13,238
  • 8
  • 62
  • 100
  • 2
    You can check for errors from *any* system call by checking for `-4095..-1`. The last page of memory is reserved so you can't mmap or have the kernel use it internally so that this range is never a valid address. [What are the return values of system calls in Assembly?](https://stackoverflow.com/q/38751614) – Peter Cordes Nov 01 '18 at 11:25
6

Architecture Calling Conventions

As you already guessed, you can't use errno because it's GLibC specific. The information you want will be in rax if it's a x86_64. The man page man 2 syscall has the following explanation:

Architecture calling conventions

       Every architecture has its own way of invoking and passing arguments
       to the kernel.  The details for various architectures are listed in
       the two tables below.

       The first table lists the instruction used to transition to kernel
       mode (which might not be the fastest or best way to transition to the
       kernel, so you might have to refer to vdso(7)), the register used to
       indicate the system call number, the register used to return the
       system call result, and the register used to signal an error.

       arch/ABI    instruction           syscall #  retval  error    Notes
       ────────────────────────────────────────────────────────────────────
       alpha       callsys               v0         a0      a3       [1]
       arc         trap0                 r8         r0      -
       arm/OABI    swi NR                -          a1      -        [2]
       arm/EABI    swi 0x0               r7         r0      -
       arm64       svc #0                x8         x0      -
       blackfin    excpt 0x0             P0         R0      -
       i386        int $0x80             eax        eax     -
       ia64        break 0x100000        r15        r8      r10      [1]
       m68k        trap #0               d0         d0      -
       microblaze  brki r14,8            r12        r3      -
       mips        syscall               v0         v0      a3       [1]
       nios2       trap                  r2         r2      r7
       parisc      ble 0x100(%sr2, %r0)  r20        r28     -
       powerpc     sc                    r0         r3      r0       [1]
       s390        svc 0                 r1         r2      -        [3]
       s390x       svc 0                 r1         r2      -        [3]
       superh      trap #0x17            r3         r0      -        [4]
       sparc/32    t 0x10                g1         o0      psr/csr  [1]
       sparc/64    t 0x6d                g1         o0      psr/csr  [1]
       tile        swint1                R10        R00     R01      [1]
       x86_64      syscall               rax        rax     -        [5]
       x32         syscall               rax        rax     -        [5]
       xtensa      syscall               a2         a2      -

And note number [5]:

[5] The x32 ABI uses the same instruction as the x86_64 ABI and
               is used on the same processors.  To differentiate between
               them, the bit mask __X32_SYSCALL_BIT is bitwise-ORed into the
               system call number for system calls under the x32 ABI.  Both
               system call tables are available though, so setting the bit
               is not a hard requirement.

(In that man page, a table showing how to pass arguments to system calls follows. It's an interesting read.)


How are error numbers returned from a syscall (e.g. write) in this environment?:

You gotta check your rax register for the return value.

Enzo Ferber
  • 3,029
  • 1
  • 14
  • 24
  • Thanks, I also saw this table, but IMHO the missing clue was that the error number is returned as a negative of the actual error number. – lanoxx May 12 '16 at 08:09
4

On Linux, a failed system call using the syscall assembly instruction will return the value -errno in the rax register. So in your case 0-0xfffffffffffffff2 == 0xE which is 14. So your errno is 14.

How do you find what errno 14 means? You should google search "Linux error code table" or look in errno.h and you'll find the answer.

Take a look here: http://www.virtsync.com/c-error-codes-include-errno

According to that table, 14 is EFAULT which means "Bad address".

David Yeager
  • 596
  • 5
  • 9
  • While the link would eventually go stale, it can be fixed by community edits, and is great for backing up your answer. – RamenChef Nov 02 '16 at 18:33
1

IIRC in the x86-64 ABI, an error is transmitted from the syscall with the carry bit set. Then eax contains the errno code.

I would suggest to study the lower layers of the source code of some libc library, like musl-libc

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547