-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[libc][AArch64] Add an AArch64 setjmp/longjmp. #101177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
if(LIBC_CONF_SETJMP_AARCH64_RESTORE_PLATFORM_REGISTER) | ||
list(APPEND setjmp_config_options "-DLIBC_COPT_SETJMP_AARCH64_RESTORE_PLATFORM_REGISTER") | ||
endif() | ||
if(setjmp_config_options) | ||
list(PREPEND setjmp_config_options "COMPILE_OPTIONS") | ||
endif() | ||
|
||
add_entrypoint_object( | ||
setjmp | ||
SRCS | ||
setjmp.cpp | ||
HDRS | ||
../setjmp_impl.h | ||
DEPENDS | ||
libc.include.setjmp | ||
${setjmp_config_options} | ||
) | ||
|
||
add_entrypoint_object( | ||
longjmp | ||
SRCS | ||
longjmp.cpp | ||
HDRS | ||
../longjmp.h | ||
DEPENDS | ||
libc.include.setjmp | ||
${setjmp_config_options} | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//===-- Implementation of longjmp for AArch64 -----------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/setjmp/longjmp.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
// TODO: if MTE stack tagging is in use (-fsanitize=memtag-stack), we need to | ||
// iterate over the region between the old and new values of sp, using STG or | ||
// ST2G instructions to clear the memory tags on the invalidated region of the | ||
// stack. But this requires a means of finding out that we're in that mode, and | ||
// as far as I can see there isn't currently a predefined macro for that. | ||
// | ||
// (__ARM_FEATURE_MEMORY_TAGGING only indicates whether the target architecture | ||
// supports the MTE instructions, not whether the compiler is configured to use | ||
// them.) | ||
|
||
[[gnu::naked]] LLVM_LIBC_FUNCTION(void, longjmp, (__jmp_buf * buf, int val)) { | ||
// If BTI branch protection is in use, the compiler will automatically insert | ||
// a BTI here, so we don't need to make any extra effort to do so. | ||
|
||
// If PAC branch protection is in use, there's no need to sign the return | ||
// address at the start of longjmp, because we're not going to use it anyway! | ||
|
||
asm( | ||
// Reload the callee-saved GPRs, including fp and lr. | ||
R"( | ||
ldp x19, x20, [x0, #0*16] | ||
ldp x21, x22, [x0, #1*16] | ||
ldp x23, x24, [x0, #2*16] | ||
ldp x25, x26, [x0, #3*16] | ||
ldp x27, x28, [x0, #4*16] | ||
ldp x29, x30, [x0, #5*16] | ||
)" | ||
|
||
#if LIBC_COPT_SETJMP_AARCH64_RESTORE_PLATFORM_REGISTER | ||
// Reload the stack pointer, and the platform register x18. | ||
R"( | ||
ldp x2, x18, [x0, #6*16] | ||
mov sp, x2 | ||
)" | ||
#else | ||
// Reload just the stack pointer. | ||
R"( | ||
ldr x2, [x0, #6*16] | ||
mov sp, x2 | ||
)" | ||
#endif | ||
|
||
#if __ARM_FP | ||
// Reload the callee-saved FP registers. | ||
R"( | ||
ldp d8, d9, [x0, #7*16] | ||
ldp d10, d11, [x0, #8*16] | ||
ldp d12, d13, [x0, #9*16] | ||
ldp d14, d15, [x0, #10*16] | ||
)" | ||
#endif | ||
|
||
// Calculate the return value. | ||
R"( | ||
cmp w1, #0 | ||
cinc w0, w1, eq | ||
)" | ||
|
||
#if __ARM_FEATURE_PAC_DEFAULT & 1 | ||
// Authenticate the return address using the PAC A key. | ||
R"( | ||
autiasp | ||
)" | ||
#elif __ARM_FEATURE_PAC_DEFAULT & 2 | ||
// Authenticate the return address using the PAC B key. | ||
R"( | ||
autibsp | ||
)" | ||
#endif | ||
|
||
R"( | ||
ret | ||
)"); | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
//===-- Implementation of setjmp for AArch64 ------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/__support/common.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/setjmp/setjmp_impl.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
[[gnu::naked]] LLVM_LIBC_FUNCTION(int, setjmp, (__jmp_buf * buf)) { | ||
// If BTI branch protection is in use, the compiler will automatically insert | ||
// a BTI here, so we don't need to make any extra effort to do so. | ||
|
||
asm( | ||
#if __ARM_FEATURE_PAC_DEFAULT & 1 | ||
// Sign the return address using the PAC A key. | ||
R"( | ||
paciasp | ||
)" | ||
#elif __ARM_FEATURE_PAC_DEFAULT & 2 | ||
// Sign the return address using the PAC B key. | ||
R"( | ||
pacibsp | ||
)" | ||
#endif | ||
|
||
// Store all the callee-saved GPRs, including fp (x29) and also lr (x30). | ||
// Of course lr isn't normally callee-saved (the call instruction itself | ||
// can't help clobbering it), but we certainly need to save it for this | ||
// purpose. | ||
R"( | ||
stp x19, x20, [x0, #0*16] | ||
stp x21, x22, [x0, #1*16] | ||
stp x23, x24, [x0, #2*16] | ||
stp x25, x26, [x0, #3*16] | ||
stp x27, x28, [x0, #4*16] | ||
stp x29, x30, [x0, #5*16] | ||
)" | ||
|
||
#if LIBC_COPT_SETJMP_AARCH64_RESTORE_PLATFORM_REGISTER | ||
// Store the stack pointer, and the platform register x18. | ||
R"( | ||
add x1, sp, #0 | ||
stp x1, x18, [x0, #6*16] | ||
)" | ||
#else | ||
// Store just the stack pointer. | ||
R"( | ||
add x1, sp, #0 | ||
str x1, [x0, #6*16] | ||
)" | ||
#endif | ||
|
||
#if __ARM_FP | ||
// Store the callee-saved FP registers. AAPCS64 only requires the low 64 | ||
// bits of v8-v15 to be preserved, i.e. each of d8,...,d15. | ||
R"( | ||
stp d8, d9, [x0, #7*16] | ||
stp d10, d11, [x0, #8*16] | ||
stp d12, d13, [x0, #9*16] | ||
stp d14, d15, [x0, #10*16] | ||
)" | ||
#endif | ||
|
||
// Set up return value of zero. | ||
R"( | ||
mov x0, #0 | ||
)" | ||
|
||
#if (__ARM_FEATURE_PAC_DEFAULT & 7) == 5 | ||
// Authenticate the return address using the PAC A key, since the | ||
// compilation options ask for PAC protection even on leaf functions. | ||
R"( | ||
autiasp | ||
)" | ||
#elif (__ARM_FEATURE_PAC_DEFAULT & 7) == 6 | ||
// Same, but using the PAC B key. | ||
R"( | ||
autibsp | ||
)" | ||
#endif | ||
|
||
R"( | ||
ret | ||
)"); | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.