[PATCH v2 08/14] LoongArch: Linux Syscall Interface

Adhemerval Zanella adhemerval.zanella@linaro.org
Tue Jan 4 14:20:37 GMT 2022



On 31/12/2021 03:44, caiyinyu wrote:
> Contains the Linux system call interface, as well as the definitions of
> a handful of system calls.
> ---
>  sysdeps/loongarch/abort-instr.h               |   2 +
>  sysdeps/loongarch/hp-timing.h                 |  42 +++
>  sysdeps/loongarch/nptl/nptl-sysdep.S          |   2 +
>  .../unix/sysv/linux/loongarch/arch-syscall.h  | 302 ++++++++++++++++
>  .../unix/sysv/linux/loongarch/bits/signum.h   |  58 ++++
>  sysdeps/unix/sysv/linux/loongarch/clone.S     | 100 ++++++
>  sysdeps/unix/sysv/linux/loongarch/clone3.S    |  87 +++++
>  sysdeps/unix/sysv/linux/loongarch/ipc_priv.h  |  22 ++
>  sysdeps/unix/sysv/linux/loongarch/syscall.c   |  35 ++
>  sysdeps/unix/sysv/linux/loongarch/sysdep.S    |  53 +++
>  sysdeps/unix/sysv/linux/loongarch/sysdep.h    | 321 ++++++++++++++++++
>  sysdeps/unix/sysv/linux/loongarch/vfork.S     |  50 +++
>  12 files changed, 1074 insertions(+)
>  create mode 100644 sysdeps/loongarch/abort-instr.h
>  create mode 100644 sysdeps/loongarch/hp-timing.h
>  create mode 100644 sysdeps/loongarch/nptl/nptl-sysdep.S
>  create mode 100644 sysdeps/unix/sysv/linux/loongarch/arch-syscall.h
>  create mode 100644 sysdeps/unix/sysv/linux/loongarch/bits/signum.h
>  create mode 100644 sysdeps/unix/sysv/linux/loongarch/clone.S
>  create mode 100644 sysdeps/unix/sysv/linux/loongarch/clone3.S
>  create mode 100644 sysdeps/unix/sysv/linux/loongarch/ipc_priv.h
>  create mode 100644 sysdeps/unix/sysv/linux/loongarch/syscall.c
>  create mode 100644 sysdeps/unix/sysv/linux/loongarch/sysdep.S
>  create mode 100644 sysdeps/unix/sysv/linux/loongarch/sysdep.h
>  create mode 100644 sysdeps/unix/sysv/linux/loongarch/vfork.S
> 
> diff --git a/sysdeps/loongarch/abort-instr.h b/sysdeps/loongarch/abort-instr.h
> new file mode 100644
> index 0000000000..92e22edfea
> --- /dev/null
> +++ b/sysdeps/loongarch/abort-instr.h
> @@ -0,0 +1,2 @@
> +/* An instruction which should crash any program is a breakpoint.  */
> +#define ABORT_INSTRUCTION asm("break 0")

Missing space after asm.

> diff --git a/sysdeps/loongarch/hp-timing.h b/sysdeps/loongarch/hp-timing.h
> new file mode 100644
> index 0000000000..7b36a539f1
> --- /dev/null
> +++ b/sysdeps/loongarch/hp-timing.h
> @@ -0,0 +1,42 @@
> +/* High precision, low overhead timing functions.
> +   Copyright (C) 2021 Free Software Foundation, Inc.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#ifndef _HP_TIMING_H
> +#define _HP_TIMING_H 1
> +
> +/* We always assume having the timestamp register.  */
> +#define HP_TIMING_AVAIL (1)
> +#define HP_SMALL_TIMING_AVAIL (1)
> +
> +/* We indeed have inlined functions.  */
> +#define HP_TIMING_INLINE (1)
> +
> +/* We use 64bit values for the times.  */
> +typedef unsigned long long int hp_timing_t;
> +
> +/* Read the stable counter.  */
> +#define HP_TIMING_NOW(Var) \
> +  ({ \
> +    unsigned long long int _count; \
> +    asm volatile("rdtime.d\t%0,$r0" : "=r"(_count)); \

Missing space after volatile.

> +    (Var) = _count; \
> +  })
> +
> +#include <hp-timing-common.h>
> +
> +#endif /* hp-timing.h */
> diff --git a/sysdeps/loongarch/nptl/nptl-sysdep.S b/sysdeps/loongarch/nptl/nptl-sysdep.S
> new file mode 100644
> index 0000000000..3f5c2a364a
> --- /dev/null
> +++ b/sysdeps/loongarch/nptl/nptl-sysdep.S
> @@ -0,0 +1,2 @@
> +/* Pull in __syscall_error.  */
> +#include <sysdep.S>

