aboutsummaryrefslogtreecommitdiff
path: root/bl31/aarch64/exception_handlers.c
blob: 860d8ebc8154c9cea6284e5d1724bab1a3df42d3 (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
/*
 * Copyright (c) 2013, ARM Limited. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of ARM nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <arch_helpers.h>
#include <platform.h>
#include <bl_common.h>
#include <bl31.h>
#include <psci.h>
#include <assert.h>
#include <runtime_svc.h>

/*******************************************************************************
 * This function checks whether this is a valid smc e.g.
 * the function id is correct, top word of args are zeroed
 * when aarch64 makes an aarch32 call etc.
 ******************************************************************************/
int validate_smc(gp_regs *regs)
{
	unsigned int rw = GET_RW(regs->spsr);
	unsigned int cc = GET_SMC_CC(regs->x0);

	/* Check if there is a difference in the caller RW and SMC CC */
	if (rw == cc) {

		/* Check whether the caller has chosen the right func. id */
		if (cc == SMC_64) {
			regs->x0 = SMC_UNK;
			return SMC_UNK;
		}

		/*
		 * Paranoid check to zero the top word of passed args
		 * irrespective of caller's register width.
		 *
		 * TODO: Check if this needed if the caller is aarch32
		 */
		regs->x0 &= (unsigned int) 0xFFFFFFFF;
		regs->x1 &= (unsigned int) 0xFFFFFFFF;
		regs->x2 &= (unsigned int) 0xFFFFFFFF;
		regs->x3 &= (unsigned int) 0xFFFFFFFF;
		regs->x4 &= (unsigned int) 0xFFFFFFFF;
		regs->x5 &= (unsigned int) 0xFFFFFFFF;
		regs->x6 &= (unsigned int) 0xFFFFFFFF;
	}

	return 0;
}

/* TODO: Break down the SMC handler into fast and standard SMC handlers. */
void smc_handler(unsigned type, unsigned long esr, gp_regs *regs)
{
	/* Check if the SMC has been correctly called */
	if (validate_smc(regs) != 0)
		return;

	switch (regs->x0) {
	case PSCI_VERSION:
		regs->x0 = psci_version();
		break;

	case PSCI_CPU_OFF:
		regs->x0 = __psci_cpu_off();
		break;

	case PSCI_CPU_SUSPEND_AARCH64:
	case PSCI_CPU_SUSPEND_AARCH32:
		regs->x0 = __psci_cpu_suspend(regs->x1, regs->x2, regs->x3);
		break;

	case PSCI_CPU_ON_AARCH64:
	case PSCI_CPU_ON_AARCH32:
		regs->x0 = psci_cpu_on(regs->x1, regs->x2, regs->x3);
		break;

	case PSCI_AFFINITY_INFO_AARCH32:
	case PSCI_AFFINITY_INFO_AARCH64:
		regs->x0 = psci_affinity_info(regs->x1, regs->x2);
		break;

	default:
		regs->x0 = SMC_UNK;
	}

	return;
}

void irq_handler(unsigned type, unsigned long esr, gp_regs *regs)
{
	plat_report_exception(type);
	assert(0);
}

void fiq_handler(unsigned type, unsigned long esr, gp_regs *regs)
{
	plat_report_exception(type);
	assert(0);
}

void serror_handler(unsigned type, unsigned long esr, gp_regs *regs)
{
	plat_report_exception(type);
	assert(0);
}

void sync_exception_handler(unsigned type, gp_regs *regs)
{
	unsigned long esr = read_esr();
	unsigned int ec = EC_BITS(esr);

	switch (ec) {

	case EC_AARCH32_SMC:
	case EC_AARCH64_SMC:
		smc_handler(type, esr, regs);
		break;

	default:
		plat_report_exception(type);
		assert(0);
	}
	return;
}

void async_exception_handler(unsigned type, gp_regs *regs)
{
	unsigned long esr = read_esr();

	switch (type) {

	case IRQ_SP_EL0:
	case IRQ_SP_ELX:
	case IRQ_AARCH64:
	case IRQ_AARCH32:
		irq_handler(type, esr, regs);
		break;

	case FIQ_SP_EL0:
	case FIQ_SP_ELX:
	case FIQ_AARCH64:
	case FIQ_AARCH32:
		fiq_handler(type, esr, regs);
		break;

	case SERROR_SP_EL0:
	case SERROR_SP_ELX:
	case SERROR_AARCH64:
	case SERROR_AARCH32:
		serror_handler(type, esr, regs);
		break;

	default:
		plat_report_exception(type);
		assert(0);
	}

	return;
}