blob: 3883fac82bffb8286027d3822e06535c9a04afae [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Olivier Deprezafcdb7c2019-11-29 14:21:48 +01002 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +01008#include <errno.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02009
10#include "cactus.h"
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +010011#include "cactus_def.h"
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +010012#include <cactus_platform_def.h>
J-Alves9f6f0142020-06-17 15:37:59 +010013#include "cactus_tests.h"
14#include <debug.h>
15#include <drivers/arm/pl011.h>
16#include <drivers/console.h>
J-Alves5aecd982020-06-11 10:25:33 +010017#include <ffa_helpers.h>
J-Alves9f6f0142020-06-17 15:37:59 +010018#include <lib/aarch64/arch_helpers.h>
19#include <lib/xlat_tables/xlat_mmu_helpers.h>
20#include <lib/xlat_tables/xlat_tables_v2.h>
J-Alves5aecd982020-06-11 10:25:33 +010021#include <sp_helpers.h>
J-Alves9f6f0142020-06-17 15:37:59 +010022#include <std_svc.h>
23#include <plat/common/platform.h>
24#include <plat_arm.h>
25#include <platform_def.h>
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010026
J-Alves63cdaa72020-10-08 17:22:45 +010027#include <cactus_test_cmds.h>
28
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020029/* Host machine information injected by the build system in the ELF file. */
30extern const char build_message[];
31extern const char version_string[];
32
J-Alvesd8edeed2020-11-18 10:48:12 +000033/* Memory section to be used for memory share operations */
34static __aligned(PAGE_SIZE) uint8_t share_page[PAGE_SIZE];
35
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010036/*
37 *
38 * Message loop function
39 * Notice we cannot use regular print functions because this serves to both
40 * "primary" and "secondary" VMs. Secondary VM cannot access UART directly
41 * but rather through Hafnium print hypercall.
42 *
43 */
J-Alves63cdaa72020-10-08 17:22:45 +010044static void __dead2 message_loop(ffa_vm_id_t vm_id, struct mailbox_buffers *mb)
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010045{
J-Alves7581c382020-05-07 18:34:20 +010046 smc_ret_values ffa_ret;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010047 uint32_t sp_response;
J-Alves63cdaa72020-10-08 17:22:45 +010048 ffa_vm_id_t source;
J-Alvesda6ac322020-11-09 15:45:30 +000049 ffa_vm_id_t destination;
J-Alves1d203f12020-11-11 11:38:49 +000050 uint64_t cactus_cmd;
51
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010052 /*
J-Alves63cdaa72020-10-08 17:22:45 +010053 * This initial wait call is necessary to inform SPMD that
54 * SP initialization has completed. It blocks until receiving
55 * a direct message request.
56 */
57
J-Alves7581c382020-05-07 18:34:20 +010058 ffa_ret = ffa_msg_wait();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010059
60 for (;;) {
J-Alves63cdaa72020-10-08 17:22:45 +010061 VERBOSE("Woke up with func id: %lx\n", ffa_ret.ret0);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010062
J-Alvesda6ac322020-11-09 15:45:30 +000063 if (ffa_ret.ret0 == FFA_ERROR) {
64 ERROR("Error: %lx\n", ffa_ret.ret2);
65 break;
66 }
67
J-Alves63cdaa72020-10-08 17:22:45 +010068 if (ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_REQ_SMC32 &&
69 ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_REQ_SMC64) {
Olivier Deprez73d81cf2020-09-15 16:57:00 +020070 ERROR("%s(%u) unknown func id 0x%lx\n",
71 __func__, vm_id, ffa_ret.ret0);
72 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010073 }
74
J-Alvesda6ac322020-11-09 15:45:30 +000075 destination = ffa_ret.ret1 & U(0xFFFF);
76
77 source = ffa_ret.ret1 >> 16;
78
79 if (destination != vm_id) {
Olivier Deprez73d81cf2020-09-15 16:57:00 +020080 ERROR("%s(%u) invalid vm id 0x%lx\n",
81 __func__, vm_id, ffa_ret.ret1);
82 break;
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010083 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010084
J-Alves63cdaa72020-10-08 17:22:45 +010085 PRINT_CMD(ffa_ret);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +010086
J-Alves1d203f12020-11-11 11:38:49 +000087 cactus_cmd = CACTUS_GET_CMD(ffa_ret);
88
89 switch (cactus_cmd) {
J-Alves63cdaa72020-10-08 17:22:45 +010090 case FFA_MEM_SHARE_SMC32:
91 case FFA_MEM_LEND_SMC32:
92 case FFA_MEM_DONATE_SMC32:
93 ffa_memory_management_test(
94 mb, vm_id, source,
95 CACTUS_GET_CMD(ffa_ret),
96 CACTUS_MEM_SEND_GET_HANDLE(ffa_ret));
97
98 /*
99 * If execution gets to this point means all operations
100 * with memory retrieval went well, as such replying
101 */
102 ffa_ret = CACTUS_SUCCESS_RESP(vm_id, source);
103 break;
J-Alvesd8edeed2020-11-18 10:48:12 +0000104 case CACTUS_REQ_MEM_SEND_CMD:
105 {
106 uint32_t mem_func =
107 CACTUS_REQ_MEM_SEND_GET_MEM_FUNC(ffa_ret);
108 ffa_vm_id_t receiver =
109 CACTUS_REQ_MEM_SEND_GET_RECEIVER(ffa_ret);
110 ffa_memory_handle_t handle;
J-Alvesda6ac322020-11-09 15:45:30 +0000111
J-Alvesd8edeed2020-11-18 10:48:12 +0000112 VERBOSE("%x requested to send memory to %x (func: %x)\n",
113 source, receiver, mem_func);
114
115 const struct ffa_memory_region_constituent
116 constituents[] = {
117 {(void *)share_page, 1, 0}
118 };
119
120 const uint32_t constituents_count = (
121 sizeof(constituents) /
122 sizeof(constituents[0])
123 );
124
125 handle = ffa_memory_init_and_send(
126 (struct ffa_memory_region *)mb->send, PAGE_SIZE,
127 vm_id, receiver, constituents,
128 constituents_count, mem_func);
129
130 /*
131 * If returned an invalid handle, we should break the
132 * test.
133 */
134 expect(handle != FFA_MEMORY_HANDLE_INVALID, true);
135
136 ffa_ret = CACTUS_MEM_SEND_CMD(vm_id, receiver, mem_func,
137 handle);
138
139 if (ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_RESP_SMC32) {
140 ERROR("Failed to send message. error: %lx\n",
141 ffa_ret.ret2);
142 ffa_ret = CACTUS_ERROR_RESP(vm_id, source);
143 break;
144 }
145
146 /* If anything went bad on the receiver's end. */
147 if (CACTUS_IS_ERROR_RESP(ffa_ret)) {
148 ERROR("Received error from receiver!\n");
149 ffa_ret = CACTUS_ERROR_RESP(vm_id, source);
150 break;
151 }
152
153 if (mem_func != FFA_MEM_DONATE_SMC32) {
154 /*
155 * Do a memory reclaim only if the mem_func
156 * regards to memory share or lend operations,
157 * as with a donate the owner is permanently
158 * given up access to the memory region.
159 */
160 if (ffa_mem_reclaim(handle, 0)
161 .ret0 == FFA_ERROR) {
162 ERROR("Failed to reclaim memory!\n");
163 ffa_ret = CACTUS_ERROR_RESP(vm_id,
164 source);
165 break;
166 }
167
168 /**
169 * Read Content that has been written to memory
170 * to validate access to memory segment has been
171 * reestablished, and receiver made use of
172 * memory region.
173 */
174 #if (LOG_LEVEL >= LOG_LEVEL_VERBOSE)
175 uint32_t *ptr =
176 (uint32_t *)constituents
177 ->address;
178 VERBOSE("Memory contents after receiver"
179 " SP's use:\n");
180 for (unsigned int i = 0U; i < 5U; i++)
181 VERBOSE(" %u: %x\n", i,
182 ptr[i]);
183 #endif
184 }
185
186 ffa_ret = CACTUS_SUCCESS_RESP(vm_id, source);
187 }
188 break;
J-Alvesda6ac322020-11-09 15:45:30 +0000189 case CACTUS_ECHO_CMD:
190 {
191 uint64_t echo_val = CACTUS_ECHO_GET_VAL(ffa_ret);
192
193 VERBOSE("Received echo at %x, value %llx.\n",
194 destination, echo_val);
195 ffa_ret = CACTUS_RESPONSE(vm_id, source, echo_val);
196 break;
197 }
198 case CACTUS_REQ_ECHO_CMD:
199 {
200 ffa_vm_id_t echo_dest =
201 CACTUS_REQ_ECHO_GET_ECHO_DEST(ffa_ret);
202 uint64_t echo_val = CACTUS_ECHO_GET_VAL(ffa_ret);
203 bool success = true;
204
205 VERBOSE("%x requested to send echo to %x, value %llx\n",
206 source, echo_dest, echo_val);
207
208 ffa_ret = CACTUS_ECHO_SEND_CMD(vm_id, echo_dest,
209 echo_val);
210
211 if (ffa_ret.ret0 != FFA_MSG_SEND_DIRECT_RESP_SMC32) {
212 ERROR("Failed to send message. error: %lx\n",
213 ffa_ret.ret2);
214 success = false;
215 }
216
217 if (CACTUS_GET_RESPONSE(ffa_ret) != echo_val) {
218 ERROR("Echo Failed!\n");
219 success = false;
220 }
221
222 ffa_ret = success ? CACTUS_SUCCESS_RESP(vm_id, source) :
223 CACTUS_ERROR_RESP(vm_id, source);
224 break;
225 }
J-Alves1d203f12020-11-11 11:38:49 +0000226 case CACTUS_DEADLOCK_CMD:
227 case CACTUS_REQ_DEADLOCK_CMD:
228 {
229 ffa_vm_id_t deadlock_dest =
230 CACTUS_DEADLOCK_GET_NEXT_DEST(ffa_ret);
231 ffa_vm_id_t deadlock_next_dest = source;
232
233 if (cactus_cmd == CACTUS_DEADLOCK_CMD) {
234 VERBOSE("%x is creating deadlock. next: %x\n",
235 source, deadlock_dest);
236 } else if (cactus_cmd == CACTUS_REQ_DEADLOCK_CMD) {
237 VERBOSE(
238 "%x requested deadlock with %x and %x\n",
239 source, deadlock_dest, deadlock_next_dest);
240
241 deadlock_next_dest =
242 CACTUS_DEADLOCK_GET_NEXT_DEST2(ffa_ret);
243 }
244
245 ffa_ret = CACTUS_DEADLOCK_SEND_CMD(vm_id, deadlock_dest,
246 deadlock_next_dest);
247
248 /*
249 * Should be true for the last partition to attempt
250 * an FF-A direct message, to the first partition.
251 */
252 bool is_deadlock_detected =
253 (ffa_ret.ret0 == FFA_ERROR) &&
254 (ffa_ret.ret2 == FFA_ERROR_BUSY);
255
256 /*
257 * Should be true after the deadlock has been detected
258 * and after the first response has been sent down the
259 * request chain.
260 */
261 bool is_returning_from_deadlock =
262 (ffa_ret.ret0 == FFA_MSG_SEND_DIRECT_RESP_SMC32)
263 && (CACTUS_IS_SUCCESS_RESP(ffa_ret));
264
265 if (is_deadlock_detected) {
266 NOTICE("Attempting dealock but got error %lx\n",
267 ffa_ret.ret2);
268 }
269
270 if (is_deadlock_detected ||
271 is_returning_from_deadlock) {
272 /*
273 * This is not the partition, that would have
274 * created the deadlock. As such, reply back
275 * to the partitions.
276 */
277 ffa_ret = CACTUS_SUCCESS_RESP(vm_id, source);
278 break;
279 }
280
281 /* Shouldn't get to this point */
282 ERROR("Deadlock test went wrong!\n");
283 ffa_ret = CACTUS_ERROR_RESP(vm_id, source);
284
285 break;
286 }
J-Alves63cdaa72020-10-08 17:22:45 +0100287 default:
288 /*
289 * Currently direct message test is handled here.
290 * TODO: create a case within the switch case
291 * For the sake of testing, add the vm id to the
292 * received message.
293 */
J-Alves63cdaa72020-10-08 17:22:45 +0100294 sp_response = ffa_ret.ret3 | vm_id;
Olivier Deprezaed7f082020-11-04 15:11:59 +0100295 VERBOSE("Replying with direct message response: %x\n", sp_response);
J-Alves63cdaa72020-10-08 17:22:45 +0100296 ffa_ret = ffa_msg_send_direct_resp(vm_id,
297 HYP_ID,
298 sp_response);
299
300 break;
301 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100302 }
Olivier Deprez73d81cf2020-09-15 16:57:00 +0200303
304 panic();
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100305}
306
307static const mmap_region_t cactus_mmap[] __attribute__((used)) = {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100308 /* PLAT_ARM_DEVICE0 area includes UART2 necessary to console */
309 MAP_REGION_FLAT(PLAT_ARM_DEVICE0_BASE, PLAT_ARM_DEVICE0_SIZE,
310 MT_DEVICE | MT_RW),
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100311 {0}
312};
313
Manish Pandey26c6f812020-04-30 11:43:00 +0100314static void cactus_print_memory_layout(unsigned int vm_id)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200315{
Olivier Deprezaed7f082020-11-04 15:11:59 +0100316 INFO("Secure Partition memory layout:\n");
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200317
Olivier Deprezaed7f082020-11-04 15:11:59 +0100318 INFO(" Text region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100319 (void *)CACTUS_TEXT_START, (void *)CACTUS_TEXT_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100320
Olivier Deprezaed7f082020-11-04 15:11:59 +0100321 INFO(" Read-only data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100322 (void *)CACTUS_RODATA_START, (void *)CACTUS_RODATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100323
Olivier Deprezaed7f082020-11-04 15:11:59 +0100324 INFO(" Data region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100325 (void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100326
Olivier Deprezaed7f082020-11-04 15:11:59 +0100327 INFO(" BSS region : %p - %p\n",
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100328 (void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
Manish Pandey26c6f812020-04-30 11:43:00 +0100329
Olivier Deprezaed7f082020-11-04 15:11:59 +0100330 INFO(" RX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100331 (void *)get_sp_rx_start(vm_id),
332 (void *)get_sp_rx_end(vm_id));
Manish Pandey26c6f812020-04-30 11:43:00 +0100333
Olivier Deprezaed7f082020-11-04 15:11:59 +0100334 INFO(" TX : %p - %p\n",
Max Shvetsovc32f4782020-06-23 09:41:15 +0100335 (void *)get_sp_tx_start(vm_id),
336 (void *)get_sp_tx_end(vm_id));
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200337}
338
Manish Pandey26c6f812020-04-30 11:43:00 +0100339static void cactus_plat_configure_mmu(unsigned int vm_id)
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100340{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100341 mmap_add_region(CACTUS_TEXT_START,
342 CACTUS_TEXT_START,
343 CACTUS_TEXT_END - CACTUS_TEXT_START,
344 MT_CODE);
345 mmap_add_region(CACTUS_RODATA_START,
346 CACTUS_RODATA_START,
347 CACTUS_RODATA_END - CACTUS_RODATA_START,
348 MT_RO_DATA);
349 mmap_add_region(CACTUS_DATA_START,
350 CACTUS_DATA_START,
351 CACTUS_DATA_END - CACTUS_DATA_START,
352 MT_RW_DATA);
353 mmap_add_region(CACTUS_BSS_START,
354 CACTUS_BSS_START,
355 CACTUS_BSS_END - CACTUS_BSS_START,
356 MT_RW_DATA);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100357
Max Shvetsovc32f4782020-06-23 09:41:15 +0100358 mmap_add_region(get_sp_rx_start(vm_id),
359 get_sp_rx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100360 (CACTUS_RX_TX_SIZE / 2),
361 MT_RO_DATA);
362
Max Shvetsovc32f4782020-06-23 09:41:15 +0100363 mmap_add_region(get_sp_tx_start(vm_id),
364 get_sp_tx_start(vm_id),
Manish Pandey26c6f812020-04-30 11:43:00 +0100365 (CACTUS_RX_TX_SIZE / 2),
366 MT_RW_DATA);
367
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100368 mmap_add(cactus_mmap);
369 init_xlat_tables();
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100370}
371
Olivier Deprez458d5532020-06-09 17:56:20 +0200372int tftf_irq_handler_dispatcher(void)
373{
374 ERROR("%s\n", __func__);
375
376 return 0;
377}
378
Antonio Nino Diaz1486f3b2018-06-26 10:30:10 +0100379void __dead2 cactus_main(void)
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200380{
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100381 assert(IS_IN_EL1() != 0);
382
Max Shvetsovc32f4782020-06-23 09:41:15 +0100383 struct mailbox_buffers mb;
J-Alvesda6ac322020-11-09 15:45:30 +0000384
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100385 /* Clear BSS */
386 memset((void *)CACTUS_BSS_START,
387 0, CACTUS_BSS_END - CACTUS_BSS_START);
388
J-Alves7581c382020-05-07 18:34:20 +0100389 /* Get current FFA id */
390 smc_ret_values ffa_id_ret = ffa_id_get();
391 if (ffa_id_ret.ret0 != FFA_SUCCESS_SMC32) {
392 ERROR("FFA_ID_GET failed.\n");
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100393 panic();
394 }
395
J-Alves7581c382020-05-07 18:34:20 +0100396 ffa_vm_id_t ffa_id = ffa_id_ret.ret2 & 0xffff;
Max Shvetsovc32f4782020-06-23 09:41:15 +0100397 mb.send = (void *) get_sp_tx_start(ffa_id);
398 mb.recv = (void *) get_sp_rx_start(ffa_id);
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100399
Manish Pandey26c6f812020-04-30 11:43:00 +0100400 /* Configure and enable Stage-1 MMU, enable D-Cache */
401 cactus_plat_configure_mmu(ffa_id);
402 enable_mmu_el1(0);
403
J-Alves7581c382020-05-07 18:34:20 +0100404 if (ffa_id == SPM_VM_ID_FIRST) {
Arunachalam Ganapathy51be1fe2020-09-22 13:25:21 +0100405 console_init(CACTUS_PL011_UART_BASE,
406 CACTUS_PL011_UART_CLK_IN_HZ,
407 PL011_BAUDRATE);
Manish Pandey29495372020-04-09 15:19:26 +0100408
409 set_putc_impl(PL011_AS_STDOUT);
410
411 NOTICE("Booting Primary Cactus Secure Partition\n%s\n%s\n",
412 build_message, version_string);
Manish Pandey29495372020-04-09 15:19:26 +0100413 } else {
J-Alves83ede9b2020-11-02 17:37:19 +0000414 smc_ret_values ret;
Manish Pandey29495372020-04-09 15:19:26 +0100415 set_putc_impl(HVC_CALL_AS_STDOUT);
416
Olivier Deprezaed7f082020-11-04 15:11:59 +0100417 NOTICE("Booting Secondary Cactus Secure Partition (ID: %x)\n%s\n%s\n",
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100418 ffa_id, build_message, version_string);
419
Olivier Deprez0be4abe2020-08-04 11:26:13 +0200420 if (ffa_id == (SPM_VM_ID_FIRST + 2)) {
Olivier Deprezaed7f082020-11-04 15:11:59 +0100421 VERBOSE("Mapping RXTX Region\n");
J-Alves83ede9b2020-11-02 17:37:19 +0000422 CONFIGURE_AND_MAP_MAILBOX(mb, PAGE_SIZE, ret);
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100423 if (ret.ret0 != FFA_SUCCESS_SMC32) {
J-Alves83ede9b2020-11-02 17:37:19 +0000424 ERROR(
425 "Failed to map RXTX buffers. Error: %lx\n",
426 ret.ret2);
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100427 panic();
428 }
Ruari Phippsbd0a7e42020-07-17 16:42:21 +0100429 }
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100430 }
Manish Pandey26c6f812020-04-30 11:43:00 +0100431
Olivier Deprezaed7f082020-11-04 15:11:59 +0100432 INFO("FF-A id: %x\n", ffa_id);
Manish Pandey26c6f812020-04-30 11:43:00 +0100433 cactus_print_memory_layout(ffa_id);
434
J-Alves9f6f0142020-06-17 15:37:59 +0100435 /* Invoking Tests */
Max Shvetsovc32f4782020-06-23 09:41:15 +0100436 ffa_tests(&mb);
J-Alves9f6f0142020-06-17 15:37:59 +0100437
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100438 /* End up to message loop */
J-Alves63cdaa72020-10-08 17:22:45 +0100439 message_loop(ffa_id, &mb);
Antonio Nino Diaz43ef3932018-07-03 14:39:47 +0100440
Olivier Deprezafcdb7c2019-11-29 14:21:48 +0100441 /* Not reached */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200442}