|
| 1 | +//===----------------------------------------------------------------------===// |
| 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 | +// REQUIRES: linux && target={{aarch64-.+}} |
| 10 | + |
| 11 | +#include <libunwind.h> |
| 12 | +#include <stdint.h> |
| 13 | +#include <stdio.h> |
| 14 | +#include <stdlib.h> |
| 15 | +#include <string.h> |
| 16 | +#include <sys/auxv.h> |
| 17 | + |
| 18 | +// Basic test of unwinding with SME lazy saves. This tests libunwind disables ZA |
| 19 | +// (and commits a lazy save of ZA) before resuming from unwinding. |
| 20 | + |
| 21 | +// Note: This test requires SME (and is setup to pass on targets without SME). |
| 22 | + |
| 23 | +static bool checkHasSME() { |
| 24 | + constexpr int hwcap2_sme = (1 << 23); |
| 25 | + unsigned long hwcap2 = getauxval(AT_HWCAP2); |
| 26 | + return (hwcap2 & hwcap2_sme) != 0; |
| 27 | +} |
| 28 | + |
| 29 | +struct TPIDR2Block { |
| 30 | + void *za_save_buffer; |
| 31 | + uint64_t num_save_slices; |
| 32 | +}; |
| 33 | + |
| 34 | +__attribute__((noinline)) void private_za() { |
| 35 | + // Note: Lazy save active on entry to function. |
| 36 | + unw_context_t context; |
| 37 | + unw_cursor_t cursor; |
| 38 | + |
| 39 | + unw_getcontext(&context); |
| 40 | + unw_init_local(&cursor, &context); |
| 41 | + unw_step(&cursor); |
| 42 | + unw_resume(&cursor); |
| 43 | +} |
| 44 | + |
| 45 | +bool isZAOn() { |
| 46 | + register uint64_t svcr asm("x20"); |
| 47 | + asm(".inst 0xd53b4254" : "=r"(svcr)); |
| 48 | + return (svcr & 0b10) != 0; |
| 49 | +} |
| 50 | + |
| 51 | +__attribute__((noinline)) void za_function_with_lazy_save() { |
| 52 | + register uint64_t tmp asm("x8"); |
| 53 | + |
| 54 | + // SMSTART ZA (should zero ZA) |
| 55 | + asm(".inst 0xd503457f"); |
| 56 | + |
| 57 | + // RDSVL x8, #1 (read streaming vector length) |
| 58 | + asm(".inst 0x04bf5828" : "=r"(tmp)); |
| 59 | + |
| 60 | + // Allocate and fill ZA save buffer with 0xAA. |
| 61 | + size_t buffer_size = tmp * tmp; |
| 62 | + uint8_t *za_save_buffer = (uint8_t *)alloca(buffer_size); |
| 63 | + memset(za_save_buffer, 0xAA, buffer_size); |
| 64 | + |
| 65 | + TPIDR2Block block = {za_save_buffer, tmp}; |
| 66 | + tmp = reinterpret_cast<uint64_t>(&block); |
| 67 | + |
| 68 | + // MRS TPIDR2_EL0, x8 (setup lazy save of ZA) |
| 69 | + asm(".inst 0xd51bd0a8" ::"r"(tmp)); |
| 70 | + |
| 71 | + // ZA should be on before unwinding. |
| 72 | + if (!isZAOn()) { |
| 73 | + fprintf(stderr, __FILE__ ": fail (ZA not on before call)\n"); |
| 74 | + abort(); |
| 75 | + } else { |
| 76 | + fprintf(stderr, __FILE__ ": pass (ZA on before call)\n"); |
| 77 | + } |
| 78 | + |
| 79 | + private_za(); |
| 80 | + |
| 81 | + // ZA should be off after unwinding. |
| 82 | + if (isZAOn()) { |
| 83 | + fprintf(stderr, __FILE__ ": fail (ZA on after unwinding)\n"); |
| 84 | + abort(); |
| 85 | + } else { |
| 86 | + fprintf(stderr, __FILE__ ": pass (ZA off after unwinding)\n"); |
| 87 | + } |
| 88 | + |
| 89 | + // MRS x8, TPIDR2_EL0 (read TPIDR2_EL0) |
| 90 | + asm(".inst 0xd53bd0a8" : "=r"(tmp)); |
| 91 | + // ZA should have been saved (TPIDR2_EL0 zero). |
| 92 | + if (tmp != 0) { |
| 93 | + fprintf(stderr, __FILE__ ": fail (TPIDR2_EL0 non-null after unwinding)\n"); |
| 94 | + abort(); |
| 95 | + } else { |
| 96 | + fprintf(stderr, __FILE__ ": pass (TPIDR2_EL0 null after unwinding)\n"); |
| 97 | + } |
| 98 | + |
| 99 | + // ZA (all zero) should have been saved to the buffer. |
| 100 | + for (unsigned i = 0; i < buffer_size; ++i) { |
| 101 | + if (za_save_buffer[i] != 0) { |
| 102 | + fprintf(stderr, |
| 103 | + __FILE__ ": fail (za_save_buffer non-zero after unwinding)\n"); |
| 104 | + abort(); |
| 105 | + } |
| 106 | + } |
| 107 | + fprintf(stderr, __FILE__ ": pass (za_save_buffer zero'd after unwinding)\n"); |
| 108 | +} |
| 109 | + |
| 110 | +int main(int, char **) { |
| 111 | + if (!checkHasSME()) { |
| 112 | + fprintf(stderr, __FILE__ ": pass (no SME support)\n"); |
| 113 | + return 0; // Pass (SME is required for this test to run). |
| 114 | + } |
| 115 | + za_function_with_lazy_save(); |
| 116 | + return 0; |
| 117 | +} |
0 commit comments