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 | |
| 84 | /** |
| 85 | * EL1 exception handler to use in unit test VMs. |
| 86 | * Skips the instruction that triggered the exception. |
| 87 | */ |
| 88 | bool exception_handler_skip_instruction(void) |
| 89 | { |
| 90 | dlog("%s function is triggered!\n", __func__); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 91 | ++exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 92 | |
| 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 Capella | 4eba3f3 | 2022-12-09 18:05:51 -0500 | [diff] [blame] | 103 | * 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 | */ |
| 109 | void 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 | */ |
| 117 | static 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 | */ |
| 127 | bool 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 Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 145 | * EL1 exception handler to use in unit test VMs. |
| 146 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 147 | */ |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 148 | static bool exception_handler_yield(void) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 149 | { |
| 150 | dlog("%s function is triggered!\n", __func__); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 151 | ++exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 152 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 153 | exception_handler_send_exception_count(); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 154 | |
| 155 | /* Indicate that elr_el1 should not be restored. */ |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | /** |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 160 | * 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 | */ |
| 164 | bool exception_handler_yield_unknown(void) |
| 165 | { |
| 166 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 167 | uintreg_t far_el1 = read_msr(FAR_EL1); |
| 168 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 169 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_UNKNOWN); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 170 | |
| 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 Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 177 | 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 | */ |
| 185 | bool exception_handler_yield_data_abort(void) |
| 186 | { |
| 187 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 188 | uintreg_t far_el1 = read_msr(FAR_EL1); |
| 189 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 190 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_DATA_ABORT_SAME_EL); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 191 | EXPECT_NE(far_el1, 0); |
| 192 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 193 | 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 | */ |
| 201 | bool exception_handler_yield_instruction_abort(void) |
| 202 | { |
| 203 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 204 | uintreg_t far_el1 = read_msr(FAR_EL1); |
| 205 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 206 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_INSTRUCTION_ABORT_SAME_EL); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 207 | EXPECT_NE(far_el1, 0); |
| 208 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 209 | return exception_handler_yield(); |
| 210 | } |
| 211 | |
| 212 | /** |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 213 | * Returns the number of times the instruction handler was invoked. |
| 214 | */ |
| 215 | int exception_handler_get_num(void) |
| 216 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 217 | return exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Resets the number of exceptions counter; |
| 222 | */ |
| 223 | void exception_handler_reset(void) |
| 224 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 225 | exception_handler_exception_count = 0; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 226 | } |
Madhukar Pappireddy | f054a05 | 2022-12-22 11:37:11 -0600 | [diff] [blame] | 227 | |
| 228 | /** |
| 229 | * Updates the last serviced virtual interrupt ID. |
| 230 | */ |
| 231 | void 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 | */ |
| 239 | uint32_t exception_handler_get_last_interrupt(void) |
| 240 | { |
| 241 | return last_serviced_interrupt; |
| 242 | } |