Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "hf/dlog.h" |
| 18 | |
| 19 | #include "vmapi/hf/call.h" |
| 20 | |
| 21 | #include "../msr.h" |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 22 | #include "sysregs.h" |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 23 | #include "test/hftest.h" |
| 24 | |
| 25 | /** |
| 26 | * Tracks the number of times the exception handler has been invoked. |
| 27 | */ |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 28 | static int exception_handler_exception_count = 0; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Sends the number of exceptions handled to the Primary VM. |
| 32 | */ |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 33 | void exception_handler_send_exception_count(void) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 34 | { |
| 35 | void *send_buf = SERVICE_SEND_BUFFER(); |
| 36 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 37 | dlog("Sending exception_count %d to primary VM\n", |
| 38 | exception_handler_exception_count); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 39 | memcpy_s(send_buf, FFA_MSG_PAYLOAD_MAX, |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 40 | (const void *)&exception_handler_exception_count, |
| 41 | sizeof(exception_handler_exception_count)); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 42 | EXPECT_EQ(ffa_msg_send(hf_vm_get_id(), HF_PRIMARY_VM_ID, |
| 43 | sizeof(exception_handler_exception_count), 0) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 44 | .func, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 45 | FFA_SUCCESS_32); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Receives the number of exceptions handled. |
| 50 | */ |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 51 | int exception_handler_receive_exception_count( |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 52 | const struct ffa_value *send_res, |
| 53 | const struct ffa_memory_region *recv_buf) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 54 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 55 | int exception_count = *((const int *)recv_buf); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 56 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame^] | 57 | EXPECT_EQ(send_res->func, FFA_MSG_SEND_32); |
| 58 | EXPECT_EQ(ffa_msg_send_size(*send_res), sizeof(exception_count)); |
| 59 | EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 60 | return exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * EL1 exception handler to use in unit test VMs. |
| 65 | * Skips the instruction that triggered the exception. |
| 66 | */ |
| 67 | bool exception_handler_skip_instruction(void) |
| 68 | { |
| 69 | dlog("%s function is triggered!\n", __func__); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 70 | ++exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 71 | |
| 72 | /* Skip instruction that triggered the exception. */ |
| 73 | uint64_t next_pc = read_msr(elr_el1); |
| 74 | next_pc += 4UL; |
| 75 | write_msr(elr_el1, next_pc); |
| 76 | |
| 77 | /* Indicate that elr_el1 should not be restored. */ |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * EL1 exception handler to use in unit test VMs. |
| 83 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 84 | */ |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 85 | static bool exception_handler_yield(void) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 86 | { |
| 87 | dlog("%s function is triggered!\n", __func__); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 88 | ++exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 89 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 90 | exception_handler_send_exception_count(); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 91 | |
| 92 | /* Indicate that elr_el1 should not be restored. */ |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | /** |
Fuad Tabba | b86325a | 2020-01-10 13:38:15 +0000 | [diff] [blame] | 97 | * EL1 exception handler to use in unit test VMs. |
| 98 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 99 | * Asserts that the Exception Class is Unknown. |
| 100 | */ |
| 101 | bool exception_handler_yield_unknown(void) |
| 102 | { |
| 103 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
| 104 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_UNKNOWN); |
| 105 | return exception_handler_yield(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * EL1 exception handler to use in unit test VMs. |
| 110 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 111 | * Asserts that the Exception Class is Data Abort (same EL). |
| 112 | */ |
| 113 | bool exception_handler_yield_data_abort(void) |
| 114 | { |
| 115 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
| 116 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_DATA_ABORT_SAME_EL); |
| 117 | return exception_handler_yield(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 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 Instruction Abort (same EL). |
| 124 | */ |
| 125 | bool exception_handler_yield_instruction_abort(void) |
| 126 | { |
| 127 | uintreg_t esr_el1 = read_msr(ESR_EL1); |
| 128 | EXPECT_EQ(GET_ESR_EC(esr_el1), EC_INSTRUCTION_ABORT_SAME_EL); |
| 129 | return exception_handler_yield(); |
| 130 | } |
| 131 | |
| 132 | /** |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 133 | * Returns the number of times the instruction handler was invoked. |
| 134 | */ |
| 135 | int exception_handler_get_num(void) |
| 136 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 137 | return exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Resets the number of exceptions counter; |
| 142 | */ |
| 143 | void exception_handler_reset(void) |
| 144 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 145 | exception_handler_exception_count = 0; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 146 | } |