| 1 | /* Copyright (C) 1997-2026 Free Software Foundation, Inc. |
| 2 | |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 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 License as |
| 7 | published by the Free Software Foundation; either version 2.1 of the |
| 8 | License, or (at your option) any later version. |
| 9 | |
| 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. |
| 14 | |
| 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 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #ifndef _AARCH64_SYSDEP_H |
| 20 | #define _AARCH64_SYSDEP_H |
| 21 | |
| 22 | #include <sysdeps/generic/sysdep.h> |
| 23 | |
| 24 | #ifdef	__ASSEMBLER__ |
| 25 | |
| 26 | /* CFI directive for return address. */ |
| 27 | #define cfi_negate_ra_state	.cfi_negate_ra_state |
| 28 | |
| 29 | /* Syntactic details of assembler. */ |
| 30 | |
| 31 | #define ASM_SIZE_DIRECTIVE(name) .size name,.-name |
| 32 | |
| 33 | /* Guarded Control Stack support. */ |
| 34 | #define CHKFEAT_X16	hint	40 |
| 35 | #define MRS_GCSPR(x)	mrs	x, s3_3_c2_c5_1 |
| 36 | #define GCSPOPM(x)	sysl	x, #3, c7, c7, #1 |
| 37 | #define GCSSS1(x)	sys	#3, c7, c7, #2, x |
| 38 | #define GCSSS2(x)	sysl	x, #3, c7, c7, #3 |
| 39 | |
| 40 | /* GNU_PROPERTY_AARCH64_* macros from elf.h for use in asm code. */ |
| 41 | #define FEATURE_1_AND 0xc0000000 |
| 42 | #define FEATURE_1_BTI 1 |
| 43 | #define FEATURE_1_PAC 2 |
| 44 | #define FEATURE_1_GCS 4 |
| 45 | |
| 46 | #define GNU_PROPERTY(type, value)	\ |
| 47 | .section .note.gnu.property, "a";	\ |
| 48 | .p2align 3;				\ |
| 49 | .word 4;				\ |
| 50 | .word 16;				\ |
| 51 | .word 5;				\ |
| 52 | .asciz "GNU";				\ |
| 53 | .word type;				\ |
| 54 | .word 4;				\ |
| 55 | .word value;				\ |
| 56 | .word 0;				\ |
| 57 | .text |
| 58 | |
| 59 | #ifdef __ARM_BUILDATTR64_FV |
| 60 | /* Add AArch64 feature bits build attributes. */ |
| 61 | # define FEATURE_1_AND_MARK(value)					\ |
| 62 | .aeabi_subsection aeabi_feature_and_bits, optional, ULEB128;	\ |
| 63 | .if ((value) & FEATURE_1_BTI);					\ |
| 64 | .aeabi_attribute Tag_Feature_BTI, 1;				\ |
| 65 | .else;								\ |
| 66 | .aeabi_attribute Tag_Feature_BTI, 0;				\ |
| 67 | .endif;								\ |
| 68 | .if ((value) & FEATURE_1_GCS);					\ |
| 69 | .aeabi_attribute Tag_Feature_GCS, 1;				\ |
| 70 | .else;								\ |
| 71 | .aeabi_attribute Tag_Feature_GCS, 0;				\ |
| 72 | .endif;								\ |
| 73 | .if ((value) & FEATURE_1_PAC);					\ |
| 74 | .aeabi_attribute Tag_Feature_PAC, 1;				\ |
| 75 | .else;								\ |
| 76 | .aeabi_attribute Tag_Feature_PAC, 0;				\ |
| 77 | .endif;								\ |
| 78 | .text |
| 79 | #else |
| 80 | /* Add a NT_GNU_PROPERTY_TYPE_0 note. */ |
| 81 | # define FEATURE_1_AND_MARK(value) GNU_PROPERTY (FEATURE_1_AND, value) |
| 82 | #endif /* __ARM_BUILDATTR64_FV */ |
| 83 | |
| 84 | /* Add marking with the supported features to all asm code where sysdep.h |
| 85 | is included. */ |
| 86 | FEATURE_1_AND_MARK (FEATURE_1_BTI | FEATURE_1_PAC | FEATURE_1_GCS) |
| 87 | |
| 88 | /* Define an entry point visible from C. */ |
| 89 | #define ENTRY(name)						\ |
| 90 | .globl C_SYMBOL_NAME(name);					\ |
| 91 | .type C_SYMBOL_NAME(name),%function;				\ |
| 92 | .p2align 6;							\ |
| 93 | C_LABEL(name)							\ |
| 94 | cfi_startproc;						\ |
| 95 | bti	c;							\ |
| 96 | CALL_MCOUNT |
| 97 | |
| 98 | /* Define an entry point visible from C. */ |
| 99 | #define ENTRY_ALIGN(name, align)				\ |
| 100 | .globl C_SYMBOL_NAME(name);					\ |
| 101 | .type C_SYMBOL_NAME(name),%function;				\ |
| 102 | .p2align align;						\ |
| 103 | C_LABEL(name)							\ |
| 104 | cfi_startproc;						\ |
| 105 | bti	c;							\ |
| 106 | CALL_MCOUNT |
| 107 | |
| 108 | /* Define an entry point visible from C with a specified alignment and |
| 109 | pre-padding with NOPs. This can be used to ensure that a critical |
| 110 | loop within a function is cache line aligned. Note this version |
| 111 | does not adjust the padding if CALL_MCOUNT is defined. */ |
| 112 | |
| 113 | #define ENTRY_ALIGN_AND_PAD(name, align, padding)		\ |
| 114 | .globl C_SYMBOL_NAME(name);					\ |
| 115 | .type C_SYMBOL_NAME(name),%function;				\ |
| 116 | .p2align align;						\ |
| 117 | .rep padding - 1; /* -1 for bti c. */			\ |
| 118 | nop;								\ |
| 119 | .endr;							\ |
| 120 | C_LABEL(name)							\ |
| 121 | cfi_startproc;						\ |
| 122 | bti	c;							\ |
| 123 | CALL_MCOUNT |
| 124 | |
| 125 | #undef	END |
| 126 | #define END(name)						\ |
| 127 | cfi_endproc;							\ |
| 128 | ASM_SIZE_DIRECTIVE(name) |
| 129 | |
| 130 | /* If compiled for profiling, call `mcount' at the start of each function. */ |
| 131 | #ifdef	PROF |
| 132 | # define CALL_MCOUNT						\ |
| 133 | 	str	x30, [sp, #-80]!;				\ |
| 134 | 	cfi_adjust_cfa_offset (80);				\ |
| 135 | 	cfi_rel_offset (x30, 0);				\ |
| 136 | 	stp	x0, x1, [sp, #16];				\ |
| 137 | 	cfi_rel_offset (x0, 16);				\ |
| 138 | 	cfi_rel_offset (x1, 24);				\ |
| 139 | 	stp	x2, x3, [sp, #32];				\ |
| 140 | 	cfi_rel_offset (x2, 32);				\ |
| 141 | 	cfi_rel_offset (x3, 40);				\ |
| 142 | 	stp	x4, x5, [sp, #48];				\ |
| 143 | 	cfi_rel_offset (x4, 48);				\ |
| 144 | 	cfi_rel_offset (x5, 56);				\ |
| 145 | 	stp	x6, x7, [sp, #64];				\ |
| 146 | 	cfi_rel_offset (x6, 64);				\ |
| 147 | 	cfi_rel_offset (x7, 72);				\ |
| 148 | 	mov	x0, x30;					\ |
| 149 | 	bl	mcount;						\ |
| 150 | 	ldp	x0, x1, [sp, #16];				\ |
| 151 | 	cfi_restore (x0);					\ |
| 152 | 	cfi_restore (x1);					\ |
| 153 | 	ldp	x2, x3, [sp, #32];				\ |
| 154 | 	cfi_restore (x2);					\ |
| 155 | 	cfi_restore (x3);					\ |
| 156 | 	ldp	x4, x5, [sp, #48];				\ |
| 157 | 	cfi_restore (x4);					\ |
| 158 | 	cfi_restore (x5);					\ |
| 159 | 	ldp	x6, x7, [sp, #64];				\ |
| 160 | 	cfi_restore (x6);					\ |
| 161 | 	cfi_restore (x7);					\ |
| 162 | 	ldr	x30, [sp], #80;					\ |
| 163 | 	cfi_adjust_cfa_offset (-80);				\ |
| 164 | 	cfi_restore (x30); |
| 165 | #else |
| 166 | # define CALL_MCOUNT		/* Do nothing. */ |
| 167 | #endif |
| 168 | |
| 169 | /* Local label name for asm code. */ |
| 170 | #ifndef L |
| 171 | # define L(name) .L##name |
| 172 | #endif |
| 173 | |
| 174 | /* Since C identifiers are not normally prefixed with an underscore |
| 175 | on this system, the asm identifier `syscall_error' intrudes on the |
| 176 | C name space. Make sure we use an innocuous name. */ |
| 177 | #define syscall_error	__syscall_error |
| 178 | #define mcount		_mcount |
| 179 | |
| 180 | #endif	/* __ASSEMBLER__ */ |
| 181 | |
| 182 | #endif /* _AARCH64_SYSDEP_H */ |