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" |
| 22 | #include "test/hftest.h" |
| 23 | |
| 24 | /** |
| 25 | * Tracks the number of times the exception handler has been invoked. |
| 26 | */ |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 27 | static int exception_handler_exception_count = 0; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Sends the number of exceptions handled to the Primary VM. |
| 31 | */ |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 32 | void exception_handler_send_exception_count(void) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 33 | { |
| 34 | void *send_buf = SERVICE_SEND_BUFFER(); |
| 35 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 36 | dlog("Sending exception_count %d to primary VM\n", |
| 37 | exception_handler_exception_count); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 38 | memcpy_s(send_buf, SPCI_MSG_PAYLOAD_MAX, |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 39 | (const void *)&exception_handler_exception_count, |
| 40 | sizeof(exception_handler_exception_count)); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 41 | EXPECT_EQ(spci_msg_send(hf_vm_get_id(), HF_PRIMARY_VM_ID, |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 42 | sizeof(exception_handler_exception_count), 0) |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 43 | .func, |
| 44 | SPCI_SUCCESS_32); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Receives the number of exceptions handled. |
| 49 | */ |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 50 | int exception_handler_receive_exception_count( |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 51 | const struct spci_value *send_res, |
| 52 | const struct spci_memory_region *recv_buf) |
| 53 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 54 | int exception_count = *((const int *)recv_buf); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 55 | |
| 56 | EXPECT_EQ(send_res->func, SPCI_MSG_SEND_32); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 57 | EXPECT_EQ(spci_msg_send_size(*send_res), sizeof(exception_count)); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 58 | EXPECT_EQ(spci_rx_release().func, SPCI_SUCCESS_32); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 59 | return exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
| 63 | * EL1 exception handler to use in unit test VMs. |
| 64 | * Skips the instruction that triggered the exception. |
| 65 | */ |
| 66 | bool exception_handler_skip_instruction(void) |
| 67 | { |
| 68 | dlog("%s function is triggered!\n", __func__); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 69 | ++exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 70 | |
| 71 | /* Skip instruction that triggered the exception. */ |
| 72 | uint64_t next_pc = read_msr(elr_el1); |
| 73 | next_pc += 4UL; |
| 74 | write_msr(elr_el1, next_pc); |
| 75 | |
| 76 | /* Indicate that elr_el1 should not be restored. */ |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * EL1 exception handler to use in unit test VMs. |
| 82 | * Yields control back to the hypervisor and sends the number of exceptions. |
| 83 | */ |
| 84 | bool exception_handler_yield(void) |
| 85 | { |
| 86 | dlog("%s function is triggered!\n", __func__); |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 87 | ++exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 88 | |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 89 | exception_handler_send_exception_count(); |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 90 | |
| 91 | /* Indicate that elr_el1 should not be restored. */ |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the number of times the instruction handler was invoked. |
| 97 | */ |
| 98 | int exception_handler_get_num(void) |
| 99 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 100 | return exception_handler_exception_count; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Resets the number of exceptions counter; |
| 105 | */ |
| 106 | void exception_handler_reset(void) |
| 107 | { |
Fuad Tabba | b0ef2a4 | 2019-12-19 11:19:25 +0000 | [diff] [blame] | 108 | exception_handler_exception_count = 0; |
Fuad Tabba | a48d122 | 2019-12-09 15:42:32 +0000 | [diff] [blame] | 109 | } |