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); |
| 96 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_UNKNOWN); |
| 97 | return exception_handler_yield(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * EL1 exception handler to use in unit test VMs. |
| 102 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 103 | * Asserts that the Exception Class is Data Abort (same EL). |
| 104 | */ |
| 105 | bool exception_handler_yield_data_abort(void) |
| 106 | { |
| 107 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
| 108 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_DATA_ABORT_SAME_EL); |
| 109 | return exception_handler_yield(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * EL1 exception handler to use in unit test VMs. |
| 114 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 115 | * Asserts that the Exception Class is Instruction Abort (same EL). |
| 116 | */ |
| 117 | bool exception_handler_yield_instruction_abort(void) |
| 118 | { |
| 119 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
| 120 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_INSTRUCTION_ABORT_SAME_EL); |
| 121 | return exception_handler_yield(); |
| 122 | } |
| 123 | |
| 124 | /** |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 125 | * Returns the number of times the instruction handler was invoked. |
| 126 | */ |
| 127 | int exception_handler_get_num(void) |
| 128 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 129 | return exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Resets the number of exceptions counter; |
| 134 | */ |
| 135 | void exception_handler_reset(void) |
| 136 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 137 | exception_handler_exception_count = 0; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 138 | } |