Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * 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 Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "hf/dlog.h" |
| 10 | |
| 11 | #include "vmapi/hf/call.h" |
| 12 | |
| 13 | #include "../msr.h" |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 14 | #include "sysregs.h" |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 15 | #include "test/hftest.h" |
| 16 | |
| 17 | /** |
| 18 | * Tracks the number of times the exception handler has been invoked. |
| 19 | */ |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 20 | static int exception_handler_exception_count = 0; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 21 | |
| 22 | /** |
Madhukar Pappireddy | f054a05 | 2022-12-22 11:37:11 -0600 | [diff] [blame] | 23 | * Tracks the virtual interrupt that was last handled by SP. |
| 24 | */ |
| 25 | static uint32_t last_serviced_interrupt = 0; |
| 26 | |
| 27 | /** |
Kathleen Capella | 4eba3f3 | 2022-12-09 18:05:51 -0500 | [diff] [blame^] | 28 | * Used to specify an instruction address to return to after exception |
| 29 | * is handled. |
| 30 | */ |
| 31 | static uint64_t exception_handler_return_addr = 0; |
| 32 | |
| 33 | /** |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 34 | * Sends the number of exceptions handled to the Primary VM. |
| 35 | */ |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 36 | void exception_handler_send_exception_count(void) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 37 | { |
J-Alves | 8304fb9 | 2022-06-24 17:14:08 +0100 | [diff] [blame] | 38 | struct ffa_partition_msg *exception_msg = |
| 39 | (struct ffa_partition_msg *)SERVICE_SEND_BUFFER(); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 40 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 41 | dlog("Sending exception_count %d to primary VM\n", |
| 42 | exception_handler_exception_count); |
J-Alves | 8304fb9 | 2022-06-24 17:14:08 +0100 | [diff] [blame] | 43 | |
| 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 Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 52 | (const void *)&exception_handler_exception_count, |
| 53 | sizeof(exception_handler_exception_count)); |
J-Alves | 8304fb9 | 2022-06-24 17:14:08 +0100 | [diff] [blame] | 54 | EXPECT_EQ(ffa_msg_send2(0).func, FFA_SUCCESS_32); |
| 55 | ffa_yield(); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Receives the number of exceptions handled. |
| 60 | */ |
J-Alves | 8304fb9 | 2022-06-24 17:14:08 +0100 | [diff] [blame] | 61 | int exception_handler_receive_exception_count(const void *recv_buf) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 62 | { |
J-Alves | 8304fb9 | 2022-06-24 17:14:08 +0100 | [diff] [blame] | 63 | 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 Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 68 | |
J-Alves | 8304fb9 | 2022-06-24 17:14:08 +0100 | [diff] [blame] | 69 | 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-Alves | 8304fb9 | 2022-06-24 17:14:08 +0100 | [diff] [blame] | 74 | |
J-Alves | 30e26d9 | 2022-09-26 12:04:09 +0100 | [diff] [blame] | 75 | if (fwk_notif == 0U || |
| 76 | exception_msg->header.size != sizeof(exception_count)) { |
| 77 | return 0; |
| 78 | } |
| 79 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 80 | EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 81 | return exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 82 | } |
| 83 | |
J-Alves | 30e26d9 | 2022-09-26 12:04:09 +0100 | [diff] [blame] | 84 | /* |
| 85 | * Returns true if the receiver has been preempted by an exception: |
| 86 | * - if the receiver is an EL1 partition, it should have sent the exception |
| 87 | * count in a message. |
| 88 | * - if the receiver is an EL0 partition, the Hyp/SPMC should return FFA_ERROR |
| 89 | * with error code FFA_ABORTED. |
| 90 | */ |
| 91 | bool exception_received(struct ffa_value *run_res, const void *recv_buf) |
| 92 | { |
| 93 | return exception_handler_receive_exception_count(recv_buf) == 1 || |
| 94 | (run_res->func == FFA_ERROR_32 && |
| 95 | ffa_error_code(*run_res) == FFA_ABORTED); |
| 96 | } |
| 97 | |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 98 | /** |
| 99 | * EL1 exception handler to use in unit test VMs. |
| 100 | * Skips the instruction that triggered the exception. |
| 101 | */ |
| 102 | bool exception_handler_skip_instruction(void) |
| 103 | { |
| 104 | dlog("%s function is triggered!\n", __func__); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 105 | ++exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 106 | |
| 107 | /* Skip instruction that triggered the exception. */ |
| 108 | uint64_t next_pc = read_msr(elr_el1); |
| 109 | next_pc += 4UL; |
| 110 | write_msr(elr_el1, next_pc); |
| 111 | |
| 112 | /* Indicate that elr_el1 should not be restored. */ |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | /** |
Kathleen Capella | 4eba3f3 | 2022-12-09 18:05:51 -0500 | [diff] [blame^] | 117 | * Warning: Intended to be used only in test code. |
| 118 | * The ability to jump to any address after an exception could |
| 119 | * possibily be exploited by malicious code. |
| 120 | * |
| 121 | * Sets the specified instruction address to return to after handler exits. |
| 122 | */ |
| 123 | void exception_handler_set_return_addr(uint64_t instr_addr) |
| 124 | { |
| 125 | exception_handler_return_addr = instr_addr; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns the specified instruction address to return to after handler exits. |
| 130 | */ |
| 131 | static uint64_t exception_handler_get_return_addr(void) |
| 132 | { |
| 133 | return exception_handler_return_addr; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * EL1 exception handler to use in unit test VMs. |
| 138 | * Skips to the instruction address specified in general |
| 139 | * register x19. |
| 140 | */ |
| 141 | bool exception_handler_skip_to_instruction(void) |
| 142 | { |
| 143 | dlog("%s function is triggered!\n", __func__); |
| 144 | ++exception_handler_exception_count; |
| 145 | |
| 146 | uint64_t instr_addr = exception_handler_get_return_addr(); |
| 147 | |
| 148 | if (instr_addr) { |
| 149 | write_msr(elr_el1, instr_addr); |
| 150 | /* Indicate that elr_el1 should not be restored. */ |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | dlog_error("%s: Return address not set, restoring elr_el1\n", __func__); |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | /** |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 159 | * EL1 exception handler to use in unit test VMs. |
| 160 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 161 | */ |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 162 | static bool exception_handler_yield(void) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 163 | { |
| 164 | dlog("%s function is triggered!\n", __func__); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 165 | ++exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 166 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 167 | exception_handler_send_exception_count(); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 168 | |
| 169 | /* Indicate that elr_el1 should not be restored. */ |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | /** |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 174 | * EL1 exception handler to use in unit test VMs. |
| 175 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 176 | * Asserts that the Exception Class is Unknown. |
| 177 | */ |
| 178 | bool exception_handler_yield_unknown(void) |
| 179 | { |
| 180 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 181 | uintreg_t far_el1 = read_msr(FAR_EL1); |
| 182 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 183 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_UNKNOWN); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 184 | |
| 185 | /* |
| 186 | * For unknown exceptions, the value of far_el1 is UNKNOWN. |
| 187 | * Hafnium sets it to 0. |
| 188 | */ |
| 189 | EXPECT_EQ(far_el1, 0); |
| 190 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 191 | return exception_handler_yield(); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * EL1 exception handler to use in unit test VMs. |
| 196 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 197 | * Asserts that the Exception Class is Data Abort (same EL). |
| 198 | */ |
| 199 | bool exception_handler_yield_data_abort(void) |
| 200 | { |
| 201 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 202 | uintreg_t far_el1 = read_msr(FAR_EL1); |
| 203 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 204 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_DATA_ABORT_SAME_EL); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 205 | EXPECT_NE(far_el1, 0); |
| 206 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 207 | return exception_handler_yield(); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * EL1 exception handler to use in unit test VMs. |
| 212 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 213 | * Asserts that the Exception Class is Instruction Abort (same EL). |
| 214 | */ |
| 215 | bool exception_handler_yield_instruction_abort(void) |
| 216 | { |
| 217 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 218 | uintreg_t far_el1 = read_msr(FAR_EL1); |
| 219 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 220 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_INSTRUCTION_ABORT_SAME_EL); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 221 | EXPECT_NE(far_el1, 0); |
| 222 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 223 | return exception_handler_yield(); |
| 224 | } |
| 225 | |
| 226 | /** |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 227 | * Returns the number of times the instruction handler was invoked. |
| 228 | */ |
| 229 | int exception_handler_get_num(void) |
| 230 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 231 | return exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Resets the number of exceptions counter; |
| 236 | */ |
| 237 | void exception_handler_reset(void) |
| 238 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 239 | exception_handler_exception_count = 0; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 240 | } |
Madhukar Pappireddy | f054a05 | 2022-12-22 11:37:11 -0600 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * Updates the last serviced virtual interrupt ID. |
| 244 | */ |
| 245 | void exception_handler_set_last_interrupt(uint32_t id) |
| 246 | { |
| 247 | last_serviced_interrupt = id; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Returns the last serviced virtual interrupt ID. |
| 252 | */ |
| 253 | uint32_t exception_handler_get_last_interrupt(void) |
| 254 | { |
| 255 | return last_serviced_interrupt; |
| 256 | } |