Commit nios2 port to master.
[platform/upstream/glibc.git] / sysdeps / unix / sysv / linux / nios2 / sysdep.h
1 /* Assembler macros for Nios II.
2    Copyright (C) 2000-2015 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    <http://www.gnu.org/licenses/>.  */
18
19 #ifndef _LINUX_NIOS2_SYSDEP_H
20 #define _LINUX_NIOS2_SYSDEP_H 1
21
22 #include <asm/unistd.h>
23 #include <sysdeps/unix/sysdep.h>
24 #include <sysdeps/nios2/sysdep.h>
25 #include <sysdeps/unix/sysv/linux/generic/sysdep.h>
26
27 /* For RTLD_PRIVATE_ERRNO.  */
28 #include <dl-sysdep.h>
29
30 #include <tls.h>
31
32 /* For Linux we can use the system call table in the header file
33         /usr/include/asm/unistd.h
34    of the kernel.  But these symbols do not follow the SYS_* syntax
35    so we have to redefine the `SYS_ify' macro here.  */
36 #undef SYS_ify
37 #define SYS_ify(syscall_name)   __NR_##syscall_name
38
39 #ifdef __ASSEMBLER__
40
41 #define SYSCALL_ERROR_LABEL __local_syscall_error
42
43 #undef PSEUDO
44 #define PSEUDO(name, syscall_name, args) \
45   ENTRY (name)                           \
46     DO_CALL (syscall_name, args)         \
47     bne r7, zero, SYSCALL_ERROR_LABEL;   \
48
49 #undef PSEUDO_END
50 #define PSEUDO_END(name) \
51   SYSCALL_ERROR_HANDLER  \
52   END (name)
53
54 #undef PSEUDO_NOERRNO
55 #define PSEUDO_NOERRNO(name, syscall_name, args) \
56   ENTRY (name)                                   \
57     DO_CALL (syscall_name, args)
58
59 #undef PSEUDO_END_NOERRNO
60 #define PSEUDO_END_NOERRNO(name) \
61   END (name)
62
63 #undef ret_NOERRNO
64 #define ret_NOERRNO ret
65
66 #undef DO_CALL
67 #define DO_CALL(syscall_name, args) \
68     DOARGS_##args                   \
69     movi r2, SYS_ify(syscall_name);  \
70     trap;
71
72 #if defined(__PIC__) || defined(PIC)
73
74 # if RTLD_PRIVATE_ERRNO
75
76 #  define SYSCALL_ERROR_HANDLER                 \
77   SYSCALL_ERROR_LABEL:                          \
78   nextpc r3;                                    \
79 1:                                              \
80   movhi r8, %hiadj(rtld_errno - 1b);            \
81   addi r8, r8, %lo(rtld_errno - 1b);            \
82   add r3, r3, r8;                               \
83   stw r2, 0(r3);                                \
84   movi r2, -1;                                  \
85   ret;
86
87 # else
88
89 #  if IS_IN (libc)
90 #   define SYSCALL_ERROR_ERRNO __libc_errno
91 #  else
92 #   define SYSCALL_ERROR_ERRNO errno
93 #  endif
94 #  define SYSCALL_ERROR_HANDLER                 \
95   SYSCALL_ERROR_LABEL:                          \
96   nextpc r3;                                    \
97 1:                                              \
98   movhi r8, %hiadj(_gp_got - 1b);               \
99   addi r8, r8, %lo(_gp_got - 1b);               \
100   add r3, r3, r8;                               \
101   ldw r3, %tls_ie(SYSCALL_ERROR_ERRNO)(r3);     \
102   add r3, r23, r3;                              \
103   stw r2, 0(r3);                                \
104   movi r2, -1;                                  \
105   ret;
106
107 # endif
108
109 #else
110
111 /* We can use a single error handler in the static library.  */
112 #define SYSCALL_ERROR_HANDLER                   \
113   SYSCALL_ERROR_LABEL:                          \
114   jmpi __syscall_error;
115
116 #endif
117
118 #define DOARGS_0 /* nothing */
119 #define DOARGS_1 /* nothing */
120 #define DOARGS_2 /* nothing */
121 #define DOARGS_3 /* nothing */
122 #define DOARGS_4 /* nothing */
123 #define DOARGS_5 ldw r8, 0(sp);
124 #define DOARGS_6 ldw r9, 4(sp); ldw r8, 0(sp);
125
126 /* The function has to return the error code.  */
127 #undef  PSEUDO_ERRVAL
128 #define PSEUDO_ERRVAL(name, syscall_name, args) \
129   ENTRY (name)                                  \
130     DO_CALL (syscall_name, args)
131
132 #undef  PSEUDO_END_ERRVAL
133 #define PSEUDO_END_ERRVAL(name) \
134   END (name)
135
136 #define ret_ERRVAL ret
137
138 #else /* __ASSEMBLER__ */
139
140 /* In order to get __set_errno() definition in INLINE_SYSCALL.  */
141 #include <errno.h>
142
143 /* Define a macro which expands into the inline wrapper code for a system
144    call.  */
145 #undef INLINE_SYSCALL
146 #define INLINE_SYSCALL(name, nr, args...)                               \
147   ({ INTERNAL_SYSCALL_DECL(err);                                        \
148      unsigned int result_var = INTERNAL_SYSCALL (name, err, nr, args);  \
149      if ( INTERNAL_SYSCALL_ERROR_P (result_var, err) )                  \
150        {                                                                \
151          __set_errno (INTERNAL_SYSCALL_ERRNO (result_var, err));        \
152          result_var = -1L;                                              \
153        }                                                                \
154      (int) result_var; })
155
156 #undef INTERNAL_SYSCALL_DECL
157 #define INTERNAL_SYSCALL_DECL(err) unsigned int err __attribute__((unused))
158
159 #undef INTERNAL_SYSCALL_ERROR_P
160 #define INTERNAL_SYSCALL_ERROR_P(val, err) ((void) (val), (unsigned int) (err))
161
162 #undef INTERNAL_SYSCALL_ERRNO
163 #define INTERNAL_SYSCALL_ERRNO(val, err)   ((void) (err), val)
164
165 #undef INTERNAL_SYSCALL_RAW
166 #define INTERNAL_SYSCALL_RAW(name, err, nr, args...)            \
167   ({ unsigned int _sys_result;                                  \
168      {                                                          \
169        /* Load argument values in temporary variables
170           to perform side effects like function calls
171           before the call-used registers are set.  */           \
172        LOAD_ARGS_##nr (args)                                    \
173        LOAD_REGS_##nr                                           \
174        register int _r2 asm ("r2") = (int)(name);               \
175        register int _err asm ("r7");                            \
176        asm volatile ("trap"                                     \
177                      : "+r" (_r2), "=r" (_err)                  \
178                      : ASM_ARGS_##nr                            \
179                      : __SYSCALL_CLOBBERS);                     \
180        _sys_result = _r2;                                       \
181        err = _err;                                              \
182      }                                                          \
183      (int) _sys_result; })
184
185 #undef INTERNAL_SYSCALL
186 #define INTERNAL_SYSCALL(name, err, nr, args...) \
187         INTERNAL_SYSCALL_RAW(SYS_ify(name), err, nr, args)
188
189 #undef INTERNAL_SYSCALL_NCS
190 #define INTERNAL_SYSCALL_NCS(number, err, nr, args...) \
191         INTERNAL_SYSCALL_RAW(number, err, nr, args)
192
193 #define LOAD_ARGS_0()
194 #define LOAD_REGS_0
195 #define ASM_ARGS_0
196 #define LOAD_ARGS_1(a1)                         \
197   LOAD_ARGS_0 ()                                \
198   int __arg1 = (int) (a1);
199 #define LOAD_REGS_1                             \
200   register int _r4 asm ("r4") = __arg1;         \
201   LOAD_REGS_0
202 #define ASM_ARGS_1                  "r" (_r4)
203 #define LOAD_ARGS_2(a1, a2)                     \
204   LOAD_ARGS_1 (a1)                              \
205   int __arg2 = (int) (a2);
206 #define LOAD_REGS_2                             \
207   register int _r5 asm ("r5") = __arg2;         \
208   LOAD_REGS_1
209 #define ASM_ARGS_2      ASM_ARGS_1, "r" (_r5)
210 #define LOAD_ARGS_3(a1, a2, a3)                 \
211   LOAD_ARGS_2 (a1, a2)                          \
212   int __arg3 = (int) (a3);
213 #define LOAD_REGS_3                             \
214   register int _r6 asm ("r6") = __arg3;         \
215   LOAD_REGS_2
216 #define ASM_ARGS_3      ASM_ARGS_2, "r" (_r6)
217 #define LOAD_ARGS_4(a1, a2, a3, a4)             \
218   LOAD_ARGS_3 (a1, a2, a3)                      \
219   int __arg4 = (int) (a4);
220 #define LOAD_REGS_4                             \
221   register int _r7 asm ("r7") = __arg4;         \
222   LOAD_REGS_3
223 #define ASM_ARGS_4      ASM_ARGS_3, "r" (_r7)
224 #define LOAD_ARGS_5(a1, a2, a3, a4, a5)         \
225   LOAD_ARGS_4 (a1, a2, a3, a4)                  \
226   int __arg5 = (int) (a5);
227 #define LOAD_REGS_5                             \
228   register int _r8 asm ("r8") = __arg5;         \
229   LOAD_REGS_4
230 #define ASM_ARGS_5      ASM_ARGS_4, "r" (_r8)
231 #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6)     \
232   LOAD_ARGS_5 (a1, a2, a3, a4, a5)              \
233   int __arg6 = (int) (a6);
234 #define LOAD_REGS_6                         \
235   register int _r9 asm ("r9") = __arg6;     \
236   LOAD_REGS_5
237 #define ASM_ARGS_6      ASM_ARGS_5, "r" (_r9)
238
239 #define __SYSCALL_CLOBBERS "memory"
240
241 #endif /* __ASSEMBLER__ */
242
243 /* Pointer mangling support.  */
244 #if IS_IN (rtld)
245 /* We cannot use the thread descriptor because in ld.so we use setjmp
246    earlier than the descriptor is initialized.  */
247 #else
248 # ifdef __ASSEMBLER__
249 #  define PTR_MANGLE_GUARD(guard) ldw guard, POINTER_GUARD(r23)
250 #  define PTR_MANGLE(dst, src, guard) xor dst, src, guard
251 #  define PTR_DEMANGLE(dst, src, guard) PTR_MANGLE (dst, src, guard)
252 # else
253 #  define PTR_MANGLE(var) \
254   (var) = (__typeof (var)) ((uintptr_t) (var) ^ THREAD_GET_POINTER_GUARD ())
255 #  define PTR_DEMANGLE(var)     PTR_MANGLE (var)
256 # endif
257 #endif
258
259
260 #endif /* linux/nios2/sysdep.h */