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>
27 /* In order to get __set_errno() definition in INLINE_SYSCALL. */
33 #define ASM_LINE_SEP !
36 #define SYS_ify(syscall_name) (__NR_##syscall_name)
38 /* WARNING: TREG must be a callee saves register so
39 that it doesn't have to be restored after a call
40 to another function */
43 # define SAVE_PIC(SREG) copy %r19, SREG ASM_LINE_SEP
44 # define LOAD_PIC(LREG) copy LREG, %r19 ASM_LINE_SEP
45 /* Inline assembly defines */
46 # define TREG_ASM "%r4" /* Cant clobber r3, it holds framemarker */
47 # define SAVE_ASM_PIC " copy %%r19, %" TREG_ASM "\n"
48 # define LOAD_ASM_PIC " copy %" TREG_ASM ", %%r19\n"
49 # define CLOB_TREG TREG_ASM ,
50 # define PIC_REG_DEF register unsigned long __r19 asm("r19");
51 # define PIC_REG_USE , "r" (__r19)
54 # define SAVE_PIC(SREG) nop ASM_LINE_SEP
55 # define LOAD_PIC(LREG) nop ASM_LINE_SEP
56 /* Inline assembly defines */
58 # define SAVE_ASM_PIC "nop \n"
59 # define LOAD_ASM_PIC "nop \n"
67 /* Syntactic details of assembler. */
69 #define ALIGNARG(log2) log2
71 /* For Linux we can use the system call table in the header file
72 /usr/include/asm/unistd.h
73 of the kernel. But these symbols do not follow the SYS_* syntax
74 so we have to redefine the `SYS_ify' macro here. */
76 #define SYS_ify(syscall_name) __NR_##syscall_name
78 /* ELF-like local names start with `.L'. */
80 #define L(name) .L##name
82 /* Linux uses a negative return value to indicate syscall errors,
83 unlike most Unices, which use the condition codes' carry flag.
85 Since version 2.1 the return value of a system call might be
86 negative even if the call succeeded. E.g., the `lseek' system call
87 might return a large offset. Therefore we must not anymore test
88 for < 0, but test for a real error by making sure the value in %eax
89 is a real error number. Linus said he will make sure the no syscall
90 returns a value in -1 .. -4095 as a valid result so we can safely
93 /* We don't want the label for the error handle to be global when we define
96 # define SYSCALL_ERROR_LABEL 0f
98 # define SYSCALL_ERROR_LABEL syscall_error
101 /* Argument manipulation from the stack for preparing to
104 #define DOARGS_0 /* nothing */
105 #define DOARGS_1 /* nothing */
106 #define DOARGS_2 /* nothing */
107 #define DOARGS_3 /* nothing */
108 #define DOARGS_4 /* nothing */
109 #define DOARGS_5 ldw -52(%sp), %r22 ASM_LINE_SEP
110 #define DOARGS_6 DOARGS_5 ldw -56(%sp), %r21 ASM_LINE_SEP
112 #define UNDOARGS_0 /* nothing */
113 #define UNDOARGS_1 /* nothing */
114 #define UNDOARGS_2 /* nothing */
115 #define UNDOARGS_3 /* nothing */
116 #define UNDOARGS_4 /* nothing */
117 #define UNDOARGS_5 /* nothing */
118 #define UNDOARGS_6 /* nothing */
120 /* Define an entry point visible from C.
122 There is currently a bug in gdb which prevents us from specifying
123 incomplete stabs information. Fake some entries here which specify
124 the current source file. */
125 #define ENTRY(name) \
127 .align ALIGNARG(4) ASM_LINE_SEP \
128 .export C_SYMBOL_NAME(name) ASM_LINE_SEP \
129 .type C_SYMBOL_NAME(name),@function ASM_LINE_SEP \
130 C_LABEL(name) ASM_LINE_SEP \
132 .CALLINFO FRAME=64,CALLS,SAVE_RP,ENTRY_GR=3 ASM_LINE_SEP \
133 .ENTRY ASM_LINE_SEP \
134 /* SAVE_RP says we do */ ASM_LINE_SEP \
135 stw %rp, -20(%sr0,%sp) ASM_LINE_SEP \
136 /*FIXME: Call mcount? (carefull with stack!) */
138 /* Some syscall wrappers do not call other functions, and
139 hence are classified as leaf, so add NO_CALLS for gdb */
140 #define ENTRY_LEAF(name) \
142 .align ALIGNARG(4) ASM_LINE_SEP \
143 .export C_SYMBOL_NAME(name) ASM_LINE_SEP \
144 .type C_SYMBOL_NAME(name),@function ASM_LINE_SEP \
145 C_LABEL(name) ASM_LINE_SEP \
147 .CALLINFO FRAME=64,NO_CALLS,SAVE_RP,ENTRY_GR=3 ASM_LINE_SEP \
148 .ENTRY ASM_LINE_SEP \
149 /* SAVE_RP says we do */ ASM_LINE_SEP \
150 stw %rp, -20(%sr0,%sp) ASM_LINE_SEP \
151 /*FIXME: Call mcount? (carefull with stack!) */
156 .PROCEND ASM_LINE_SEP \
157 .size C_SYMBOL_NAME(name), .-C_SYMBOL_NAME(name) ASM_LINE_SEP
159 /* If compiled for profiling, call `mcount' at the start
160 of each function. No, don't bother. gcc will put the
162 #define CALL_MCOUNT /* Do nothing. */
164 /* syscall wrappers consist of
178 #define PSEUDO(name, syscall_name, args) \
179 ENTRY (name) ASM_LINE_SEP \
180 /* If necc. load args from stack */ ASM_LINE_SEP \
181 DOARGS_##args ASM_LINE_SEP \
182 DO_CALL (syscall_name, args) ASM_LINE_SEP \
183 UNDOARGS_##args ASM_LINE_SEP \
187 /* Return value set by ERRNO code */ ASM_LINE_SEP \
188 bv,n 0(2) ASM_LINE_SEP
191 #define PSEUDO_END(name) \
194 /* We don't set the errno on the return from the syscall */
195 #define PSEUDO_NOERRNO(name, syscall_name, args) \
196 ENTRY_LEAF (name) ASM_LINE_SEP \
197 DOARGS_##args ASM_LINE_SEP \
198 DO_CALL_NOERRNO (syscall_name, args) ASM_LINE_SEP \
199 UNDOARGS_##args ASM_LINE_SEP \
202 #define ret_NOERRNO ret
204 #undef PSEUDO_END_NOERRNO
205 #define PSEUDO_END_NOERRNO(name) \
208 /* This has to return the error value */
210 #define PSEUDO_ERRVAL(name, syscall_name, args) \
211 ENTRY_LEAF (name) ASM_LINE_SEP \
212 DOARGS_##args ASM_LINE_SEP \
213 DO_CALL_ERRVAL (syscall_name, args) ASM_LINE_SEP \
214 UNDOARGS_##args ASM_LINE_SEP \
217 #define ret_ERRVAL ret
219 #undef PSEUDO_END_ERRVAL
220 #define PSEUDO_END_ERRVAL(name) \
224 #define JUMPTARGET(name) name
225 #define SYSCALL_PIC_SETUP /* Nothing. */
228 /* FIXME: This comment is not true.
229 * All the syscall assembly macros rely on finding the approriate
230 SYSCALL_ERROR_LABEL or rather HANDLER. */
232 /* int * __errno_location(void) so you have to store your value
233 into the return address! */
234 #define DEFAULT_SYSCALL_ERROR_HANDLER \
235 .import __errno_location,code ASM_LINE_SEP \
236 /* branch to errno handler */ ASM_LINE_SEP \
237 bl __errno_location,%rp ASM_LINE_SEP
239 /* Here are the myriad of configuration options that the above can
240 work for... what we've done is provide the framework for future
241 changes if required to each section */
244 # if RTLD_PRIVATE_ERRNO
245 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
246 # else /* !RTLD_PRIVATE_ERRNO */
247 # if defined _LIBC_REENTRANT
248 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
249 # else /* !_LIBC_REENTRANT */
250 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
251 # endif /* _LIBC_REENTRANT */
252 # endif /* RTLD_PRIVATE_ERRNO */
254 # ifndef _LIBC_REENTRANT
255 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
257 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
262 /* Linux takes system call arguments in registers:
271 The compiler calls us by the C convention:
272 syscall number in the DO_CALL macro
280 gr22 and gr21 are caller-saves, so we can just load the arguments
281 there and generally be happy. */
283 /* the cmpb...no_error code below inside DO_CALL
284 * is intended to mimic the if (__sys_res...)
285 * code inside INLINE_SYSCALL
287 #define NO_ERROR -0x1000
290 #define DO_CALL(syscall_name, args) \
291 /* Create a frame */ ASM_LINE_SEP \
292 stwm TREG, 64(%sp) ASM_LINE_SEP \
293 stw %sp, -4(%sp) ASM_LINE_SEP \
294 stw %r19, -32(%sp) ASM_LINE_SEP \
295 /* Save r19 */ ASM_LINE_SEP \
296 SAVE_PIC(TREG) ASM_LINE_SEP \
297 /* Do syscall, delay loads # */ ASM_LINE_SEP \
298 ble 0x100(%sr2,%r0) ASM_LINE_SEP \
299 ldi SYS_ify (syscall_name), %r20 ASM_LINE_SEP \
300 ldi NO_ERROR,%r1 ASM_LINE_SEP \
301 cmpb,>>=,n %r1,%ret0,L(pre_end) ASM_LINE_SEP \
302 /* Restore r19 from TREG */ ASM_LINE_SEP \
303 LOAD_PIC(TREG) /* delay */ ASM_LINE_SEP \
304 SYSCALL_ERROR_HANDLER ASM_LINE_SEP \
305 /* Use TREG for temp storage */ ASM_LINE_SEP \
306 copy %ret0, TREG /* delay */ ASM_LINE_SEP \
307 /* OPTIMIZE: Don't reload r19 */ ASM_LINE_SEP \
308 /* do a -1*syscall_ret0 */ ASM_LINE_SEP \
309 sub %r0, TREG, TREG ASM_LINE_SEP \
310 /* Store into errno location */ ASM_LINE_SEP \
311 stw TREG, 0(%sr0,%ret0) ASM_LINE_SEP \
312 /* return -1 as error */ ASM_LINE_SEP \
313 ldo -1(%r0), %ret0 ASM_LINE_SEP \
314 L(pre_end): ASM_LINE_SEP \
315 /* Restore our frame, restoring TREG */ ASM_LINE_SEP \
316 ldwm -64(%sp), TREG ASM_LINE_SEP \
317 /* Restore return pointer */ ASM_LINE_SEP \
318 ldw -20(%sp),%rp ASM_LINE_SEP
320 /* We do nothing with the return, except hand it back to someone else */
321 #undef DO_CALL_NOERRNO
322 #define DO_CALL_NOERRNO(syscall_name, args) \
323 /* No need to store r19 */ ASM_LINE_SEP \
324 ble 0x100(%sr2,%r0) ASM_LINE_SEP \
325 ldi SYS_ify (syscall_name), %r20 ASM_LINE_SEP \
326 /* Caller will restore r19 */ ASM_LINE_SEP
328 /* Here, we return the ERRVAL in assembly, note we don't call the
329 error handler function, but we do 'negate' the return _IF_
330 it's an error. Not sure if this is the right semantic. */
332 #undef DO_CALL_ERRVAL
333 #define DO_CALL_ERRVAL(syscall_name, args) \
334 /* No need to store r19 */ ASM_LINE_SEP \
335 ble 0x100(%sr2,%r0) ASM_LINE_SEP \
336 ldi SYS_ify (syscall_name), %r20 ASM_LINE_SEP \
337 /* Caller will restore r19 */ ASM_LINE_SEP \
338 ldi NO_ERROR,%r1 ASM_LINE_SEP \
339 cmpb,>>=,n %r1,%ret0,0f ASM_LINE_SEP \
340 sub %r0, %ret0, %ret0 ASM_LINE_SEP \
346 /* GCC has to be warned that a syscall may clobber all the ABI
347 registers listed as "caller-saves", see page 8, Table 2
348 in section 2.2.6 of the PA-RISC RUN-TIME architecture
349 document. However! r28 is the result and will conflict with
350 the clobber list so it is left out. Also the input arguments
351 registers r20 -> r26 will conflict with the list so they
352 are treated specially. Although r19 is clobbered by the syscall
353 we cannot say this because it would violate ABI, thus we say
354 TREG is clobbered and use that register to save/restore r19
355 across the syscall. */
357 #define CALL_CLOB_REGS "%r1", "%r2", CLOB_TREG \
358 "%r20", "%r29", "%r31"
360 #undef INLINE_SYSCALL
361 #define INLINE_SYSCALL(name, nr, args...) \
365 register unsigned long __res asm("r28"); \
367 LOAD_ARGS_##nr(args) \
368 /* FIXME: HACK save/load r19 around syscall */ \
371 " ble 0x100(%%sr2, %%r0)\n" \
375 : "i" (SYS_ify(name)) PIC_REG_USE ASM_ARGS_##nr \
376 : "memory", CALL_CLOB_REGS CLOB_ARGS_##nr \
378 __sys_res = (long)__res; \
380 if ( (unsigned long)__sys_res >= (unsigned long)-4095 ){ \
381 __set_errno(-__sys_res); \
387 /* INTERNAL_SYSCALL_DECL - Allows us to setup some function static
388 value to use within the context of the syscall
389 INTERNAL_SYSCALL_ERROR_P - Returns 0 if it wasn't an error, 1 otherwise
390 You are allowed to use the syscall result (val) and the DECL error
391 variable to determine what went wrong.
392 INTERLAL_SYSCALL_ERRNO - Munges the val/err pair into the error number.
393 In our case we just flip the sign. */
395 #undef INTERNAL_SYSCALL_DECL
396 #define INTERNAL_SYSCALL_DECL(err)
398 #undef INTERNAL_SYSCALL_ERROR_P
399 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
400 ((val < 0) && (val > -4095))
402 #undef INTERNAL_SYSCALL_ERRNO
403 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
405 /* Similar to INLINE_SYSCALL but we don't set errno */
406 #undef INTERNAL_SYSCALL
407 #define INTERNAL_SYSCALL(name, err, nr, args...) \
411 register unsigned long __res asm("r28"); \
413 LOAD_ARGS_##nr(args) \
414 /* FIXME: HACK save/load r19 around syscall */ \
417 " ble 0x100(%%sr2, %%r0)\n" \
421 : "i" (SYS_ify(name)) PIC_REG_USE ASM_ARGS_##nr \
422 : "memory", CALL_CLOB_REGS CLOB_ARGS_##nr \
424 __sys_res = (long)__res; \
430 /* The _NCS variant allows non-constant syscall numbers. */
431 #undef INTERNAL_SYSCALL_NCS
432 #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
436 register unsigned long __res asm("r28"); \
438 LOAD_ARGS_##nr(args) \
439 /* FIXME: HACK save/load r19 around syscall */ \
442 " ble 0x100(%%sr2, %%r0)\n" \
443 " copy %1, %%r20\n" \
446 : "r" (name) PIC_REG_USE ASM_ARGS_##nr \
447 : "memory", CALL_CLOB_REGS CLOB_ARGS_##nr \
449 __sys_res = (long)__res; \
456 #define LOAD_ARGS_0()
457 #define LOAD_ARGS_1(r26) \
458 register unsigned long __r26 __asm__("r26") = (unsigned long)(r26); \
460 #define LOAD_ARGS_2(r26,r25) \
461 register unsigned long __r25 __asm__("r25") = (unsigned long)(r25); \
463 #define LOAD_ARGS_3(r26,r25,r24) \
464 register unsigned long __r24 __asm__("r24") = (unsigned long)(r24); \
466 #define LOAD_ARGS_4(r26,r25,r24,r23) \
467 register unsigned long __r23 __asm__("r23") = (unsigned long)(r23); \
468 LOAD_ARGS_3(r26,r25,r24)
469 #define LOAD_ARGS_5(r26,r25,r24,r23,r22) \
470 register unsigned long __r22 __asm__("r22") = (unsigned long)(r22); \
471 LOAD_ARGS_4(r26,r25,r24,r23)
472 #define LOAD_ARGS_6(r26,r25,r24,r23,r22,r21) \
473 register unsigned long __r21 __asm__("r21") = (unsigned long)(r21); \
474 LOAD_ARGS_5(r26,r25,r24,r23,r22)
476 /* Even with zero args we use r20 for the syscall number */
478 #define ASM_ARGS_1 ASM_ARGS_0, "r" (__r26)
479 #define ASM_ARGS_2 ASM_ARGS_1, "r" (__r25)
480 #define ASM_ARGS_3 ASM_ARGS_2, "r" (__r24)
481 #define ASM_ARGS_4 ASM_ARGS_3, "r" (__r23)
482 #define ASM_ARGS_5 ASM_ARGS_4, "r" (__r22)
483 #define ASM_ARGS_6 ASM_ARGS_5, "r" (__r21)
485 /* The registers not listed as inputs but clobbered */
487 #define CLOB_ARGS_5 CLOB_ARGS_6, "%r21"
488 #define CLOB_ARGS_4 CLOB_ARGS_5, "%r22"
489 #define CLOB_ARGS_3 CLOB_ARGS_4, "%r23"
490 #define CLOB_ARGS_2 CLOB_ARGS_3, "%r24"
491 #define CLOB_ARGS_1 CLOB_ARGS_2, "%r25"
492 #define CLOB_ARGS_0 CLOB_ARGS_1, "%r26"
494 #endif /* __ASSEMBLER__ */
496 /* Pointer mangling is not yet supported for HPPA. */
497 #define PTR_MANGLE(var) (void) (var)
498 #define PTR_DEMANGLE(var) (void) (var)