xref: /optee_os/lib/libutils/isoc/arch/arm/setjmp_a64.S (revision fedadb6460b1ea7db709c6f5a0572f5a8cb8e5c9)
1/* SPDX-License-Identifier: BSD-3-Clause */
2/*
3   Copyright (c) 2011, 2012 ARM Ltd
4   All rights reserved.
5
6   Redistribution and use in source and binary forms, with or without
7   modification, are permitted provided that the following conditions
8   are met:
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in the
13      documentation and/or other materials provided with the distribution.
14   3. The name of the company may not be used to endorse or promote
15      products derived from this software without specific prior written
16      permission.
17
18   THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
19   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21   IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <asan.h>
31#include <asm.S>
32
33#define GPR_LAYOUT			\
34	REG_PAIR (x19, x20,  0);	\
35	REG_PAIR (x21, x22, 16);	\
36	REG_PAIR (x23, x24, 32);	\
37	REG_PAIR (x25, x26, 48);	\
38	REG_PAIR (x27, x28, 64);	\
39	REG_PAIR (x29, x30, 80);	\
40	REG_ONE (x16,      96)
41
42#define FPR_LAYOUT			\
43	REG_PAIR ( d8,  d9, 112);	\
44	REG_PAIR (d10, d11, 128);	\
45	REG_PAIR (d12, d13, 144);	\
46	REG_PAIR (d14, d15, 160);
47
48// int setjmp (jmp_buf)
49	.global	setjmp
50	.type	setjmp, %function
51setjmp:
52BTI(	bti	c)
53	mov	x16, sp
54#define REG_PAIR(REG1, REG2, OFFS)	stp REG1, REG2, [x0, OFFS]
55#define REG_ONE(REG1, OFFS)		str REG1, [x0, OFFS]
56	GPR_LAYOUT
57	FPR_LAYOUT
58#ifdef CFG_FTRACE_SUPPORT
59	stp	x29, x30, [sp, #-16]!
60	mov	x29, sp
61	add	x0, x0, #104
62	bl	ftrace_setjmp
63	ldp	x29, x30, [sp], #16
64#endif
65#undef REG_PAIR
66#undef REG_ONE
67	mov	w0, #0
68	ret
69	.size	setjmp, .-setjmp
70
71// void longjmp (jmp_buf, int) __attribute__ ((noreturn))
72	.global	longjmp
73	.type	longjmp, %function
74longjmp:
75BTI(	bti	c)
76#define REG_PAIR(REG1, REG2, OFFS)	ldp REG1, REG2, [x0, OFFS]
77#define REG_ONE(REG1, OFFS)		ldr REG1, [x0, OFFS]
78#ifdef CFG_FTRACE_SUPPORT
79	stp	x0, x1, [sp, #-16]!
80	stp	x29, x30, [sp, #-16]!
81	mov	x29, sp
82	add	x0, x0, #104
83	bl	ftrace_longjmp
84	ldp	x29, x30, [sp], #16
85	ldp	x0, x1, [sp], #16
86#endif
87
88#if ASAN_IS_ENABLED && (!defined(__KERNEL__) || !defined(CFG_DYN_CONFIG))
89	stp	x0, x1, [sp, #-16]!
90	ldr	x0, [x0, 96]
91	bl	asan_handle_longjmp
92	ldp	x0, x1, [sp], #16
93#endif
94	GPR_LAYOUT
95	FPR_LAYOUT
96#undef REG_PAIR
97#undef REG_ONE
98	mov	sp, x16
99	cmp	w1, #0
100	cinc	w0, w1, eq
101/*
102 * clang has a bug and doesn't insert bti after setjmp
103 * causing BTI ecxception. Remove this when the bug is fixed.
104 * https://bugs.llvm.org/show_bug.cgi?id=49544
105 */
106#if defined(__clang__) && defined(CFG_TA_BTI)
107	ret
108#else
109	// use br not ret, as ret is guaranteed to mispredict
110	br	x30
111#endif
112	.size	longjmp, .-longjmp
113
114BTI(emit_aarch64_feature_1_and     GNU_PROPERTY_AARCH64_FEATURE_1_BTI)
115