Back to index

glibc  2.9
sysdep.h
Go to the documentation of this file.
00001 /* Assembler macros for 64 bit S/390.
00002    Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
00003    Free Software Foundation, Inc.
00004    Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
00005    This file is part of the GNU C Library.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; if not, write to the Free
00019    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020    02111-1307 USA.  */
00021 
00022 #ifndef _LINUX_S390_SYSDEP_H
00023 #define _LINUX_S390_SYSDEP_H
00024 
00025 #include <sysdeps/s390/s390-64/sysdep.h>
00026 #include <sysdeps/unix/sysdep.h>
00027 #include <dl-sysdep.h>      /* For RTLD_PRIVATE_ERRNO.  */
00028 #include <tls.h>
00029 
00030 /* For Linux we can use the system call table in the header file
00031        /usr/include/asm/unistd.h
00032    of the kernel.  But these symbols do not follow the SYS_* syntax
00033    so we have to redefine the `SYS_ify' macro here.  */
00034 /* In newer 2.1 kernels __NR_syscall is missing so we define it here.  */
00035 #define __NR_syscall 0
00036 
00037 /*
00038  * Newer kernel versions redefined __NR_pread and __NR_pwrite to
00039  * __NR_pread64 and __NR_pwrite64.
00040  */
00041 #ifndef __NR_pread
00042 # define __NR_pread __NR_pread64
00043 #endif
00044 #ifndef __NR_pwrite
00045 # define __NR_pwrite __NR_pwrite64
00046 #endif
00047 
00048 #undef SYS_ify
00049 #define SYS_ify(syscall_name)      __NR_##syscall_name
00050 
00051 #ifdef __ASSEMBLER__
00052 
00053 /* Linux uses a negative return value to indicate syscall errors, unlike
00054    most Unices, which use the condition codes' carry flag.
00055 
00056    Since version 2.1 the return value of a system call might be negative
00057    even if the call succeeded.     E.g., the `lseek' system call might return
00058    a large offset.  Therefore we must not anymore test for < 0, but test
00059    for a real error by making sure the value in gpr2 is a real error
00060    number.  Linus said he will make sure the no syscall returns a value
00061    in -1 .. -4095 as a valid result so we can savely test with -4095.  */
00062 
00063 #undef PSEUDO
00064 #define       PSEUDO(name, syscall_name, args)                              \
00065   .text;                                                             \
00066   ENTRY (name)                                                              \
00067     DO_CALL (syscall_name, args);                                    \
00068     lghi %r4,-4095 ;                                                 \
00069     clgr %r2,%r4 ;                                                   \
00070     jgnl SYSCALL_ERROR_LABEL
00071 
00072 #undef PSEUDO_END
00073 #define PSEUDO_END(name)                                             \
00074   SYSCALL_ERROR_HANDLER;                                             \
00075   END (name)
00076 
00077 #undef PSEUDO_NOERRNO
00078 #define       PSEUDO_NOERRNO(name, syscall_name, args)                      \
00079   .text;                                                             \
00080   ENTRY (name)                                                              \
00081     DO_CALL (syscall_name, args)
00082 
00083 #undef PSEUDO_END_NOERRNO
00084 #define PSEUDO_END_NOERRNO(name)                                     \
00085   SYSCALL_ERROR_HANDLER;                                             \
00086   END (name)
00087 
00088 #undef PSEUDO_ERRVAL
00089 #define       PSEUDO_ERRVAL(name, syscall_name, args)                              \
00090   .text;                                                             \
00091   ENTRY (name)                                                              \
00092     DO_CALL (syscall_name, args);                                    \
00093     lcgr %r2,%r2
00094 
00095 #undef PSEUDO_END_ERRVAL
00096 #define PSEUDO_END_ERRVAL(name)                                             \
00097   SYSCALL_ERROR_HANDLER;                                             \
00098   END (name)
00099 
00100 #ifndef PIC
00101 # define SYSCALL_ERROR_LABEL syscall_error
00102 # define SYSCALL_ERROR_HANDLER
00103 #else
00104 # if RTLD_PRIVATE_ERRNO
00105 #  define SYSCALL_ERROR_LABEL 0f
00106 #  define SYSCALL_ERROR_HANDLER \
00107 0:  larl  %r1,rtld_errno;                                            \
00108     lcr   %r2,%r2;                                                   \
00109     st    %r2,0(%r1);                                                       \
00110     lghi  %r2,-1;                                                    \
00111     br    %r14
00112 # elif defined _LIBC_REENTRANT
00113 #  if USE___THREAD
00114 #   ifndef NOT_IN_libc
00115 #    define SYSCALL_ERROR_ERRNO __libc_errno
00116 #   else
00117 #    define SYSCALL_ERROR_ERRNO errno
00118 #   endif
00119 #   define SYSCALL_ERROR_LABEL 0f
00120 #   define SYSCALL_ERROR_HANDLER \
00121 0:  lcr   %r0,%r2;                                                   \
00122     larl  %r1,SYSCALL_ERROR_ERRNO@indntpoff;                                \
00123     lg    %r1,0(%r1);                                                       \
00124     ear   %r2,%a0;                                                   \
00125     sllg  %r2,%r2,32;                                                       \
00126     ear   %r2,%a1;                                                   \
00127     st    %r0,0(%r1,%r2);                                            \
00128     lghi   %r2,-1;                                                   \
00129     br    %r14
00130 #  else
00131 #   define SYSCALL_ERROR_LABEL syscall_error@plt
00132 #   define SYSCALL_ERROR_HANDLER
00133 #  endif
00134 # else
00135 #  define SYSCALL_ERROR_LABEL 0f
00136 #  define SYSCALL_ERROR_HANDLER \
00137 0:  larl  %r1,_GLOBAL_OFFSET_TABLE_;                                        \
00138     lg    %r1,errno@GOT(%r1);                                               \
00139     lcr   %r2,%r2;                                                   \
00140     st    %r2,0(%r1);                                                       \
00141     lghi  %r2,-1;                                                    \
00142     br    %r14
00143 # endif /* _LIBC_REENTRANT */
00144 #endif /* PIC */
00145 
00146 /* Linux takes system call arguments in registers:
00147 
00148        syscall number       1           call-clobbered
00149        arg 1         2           call-clobbered
00150        arg 2         3           call-clobbered
00151        arg 3         4           call-clobbered
00152        arg 4         5           call-clobbered
00153        arg 5         6           call-saved
00154        arg 6         7           call-saved
00155 
00156    (Of course a function with say 3 arguments does not have entries for
00157    arguments 4 and 5.)
00158    For system calls with 6 parameters a stack operation is required
00159    to load the 6th parameter to register 7. Call saved register 7 is
00160    moved to register 0 and back to avoid an additional stack frame.
00161  */
00162 
00163 #define DO_CALL(syscall, args)                                              \
00164   .if args > 5;                                                             \
00165     lgr %r0,%r7;                                                     \
00166     lg %r7,160(%r15);                                                       \
00167   .endif;                                                            \
00168   .if SYS_ify (syscall) < 256;                                              \
00169     svc SYS_ify (syscall);                                           \
00170   .else;                                                             \
00171     lghi %r1,SYS_ify (syscall);                                             \
00172     svc 0;                                                           \
00173   .endif;                                                            \
00174   .if args > 5;                                                             \
00175     lgr %r7,%r0;                                                     \
00176   .endif
00177 
00178 #define ret                                                          \
00179     br     14
00180 
00181 #define ret_NOERRNO                                                  \
00182     br     14
00183 
00184 #define ret_ERRVAL                                                   \
00185     br     14
00186 
00187 #endif /* __ASSEMBLER__ */
00188 
00189 #undef INLINE_SYSCALL
00190 #define INLINE_SYSCALL(name, nr, args...)                            \
00191   ({                                                                 \
00192     long _ret = INTERNAL_SYSCALL (name, , nr, args);                        \
00193     if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (_ret, ), 0))            \
00194      {                                                               \
00195        __set_errno (INTERNAL_SYSCALL_ERRNO (_ret, ));                       \
00196        _ret = -1;                                                    \
00197      }                                                               \
00198     _ret; })
00199 
00200 #undef INTERNAL_SYSCALL_DECL
00201 #define INTERNAL_SYSCALL_DECL(err) do { } while (0)
00202 
00203 #undef INTERNAL_SYSCALL_DIRECT
00204 #define INTERNAL_SYSCALL_DIRECT(name, err, nr, args...)                     \
00205   ({                                                                 \
00206     DECLARGS_##nr(args)                                                     \
00207     register long _ret asm("2");                                     \
00208     asm volatile (                                                   \
00209     "svc    %b1\n\t"                                                 \
00210     : "=d" (_ret)                                                    \
00211     : "i" (__NR_##name) ASMFMT_##nr                                         \
00212     : "memory" );                                                    \
00213     _ret; })
00214 
00215 #undef INTERNAL_SYSCALL_SVC0
00216 #define INTERNAL_SYSCALL_SVC0(name, err, nr, args...)                       \
00217   ({                                                                 \
00218     DECLARGS_##nr(args)                                                     \
00219     register unsigned long _nr asm("1") = (unsigned long)(__NR_##name);            \
00220     register long _ret asm("2");                                     \
00221     asm volatile (                                                   \
00222     "svc    0\n\t"                                                   \
00223     : "=d" (_ret)                                                    \
00224     : "d" (_nr) ASMFMT_##nr                                          \
00225     : "memory" );                                                    \
00226     _ret; })
00227 
00228 #undef INTERNAL_SYSCALL_NCS
00229 #define INTERNAL_SYSCALL_NCS(no, err, nr, args...)                          \
00230   ({                                                                 \
00231     DECLARGS_##nr(args)                                                     \
00232     register unsigned long _nr asm("1") = (unsigned long)(no);              \
00233     register long _ret asm("2");                                     \
00234     asm volatile (                                                   \
00235     "svc    0\n\t"                                                   \
00236     : "=d" (_ret)                                                    \
00237     : "d" (_nr) ASMFMT_##nr                                          \
00238     : "memory" );                                                    \
00239     _ret; })
00240 
00241 #undef INTERNAL_SYSCALL
00242 #define INTERNAL_SYSCALL(name, err, nr, args...)                     \
00243   (((__NR_##name) < 256) ?                                           \
00244     INTERNAL_SYSCALL_DIRECT(name, err, nr, args) :                          \
00245     INTERNAL_SYSCALL_SVC0(name, err,nr, args))
00246 
00247 #undef INTERNAL_SYSCALL_ERROR_P
00248 #define INTERNAL_SYSCALL_ERROR_P(val, err)                                  \
00249   ((unsigned long) (val) >= -4095UL)
00250 
00251 #undef INTERNAL_SYSCALL_ERRNO
00252 #define INTERNAL_SYSCALL_ERRNO(val, err)  (-(val))
00253 
00254 #define DECLARGS_0()
00255 #define DECLARGS_1(arg1) \
00256        register unsigned long gpr2 asm ("2") = (unsigned long)(arg1);
00257 #define DECLARGS_2(arg1, arg2) \
00258        DECLARGS_1(arg1) \
00259        register unsigned long gpr3 asm ("3") = (unsigned long)(arg2);
00260 #define DECLARGS_3(arg1, arg2, arg3) \
00261        DECLARGS_2(arg1, arg2) \
00262        register unsigned long gpr4 asm ("4") = (unsigned long)(arg3);
00263 #define DECLARGS_4(arg1, arg2, arg3, arg4) \
00264        DECLARGS_3(arg1, arg2, arg3) \
00265        register unsigned long gpr5 asm ("5") = (unsigned long)(arg4);
00266 #define DECLARGS_5(arg1, arg2, arg3, arg4, arg5) \
00267        DECLARGS_4(arg1, arg2, arg3, arg4) \
00268        register unsigned long gpr6 asm ("6") = (unsigned long)(arg5);
00269 #define DECLARGS_6(arg1, arg2, arg3, arg4, arg5, arg6) \
00270        DECLARGS_5(arg1, arg2, arg3, arg4, arg5) \
00271        register unsigned long gpr7 asm ("7") = (unsigned long)(arg6);
00272 
00273 #define ASMFMT_0
00274 #define ASMFMT_1 , "0" (gpr2)
00275 #define ASMFMT_2 , "0" (gpr2), "d" (gpr3)
00276 #define ASMFMT_3 , "0" (gpr2), "d" (gpr3), "d" (gpr4)
00277 #define ASMFMT_4 , "0" (gpr2), "d" (gpr3), "d" (gpr4), "d" (gpr5)
00278 #define ASMFMT_5 , "0" (gpr2), "d" (gpr3), "d" (gpr4), "d" (gpr5), "d" (gpr6)
00279 #define ASMFMT_6 , "0" (gpr2), "d" (gpr3), "d" (gpr4), "d" (gpr5), "d" (gpr6), "d" (gpr7)
00280 
00281 /* Pointer mangling support.  */
00282 #if defined NOT_IN_libc && defined IS_IN_rtld
00283 /* We cannot use the thread descriptor because in ld.so we use setjmp
00284    earlier than the descriptor is initialized.  */
00285 #else
00286 /* For the time being just use stack_guard rather than a separate
00287    pointer_guard.  */
00288 # ifdef __ASSEMBLER__
00289 #  define PTR_MANGLE(reg, tmpreg) \
00290   ear     tmpreg,%a0;                     \
00291   sllg    tmpreg,tmpreg,32;        \
00292   ear     tmpreg,%a1;                     \
00293   xg      reg,STACK_GUARD(tmpreg)
00294 #  define PTR_MANGLE2(reg, tmpreg) \
00295   xg      reg,STACK_GUARD(tmpreg)
00296 #  define PTR_DEMANGLE(reg, tmpreg) PTR_MANGLE (reg, tmpreg)
00297 # else
00298 #  define PTR_MANGLE(var) \
00299   (var) = (void *) ((uintptr_t) (var) ^ THREAD_GET_POINTER_GUARD ())
00300 #  define PTR_DEMANGLE(var) PTR_MANGLE (var)
00301 # endif
00302 #endif
00303 
00304 #endif /* _LINUX_S390_SYSDEP_H */