Add Changelog ...
[glibc.git] / sysdeps / unix / sysv / linux / hppa / sysdep.h
blob83dd599c68ce1f418b9b0f11a848505f608c721a
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, see
20 <http://www.gnu.org/licenses/>. */
22 #include <asm/unistd.h>
23 #include <sysdeps/generic/sysdep.h>
24 #include <sys/syscall.h>
26 /* In order to get __set_errno() definition in INLINE_SYSCALL. */
27 #ifndef __ASSEMBLER__
28 #include <errno.h>
29 #endif
31 #undef ASM_LINE_SEP
32 #define ASM_LINE_SEP !
34 #undef SYS_ify
35 #define SYS_ify(syscall_name) (__NR_##syscall_name)
37 /* WARNING: TREG must be a callee saves register so
38 that it doesn't have to be restored after a call
39 to another function */
40 #ifdef PIC
41 # define TREG %r3
42 # define SAVE_PIC(SREG) copy %r19, SREG ASM_LINE_SEP
43 # define LOAD_PIC(LREG) copy LREG, %r19 ASM_LINE_SEP
44 /* Inline assembly defines */
45 # define TREG_ASM "%r4" /* Cant clobber r3, it holds framemarker */
46 # define SAVE_ASM_PIC " copy %%r19, %" TREG_ASM "\n"
47 # define LOAD_ASM_PIC " copy %" TREG_ASM ", %%r19\n"
48 # define CLOB_TREG TREG_ASM ,
49 # define PIC_REG_DEF register unsigned long __r19 asm("r19");
50 # define PIC_REG_USE , "r" (__r19)
51 #else
52 # define TREG %r3
53 # define SAVE_PIC(SREG) nop ASM_LINE_SEP
54 # define LOAD_PIC(LREG) nop ASM_LINE_SEP
55 /* Inline assembly defines */
56 # define TREG_ASM
57 # define SAVE_ASM_PIC "nop \n"
58 # define LOAD_ASM_PIC "nop \n"
59 # define CLOB_TREG
60 # define PIC_REG_DEF
61 # define PIC_REG_USE
62 #endif
64 #ifdef __ASSEMBLER__
66 /* Syntactic details of assembler. */
68 #define ALIGNARG(log2) log2
70 /* For Linux we can use the system call table in the header file
71 /usr/include/asm/unistd.h
72 of the kernel. But these symbols do not follow the SYS_* syntax
73 so we have to redefine the `SYS_ify' macro here. */
74 #undef SYS_ify
75 #define SYS_ify(syscall_name) __NR_##syscall_name
77 /* ELF-like local names start with `.L'. */
78 #undef L
79 #define L(name) .L##name
81 /* Linux uses a negative return value to indicate syscall errors,
82 unlike most Unices, which use the condition codes' carry flag.
84 Since version 2.1 the return value of a system call might be
85 negative even if the call succeeded. E.g., the `lseek' system call
86 might return a large offset. Therefore we must not anymore test
87 for < 0, but test for a real error by making sure the value in %eax
88 is a real error number. Linus said he will make sure the no syscall
89 returns a value in -1 .. -4095 as a valid result so we can safely
90 test with -4095. */
92 /* We don't want the label for the error handle to be global when we define
93 it here. */
94 /*#ifdef PIC
95 # define SYSCALL_ERROR_LABEL 0f
96 #else
97 # define SYSCALL_ERROR_LABEL syscall_error
98 #endif*/
100 /* Argument manipulation from the stack for preparing to
101 make a syscall */
103 #define DOARGS_0 /* nothing */
104 #define DOARGS_1 /* nothing */
105 #define DOARGS_2 /* nothing */
106 #define DOARGS_3 /* nothing */
107 #define DOARGS_4 /* nothing */
108 #define DOARGS_5 ldw -52(%sp), %r22 ASM_LINE_SEP
109 #define DOARGS_6 DOARGS_5 ldw -56(%sp), %r21 ASM_LINE_SEP
111 #define UNDOARGS_0 /* nothing */
112 #define UNDOARGS_1 /* nothing */
113 #define UNDOARGS_2 /* nothing */
114 #define UNDOARGS_3 /* nothing */
115 #define UNDOARGS_4 /* nothing */
116 #define UNDOARGS_5 /* nothing */
117 #define UNDOARGS_6 /* nothing */
119 /* Define an entry point visible from C.
121 There is currently a bug in gdb which prevents us from specifying
122 incomplete stabs information. Fake some entries here which specify
123 the current source file. */
124 #define ENTRY(name) \
125 .text ASM_LINE_SEP \
126 .align ALIGNARG(4) ASM_LINE_SEP \
127 .export C_SYMBOL_NAME(name) ASM_LINE_SEP \
128 .type C_SYMBOL_NAME(name),@function ASM_LINE_SEP \
129 C_LABEL(name) ASM_LINE_SEP \
130 .PROC ASM_LINE_SEP \
131 .CALLINFO FRAME=64,CALLS,SAVE_RP,ENTRY_GR=3 ASM_LINE_SEP \
132 .ENTRY ASM_LINE_SEP \
133 /* SAVE_RP says we do */ ASM_LINE_SEP \
134 stw %rp, -20(%sr0,%sp) ASM_LINE_SEP \
135 /*FIXME: Call mcount? (carefull with stack!) */
137 /* Some syscall wrappers do not call other functions, and
138 hence are classified as leaf, so add NO_CALLS for gdb */
139 #define ENTRY_LEAF(name) \
140 .text ASM_LINE_SEP \
141 .align ALIGNARG(4) ASM_LINE_SEP \
142 .export C_SYMBOL_NAME(name) ASM_LINE_SEP \
143 .type C_SYMBOL_NAME(name),@function ASM_LINE_SEP \
144 C_LABEL(name) ASM_LINE_SEP \
145 .PROC ASM_LINE_SEP \
146 .CALLINFO FRAME=64,NO_CALLS,SAVE_RP,ENTRY_GR=3 ASM_LINE_SEP \
147 .ENTRY ASM_LINE_SEP \
148 /* SAVE_RP says we do */ ASM_LINE_SEP \
149 stw %rp, -20(%sr0,%sp) ASM_LINE_SEP \
150 /*FIXME: Call mcount? (carefull with stack!) */
152 #undef END
153 #define END(name) \
154 .EXIT ASM_LINE_SEP \
155 .PROCEND ASM_LINE_SEP \
156 .size C_SYMBOL_NAME(name), .-C_SYMBOL_NAME(name) ASM_LINE_SEP
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. */
163 /* syscall wrappers consist of
164 #include <sysdep.h>
165 PSEUDO(...)
167 PSEUDO_END(...)
169 which means
170 ENTRY(name)
171 DO_CALL(...)
173 bv 0(2)
177 #define PSEUDO(name, syscall_name, args) \
178 ENTRY (name) ASM_LINE_SEP \
179 /* If necc. load args from stack */ ASM_LINE_SEP \
180 DOARGS_##args ASM_LINE_SEP \
181 DO_CALL (syscall_name, args) ASM_LINE_SEP \
182 UNDOARGS_##args ASM_LINE_SEP \
183 nop ASM_LINE_SEP
185 #define ret \
186 /* Return value set by ERRNO code */ ASM_LINE_SEP \
187 bv,n 0(2) ASM_LINE_SEP
189 #undef PSEUDO_END
190 #define PSEUDO_END(name) \
191 END (name)
193 /* We don't set the errno on the return from the syscall */
194 #define PSEUDO_NOERRNO(name, syscall_name, args) \
195 ENTRY_LEAF (name) ASM_LINE_SEP \
196 DOARGS_##args ASM_LINE_SEP \
197 DO_CALL_NOERRNO (syscall_name, args) ASM_LINE_SEP \
198 UNDOARGS_##args ASM_LINE_SEP \
199 nop ASM_LINE_SEP
201 #define ret_NOERRNO ret
203 #undef PSEUDO_END_NOERRNO
204 #define PSEUDO_END_NOERRNO(name) \
205 END (name)
207 /* This has to return the error value */
208 #undef PSEUDO_ERRVAL
209 #define PSEUDO_ERRVAL(name, syscall_name, args) \
210 ENTRY_LEAF (name) ASM_LINE_SEP \
211 DOARGS_##args ASM_LINE_SEP \
212 DO_CALL_ERRVAL (syscall_name, args) ASM_LINE_SEP \
213 UNDOARGS_##args ASM_LINE_SEP \
214 nop ASM_LINE_SEP
216 #define ret_ERRVAL ret
218 #undef PSEUDO_END_ERRVAL
219 #define PSEUDO_END_ERRVAL(name) \
220 END(name)
222 #undef JUMPTARGET
223 #define JUMPTARGET(name) name
224 #define SYSCALL_PIC_SETUP /* Nothing. */
227 /* FIXME: This comment is not true.
228 * All the syscall assembly macros rely on finding the approriate
229 SYSCALL_ERROR_LABEL or rather HANDLER. */
231 /* int * __errno_location(void) so you have to store your value
232 into the return address! */
233 #define DEFAULT_SYSCALL_ERROR_HANDLER \
234 .import __errno_location,code ASM_LINE_SEP \
235 /* branch to errno handler */ ASM_LINE_SEP \
236 bl __errno_location,%rp ASM_LINE_SEP
238 /* Here are the myriad of configuration options that the above can
239 work for... what we've done is provide the framework for future
240 changes if required to each section */
242 #ifdef PIC
243 # if RTLD_PRIVATE_ERRNO
244 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
245 # else /* !RTLD_PRIVATE_ERRNO */
246 # if defined _LIBC_REENTRANT
247 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
248 # else /* !_LIBC_REENTRANT */
249 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
250 # endif /* _LIBC_REENTRANT */
251 # endif /* RTLD_PRIVATE_ERRNO */
252 #else
253 # ifndef _LIBC_REENTRANT
254 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
255 # else
256 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
257 # endif
258 #endif
261 /* Linux takes system call arguments in registers:
262 syscall number gr20
263 arg 1 gr26
264 arg 2 gr25
265 arg 3 gr24
266 arg 4 gr23
267 arg 5 gr22
268 arg 6 gr21
270 The compiler calls us by the C convention:
271 syscall number in the DO_CALL macro
272 arg 1 gr26
273 arg 2 gr25
274 arg 3 gr24
275 arg 4 gr23
276 arg 5 -52(sp)
277 arg 6 -56(sp)
279 gr22 and gr21 are caller-saves, so we can just load the arguments
280 there and generally be happy. */
282 /* the cmpb...no_error code below inside DO_CALL
283 * is intended to mimic the if (__sys_res...)
284 * code inside INLINE_SYSCALL
286 #define NO_ERROR -0x1000
288 #undef DO_CALL
289 #define DO_CALL(syscall_name, args) \
290 /* Create a frame */ ASM_LINE_SEP \
291 stwm TREG, 64(%sp) ASM_LINE_SEP \
292 stw %sp, -4(%sp) ASM_LINE_SEP \
293 stw %r19, -32(%sp) ASM_LINE_SEP \
294 /* Save r19 */ ASM_LINE_SEP \
295 SAVE_PIC(TREG) ASM_LINE_SEP \
296 /* Do syscall, delay loads # */ ASM_LINE_SEP \
297 ble 0x100(%sr2,%r0) ASM_LINE_SEP \
298 ldi SYS_ify (syscall_name), %r20 ASM_LINE_SEP \
299 ldi NO_ERROR,%r1 ASM_LINE_SEP \
300 cmpb,>>=,n %r1,%ret0,L(pre_end) ASM_LINE_SEP \
301 /* Restore r19 from TREG */ ASM_LINE_SEP \
302 LOAD_PIC(TREG) /* delay */ ASM_LINE_SEP \
303 SYSCALL_ERROR_HANDLER ASM_LINE_SEP \
304 /* Use TREG for temp storage */ ASM_LINE_SEP \
305 copy %ret0, TREG /* delay */ ASM_LINE_SEP \
306 /* OPTIMIZE: Don't reload r19 */ ASM_LINE_SEP \
307 /* do a -1*syscall_ret0 */ ASM_LINE_SEP \
308 sub %r0, TREG, TREG ASM_LINE_SEP \
309 /* Store into errno location */ ASM_LINE_SEP \
310 stw TREG, 0(%sr0,%ret0) ASM_LINE_SEP \
311 /* return -1 as error */ ASM_LINE_SEP \
312 ldo -1(%r0), %ret0 ASM_LINE_SEP \
313 L(pre_end): ASM_LINE_SEP \
314 /* Restore our frame, restoring TREG */ ASM_LINE_SEP \
315 ldwm -64(%sp), TREG ASM_LINE_SEP \
316 /* Restore return pointer */ ASM_LINE_SEP \
317 ldw -20(%sp),%rp ASM_LINE_SEP
319 /* We do nothing with the return, except hand it back to someone else */
320 #undef DO_CALL_NOERRNO
321 #define DO_CALL_NOERRNO(syscall_name, args) \
322 /* No need to store r19 */ ASM_LINE_SEP \
323 ble 0x100(%sr2,%r0) ASM_LINE_SEP \
324 ldi SYS_ify (syscall_name), %r20 ASM_LINE_SEP \
325 /* Caller will restore r19 */ ASM_LINE_SEP
327 /* Here, we return the ERRVAL in assembly, note we don't call the
328 error handler function, but we do 'negate' the return _IF_
329 it's an error. Not sure if this is the right semantic. */
331 #undef DO_CALL_ERRVAL
332 #define DO_CALL_ERRVAL(syscall_name, args) \
333 /* No need to store r19 */ ASM_LINE_SEP \
334 ble 0x100(%sr2,%r0) ASM_LINE_SEP \
335 ldi SYS_ify (syscall_name), %r20 ASM_LINE_SEP \
336 /* Caller will restore r19 */ ASM_LINE_SEP \
337 ldi NO_ERROR,%r1 ASM_LINE_SEP \
338 cmpb,>>=,n %r1,%ret0,0f ASM_LINE_SEP \
339 sub %r0, %ret0, %ret0 ASM_LINE_SEP \
340 0: ASM_LINE_SEP
343 #else
345 /* GCC has to be warned that a syscall may clobber all the ABI
346 registers listed as "caller-saves", see page 8, Table 2
347 in section 2.2.6 of the PA-RISC RUN-TIME architecture
348 document. However! r28 is the result and will conflict with
349 the clobber list so it is left out. Also the input arguments
350 registers r20 -> r26 will conflict with the list so they
351 are treated specially. Although r19 is clobbered by the syscall
352 we cannot say this because it would violate ABI, thus we say
353 TREG is clobbered and use that register to save/restore r19
354 across the syscall. */
356 #define CALL_CLOB_REGS "%r1", "%r2", CLOB_TREG \
357 "%r20", "%r29", "%r31"
359 #undef INLINE_SYSCALL
360 #define INLINE_SYSCALL(name, nr, args...) \
361 ({ \
362 long __sys_res; \
364 register unsigned long __res asm("r28"); \
365 PIC_REG_DEF \
366 LOAD_ARGS_##nr(args) \
367 /* FIXME: HACK save/load r19 around syscall */ \
368 asm volatile( \
369 SAVE_ASM_PIC \
370 " ble 0x100(%%sr2, %%r0)\n" \
371 " ldi %1, %%r20\n" \
372 LOAD_ASM_PIC \
373 : "=r" (__res) \
374 : "i" (SYS_ify(name)) PIC_REG_USE ASM_ARGS_##nr \
375 : "memory", CALL_CLOB_REGS CLOB_ARGS_##nr \
376 ); \
377 __sys_res = (long)__res; \
379 if ( (unsigned long)__sys_res >= (unsigned long)-4095 ){ \
380 __set_errno(-__sys_res); \
381 __sys_res = -1; \
383 __sys_res; \
386 /* INTERNAL_SYSCALL_DECL - Allows us to setup some function static
387 value to use within the context of the syscall
388 INTERNAL_SYSCALL_ERROR_P - Returns 0 if it wasn't an error, 1 otherwise
389 You are allowed to use the syscall result (val) and the DECL error
390 variable to determine what went wrong.
391 INTERLAL_SYSCALL_ERRNO - Munges the val/err pair into the error number.
392 In our case we just flip the sign. */
394 #undef INTERNAL_SYSCALL_DECL
395 #define INTERNAL_SYSCALL_DECL(err)
397 #undef INTERNAL_SYSCALL_ERROR_P
398 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
399 ((val < 0) && (val > -4095))
401 #undef INTERNAL_SYSCALL_ERRNO
402 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
404 /* Similar to INLINE_SYSCALL but we don't set errno */
405 #undef INTERNAL_SYSCALL
406 #define INTERNAL_SYSCALL(name, err, nr, args...) \
407 ({ \
408 long __sys_res; \
410 register unsigned long __res asm("r28"); \
411 PIC_REG_DEF \
412 LOAD_ARGS_##nr(args) \
413 /* FIXME: HACK save/load r19 around syscall */ \
414 asm volatile( \
415 SAVE_ASM_PIC \
416 " ble 0x100(%%sr2, %%r0)\n" \
417 " ldi %1, %%r20\n" \
418 LOAD_ASM_PIC \
419 : "=r" (__res) \
420 : "i" (SYS_ify(name)) PIC_REG_USE ASM_ARGS_##nr \
421 : "memory", CALL_CLOB_REGS CLOB_ARGS_##nr \
422 ); \
423 __sys_res = (long)__res; \
425 __sys_res; \
429 /* The _NCS variant allows non-constant syscall numbers. */
430 #undef INTERNAL_SYSCALL_NCS
431 #define INTERNAL_SYSCALL_NCS(name, err, nr, args...) \
432 ({ \
433 long __sys_res; \
435 register unsigned long __res asm("r28"); \
436 PIC_REG_DEF \
437 LOAD_ARGS_##nr(args) \
438 /* FIXME: HACK save/load r19 around syscall */ \
439 asm volatile( \
440 SAVE_ASM_PIC \
441 " ble 0x100(%%sr2, %%r0)\n" \
442 " copy %1, %%r20\n" \
443 LOAD_ASM_PIC \
444 : "=r" (__res) \
445 : "r" (name) PIC_REG_USE ASM_ARGS_##nr \
446 : "memory", CALL_CLOB_REGS CLOB_ARGS_##nr \
447 ); \
448 __sys_res = (long)__res; \
450 __sys_res; \
455 #define LOAD_ARGS_0()
456 #define LOAD_ARGS_1(r26) \
457 register unsigned long __r26 __asm__("r26") = (unsigned long)(r26); \
458 LOAD_ARGS_0()
459 #define LOAD_ARGS_2(r26,r25) \
460 register unsigned long __r25 __asm__("r25") = (unsigned long)(r25); \
461 LOAD_ARGS_1(r26)
462 #define LOAD_ARGS_3(r26,r25,r24) \
463 register unsigned long __r24 __asm__("r24") = (unsigned long)(r24); \
464 LOAD_ARGS_2(r26,r25)
465 #define LOAD_ARGS_4(r26,r25,r24,r23) \
466 register unsigned long __r23 __asm__("r23") = (unsigned long)(r23); \
467 LOAD_ARGS_3(r26,r25,r24)
468 #define LOAD_ARGS_5(r26,r25,r24,r23,r22) \
469 register unsigned long __r22 __asm__("r22") = (unsigned long)(r22); \
470 LOAD_ARGS_4(r26,r25,r24,r23)
471 #define LOAD_ARGS_6(r26,r25,r24,r23,r22,r21) \
472 register unsigned long __r21 __asm__("r21") = (unsigned long)(r21); \
473 LOAD_ARGS_5(r26,r25,r24,r23,r22)
475 /* Even with zero args we use r20 for the syscall number */
476 #define ASM_ARGS_0
477 #define ASM_ARGS_1 ASM_ARGS_0, "r" (__r26)
478 #define ASM_ARGS_2 ASM_ARGS_1, "r" (__r25)
479 #define ASM_ARGS_3 ASM_ARGS_2, "r" (__r24)
480 #define ASM_ARGS_4 ASM_ARGS_3, "r" (__r23)
481 #define ASM_ARGS_5 ASM_ARGS_4, "r" (__r22)
482 #define ASM_ARGS_6 ASM_ARGS_5, "r" (__r21)
484 /* The registers not listed as inputs but clobbered */
485 #define CLOB_ARGS_6
486 #define CLOB_ARGS_5 CLOB_ARGS_6, "%r21"
487 #define CLOB_ARGS_4 CLOB_ARGS_5, "%r22"
488 #define CLOB_ARGS_3 CLOB_ARGS_4, "%r23"
489 #define CLOB_ARGS_2 CLOB_ARGS_3, "%r24"
490 #define CLOB_ARGS_1 CLOB_ARGS_2, "%r25"
491 #define CLOB_ARGS_0 CLOB_ARGS_1, "%r26"
493 #endif /* __ASSEMBLER__ */
495 /* Pointer mangling is not yet supported for HPPA. */
496 #define PTR_MANGLE(var) (void) (var)
497 #define PTR_DEMANGLE(var) (void) (var)