blob: 5c6f87b0f3f56ce4485cc3176bd333c67e734193 [file] [log] [blame]
Fuad Tabbaa48d1222019-12-09 15:42:32 +00001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * 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 Tabbaa48d1222019-12-09 15:42:32 +00007 */
8
9#include "hf/dlog.h"
10
11#include "vmapi/hf/call.h"
12
13#include "../msr.h"
Fuad Tabbab86325a2020-01-10 13:38:15 +000014#include "sysregs.h"
Fuad Tabbaa48d1222019-12-09 15:42:32 +000015#include "test/hftest.h"
16
17/**
18 * Tracks the number of times the exception handler has been invoked.
19 */
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000020static int exception_handler_exception_count = 0;
Fuad Tabbaa48d1222019-12-09 15:42:32 +000021
22/**
23 * Sends the number of exceptions handled to the Primary VM.
24 */
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000025void exception_handler_send_exception_count(void)
Fuad Tabbaa48d1222019-12-09 15:42:32 +000026{
J-Alves8304fb92022-06-24 17:14:08 +010027 struct ffa_partition_msg *exception_msg =
28 (struct ffa_partition_msg *)SERVICE_SEND_BUFFER();
Fuad Tabbaa48d1222019-12-09 15:42:32 +000029
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000030 dlog("Sending exception_count %d to primary VM\n",
31 exception_handler_exception_count);
J-Alves8304fb92022-06-24 17:14:08 +010032
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 Tabbab0ef2a42019-12-19 11:19:25 +000041 (const void *)&exception_handler_exception_count,
42 sizeof(exception_handler_exception_count));
J-Alves8304fb92022-06-24 17:14:08 +010043 EXPECT_EQ(ffa_msg_send2(0).func, FFA_SUCCESS_32);
44 ffa_yield();
Fuad Tabbaa48d1222019-12-09 15:42:32 +000045}
46
47/**
48 * Receives the number of exceptions handled.
49 */
J-Alves8304fb92022-06-24 17:14:08 +010050int exception_handler_receive_exception_count(const void *recv_buf)
Fuad Tabbaa48d1222019-12-09 15:42:32 +000051{
J-Alves8304fb92022-06-24 17:14:08 +010052 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 Tabbaa48d1222019-12-09 15:42:32 +000057
J-Alves8304fb92022-06-24 17:14:08 +010058 /* 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 Walbranb5ab43c2020-04-30 11:32:54 +010068 EXPECT_EQ(ffa_rx_release().func, FFA_SUCCESS_32);
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000069 return exception_count;
Fuad Tabbaa48d1222019-12-09 15:42:32 +000070}
71
72/**
73 * EL1 exception handler to use in unit test VMs.
74 * Skips the instruction that triggered the exception.
75 */
76bool exception_handler_skip_instruction(void)
77{
78 dlog("%s function is triggered!\n", __func__);
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000079 ++exception_handler_exception_count;
Fuad Tabbaa48d1222019-12-09 15:42:32 +000080
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 Tabbab86325a2020-01-10 13:38:15 +000094static bool exception_handler_yield(void)
Fuad Tabbaa48d1222019-12-09 15:42:32 +000095{
96 dlog("%s function is triggered!\n", __func__);
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000097 ++exception_handler_exception_count;
Fuad Tabbaa48d1222019-12-09 15:42:32 +000098
Fuad Tabbab0ef2a42019-12-19 11:19:25 +000099 exception_handler_send_exception_count();
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000100
101 /* Indicate that elr_el1 should not be restored. */
102 return true;
103}
104
105/**
Fuad Tabbab86325a2020-01-10 13:38:15 +0000106 * 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 */
110bool exception_handler_yield_unknown(void)
111{
112 uintreg_t esr_el1 = read_msr(ESR_EL1);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100113 uintreg_t far_el1 = read_msr(FAR_EL1);
114
Fuad Tabbab86325a2020-01-10 13:38:15 +0000115 EXPECT_EQ(GET_ESR_EC(esr_el1), EC_UNKNOWN);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100116
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 Tabbab86325a2020-01-10 13:38:15 +0000123 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 */
131bool exception_handler_yield_data_abort(void)
132{
133 uintreg_t esr_el1 = read_msr(ESR_EL1);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100134 uintreg_t far_el1 = read_msr(FAR_EL1);
135
Fuad Tabbab86325a2020-01-10 13:38:15 +0000136 EXPECT_EQ(GET_ESR_EC(esr_el1), EC_DATA_ABORT_SAME_EL);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100137 EXPECT_NE(far_el1, 0);
138
Fuad Tabbab86325a2020-01-10 13:38:15 +0000139 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 */
147bool exception_handler_yield_instruction_abort(void)
148{
149 uintreg_t esr_el1 = read_msr(ESR_EL1);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100150 uintreg_t far_el1 = read_msr(FAR_EL1);
151
Fuad Tabbab86325a2020-01-10 13:38:15 +0000152 EXPECT_EQ(GET_ESR_EC(esr_el1), EC_INSTRUCTION_ABORT_SAME_EL);
Fuad Tabbac3847c72020-08-11 09:32:25 +0100153 EXPECT_NE(far_el1, 0);
154
Fuad Tabbab86325a2020-01-10 13:38:15 +0000155 return exception_handler_yield();
156}
157
158/**
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000159 * Returns the number of times the instruction handler was invoked.
160 */
161int exception_handler_get_num(void)
162{
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000163 return exception_handler_exception_count;
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000164}
165
166/**
167 * Resets the number of exceptions counter;
168 */
169void exception_handler_reset(void)
170{
Fuad Tabbab0ef2a42019-12-19 11:19:25 +0000171 exception_handler_exception_count = 0;
Fuad Tabbaa48d1222019-12-09 15:42:32 +0000172}