blob: 9c507b637360e29a616a2513dae963a4c1656a13 [file] [log] [blame]
Shruti Gupta9d0cfe82023-04-17 10:57:26 +01001/*
Shruti Gupta21a30ed2024-01-13 23:07:43 +00002 * Copyright (c) 2023-2024, Arm Limited. All rights reserved.
Shruti Gupta9d0cfe82023-04-17 10:57:26 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include <stdio.h>
9#include <arch_features.h>
Shruti Gupta21a30ed2024-01-13 23:07:43 +000010#include <assert.h>
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010011#include <debug.h>
12#include <pauth.h>
13#include <realm_rsi.h>
14#include <sync.h>
15
Shruti Gupta21a30ed2024-01-13 23:07:43 +000016static volatile bool set_cmd_done[MAX_REC_COUNT];
17static uint128_t pauth_keys_before[MAX_REC_COUNT][NUM_KEYS];
18static uint128_t pauth_keys_after[MAX_REC_COUNT][NUM_KEYS];
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010019
20static bool exception_handler(void)
21{
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010022 /* Disable PAuth to avoid further PAuth faults. */
23 pauth_disable();
24
Shruti Gupta042e9602024-05-30 13:55:11 +010025 rsi_exit_to_host(HOST_CALL_EXIT_SUCCESS_CMD);
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010026
27 /* Does not return. */
28 return false;
29}
30
31void dummy_func(void)
32{
Shruti Guptaa276b202023-12-18 10:07:43 +000033 realm_printf("shouldn't reach here.\n");
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010034 rsi_exit_to_host(HOST_CALL_EXIT_FAILED_CMD);
35}
36
37bool test_realm_pauth_fault(void)
38{
39 u_register_t ptr = (u_register_t)dummy_func;
40
41 if (!is_armv8_3_pauth_present()) {
42 return false;
43 }
44
45 register_custom_sync_exception_handler(exception_handler);
Shruti Guptaa276b202023-12-18 10:07:43 +000046 realm_printf("overwrite LR to generate fault.\n");
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010047 __asm__("mov x17, x30; "
48 "mov x30, %0; " /* overwite LR. */
49 "isb; "
50 "autiasp; "
51 "ret; " /* fault on return. */
52 :
53 : "r"(ptr));
54
55 /* Does not return. */
56 return false;
57}
58
59/*
60 * TF-A is expected to allow access to key registers from lower EL's,
61 * reading the keys excercises this, on failure this will trap to
62 * EL3 and crash.
63 */
64bool test_realm_pauth_set_cmd(void)
65{
Shruti Gupta21a30ed2024-01-13 23:07:43 +000066 unsigned int rec = read_mpidr_el1() & MPID_MASK;
67
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010068 if (!is_armv8_3_pauth_present()) {
69 return false;
70 }
Shruti Gupta21a30ed2024-01-13 23:07:43 +000071 assert(rec < MAX_REC_COUNT);
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010072 pauth_test_lib_test_intrs();
Shruti Gupta21a30ed2024-01-13 23:07:43 +000073 pauth_test_lib_fill_regs_and_template(pauth_keys_before[rec]);
74 set_cmd_done[rec] = true;
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010075 return true;
76}
77
78bool test_realm_pauth_check_cmd(void)
79{
Shruti Gupta21a30ed2024-01-13 23:07:43 +000080 unsigned int rec = read_mpidr_el1() & MPID_MASK;
81 bool ret;
82
83 assert(rec < MAX_REC_COUNT);
84 if (!is_armv8_3_pauth_present() || !set_cmd_done[rec]) {
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010085 return false;
86 }
Shruti Gupta21a30ed2024-01-13 23:07:43 +000087 ret = pauth_test_lib_compare_template(pauth_keys_before[rec], pauth_keys_after[rec]);
88 realm_printf("Pauth key comparison ret=%d\n", ret);
89 return ret;
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010090}