1 /* Assembler macros for PA-RISC.
2 Copyright (C) 1999, 2001, 2002, 2003, 2007
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper, <drepper@cygnus.com>, August 1999.
6 Linux/PA-RISC changes by Philipp Rumpf, <prumpf@tux.org>, March 2000.
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 The GNU C Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with the GNU C Library; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include <asm/unistd.h>
24 #include <sysdeps/generic/sysdep.h>
25 #include <sys/syscall.h>
28 #define ASM_LINE_SEP !
31 #define SYS_ify(syscall_name) (__NR_##syscall_name)
33 /* WARNING: TREG must be a callee saves register so
34 that it doesn't have to be restored after a call
35 to another function */
38 # define SAVE_PIC(SREG) copy %r19, SREG ASM_LINE_SEP
39 # define LOAD_PIC(LREG) copy LREG, %r19 ASM_LINE_SEP
40 /* Inline assembly defines */
41 # define TREG_ASM "%r4" /* Cant clobber r3, it holds framemarker */
42 # define SAVE_ASM_PIC " copy %%r19, %" TREG_ASM "\n"
43 # define LOAD_ASM_PIC " copy %" TREG_ASM ", %%r19\n"
44 # define CLOB_TREG TREG_ASM ,
45 # define PIC_REG_DEF register unsigned long __r19 asm("r19");
46 # define PIC_REG_USE , "r" (__r19)
49 # define SAVE_PIC(SREG) nop ASM_LINE_SEP
50 # define LOAD_PIC(LREG) nop ASM_LINE_SEP
51 /* Inline assembly defines */
53 # define SAVE_ASM_PIC "nop \n"
54 # define LOAD_ASM_PIC "nop \n"
62 /* Syntactic details of assembler. */
64 #define ALIGNARG(log2) log2
66 /* For Linux we can use the system call table in the header file
67 /usr/include/asm/unistd.h
68 of the kernel. But these symbols do not follow the SYS_* syntax
69 so we have to redefine the `SYS_ify' macro here. */
71 #define SYS_ify(syscall_name) __NR_##syscall_name
73 /* ELF-like local names start with `.L'. */
75 #define L(name) .L##name
77 /* Linux uses a negative return value to indicate syscall errors,
78 unlike most Unices, which use the condition codes' carry flag.
80 Since version 2.1 the return value of a system call might be
81 negative even if the call succeeded. E.g., the `lseek' system call
82 might return a large offset. Therefore we must not anymore test
83 for < 0, but test for a real error by making sure the value in %eax
84 is a real error number. Linus said he will make sure the no syscall
85 returns a value in -1 .. -4095 as a valid result so we can safely
88 /* We don't want the label for the error handle to be global when we define
91 # define SYSCALL_ERROR_LABEL 0f
93 # define SYSCALL_ERROR_LABEL syscall_error
96 /* Argument manipulation from the stack for preparing to
99 #define DOARGS_0 /* nothing */
100 #define DOARGS_1 /* nothing */
101 #define DOARGS_2 /* nothing */
102 #define DOARGS_3 /* nothing */
103 #define DOARGS_4 /* nothing */
104 #define DOARGS_5 ldw -52(%sp), %r22 ASM_LINE_SEP
105 #define DOARGS_6 DOARGS_5 ldw -56(%sp), %r21 ASM_LINE_SEP
107 #define UNDOARGS_0 /* nothing */
108 #define UNDOARGS_1 /* nothing */
109 #define UNDOARGS_2 /* nothing */
110 #define UNDOARGS_3 /* nothing */
111 #define UNDOARGS_4 /* nothing */
112 #define UNDOARGS_5 /* nothing */
113 #define UNDOARGS_6 /* nothing */
115 /* Define an entry point visible from C.
117 There is currently a bug in gdb which prevents us from specifying
118 incomplete stabs information. Fake some entries here which specify
119 the current source file. */
120 #define ENTRY(name) \
122 .align ALIGNARG(4) ASM_LINE_SEP \
123 .export C_SYMBOL_NAME(name) ASM_LINE_SEP \
124 .type C_SYMBOL_NAME(name),@function ASM_LINE_SEP \
125 C_LABEL(name) 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 /*FIXME: Call mcount? (carefull with stack!) */
133 /* Some syscall wrappers do not call other functions, and
134 hence are classified as leaf, so add NO_CALLS for gdb */
135 #define ENTRY_LEAF(name) \
137 .align ALIGNARG(4) ASM_LINE_SEP \
138 .export C_SYMBOL_NAME(name) ASM_LINE_SEP \
139 .type C_SYMBOL_NAME(name),@function ASM_LINE_SEP \
140 C_LABEL(name) ASM_LINE_SEP \
142 .CALLINFO FRAME=64,NO_CALLS,SAVE_RP,ENTRY_GR=3 ASM_LINE_SEP \
143 .ENTRY ASM_LINE_SEP \
144 /* SAVE_RP says we do */ ASM_LINE_SEP \
145 stw %rp, -20(%sr0,%sp) ASM_LINE_SEP \
146 /*FIXME: Call mcount? (carefull with stack!) */
151 .PROCEND ASM_LINE_SEP \
152 .size C_SYMBOL_NAME(name), .-C_SYMBOL_NAME(name) ASM_LINE_SEP
154 /* If compiled for profiling, call `mcount' at the start
155 of each function. No, don't bother. gcc will put the
157 #define CALL_MCOUNT /* Do nothing. */
159 /* syscall wrappers consist of
173 #define PSEUDO(name, syscall_name, args) \
174 ENTRY (name) ASM_LINE_SEP \
175 /* If necc. load args from stack */ ASM_LINE_SEP \
176 DOARGS_##args ASM_LINE_SEP \
177 DO_CALL (syscall_name, args) ASM_LINE_SEP \
178 UNDOARGS_##args ASM_LINE_SEP \
182 /* Return value set by ERRNO code */ ASM_LINE_SEP \
183 bv,n 0(2) ASM_LINE_SEP
186 #define PSEUDO_END(name) \
189 /* We don't set the errno on the return from the syscall */
190 #define PSEUDO_NOERRNO(name, syscall_name, args) \
191 ENTRY_LEAF (name) ASM_LINE_SEP \
192 DOARGS_##args ASM_LINE_SEP \
193 DO_CALL_NOERRNO (syscall_name, args) ASM_LINE_SEP \
194 UNDOARGS_##args ASM_LINE_SEP \
197 #define ret_NOERRNO ret
199 #undef PSEUDO_END_NOERRNO
200 #define PSEUDO_END_NOERRNO(name) \
203 /* This has to return the error value */
205 #define PSEUDO_ERRVAL(name, syscall_name, args) \
206 ENTRY_LEAF (name) ASM_LINE_SEP \
207 DOARGS_##args ASM_LINE_SEP \
208 DO_CALL_ERRVAL (syscall_name, args) ASM_LINE_SEP \
209 UNDOARGS_##args ASM_LINE_SEP \
212 #define ret_ERRVAL ret
214 #undef PSEUDO_END_ERRVAL
215 #define PSEUDO_END_ERRVAL(name) \
219 #define JUMPTARGET(name) name
220 #define SYSCALL_PIC_SETUP /* Nothing. */
223 /* FIXME: This comment is not true.
224 * All the syscall assembly macros rely on finding the approriate
225 SYSCALL_ERROR_LABEL or rather HANDLER. */
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
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 */
239 # if RTLD_PRIVATE_ERRNO
240 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
241 # else /* !RTLD_PRIVATE_ERRNO */
242 # if defined _LIBC_REENTRANT
243 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
244 # else /* !_LIBC_REENTRANT */
245 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
246 # endif /* _LIBC_REENTRANT */
247 # endif /* RTLD_PRIVATE_ERRNO */
249 # ifndef _LIBC_REENTRANT
250 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
252 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
257 /* Linux takes system call arguments in registers:
266 The compiler calls us by the C convention:
267 syscall number in the DO_CALL macro
275 gr22 and gr21 are caller-saves, so we can just load the arguments
276 there and generally be happy. */
278 /* the cmpb...no_error code below inside DO_CALL
279 * is intended to mimic the if (__sys_res...)
280 * code inside INLINE_SYSCALL
282 #define NO_ERROR -0x1000
285 #define DO_CALL(syscall_name, args) \
286 /* Create a frame */ ASM_LINE_SEP \
287 stwm TREG, 64(%sp) ASM_LINE_SEP \
288 stw %sp, -4(%sp) ASM_LINE_SEP \
289 stw %r19, -32(%sp) ASM_LINE_SEP \
290 /* Save r19 */ ASM_LINE_SEP \
291 SAVE_PIC(TREG) ASM_LINE_SEP \
292 /* Do syscall, delay loads # */ ASM_LINE_SEP \
293 ble 0x100(%sr2,%r0) ASM_LINE_SEP \
294 ldi SYS_ify (syscall_name), %r20 ASM_LINE_SEP \
295 ldi NO_ERROR,%r1 ASM_LINE_SEP \
296 cmpb,>>=,n %r1,%ret0,L(pre_end) ASM_LINE_SEP \
297 /* Restore r19 from TREG */ ASM_LINE_SEP \
298 LOAD_PIC(TREG) /* delay */ ASM_LINE_SEP \
299 SYSCALL_ERROR_HANDLER ASM_LINE_SEP \
300 /* Use TREG for temp storage */ ASM_LINE_SEP \
301 copy %ret0, TREG /* delay */ ASM_LINE_SEP \
302 /* OPTIMIZE: Don't reload r19 */ ASM_LINE_SEP \
303 /* do a -1*syscall_ret0 */ ASM_LINE_SEP \
304 sub %r0, TREG, TREG ASM_LINE_SEP \
305 /* Store into errno location */ ASM_LINE_SEP \
306 stw TREG, 0(%sr0,%ret0) ASM_LINE_SEP \
307 /* return -1 as error */ ASM_LINE_SEP \
308 ldo -1(%r0), %ret0 ASM_LINE_SEP \
309 L(pre_end): ASM_LINE_SEP \
310 /* Restore our frame, restoring TREG */ ASM_LINE_SEP \
311 ldwm -64(%sp), TREG ASM_LINE_SEP \
312 /* Restore return pointer */ ASM_LINE_SEP \
313 ldw -20(%sp),%rp ASM_LINE_SEP
315 /* We do nothing with the return, except hand it back to someone else */
316 #undef DO_CALL_NOERRNO
317 #define DO_CALL_NOERRNO(syscall_name, args) \
318 /* No need to store r19 */ ASM_LINE_SEP \
319 ble 0x100(%sr2,%r0) ASM_LINE_SEP \
320 ldi SYS_ify (syscall_name), %r20 ASM_LINE_SEP \
321 /* Caller will restore r19 */ ASM_LINE_SEP
323 /* Here, we return the ERRVAL in assembly, note we don't call the
324 error handler function, but we do 'negate' the return _IF_
325 it's an error. Not sure if this is the right semantic. */
327 #undef DO_CALL_ERRVAL
328 #define DO_CALL_ERRVAL(syscall_name, args) \
329 /* No need to store r19 */ ASM_LINE_SEP \
330 ble 0x100(%sr2,%r0) ASM_LINE_SEP \
331 ldi SYS_ify (syscall_name), %r20 ASM_LINE_SEP \
332 /* Caller will restore r19 */ ASM_LINE_SEP \
333 ldi NO_ERROR,%r1 ASM_LINE_SEP \
334 cmpb,>>=,n %r1,%ret0,0f ASM_LINE_SEP \
335 sub %r0, %ret0, %ret0 ASM_LINE_SEP \
341 /* GCC has to be warned that a syscall may clobber all the ABI
342 registers listed as "caller-saves", see page 8, Table 2
343 in section 2.2.6 of the PA-RISC RUN-TIME architecture
344 document. However! r28 is the result and will conflict with
345 the clobber list so it is left out. Also the input arguments
346 registers r20 -> r26 will conflict with the list so they
347 are treated specially. Although r19 is clobbered by the syscall
348 we cannot say this because it would violate ABI, thus we say
349 TREG is clobbered and use that register to save/restore r19
350 across the syscall. */
352 #define CALL_CLOB_REGS "%r1", "%r2", CLOB_TREG \
353 "%r20", "%r29", "%r31"
355 #undef INLINE_SYSCALL
356 #define INLINE_SYSCALL(name, nr, args...) \
360 register unsigned long __res asm("r28"); \
362 LOAD_ARGS_##nr(args) \
363 /* FIXME: HACK save/load r19 around syscall */ \
366 " ble 0x100(%%sr2, %%r0)\n" \
370 : "i" (SYS_ify(name)) PIC_REG_USE ASM_ARGS_##nr \
371 : "memory", CALL_CLOB_REGS CLOB_ARGS_##nr \
373 __sys_res = (long)__res; \
375 if ( (unsigned long)__sys_res >= (unsigned long)-4095 ){ \
376 __set_errno(-__sys_res); \
382 /* INTERNAL_SYSCALL_DECL - Allows us to setup some function static
383 value to use within the context of the syscall
384 INTERNAL_SYSCALL_ERROR_P - Returns 0 if it wasn't an error, 1 otherwise
385 You are allowed to use the syscall result (val) and the DECL error
386 variable to determine what went wrong.
387 INTERLAL_SYSCALL_ERRNO - Munges the val/err pair into the error number.
388 In our case we just flip the sign. */
390 #undef INTERNAL_SYSCALL_DECL
391 #define INTERNAL_SYSCALL_DECL(err)
393 #undef INTERNAL_SYSCALL_ERROR_P
394 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
395 ((val < 0) && (val > -4095))
397 #undef INTERNAL_SYSCALL_ERRNO
398 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
400 /* Similar to INLINE_SYSCALL but we don't set errno */
401 #undef INTERNAL_SYSCALL
402 #define INTERNAL_SYSCALL(name, err, nr, args...) \
406 register unsigned long __res asm("r28"); \
408 LOAD_ARGS_##nr(args) \
409 /* FIXME: HACK save/load r19 around syscall */ \
412 " ble 0x100(%%sr2, %%r0)\n" \
416 : "i" (SYS_ify(name)) PIC_REG_USE ASM_ARGS_##nr \
417 : "memory", CALL_CLOB_REGS CLOB_ARGS_##nr \
419 __sys_res = (long)__res; \
425 /* The _NCS variant allows non-constant syscall numbers. */
426 #undef INTERNAL_SYSCALL_NCS
427 #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
431 register unsigned long __res asm("r28"); \
433 LOAD_ARGS_##nr(args) \
434 /* FIXME: HACK save/load r19 around syscall */ \
437 " ble 0x100(%%sr2, %%r0)\n" \
438 " copy %1, %%r20\n" \
441 : "r" (name) PIC_REG_USE ASM_ARGS_##nr \
442 : "memory", CALL_CLOB_REGS CLOB_ARGS_##nr \
444 __sys_res = (long)__res; \
451 #define LOAD_ARGS_0()
452 #define LOAD_ARGS_1(r26) \
453 register unsigned long __r26 __asm__("r26") = (unsigned long)(r26); \
455 #define LOAD_ARGS_2(r26,r25) \
456 register unsigned long __r25 __asm__("r25") = (unsigned long)(r25); \
458 #define LOAD_ARGS_3(r26,r25,r24) \
459 register unsigned long __r24 __asm__("r24") = (unsigned long)(r24); \
461 #define LOAD_ARGS_4(r26,r25,r24,r23) \
462 register unsigned long __r23 __asm__("r23") = (unsigned long)(r23); \
463 LOAD_ARGS_3(r26,r25,r24)
464 #define LOAD_ARGS_5(r26,r25,r24,r23,r22) \
465 register unsigned long __r22 __asm__("r22") = (unsigned long)(r22); \
466 LOAD_ARGS_4(r26,r25,r24,r23)
467 #define LOAD_ARGS_6(r26,r25,r24,r23,r22,r21) \
468 register unsigned long __r21 __asm__("r21") = (unsigned long)(r21); \
469 LOAD_ARGS_5(r26,r25,r24,r23,r22)
471 /* Even with zero args we use r20 for the syscall number */
473 #define ASM_ARGS_1 ASM_ARGS_0, "r" (__r26)
474 #define ASM_ARGS_2 ASM_ARGS_1, "r" (__r25)
475 #define ASM_ARGS_3 ASM_ARGS_2, "r" (__r24)
476 #define ASM_ARGS_4 ASM_ARGS_3, "r" (__r23)
477 #define ASM_ARGS_5 ASM_ARGS_4, "r" (__r22)
478 #define ASM_ARGS_6 ASM_ARGS_5, "r" (__r21)
480 /* The registers not listed as inputs but clobbered */
482 #define CLOB_ARGS_5 CLOB_ARGS_6, "%r21"
483 #define CLOB_ARGS_4 CLOB_ARGS_5, "%r22"
484 #define CLOB_ARGS_3 CLOB_ARGS_4, "%r23"
485 #define CLOB_ARGS_2 CLOB_ARGS_3, "%r24"
486 #define CLOB_ARGS_1 CLOB_ARGS_2, "%r25"
487 #define CLOB_ARGS_0 CLOB_ARGS_1, "%r26"
489 #endif /* __ASSEMBLER__ */
491 /* Pointer mangling is not yet supported for HPPA. */
492 #define PTR_MANGLE(var) (void) (var)
493 #define PTR_DEMANGLE(var) (void) (var)