1  /* Assembler macros for 64 bit S/390.
       2     Copyright (C) 2001-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #ifndef _LINUX_S390_SYSDEP_H
      20  #define _LINUX_S390_SYSDEP_H
      21  
      22  #include <sysdeps/s390/s390-64/sysdep.h>
      23  #include <sysdeps/unix/sysdep.h>
      24  #include <sysdeps/unix/sysv/linux/s390/sysdep.h>
      25  #include <sysdeps/unix/sysv/linux/sysdep.h>
      26  #include <dl-sysdep.h>	/* For RTLD_PRIVATE_ERRNO.  */
      27  #include <tls.h>
      28  
      29  /* For Linux we can use the system call table in the header file
      30  	/usr/include/asm/unistd.h
      31     of the kernel.  But these symbols do not follow the SYS_* syntax
      32     so we have to redefine the `SYS_ify' macro here.  */
      33  /* In newer 2.1 kernels __NR_syscall is missing so we define it here.  */
      34  #define __NR_syscall 0
      35  
      36  #undef SYS_ify
      37  #define SYS_ify(syscall_name)	__NR_##syscall_name
      38  
      39  #ifdef __ASSEMBLER__
      40  
      41  /* Linux uses a negative return value to indicate syscall errors, unlike
      42     most Unices, which use the condition codes' carry flag.
      43  
      44     Since version 2.1 the return value of a system call might be negative
      45     even if the call succeeded.	E.g., the `lseek' system call might return
      46     a large offset.  Therefore we must not anymore test for < 0, but test
      47     for a real error by making sure the value in gpr2 is a real error
      48     number.  Linus said he will make sure that no syscall returns a value
      49     in -1 .. -4095 as a valid result so we can safely test with -4095.  */
      50  
      51  #undef PSEUDO
      52  #define	PSEUDO(name, syscall_name, args)				      \
      53    .text;								      \
      54    ENTRY (name)								      \
      55      DO_CALL (syscall_name, args);					      \
      56      lghi %r4,-4095 ;							      \
      57      clgr %r2,%r4 ;							      \
      58      jgnl SYSCALL_ERROR_LABEL
      59  
      60  #undef PSEUDO_END
      61  #define PSEUDO_END(name)						      \
      62    SYSCALL_ERROR_HANDLER;						      \
      63    END (name)
      64  
      65  #undef PSEUDO_NOERRNO
      66  #define	PSEUDO_NOERRNO(name, syscall_name, args)			      \
      67    .text;								      \
      68    ENTRY (name)								      \
      69      DO_CALL (syscall_name, args)
      70  
      71  #undef PSEUDO_END_NOERRNO
      72  #define PSEUDO_END_NOERRNO(name)					      \
      73    SYSCALL_ERROR_HANDLER;						      \
      74    END (name)
      75  
      76  #undef PSEUDO_ERRVAL
      77  #define	PSEUDO_ERRVAL(name, syscall_name, args)				      \
      78    .text;								      \
      79    ENTRY (name)								      \
      80      DO_CALL (syscall_name, args);					      \
      81      lcgr %r2,%r2
      82  
      83  #undef PSEUDO_END_ERRVAL
      84  #define PSEUDO_END_ERRVAL(name)						      \
      85    SYSCALL_ERROR_HANDLER;						      \
      86    END (name)
      87  
      88  #undef SYSCALL_ERROR_LABEL
      89  #ifndef PIC
      90  # undef SYSCALL_ERROR_LABEL
      91  # define SYSCALL_ERROR_LABEL syscall_error
      92  # define SYSCALL_ERROR_HANDLER
      93  #else
      94  # if RTLD_PRIVATE_ERRNO
      95  #  undef SYSCALL_ERROR_LABEL
      96  #  define SYSCALL_ERROR_LABEL 0f
      97  #  define SYSCALL_ERROR_HANDLER \
      98  0:  larl  %r1,rtld_errno;						      \
      99      lcr   %r2,%r2;							      \
     100      st    %r2,0(%r1);							      \
     101      lghi  %r2,-1;							      \
     102      br    %r14
     103  # elif defined _LIBC_REENTRANT
     104  #  if IS_IN (libc)
     105  #   define SYSCALL_ERROR_ERRNO __libc_errno
     106  #  else
     107  #   define SYSCALL_ERROR_ERRNO errno
     108  #  endif
     109  #  undef SYSCALL_ERROR_LABEL
     110  #  define SYSCALL_ERROR_LABEL 0f
     111  #  define SYSCALL_ERROR_HANDLER \
     112  0:  lcr   %r0,%r2;							      \
     113      larl  %r1,SYSCALL_ERROR_ERRNO@indntpoff;				      \
     114      lg    %r1,0(%r1);							      \
     115      ear   %r2,%a0;							      \
     116      sllg  %r2,%r2,32;							      \
     117      ear   %r2,%a1;							      \
     118      st    %r0,0(%r1,%r2);						      \
     119      lghi   %r2,-1;							      \
     120      br    %r14
     121  # else
     122  #  undef SYSCALL_ERROR_LABEL
     123  #  define SYSCALL_ERROR_LABEL 0f
     124  #  define SYSCALL_ERROR_HANDLER \
     125  0:  larl  %r1,_GLOBAL_OFFSET_TABLE_;					      \
     126      lg    %r1,errno@GOT(%r1);						      \
     127      lcr   %r2,%r2;							      \
     128      st    %r2,0(%r1);							      \
     129      lghi  %r2,-1;							      \
     130      br    %r14
     131  # endif /* _LIBC_REENTRANT */
     132  #endif /* PIC */
     133  
     134  /* Linux takes system call arguments in registers:
     135  
     136  	syscall number	1	     call-clobbered
     137  	arg 1		2	     call-clobbered
     138  	arg 2		3	     call-clobbered
     139  	arg 3		4	     call-clobbered
     140  	arg 4		5	     call-clobbered
     141  	arg 5		6	     call-saved
     142  	arg 6		7	     call-saved
     143  
     144     (Of course a function with say 3 arguments does not have entries for
     145     arguments 4 and 5.)
     146     For system calls with 6 parameters a stack operation is required
     147     to load the 6th parameter to register 7. Call saved register 7 is
     148     moved to register 0 and back to avoid an additional stack frame.
     149   */
     150  
     151  #define DO_CALL(syscall, args)						      \
     152    .if args > 5;								      \
     153      lgr %r0,%r7;							      \
     154      lg %r7,160(%r15);							      \
     155    .endif;								      \
     156      lghi %r1,SYS_ify (syscall);						      \
     157      svc 0;								      \
     158    .if args > 5;								      \
     159      lgr %r7,%r0;							      \
     160    .endif
     161  
     162  #define ret								      \
     163      br	    14
     164  
     165  #define ret_NOERRNO							      \
     166      br	    14
     167  
     168  #define ret_ERRVAL							      \
     169      br	    14
     170  
     171  #else
     172  
     173  # undef HAVE_INTERNAL_BRK_ADDR_SYMBOL
     174  # define HAVE_INTERNAL_BRK_ADDR_SYMBOL 1
     175  
     176  #endif /* __ASSEMBLER__ */
     177  
     178  #endif /* _LINUX_S390_SYSDEP_H */