-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[libunwind] Call __arm_za_disable before entering EH
#160905
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
Open
MacDue
wants to merge
4
commits into
llvm:main
Choose a base branch
from
MacDue:za_disable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // REQUIRES: linux && target={{aarch64-.+}} | ||
|
|
||
| #include <libunwind.h> | ||
| #include <stdint.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <sys/auxv.h> | ||
|
|
||
| // Basic test of unwinding with SME lazy saves. This tests libunwind disables ZA | ||
| // (and commits a lazy save of ZA) before resuming from unwinding. | ||
|
|
||
| // Note: This test requires SME (and is setup to pass on targets without SME). | ||
|
|
||
| static bool checkHasSME() { | ||
| constexpr int hwcap2_sme = (1 << 23); | ||
| unsigned long hwcap2 = getauxval(AT_HWCAP2); | ||
| return (hwcap2 & hwcap2_sme) != 0; | ||
| } | ||
|
|
||
| struct TPIDR2Block { | ||
| void *za_save_buffer; | ||
| uint64_t num_save_slices; | ||
| }; | ||
|
|
||
| __attribute__((noinline)) void private_za() { | ||
| // Note: Lazy save active on entry to function. | ||
| unw_context_t context; | ||
| unw_cursor_t cursor; | ||
|
|
||
| unw_getcontext(&context); | ||
| unw_init_local(&cursor, &context); | ||
| unw_step(&cursor); | ||
| unw_resume(&cursor); | ||
| } | ||
|
|
||
| bool isZAOn() { | ||
| register uint64_t svcr asm("x20"); | ||
| asm(".inst 0xd53b4254" : "=r"(svcr)); | ||
| return (svcr & 0b10) != 0; | ||
| } | ||
|
|
||
| __attribute__((noinline)) void za_function_with_lazy_save() { | ||
| register uint64_t tmp asm("x8"); | ||
|
|
||
| // SMSTART ZA (should zero ZA) | ||
| asm(".inst 0xd503457f"); | ||
|
|
||
| // RDSVL x8, #1 (read streaming vector length) | ||
| asm(".inst 0x04bf5828" : "=r"(tmp)); | ||
|
|
||
| // Allocate and fill ZA save buffer with 0xAA. | ||
| size_t buffer_size = tmp * tmp; | ||
| uint8_t *za_save_buffer = (uint8_t *)alloca(buffer_size); | ||
| memset(za_save_buffer, 0xAA, buffer_size); | ||
|
|
||
| TPIDR2Block block = {za_save_buffer, tmp}; | ||
| tmp = reinterpret_cast<uint64_t>(&block); | ||
|
|
||
| // MRS TPIDR2_EL0, x8 (setup lazy save of ZA) | ||
| asm(".inst 0xd51bd0a8" ::"r"(tmp)); | ||
|
|
||
| // ZA should be on before unwinding. | ||
| if (!isZAOn()) { | ||
| fprintf(stderr, __FILE__ ": fail (ZA not on before call)\n"); | ||
| abort(); | ||
| } else { | ||
| fprintf(stderr, __FILE__ ": pass (ZA on before call)\n"); | ||
| } | ||
|
|
||
| private_za(); | ||
|
|
||
| // ZA should be off after unwinding. | ||
| if (isZAOn()) { | ||
| fprintf(stderr, __FILE__ ": fail (ZA on after unwinding)\n"); | ||
| abort(); | ||
| } else { | ||
| fprintf(stderr, __FILE__ ": pass (ZA off after unwinding)\n"); | ||
| } | ||
|
|
||
| // MRS x8, TPIDR2_EL0 (read TPIDR2_EL0) | ||
| asm(".inst 0xd53bd0a8" : "=r"(tmp)); | ||
| // ZA should have been saved (TPIDR2_EL0 zero). | ||
| if (tmp != 0) { | ||
| fprintf(stderr, __FILE__ ": fail (TPIDR2_EL0 non-null after unwinding)\n"); | ||
| abort(); | ||
| } else { | ||
| fprintf(stderr, __FILE__ ": pass (TPIDR2_EL0 null after unwinding)\n"); | ||
| } | ||
|
|
||
| // ZA (all zero) should have been saved to the buffer. | ||
| for (unsigned i = 0; i < buffer_size; ++i) { | ||
| if (za_save_buffer[i] != 0) { | ||
| fprintf(stderr, | ||
| __FILE__ ": fail (za_save_buffer non-zero after unwinding)\n"); | ||
| abort(); | ||
| } | ||
| } | ||
| fprintf(stderr, __FILE__ ": pass (za_save_buffer zero'd after unwinding)\n"); | ||
| } | ||
|
|
||
| int main(int, char **) { | ||
| if (!checkHasSME()) { | ||
| fprintf(stderr, __FILE__ ": pass (no SME support)\n"); | ||
| return 0; // Pass (SME is required for this test to run). | ||
| } | ||
| za_function_with_lazy_save(); | ||
| return 0; | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we should link to something more stable like their tag versions, say https://github.com/ARM-software/abi-aa/blob/2025Q1/aapcs64/aapcs64.rst#exceptions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what that'd be here? I'm aware GCC versions their symbols, but this symbol could be provided by libgcc/ibgcc_s or compiler-rt, and I'm not sure what version information is generally available.