summaryrefslogtreecommitdiff
path: root/cactus/cactus_main.c
blob: 3dd5009b9d458dc04081a78d3bf77879bb58e50a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <console.h>
#include <debug.h>
#include <mm_svc.h>
#include <pl011.h>
#include <plat_arm.h>
#include <platform_def.h>
#include <secure_partition.h>
#include <spm_svc.h>
#include <std_svc.h>

#include "cactus.h"
#include "cactus_helpers.h"
#include "cactus_tests.h"


/* Host machine information injected by the build system in the ELF file. */
extern const char build_message[];
extern const char version_string[];

/*
 * The ARM Trusted Firmware passes a description of the memory resources
 * allocated to the secure partition through the x0 register. This maps to
 * a secure_partition_boot_info_t structure type.
 *
 * This functions prints the information stored in this structure.
 */
static void cactus_print_memory_layout(const secure_partition_boot_info_t *boot_info)
{
	NOTICE("Secure Partition memory layout:\n");

	NOTICE("  Secure Partition image   : %p - %p\n",
		(void *) boot_info->sp_image_base,
		(void *)(boot_info->sp_image_base + boot_info->sp_image_size));
	NOTICE("    Text region            : %p - %p\n",
		(void *) CACTUS_TEXT_START, (void *) CACTUS_TEXT_END);
	NOTICE("    Read-only data region  : %p - %p\n",
		(void *) CACTUS_RODATA_START, (void *) CACTUS_RODATA_END);
	NOTICE("    Read-write data region : %p - %p\n",
		(void *) CACTUS_RWDATA_START, (void *) CACTUS_RWDATA_END);
	NOTICE("      BSS region           : %p - %p\n",
		(void *) CACTUS_BSS_START, (void *) CACTUS_BSS_END);
	NOTICE("    Unused SP image space  : %p - %p\n",
		(void *) CACTUS_BSS_END,
		(void *)(boot_info->sp_image_base + boot_info->sp_image_size));

	NOTICE("  EL3-EL0 shared buffer    : %p - %p\n",
		(void *) boot_info->sp_shared_buf_base,
		(void *)(boot_info->sp_shared_buf_base + boot_info->sp_shared_buf_size));

	NOTICE("  S-NS shared buffer       : %p - %p\n",
		(void *) boot_info->sp_ns_comm_buf_base,
		(void *)(boot_info->sp_ns_comm_buf_base + boot_info->sp_ns_comm_buf_size));

	assert(boot_info->sp_ns_comm_buf_base == ARM_SECURE_SERVICE_BUFFER_BASE);
	assert(boot_info->sp_ns_comm_buf_size == ARM_SECURE_SERVICE_BUFFER_SIZE);

	NOTICE("  Stacks region (%u CPUS)   : %p - %p\n",
		boot_info->num_cpus,
		(void *) boot_info->sp_stack_base,
		(void *)(boot_info->sp_stack_base +
			 (boot_info->sp_pcpu_stack_size * boot_info->num_cpus)));

	NOTICE("  Heap region              : %p - %p\n",
		(void *) boot_info->sp_heap_base,
		(void *)(boot_info->sp_heap_base + boot_info->sp_heap_size));

	NOTICE("Total memory               : %p - %p\n",
		(void *) boot_info->sp_mem_base, (void *) boot_info->sp_mem_limit);
}




static __dead2 void secure_services_loop(void)
{
	int32_t event_status_code;
	svc_args svc_values = { 0 };

	/*
	 * The first time this loop is executed corresponds to when Cactus has
	 * finished initialising its run time environment and is ready to handle
	 * secure service requests.
	 */
	NOTICE("Cactus: Signal end of init to SPM\n");
	event_status_code = SPM_SUCCESS;

	while (1) {
		svc_values.arg0 = SP_EVENT_COMPLETE_AARCH64;
		svc_values.arg1 = event_status_code;
		int32_t event_id = cactus_svc(&svc_values);

		switch (event_id) {
		case MM_COMMUNICATE_AARCH64:
		  {
			uint64_t ctx_addr = svc_values.arg1;
			uint32_t ctx_size = svc_values.arg2;
			uint64_t cookie = svc_values.arg3;

			NOTICE("Cactus: Received MM_COMMUNICATE_AARCH64 call\n");
			NOTICE("Cactus:   Context address: 0x%lx\n", ctx_addr);
			NOTICE("Cactus:   Context size   : %u\n", ctx_size);
			NOTICE("Cactus:   Cookie         : 0x%lx\n", cookie);

			if (ctx_addr == 0) {
				ERROR("Context address is invalid\n");
				event_status_code = SPM_INVALID_PARAMETER;
				continue;
			}

			secure_partition_request_info_t *sps = (void *)(uintptr_t) ctx_addr;
			NOTICE("Received fast secure service request with ID #%u\n",
			       sps->id);
			event_status_code = SPM_SUCCESS;
			break;
		  }

		case MM_COMMUNICATE_AARCH32:
		  {
			uint32_t ctx_addr = svc_values.arg1;
			uint32_t ctx_size = svc_values.arg2;
			uint32_t cookie = svc_values.arg3;

			NOTICE("Cactus: Received MM_COMMUNICATE_AARCH32 call\n");
			NOTICE("Cactus:   Context address: 0x%x\n", ctx_addr);
			NOTICE("Cactus:   Context size   : %u\n", ctx_size);
			NOTICE("Cactus:   Cookie         : 0x%x\n", cookie);

			if (ctx_addr == 0) {
				ERROR("Context address is invalid\n");
				event_status_code = SPM_INVALID_PARAMETER;
				continue;
			}

			secure_partition_request_info_t *sps = (void *)(uintptr_t) ctx_addr;
			NOTICE("Received fast secure service request with ID #%u\n",
			       sps->id);
			event_status_code = SPM_SUCCESS;
			break;
		  }

		default:
			NOTICE("Unhandled Service ID 0x%x\n", event_id);
			event_status_code = SPM_NOT_SUPPORTED;
			break;
		}
	}
}


void __dead2 cactus_main(void *el3_el0_buffer, size_t el3_el0_buffer_size)
{
	console_init(PLAT_ARM_UART_BASE,
		     PLAT_ARM_UART_CLK_IN_HZ,
		     PL011_BAUDRATE);

	NOTICE("Booting test Secure Partition Cactus\n");
	NOTICE("%s\n", build_message);
	NOTICE("%s\n", version_string);
	NOTICE("Running at S-EL0\n");

	const secure_partition_boot_info_t *boot_info =
		(const secure_partition_boot_info_t *) el3_el0_buffer;

	if (el3_el0_buffer_size < sizeof(secure_partition_boot_info_t)) {
		ERROR("The memory buffer shared between EL3/S-EL0 is too small\n");
		ERROR("It is %lu bytes, it should be at least %lu bytes\n",
			el3_el0_buffer_size,
			sizeof(secure_partition_boot_info_t));
		panic();
	}

	if ((CACTUS_TEXT_START != boot_info->sp_image_base) ||
	    (CACTUS_RWDATA_END > boot_info->sp_image_base
		    + boot_info->sp_image_size)) {
		ERROR("Cactus does not fit in the buffer allocated for the secure partition\n");
		panic();
	}

	cactus_print_memory_layout(boot_info);


	/*
	 * Run some initial tests.
	 *
	 * These are executed when the system is still booting, just after SPM
	 * has handed over to Cactus.
	 */
	misc_tests();
	system_setup_tests();
	mem_attr_changes_tests(boot_info);

	/*
	 * Handle secure service requests.
	 */
	secure_services_loop();
}