chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / ports / sysdeps / unix / sysv / linux / hppa / sysdep.h
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.
7
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.
12
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.
17
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
21    02111-1307 USA.  */
22
23 #include <asm/unistd.h>
24 #include <sysdeps/generic/sysdep.h>
25 #include <sys/syscall.h>
26
27 /* In order to get __set_errno() definition in INLINE_SYSCALL.  */
28 #ifndef __ASSEMBLER__
29 #include <errno.h>
30 #endif
31
32 #undef ASM_LINE_SEP
33 #define ASM_LINE_SEP ! 
34
35 #undef SYS_ify
36 #define SYS_ify(syscall_name)   (__NR_##syscall_name)
37
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 */
41 #ifdef PIC
42 # define TREG %r3
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)
52 #else
53 # define TREG %r3
54 # define SAVE_PIC(SREG) nop ASM_LINE_SEP
55 # define LOAD_PIC(LREG) nop ASM_LINE_SEP
56 /* Inline assembly defines */
57 # define TREG_ASM 
58 # define SAVE_ASM_PIC   "nop \n"
59 # define LOAD_ASM_PIC   "nop \n"
60 # define CLOB_TREG
61 # define PIC_REG_DEF
62 # define PIC_REG_USE
63 #endif
64
65 #ifdef __ASSEMBLER__
66
67 /* Syntactic details of assembler.  */
68
69 #define ALIGNARG(log2) log2
70
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.  */
75 #undef SYS_ify
76 #define SYS_ify(syscall_name)   __NR_##syscall_name
77
78 /* ELF-like local names start with `.L'.  */
79 #undef L
80 #define L(name) .L##name
81
82 /* Linux uses a negative return value to indicate syscall errors,
83    unlike most Unices, which use the condition codes' carry flag.
84
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
91    test with -4095.  */
92
93 /* We don't want the label for the error handle to be global when we define
94    it here.  */
95 /*#ifdef PIC
96 # define SYSCALL_ERROR_LABEL 0f
97 #else
98 # define SYSCALL_ERROR_LABEL syscall_error
99 #endif*/
100
101 /* Argument manipulation from the stack for preparing to
102    make a syscall */
103
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
111
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 */
119
120 /* Define an entry point visible from C.
121
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)                                                     \
126         .text                                           ASM_LINE_SEP    \
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    \
131         .PROC                                           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!) */
137
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)                                                \
141         .text                                           ASM_LINE_SEP    \
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    \
146         .PROC                                           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!) */
152
153 #undef  END
154 #define END(name)                                                       \
155         .EXIT                                           ASM_LINE_SEP    \
156         .PROCEND                                        ASM_LINE_SEP    \
157 .size   C_SYMBOL_NAME(name), .-C_SYMBOL_NAME(name)      ASM_LINE_SEP
158
159 /* If compiled for profiling, call `mcount' at the start 
160    of each function. No, don't bother.  gcc will put the 
161    call in for us.  */
162 #define CALL_MCOUNT             /* Do nothing.  */
163
164 /* syscall wrappers consist of
165         #include <sysdep.h>
166         PSEUDO(...)
167         ret
168         PSEUDO_END(...)
169
170    which means
171         ENTRY(name)
172         DO_CALL(...)
173         nop
174         bv 0(2)
175         nop
176 */
177
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    \
184   nop                                           ASM_LINE_SEP
185
186 #define ret \
187   /* Return value set by ERRNO code */          ASM_LINE_SEP    \
188   bv,n 0(2)                                     ASM_LINE_SEP
189
190 #undef  PSEUDO_END
191 #define PSEUDO_END(name)                                        \
192   END (name)
193
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    \
200   nop                                           ASM_LINE_SEP
201
202 #define ret_NOERRNO ret
203
204 #undef  PSEUDO_END_NOERRNO
205 #define PSEUDO_END_NOERRNO(name)                                \
206   END (name)
207
208 /* This has to return the error value */
209 #undef  PSEUDO_ERRVAL
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    \
215   nop                                           ASM_LINE_SEP
216
217 #define ret_ERRVAL ret
218
219 #undef  PSEUDO_END_ERRVAL
220 #define PSEUDO_END_ERRVAL(name)                                 \
221         END(name)
222
223 #undef JUMPTARGET
224 #define JUMPTARGET(name)        name
225 #define SYSCALL_PIC_SETUP       /* Nothing.  */
226
227
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. */
231
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
238
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 */
242
243 #ifdef PIC
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 */
253 #else
254 # ifndef _LIBC_REENTRANT
255 #  define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
256 # else
257 #  define SYSCALL_ERROR_HANDLER DEFAULT_SYSCALL_ERROR_HANDLER
258 # endif
259 #endif
260
261
262 /* Linux takes system call arguments in registers:
263         syscall number  gr20
264         arg 1           gr26
265         arg 2           gr25
266         arg 3           gr24
267         arg 4           gr23
268         arg 5           gr22
269         arg 6           gr21
270
271    The compiler calls us by the C convention:
272         syscall number  in the DO_CALL macro
273         arg 1           gr26
274         arg 2           gr25
275         arg 3           gr24
276         arg 4           gr23
277         arg 5           -52(sp)
278         arg 6           -56(sp)
279
280    gr22 and gr21 are caller-saves, so we can just load the arguments
281    there and generally be happy. */
282
283 /* the cmpb...no_error code below inside DO_CALL
284  * is intended to mimic the if (__sys_res...)
285  * code inside INLINE_SYSCALL
286  */
287 #define NO_ERROR -0x1000
288
289 #undef  DO_CALL
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
319
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
327
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. */
331
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    \
341 0:                                              ASM_LINE_SEP
342
343
344 #else
345
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. */
356
357 #define CALL_CLOB_REGS  "%r1", "%r2", CLOB_TREG \
358                         "%r20", "%r29", "%r31"
359
360 #undef INLINE_SYSCALL
361 #define INLINE_SYSCALL(name, nr, args...)                               \
362 ({                                                                      \
363         long __sys_res;                                                 \
364         {                                                               \
365                 register unsigned long __res asm("r28");                \
366                 PIC_REG_DEF                                             \
367                 LOAD_ARGS_##nr(args)                                    \
368                 /* FIXME: HACK save/load r19 around syscall */          \
369                 asm volatile(                                           \
370                         SAVE_ASM_PIC                                    \
371                         "       ble  0x100(%%sr2, %%r0)\n"              \
372                         "       ldi %1, %%r20\n"                        \
373                         LOAD_ASM_PIC                                    \
374                         : "=r" (__res)                                  \
375                         : "i" (SYS_ify(name)) PIC_REG_USE ASM_ARGS_##nr \
376                         : "memory", CALL_CLOB_REGS CLOB_ARGS_##nr       \
377                 );                                                      \
378                 __sys_res = (long)__res;                                \
379         }                                                               \
380         if ( (unsigned long)__sys_res >= (unsigned long)-4095 ){        \
381                 __set_errno(-__sys_res);                                \
382                 __sys_res = -1;                                         \
383         }                                                               \
384         __sys_res;                                                      \
385 })
386
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. */
394
395 #undef INTERNAL_SYSCALL_DECL
396 #define INTERNAL_SYSCALL_DECL(err) 
397
398 #undef INTERNAL_SYSCALL_ERROR_P
399 #define INTERNAL_SYSCALL_ERROR_P(val, err) \
400         ((val < 0) && (val > -4095))
401
402 #undef INTERNAL_SYSCALL_ERRNO
403 #define INTERNAL_SYSCALL_ERRNO(val, err) (-(val))
404
405 /* Similar to INLINE_SYSCALL but we don't set errno */
406 #undef INTERNAL_SYSCALL
407 #define INTERNAL_SYSCALL(name, err, nr, args...)                        \
408 ({                                                                      \
409         long __sys_res;                                                 \
410         {                                                               \
411                 register unsigned long __res asm("r28");                \
412                 PIC_REG_DEF                                             \
413                 LOAD_ARGS_##nr(args)                                    \
414                 /* FIXME: HACK save/load r19 around syscall */          \
415                 asm volatile(                                           \
416                         SAVE_ASM_PIC                                    \
417                         "       ble  0x100(%%sr2, %%r0)\n"              \
418                         "       ldi %1, %%r20\n"                        \
419                         LOAD_ASM_PIC                                    \
420                         : "=r" (__res)                                  \
421                         : "i" (SYS_ify(name)) PIC_REG_USE ASM_ARGS_##nr \
422                         : "memory", CALL_CLOB_REGS CLOB_ARGS_##nr       \
423                 );                                                      \
424                 __sys_res = (long)__res;                                \
425         }                                                               \
426         __sys_res;                                                      \
427  })
428
429
430 /* The _NCS variant allows non-constant syscall numbers.  */
431 #undef INTERNAL_SYSCALL_NCS
432 #define INTERNAL_SYSCALL_NCS(name, err, nr, args...)                    \
433 ({                                                                      \
434         long __sys_res;                                                 \
435         {                                                               \
436                 register unsigned long __res asm("r28");                \
437                 PIC_REG_DEF                                             \
438                 LOAD_ARGS_##nr(args)                                    \
439                 /* FIXME: HACK save/load r19 around syscall */          \
440                 asm volatile(                                           \
441                         SAVE_ASM_PIC                                    \
442                         "       ble  0x100(%%sr2, %%r0)\n"              \
443                         "       copy %1, %%r20\n"                       \
444                         LOAD_ASM_PIC                                    \
445                         : "=r" (__res)                                  \
446                         : "r" (name) PIC_REG_USE ASM_ARGS_##nr          \
447                         : "memory", CALL_CLOB_REGS CLOB_ARGS_##nr       \
448                 );                                                      \
449                 __sys_res = (long)__res;                                \
450         }                                                               \
451         __sys_res;                                                      \
452  })
453
454
455
456 #define LOAD_ARGS_0()
457 #define LOAD_ARGS_1(r26)                                                \
458   register unsigned long __r26 __asm__("r26") = (unsigned long)(r26);   \
459   LOAD_ARGS_0()
460 #define LOAD_ARGS_2(r26,r25)                                            \
461   register unsigned long __r25 __asm__("r25") = (unsigned long)(r25);   \
462   LOAD_ARGS_1(r26)
463 #define LOAD_ARGS_3(r26,r25,r24)                                        \
464   register unsigned long __r24 __asm__("r24") = (unsigned long)(r24);   \
465   LOAD_ARGS_2(r26,r25)
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)
475
476 /* Even with zero args we use r20 for the syscall number */
477 #define ASM_ARGS_0
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)
484
485 /* The registers not listed as inputs but clobbered */
486 #define CLOB_ARGS_6
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"
493
494 #endif  /* __ASSEMBLER__ */
495
496 /* Pointer mangling is not yet supported for HPPA.  */
497 #define PTR_MANGLE(var) (void) (var)
498 #define PTR_DEMANGLE(var) (void) (var)
499