aboutsummaryrefslogtreecommitdiff
path: root/spm/cactus/cactus_main.c
blob: 4580f23eb9e66cac0ac2f5122cdb722be61ba4a7 (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
/*
 * Copyright (c) 2018, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <cactus_def.h>
#include <debug.h>
#include <drivers/arm/pl011.h>
#include <drivers/console.h>
#include <errno.h>
#include <plat_arm.h>
#include <platform_def.h>
#include <sp_helpers.h>
#include <sprt_client.h>
#include <sprt_svc.h>
#include <std_svc.h>

#include "cactus.h"
#include "cactus_def.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[];

static void cactus_print_memory_layout(void)
{
	NOTICE("Secure Partition memory layout:\n");

	NOTICE("  Image regions\n");
	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("    Data region            : %p - %p\n",
		(void *)CACTUS_DATA_START, (void *)CACTUS_DATA_END);
	NOTICE("    BSS region             : %p - %p\n",
		(void *)CACTUS_BSS_START, (void *)CACTUS_BSS_END);
	NOTICE("    Total image memory     : %p - %p\n",
		(void *)CACTUS_IMAGE_BASE,
		(void *)(CACTUS_IMAGE_BASE + CACTUS_IMAGE_SIZE));
	NOTICE("  SPM regions\n");
	NOTICE("    SPM <-> SP buffer      : %p - %p\n",
		(void *)CACTUS_SPM_BUF_BASE,
		(void *)(CACTUS_SPM_BUF_BASE + CACTUS_SPM_BUF_SIZE));
	NOTICE("    NS <-> SP buffer       : %p - %p\n",
		(void *)CACTUS_NS_BUF_BASE,
		(void *)(CACTUS_NS_BUF_BASE + CACTUS_NS_BUF_SIZE));
	NOTICE("  Test regions\n");
	NOTICE("    Test region            : %p - %p\n",
		(void *)CACTUS_TEST_MEM_BASE,
		(void *)(CACTUS_TEST_MEM_BASE + CACTUS_TEST_MEM_SIZE));
}

static void cactus_message_handler(struct sprt_queue_entry_message *message)
{
	u_register_t ret0 = 0U, ret1 = 0U, ret2 = 0U, ret3 = 0U;

	if (message->type == SPRT_MSG_TYPE_SERVICE_REQUEST) {
		switch (message->args[1]) {

		case CACTUS_PRINT_MAGIC:
			INFO("Cactus: Magic: 0x%x\n", CACTUS_MAGIC_NUMBER);
			ret0 = SPRT_SUCCESS;
			break;

		case CACTUS_GET_MAGIC:
			ret1 = CACTUS_MAGIC_NUMBER;
			ret0 = SPRT_SUCCESS;
			break;

		case CACTUS_SLEEP_MS:
			sp_sleep(message->args[2]);
			ret0 = SPRT_SUCCESS;
			break;

		default:
			NOTICE("Cactus: Unhandled Service ID 0x%x\n",
			       (unsigned int)message->args[1]);
			ret0 = SPRT_NOT_SUPPORTED;
			break;
		}
	} else {
		NOTICE("Cactus: Unhandled Service type 0x%x\n",
		       (unsigned int)message->type);
		ret0 = SPRT_NOT_SUPPORTED;
	}

	sprt_message_end(message, ret0, ret1, ret2, ret3);
}

void __dead2 cactus_main(void)
{
	console_init(PL011_UART2_BASE,
		     PL011_UART2_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");

	cactus_print_memory_layout();

	/*
	 * 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();

	/*
	 * Handle secure service requests.
	 */
	sprt_initialize_queues((void *)CACTUS_SPM_BUF_BASE);

	while (1) {
		struct sprt_queue_entry_message message;

		/*
		 * Try to fetch a message from the blocking requests queue. If
		 * it is empty, try to fetch from the non-blocking requests
		 * queue. Repeat until both of them are empty.
		 */
		while (1) {
			int err = sprt_get_next_message(&message,
					SPRT_QUEUE_NUM_BLOCKING);
			if (err == -ENOENT) {
				err = sprt_get_next_message(&message,
						SPRT_QUEUE_NUM_NON_BLOCKING);
				if (err == -ENOENT) {
					break;
				} else {
					assert(err == 0);
					cactus_message_handler(&message);
				}
			} else {
				assert(err == 0);
				cactus_message_handler(&message);
			}
		}

		sprt_wait_for_messages();
	}
}