| 1 | /*	$NetBSD: profile.h,v 1.18.42.1 2026/04/02 19:12:03 martin Exp $	*/ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 2001 Ben Harris |
| 5 | * Copyright (c) 1995-1996 Mark Brinicombe |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. All advertising materials mentioning features or use of this software |
| 16 | * must display the following acknowledgement: |
| 17 | *	This product includes software developed by Mark Brinicombe. |
| 18 | * 4. The name of the author may not be used to endorse or promote products |
| 19 | * derived from this software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 22 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 24 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 26 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 30 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | #define	_MCOUNT_DECL void _mcount |
| 34 | |
| 35 | /* |
| 36 | * Cannot implement mcount in C as GCC will trash the ip register when it |
| 37 | * pushes a trapframe. Pity we cannot insert assembly before the function |
| 38 | * prologue. |
| 39 | */ |
| 40 | |
| 41 | #define	PLTSYM |
| 42 | |
| 43 | #if defined (_ARM_ARCH_4T) |
| 44 | # define RET		"bx	ip" |
| 45 | #else |
| 46 | # define RET		"mov	pc, ip" |
| 47 | #endif |
| 48 | |
| 49 | #if defined(__ARM_DWARF_EH__) |
| 50 | #define _PROF_UNWINDER_SAVE	"" |
| 51 | #define _PROF_UNWINDER_START	"" |
| 52 | #define _PROF_UNWINDER_END	"" |
| 53 | #else |
| 54 | #define _PROF_UNWINDER_SAVE	".save {r0-r3, lr}\n" |
| 55 | #define _PROF_UNWINDER_START	".fnstart\n" |
| 56 | #define _PROF_UNWINDER_END	".fnend\n" |
| 57 | #endif |
| 58 | |
| 59 | #define MCOUNT_ASM_NAME "__gnu_mcount_nc" |
| 60 | #define	MCOUNT								\ |
| 61 | 	__asm(".text");							\ |
| 62 | 	__asm(".align	0");						\ |
| 63 | 	__asm(".arm");							\ |
| 64 | 	__asm(".type	" MCOUNT_ASM_NAME ",%function");		\ |
| 65 | 	__asm(".global	" MCOUNT_ASM_NAME);				\ |
| 66 | 	__asm(MCOUNT_ASM_NAME ":");					\ |
| 67 | 	__asm(_PROF_UNWINDER_START);					\ |
| 68 | 	__asm(".cfi_startproc");					\ |
| 69 | 	/*								\ |
| 70 | 	 * Preserve registers that are trashed during mcount		\ |
| 71 | 	 */								\ |
| 72 | 	__asm("push	{r0-r3, lr}");					\ |
| 73 | 	__asm(_PROF_UNWINDER_SAVE);					\ |
| 74 | 	__asm(".cfi_def_cfa_offset 20");				\ |
| 75 | 	__asm(".cfi_offset 14, -4");					\ |
| 76 | 	__asm(".cfi_offset 3, -8");					\ |
| 77 | 	__asm(".cfi_offset 2, -12");					\ |
| 78 | 	__asm(".cfi_offset 1, -16");					\ |
| 79 | 	__asm(".cfi_offset 0, -20");					\ |
| 80 | 	/*								\ |
| 81 | 	 * find the return address for mcount,				\ |
| 82 | 	 * and the return address for mcount's caller.			\ |
| 83 | 	 *								\ |
| 84 | 	 * frompcindex = pc pushed by call into self.			\ |
| 85 | 	 */								\ |
| 86 | 	__asm("ldr	r0, [sp, #20]");				\ |
| 87 | 	/*								\ |
| 88 | 	 * selfpc = pc pushed by mcount call				\ |
| 89 | 	 */								\ |
| 90 | 	__asm("mov	r1, lr");					\ |
| 91 | 	/*								\ |
| 92 | 	 * Call the real mcount code					\ |
| 93 | 	 */								\ |
| 94 | 	__asm("bl	" ___STRING(_C_LABEL(_mcount)) PLTSYM);		\ |
| 95 | 	/*								\ |
| 96 | 	 * Restore registers that were trashed during mcount		\ |
| 97 | 	 */								\ |
| 98 | 	__asm("pop	{r0-r3, ip, lr}");				\ |
| 99 | 	__asm(RET);							\ |
| 100 | 	__asm(".cfi_endproc");						\ |
| 101 | 	__asm(".size	" MCOUNT_ASM_NAME ", .-" MCOUNT_ASM_NAME); |
| 102 | |
| 103 | #ifdef _KERNEL |
| 104 | #include <arm/cpufunc.h> |
| 105 | /* |
| 106 | * splhigh() and splx() are heavyweight, and call mcount(). Therefore |
| 107 | * we disabled interrupts (IRQ, but not FIQ) directly on the CPU. |
| 108 | * |
| 109 | * We're lucky that the CPSR and 's' both happen to be 'int's. |
| 110 | */ |
| 111 | #define	MCOUNT_ENTER	s = __set_cpsr_c(0x0080, 0x0080);	/* kill IRQ */ |
| 112 | #define	MCOUNT_EXIT	__set_cpsr_c(0xffffffff, s);	/* restore old value */ |
| 113 | #endif /* _KERNEL */ |