blob: 31b26e717d739052e241313ce5455a2703a89787 [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{
22 u_register_t lr = read_elr_el1();
23
24 /* Disable PAuth to avoid further PAuth faults. */
25 pauth_disable();
26
27 /* Check for PAuth exception. */
28 /* Note- PAuth decode instruction clobbers PAC Fields[63:56] in case of error. */
29 if (lr & (0xFFULL << 56U)) {
30 rsi_exit_to_host(HOST_CALL_EXIT_SUCCESS_CMD);
31 }
32
33 rsi_exit_to_host(HOST_CALL_EXIT_FAILED_CMD);
34
35 /* Does not return. */
36 return false;
37}
38
39void dummy_func(void)
40{
Shruti Guptaa276b202023-12-18 10:07:43 +000041 realm_printf("shouldn't reach here.\n");
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010042 rsi_exit_to_host(HOST_CALL_EXIT_FAILED_CMD);
43}
44
45bool test_realm_pauth_fault(void)
46{
47 u_register_t ptr = (u_register_t)dummy_func;
48
49 if (!is_armv8_3_pauth_present()) {
50 return false;
51 }
52
53 register_custom_sync_exception_handler(exception_handler);
Shruti Guptaa276b202023-12-18 10:07:43 +000054 realm_printf("overwrite LR to generate fault.\n");
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010055 __asm__("mov x17, x30; "
56 "mov x30, %0; " /* overwite LR. */
57 "isb; "
58 "autiasp; "
59 "ret; " /* fault on return. */
60 :
61 : "r"(ptr));
62
63 /* Does not return. */
64 return false;
65}
66
67/*
68 * TF-A is expected to allow access to key registers from lower EL's,
69 * reading the keys excercises this, on failure this will trap to
70 * EL3 and crash.
71 */
72bool test_realm_pauth_set_cmd(void)
73{
Shruti Gupta21a30ed2024-01-13 23:07:43 +000074 unsigned int rec = read_mpidr_el1() & MPID_MASK;
75
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010076 if (!is_armv8_3_pauth_present()) {
77 return false;
78 }
Shruti Gupta21a30ed2024-01-13 23:07:43 +000079 assert(rec < MAX_REC_COUNT);
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010080 pauth_test_lib_test_intrs();
Shruti Gupta21a30ed2024-01-13 23:07:43 +000081 pauth_test_lib_fill_regs_and_template(pauth_keys_before[rec]);
82 set_cmd_done[rec] = true;
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010083 return true;
84}
85
86bool test_realm_pauth_check_cmd(void)
87{
Shruti Gupta21a30ed2024-01-13 23:07:43 +000088 unsigned int rec = read_mpidr_el1() & MPID_MASK;
89 bool ret;
90
91 assert(rec < MAX_REC_COUNT);
92 if (!is_armv8_3_pauth_present() || !set_cmd_done[rec]) {
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010093 return false;
94 }
Shruti Gupta21a30ed2024-01-13 23:07:43 +000095 ret = pauth_test_lib_compare_template(pauth_keys_before[rec], pauth_keys_after[rec]);
96 realm_printf("Pauth key comparison ret=%d\n", ret);
97 return ret;
Shruti Gupta9d0cfe82023-04-17 10:57:26 +010098}