Only pass the minimum number of syscall arguments

Previously, __internal_syscall() compiled into asm-code that unconditionally
sets the syscall argument registers a0 to a5.

For example, the instruction sequence for a exit syscall looked like
this:

    li    a0, 1   # in ther caller of exit()
    # ...         # in newlib:
    li    a1, 0   # unused arguments
    li    a2, 0
    li    a3, 0
    li    a4, 0
    li    a5, 0
    li    a7, 93  # exit syscall number

(i.e. the binary contains then 5 superfluous instructions for this
one argument syscall)

This commit changes the RISC-V syscall code such that only the required
syscall argument registers are set.

GCC detects that argc is known at compile time and thus evaluates all the
if-statements where argc is used at compile time (tested with -O2 and -Os).
This commit is contained in:
Georg Sauthoff
2020-02-10 21:19:58 +01:00
committed by Corinna Vinschen
parent 2379142bc5
commit 9b51beeb2a
18 changed files with 49 additions and 30 deletions

View File

@ -22,31 +22,50 @@ __syscall_error(long a0)
}
static inline long
__internal_syscall(long n, long _a0, long _a1, long _a2, long _a3, long _a4, long _a5)
__internal_syscall(long n, int argc, long _a0, long _a1, long _a2, long _a3, long _a4, long _a5)
{
register long a0 asm("a0") = _a0;
register long a1 asm("a1") = _a1;
register long a2 asm("a2") = _a2;
register long a3 asm("a3") = _a3;
register long a4 asm("a4") = _a4;
register long a5 asm("a5") = _a5;
#ifdef __riscv_32e
register long syscall_id asm("t0") = n;
#else
register long syscall_id asm("a7") = n;
#endif
asm volatile ("scall"
: "+r"(a0) : "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5), "r"(syscall_id));
register long a0 asm("a0") = _a0;
if (argc < 2) {
asm volatile ("ecall" : "+r"(a0) : "r"(syscall_id));
return a0;
}
register long a1 asm("a1") = _a1;
if (argc == 2) {
asm volatile ("ecall" : "+r"(a0) : "r"(a1), "r"(syscall_id));
return a0;
}
register long a2 asm("a2") = _a2;
if (argc == 3) {
asm volatile ("ecall" : "+r"(a0) : "r"(a1), "r"(a2), "r"(syscall_id));
return a0;
}
register long a3 asm("a3") = _a3;
if (argc == 4) {
asm volatile ("ecall" : "+r"(a0) : "r"(a1), "r"(a2), "r"(a3), "r"(syscall_id));
return a0;
}
register long a4 asm("a4") = _a4;
if (argc == 5) {
asm volatile ("ecall" : "+r"(a0) : "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(syscall_id));
return a0;
}
register long a5 asm("a5") = _a5;
asm volatile ("ecall" : "+r"(a0) : "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5), "r"(syscall_id));
return a0;
}
static inline long
syscall_errno(long n, long _a0, long _a1, long _a2, long _a3, long _a4, long _a5)
syscall_errno(long n, int argc, long _a0, long _a1, long _a2, long _a3, long _a4, long _a5)
{
long a0 = __internal_syscall (n, _a0, _a1, _a2, _a3, _a4, _a5);
long a0 = __internal_syscall (n, argc, _a0, _a1, _a2, _a3, _a4, _a5);
if (a0 < 0)
return __syscall_error (a0);