|
| 1 | +//===-- Implementation of longjmp for AArch64 -----------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "src/setjmp/longjmp.h" |
| 10 | +#include "src/__support/common.h" |
| 11 | +#include "src/__support/macros/config.h" |
| 12 | + |
| 13 | +namespace LIBC_NAMESPACE_DECL { |
| 14 | + |
| 15 | +// TODO: if MTE stack tagging is in use (-fsanitize=memtag-stack), we need to |
| 16 | +// iterate over the region between the old and new values of sp, using STG or |
| 17 | +// ST2G instructions to clear the memory tags on the invalidated region of the |
| 18 | +// stack. But this requires a means of finding out that we're in that mode, and |
| 19 | +// as far as I can see there isn't currently a predefined macro for that. |
| 20 | +// |
| 21 | +// (__ARM_FEATURE_MEMORY_TAGGING only indicates whether the target architecture |
| 22 | +// supports the MTE instructions, not whether the compiler is configured to use |
| 23 | +// them.) |
| 24 | + |
| 25 | +[[gnu::naked]] LLVM_LIBC_FUNCTION(void, longjmp, (__jmp_buf * buf, int val)) { |
| 26 | + // If BTI branch protection is in use, the compiler will automatically insert |
| 27 | + // a BTI here, so we don't need to make any extra effort to do so. |
| 28 | + |
| 29 | + // If PAC branch protection is in use, there's no need to sign the return |
| 30 | + // address at the start of longjmp, because we're not going to use it anyway! |
| 31 | + |
| 32 | + asm( |
| 33 | + // Reload the callee-saved GPRs, including fp and lr. |
| 34 | + R"( |
| 35 | + ldp x19, x20, [x0, #0*16] |
| 36 | + ldp x21, x22, [x0, #1*16] |
| 37 | + ldp x23, x24, [x0, #2*16] |
| 38 | + ldp x25, x26, [x0, #3*16] |
| 39 | + ldp x27, x28, [x0, #4*16] |
| 40 | + ldp x29, x30, [x0, #5*16] |
| 41 | + )" |
| 42 | + |
| 43 | +#if LIBC_COPT_SETJMP_AARCH64_RESTORE_PLATFORM_REGISTER |
| 44 | + // Reload the stack pointer, and the platform register x18. |
| 45 | + R"( |
| 46 | + ldp x2, x18, [x0, #6*16] |
| 47 | + mov sp, x2 |
| 48 | + )" |
| 49 | +#else |
| 50 | + // Reload just the stack pointer. |
| 51 | + R"( |
| 52 | + ldr x2, [x0, #6*16] |
| 53 | + mov sp, x2 |
| 54 | + )" |
| 55 | +#endif |
| 56 | + |
| 57 | +#if __ARM_FP |
| 58 | + // Reload the callee-saved FP registers. |
| 59 | + R"( |
| 60 | + ldp d8, d9, [x0, #7*16] |
| 61 | + ldp d10, d11, [x0, #8*16] |
| 62 | + ldp d12, d13, [x0, #9*16] |
| 63 | + ldp d14, d15, [x0, #10*16] |
| 64 | + )" |
| 65 | +#endif |
| 66 | + |
| 67 | + // Calculate the return value. |
| 68 | + R"( |
| 69 | + cmp w1, #0 |
| 70 | + cinc w0, w1, eq |
| 71 | + )" |
| 72 | + |
| 73 | +#if __ARM_FEATURE_PAC_DEFAULT & 1 |
| 74 | + // Authenticate the return address using the PAC A key. |
| 75 | + R"( |
| 76 | + autiasp |
| 77 | + )" |
| 78 | +#elif __ARM_FEATURE_PAC_DEFAULT & 2 |
| 79 | + // Authenticate the return address using the PAC B key. |
| 80 | + R"( |
| 81 | + autibsp |
| 82 | + )" |
| 83 | +#endif |
| 84 | + |
| 85 | + R"( |
| 86 | + ret |
| 87 | + )"); |
| 88 | +} |
| 89 | + |
| 90 | +} // namespace LIBC_NAMESPACE_DECL |
0 commit comments