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 | { |
| 27 | void *send_buf = SERVICE_SEND_BUFFER(); |
| 28 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 29 | dlog("Sending exception_count %d to primary VM\n", |
| 30 | exception_handler_exception_count); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 31 | memcpy_s(send_buf, FFA_MSG_PAYLOAD_MAX, |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 32 | (const void *)&exception_handler_exception_count, |
| 33 | sizeof(exception_handler_exception_count)); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 34 | EXPECT_EQ(ffa_msg_send(hf_vm_get_id(), HF_PRIMARY_VM_ID, |
| 35 | sizeof(exception_handler_exception_count), 0) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 36 | .func, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 37 | FFA_SUCCESS_32); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Receives the number of exceptions handled. |
| 42 | */ |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 43 | int exception_handler_receive_exception_count( |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 44 | const struct ffa_value *send_res, |
| 45 | const struct ffa_memory_region *recv_buf) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 46 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 47 | int exception_count = *((const int *)recv_buf); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 48 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 49 | EXPECT_EQ(send_res->func, FFA_MSG_SEND_32); |
| 50 | EXPECT_EQ(ffa_msg_send_size(*send_res), sizeof(exception_count)); |
| 51 | EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 52 | return exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
| 56 | * EL1 exception handler to use in unit test VMs. |
| 57 | * Skips the instruction that triggered the exception. |
| 58 | */ |
| 59 | bool exception_handler_skip_instruction(void) |
| 60 | { |
| 61 | dlog("%s function is triggered!\n", __func__); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 62 | ++exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 63 | |
| 64 | /* Skip instruction that triggered the exception. */ |
| 65 | uint64_t next_pc = read_msr(elr_el1); |
| 66 | next_pc += 4UL; |
| 67 | write_msr(elr_el1, next_pc); |
| 68 | |
| 69 | /* Indicate that elr_el1 should not be restored. */ |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * EL1 exception handler to use in unit test VMs. |
| 75 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 76 | */ |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 77 | static bool exception_handler_yield(void) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 78 | { |
| 79 | dlog("%s function is triggered!\n", __func__); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 80 | ++exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 81 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 82 | exception_handler_send_exception_count(); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 83 | |
| 84 | /* Indicate that elr_el1 should not be restored. */ |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | /** |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 89 | * EL1 exception handler to use in unit test VMs. |
| 90 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 91 | * Asserts that the Exception Class is Unknown. |
| 92 | */ |
| 93 | bool exception_handler_yield_unknown(void) |
| 94 | { |
| 95 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame^] | 96 | uintreg_t far_el1 = read_msr(FAR_EL1); |
| 97 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 98 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_UNKNOWN); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame^] | 99 | |
| 100 | /* |
| 101 | * For unknown exceptions, the value of far_el1 is UNKNOWN. |
| 102 | * Hafnium sets it to 0. |
| 103 | */ |
| 104 | EXPECT_EQ(far_el1, 0); |
| 105 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 106 | return exception_handler_yield(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * EL1 exception handler to use in unit test VMs. |
| 111 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 112 | * Asserts that the Exception Class is Data Abort (same EL). |
| 113 | */ |
| 114 | bool exception_handler_yield_data_abort(void) |
| 115 | { |
| 116 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame^] | 117 | uintreg_t far_el1 = read_msr(FAR_EL1); |
| 118 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 119 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_DATA_ABORT_SAME_EL); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame^] | 120 | EXPECT_NE(far_el1, 0); |
| 121 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 122 | return exception_handler_yield(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * EL1 exception handler to use in unit test VMs. |
| 127 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 128 | * Asserts that the Exception Class is Instruction Abort (same EL). |
| 129 | */ |
| 130 | bool exception_handler_yield_instruction_abort(void) |
| 131 | { |
| 132 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame^] | 133 | uintreg_t far_el1 = read_msr(FAR_EL1); |
| 134 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 135 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_INSTRUCTION_ABORT_SAME_EL); |
Fuad Tabba | c3847c7 | 2020-08-11 09:32:25 +0100 | [diff] [blame^] | 136 | EXPECT_NE(far_el1, 0); |
| 137 | |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 138 | return exception_handler_yield(); |
| 139 | } |
| 140 | |
| 141 | /** |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 142 | * Returns the number of times the instruction handler was invoked. |
| 143 | */ |
| 144 | int exception_handler_get_num(void) |
| 145 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 146 | return exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Resets the number of exceptions counter; |
| 151 | */ |
| 152 | void exception_handler_reset(void) |
| 153 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 154 | exception_handler_exception_count = 0; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 155 | } |