Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 1 | /* |
Deepika Bhavnani | c249d5e | 2020-02-06 16:29:45 -0600 | [diff] [blame] | 2 | * Copyright (c) 2018-2020, Arm Limited. All rights reserved. |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #ifndef __TEST_HELPERS_H__ |
| 8 | #define __TEST_HELPERS_H__ |
| 9 | |
Joel Hutton | 8790f02 | 2019-03-15 14:47:02 +0000 | [diff] [blame] | 10 | #include <arch_features.h> |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 11 | #include <plat_topology.h> |
| 12 | #include <psci.h> |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 13 | #include <ffa_svc.h> |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 14 | #include <tftf_lib.h> |
| 15 | #include <trusted_os.h> |
| 16 | #include <tsp.h> |
| 17 | #include <uuid.h> |
| 18 | #include <uuid_utils.h> |
| 19 | |
| 20 | typedef struct { |
| 21 | uintptr_t addr; |
| 22 | size_t size; |
| 23 | unsigned int attr; |
| 24 | void *arg; |
| 25 | } map_args_unmap_t; |
| 26 | |
| 27 | typedef test_result_t (*test_function_arg_t)(void *arg); |
| 28 | |
Deepika Bhavnani | c249d5e | 2020-02-06 16:29:45 -0600 | [diff] [blame] | 29 | #ifndef __aarch64__ |
Joel Hutton | 8790f02 | 2019-03-15 14:47:02 +0000 | [diff] [blame] | 30 | #define SKIP_TEST_IF_AARCH32() \ |
| 31 | do { \ |
| 32 | tftf_testcase_printf("Test not supported on aarch32\n"); \ |
| 33 | return TEST_RESULT_SKIPPED; \ |
| 34 | } while (0) |
| 35 | #else |
| 36 | #define SKIP_TEST_IF_AARCH32() |
| 37 | #endif |
| 38 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 39 | #define SKIP_TEST_IF_LESS_THAN_N_CLUSTERS(n) \ |
| 40 | do { \ |
| 41 | unsigned int clusters_cnt; \ |
| 42 | clusters_cnt = tftf_get_total_clusters_count(); \ |
| 43 | if (clusters_cnt < (n)) { \ |
| 44 | tftf_testcase_printf( \ |
| 45 | "Need at least %u clusters, only found %u\n", \ |
| 46 | (n), clusters_cnt); \ |
| 47 | return TEST_RESULT_SKIPPED; \ |
| 48 | } \ |
| 49 | } while (0) |
| 50 | |
| 51 | #define SKIP_TEST_IF_LESS_THAN_N_CPUS(n) \ |
| 52 | do { \ |
| 53 | unsigned int cpus_cnt; \ |
| 54 | cpus_cnt = tftf_get_total_cpus_count(); \ |
| 55 | if (cpus_cnt < (n)) { \ |
| 56 | tftf_testcase_printf( \ |
| 57 | "Need at least %u CPUs, only found %u\n", \ |
| 58 | (n), cpus_cnt); \ |
| 59 | return TEST_RESULT_SKIPPED; \ |
| 60 | } \ |
| 61 | } while (0) |
| 62 | |
| 63 | #define SKIP_TEST_IF_TRUSTED_OS_NOT_PRESENT() \ |
| 64 | do { \ |
| 65 | uuid_t tos_uuid; \ |
| 66 | \ |
| 67 | if (!is_trusted_os_present(&tos_uuid)) { \ |
| 68 | tftf_testcase_printf("No Trusted OS detected\n"); \ |
| 69 | return TEST_RESULT_SKIPPED; \ |
| 70 | } \ |
| 71 | } while (0) |
| 72 | |
| 73 | #define SKIP_TEST_IF_TSP_NOT_PRESENT() \ |
| 74 | do { \ |
| 75 | uuid_t tos_uuid; \ |
| 76 | char tos_uuid_str[UUID_STR_SIZE]; \ |
| 77 | \ |
| 78 | if (!is_trusted_os_present(&tos_uuid)) { \ |
| 79 | tftf_testcase_printf("No Trusted OS detected\n"); \ |
| 80 | return TEST_RESULT_SKIPPED; \ |
| 81 | } \ |
| 82 | \ |
| 83 | if (!uuid_equal(&tos_uuid, &tsp_uuid)) { \ |
| 84 | tftf_testcase_printf( \ |
| 85 | "Trusted OS is not the TSP, its UUID is: %s\n", \ |
| 86 | uuid_to_str(&tos_uuid, tos_uuid_str)); \ |
| 87 | return TEST_RESULT_SKIPPED; \ |
| 88 | } \ |
| 89 | } while (0) |
| 90 | |
Joel Hutton | 8790f02 | 2019-03-15 14:47:02 +0000 | [diff] [blame] | 91 | #define SKIP_TEST_IF_PAUTH_NOT_SUPPORTED() \ |
| 92 | do { \ |
| 93 | if (!is_armv8_3_pauth_present()) { \ |
| 94 | tftf_testcase_printf( \ |
| 95 | "Pointer Authentication not supported\n"); \ |
| 96 | return TEST_RESULT_SKIPPED; \ |
| 97 | } \ |
| 98 | } while (0) |
| 99 | |
Jimmy Brisson | 90f1d5c | 2020-04-16 10:54:51 -0500 | [diff] [blame] | 100 | #define SKIP_TEST_IF_FGT_NOT_SUPPORTED() \ |
| 101 | do { \ |
| 102 | if (!is_armv8_6_fgt_present()) { \ |
| 103 | tftf_testcase_printf( \ |
| 104 | "Fine Grained Traps not supported\n"); \ |
| 105 | return TEST_RESULT_SKIPPED; \ |
| 106 | } \ |
| 107 | } while (0) |
| 108 | |
Jimmy Brisson | 945095a | 2020-04-16 10:54:59 -0500 | [diff] [blame^] | 109 | #define SKIP_TEST_IF_ECV_NOT_SELF_SYNC() \ |
| 110 | do { \ |
| 111 | if (get_armv8_6_ecv_support() != \ |
| 112 | ID_AA64MMFR0_EL1_ECV_SELF_SYNCH) { \ |
| 113 | tftf_testcase_printf("ARMv8.6-ECV not supported\n"); \ |
| 114 | return TEST_RESULT_SKIPPED; \ |
| 115 | } \ |
| 116 | } while (0) |
| 117 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 118 | #define SKIP_TEST_IF_MM_NOT_PRESENT() \ |
| 119 | do { \ |
| 120 | smc_args version_smc = { MM_VERSION_AARCH32 }; \ |
| 121 | smc_ret_values smc_ret = tftf_smc(&version_smc); \ |
| 122 | uint32_t version = smc_ret.ret0; \ |
| 123 | \ |
| 124 | if (version == (uint32_t) SMC_UNKNOWN) { \ |
| 125 | tftf_testcase_printf("SPM not detected.\n"); \ |
| 126 | return TEST_RESULT_SKIPPED; \ |
| 127 | } \ |
| 128 | } while (0) |
| 129 | |
Sandrine Bailleux | 277fb76 | 2019-10-08 12:10:45 +0200 | [diff] [blame] | 130 | #define SKIP_TEST_IF_MTE_SUPPORT_LESS_THAN(n) \ |
| 131 | do { \ |
| 132 | if (get_armv8_5_mte_support() < (n)) { \ |
| 133 | tftf_testcase_printf( \ |
| 134 | "Memory Tagging Extension not supported\n"); \ |
| 135 | return TEST_RESULT_SKIPPED; \ |
| 136 | } \ |
| 137 | } while (0) |
| 138 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 139 | #define SKIP_TEST_IF_MM_VERSION_LESS_THAN(major, minor) \ |
| 140 | do { \ |
| 141 | smc_args version_smc = { MM_VERSION_AARCH32 }; \ |
| 142 | smc_ret_values smc_ret = tftf_smc(&version_smc); \ |
| 143 | uint32_t version = smc_ret.ret0; \ |
| 144 | \ |
| 145 | if (version == (uint32_t) SMC_UNKNOWN) { \ |
| 146 | tftf_testcase_printf("SPM not detected.\n"); \ |
| 147 | return TEST_RESULT_SKIPPED; \ |
| 148 | } \ |
| 149 | \ |
| 150 | if (version < MM_VERSION_FORM(major, minor)) { \ |
| 151 | tftf_testcase_printf("MM_VERSION returned %d.%d\n" \ |
| 152 | "The required version is %d.%d\n", \ |
| 153 | version >> MM_VERSION_MAJOR_SHIFT, \ |
| 154 | version & MM_VERSION_MINOR_MASK, \ |
| 155 | major, minor); \ |
| 156 | return TEST_RESULT_SKIPPED; \ |
| 157 | } \ |
| 158 | \ |
| 159 | VERBOSE("MM_VERSION returned %d.%d\n", \ |
| 160 | version >> MM_VERSION_MAJOR_SHIFT, \ |
| 161 | version & MM_VERSION_MINOR_MASK); \ |
| 162 | } while (0) |
| 163 | |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 164 | #define SKIP_TEST_IF_FFA_VERSION_LESS_THAN(major, minor) \ |
Antonio Nino Diaz | 652d20a | 2018-12-10 17:17:33 +0000 | [diff] [blame] | 165 | do { \ |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 166 | smc_args version_smc = { FFA_VERSION }; \ |
Antonio Nino Diaz | 652d20a | 2018-12-10 17:17:33 +0000 | [diff] [blame] | 167 | smc_ret_values smc_ret = tftf_smc(&version_smc); \ |
Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 168 | uint32_t version = smc_ret.ret2; \ |
Antonio Nino Diaz | 652d20a | 2018-12-10 17:17:33 +0000 | [diff] [blame] | 169 | \ |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 170 | if (smc_ret.ret0 != FFA_SUCCESS_SMC32) { \ |
Antonio Nino Diaz | 652d20a | 2018-12-10 17:17:33 +0000 | [diff] [blame] | 171 | tftf_testcase_printf("SPM not detected.\n"); \ |
| 172 | return TEST_RESULT_SKIPPED; \ |
| 173 | } \ |
Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 174 | \ |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 175 | if ((version & FFA_VERSION_BIT31_MASK) != 0) { \ |
| 176 | tftf_testcase_printf("FFA_VERSION bad response.\n"); \ |
Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 177 | return TEST_RESULT_SKIPPED; \ |
| 178 | } \ |
Antonio Nino Diaz | 652d20a | 2018-12-10 17:17:33 +0000 | [diff] [blame] | 179 | \ |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 180 | if (version < MAKE_FFA_VERSION(major, minor)) { \ |
| 181 | tftf_testcase_printf("FFA_VERSION returned %d.%d\n" \ |
Antonio Nino Diaz | 652d20a | 2018-12-10 17:17:33 +0000 | [diff] [blame] | 182 | "The required version is %d.%d\n", \ |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 183 | version >> FFA_VERSION_MAJOR_SHIFT,\ |
| 184 | version & FFA_VERSION_MINOR_MASK, \ |
Antonio Nino Diaz | 652d20a | 2018-12-10 17:17:33 +0000 | [diff] [blame] | 185 | major, minor); \ |
| 186 | return TEST_RESULT_SKIPPED; \ |
| 187 | } \ |
| 188 | } while (0) |
| 189 | |
Petre-Ionut Tudor | f68ebdb | 2019-09-18 16:13:00 +0100 | [diff] [blame] | 190 | #define SKIP_TEST_IF_ARCH_DEBUG_VERSION_LESS_THAN(version) \ |
| 191 | do { \ |
Petre-Ionut Tudor | f1a45f7 | 2019-10-08 16:51:45 +0100 | [diff] [blame] | 192 | uint32_t debug_ver = arch_get_debug_version(); \ |
Petre-Ionut Tudor | f68ebdb | 2019-09-18 16:13:00 +0100 | [diff] [blame] | 193 | \ |
Petre-Ionut Tudor | f1a45f7 | 2019-10-08 16:51:45 +0100 | [diff] [blame] | 194 | if (debug_ver < version) { \ |
Petre-Ionut Tudor | f68ebdb | 2019-09-18 16:13:00 +0100 | [diff] [blame] | 195 | tftf_testcase_printf("Debug version returned %d\n" \ |
| 196 | "The required version is %d\n", \ |
Petre-Ionut Tudor | f1a45f7 | 2019-10-08 16:51:45 +0100 | [diff] [blame] | 197 | debug_ver, \ |
Petre-Ionut Tudor | f68ebdb | 2019-09-18 16:13:00 +0100 | [diff] [blame] | 198 | version); \ |
| 199 | return TEST_RESULT_SKIPPED; \ |
| 200 | } \ |
| 201 | } while (0) |
| 202 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 203 | /* Helper macro to verify if system suspend API is supported */ |
| 204 | #define is_psci_sys_susp_supported() \ |
| 205 | (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \ |
| 206 | == PSCI_E_SUCCESS) |
| 207 | |
| 208 | /* Helper macro to verify if PSCI_STAT_COUNT API is supported */ |
| 209 | #define is_psci_stat_count_supported() \ |
| 210 | (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \ |
| 211 | == PSCI_E_SUCCESS) |
| 212 | |
| 213 | /* |
| 214 | * Helper function to verify the system state is ready for system |
| 215 | * suspend. i.e., a single CPU is running and all other CPUs are powered off. |
| 216 | * Returns 1 if the system is ready to suspend, 0 otherwise. |
| 217 | */ |
| 218 | int is_sys_suspend_state_ready(void); |
| 219 | |
| 220 | /* |
| 221 | * Helper function to reset the system. This function shouldn't return. |
| 222 | * It is not marked with __dead to help the test to catch some error in |
| 223 | * TF |
| 224 | */ |
| 225 | void psci_system_reset(void); |
| 226 | |
| 227 | /* |
| 228 | * Helper function that enables/disables the mem_protect mechanism |
| 229 | */ |
| 230 | int psci_mem_protect(int val); |
| 231 | |
| 232 | |
| 233 | /* |
| 234 | * Helper function to call PSCI MEM_PROTECT_CHECK |
| 235 | */ |
| 236 | int psci_mem_protect_check(uintptr_t addr, size_t size); |
| 237 | |
| 238 | |
| 239 | /* |
| 240 | * Helper function to get a sentinel address that can be used to test mem_protect |
| 241 | */ |
| 242 | unsigned char *psci_mem_prot_get_sentinel(void); |
| 243 | |
| 244 | /* |
| 245 | * Helper function to memory map and unmap a region needed by a test. |
| 246 | * |
| 247 | * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or |
| 248 | * unmapped. Otherwise, return the test functions's result. |
| 249 | */ |
| 250 | test_result_t map_test_unmap(const map_args_unmap_t *args, |
| 251 | test_function_arg_t test); |
| 252 | |
| 253 | #endif /* __TEST_HELPERS_H__ */ |