Back to index

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