blob: 6d72f6e28d3ed6749dc3cc708eb70486b092e495 [file] [log] [blame]
Fuad Tabbaa48d1222019-12-09 15:42:32 +00001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Fuad Tabbaa48d1222019-12-09 15:42:32 +00007 */
8
9#include "hf/dlog.h"
10
11#include "vmapi/hf/call.h"
12
13#include "../msr.h"
Fuad Tabbab86325a2020-01-10 13:38:15 +000014#include "sysregs.h"
Fuad Tabbaa48d1222019-12-09 15:42:32 +000015#include "test/hftest.h"
16
17/**
18 * Tracks the number of times the exception handler has been invoked.
19 */
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000020static int exception_handler_exception_count = 0;
Fuad Tabbaa48d1222019-12-09 15:42:32 +000021
22/**
Madhukar Pappireddyf054a052022-12-22 11:37:11 -060023 * Tracks the virtual interrupt that was last handled by SP.
24 */
25static uint32_t last_serviced_interrupt = 0;
26
27/**
Kathleen Capella4eba3f32022-12-09 18:05:51 -050028 * Used to specify an instruction address to return to after exception
29 * is handled.
30 */
31static uint64_t exception_handler_return_addr = 0;
32
33/**
Fuad Tabbaa48d1222019-12-09 15:42:32 +000034 * Sends the number of exceptions handled to the Primary VM.
35 */
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000036void exception_handler_send_exception_count(void)
Fuad Tabbaa48d1222019-12-09 15:42:32 +000037{
J-Alves8304fb92022-06-24 17:14:08 +010038 struct ffa_partition_msg *exception_msg =
39 (struct ffa_partition_msg *)SERVICE_SEND_BUFFER();
Fuad Tabbaa48d1222019-12-09 15:42:32 +000040
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000041 dlog("Sending exception_count %d to primary VM\n",
42 exception_handler_exception_count);
J-Alves8304fb92022-06-24 17:14:08 +010043
44 /*
45 * TODO: remove use of HF_PRIMARY_VM_ID, replace with a mechanism that
46 * allows to detect the caller to a running test service. This may
47 * eventually become to be another endpoint, different from primary VM.
48 */
49 ffa_rxtx_header_init(hf_vm_get_id(), HF_PRIMARY_VM_ID, sizeof(int),
50 &exception_msg->header);
51 memcpy_s(exception_msg->payload, FFA_MSG_PAYLOAD_MAX,
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000052 (const void *)&exception_handler_exception_count,
53 sizeof(exception_handler_exception_count));
J-Alves8304fb92022-06-24 17:14:08 +010054 EXPECT_EQ(ffa_msg_send2(0).func, FFA_SUCCESS_32);
55 ffa_yield();
Fuad Tabbaa48d1222019-12-09 15:42:32 +000056}
57
58/**
59 * Receives the number of exceptions handled.
60 */
J-Alves8304fb92022-06-24 17:14:08 +010061int exception_handler_receive_exception_count(const void *recv_buf)
Fuad Tabbaa48d1222019-12-09 15:42:32 +000062{
J-Alves8304fb92022-06-24 17:14:08 +010063 struct ffa_partition_msg *exception_msg =
64 (struct ffa_partition_msg *)recv_buf;
65 int exception_count = *((const int *)exception_msg->payload);
66 struct ffa_value ret;
67 ffa_notifications_bitmap_t fwk_notif;
Fuad Tabbaa48d1222019-12-09 15:42:32 +000068
J-Alves8304fb92022-06-24 17:14:08 +010069 ret = ffa_notification_get(hf_vm_get_id(), 0,
70 FFA_NOTIFICATION_FLAG_BITMAP_HYP |
71 FFA_NOTIFICATION_FLAG_BITMAP_SPM);
72
73 fwk_notif = ffa_notification_get_from_framework(ret);
J-Alves8304fb92022-06-24 17:14:08 +010074
J-Alves30e26d92022-09-26 12:04:09 +010075 if (fwk_notif == 0U ||
76 exception_msg->header.size != sizeof(exception_count)) {
77 return 0;
78 }
79
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010080 EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32);
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000081 return exception_count;
Fuad Tabbaa48d1222019-12-09 15:42:32 +000082}
83
84/**
85 * EL1 exception handler to use in unit test VMs.
86 * Skips the instruction that triggered the exception.
87 */
88bool exception_handler_skip_instruction(void)
89{
90 dlog("%s function is triggered!\n", __func__);
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000091 ++exception_handler_exception_count;
Fuad Tabbaa48d1222019-12-09 15:42:32 +000092
93 /* Skip instruction that triggered the exception. */
94 uint64_t next_pc = read_msr(elr_el1);
95 next_pc += 4UL;
96 write_msr(elr_el1, next_pc);
97
98 /* Indicate that elr_el1 should not be restored. */
99 return true;
100}
101
102/**
Kathleen Capella4eba3f32022-12-09 18:05:51 -0500103 * Warning: Intended to be used only in test code.
104 * The ability to jump to any address after an exception could
105 * possibily be exploited by malicious code.
106 *
107 * Sets the specified instruction address to return to after handler exits.
108 */
109void exception_handler_set_return_addr(uint64_t instr_addr)
110{
111 exception_handler_return_addr = instr_addr;
112}
113
114/**
115 * Returns the specified instruction address to return to after handler exits.
116 */
117static uint64_t exception_handler_get_return_addr(void)
118{
119 return exception_handler_return_addr;
120}
121
122/**
123 * EL1 exception handler to use in unit test VMs.
124 * Skips to the instruction address specified in general
125 * register x19.
126 */
127bool exception_handler_skip_to_instruction(void)
128{
129 dlog("%s function is triggered!\n", __func__);
130 ++exception_handler_exception_count;
131
132 uint64_t instr_addr = exception_handler_get_return_addr();
133
134 if (instr_addr) {
135 write_msr(elr_el1, instr_addr);
136 /* Indicate that elr_el1 should not be restored. */
137 return true;
138 }
139
140 dlog_error("%s: Return address not set, restoring elr_el1\n", __func__);
141 return false;
142}
143
144/**
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000145 * EL1 exception handler to use in unit test VMs.
146 * Yields control back to the hypervisor and sends the number of exceptions.
147 */
Fuad Tabbab86325a2020-01-10 13:38:15 +0000148static bool exception_handler_yield(void)
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000149{
150 dlog("%s function is triggered!\n", __func__);
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000151 ++exception_handler_exception_count;
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000152
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000153 exception_handler_send_exception_count();
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000154
155 /* Indicate that elr_el1 should not be restored. */
156 return true;
157}
158
159/**
Fuad Tabbab86325a2020-01-10 13:38:15 +0000160 * EL1 exception handler to use in unit test VMs.
161 * Yields control back to the hypervisor and sends the number of exceptions.
162 * Asserts that the Exception Class is Unknown.
163 */
164bool exception_handler_yield_unknown(void)
165{
166 uintreg_t esr_el1 = read_msr(ESR_EL1);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100167 uintreg_t far_el1 = read_msr(FAR_EL1);
168
Fuad Tabbab86325a2020-01-10 13:38:15 +0000169 EXPECT_EQ(GET_ESR_EC(esr_el1), EC_UNKNOWN);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100170
171 /*
172 * For unknown exceptions, the value of far_el1 is UNKNOWN.
173 * Hafnium sets it to 0.
174 */
175 EXPECT_EQ(far_el1, 0);
176
Fuad Tabbab86325a2020-01-10 13:38:15 +0000177 return exception_handler_yield();
178}
179
180/**
181 * EL1 exception handler to use in unit test VMs.
182 * Yields control back to the hypervisor and sends the number of exceptions.
183 * Asserts that the Exception Class is Data Abort (same EL).
184 */
185bool exception_handler_yield_data_abort(void)
186{
187 uintreg_t esr_el1 = read_msr(ESR_EL1);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100188 uintreg_t far_el1 = read_msr(FAR_EL1);
189
Fuad Tabbab86325a2020-01-10 13:38:15 +0000190 EXPECT_EQ(GET_ESR_EC(esr_el1), EC_DATA_ABORT_SAME_EL);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100191 EXPECT_NE(far_el1, 0);
192
Fuad Tabbab86325a2020-01-10 13:38:15 +0000193 return exception_handler_yield();
194}
195
196/**
197 * EL1 exception handler to use in unit test VMs.
198 * Yields control back to the hypervisor and sends the number of exceptions.
199 * Asserts that the Exception Class is Instruction Abort (same EL).
200 */
201bool exception_handler_yield_instruction_abort(void)
202{
203 uintreg_t esr_el1 = read_msr(ESR_EL1);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100204 uintreg_t far_el1 = read_msr(FAR_EL1);
205
Fuad Tabbab86325a2020-01-10 13:38:15 +0000206 EXPECT_EQ(GET_ESR_EC(esr_el1), EC_INSTRUCTION_ABORT_SAME_EL);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100207 EXPECT_NE(far_el1, 0);
208
Fuad Tabbab86325a2020-01-10 13:38:15 +0000209 return exception_handler_yield();
210}
211
212/**
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000213 * Returns the number of times the instruction handler was invoked.
214 */
215int exception_handler_get_num(void)
216{
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000217 return exception_handler_exception_count;
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000218}
219
220/**
221 * Resets the number of exceptions counter;
222 */
223void exception_handler_reset(void)
224{
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000225 exception_handler_exception_count = 0;
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000226}
Madhukar Pappireddyf054a052022-12-22 11:37:11 -0600227
228/**
229 * Updates the last serviced virtual interrupt ID.
230 */
231void exception_handler_set_last_interrupt(uint32_t id)
232{
233 last_serviced_interrupt = id;
234}
235
236/**
237 * Returns the last serviced virtual interrupt ID.
238 */
239uint32_t exception_handler_get_last_interrupt(void)
240{
241 return last_serviced_interrupt;
242}