blob: c6331e446938055aa7c03e28c8fcc2ed791eb350 [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
AlexeiFedorov380b2af2022-11-23 17:31:27 +0000143 rmi_init_cmp_result();
144
Olivier Deprez82bbc572022-01-11 11:50:36 +0100145 /* Delegate the shared page to Realm. */
nabkah01002e5692022-10-10 12:36:46 +0100146 retmm = rmi_granule_delegate((u_register_t)&share_page);
Olivier Deprez82bbc572022-01-11 11:50:36 +0100147 if (retmm != 0UL) {
148 ERROR("Granule delegate failed!\n");
149 goto out_unregister;
150 }
151
152 /* This access shall trigger a GPF. */
153 *((volatile uint64_t *)share_page);
154
155 if ((sync_exception_triggered != true) ||
156 (data_abort_triggered != true)) {
157 goto out_undelegate;
158 }
159
AlexeiFedorov380b2af2022-11-23 17:31:27 +0000160 result = host_cmp_result();
Olivier Deprez82bbc572022-01-11 11:50:36 +0100161
162out_undelegate:
163 /* Undelegate the shared page. */
nabkah01002e5692022-10-10 12:36:46 +0100164 retmm = rmi_granule_undelegate((u_register_t)&share_page);
Olivier Deprez82bbc572022-01-11 11:50:36 +0100165 if (retmm != 0UL) {
166 ERROR("Granule undelegate failed!\n");
167 }
168
169out_unregister:
170 unregister_custom_sync_exception_handler();
171
172 return result;
173}
174
nabkah01bf00f8d2022-03-22 12:37:19 +0000175/**
176 * @Test_Aim@ Check a secure region cannot be accessed from normal world.
177 *
178 * Following test intends to run on RME enabled platforms when EL3
179 * is Root world. In a non RME platform, EL3 is secure.
180 * Access to secure memory from NS world is already covered
181 * by el3_memory_cannot_be_accessed_in_ns.
182 */
183test_result_t s_memory_cannot_be_accessed_in_ns(void)
184{
185 const uintptr_t test_address = SECURE_MEMORY_ACCESS_ADDR;
186
187 /* skipp non RME platforms */
188 if (get_armv9_2_feat_rme_support() == 0U) {
189 return TEST_RESULT_SKIPPED;
190 }
191
192 VERBOSE("Attempt to access secure memory (0x%lx)\n", test_address);
193
194 data_abort_triggered = false;
195 sync_exception_triggered = false;
196 register_custom_sync_exception_handler(data_abort_handler);
197 dsbsy();
198
199 int rc = mmap_add_dynamic_region(test_address, test_address, PAGE_SIZE,
200 MT_MEMORY | MT_RW | MT_NS);
201
202 if (rc != 0) {
203 tftf_testcase_printf("%d: mmap_add_dynamic_region() = %d\n", __LINE__, rc);
204 return TEST_RESULT_FAIL;
205 }
206
207 *((volatile uint64_t *)test_address);
208
209 mmap_remove_dynamic_region(test_address, PAGE_SIZE);
210
211 dsbsy();
212 unregister_custom_sync_exception_handler();
213
214 if (sync_exception_triggered == false) {
215 tftf_testcase_printf("No sync exception while accessing (0x%lx)\n", test_address);
216 return TEST_RESULT_SKIPPED;
217 }
218
219 if (data_abort_triggered == false) {
220 tftf_testcase_printf("Sync exception is not data abort\n");
221 return TEST_RESULT_FAIL;
222 }
223
224 return TEST_RESULT_SUCCESS;
225}
226
nabkah01905b2c72022-02-10 10:46:32 +0000227static test_result_t memory_cannot_be_accessed_in_rl(u_register_t params)
228{
229 u_register_t retrmm;
AlexeiFedorovc21694d2022-12-16 12:19:52 +0000230 test_result_t result = TEST_RESULT_FAIL;
nabkah01905b2c72022-02-10 10:46:32 +0000231 static char rd[GRANULE_SIZE] __aligned(GRANULE_SIZE);
232
233 if (get_armv9_2_feat_rme_support() == 0U) {
234 return TEST_RESULT_SKIPPED;
235 }
236
AlexeiFedorov380b2af2022-11-23 17:31:27 +0000237 rmi_init_cmp_result();
238
nabkah01002e5692022-10-10 12:36:46 +0100239 retrmm = rmi_version();
nabkah01905b2c72022-02-10 10:46:32 +0000240
241 VERBOSE("RMM version is: %lu.%lu\n",
242 RMI_ABI_VERSION_GET_MAJOR(retrmm),
243 RMI_ABI_VERSION_GET_MINOR(retrmm));
244
245 /*
246 * TODO: Remove this once SMC_RMM_REALM_CREATE is implemented in TRP
247 * For the moment skip the test if RMM is TRP, TRP version is always null.
248 */
249 if (retrmm == 0U) {
250 return TEST_RESULT_SKIPPED;
251 }
252
nabkah01002e5692022-10-10 12:36:46 +0100253 retrmm = rmi_granule_delegate((u_register_t)&rd[0]);
nabkah01905b2c72022-02-10 10:46:32 +0000254 if (retrmm != 0UL) {
255 ERROR("Delegate operation returns fail, %lx\n", retrmm);
256 return TEST_RESULT_FAIL;
257 }
258
259 /* Create a realm using a parameter in a secure physical address space should fail. */
nabkah01002e5692022-10-10 12:36:46 +0100260 retrmm = rmi_realm_create((u_register_t)&rd[0], params);
nabkah01905b2c72022-02-10 10:46:32 +0000261 if (retrmm == 0UL) {
262 ERROR("Realm create operation should fail, %lx\n", retrmm);
nabkah01002e5692022-10-10 12:36:46 +0100263 retrmm = rmi_realm_destroy((u_register_t)&rd[0]);
nabkah01905b2c72022-02-10 10:46:32 +0000264 if (retrmm != 0UL) {
265 ERROR("Realm destroy operation returns fail, %lx\n", retrmm);
nabkah01905b2c72022-02-10 10:46:32 +0000266 }
AlexeiFedorovc21694d2022-12-16 12:19:52 +0000267 } else if (retrmm != RMI_ERROR_INPUT) {
268 ERROR("Realm create operation should fail with code:%d retrmm:%ld\n",
269 RMI_ERROR_INPUT, retrmm);
270 } else {
271 result = TEST_RESULT_SUCCESS;
nabkah01905b2c72022-02-10 10:46:32 +0000272 }
273
nabkah01002e5692022-10-10 12:36:46 +0100274 retrmm = rmi_granule_undelegate((u_register_t)&rd[0]);
nabkah01905b2c72022-02-10 10:46:32 +0000275 if (retrmm != 0UL) {
276 INFO("Undelegate operation returns fail, %lx\n", retrmm);
277 return TEST_RESULT_FAIL;
278 }
279
AlexeiFedorovc21694d2022-12-16 12:19:52 +0000280 if (result == TEST_RESULT_SUCCESS) {
281 return host_cmp_result();
282 }
283
284 return TEST_RESULT_FAIL;
nabkah01905b2c72022-02-10 10:46:32 +0000285}
286
nabkah0146b418d2022-02-16 17:01:54 +0000287/**
288 * @Test_Aim@ Check a root region cannot be accessed from a secure partition.
289 *
290 * This change adds TFTF and cactus test to permit checking a root region
291 * cannot be accessed from secure world.
292 * A hardcoded address marked Root in the GPT is shared to a secure
293 * partition. The SP retrieves the region from the SPM, maps it and
294 * attempts a read access to the region. It is expected to trigger a GPF
295 * data abort on the PE caught by a custom exception handler.
296 *
297 */
298test_result_t rt_memory_cannot_be_accessed_in_s(void)
299{
300 const uintptr_t test_address = EL3_MEMORY_ACCESS_ADDR;
301 struct ffa_memory_region_constituent constituents[] = {
302 {
303 (void *)test_address, 1, 0
304 }
305 };
306 const uint32_t constituents_count = sizeof(constituents) /
307 sizeof(struct ffa_memory_region_constituent);
308 ffa_memory_handle_t handle;
309 struct mailbox_buffers mb;
Daniel Boulbyce386b12022-03-29 18:36:36 +0100310 struct ffa_value ret;
nabkah0146b418d2022-02-16 17:01:54 +0000311
312 if (get_armv9_2_feat_rme_support() == 0U) {
313 return TEST_RESULT_SKIPPED;
314 }
315
Olivier Deprezfac8ed72022-12-20 11:09:43 +0100316 SKIP_TEST_IF_FFA_VERSION_LESS_THAN(1,1);
nabkah0146b418d2022-02-16 17:01:54 +0000317
AlexeiFedorovc21694d2022-12-16 12:19:52 +0000318 INIT_TFTF_MAILBOX(mb);
319
Olivier Deprezfac8ed72022-12-20 11:09:43 +0100320 CHECK_SPMC_TESTING_SETUP(1, 1, expected_sp_uuids);
321
nabkah0146b418d2022-02-16 17:01:54 +0000322 GET_TFTF_MAILBOX(mb);
323
324 handle = memory_init_and_send((struct ffa_memory_region *)mb.send,
325 PAGE_SIZE, SENDER, RECEIVER,
326 constituents, constituents_count,
327 FFA_MEM_SHARE_SMC32, &ret);
328
329 if (handle == FFA_MEMORY_HANDLE_INVALID) {
330 return TEST_RESULT_FAIL;
331 }
332
333 VERBOSE("TFTF - Handle: %llx Address: %p\n",
334 handle, constituents[0].address);
335
336 /* Retrieve the shared page and attempt accessing it. */
337 ret = cactus_mem_send_cmd(SENDER, RECEIVER, FFA_MEM_SHARE_SMC32,
338 handle, 0, true, 1);
339
340 if (is_ffa_call_error(ffa_mem_reclaim(handle, 0))) {
341 ERROR("Memory reclaim failed!\n");
342 return TEST_RESULT_FAIL;
343 }
344
345 /*
346 * Expect success response with value 1 hinting an exception
347 * triggered while the SP accessed the region.
348 */
349 if (!(cactus_get_response(ret) == CACTUS_SUCCESS &&
350 cactus_error_code(ret) == 1)) {
351 ERROR("Exceptions test failed!\n");
352 return TEST_RESULT_FAIL;
353 }
354
355 return TEST_RESULT_SUCCESS;
356}
357
nabkah01905b2c72022-02-10 10:46:32 +0000358test_result_t s_memory_cannot_be_accessed_in_rl(void)
359{
360 u_register_t params = (u_register_t)SECURE_MEMORY_ACCESS_ADDR;
361 return memory_cannot_be_accessed_in_rl(params);
362}
363
nabkah01e7147862022-02-11 19:08:00 +0000364test_result_t rt_memory_cannot_be_accessed_in_rl(void)
365{
366 u_register_t params = (u_register_t)EL3_MEMORY_ACCESS_ADDR;
367 return memory_cannot_be_accessed_in_rl(params);
368}
369
Manish Pandey5f513d62022-01-17 11:05:53 +0000370#else
Olivier Deprez82bbc572022-01-11 11:50:36 +0100371
nabkah01bf00f8d2022-03-22 12:37:19 +0000372test_result_t el3_memory_cannot_be_accessed_in_ns(void)
Manish Pandey5f513d62022-01-17 11:05:53 +0000373{
374 tftf_testcase_printf("Test not ported to AArch32\n");
375 return TEST_RESULT_SKIPPED;
376}
Olivier Deprez82bbc572022-01-11 11:50:36 +0100377
378test_result_t rl_memory_cannot_be_accessed_in_ns(void)
379{
380 tftf_testcase_printf("Test not ported to AArch32\n");
381 return TEST_RESULT_SKIPPED;
382}
383
nabkah01bf00f8d2022-03-22 12:37:19 +0000384test_result_t s_memory_cannot_be_accessed_in_ns(void)
385{
386 tftf_testcase_printf("Test not ported to AArch32\n");
387 return TEST_RESULT_SKIPPED;
388}
nabkah01905b2c72022-02-10 10:46:32 +0000389
390test_result_t s_memory_cannot_be_accessed_in_rl(void)
391{
392 tftf_testcase_printf("Test not ported to AArch32\n");
393 return TEST_RESULT_SKIPPED;
394}
395
nabkah01e7147862022-02-11 19:08:00 +0000396test_result_t rt_memory_cannot_be_accessed_in_rl(void)
397{
398 tftf_testcase_printf("Test not ported to AArch32\n");
399 return TEST_RESULT_SKIPPED;
400}
401
Manish Pandey5f513d62022-01-17 11:05:53 +0000402#endif /* __aarch64__ */