Why do you need this file?  Newer Linux ports should have libpthread function
on libc.so.

> diff --git a/sysdeps/unix/sysv/linux/loongarch/bits/signum.h b/sysdeps/unix/sysv/linux/loongarch/bits/signum.h
> new file mode 100644
> index 0000000000..111759778b
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/loongarch/bits/signum.h
> @@ -0,0 +1,58 @@
> +/* Signal number definitions.  Linux version.
> +   Copyright (C) 2021 Free Software Foundation, Inc.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#ifndef _BITS_SIGNUM_H
> +#define _BITS_SIGNUM_H 1
> +
> +#ifndef _SIGNAL_H
> +#error "Never include <bits/signum.h> directly; use <signal.h> instead."
> +#endif
> +
> +#include <bits/signum-generic.h>
> +
> +/* Adjustments and additions to the signal number constants for
> +   most Linux systems.  */
> +
> +#define SIGSTKFLT 16 /* Stack fault (obsolete).  */
> +#define SIGPWR 30    /* Power failure imminent.  */
> +
> +#undef SIGBUS
> +#define SIGBUS 7
> +#undef SIGUSR1
> +#define SIGUSR1 10
> +#undef SIGUSR2
> +#define SIGUSR2 12
> +#undef SIGCHLD
> +#define SIGCHLD 17
> +#undef SIGCONT
> +#define SIGCONT 18
> +#undef SIGSTOP
> +#define SIGSTOP 19
> +#undef SIGTSTP
> +#define SIGTSTP 20
> +#undef SIGURG
> +#define SIGURG 23
> +#undef SIGPOLL
> +#define SIGPOLL 29
> +#undef SIGSYS
> +#define SIGSYS 31
> +
> +#undef __SIGRTMAX
> +#define __SIGRTMAX 64
> +
> +#endif /* <signal.h> included.  */

This file has been refactore by e4e11b1dba261cb650e6 (2.32), now ports that
deviate from Linux generic ABI should provide a signum-arch.h header. And
this seems not to be the case here.


> diff --git a/sysdeps/unix/sysv/linux/loongarch/clone.S b/sysdeps/unix/sysv/linux/loongarch/clone.S
> new file mode 100644
> index 0000000000..d2bed24440
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/loongarch/clone.S
> @@ -0,0 +1,100 @@
> +/* The clone syscall wrapper.
> +   Copyright (C) 2021 Free Software Foundation, Inc.
> +
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library.  If not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +/* clone() is even more special than fork() as it mucks with stacks
> +   and invokes a function in the right context after its all over.  */
> +
> +#include <sys/asm.h>
> +#include <sysdep.h>
> +#define _ERRNO_H  1
> +#include <bits/errno.h>
> +#include <tls.h>
> +#include "tcb-offsets.h"
> +
> +/* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg,
> +   void *parent_tidptr, void *tls, void *child_tidptr) */
> +
> +ENTRY (__clone)
> +
> +	/* Align stack to 16 or 8 bytes per the ABI.  */
> +#ifdef __loongarch_lp64
> +	bstrins.d	a1, zero, 3, 0
> +#else
> +#error "32bit LoongArch systems are not supported"
> +#endif

As before, I think we should move the LP64 ABI tests to configure time
instead of adding multiple checks on arch-specific code.

