powerpc64: Add POWER8 strnlen
[glibc.git] / sysdeps / powerpc / powerpc64 / sysdep.h
blobdb7c1d78b523c634be71fcf6b42f35d80defa0d7
1 /* Assembly macros for 64-bit PowerPC.
2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <sysdeps/powerpc/sysdep.h>
21 #ifdef __ASSEMBLER__
23 /* Stack frame offsets. */
24 #if _CALL_ELF != 2
25 #define FRAME_MIN_SIZE 112
26 #define FRAME_MIN_SIZE_PARM 112
27 #define FRAME_BACKCHAIN 0
28 #define FRAME_CR_SAVE 8
29 #define FRAME_LR_SAVE 16
30 #define FRAME_TOC_SAVE 40
31 #define FRAME_PARM_SAVE 48
32 #define FRAME_PARM1_SAVE 48
33 #define FRAME_PARM2_SAVE 56
34 #define FRAME_PARM3_SAVE 64
35 #define FRAME_PARM4_SAVE 72
36 #define FRAME_PARM5_SAVE 80
37 #define FRAME_PARM6_SAVE 88
38 #define FRAME_PARM7_SAVE 96
39 #define FRAME_PARM8_SAVE 104
40 #define FRAME_PARM9_SAVE 112
41 #else
42 #define FRAME_MIN_SIZE 32
43 #define FRAME_MIN_SIZE_PARM 96
44 #define FRAME_BACKCHAIN 0
45 #define FRAME_CR_SAVE 8
46 #define FRAME_LR_SAVE 16
47 #define FRAME_TOC_SAVE 24
48 #define FRAME_PARM_SAVE 32
49 #define FRAME_PARM1_SAVE 32
50 #define FRAME_PARM2_SAVE 40
51 #define FRAME_PARM3_SAVE 48
52 #define FRAME_PARM4_SAVE 56
53 #define FRAME_PARM5_SAVE 64
54 #define FRAME_PARM6_SAVE 72
55 #define FRAME_PARM7_SAVE 80
56 #define FRAME_PARM8_SAVE 88
57 #define FRAME_PARM9_SAVE 96
58 #endif
60 /* Support macros for CALL_MCOUNT. */
61 #if _CALL_ELF == 2
62 #define call_mcount_parm_offset (-64)
63 #else
64 #define call_mcount_parm_offset FRAME_PARM_SAVE
65 #endif
66 .macro SAVE_ARG NARG
67 .if \NARG
68 SAVE_ARG \NARG-1
69 std 2+\NARG,call_mcount_parm_offset-8+8*(\NARG)(1)
70 .endif
71 .endm
73 .macro REST_ARG NARG
74 .if \NARG
75 REST_ARG \NARG-1
76 ld 2+\NARG,FRAME_MIN_SIZE_PARM+call_mcount_parm_offset-8+8*(\NARG)(1)
77 .endif
78 .endm
80 .macro CFI_SAVE_ARG NARG
81 .if \NARG
82 CFI_SAVE_ARG \NARG-1
83 cfi_offset(2+\NARG,call_mcount_parm_offset-8+8*(\NARG))
84 .endif
85 .endm
87 .macro CFI_REST_ARG NARG
88 .if \NARG
89 CFI_REST_ARG \NARG-1
90 cfi_restore(2+\NARG)
91 .endif
92 .endm
94 /* If compiled for profiling, call `_mcount' at the start of each function.
95 see ppc-mcount.S for more details. */
96 .macro CALL_MCOUNT NARG
97 #ifdef PROF
98 mflr r0
99 SAVE_ARG \NARG
100 std r0,FRAME_LR_SAVE(r1)
101 stdu r1,-FRAME_MIN_SIZE_PARM(r1)
102 cfi_adjust_cfa_offset(FRAME_MIN_SIZE_PARM)
103 cfi_offset(lr,FRAME_LR_SAVE)
104 CFI_SAVE_ARG \NARG
105 bl JUMPTARGET (_mcount)
106 #ifndef SHARED
108 #endif
109 ld r0,FRAME_MIN_SIZE_PARM+FRAME_LR_SAVE(r1)
110 REST_ARG \NARG
111 mtlr r0
112 addi r1,r1,FRAME_MIN_SIZE_PARM
113 cfi_adjust_cfa_offset(-FRAME_MIN_SIZE_PARM)
114 cfi_restore(lr)
115 CFI_REST_ARG \NARG
116 #endif
117 .endm
119 #if _CALL_ELF != 2
121 /* Macro to prepare for calling via a function pointer. */
122 .macro PPC64_LOAD_FUNCPTR PTR
123 ld r12,0(\PTR)
124 ld r2,8(\PTR)
125 mtctr r12
126 ld r11,16(\PTR)
127 .endm
129 #ifdef USE_PPC64_OVERLAPPING_OPD
130 # define OPD_ENT(name) .quad BODY_LABEL (name), .TOC.@tocbase
131 #else
132 # define OPD_ENT(name) .quad BODY_LABEL (name), .TOC.@tocbase, 0
133 #endif
135 #define ENTRY_1(name) \
136 .type BODY_LABEL(name),@function; \
137 .globl name; \
138 .section ".opd","aw"; \
139 .align 3; \
140 name##: OPD_ENT (name); \
141 .previous;
143 #define DOT_LABEL(X) X
144 #define BODY_LABEL(X) .LY##X
145 #define ENTRY_2(name) \
146 .type name,@function; \
147 ENTRY_1(name)
148 #define END_2(name) \
149 .size name,.-BODY_LABEL(name); \
150 .size BODY_LABEL(name),.-BODY_LABEL(name);
151 #define LOCALENTRY(name)
153 #else /* _CALL_ELF */
155 /* Macro to prepare for calling via a function pointer. */
156 .macro PPC64_LOAD_FUNCPTR PTR
157 mr r12,\PTR
158 mtctr r12
159 .endm
161 #define DOT_LABEL(X) X
162 #define BODY_LABEL(X) X
163 #define ENTRY_2(name) \
164 .globl name; \
165 .type name,@function;
166 #define END_2(name) \
167 .size name,.-name;
168 #define LOCALENTRY(name) \
169 1: addis r2,r12,.TOC.-1b@ha; \
170 addi r2,r2,.TOC.-1b@l; \
171 .localentry name,.-name;
173 #endif /* _CALL_ELF */
175 #define ENTRY(name) \
176 .section ".text"; \
177 ENTRY_2(name) \
178 .align ALIGNARG(2); \
179 BODY_LABEL(name): \
180 cfi_startproc; \
181 LOCALENTRY(name)
183 #define EALIGN_W_0 /* No words to insert. */
184 #define EALIGN_W_1 nop
185 #define EALIGN_W_2 nop;nop
186 #define EALIGN_W_3 nop;nop;nop
187 #define EALIGN_W_4 EALIGN_W_3;nop
188 #define EALIGN_W_5 EALIGN_W_4;nop
189 #define EALIGN_W_6 EALIGN_W_5;nop
190 #define EALIGN_W_7 EALIGN_W_6;nop
192 /* EALIGN is like ENTRY, but does alignment to 'words'*4 bytes
193 past a 2^alignt boundary. */
194 #define EALIGN(name, alignt, words) \
195 .section ".text"; \
196 ENTRY_2(name) \
197 .align ALIGNARG(alignt); \
198 EALIGN_W_##words; \
199 BODY_LABEL(name): \
200 cfi_startproc; \
201 LOCALENTRY(name)
203 /* Local labels stripped out by the linker. */
204 #undef L
205 #define L(x) .L##x
207 #define tostring(s) #s
208 #define stringify(s) tostring(s)
209 #define XGLUE(a,b) a##b
210 #define GLUE(a,b) XGLUE(a,b)
211 #define LT_LABEL(name) GLUE(.LT,name)
212 #define LT_LABELSUFFIX(name,suffix) GLUE(GLUE(.LT,name),suffix)
214 /* Support Traceback tables */
215 #define TB_ASM 0x000c000000000000
216 #define TB_GLOBALLINK 0x0000800000000000
217 #define TB_IS_EPROL 0x0000400000000000
218 #define TB_HAS_TBOFF 0x0000200000000000
219 #define TB_INT_PROC 0x0000100000000000
220 #define TB_HAS_CTL 0x0000080000000000
221 #define TB_TOCLESS 0x0000040000000000
222 #define TB_FP_PRESENT 0x0000020000000000
223 #define TB_LOG_ABORT 0x0000010000000000
224 #define TB_INT_HANDL 0x0000008000000000
225 #define TB_NAME_PRESENT 0x0000004000000000
226 #define TB_USES_ALLOCA 0x0000002000000000
227 #define TB_SAVES_CR 0x0000000200000000
228 #define TB_SAVES_LR 0x0000000100000000
229 #define TB_STORES_BC 0x0000000080000000
230 #define TB_FIXUP 0x0000000040000000
231 #define TB_FP_SAVED(fprs) (((fprs) & 0x3f) << 24)
232 #define TB_GPR_SAVED(gprs) (((fprs) & 0x3f) << 16)
233 #define TB_FIXEDPARMS(parms) (((parms) & 0xff) << 8)
234 #define TB_FLOATPARMS(parms) (((parms) & 0x7f) << 1)
235 #define TB_PARMSONSTK 0x0000000000000001
237 #define PPC_HIGHER(v) (((v) >> 32) & 0xffff)
238 #define TB_DEFAULT TB_ASM | TB_HAS_TBOFF | TB_NAME_PRESENT
240 #define TRACEBACK(name) \
241 LT_LABEL(name): ; \
242 .long 0 ; \
243 .quad TB_DEFAULT ; \
244 .long LT_LABEL(name)-BODY_LABEL(name) ; \
245 .short LT_LABELSUFFIX(name,_name_end)-LT_LABELSUFFIX(name,_name_start) ; \
246 LT_LABELSUFFIX(name,_name_start): ;\
247 .ascii stringify(name) ; \
248 LT_LABELSUFFIX(name,_name_end): ; \
249 .align 2 ;
251 #define TRACEBACK_MASK(name,mask) \
252 LT_LABEL(name): ; \
253 .long 0 ; \
254 .quad TB_DEFAULT | mask ; \
255 .long LT_LABEL(name)-BODY_LABEL(name) ; \
256 .short LT_LABELSUFFIX(name,_name_end)-LT_LABELSUFFIX(name,_name_start) ; \
257 LT_LABELSUFFIX(name,_name_start): ;\
258 .ascii stringify(name) ; \
259 LT_LABELSUFFIX(name,_name_end): ; \
260 .align 2 ;
262 /* END generates Traceback tables */
263 #undef END
264 #define END(name) \
265 cfi_endproc; \
266 TRACEBACK(name) \
267 END_2(name)
269 /* This form supports more informative traceback tables */
270 #define END_GEN_TB(name,mask) \
271 cfi_endproc; \
272 TRACEBACK_MASK(name,mask) \
273 END_2(name)
275 #if !IS_IN(rtld) && defined (ENABLE_LOCK_ELISION)
276 # define ABORT_TRANSACTION \
277 cmpdi 13,0; \
278 beq 1f; \
279 lwz 0,TM_CAPABLE(13); \
280 cmpwi 0,0; \
281 beq 1f; \
282 li 11,_ABORT_SYSCALL; \
283 tabort. 11; \
284 .align 4; \
286 #else
287 # define ABORT_TRANSACTION
288 #endif
290 #define DO_CALL(syscall) \
291 ABORT_TRANSACTION \
292 li 0,syscall; \
295 /* ppc64 is always PIC */
296 #undef JUMPTARGET
297 #define JUMPTARGET(name) DOT_LABEL(name)
299 #define PSEUDO(name, syscall_name, args) \
300 .section ".text"; \
301 ENTRY (name) \
302 DO_CALL (SYS_ify (syscall_name));
304 #ifdef SHARED
305 #define TAIL_CALL_SYSCALL_ERROR \
306 b JUMPTARGET(__syscall_error)
307 #else
308 /* Static version might be linked into a large app with a toc exceeding
309 64k. We can't put a toc adjusting stub on a plain branch, so can't
310 tail call __syscall_error. */
311 #define TAIL_CALL_SYSCALL_ERROR \
312 .ifdef .Local_syscall_error; \
313 b .Local_syscall_error; \
314 .else; \
315 .Local_syscall_error: \
316 mflr 0; \
317 std 0,FRAME_LR_SAVE(1); \
318 stdu 1,-FRAME_MIN_SIZE(1); \
319 cfi_adjust_cfa_offset(FRAME_MIN_SIZE); \
320 cfi_offset(lr,FRAME_LR_SAVE); \
321 bl JUMPTARGET(__syscall_error); \
322 nop; \
323 ld 0,FRAME_MIN_SIZE+FRAME_LR_SAVE(1); \
324 addi 1,1,FRAME_MIN_SIZE; \
325 cfi_adjust_cfa_offset(-FRAME_MIN_SIZE); \
326 mtlr 0; \
327 cfi_restore(lr); \
328 blr; \
329 .endif
330 #endif
332 #define PSEUDO_RET \
333 bnslr+; \
334 TAIL_CALL_SYSCALL_ERROR
336 #define ret PSEUDO_RET
338 #undef PSEUDO_END
339 #define PSEUDO_END(name) \
340 END (name)
342 #define PSEUDO_NOERRNO(name, syscall_name, args) \
343 .section ".text"; \
344 ENTRY (name) \
345 DO_CALL (SYS_ify (syscall_name));
347 #define PSEUDO_RET_NOERRNO \
350 #define ret_NOERRNO PSEUDO_RET_NOERRNO
352 #undef PSEUDO_END_NOERRNO
353 #define PSEUDO_END_NOERRNO(name) \
354 END (name)
356 #define PSEUDO_ERRVAL(name, syscall_name, args) \
357 .section ".text"; \
358 ENTRY (name) \
359 DO_CALL (SYS_ify (syscall_name));
361 #define PSEUDO_RET_ERRVAL \
364 #define ret_ERRVAL PSEUDO_RET_ERRVAL
366 #undef PSEUDO_END_ERRVAL
367 #define PSEUDO_END_ERRVAL(name) \
368 END (name)
370 #else /* !__ASSEMBLER__ */
372 #if _CALL_ELF != 2
374 #define PPC64_LOAD_FUNCPTR(ptr) \
375 "ld 12,0(" #ptr ");\n" \
376 "ld 2,8(" #ptr ");\n" \
377 "mtctr 12;\n" \
378 "ld 11,16(" #ptr ");"
380 #ifdef USE_PPC64_OVERLAPPING_OPD
381 # define OPD_ENT(name) ".quad " BODY_PREFIX #name ", .TOC.@tocbase;"
382 #else
383 # define OPD_ENT(name) ".quad " BODY_PREFIX #name ", .TOC.@tocbase, 0;"
384 #endif
386 #define ENTRY_1(name) \
387 ".type " BODY_PREFIX #name ",@function;\n" \
388 ".globl " #name ";\n" \
389 ".pushsection \".opd\",\"aw\";\n" \
390 ".align 3;\n" \
391 #name ":\n" \
392 OPD_ENT (name) "\n" \
393 ".popsection;"
395 #define DOT_PREFIX ""
396 #define BODY_PREFIX ".LY"
397 #define ENTRY_2(name) \
398 ".type " #name ",@function;\n" \
399 ENTRY_1(name)
400 #define END_2(name) \
401 ".size " #name ",.-" BODY_PREFIX #name ";\n" \
402 ".size " BODY_PREFIX #name ",.-" BODY_PREFIX #name ";"
403 #define LOCALENTRY(name)
405 #else /* _CALL_ELF */
407 #define PPC64_LOAD_FUNCPTR(ptr) \
408 "mr 12," #ptr ";\n" \
409 "mtctr 12;"
411 #define DOT_PREFIX ""
412 #define BODY_PREFIX ""
413 #define ENTRY_2(name) \
414 ".type " #name ",@function;\n" \
415 ".globl " #name ";"
416 #define END_2(name) \
417 ".size " #name ",.-" #name ";"
418 #define LOCALENTRY(name) \
419 "1: addis 2,12,.TOC.-1b@ha;\n" \
420 "addi 2,2,.TOC.-1b@l;\n" \
421 ".localentry " #name ",.-" #name ";"
423 #endif /* _CALL_ELF */
425 #endif /* __ASSEMBLER__ */