| 1 | /*	$OpenBSD: asm.h,v 1.7 2022/12/07 23:25:59 guenther Exp $	*/ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2020 Dale Rahn <drahn@openbsd.org> |
| 5 | * |
| 6 | * Permission to use, copy, modify, and distribute this software for any |
| 7 | * purpose with or without fee is hereby granted, provided that the above |
| 8 | * copyright notice and this permission notice appear in all copies. |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | */ |
| 18 | |
| 19 | #ifndef _POWERPC64_ASM_H_ |
| 20 | #define _POWERPC64_ASM_H_ |
| 21 | |
| 22 | #define _C_LABEL(x)	x |
| 23 | #define _ASM_LABEL(x)	x |
| 24 | |
| 25 | #define _TMP_LABEL(x)	.L_ ## x |
| 26 | #define _GEP_LABEL(x)	.L_ ## x ## _gep0 |
| 27 | #define _LEP_LABEL(x)	.L_ ## x ## _lep0 |
| 28 | |
| 29 | #define _ENTRY(x)						\ |
| 30 | 	.text; .align 2; .globl x; .type x,@function; x:	\ |
| 31 | 	_GEP_LABEL(x):						\ |
| 32 | 	addis	%r2, %r12, .TOC.-_GEP_LABEL(x)@ha;		\ |
| 33 | 	addi	%r2, %r2, .TOC.-_GEP_LABEL(x)@l;		\ |
| 34 | 	_LEP_LABEL(x):						\ |
| 35 | 	.localentry	x, _LEP_LABEL(x)-_GEP_LABEL(x); |
| 36 | |
| 37 | #if defined(PROF) || defined(GPROF) |
| 38 | # define _PROF_PROLOGUE(y)					\ |
| 39 | 	.section ".data";					\ |
| 40 | 	.align 2;						\ |
| 41 | _TMP_LABEL(y):;							\ |
| 42 | 	.long	0;						\ |
| 43 | 	.section ".text";					\ |
| 44 | 	mflr	%r0;						\ |
| 45 | 	addis	%r11, %r2, _TMP_LABEL(y)@toc@ha;		\ |
| 46 | 	std	%r0, 8(%r1);					\ |
| 47 | 	addi	%r0, %r11, _TMP_LABEL(y)@toc@l;			\ |
| 48 | 	bl _mcount; |
| 49 | #else |
| 50 | # define _PROF_PROLOGUE(y) |
| 51 | #endif |
| 52 | |
| 53 | #define ENTRY(y)	_ENTRY(y); _PROF_PROLOGUE(y) |
| 54 | #define ASENTRY(y)	_ENTRY(y); _PROF_PROLOGUE(y) |
| 55 | #define END(y)		.size y, . - y |
| 56 | |
| 57 | #define STRONG_ALIAS(alias,sym) \ |
| 58 | 	.global alias; .set alias,sym |
| 59 | #define WEAK_ALIAS(alias,sym) \ |
| 60 | 	.weak alias; .set alias,sym |
| 61 | |
| 62 | #if defined(_RET_PROTECTOR) |
| 63 | # define RETGUARD_SETUP(x, reg)						\ |
| 64 | 	RETGUARD_SYMBOL(x);						\ |
| 65 | 	mflr %r0;							\ |
| 66 | 	addis reg, %r2, (__retguard_ ## x)@toc@ha;			\ |
| 67 | 	ld reg, ((__retguard_ ## x)@toc@l)(reg);			\ |
| 68 | 	xor reg, reg, %r0 |
| 69 | # define RETGUARD_CHECK(x, reg)						\ |
| 70 | 	mflr %r0;							\ |
| 71 | 	xor reg, reg, %r0;						\ |
| 72 | 	addis %r12, %r2, (__retguard_ ## x)@toc@ha;			\ |
| 73 | 	ld %r12, ((__retguard_ ## x)@toc@l)(%r12);			\ |
| 74 | 	tdne reg, %r12 |
| 75 | # define RETGUARD_SAVE(reg, loc)					\ |
| 76 | 	std reg, loc |
| 77 | # define RETGUARD_LOAD(reg, loc)					\ |
| 78 | 	ld reg, loc |
| 79 | # define RETGUARD_SYMBOL(x)						\ |
| 80 | 	.ifndef __retguard_ ## x;					\ |
| 81 | 	.hidden __retguard_ ## x;					\ |
| 82 | 	.type __retguard_ ## x,@object;				\ |
| 83 | 	.pushsection .openbsd.randomdata.retguard,"aw",@progbits; 	\ |
| 84 | 	.weak __retguard_ ## x;					\ |
| 85 | 	.p2align 3;							\ |
| 86 | 	__retguard_ ## x: ;						\ |
| 87 | 	.quad 0;							\ |
| 88 | 	.size __retguard_ ## x, 8;					\ |
| 89 | 	.popsection;							\ |
| 90 | 	.endif |
| 91 | #else |
| 92 | # define RETGUARD_SETUP(x, reg) |
| 93 | # define RETGUARD_CHECK(x, reg) |
| 94 | # define RETGUARD_SAVE(reg, loc) |
| 95 | # define RETGUARD_LOAD(reg, loc) |
| 96 | # define RETGUARD_SYMBOL(x) |
| 97 | #endif |
| 98 | |
| 99 | |
| 100 | #endif /* !_POWERPC64_ASM_H_ */ |