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