Shruti Gupta | 9d0cfe8 | 2023-04-17 10:57:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2023, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <arch_features.h> |
| 10 | #include <debug.h> |
| 11 | #include <pauth.h> |
| 12 | #include <realm_rsi.h> |
| 13 | #include <sync.h> |
| 14 | |
| 15 | static volatile bool set_cmd_done; |
| 16 | |
| 17 | static bool exception_handler(void) |
| 18 | { |
| 19 | u_register_t lr = read_elr_el1(); |
| 20 | |
| 21 | /* Disable PAuth to avoid further PAuth faults. */ |
| 22 | pauth_disable(); |
| 23 | |
| 24 | /* Check for PAuth exception. */ |
| 25 | /* Note- PAuth decode instruction clobbers PAC Fields[63:56] in case of error. */ |
| 26 | if (lr & (0xFFULL << 56U)) { |
| 27 | rsi_exit_to_host(HOST_CALL_EXIT_SUCCESS_CMD); |
| 28 | } |
| 29 | |
| 30 | rsi_exit_to_host(HOST_CALL_EXIT_FAILED_CMD); |
| 31 | |
| 32 | /* Does not return. */ |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | void dummy_func(void) |
| 37 | { |
Shruti Gupta | a276b20 | 2023-12-18 10:07:43 +0000 | [diff] [blame] | 38 | realm_printf("shouldn't reach here.\n"); |
Shruti Gupta | 9d0cfe8 | 2023-04-17 10:57:26 +0100 | [diff] [blame] | 39 | rsi_exit_to_host(HOST_CALL_EXIT_FAILED_CMD); |
| 40 | } |
| 41 | |
| 42 | bool test_realm_pauth_fault(void) |
| 43 | { |
| 44 | u_register_t ptr = (u_register_t)dummy_func; |
| 45 | |
| 46 | if (!is_armv8_3_pauth_present()) { |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | register_custom_sync_exception_handler(exception_handler); |
Shruti Gupta | a276b20 | 2023-12-18 10:07:43 +0000 | [diff] [blame] | 51 | realm_printf("overwrite LR to generate fault.\n"); |
Shruti Gupta | 9d0cfe8 | 2023-04-17 10:57:26 +0100 | [diff] [blame] | 52 | __asm__("mov x17, x30; " |
| 53 | "mov x30, %0; " /* overwite LR. */ |
| 54 | "isb; " |
| 55 | "autiasp; " |
| 56 | "ret; " /* fault on return. */ |
| 57 | : |
| 58 | : "r"(ptr)); |
| 59 | |
| 60 | /* Does not return. */ |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * TF-A is expected to allow access to key registers from lower EL's, |
| 66 | * reading the keys excercises this, on failure this will trap to |
| 67 | * EL3 and crash. |
| 68 | */ |
| 69 | bool test_realm_pauth_set_cmd(void) |
| 70 | { |
| 71 | if (!is_armv8_3_pauth_present()) { |
| 72 | return false; |
| 73 | } |
| 74 | pauth_test_lib_test_intrs(); |
| 75 | pauth_test_lib_fill_regs_and_template(); |
| 76 | set_cmd_done = true; |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | bool test_realm_pauth_check_cmd(void) |
| 81 | { |
| 82 | if (!is_armv8_3_pauth_present() || !set_cmd_done) { |
| 83 | return false; |
| 84 | } |
| 85 | return pauth_test_lib_compare_template(); |
| 86 | } |