blob: 7e4a861b3b7c7e83cd92fb8b468406c46b2c9a53 [file] [log] [blame]
Manish Pandey5f513d62022-01-17 11:05:53 +00001/*
2 * Copyright (c) 2022, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Olivier Deprez82bbc572022-01-11 11:50:36 +01007#include <plat/common/platform.h>
8
Manish Pandey5f513d62022-01-17 11:05:53 +00009#include <arch.h>
10#include <arch_helpers.h>
11#include <arch_features.h>
12#include <debug.h>
13#ifdef __aarch64__
14#include <sync.h>
15#endif
nabkah01002e5692022-10-10 12:36:46 +010016#include <host_realm_helper.h>
Olivier Deprez82bbc572022-01-11 11:50:36 +010017#include <lib/aarch64/arch_features.h>
nabkah01002e5692022-10-10 12:36:46 +010018#include <test_helpers.h>
Manish Pandey5f513d62022-01-17 11:05:53 +000019#include <tftf_lib.h>
Manish Pandey368054b2022-03-15 13:24:59 +000020#include <xlat_tables_v2.h>
Manish Pandey5f513d62022-01-17 11:05:53 +000021#include <platform_def.h>
nabkah0146b418d2022-02-16 17:01:54 +000022#include <cactus_test_cmds.h>
23#include <ffa_endpoints.h>
24
Manish Pandey5f513d62022-01-17 11:05:53 +000025/*
26 * Using "__aarch64__" here looks weird but its unavoidable because of following reason
27 * This test is part of standard test which runs on all platforms but pre-requisite
28 * to run this test (custom sync exception handler) is only implemented for aarch64.
29 * TODO: Write a framework so that tests kept in standard list can be selectively
30 * run on a given architecture
31 */
32#ifdef __aarch64__
33
nabkah0146b418d2022-02-16 17:01:54 +000034#define SENDER HYP_ID
35#define RECEIVER SP_ID(1)
36
Manish Pandey5f513d62022-01-17 11:05:53 +000037static volatile bool sync_exception_triggered;
38static volatile bool data_abort_triggered;
nabkah0146b418d2022-02-16 17:01:54 +000039static const struct ffa_uuid expected_sp_uuids[] = {
40 {PRIMARY_UUID}, {SECONDARY_UUID}, {TERTIARY_UUID}
41};
Manish Pandey5f513d62022-01-17 11:05:53 +000042
Olivier Deprez82bbc572022-01-11 11:50:36 +010043static __aligned(PAGE_SIZE) uint64_t share_page[PAGE_SIZE / sizeof(uint64_t)];
44
Manish Pandey5f513d62022-01-17 11:05:53 +000045static bool data_abort_handler(void)
46{
47 uint64_t esr_elx = IS_IN_EL2() ? read_esr_el2() : read_esr_el1();
48 unsigned int rme_supported = get_armv9_2_feat_rme_support();
49
50 sync_exception_triggered = true;
51
52 VERBOSE("%s esr_elx %llx\n", __func__, esr_elx);
53
54 if (EC_BITS(esr_elx) == EC_DABORT_CUR_EL) {
55 if (rme_supported == 0) {
56 /* Synchronous external data abort triggered by trustzone controller */
57 if ((ISS_BITS(esr_elx) & ISS_DFSC_MASK) == DFSC_EXT_DABORT) {
58 VERBOSE("%s TZC Data Abort caught\n", __func__);
59 data_abort_triggered = true;
60 return true;
61 }
62 } else {
63 /* Synchronous data abort triggered by Granule protection */
64 if ((ISS_BITS(esr_elx) & ISS_DFSC_MASK) == DFSC_GPF_DABORT) {
65 VERBOSE("%s GPF Data Abort caught\n", __func__);
66 data_abort_triggered = true;
67 return true;
68 }
69 }
70 }
71
72 return false;
73}
74
nabkah01bf00f8d2022-03-22 12:37:19 +000075test_result_t el3_memory_cannot_be_accessed_in_ns(void)
Manish Pandey5f513d62022-01-17 11:05:53 +000076{
77 const uintptr_t test_address = EL3_MEMORY_ACCESS_ADDR;
78
Manish Pandey5f513d62022-01-17 11:05:53 +000079 VERBOSE("Attempt to access el3 memory (0x%lx)\n", test_address);
80
Manish Pandey368054b2022-03-15 13:24:59 +000081 sync_exception_triggered = false;
Manish Pandey5f513d62022-01-17 11:05:53 +000082 data_abort_triggered = false;
Manish Pandey368054b2022-03-15 13:24:59 +000083
84 int rc = mmap_add_dynamic_region(test_address, test_address, PAGE_SIZE,
85 MT_MEMORY | MT_RW | MT_NS);
86 if (rc != 0) {
87 tftf_testcase_printf("%d: mmap_add_dynamic_region() = %d\n", __LINE__, rc);
88 return TEST_RESULT_FAIL;
89 }
90
Manish Pandey5f513d62022-01-17 11:05:53 +000091 register_custom_sync_exception_handler(data_abort_handler);
Manish Pandey5f513d62022-01-17 11:05:53 +000092 *((volatile uint64_t *)test_address);
Manish Pandey5f513d62022-01-17 11:05:53 +000093 unregister_custom_sync_exception_handler();
94
Manish Pandey368054b2022-03-15 13:24:59 +000095 rc = mmap_remove_dynamic_region(test_address, PAGE_SIZE);
96 if (rc != 0) {
97 tftf_testcase_printf("%d: mmap_remove_dynamic_region() = %d\n", __LINE__, rc);
98 return TEST_RESULT_FAIL;
99 }
100
Manish Pandey5f513d62022-01-17 11:05:53 +0000101 if (sync_exception_triggered == false) {
102 tftf_testcase_printf("No sync exception while accessing (0x%lx)\n", test_address);
103 return TEST_RESULT_SKIPPED;
104 }
105
106 if (data_abort_triggered == false) {
107 tftf_testcase_printf("Sync exception is not data abort\n");
108 return TEST_RESULT_FAIL;
109 }
110
111 return TEST_RESULT_SUCCESS;
112}
Olivier Deprez82bbc572022-01-11 11:50:36 +0100113
114/**
115 * @Test_Aim@ Check a realm region cannot be accessed from normal world.
116 *
117 * This test delegates a TFTF allocated buffer to Realm. It then attempts
118 * a read access to the region from normal world. This results in the PE
119 * triggering a GPF caught by a custom synchronous abort handler.
120 *
121 */
122test_result_t rl_memory_cannot_be_accessed_in_ns(void)
123{
124 test_result_t result = TEST_RESULT_FAIL;
125 u_register_t retmm;
126
127 if (get_armv9_2_feat_rme_support() == 0U) {
128 return TEST_RESULT_SKIPPED;
129 }
130
131 sync_exception_triggered = false;
132 data_abort_triggered = false;
133 register_custom_sync_exception_handler(data_abort_handler);
134
135 /* First read access to the test region must not fail. */
136 *((volatile uint64_t *)share_page);
137
138 if ((sync_exception_triggered != false) ||
139 (data_abort_triggered != false)) {
140 goto out_unregister;
141 }
142
143 /* Delegate the shared page to Realm. */
nabkah01002e5692022-10-10 12:36:46 +0100144 retmm = rmi_granule_delegate((u_register_t)&share_page);
Olivier Deprez82bbc572022-01-11 11:50:36 +0100145 if (retmm != 0UL) {
146 ERROR("Granule delegate failed!\n");
147 goto out_unregister;
148 }
149
150 /* This access shall trigger a GPF. */
151 *((volatile uint64_t *)share_page);
152
153 if ((sync_exception_triggered != true) ||
154 (data_abort_triggered != true)) {
155 goto out_undelegate;
156 }
157
158 result = TEST_RESULT_SUCCESS;
159
160out_undelegate:
161 /* Undelegate the shared page. */
nabkah01002e5692022-10-10 12:36:46 +0100162 retmm = rmi_granule_undelegate((u_register_t)&share_page);
Olivier Deprez82bbc572022-01-11 11:50:36 +0100163 if (retmm != 0UL) {
164 ERROR("Granule undelegate failed!\n");
165 }
166
167out_unregister:
168 unregister_custom_sync_exception_handler();
169
170 return result;
171}
172
nabkah01bf00f8d2022-03-22 12:37:19 +0000173/**
174 * @Test_Aim@ Check a secure region cannot be accessed from normal world.
175 *
176 * Following test intends to run on RME enabled platforms when EL3
177 * is Root world. In a non RME platform, EL3 is secure.
178 * Access to secure memory from NS world is already covered
179 * by el3_memory_cannot_be_accessed_in_ns.
180 */
181test_result_t s_memory_cannot_be_accessed_in_ns(void)
182{
183 const uintptr_t test_address = SECURE_MEMORY_ACCESS_ADDR;
184
185 /* skipp non RME platforms */
186 if (get_armv9_2_feat_rme_support() == 0U) {
187 return TEST_RESULT_SKIPPED;
188 }
189
190 VERBOSE("Attempt to access secure memory (0x%lx)\n", test_address);
191
192 data_abort_triggered = false;
193 sync_exception_triggered = false;
194 register_custom_sync_exception_handler(data_abort_handler);
195 dsbsy();
196
197 int rc = mmap_add_dynamic_region(test_address, test_address, PAGE_SIZE,
198 MT_MEMORY | MT_RW | MT_NS);
199
200 if (rc != 0) {
201 tftf_testcase_printf("%d: mmap_add_dynamic_region() = %d\n", __LINE__, rc);
202 return TEST_RESULT_FAIL;
203 }
204
205 *((volatile uint64_t *)test_address);
206
207 mmap_remove_dynamic_region(test_address, PAGE_SIZE);
208
209 dsbsy();
210 unregister_custom_sync_exception_handler();
211
212 if (sync_exception_triggered == false) {
213 tftf_testcase_printf("No sync exception while accessing (0x%lx)\n", test_address);
214 return TEST_RESULT_SKIPPED;
215 }
216
217 if (data_abort_triggered == false) {
218 tftf_testcase_printf("Sync exception is not data abort\n");
219 return TEST_RESULT_FAIL;
220 }
221
222 return TEST_RESULT_SUCCESS;
223}
224
nabkah01905b2c72022-02-10 10:46:32 +0000225static test_result_t memory_cannot_be_accessed_in_rl(u_register_t params)
226{
227 u_register_t retrmm;
228 static char rd[GRANULE_SIZE] __aligned(GRANULE_SIZE);
229
230 if (get_armv9_2_feat_rme_support() == 0U) {
231 return TEST_RESULT_SKIPPED;
232 }
233
nabkah01002e5692022-10-10 12:36:46 +0100234 retrmm = rmi_version();
nabkah01905b2c72022-02-10 10:46:32 +0000235
236 VERBOSE("RMM version is: %lu.%lu\n",
237 RMI_ABI_VERSION_GET_MAJOR(retrmm),
238 RMI_ABI_VERSION_GET_MINOR(retrmm));
239
240 /*
241 * TODO: Remove this once SMC_RMM_REALM_CREATE is implemented in TRP
242 * For the moment skip the test if RMM is TRP, TRP version is always null.
243 */
244 if (retrmm == 0U) {
245 return TEST_RESULT_SKIPPED;
246 }
247
nabkah01002e5692022-10-10 12:36:46 +0100248 retrmm = rmi_granule_delegate((u_register_t)&rd[0]);
nabkah01905b2c72022-02-10 10:46:32 +0000249 if (retrmm != 0UL) {
250 ERROR("Delegate operation returns fail, %lx\n", retrmm);
251 return TEST_RESULT_FAIL;
252 }
253
254 /* Create a realm using a parameter in a secure physical address space should fail. */
nabkah01002e5692022-10-10 12:36:46 +0100255 retrmm = rmi_realm_create((u_register_t)&rd[0], params);
nabkah01905b2c72022-02-10 10:46:32 +0000256 if (retrmm == 0UL) {
257 ERROR("Realm create operation should fail, %lx\n", retrmm);
nabkah01002e5692022-10-10 12:36:46 +0100258 retrmm = rmi_realm_destroy((u_register_t)&rd[0]);
nabkah01905b2c72022-02-10 10:46:32 +0000259 if (retrmm != 0UL) {
260 ERROR("Realm destroy operation returns fail, %lx\n", retrmm);
261 return TEST_RESULT_FAIL;
262 }
263 return TEST_RESULT_FAIL;
264 } else if (retrmm != RMM_STATUS_ERROR_INPUT) {
265 ERROR("Realm create operation should fail with code:%ld retrmm:%ld\n",
266 RMM_STATUS_ERROR_INPUT, retrmm);
267 return TEST_RESULT_FAIL;
268 }
269
nabkah01002e5692022-10-10 12:36:46 +0100270 retrmm = rmi_granule_undelegate((u_register_t)&rd[0]);
nabkah01905b2c72022-02-10 10:46:32 +0000271 if (retrmm != 0UL) {
272 INFO("Undelegate operation returns fail, %lx\n", retrmm);
273 return TEST_RESULT_FAIL;
274 }
275
276 return TEST_RESULT_SUCCESS;
277}
278
nabkah0146b418d2022-02-16 17:01:54 +0000279/**
280 * @Test_Aim@ Check a root region cannot be accessed from a secure partition.
281 *
282 * This change adds TFTF and cactus test to permit checking a root region
283 * cannot be accessed from secure world.
284 * A hardcoded address marked Root in the GPT is shared to a secure
285 * partition. The SP retrieves the region from the SPM, maps it and
286 * attempts a read access to the region. It is expected to trigger a GPF
287 * data abort on the PE caught by a custom exception handler.
288 *
289 */
290test_result_t rt_memory_cannot_be_accessed_in_s(void)
291{
292 const uintptr_t test_address = EL3_MEMORY_ACCESS_ADDR;
293 struct ffa_memory_region_constituent constituents[] = {
294 {
295 (void *)test_address, 1, 0
296 }
297 };
298 const uint32_t constituents_count = sizeof(constituents) /
299 sizeof(struct ffa_memory_region_constituent);
300 ffa_memory_handle_t handle;
301 struct mailbox_buffers mb;
Daniel Boulbyce386b12022-03-29 18:36:36 +0100302 struct ffa_value ret;
nabkah0146b418d2022-02-16 17:01:54 +0000303
304 if (get_armv9_2_feat_rme_support() == 0U) {
305 return TEST_RESULT_SKIPPED;
306 }
307
308 INIT_TFTF_MAILBOX(mb);
309
310 CHECK_SPMC_TESTING_SETUP(1, 1, expected_sp_uuids);
311
312 GET_TFTF_MAILBOX(mb);
313
314 handle = memory_init_and_send((struct ffa_memory_region *)mb.send,
315 PAGE_SIZE, SENDER, RECEIVER,
316 constituents, constituents_count,
317 FFA_MEM_SHARE_SMC32, &ret);
318
319 if (handle == FFA_MEMORY_HANDLE_INVALID) {
320 return TEST_RESULT_FAIL;
321 }
322
323 VERBOSE("TFTF - Handle: %llx Address: %p\n",
324 handle, constituents[0].address);
325
326 /* Retrieve the shared page and attempt accessing it. */
327 ret = cactus_mem_send_cmd(SENDER, RECEIVER, FFA_MEM_SHARE_SMC32,
328 handle, 0, true, 1);
329
330 if (is_ffa_call_error(ffa_mem_reclaim(handle, 0))) {
331 ERROR("Memory reclaim failed!\n");
332 return TEST_RESULT_FAIL;
333 }
334
335 /*
336 * Expect success response with value 1 hinting an exception
337 * triggered while the SP accessed the region.
338 */
339 if (!(cactus_get_response(ret) == CACTUS_SUCCESS &&
340 cactus_error_code(ret) == 1)) {
341 ERROR("Exceptions test failed!\n");
342 return TEST_RESULT_FAIL;
343 }
344
345 return TEST_RESULT_SUCCESS;
346}
347
nabkah01905b2c72022-02-10 10:46:32 +0000348test_result_t s_memory_cannot_be_accessed_in_rl(void)
349{
350 u_register_t params = (u_register_t)SECURE_MEMORY_ACCESS_ADDR;
351 return memory_cannot_be_accessed_in_rl(params);
352}
353
nabkah01e7147862022-02-11 19:08:00 +0000354test_result_t rt_memory_cannot_be_accessed_in_rl(void)
355{
356 u_register_t params = (u_register_t)EL3_MEMORY_ACCESS_ADDR;
357 return memory_cannot_be_accessed_in_rl(params);
358}
359
Manish Pandey5f513d62022-01-17 11:05:53 +0000360#else
Olivier Deprez82bbc572022-01-11 11:50:36 +0100361
nabkah01bf00f8d2022-03-22 12:37:19 +0000362test_result_t el3_memory_cannot_be_accessed_in_ns(void)
Manish Pandey5f513d62022-01-17 11:05:53 +0000363{
364 tftf_testcase_printf("Test not ported to AArch32\n");
365 return TEST_RESULT_SKIPPED;
366}
Olivier Deprez82bbc572022-01-11 11:50:36 +0100367
368test_result_t rl_memory_cannot_be_accessed_in_ns(void)
369{
370 tftf_testcase_printf("Test not ported to AArch32\n");
371 return TEST_RESULT_SKIPPED;
372}
373
nabkah01bf00f8d2022-03-22 12:37:19 +0000374test_result_t s_memory_cannot_be_accessed_in_ns(void)
375{
376 tftf_testcase_printf("Test not ported to AArch32\n");
377 return TEST_RESULT_SKIPPED;
378}
nabkah01905b2c72022-02-10 10:46:32 +0000379
380test_result_t s_memory_cannot_be_accessed_in_rl(void)
381{
382 tftf_testcase_printf("Test not ported to AArch32\n");
383 return TEST_RESULT_SKIPPED;
384}
385
nabkah01e7147862022-02-11 19:08:00 +0000386test_result_t rt_memory_cannot_be_accessed_in_rl(void)
387{
388 tftf_testcase_printf("Test not ported to AArch32\n");
389 return TEST_RESULT_SKIPPED;
390}
391
Manish Pandey5f513d62022-01-17 11:05:53 +0000392#endif /* __aarch64__ */