1 /* Copyright (C) 1999-2021 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #ifndef _LINUX_IA64_SYSDEP_H
19 #define _LINUX_IA64_SYSDEP_H 1
20 
21 #include <sysdeps/unix/sysdep.h>
22 #include <sysdeps/unix/sysv/linux/sysdep.h>
23 #include <sysdeps/ia64/sysdep.h>
24 #include <dl-sysdep.h>
25 #include <tls.h>
26 #include <asm/break.h>
27 
28 /* In order to get __set_errno() definition in INLINE_SYSCALL.  */
29 #ifndef __ASSEMBLER__
30 #include <errno.h>
31 #endif
32 
33 /* As of GAS v2.4.90.0.7, including a ".align" directive inside a
34    function will cause bad unwind info to be emitted (GAS doesn't know
35    how to account for the padding introduced by the .align directive).
36    Turning on this macro will work around this bug by introducing the
37    necessary padding explicitly. */
38 #define GAS_ALIGN_BREAKS_UNWIND_INFO
39 
40 /* For Linux we can use the system call table in the header file
41 	/usr/include/asm/unistd.h
42    of the kernel.  But these symbols do not follow the SYS_* syntax
43    so we have to redefine the `SYS_ify' macro here.  */
44 #undef SYS_ify
45 #define SYS_ify(syscall_name)	__NR_##syscall_name
46 
47 #if defined USE_DL_SYSINFO \
48 	&& (IS_IN (libc) \
49 	    || IS_IN (libpthread) || IS_IN (librt))
50 # define IA64_USE_NEW_STUB
51 #else
52 # undef IA64_USE_NEW_STUB
53 #endif
54 
55 #ifdef __ASSEMBLER__
56 
57 #undef CALL_MCOUNT
58 #ifdef PROF
59 # define CALL_MCOUNT							\
60 	.data;								\
61 1:	data8 0;	/* XXX fixme: use .xdata8 once labels work */	\
62 	.previous;							\
63 	.prologue;							\
64 	.save ar.pfs, r40;						\
65 	alloc out0 = ar.pfs, 8, 0, 4, 0;				\
66 	mov out1 = gp;							\
67 	.save rp, out2;							\
68 	mov out2 = rp;							\
69 	.body;								\
70 	;;								\
71 	addl out3 = @ltoff(1b), gp;					\
72 	br.call.sptk.many rp = _mcount					\
73 	;;
74 #else
75 # define CALL_MCOUNT	/* Do nothing. */
76 #endif
77 
78 /* Linux uses a negative return value to indicate syscall errors, unlike
79    most Unices, which use the condition codes' carry flag.
80 
81    Since version 2.1 the return value of a system call might be negative
82    even if the call succeeded.  E.g., the `lseek' system call might return
83    a large offset.  Therefore we must not anymore test for < 0, but test
84    for a real error by making sure the value in %d0 is a real error
85    number.  Linus said he will make sure the no syscall returns a value
86    in -1 .. -4095 as a valid result so we can savely test with -4095.  */
87 
88 /* We don't want the label for the error handler to be visible in the symbol
89    table when we define it here.  */
90 #undef SYSCALL_ERROR_LABEL
91 #define SYSCALL_ERROR_LABEL __syscall_error
92 
93 #undef PSEUDO
94 #define	PSEUDO(name, syscall_name, args)	\
95   ENTRY(name)					\
96     DO_CALL (SYS_ify(syscall_name));		\
97 	cmp.eq p6,p0=-1,r10;			\
98 (p6)	br.cond.spnt.few __syscall_error;
99 
100 #define DO_CALL_VIA_BREAK(num)			\
101 	mov r15=num;				\
102 	break __IA64_BREAK_SYSCALL
103 
104 #ifdef IA64_USE_NEW_STUB
105 # ifdef SHARED
106 #  define DO_CALL(num)				\
107 	.prologue;				\
108 	adds r2 = SYSINFO_OFFSET, r13;;		\
109 	ld8 r2 = [r2];				\
110 	.save ar.pfs, r11;			\
111 	mov r11 = ar.pfs;;			\
112 	.body;					\
113 	mov r15 = num;				\
114 	mov b7 = r2;				\
115 	br.call.sptk.many b6 = b7;;		\
116 	.restore sp;				\
117 	mov ar.pfs = r11;			\
118 	.prologue;				\
119 	.body
120 # else /* !SHARED */
121 #  define DO_CALL(num)				\
122 	.prologue;				\
123 	mov r15 = num;				\
124 	movl r2 = _dl_sysinfo;;			\
125 	ld8 r2 = [r2];				\
126 	.save ar.pfs, r11;			\
127 	mov r11 = ar.pfs;;			\
128 	.body;					\
129 	mov b7 = r2;				\
130 	br.call.sptk.many b6 = b7;;		\
131 	.restore sp;				\
132 	mov ar.pfs = r11;			\
133 	.prologue;				\
134 	.body
135 # endif
136 #else
137 # define DO_CALL(num)				DO_CALL_VIA_BREAK(num)
138 #endif
139 
140 #undef PSEUDO_END
141 #define PSEUDO_END(name)	.endp C_SYMBOL_NAME(name);
142 
143 #undef PSEUDO_NOERRNO
144 #define	PSEUDO_NOERRNO(name, syscall_name, args)	\
145   ENTRY(name)						\
146     DO_CALL (SYS_ify(syscall_name));
147 
148 #undef PSEUDO_END_NOERRNO
149 #define PSEUDO_END_NOERRNO(name)	.endp C_SYMBOL_NAME(name);
150 
151 #undef PSEUDO_ERRVAL
152 #define	PSEUDO_ERRVAL(name, syscall_name, args)	\
153   ENTRY(name)					\
154     DO_CALL (SYS_ify(syscall_name));		\
155 	cmp.eq p6,p0=-1,r10;			\
156 (p6)	mov r10=r8;
157 
158 
159 #undef PSEUDO_END_ERRVAL
160 #define PSEUDO_END_ERRVAL(name)	.endp C_SYMBOL_NAME(name);
161 
162 #undef END
163 #define END(name)						\
164 	.size	C_SYMBOL_NAME(name), . - C_SYMBOL_NAME(name) ;	\
165 	.endp	C_SYMBOL_NAME(name)
166 
167 #define ret			br.ret.sptk.few b0
168 #define ret_NOERRNO		ret
169 #define ret_ERRVAL		ret
170 
171 #else /* not __ASSEMBLER__ */
172 
173 #undef HAVE_INTERNAL_BRK_ADDR_SYMBOL
174 #define HAVE_INTERNAL_BRK_ADDR_SYMBOL 1
175 
176 #define BREAK_INSN_1(num) "break " #num ";;\n\t"
177 #define BREAK_INSN(num) BREAK_INSN_1(num)
178 
179 /* On IA-64 we have stacked registers for passing arguments.  The
180    "out" registers end up being the called function's "in"
181    registers.
182 
183    Also, since we have plenty of registers we have two return values
184    from a syscall.  r10 is set to -1 on error, whilst r8 contains the
185    (non-negative) errno on error or the return value on success.
186  */
187 
188 #ifdef IA64_USE_NEW_STUB
189 
190 # define INTERNAL_SYSCALL_NCS(name, nr, args...)			      \
191 ({									      \
192     LOAD_ARGS_##nr (args)						      \
193     register long _r8 __asm ("r8");					      \
194     register long _r10 __asm ("r10");					      \
195     register long _r15 __asm ("r15") = name;				      \
196     register void *_b7 __asm ("b7") = ((tcbhead_t *)__thread_self)->__private;\
197     LOAD_REGS_##nr							      \
198     /*									      \
199      * Don't specify any unwind info here.  We mark ar.pfs as		      \
200      * clobbered.  This will force the compiler to save ar.pfs		      \
201      * somewhere and emit appropriate unwind info for that save.	      \
202      */									      \
203     __asm __volatile ("br.call.sptk.many b6=%0;;\n"			      \
204 		      : "=b"(_b7), "=r" (_r8), "=r" (_r10), "=r" (_r15)	      \
205 			ASM_OUTARGS_##nr				      \
206 		      : "0" (_b7), "3" (_r15) ASM_ARGS_##nr		      \
207 		      : "memory", "ar.pfs" ASM_CLOBBERS_##nr);		      \
208     _r10 == -1 ? -_r8 : _r8;						      \
209 })
210 
211 #else /* !IA64_USE_NEW_STUB */
212 
213 # define INTERNAL_SYSCALL_NCS(name, nr, args...)		\
214 ({								\
215     LOAD_ARGS_##nr (args)					\
216     register long _r8 asm ("r8");				\
217     register long _r10 asm ("r10");				\
218     register long _r15 asm ("r15") = name;			\
219     LOAD_REGS_##nr						\
220     __asm __volatile (BREAK_INSN (__IA64_BREAK_SYSCALL)		\
221 		      : "=r" (_r8), "=r" (_r10), "=r" (_r15)	\
222 			ASM_OUTARGS_##nr			\
223 		      : "2" (_r15) ASM_ARGS_##nr		\
224 		      : "memory" ASM_CLOBBERS_##nr);		\
225     _r10 == -1 ? -_r8 : _r8;					\
226 })
227 
228 #endif /* !IA64_USE_NEW_STUB */
229 
230 #define INTERNAL_SYSCALL(name, nr, args...)	\
231   INTERNAL_SYSCALL_NCS (__NR_##name, nr, ##args)
232 
233 #define LOAD_ARGS_0()
234 #define LOAD_REGS_0
235 #define LOAD_ARGS_1(a1)					\
236   long _arg1 = (long) (a1);				\
237   LOAD_ARGS_0 ()
238 #define LOAD_REGS_1					\
239   register long _out0 asm ("out0") = _arg1;		\
240   LOAD_REGS_0
241 #define LOAD_ARGS_2(a1, a2)				\
242   long _arg2 = (long) (a2);				\
243   LOAD_ARGS_1 (a1)
244 #define LOAD_REGS_2					\
245   register long _out1 asm ("out1") = _arg2;		\
246   LOAD_REGS_1
247 #define LOAD_ARGS_3(a1, a2, a3)				\
248   long _arg3 = (long) (a3);				\
249   LOAD_ARGS_2 (a1, a2)
250 #define LOAD_REGS_3					\
251   register long _out2 asm ("out2") = _arg3;		\
252   LOAD_REGS_2
253 #define LOAD_ARGS_4(a1, a2, a3, a4)			\
254   long _arg4 = (long) (a4);				\
255   LOAD_ARGS_3 (a1, a2, a3)
256 #define LOAD_REGS_4					\
257   register long _out3 asm ("out3") = _arg4;		\
258   LOAD_REGS_3
259 #define LOAD_ARGS_5(a1, a2, a3, a4, a5)			\
260   long _arg5 = (long) (a5);				\
261   LOAD_ARGS_4 (a1, a2, a3, a4)
262 #define LOAD_REGS_5					\
263   register long _out4 asm ("out4") = _arg5;		\
264   LOAD_REGS_4
265 #define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6)		\
266   long _arg6 = (long) (a6);	    			\
267   LOAD_ARGS_5 (a1, a2, a3, a4, a5)
268 #define LOAD_REGS_6					\
269   register long _out5 asm ("out5") = _arg6;		\
270   LOAD_REGS_5
271 
272 #define ASM_OUTARGS_0
273 #define ASM_OUTARGS_1	ASM_OUTARGS_0, "=r" (_out0)
274 #define ASM_OUTARGS_2	ASM_OUTARGS_1, "=r" (_out1)
275 #define ASM_OUTARGS_3	ASM_OUTARGS_2, "=r" (_out2)
276 #define ASM_OUTARGS_4	ASM_OUTARGS_3, "=r" (_out3)
277 #define ASM_OUTARGS_5	ASM_OUTARGS_4, "=r" (_out4)
278 #define ASM_OUTARGS_6	ASM_OUTARGS_5, "=r" (_out5)
279 
280 #ifdef IA64_USE_NEW_STUB
281 #define ASM_ARGS_0
282 #define ASM_ARGS_1	ASM_ARGS_0, "4" (_out0)
283 #define ASM_ARGS_2	ASM_ARGS_1, "5" (_out1)
284 #define ASM_ARGS_3	ASM_ARGS_2, "6" (_out2)
285 #define ASM_ARGS_4	ASM_ARGS_3, "7" (_out3)
286 #define ASM_ARGS_5	ASM_ARGS_4, "8" (_out4)
287 #define ASM_ARGS_6	ASM_ARGS_5, "9" (_out5)
288 #else
289 #define ASM_ARGS_0
290 #define ASM_ARGS_1	ASM_ARGS_0, "3" (_out0)
291 #define ASM_ARGS_2	ASM_ARGS_1, "4" (_out1)
292 #define ASM_ARGS_3	ASM_ARGS_2, "5" (_out2)
293 #define ASM_ARGS_4	ASM_ARGS_3, "6" (_out3)
294 #define ASM_ARGS_5	ASM_ARGS_4, "7" (_out4)
295 #define ASM_ARGS_6	ASM_ARGS_5, "8" (_out5)
296 #endif
297 
298 #define ASM_CLOBBERS_0	ASM_CLOBBERS_1, "out0"
299 #define ASM_CLOBBERS_1	ASM_CLOBBERS_2, "out1"
300 #define ASM_CLOBBERS_2	ASM_CLOBBERS_3, "out2"
301 #define ASM_CLOBBERS_3	ASM_CLOBBERS_4, "out3"
302 #define ASM_CLOBBERS_4	ASM_CLOBBERS_5, "out4"
303 #define ASM_CLOBBERS_5	ASM_CLOBBERS_6, "out5"
304 #define ASM_CLOBBERS_6_COMMON	, "out6", "out7",			\
305   /* Non-stacked integer registers, minus r8, r10, r15.  */		\
306   "r2", "r3", "r9", "r11", "r13", "r14", "r16", "r17", "r18",		\
307   "r19", "r20", "r21", "r22", "r23", "r24", "r25", "r26", "r27",	\
308   "r28", "r29", "r30", "r31",						\
309   /* Predicate registers.  */						\
310   "p6", "p7", "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15",	\
311   /* Non-rotating fp registers.  */					\
312   "f6", "f7", "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",	\
313   /* Branch registers.  */						\
314   "b6"
315 
316 #ifdef IA64_USE_NEW_STUB
317 # define ASM_CLOBBERS_6	ASM_CLOBBERS_6_COMMON
318 #else
319 # define ASM_CLOBBERS_6	ASM_CLOBBERS_6_COMMON , "b7"
320 #endif
321 
322 #endif /* not __ASSEMBLER__ */
323 
324 /* Pointer mangling support.  */
325 #if IS_IN (rtld)
326 /* We cannot use the thread descriptor because in ld.so we use setjmp
327    earlier than the descriptor is initialized.  */
328 #else
329 # ifdef __ASSEMBLER__
330 #  define PTR_MANGLE(reg, tmpreg) \
331         add	tmpreg=-16,r13		\
332         ;;				\
333         ld8	tmpreg=[tmpreg]		\
334         ;;				\
335         xor	reg=reg, tmpreg
336 #  define PTR_DEMANGLE(reg, tmpreg) PTR_MANGLE (reg, tmpreg)
337 # else
338 #  define PTR_MANGLE(var) \
339   (var) = (void *) ((uintptr_t) (var) ^ THREAD_GET_POINTER_GUARD ())
340 #  define PTR_DEMANGLE(var)	PTR_MANGLE (var)
341 # endif
342 #endif
343 
344 #endif /* linux/ia64/sysdep.h */
345