> +
> +	/* Sanity check arguments.  */
> +	beqz		a0, L (invalid) /* No NULL function pointers.  */
> +	beqz		a1, L (invalid) /* No NULL stack pointers.  */
> +
> +	addi.d 		a1, a1, -16 /* Reserve argument save space.  */
> +	st.d		a0, a1, 0   /* Save function pointer.  */
> +	st.d		a3, a1, SZREG   /* Save argument pointer.  */
> +
> +	/* The syscall expects the args to be in different slots.  */
> +	or		a0, a2, zero
> +	or		a2, a4, zero
> +	or		a3, a6, zero
> +	or		a4, a5, zero
> +
> +	/* Do the system call.  */
> +	li.d		a7,__NR_clone
> +	syscall		0
> +
> +	blt		a0, zero ,L (error)
> +	beqz		a0,L (thread_start)
> +
> +	/* Successful return from the parent.  */
> +	ret
> +
> +L (invalid):
> +	li.d		a0, -EINVAL
> +
> +	/* Something bad happened -- no child created.  */
> +L (error):
> +	b		__syscall_error
> +
> +END (__clone)
> +
> +/* Load up the arguments to the function.  Put this block of code in
> +   its own function so that we can terminate the stack trace with our
> +   debug info.  */
> +ENTRY (__thread_start)
> +L (thread_start):
> +
> +/* Terminate call stack by noting ra is undefined.  Use a dummy
> +   .cfi_label to force starting the FDE.  */
> +	.cfi_label .Ldummy
> +	cfi_undefined (1)
> +
> +	/* Restore the arg for user's function.  */
> +	ld.d		a1, sp, 0   /* Function pointer.  */
> +	ld.d		a0, sp, SZREG   /* Argument pointer.  */
> +
> +	/* Call the user's function.  */
> +	jirl		ra, a1, 0
> +
> +	/* Call exit with the function's return value.  */
> +	li.d		a7, __NR_exit
> +	syscall		0
> +
> +	END (__thread_start)
> +
> +libc_hidden_def (__clone)
> +weak_alias (__clone, clone)
> diff --git a/sysdeps/unix/sysv/linux/loongarch/ipc_priv.h b/sysdeps/unix/sysv/linux/loongarch/ipc_priv.h
> new file mode 100644
> index 0000000000..dde8fa6989
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/loongarch/ipc_priv.h
> @@ -0,0 +1,22 @@
> +/* Old SysV permission definition for Linux.
> +   Copyright (C) 2021 Loongson Technology, Inc.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <sys/ipc.h> /* For __key_t  */
> +
> +#define __IPC_64 0x0
> +#include <sysdeps/unix/sysv/linux/ipc_priv.h>

Why can't you use the generic sysdeps/unix/sysv/linux/ipc_priv.h here?
I woudl asusme that LoongArch will have __ASSUME_SYSVIPC_DEFAULT_IPC_64
defined and thus __IPC_64 will be 0x0.

> diff --git a/sysdeps/unix/sysv/linux/loongarch/syscall.c b/sysdeps/unix/sysv/linux/loongarch/syscall.c
> new file mode 100644
> index 0000000000..d0067bc2d0
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/loongarch/syscall.c
> @@ -0,0 +1,35 @@
> +/* system call interface.
> +   Copyright (C) 2021 Free Software Foundation, Inc.
> +
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library.  If not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <sysdep.h>
> +
> +long int
> +syscall (long int syscall_number, long int arg1, long int arg2, long int arg3,
> +	 long int arg4, long int arg5, long int arg6, long int arg7)
> +{
> +  long int ret;
> +
> +  ret = INTERNAL_SYSCALL_NCS (syscall_number, 7, arg1, arg2, arg3, arg4, arg5,
> +			      arg6, arg7);
> +
> +  if (INTERNAL_SYSCALL_ERROR_P (ret))
> +    return __syscall_error (ret);
> +
> +  return ret;
> +}

Do you really need 7 arguments for syscall? Otherwise you should use the generic
interface.


More information about the Libc-alpha mailing list