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 | /* Assert we received a message with the exception count. */ |
| 59 | ret = ffa_notification_get(hf_vm_get_id(), 0, |
| 60 | FFA_NOTIFICATION_FLAG_BITMAP_HYP | |
| 61 | FFA_NOTIFICATION_FLAG_BITMAP_SPM); |
| 62 | |
| 63 | fwk_notif = ffa_notification_get_from_framework(ret); |
| 64 | ASSERT_TRUE(is_ffa_hyp_buffer_full_notification(fwk_notif) || |
| 65 | is_ffa_spm_buffer_full_notification(fwk_notif)); |
| 66 | |
| 67 | EXPECT_EQ(exception_msg->header.size, sizeof(exception_count)); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 68 | EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 69 | return exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
| 73 | * EL1 exception handler to use in unit test VMs. |
| 74 | * Skips the instruction that triggered the exception. |
| 75 | */ |
| 76 | bool exception_handler_skip_instruction(void) |
| 77 | { |
| 78 | dlog("%s function is triggered!\n", __func__); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 79 | ++exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 80 | |
| 81 | /* Skip instruction that triggered the exception. */ |
| 82 | uint64_t next_pc = read_msr(elr_el1); |
| 83 | next_pc += 4UL; |
| 84 | write_msr(elr_el1, next_pc); |
| 85 | |
| 86 | /* Indicate that elr_el1 should not be restored. */ |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * EL1 exception handler to use in unit test VMs. |
| 92 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 93 | */ |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 94 | static bool exception_handler_yield(void) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 95 | { |
| 96 | dlog("%s function is triggered!\n", __func__); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 97 | ++exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 98 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 99 | exception_handler_send_exception_count(); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 100 | |
| 101 | /* Indicate that elr_el1 should not be restored. */ |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | /** |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 106 | * EL1 exception handler to use in unit test VMs. |
| 107 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 108 | * Asserts that the Exception Class is Unknown. |
| 109 | */ |
| 110 | bool exception_handler_yield_unknown(void) |
| 111 | { |
| 112 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 113 | uintreg_t far_el1 = read_msr(FAR_EL1); |
| 114 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 115 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_UNKNOWN); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | * For unknown exceptions, the value of far_el1 is UNKNOWN. |
| 119 | * Hafnium sets it to 0. |
| 120 | */ |
| 121 | EXPECT_EQ(far_el1, 0); |
| 122 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 123 | return exception_handler_yield(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * EL1 exception handler to use in unit test VMs. |
| 128 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 129 | * Asserts that the Exception Class is Data Abort (same EL). |
| 130 | */ |
| 131 | bool exception_handler_yield_data_abort(void) |
| 132 | { |
| 133 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 134 | uintreg_t far_el1 = read_msr(FAR_EL1); |
| 135 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 136 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_DATA_ABORT_SAME_EL); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 137 | EXPECT_NE(far_el1, 0); |
| 138 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 139 | return exception_handler_yield(); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * EL1 exception handler to use in unit test VMs. |
| 144 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 145 | * Asserts that the Exception Class is Instruction Abort (same EL). |
| 146 | */ |
| 147 | bool exception_handler_yield_instruction_abort(void) |
| 148 | { |
| 149 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 150 | uintreg_t far_el1 = read_msr(FAR_EL1); |
| 151 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 152 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_INSTRUCTION_ABORT_SAME_EL); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame] | 153 | EXPECT_NE(far_el1, 0); |
| 154 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 155 | return exception_handler_yield(); |
| 156 | } |
| 157 | |
| 158 | /** |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 159 | * Returns the number of times the instruction handler was invoked. |
| 160 | */ |
| 161 | int exception_handler_get_num(void) |
| 162 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 163 | return exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Resets the number of exceptions counter; |
| 168 | */ |
| 169 | void exception_handler_reset(void) |
| 170 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 171 | exception_handler_exception_count = 0; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 172 | } |