blob: 7cf76d70d0e15fb6bd8a7711113f250d93ce74dc [file] [log] [blame]
Daniel Boulbyea296e82025-01-31 10:08:16 +00001/*
2 * Copyright 2025 The Hafnium Authors.
3 *
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.
7 */
8
9#include <gmock/gmock.h>
10
11extern "C" {
12#include "hf/check.h"
13#include "hf/vcpu.h"
14#include "hf/vm.h"
15}
16
17#include <map>
18
19#include "mm_test.hh"
20
21namespace
22{
23using namespace ::std::placeholders;
24using ::testing::AllOf;
25using ::testing::Each;
26using ::testing::SizeIs;
27using struct_vm = struct vm;
28using struct_vcpu = struct vcpu;
29using struct_vm_locked = struct vm_locked;
30
31constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 64;
32const int TOP_LEVEL = arch_mm_stage2_max_level();
33class vcpu : public ::testing::Test
34{
35 protected:
36 static std::unique_ptr<uint8_t[]> test_heap;
37 struct mpool ppool;
38 const uint32_t first_intid = HF_NUM_INTIDS - 2;
39 const uint32_t second_intid = HF_NUM_INTIDS - 1;
40 struct_vm *test_vm;
41 struct_vcpu *test_vcpu;
Daniel Boulbyd633a612025-03-07 18:08:04 +000042 struct vcpu_locked vcpu_locked;
Daniel Boulbyea296e82025-01-31 10:08:16 +000043 struct interrupts *interrupts;
44
45 void SetUp() override
46 {
Daniel Boulbyd633a612025-03-07 18:08:04 +000047 if (!test_heap) {
48 test_heap = std::make_unique<uint8_t[]>(TEST_HEAP_SIZE);
49 mpool_init(&ppool, sizeof(struct mm_page_table));
50 mpool_add_chunk(&ppool, test_heap.get(),
51 TEST_HEAP_SIZE);
Daniel Boulbyea296e82025-01-31 10:08:16 +000052 }
Daniel Boulbyd633a612025-03-07 18:08:04 +000053
J-Alves7867e7d2025-03-04 14:25:21 +000054 test_vm = vm_init(HF_VM_ID_OFFSET, 8, &ppool, false, 0);
Daniel Boulbyea296e82025-01-31 10:08:16 +000055 test_vcpu = vm_get_vcpu(test_vm, 0);
Daniel Boulbyd633a612025-03-07 18:08:04 +000056 vcpu_locked = vcpu_lock(test_vcpu);
Daniel Boulbyea296e82025-01-31 10:08:16 +000057 interrupts = &test_vcpu->interrupts;
58
59 /* Enable the interrupts used in testing. */
Daniel Boulbyd633a612025-03-07 18:08:04 +000060 vcpu_virt_interrupt_enable(vcpu_locked, first_intid, true);
61 vcpu_virt_interrupt_enable(vcpu_locked, second_intid, true);
62 }
63
64 void TearDown() override
65 {
66 vcpu_unlock(&vcpu_locked);
Daniel Boulbyea296e82025-01-31 10:08:16 +000067 }
68};
69
70std::unique_ptr<uint8_t[]> vcpu::test_heap;
71
72/**
73 * Check that interrupts that are set pending, can later be fetched
74 * from the queue.
75 */
76TEST_F(vcpu, pending_interrupts_are_fetched)
77{
Daniel Boulbyea296e82025-01-31 10:08:16 +000078 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 0);
79
80 /* Pend the interrupts, and check the count is incremented. */
81 vcpu_virt_interrupt_inject(vcpu_locked, first_intid);
82 vcpu_virt_interrupt_inject(vcpu_locked, second_intid);
83 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 2);
84
85 /*
86 * Check the pended interrupts are correctly returned, and once both
87 * have been returned the invalid intid is given to show there are no
88 * more pending interrupts.
89 */
90 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
91 first_intid);
92 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
93 second_intid);
94 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
95 HF_INVALID_INTID);
96
97 /*
98 * Check, having been fetched, the interrupts are no longer marked as
99 * pending in the bitmap, and the interrupt count is 0.
100 */
101 EXPECT_FALSE(vcpu_is_virt_interrupt_pending(interrupts, first_intid));
102 EXPECT_FALSE(vcpu_is_virt_interrupt_pending(interrupts, second_intid));
103 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 0);
104
105 /*
106 * Check that this expected behavour happens on a consecutive run.
107 * Invert the order of the interrupts to add some variation.
108 */
109 vcpu_virt_interrupt_inject(vcpu_locked, second_intid);
110 vcpu_virt_interrupt_inject(vcpu_locked, first_intid);
111
112 EXPECT_TRUE(vcpu_is_virt_interrupt_pending(interrupts, second_intid));
113 EXPECT_TRUE(vcpu_is_virt_interrupt_pending(interrupts, first_intid));
114 EXPECT_EQ(vcpu_virt_interrupt_irq_count_get(vcpu_locked), 2);
115
116 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
117 second_intid);
118 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
119 first_intid);
120 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
121 HF_INVALID_INTID);
122
123 EXPECT_FALSE(vcpu_is_virt_interrupt_pending(interrupts, second_intid));
124 EXPECT_FALSE(vcpu_is_virt_interrupt_pending(interrupts, first_intid));
125 EXPECT_EQ(vcpu_virt_interrupt_irq_count_get(vcpu_locked), 0);
Daniel Boulbyea296e82025-01-31 10:08:16 +0000126}
127
128/*
129 * Check that a disabled interrupt will not be returned until it is
130 * enabled.
131 */
132TEST_F(vcpu, pending_interrupts_not_enabled_are_not_returned)
133{
Daniel Boulbyea296e82025-01-31 10:08:16 +0000134 /*
135 * Pend the interrupts, check the count is incremented, the pending
136 * interrupts are returned correctly and this causes the count to
137 * return to 0.
138 */
139 vcpu_virt_interrupt_inject(vcpu_locked, first_intid);
140 vcpu_virt_interrupt_inject(vcpu_locked, second_intid);
141 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 2);
142 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
143 first_intid);
144 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
145 second_intid);
146 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 0);
147
148 /* Again pend the interrupts. */
149 vcpu_virt_interrupt_inject(vcpu_locked, first_intid);
150 vcpu_virt_interrupt_inject(vcpu_locked, second_intid);
151 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 2);
152
153 /* Disable the first interrupt. */
154 vcpu_virt_interrupt_enable(vcpu_locked, first_intid, false);
155 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 1);
156
157 /*
158 * Check that the disabled first interrupt is not returned,
159 * the second intid should be returned and then the invalid
160 * intid to show there are no more pending and enabled interrupts.
161 */
162 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
163 second_intid);
164 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
165 HF_INVALID_INTID);
166 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 0);
167
168 /* Reenable the first interrupt and disable the second interrupt.*/
169 vcpu_virt_interrupt_enable(vcpu_locked, first_intid, true);
170 vcpu_virt_interrupt_enable(vcpu_locked, second_intid, false);
171 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 1);
172
173 /*
174 * Check that an interrupt injected when the interrupt is disabled will
175 * eventually be returned once the interrupt is enabled.
176 */
177 vcpu_virt_interrupt_inject(vcpu_locked, second_intid);
178 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 1);
179
180 /*
181 * Check that it is now returned as a pending interrupt and is the only
182 * interrupt pending.
183 */
184 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
185 first_intid);
186 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
187 HF_INVALID_INTID);
188 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 0);
189
190 /* Enable the second interrupt to check it will now be returned. */
191 vcpu_virt_interrupt_enable(vcpu_locked, second_intid, true);
192 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 1);
193
194 /*
195 * Check that it is now returned as a pending interrupt and is the only
196 * interrupt pending.
197 */
198 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
199 second_intid);
200 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
201 HF_INVALID_INTID);
202 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 0);
Daniel Boulbyea296e82025-01-31 10:08:16 +0000203}
J-Alves7867e7d2025-03-04 14:25:21 +0000204
205/**
206 * Check the queue state from disabling some interrupts. And then reenabling.
207 */
208TEST_F(vcpu, injecting_getting_interrupts_multiple_times)
209{
210 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 0);
211
212 /* Pend the interrupts, and check the count is incremented. */
213 vcpu_virt_interrupt_inject(vcpu_locked, first_intid);
214 vcpu_virt_interrupt_inject(vcpu_locked, second_intid);
215
216 for (uint32_t i = 0; i < VINT_QUEUE_MAX * 3; i++) {
217 uint32_t it_intid = vcpu_virt_interrupt_get_pending_and_enabled(
218 vcpu_locked);
219 uint32_t peek_intid =
220 vcpu_virt_interrupt_peek_pending_and_enabled(
221 vcpu_locked);
222
223 EXPECT_NE(it_intid, HF_INVALID_INTID);
224 /*
225 * Sequence to validate the `first_intid` and `second_intid`
226 * are retrieved and left pending as expected.
227 */
228 if (i % 2 == 0) {
229 EXPECT_EQ(it_intid, first_intid);
230 EXPECT_EQ(peek_intid, second_intid);
231 } else {
232 EXPECT_EQ(it_intid, second_intid);
233 EXPECT_EQ(peek_intid, first_intid);
234 }
235 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 1);
236 vcpu_virt_interrupt_inject(vcpu_locked, it_intid);
237 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 2);
238 }
239
240 EXPECT_NE(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
241 HF_INVALID_INTID);
242 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 1);
243 EXPECT_NE(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
244 HF_INVALID_INTID);
245 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 0);
246}
247
248/*
249 * Test that each interrupt ID is only set to pending and the count is
250 * incremented once.
251 */
252TEST_F(vcpu, pending_interrupt_is_only_added_once)
253{
254 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 0);
255
256 /* Pend the interrupt, and check the count is incremented. */
257 vcpu_virt_interrupt_inject(vcpu_locked, first_intid);
258 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 1);
259
260 /* Inject the same interrupt the count should not be incremented. */
261 vcpu_virt_interrupt_inject(vcpu_locked, first_intid);
262 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 1);
263
264 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
265 first_intid);
266 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
267 HF_INVALID_INTID);
268 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 0);
269}
270
271/* Check that an cleared interrupts are made to be no longer pending. */
272TEST_F(vcpu, pending_interrupts_can_be_cleared)
273{
274 /*
275 * Pend the interrupts, check the count is incremented, the pending
276 * interrupts are returned correctly and this causes the count to
277 * return to 0.
278 */
279 vcpu_virt_interrupt_inject(vcpu_locked, first_intid);
280 vcpu_virt_interrupt_inject(vcpu_locked, second_intid);
281 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 2);
282 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
283 first_intid);
284 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
285 second_intid);
286 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 0);
287
288 /* Again pend the interrupts. */
289 vcpu_virt_interrupt_inject(vcpu_locked, first_intid);
290 vcpu_virt_interrupt_inject(vcpu_locked, second_intid);
291
292 /* Remove the first interrupt. */
293 vcpu_virt_interrupt_clear(vcpu_locked, first_intid);
294 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 1);
295
296 /*
297 * Check that the first interrupt is cleared.
298 * The second intid should be returned and then the invalid
299 * intid to show there are no more pending and enabled interrupts.
300 */
301 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
302 second_intid);
303 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
304 HF_INVALID_INTID);
305 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 0);
306
307 /* Inject the interrupts again. */
308 vcpu_virt_interrupt_inject(vcpu_locked, first_intid);
309 vcpu_virt_interrupt_inject(vcpu_locked, second_intid);
310 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 2);
311
312 /* Remove the second interrupt. */
313 vcpu_virt_interrupt_clear(vcpu_locked, second_intid);
314 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 1);
315
316 /*
317 * Check that it is now returned as a pending interrupt and is the only
318 * interrupt pending.
319 */
320 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
321 first_intid);
322 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked),
323 HF_INVALID_INTID);
324 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), 0);
325}
326
327/*
328 * Check that when an interrupt is cleared space is create for a new
329 * interrupt to be injected. In addition that after clearing interrupts
330 * the FIFO policy of the remaining interrupts is maintainted.
331 */
332TEST_F(vcpu, pending_interrupts_clear_full_list)
333{
334 /* Fill the interrupt queue for the vCPU. */
335 for (int i = 0; i < VINT_QUEUE_MAX; i++) {
336 vcpu_virt_interrupt_enable(vcpu_locked, i, true);
337 vcpu_virt_interrupt_inject(vcpu_locked, i);
338 }
339
340 EXPECT_EQ(vcpu_virt_interrupt_count_get(vcpu_locked), VINT_QUEUE_MAX);
341
342 /* Check clearing an interrupt clears space for another interrupt. */
343 vcpu_virt_interrupt_clear(vcpu_locked, 2);
344
345 vcpu_virt_interrupt_enable(vcpu_locked, VINT_QUEUE_MAX, true);
346 vcpu_virt_interrupt_inject(vcpu_locked, VINT_QUEUE_MAX);
347
348 /* Check disabled interrupts are also cleared. */
349 vcpu_virt_interrupt_enable(vcpu_locked, 1, false);
350 vcpu_virt_interrupt_clear(vcpu_locked, 1);
351
352 vcpu_virt_interrupt_inject(vcpu_locked, VINT_QUEUE_MAX + 1);
353 /*
354 * Enable the interrupt after injecting it to ensure it will be returned
355 * by vcpu_virt_interrupt_get_pending_and_enabled but is correctly
356 * injected when disabled.
357 */
358 vcpu_virt_interrupt_enable(vcpu_locked, VINT_QUEUE_MAX + 1, true);
359
360 /* Get the interrupts to check the FIFO order is maintained. */
361 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked), 0);
362 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked), 3);
363 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked), 4);
364 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked), 5);
365 EXPECT_EQ(vcpu_virt_interrupt_get_pending_and_enabled(vcpu_locked), 6);
366}
Daniel Boulbyea296e82025-01-31 10:08:16 +0000367} /* namespace */