| 1 | /*	$OpenBSD: profile.h,v 1.4 2021/05/21 16:49:57 deraadt Exp $	*/ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2015 Dale Rahn <drahn@dalerahn.com> |
| 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 | #define	_MCOUNT_DECL void _mcount |
| 20 | |
| 21 | #define MCOUNT_ASM_NAME "__mcount" |
| 22 | |
| 23 | #ifdef __PIC__ |
| 24 | #define	PLTSYM		"" /* XXX -aarch64 defaults to PLT? */ |
| 25 | #else |
| 26 | #define	PLTSYM		"" |
| 27 | #endif |
| 28 | |
| 29 | #define MCOUNT							\ |
| 30 | __asm__ (".text						\n;"	\ |
| 31 | 	 ".align 3					\n;"	\ |
| 32 | 	 ".globl " MCOUNT_ASM_NAME "			\n;"	\ |
| 33 | 	 ".type " MCOUNT_ASM_NAME ",@function		\n;"	\ |
| 34 | 	 MCOUNT_ASM_NAME ":				\n;"	\ |
| 35 | 	 "	addi	sp, sp, -176			\n"	\ |
| 36 | 	 "	sd	fp, 0(sp)			\n"	\ |
| 37 | 	 "	sd	ra, 8(sp)			\n"	\ |
| 38 | 	 "	sd	s1, 24(sp)			\n"	\ |
| 39 | 	 "	sd	a0, 32(sp)			\n"	\ |
| 40 | 	 "	sd	a1, 40(sp)			\n"	\ |
| 41 | 	 "	sd	a2, 48(sp)			\n"	\ |
| 42 | 	 "	sd	a3, 56(sp)			\n"	\ |
| 43 | 	 "	sd	a4, 64(sp)			\n"	\ |
| 44 | 	 "	sd	a5, 72(sp)			\n"	\ |
| 45 | 	 "	sd	a6, 80(sp)			\n"	\ |
| 46 | 	 "	sd	a7, 88(sp)			\n"	\ |
| 47 | 	 "	sd	s2, 96(sp)			\n"	\ |
| 48 | 	 "	sd	s3, 104(sp)			\n"	\ |
| 49 | 	 "	sd	s4, 112(sp)			\n"	\ |
| 50 | 	 "	sd	s5, 120(sp)			\n"	\ |
| 51 | 	 "	sd	s6, 128(sp)			\n"	\ |
| 52 | 	 "	sd	s7, 136(sp)			\n"	\ |
| 53 | 	 "	sd	s8, 144(sp)			\n"	\ |
| 54 | 	 "	sd	s9, 152(sp)			\n"	\ |
| 55 | 	 "	sd	s10, 160(sp)			\n"	\ |
| 56 | 	 "	sd	s11, 168(sp)			\n"	\ |
| 57 | 	 "	ld	a0, 8(fp)			\n"	\ |
| 58 | 	 "	mv	a1, x1				\n"	\ |
| 59 | 	 "	call	" __STRING(_mcount) PLTSYM "	\n"	\ |
| 60 | 	 /* restore argument registers */			\ |
| 61 | 	 "	ld	fp, 0(sp)			\n"	\ |
| 62 | 	 "	ld	ra, 8(sp)			\n"	\ |
| 63 | 	 "	ld	s1, 24(sp)			\n"	\ |
| 64 | 	 "	ld	a0, 32(sp)			\n"	\ |
| 65 | 	 "	ld	a1, 40(sp)			\n"	\ |
| 66 | 	 "	ld	a2, 48(sp)			\n"	\ |
| 67 | 	 "	ld	a3, 56(sp)			\n"	\ |
| 68 | 	 "	ld	a4, 64(sp)			\n"	\ |
| 69 | 	 "	ld	a5, 72(sp)			\n"	\ |
| 70 | 	 "	ld	a6, 80(sp)			\n"	\ |
| 71 | 	 "	ld	a7, 88(sp)			\n"	\ |
| 72 | 	 "	ld	s2, 96(sp)			\n"	\ |
| 73 | 	 "	ld	s3, 104(sp)			\n"	\ |
| 74 | 	 "	ld	s4, 112(sp)			\n"	\ |
| 75 | 	 "	ld	s5, 120(sp)			\n"	\ |
| 76 | 	 "	ld	s6, 128(sp)			\n"	\ |
| 77 | 	 "	ld	s7, 136(sp)			\n"	\ |
| 78 | 	 "	ld	s8, 144(sp)			\n"	\ |
| 79 | 	 "	ld	s9, 152(sp)			\n"	\ |
| 80 | 	 "	ld	s10, 160(sp)			\n"	\ |
| 81 | 	 "	ld	s11, 168(sp)			\n"	\ |
| 82 | 	 "	addi	sp, sp, 176			\n"	\ |
| 83 | 	 "	jr	ra				\n"); |
| 84 | |
| 85 | #ifdef _KERNEL |
| 86 | // Change this to dair read/set, then restore. |
| 87 | #define MCOUNT_ENTER						\ |
| 88 | __asm__ ("mrs %x0,daif; msr daifset, #0x2": "=r"(s)); |
| 89 | #define	MCOUNT_EXIT						\ |
| 90 | __asm__ ("msr daif, %x0":: "r"(s)); |
| 91 | |
| 92 | #endif |