1 /* Assembler macros for PA-RISC.
2    Copyright (C) 1999-2018 Free Software Foundation, Inc.
3    Contributed by Ulrich Drepper, <drepper@cygnus.com>, August 1999.
4    Linux/PA-RISC changes by Philipp Rumpf, <prumpf@tux.org>, March 2000.
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    <http://www.gnu.org/licenses/>.  */
19 
20 #ifndef _LINUX_HPPA_SYSDEP_H
21 #define _LINUX_HPPA_SYSDEP_H 1
22 
23 #include <common/sysdep.h>
24 #include <sys/syscall.h>
25 
26 /* In order to get __set_errno() definition in INLINE_SYSCALL.  */
27 #ifndef __ASSEMBLER__
28 #include <errno.h>
29 #endif
30 
31 #undef ASM_LINE_SEP
32 #define ASM_LINE_SEP !
33 
34 #undef SYS_ify
35 #define SYS_ify(syscall_name)	(__NR_##syscall_name)
36 
37 /* The vfork, fork, and clone syscalls clobber r19
38  * and r21. We list r21 as either clobbered or as an
39  * input to a 6-argument syscall. We must save and
40  * restore r19 in both PIC and non-PIC cases.
41  */
42 /* WARNING: TREG must be a callee saves register so
43    that it doesn't have to be restored after a call
44    to another function */
45 #define TREG 4
46 #define SAVE_PIC(SREG) \
47 	copy %r19, SREG
48 #define LOAD_PIC(LREG) \
49 	copy LREG , %r19
50 /* Inline assembly defines */
51 #define TREG_ASM "%r4" /* Cant clobber r3, it holds framemarker */
52 #define SAVE_ASM_PIC	"       copy %%r19, %" TREG_ASM "\n"
53 #define LOAD_ASM_PIC	"       copy %" TREG_ASM ", %%r19\n"
54 #define CLOB_TREG	TREG_ASM ,
55 #define PIC_REG_DEF	register unsigned long __r19 asm("r19");
56 #define PIC_REG_USE	, "r" (__r19)
57 
58 #ifdef __ASSEMBLER__
59 
60 /* Syntactic details of assembler.  */
61 
62 #define ALIGNARG(log2) log2
63 
64 /* For Linux we can use the system call table in the header file
65 	/usr/include/asm/unistd.h
66    of the kernel.  But these symbols do not follow the SYS_* syntax
67    so we have to redefine the `SYS_ify' macro here.  */
68 #undef SYS_ify
69 #define SYS_ify(syscall_name)	__NR_##syscall_name
70 
71 /* ELF-like local names start with `.L'.  */
72 #undef L
73 #define L(name)	.L##name
74 
75 /* Linux uses a negative return value to indicate syscall errors,
76    unlike most Unices, which use the condition codes' carry flag.
77 
78    Since version 2.1 the return value of a system call might be
79    negative even if the call succeeded.  E.g., the `lseek' system call
80    might return a large offset.  Therefore we must not anymore test
81    for < 0, but test for a real error by making sure the value in %eax
82    is a real error number.  Linus said he will make sure the no syscall
83    returns a value in -1 .. -4095 as a valid result so we can safely
84    test with -4095.  */
85 
86 /* We don't want the label for the error handle to be global when we define
87    it here.  */
88 /*#ifdef PIC
89 # define SYSCALL_ERROR_LABEL 0f
90 #else
91 # define SYSCALL_ERROR_LABEL syscall_error
92 #endif*/
93 
94 /* Argument manipulation from the stack for preparing to
95    make a syscall */
96 
97 #define DOARGS_0 /* nothing */
98 #define DOARGS_1 /* nothing */
99 #define DOARGS_2 /* nothing */
100 #define DOARGS_3 /* nothing */
101 #define DOARGS_4 /* nothing */
102 #define DOARGS_5 ldw -52(%sp), %r22		ASM_LINE_SEP
103 #define DOARGS_6 DOARGS_5 ldw -56(%sp), %r21	ASM_LINE_SEP
104 
105 #define UNDOARGS_0 /* nothing */
106 #define UNDOARGS_1 /* nothing */
107 #define UNDOARGS_2 /* nothing */
108 #define UNDOARGS_3 /* nothing */
109 #define UNDOARGS_4 /* nothing */
110 #define UNDOARGS_5 /* nothing */
111 #define UNDOARGS_6 /* nothing */
112 
113 /* Define an entry point visible from C.
114 
115    There is currently a bug in gdb which prevents us from specifying
116    incomplete stabs information.  Fake some entries here which specify
117    the current source file.  */
118 #undef ENTRY
119 #define	ENTRY(name)							\
120 	.text						ASM_LINE_SEP	\
121 	.align ALIGNARG(4)				ASM_LINE_SEP	\
122 	.export C_SYMBOL_NAME(name)			ASM_LINE_SEP	\
123 	.type	C_SYMBOL_NAME(name),@function		ASM_LINE_SEP	\
124 	cfi_startproc					ASM_LINE_SEP	\
125 	C_LABEL(name)					ASM_LINE_SEP	\
126 	.PROC						ASM_LINE_SEP	\
127 	.CALLINFO FRAME=64,CALLS,SAVE_RP,ENTRY_GR=3	ASM_LINE_SEP	\
128 	.ENTRY						ASM_LINE_SEP	\
129 	/* SAVE_RP says we do */			ASM_LINE_SEP	\
130 	stw %rp, -20(%sr0,%sp)				ASM_LINE_SEP	\
131 	.cfi_offset 2, -20				ASM_LINE_SEP	\
132 	/*FIXME: Call mcount? (carefull with stack!) */
133 
134 /* Some syscall wrappers do not call other functions, and
135    hence are classified as leaf, so add NO_CALLS for gdb */
136 #define	ENTRY_LEAF(name)						\
137 	.text						ASM_LINE_SEP	\
138 	.align ALIGNARG(4)				ASM_LINE_SEP	\
139 	.export C_SYMBOL_NAME(name)			ASM_LINE_SEP	\
140 	.type	C_SYMBOL_NAME(name),@function		ASM_LINE_SEP	\
141 	cfi_startproc					ASM_LINE_SEP	\
142 	C_LABEL(name)					ASM_LINE_SEP	\
143 	.PROC						ASM_LINE_SEP	\
144 	.CALLINFO FRAME=64,NO_CALLS,SAVE_RP,ENTRY_GR=3	ASM_LINE_SEP	\
145 	.ENTRY						ASM_LINE_SEP	\
146 	/* SAVE_RP says we do */			ASM_LINE_SEP	\
147 	stw %rp, -20(%sr0,%sp)				ASM_LINE_SEP	\
148 	.cfi_offset 2, -20				ASM_LINE_SEP	\
149 	/*FIXME: Call mcount? (carefull with stack!) */
150 
151 #undef	END
152 #define END(name)							\
153 	.EXIT						ASM_LINE_SEP	\
154 	.PROCEND					ASM_LINE_SEP	\
155 	cfi_endproc					ASM_LINE_SEP	\
156 .size	C_SYMBOL_NAME(name), .-C_SYMBOL_NAME(name)	ASM_LINE_SEP
157 
158 /* If compiled for profiling, call `mcount' at the start
159    of each function. No, don't bother.  gcc will put the
160    call in for us.  */
161 #define CALL_MCOUNT		/* Do nothing.  */
162 
163 /* syscall wrappers consist of
164 	#include <sysdep.h>
165 	PSEUDO(...)
166 	ret
167 	PSEUDO_END(...)
168 
169    which means
170 	ENTRY(name)
171 	DO_CALL(...)
172 	bv,n 0(2)
173 */
174 
175 #undef PSEUDO
176 #define	PSEUDO(name, syscall_name, args)			\
177   ENTRY (name)					ASM_LINE_SEP	\
178   /* If necc. load args from stack */		ASM_LINE_SEP	\
179   DOARGS_##args					ASM_LINE_SEP	\
180   DO_CALL (syscall_name, args)			ASM_LINE_SEP	\
181   UNDOARGS_##args				ASM_LINE_SEP
182 
183 #define ret \
184   /* Return value set by ERRNO code */		ASM_LINE_SEP	\
185   bv,n 0(2)					ASM_LINE_SEP
186 
187 #undef	PSEUDO_END
188 #define	PSEUDO_END(name)					\
189   END (name)
190 
191 /* We don't set the errno on the return from the syscall */
192 #define	PSEUDO_NOERRNO(name, syscall_name, args)		\
193   ENTRY_LEAF (name)				ASM_LINE_SEP	\
194   DOARGS_##args					ASM_LINE_SEP	\
195   DO_CALL_NOERRNO (syscall_name, args)		ASM_LINE_SEP	\
196   UNDOARGS_##args				ASM_LINE_SEP
197 
198 #define ret_NOERRNO ret
199 
200 #undef	PSEUDO_END_NOERRNO
201 #define	PSEUDO_END_NOERRNO(name)				\
202   END (name)
203 
204 /* This has to return the error value */
205 #undef  PSEUDO_ERRVAL
206 #define PSEUDO_ERRVAL(name, syscall_name, args)			\
207   ENTRY_LEAF (name)				ASM_LINE_SEP	\
208   DOARGS_##args					ASM_LINE_SEP	\
209   DO_CALL_ERRVAL (syscall_name, args)		ASM_LINE_SEP	\
210   UNDOARGS_##args				ASM_LINE_SEP
211 
212 #define ret_ERRVAL ret
213 
214 #undef	PSEUDO_END_ERRVAL
215 #define PSEUDO_END_ERRVAL(name)					\
216 	END(name)
217 
218 #undef JUMPTARGET
219 #define JUMPTARGET(name)	name
220 #define SYSCALL_PIC_SETUP	/* Nothing.  */
221 
222 
223 /* FIXME: This comment is not true.
224  * All the syscall assembly macros rely on finding the appropriate
225    SYSCALL_ERROR_LABEL or rather HANDLER. */
226 
227 /* int * __errno_location(void) so you have to store your value
228    into the return address! */
229 #define DEFAULT_SYSCALL_ERROR_HANDLER			\
230 	.import __errno_location,code	ASM_LINE_SEP	\
231 	/* branch to errno handler */	ASM_LINE_SEP	\
232 	bl __errno_location,%rp		ASM_LINE_SEP
233 
234 /* Here are the myriad of configuration options that the above can
235    work for... what we've done is provide the framework for future
236    changes if required to each section */
237 
238 #ifdef __PIC__
239 #  if defined _LIBC_REENTRANT
240 #   define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
241 #  else /* !_LIBC_REENTRANT */
242 #   define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
243 #  endif /* _LIBC_REENTRANT */
244 #else
245 # ifndef _LIBC_REENTRANT
246 #  define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
247 # else
248 #  define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
249 # endif
250 #endif
251 
252 
253 /* Linux takes system call arguments in registers:
254 	syscall number	gr20
255 	arg 1		gr26
256 	arg 2		gr25
257 	arg 3		gr24
258 	arg 4		gr23
259 	arg 5		gr22
260 	arg 6		gr21
261 
262    The compiler calls us by the C convention:
263 	syscall number	in the DO_CALL macro
264 	arg 1		gr26
265 	arg 2		gr25
266 	arg 3		gr24
267 	arg 4		gr23
268 	arg 5		-52(sp)
269 	arg 6		-56(sp)
270 
271    gr22 and gr21 are caller-saves, so we can just load the arguments
272    there and generally be happy. */
273 
274 /* the cmpb...no_error code below inside DO_CALL
275  * is intended to mimic the if (__sys_res...)
276  * code inside INLINE_SYSCALL
277  */
278 #define NO_ERROR -0x1000
279 
280 #undef	DO_CALL
281 #define DO_CALL(syscall_name, args)				\
282 	/* Create a frame */			ASM_LINE_SEP	\
283 	stwm TREG, 64(%sp)			ASM_LINE_SEP	\
284 	.cfi_def_cfa_offset -64			ASM_LINE_SEP	\
285 	.cfi_offset TREG, 0			ASM_LINE_SEP	\
286 	stw %sp, -4(%sp)			ASM_LINE_SEP	\
287 	stw %r19, -32(%sp)			ASM_LINE_SEP	\
288 	.cfi_offset 19, 32			ASM_LINE_SEP	\
289 	/* Save r19 */				ASM_LINE_SEP	\
290 	SAVE_PIC(TREG)				ASM_LINE_SEP	\
291 	/* Do syscall, delay loads # */		ASM_LINE_SEP	\
292 	ble  0x100(%sr2,%r0)			ASM_LINE_SEP	\
293 	ldi SYS_ify (syscall_name), %r20	ASM_LINE_SEP	\
294 	ldi NO_ERROR,%r1			ASM_LINE_SEP	\
295 	cmpb,>>=,n %r1,%ret0,L(pre_end)		ASM_LINE_SEP	\
296 	/* Restore r19 from TREG */		ASM_LINE_SEP	\
297 	LOAD_PIC(TREG) /* delay */		ASM_LINE_SEP	\
298 	SYSCALL_ERROR_HANDLER			ASM_LINE_SEP	\
299 	/* Use TREG for temp storage */		ASM_LINE_SEP	\
300 	copy %ret0, TREG /* delay */		ASM_LINE_SEP	\
301 	/* OPTIMIZE: Don't reload r19 */	ASM_LINE_SEP	\
302 	/* do a -1*syscall_ret0 */		ASM_LINE_SEP	\
303 	sub %r0, TREG, TREG			ASM_LINE_SEP	\
304 	/* Store into errno location */		ASM_LINE_SEP	\
305 	stw TREG, 0(%sr0,%ret0)			ASM_LINE_SEP	\
306 	/* return -1 as error */		ASM_LINE_SEP	\
307 	ldo -1(%r0), %ret0			ASM_LINE_SEP	\
308 L(pre_end):					ASM_LINE_SEP	\
309 	/* Restore our frame, restoring TREG */	ASM_LINE_SEP	\
310 	ldwm -64(%sp), TREG			ASM_LINE_SEP	\
311 	/* Restore return pointer */		ASM_LINE_SEP	\
312 	ldw -20(%sp),%rp			ASM_LINE_SEP
313 
314 /* We do nothing with the return, except hand it back to someone else */
315 #undef  DO_CALL_NOERRNO
316 #define DO_CALL_NOERRNO(syscall_name, args)			\
317 	/* No need to store r19 */		ASM_LINE_SEP	\
318 	ble  0x100(%sr2,%r0)                    ASM_LINE_SEP    \
319 	ldi SYS_ify (syscall_name), %r20        ASM_LINE_SEP    \
320 	/* Caller will restore r19 */		ASM_LINE_SEP
321 
322 /* Here, we return the ERRVAL in assembly, note we don't call the
323    error handler function, but we do 'negate' the return _IF_
324    it's an error. Not sure if this is the right semantic. */
325 
326 #undef	DO_CALL_ERRVAL
327 #define DO_CALL_ERRVAL(syscall_name, args)			\
328 	/* No need to store r19 */		ASM_LINE_SEP	\
329 	ble  0x100(%sr2,%r0)			ASM_LINE_SEP	\
330 	ldi SYS_ify (syscall_name), %r20	ASM_LINE_SEP	\
331 	/* Caller will restore r19 */		ASM_LINE_SEP	\
332 	ldi NO_ERROR,%r1			ASM_LINE_SEP	\
333 	cmpb,>>=,n %r1,%ret0,0f			ASM_LINE_SEP	\
334 	sub %r0, %ret0, %ret0			ASM_LINE_SEP	\
335 0:						ASM_LINE_SEP
336 
337 
338 #else
339 
340 /* GCC has to be warned that a syscall may clobber all the ABI
341    registers listed as "caller-saves", see page 8, Table 2
342    in section 2.2.6 of the PA-RISC RUN-TIME architecture
343    document. However! r28 is the result and will conflict with
344    the clobber list so it is left out. Also the input arguments
345    registers r20 -> r26 will conflict with the list so they
346    are treated specially. Although r19 is clobbered by the syscall
347    we cannot say this because it would violate ABI, thus we say
348    TREG is clobbered and use that register to save/restore r19
349    across the syscall. */
350 
351 #define CALL_CLOB_REGS	"%r1", "%r2", CLOB_TREG \
352 			"%r20", "%r29", "%r31"
353 
354 #endif	/* __ASSEMBLER__ */
355 
356 #endif /* _LINUX_HPPA_SYSDEP_H */
357