blob: c3399a7e15ae042b596b0cd9b453f1d2c8f5cfbb [file] [log] [blame]
Alexander Graf97d44b12016-05-20 23:28:22 +02001/*
2 * (C) Copyright 2016
3 * Alexander Graf <agraf@suse.de>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#ifndef _SETJMP_H_
9#define _SETJMP_H_ 1
10
11struct jmp_buf_data {
12 ulong target;
13 ulong regs[5];
Alexander Graf692fcdd2016-09-27 09:30:32 +020014 int ret;
Alexander Graf97d44b12016-05-20 23:28:22 +020015};
16
17typedef struct jmp_buf_data jmp_buf[1];
18
19static inline int setjmp(jmp_buf jmp)
20{
Alexander Graf692fcdd2016-09-27 09:30:32 +020021 jmp->ret = 0;
Alexander Graf97d44b12016-05-20 23:28:22 +020022
23#ifdef CONFIG_ARM64
24 asm volatile(
25 "adr x1, jmp_target\n"
Alexander Graf692fcdd2016-09-27 09:30:32 +020026 "str x1, %0\n"
27 "stp x26, x27, %1\n"
28 "stp x28, x29, %2\n"
Alexander Graf97d44b12016-05-20 23:28:22 +020029 "mov x1, sp\n"
Alexander Graf692fcdd2016-09-27 09:30:32 +020030 "str x1, %3\n"
Alexander Graf97d44b12016-05-20 23:28:22 +020031 "jmp_target: "
Alexander Graf692fcdd2016-09-27 09:30:32 +020032 : "=m" (jmp->target), "=m" (jmp->regs[0]),
33 "=m" (jmp->regs[2]), "=m" (jmp->regs[4])
Alexander Graf97d44b12016-05-20 23:28:22 +020034 :
35 : "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
36 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
37 "x16", "x17", "x18", "x19", "x20", "x21", "x22",
38 "x23", "x24", "x25", /* x26, x27, x28, x29, sp */
39 "x30", "cc", "memory");
40#else
41 asm volatile(
Tom Rini3a649402017-03-18 09:01:44 -040042#if CONFIG_IS_ENABLED(SYS_THUMB_BUILD)
Alexander Graf0de02de2016-07-05 20:37:17 +020043 ".align 2\n"
Alexander Grafe6777242016-06-13 14:01:07 +020044 "adr r0, jmp_target\n"
45 "add r0, r0, $1\n"
Alexander Graf97d44b12016-05-20 23:28:22 +020046#else
47 "adr r0, jmp_target\n"
48#endif
Alexander Graf692fcdd2016-09-27 09:30:32 +020049 "mov r1, %0\n"
Alexander Graf97d44b12016-05-20 23:28:22 +020050 "mov r2, sp\n"
Alexander Grafe6777242016-06-13 14:01:07 +020051 "stm r1!, {r0, r2, r4, r5, r6, r7}\n"
Alexander Graf0de02de2016-07-05 20:37:17 +020052 ".align 2\n"
53 "jmp_target: \n"
Alexander Graf692fcdd2016-09-27 09:30:32 +020054 :
Alexander Graf97d44b12016-05-20 23:28:22 +020055 : "l" (&jmp->target)
56 : "r0", "r1", "r2", "r3", /* "r4", "r5", "r6", "r7", */
57 "r8", "r9", "r10", "r11", /* sp, */ "ip", "lr",
58 "cc", "memory");
59#endif
60
Alexander Graf692fcdd2016-09-27 09:30:32 +020061 return jmp->ret;
Alexander Graf97d44b12016-05-20 23:28:22 +020062}
63
Alexander Graf692fcdd2016-09-27 09:30:32 +020064static inline __noreturn void longjmp(jmp_buf jmp, int ret)
Alexander Graf97d44b12016-05-20 23:28:22 +020065{
Alexander Graf692fcdd2016-09-27 09:30:32 +020066 jmp->ret = ret;
67
Alexander Graf97d44b12016-05-20 23:28:22 +020068#ifdef CONFIG_ARM64
69 asm volatile(
70 "ldr x0, %0\n"
71 "ldr x1, %3\n"
72 "mov sp, x1\n"
73 "ldp x26, x27, %1\n"
74 "ldp x28, x25, %2\n"
75 "mov x29, x25\n"
76 "br x0\n"
77 :
78 : "m" (jmp->target), "m" (jmp->regs[0]), "m" (jmp->regs[2]),
79 "m" (jmp->regs[4])
80 : "x0", "x1", "x25", "x26", "x27", "x28");
81#else
82 asm volatile(
83 "mov r1, %0\n"
Alexander Grafe6777242016-06-13 14:01:07 +020084 "ldm r1!, {r0, r2, r4, r5, r6, r7}\n"
Alexander Graf97d44b12016-05-20 23:28:22 +020085 "mov sp, r2\n"
86 "bx r0\n"
87 :
88 : "l" (&jmp->target)
89 : "r1");
90#endif
91
92 while (1) { }
93}
94
95
96#endif /* _SETJMP_H_ */