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 | /** |
| 23 | * Sends the number of exceptions handled to the Primary VM. |
| 24 | */ |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 25 | void exception_handler_send_exception_count(void) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 26 | { |
J-Alves | 8304fb9 | 2022-06-24 17:14:08 +0100 | [diff] [blame] | 27 | struct ffa_partition_msg *exception_msg = |
| 28 | (struct ffa_partition_msg *)SERVICE_SEND_BUFFER(); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 29 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 30 | dlog("Sending exception_count %d to primary VM\n", |
| 31 | exception_handler_exception_count); |
J-Alves | 8304fb9 | 2022-06-24 17:14:08 +0100 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * TODO: remove use of HF_PRIMARY_VM_ID, replace with a mechanism that |
| 35 | * allows to detect the caller to a running test service. This may |
| 36 | * eventually become to be another endpoint, different from primary VM. |
| 37 | */ |
| 38 | ffa_rxtx_header_init(hf_vm_get_id(), HF_PRIMARY_VM_ID, sizeof(int), |
| 39 | &exception_msg->header); |
| 40 | memcpy_s(exception_msg->payload, FFA_MSG_PAYLOAD_MAX, |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 41 | (const void *)&exception_handler_exception_count, |
| 42 | sizeof(exception_handler_exception_count)); |
J-Alves | 8304fb9 | 2022-06-24 17:14:08 +0100 | [diff] [blame] | 43 | EXPECT_EQ(ffa_msg_send2(0).func, FFA_SUCCESS_32); |
| 44 | ffa_yield(); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Receives the number of exceptions handled. |
| 49 | */ |
J-Alves | 8304fb9 | 2022-06-24 17:14:08 +0100 | [diff] [blame] | 50 | int exception_handler_receive_exception_count(const void *recv_buf) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 51 | { |
J-Alves | 8304fb9 | 2022-06-24 17:14:08 +0100 | [diff] [blame] | 52 | struct ffa_partition_msg *exception_msg = |
| 53 | (struct ffa_partition_msg *)recv_buf; |
| 54 | int exception_count = *((const int *)exception_msg->payload); |
| 55 | struct ffa_value ret; |
| 56 | ffa_notifications_bitmap_t fwk_notif; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 57 | |
J-Alves | 8304fb9 | 2022-06-24 17:14:08 +0100 | [diff] [blame] | 58 | ret = ffa_notification_get(hf_vm_get_id(), 0, |
| 59 | FFA_NOTIFICATION_FLAG_BITMAP_HYP | |
| 60 | FFA_NOTIFICATION_FLAG_BITMAP_SPM); |
| 61 | |
| 62 | fwk_notif = ffa_notification_get_from_framework(ret); |
J-Alves | 8304fb9 | 2022-06-24 17:14:08 +0100 | [diff] [blame] | 63 | |
J-Alves | 30e26d9 | 2022-09-26 12:04:09 +0100 | [diff] [blame^] | 64 | if (fwk_notif == 0U || |
| 65 | exception_msg->header.size != sizeof(exception_count)) { |
| 66 | return 0; |
| 67 | } |
| 68 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 69 | EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 70 | return exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 71 | } |
| 72 | |
J-Alves | 30e26d9 | 2022-09-26 12:04:09 +0100 | [diff] [blame^] | 73 | /* |
| 74 | * Returns true if the receiver has been preempted by an exception: |
| 75 | * - if the receiver is an EL1 partition, it should have sent the exception |
| 76 | * count in a message. |
| 77 | * - if the receiver is an EL0 partition, the Hyp/SPMC should return FFA_ERROR |
| 78 | * with error code FFA_ABORTED. |
| 79 | */ |
| 80 | bool exception_received(struct ffa_value *run_res, const void *recv_buf) |
| 81 | { |
| 82 | return exception_handler_receive_exception_count(recv_buf) == 1 || |
| 83 | (run_res->func == FFA_ERROR_32 && |
| 84 | ffa_error_code(*run_res) == FFA_ABORTED); |
| 85 | } |
| 86 | |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 87 | /** |
| 88 | * EL1 exception handler to use in unit test VMs. |
| 89 | * Skips the instruction that triggered the exception. |
| 90 | */ |
| 91 | bool exception_handler_skip_instruction(void) |
| 92 | { |
| 93 | dlog("%s function is triggered!\n", __func__); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 94 | ++exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 95 | |
| 96 | /* Skip instruction that triggered the exception. */ |
| 97 | uint64_t next_pc = read_msr(elr_el1); |
| 98 | next_pc += 4UL; |
| 99 | write_msr(elr_el1, next_pc); |
| 100 | |
| 101 | /* Indicate that elr_el1 should not be restored. */ |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * EL1 exception handler to use in unit test VMs. |
| 107 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 108 | */ |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 109 | static bool exception_handler_yield(void) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 110 | { |
| 111 | dlog("%s function is triggered!\n", __func__); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 112 | ++exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 113 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 114 | exception_handler_send_exception_count(); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 115 | |
| 116 | /* Indicate that elr_el1 should not be restored. */ |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | /** |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 121 | * EL1 exception handler to use in unit test VMs. |
| 122 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 123 | * Asserts that the Exception Class is Unknown. |
| 124 | */ |
| 125 | bool exception_handler_yield_unknown(void) |
| 126 | { |
| 127 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 128 | uintreg_t far_el1 = read_msr(FAR_EL1); |
| 129 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 130 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_UNKNOWN); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * For unknown exceptions, the value of far_el1 is UNKNOWN. |
| 134 | * Hafnium sets it to 0. |
| 135 | */ |
| 136 | EXPECT_EQ(far_el1, 0); |
| 137 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 138 | return exception_handler_yield(); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * EL1 exception handler to use in unit test VMs. |
| 143 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 144 | * Asserts that the Exception Class is Data Abort (same EL). |
| 145 | */ |
| 146 | bool exception_handler_yield_data_abort(void) |
| 147 | { |
| 148 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 149 | uintreg_t far_el1 = read_msr(FAR_EL1); |
| 150 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 151 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_DATA_ABORT_SAME_EL); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 152 | EXPECT_NE(far_el1, 0); |
| 153 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 154 | return exception_handler_yield(); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * EL1 exception handler to use in unit test VMs. |
| 159 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 160 | * Asserts that the Exception Class is Instruction Abort (same EL). |
| 161 | */ |
| 162 | bool exception_handler_yield_instruction_abort(void) |
| 163 | { |
| 164 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 165 | uintreg_t far_el1 = read_msr(FAR_EL1); |
| 166 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 167 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_INSTRUCTION_ABORT_SAME_EL); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 168 | EXPECT_NE(far_el1, 0); |
| 169 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 170 | return exception_handler_yield(); |
| 171 | } |
| 172 | |
| 173 | /** |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 174 | * Returns the number of times the instruction handler was invoked. |
| 175 | */ |
| 176 | int exception_handler_get_num(void) |
| 177 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 178 | return exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Resets the number of exceptions counter; |
| 183 | */ |
| 184 | void exception_handler_reset(void) |
| 185 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 186 | exception_handler_exception_count = 0; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 187 | } |