Update copyright notices with scripts/update-copyrights
[glibc.git] / ports / sysdeps / unix / sysv / linux / hppa / sysdep.h
blob20af8a76f102a4644ace5fc9b8379fea29ae1a60
1 /* Assembler macros for PA-RISC.
2 Copyright (C) 1999-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper, <drepper@cygnus.com>, August 1999.
5 Linux/PA-RISC changes by Philipp Rumpf, <prumpf@tux.org>, March 2000.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library. If not, see
19 <http://www.gnu.org/licenses/>. */
21 #include <asm/unistd.h>
22 #include <sysdeps/generic/sysdep.h>
24 /* In order to get __set_errno() definition in INLINE_SYSCALL. */
25 #ifndef __ASSEMBLER__
26 #include <errno.h>
27 #endif
29 #undef ASM_LINE_SEP
30 #define ASM_LINE_SEP !
32 #undef SYS_ify
33 #define SYS_ify(syscall_name) (__NR_##syscall_name)
35 /* The vfork, fork, and clone syscalls clobber r19
36 * and r21. We list r21 as either clobbered or as an
37 * input to a 6-argument syscall. We must save and
38 * restore r19 in both PIC and non-PIC cases.
40 /* WARNING: TREG must be a callee saves register so
41 that it doesn't have to be restored after a call
42 to another function */
43 #define TREG 4
44 #define SAVE_PIC(SREG) \
45 copy %r19, SREG ASM_LINE_SEP \
46 .cfi_register 19, SREG
47 #define LOAD_PIC(LREG) \
48 copy LREG , %r19 ASM_LINE_SEP \
49 .cfi_restore 19
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)
58 #ifdef __ASSEMBLER__
60 /* Syntactic details of assembler. */
62 #define ALIGNARG(log2) log2
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
71 /* ELF-like local names start with `.L'. */
72 #undef L
73 #define L(name) .L##name
75 /* Linux uses a negative return value to indicate syscall errors,
76 unlike most Unices, which use the condition codes' carry flag.
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. */
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*/
94 /* Argument manipulation from the stack for preparing to
95 make a syscall */
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
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 */
113 /* Define an entry point visible from C.
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 #define ENTRY(name) \
119 .text ASM_LINE_SEP \
120 .align ALIGNARG(4) ASM_LINE_SEP \
121 .export C_SYMBOL_NAME(name) ASM_LINE_SEP \
122 .type C_SYMBOL_NAME(name),@function ASM_LINE_SEP \
123 cfi_startproc ASM_LINE_SEP \
124 C_LABEL(name) ASM_LINE_SEP \
125 .PROC ASM_LINE_SEP \
126 .CALLINFO FRAME=64,CALLS,SAVE_RP,ENTRY_GR=3 ASM_LINE_SEP \
127 .ENTRY ASM_LINE_SEP \
128 /* SAVE_RP says we do */ ASM_LINE_SEP \
129 stw %rp, -20(%sr0,%sp) ASM_LINE_SEP \
130 .cfi_offset 2, -20 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) \
136 .text ASM_LINE_SEP \
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 cfi_startproc ASM_LINE_SEP \
141 C_LABEL(name) ASM_LINE_SEP \
142 .PROC ASM_LINE_SEP \
143 .CALLINFO FRAME=64,NO_CALLS,SAVE_RP,ENTRY_GR=3 ASM_LINE_SEP \
144 .ENTRY ASM_LINE_SEP \
145 /* SAVE_RP says we do */ ASM_LINE_SEP \
146 stw %rp, -20(%sr0,%sp) ASM_LINE_SEP \
147 .cfi_offset 2, -20 ASM_LINE_SEP \
148 /*FIXME: Call mcount? (carefull with stack!) */
150 #undef END
151 #define END(name) \
152 .EXIT ASM_LINE_SEP \
153 .PROCEND ASM_LINE_SEP \
154 cfi_endproc ASM_LINE_SEP \
155 .size C_SYMBOL_NAME(name), .-C_SYMBOL_NAME(name) ASM_LINE_SEP
157 /* If compiled for profiling, call `mcount' at the start
158 of each function. No, don't bother. gcc will put the
159 call in for us. */
160 #define CALL_MCOUNT /* Do nothing. */
162 /* syscall wrappers consist of
163 #include <sysdep.h>
164 PSEUDO(...)
166 PSEUDO_END(...)
168 which means
169 ENTRY(name)
170 DO_CALL(...)
171 bv,n 0(2)
174 #define PSEUDO(name, syscall_name, args) \
175 ENTRY (name) ASM_LINE_SEP \
176 /* If necc. load args from stack */ ASM_LINE_SEP \
177 DOARGS_##args ASM_LINE_SEP \
178 DO_CALL (syscall_name, args) ASM_LINE_SEP \
179 UNDOARGS_##args ASM_LINE_SEP
181 #define ret \
182 /* Return value set by ERRNO code */ ASM_LINE_SEP \
183 bv,n 0(2) ASM_LINE_SEP
185 #undef PSEUDO_END
186 #define PSEUDO_END(name) \
187 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
196 #define ret_NOERRNO ret
198 #undef PSEUDO_END_NOERRNO
199 #define PSEUDO_END_NOERRNO(name) \
200 END (name)
202 /* This has to return the error value */
203 #undef PSEUDO_ERRVAL
204 #define PSEUDO_ERRVAL(name, syscall_name, args) \
205 ENTRY_LEAF (name) ASM_LINE_SEP \
206 DOARGS_##args ASM_LINE_SEP \
207 DO_CALL_ERRVAL (syscall_name, args) ASM_LINE_SEP \
208 UNDOARGS_##args ASM_LINE_SEP
210 #define ret_ERRVAL ret
212 #undef PSEUDO_END_ERRVAL
213 #define PSEUDO_END_ERRVAL(name) \
214 END(name)
216 #undef JUMPTARGET
217 #define JUMPTARGET(name) name
218 #define SYSCALL_PIC_SETUP /* Nothing. */
221 /* FIXME: This comment is not true.
222 * All the syscall assembly macros rely on finding the appropriate
223 SYSCALL_ERROR_LABEL or rather HANDLER. */
225 /* int * __errno_location(void) so you have to store your value
226 into the return address! */
227 #define DEFAULT_SYSCALL_ERROR_HANDLER \
228 .import __errno_location,code ASM_LINE_SEP \
229 /* branch to errno handler */ ASM_LINE_SEP \
230 bl __errno_location,%rp ASM_LINE_SEP
232 /* Here are the myriad of configuration options that the above can
233 work for... what we've done is provide the framework for future
234 changes if required to each section */
236 #ifdef PIC
237 # if RTLD_PRIVATE_ERRNO
238 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
239 # else /* !RTLD_PRIVATE_ERRNO */
240 # if defined _LIBC_REENTRANT
241 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
242 # else /* !_LIBC_REENTRANT */
243 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
244 # endif /* _LIBC_REENTRANT */
245 # endif /* RTLD_PRIVATE_ERRNO */
246 #else
247 # ifndef _LIBC_REENTRANT
248 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
249 # else
250 # define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
251 # endif
252 #endif
255 /* Linux takes system call arguments in registers:
256 syscall number gr20
257 arg 1 gr26
258 arg 2 gr25
259 arg 3 gr24
260 arg 4 gr23
261 arg 5 gr22
262 arg 6 gr21
264 The compiler calls us by the C convention:
265 syscall number in the DO_CALL macro
266 arg 1 gr26
267 arg 2 gr25
268 arg 3 gr24
269 arg 4 gr23
270 arg 5 -52(sp)
271 arg 6 -56(sp)
273 gr22 and gr21 are caller-saves, so we can just load the arguments
274 there and generally be happy. */
276 /* the cmpb...no_error code below inside DO_CALL
277 * is intended to mimic the if (__sys_res...)
278 * code inside INLINE_SYSCALL
280 #define NO_ERROR -0x1000
282 #undef DO_CALL
283 #define DO_CALL(syscall_name, args) \
284 /* Create a frame */ ASM_LINE_SEP \
285 stwm TREG, 64(%sp) ASM_LINE_SEP \
286 .cfi_offset TREG, 0 ASM_LINE_SEP \
287 .cfi_adjust_cfa_offset 64 ASM_LINE_SEP \
288 stw %sp, -4(%sp) ASM_LINE_SEP \
289 .cfi_offset 30, -4 ASM_LINE_SEP \
290 stw %r19, -32(%sp) ASM_LINE_SEP \
291 .cfi_offset 19, -32 ASM_LINE_SEP \
292 /* Save r19 */ ASM_LINE_SEP \
293 SAVE_PIC(TREG) ASM_LINE_SEP \
294 /* Do syscall, delay loads # */ ASM_LINE_SEP \
295 ble 0x100(%sr2,%r0) ASM_LINE_SEP \
296 ldi SYS_ify (syscall_name), %r20 ASM_LINE_SEP \
297 ldi NO_ERROR,%r1 ASM_LINE_SEP \
298 cmpb,>>=,n %r1,%ret0,L(pre_end) ASM_LINE_SEP \
299 /* Restore r19 from TREG */ ASM_LINE_SEP \
300 LOAD_PIC(TREG) /* delay */ ASM_LINE_SEP \
301 SYSCALL_ERROR_HANDLER ASM_LINE_SEP \
302 /* Use TREG for temp storage */ ASM_LINE_SEP \
303 copy %ret0, TREG /* delay */ ASM_LINE_SEP \
304 /* OPTIMIZE: Don't reload r19 */ ASM_LINE_SEP \
305 /* do a -1*syscall_ret0 */ ASM_LINE_SEP \
306 sub %r0, TREG, TREG ASM_LINE_SEP \
307 /* Store into errno location */ ASM_LINE_SEP \
308 stw TREG, 0(%sr0,%ret0) ASM_LINE_SEP \
309 /* return -1 as error */ ASM_LINE_SEP \
310 ldo -1(%r0), %ret0 ASM_LINE_SEP \
311 L(pre_end): ASM_LINE_SEP \
312 /* Restore our frame, restoring TREG */ ASM_LINE_SEP \
313 ldwm -64(%sp), TREG ASM_LINE_SEP \
314 .cfi_adjust_cfa_offset -64 ASM_LINE_SEP \
315 /* Restore return pointer */ ASM_LINE_SEP \
316 ldw -20(%sp),%rp ASM_LINE_SEP \
317 .cfi_restore 2 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)