Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +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. |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <gmock/gmock.h> |
| 10 | |
| 11 | extern "C" { |
Daniel Boulby | 8435071 | 2021-11-26 11:13:20 +0000 | [diff] [blame] | 12 | #include "hf/check.h" |
J-Alves | 67f5ba3 | 2024-09-27 18:07:11 +0100 | [diff] [blame] | 13 | #include "hf/list.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 14 | #include "hf/mpool.h" |
Madhukar Pappireddy | a067dc1 | 2024-10-16 22:20:44 -0500 | [diff] [blame] | 15 | #include "hf/timer_mgmt.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 16 | #include "hf/vm.h" |
| 17 | } |
| 18 | |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 19 | #include <list> |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <span> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "mm_test.hh" |
| 25 | |
| 26 | namespace |
| 27 | { |
| 28 | using namespace ::std::placeholders; |
| 29 | |
| 30 | using ::testing::AllOf; |
| 31 | using ::testing::Each; |
| 32 | using ::testing::SizeIs; |
| 33 | |
| 34 | using struct_vm = struct vm; |
Olivier Deprez | 181074b | 2023-02-02 14:53:23 +0100 | [diff] [blame] | 35 | using struct_vcpu = struct vcpu; |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 36 | using struct_vm_locked = struct vm_locked; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 37 | |
Olivier Deprez | d5a5489 | 2023-02-02 16:45:59 +0100 | [diff] [blame] | 38 | constexpr size_t TEST_HEAP_SIZE = PAGE_SIZE * 64; |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 39 | const int TOP_LEVEL = arch_mm_stage2_max_level(); |
| 40 | |
| 41 | class vm : public ::testing::Test |
| 42 | { |
Olivier Deprez | d5a5489 | 2023-02-02 16:45:59 +0100 | [diff] [blame] | 43 | protected: |
| 44 | static std::unique_ptr<uint8_t[]> test_heap; |
| 45 | |
| 46 | struct mpool ppool; |
| 47 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 48 | void SetUp() override |
| 49 | { |
Olivier Deprez | d5a5489 | 2023-02-02 16:45:59 +0100 | [diff] [blame] | 50 | if (!test_heap) { |
| 51 | /* |
| 52 | * TODO: replace with direct use of stdlib allocator so |
| 53 | * sanitizers are more effective. |
| 54 | */ |
| 55 | test_heap = std::make_unique<uint8_t[]>(TEST_HEAP_SIZE); |
| 56 | mpool_init(&ppool, sizeof(struct mm_page_table)); |
| 57 | mpool_add_chunk(&ppool, test_heap.get(), |
| 58 | TEST_HEAP_SIZE); |
| 59 | } |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 60 | } |
| 61 | |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 62 | public: |
J-Alves | beeb6dc | 2021-12-08 18:21:32 +0000 | [diff] [blame] | 63 | static bool BootOrderSmallerThan(struct_vm *vm1, struct_vm *vm2) |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 64 | { |
J-Alves | beeb6dc | 2021-12-08 18:21:32 +0000 | [diff] [blame] | 65 | return vm1->boot_order < vm2->boot_order; |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 66 | } |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Olivier Deprez | d5a5489 | 2023-02-02 16:45:59 +0100 | [diff] [blame] | 69 | std::unique_ptr<uint8_t[]> vm::test_heap; |
| 70 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 71 | /** |
| 72 | * If nothing is mapped, unmapping the hypervisor has no effect. |
| 73 | */ |
| 74 | TEST_F(vm, vm_unmap_hypervisor_not_mapped) |
| 75 | { |
| 76 | struct_vm *vm; |
| 77 | struct vm_locked vm_locked; |
| 78 | |
Olivier Deprez | 878bd5b | 2021-04-15 19:05:10 +0200 | [diff] [blame] | 79 | /* TODO: check ptable usage (security state?) */ |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 80 | EXPECT_TRUE(vm_init_next(1, &ppool, &vm, false, 0)); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 81 | vm_locked = vm_lock(vm); |
Raghu Krishnamurthy | 0132b51 | 2021-02-03 14:13:26 -0800 | [diff] [blame] | 82 | ASSERT_TRUE(mm_vm_init(&vm->ptable, vm->id, &ppool)); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 83 | EXPECT_TRUE(vm_unmap_hypervisor(vm_locked, &ppool)); |
| 84 | EXPECT_THAT( |
| 85 | mm_test::get_ptable(vm->ptable), |
| 86 | AllOf(SizeIs(4), Each(Each(arch_mm_absent_pte(TOP_LEVEL))))); |
| 87 | mm_vm_fini(&vm->ptable, &ppool); |
| 88 | vm_unlock(&vm_locked); |
| 89 | } |
| 90 | |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 91 | /** |
| 92 | * Validate the "boot_list" is created properly, according to vm's "boot_order" |
| 93 | * field. |
| 94 | */ |
| 95 | TEST_F(vm, vm_boot_order) |
| 96 | { |
| 97 | struct_vm *vm_cur; |
Olivier Deprez | 181074b | 2023-02-02 14:53:23 +0100 | [diff] [blame] | 98 | struct_vcpu *vcpu; |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 99 | std::list<struct_vm *> expected_final_order; |
| 100 | |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 101 | /* |
Olivier Deprez | 181074b | 2023-02-02 14:53:23 +0100 | [diff] [blame] | 102 | * Insertion when no call to "vcpu_update_boot" has been made yet. |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 103 | * The "boot_list" is expected to be empty. |
| 104 | */ |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 105 | EXPECT_TRUE(vm_init_next(1, &ppool, &vm_cur, false, 0)); |
J-Alves | beeb6dc | 2021-12-08 18:21:32 +0000 | [diff] [blame] | 106 | vm_cur->boot_order = 3; |
Olivier Deprez | 181074b | 2023-02-02 14:53:23 +0100 | [diff] [blame] | 107 | vcpu = vm_get_vcpu(vm_cur, 0); |
| 108 | vcpu_update_boot(vcpu); |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 109 | expected_final_order.push_back(vm_cur); |
| 110 | |
Olivier Deprez | 181074b | 2023-02-02 14:53:23 +0100 | [diff] [blame] | 111 | EXPECT_EQ(vcpu_get_boot_vcpu()->vm->id, vm_cur->id); |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 112 | |
| 113 | /* Insertion at the head of the boot list */ |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 114 | EXPECT_TRUE(vm_init_next(1, &ppool, &vm_cur, false, 0)); |
J-Alves | beeb6dc | 2021-12-08 18:21:32 +0000 | [diff] [blame] | 115 | vm_cur->boot_order = 1; |
Olivier Deprez | 181074b | 2023-02-02 14:53:23 +0100 | [diff] [blame] | 116 | vcpu = vm_get_vcpu(vm_cur, 0); |
| 117 | vcpu_update_boot(vcpu); |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 118 | expected_final_order.push_back(vm_cur); |
| 119 | |
Olivier Deprez | 181074b | 2023-02-02 14:53:23 +0100 | [diff] [blame] | 120 | EXPECT_EQ(vcpu_get_boot_vcpu()->vm->id, vm_cur->id); |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 121 | |
| 122 | /* Insertion of two in the middle of the boot list */ |
Olivier Deprez | 181074b | 2023-02-02 14:53:23 +0100 | [diff] [blame] | 123 | for (uint32_t i = 0; i < 2; i++) { |
Daniel Boulby | 6c2aa33 | 2024-11-13 13:54:08 +0000 | [diff] [blame^] | 124 | EXPECT_TRUE(vm_init_next(MAX_CPUS, &ppool, &vm_cur, false, 0)); |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 125 | vm_cur->boot_order = 2; |
Olivier Deprez | 181074b | 2023-02-02 14:53:23 +0100 | [diff] [blame] | 126 | vcpu = vm_get_vcpu(vm_cur, 0); |
| 127 | vcpu_update_boot(vcpu); |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 128 | expected_final_order.push_back(vm_cur); |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Insertion in the end of the list. |
| 133 | * This tests shares the data with "vm_unmap_hypervisor_not_mapped". |
| 134 | * As such, a VM is expected to have been initialized before this |
| 135 | * test, with ID 1 and boot_order 0. |
| 136 | */ |
| 137 | vm_cur = vm_find(1); |
| 138 | EXPECT_FALSE(vm_cur == NULL); |
Olivier Deprez | 181074b | 2023-02-02 14:53:23 +0100 | [diff] [blame] | 139 | vcpu = vm_get_vcpu(vm_cur, 0); |
| 140 | vcpu_update_boot(vcpu); |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 141 | expected_final_order.push_back(vm_cur); |
| 142 | |
| 143 | /* |
| 144 | * Number of VMs initialized should be the same as in the |
| 145 | * "expected_final_order", before the final verification. |
| 146 | */ |
| 147 | EXPECT_EQ(expected_final_order.size(), vm_get_count()) |
| 148 | << "Something went wrong with the test itself...\n"; |
| 149 | |
J-Alves | beeb6dc | 2021-12-08 18:21:32 +0000 | [diff] [blame] | 150 | /* Sort VMs from lower to higher "boot_order" field.*/ |
| 151 | expected_final_order.sort(vm::BootOrderSmallerThan); |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 152 | |
| 153 | std::list<struct_vm *>::iterator it; |
Olivier Deprez | 181074b | 2023-02-02 14:53:23 +0100 | [diff] [blame] | 154 | vcpu = vcpu_get_boot_vcpu(); |
| 155 | for (it = expected_final_order.begin(); |
| 156 | it != expected_final_order.end(); it++) { |
| 157 | EXPECT_TRUE(vcpu != NULL); |
| 158 | EXPECT_EQ((*it)->id, vcpu->vm->id); |
J-Alves | 67f5ba3 | 2024-09-27 18:07:11 +0100 | [diff] [blame] | 159 | vcpu = vcpu_get_next_boot(vcpu); |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 160 | } |
| 161 | } |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 162 | |
Madhukar Pappireddy | a067dc1 | 2024-10-16 22:20:44 -0500 | [diff] [blame] | 163 | TEST_F(vm, vcpu_arch_timer) |
| 164 | { |
| 165 | const cpu_id_t cpu_ids[2] = {0, 1}; |
| 166 | struct_vcpu *vm0_vcpu; |
| 167 | struct_vcpu *vm1_vcpu; |
| 168 | struct_vcpu *deadline_vcpu; |
| 169 | struct_vcpu *target_vcpu; |
| 170 | struct vcpu_locked vcpu_locked; |
| 171 | struct cpu *cpu0; |
| 172 | struct cpu *cpu1; |
| 173 | |
| 174 | /* Initialie CPU module with two physical CPUs. */ |
| 175 | cpu_module_init(cpu_ids, 2); |
| 176 | cpu0 = cpu_find_index(0); |
| 177 | cpu1 = cpu_find_index(1); |
| 178 | |
| 179 | /* Two UP endpoints are deployed for this test. */ |
| 180 | CHECK(vm_get_count() >= 2); |
| 181 | vm0_vcpu = vm_get_vcpu(vm_find_index(0), 0); |
| 182 | vm1_vcpu = vm_get_vcpu(vm_find_index(1), 0); |
| 183 | |
| 184 | /* The execution context of each VM is scheduled on CPU0. */ |
| 185 | vm0_vcpu->cpu = cpu0; |
| 186 | vm1_vcpu->cpu = cpu0; |
| 187 | |
| 188 | /* |
| 189 | * Enable the timer peripheral for each vCPU and setup an arbitraty |
| 190 | * countdown value. |
| 191 | */ |
| 192 | vm0_vcpu->regs.arch_timer.cval = 555555; |
| 193 | vm1_vcpu->regs.arch_timer.cval = 999999; |
| 194 | vm0_vcpu->regs.arch_timer.ctl = 1; |
| 195 | vm1_vcpu->regs.arch_timer.ctl = 1; |
| 196 | |
| 197 | /* No vCPU is being tracked through either timer list. */ |
| 198 | deadline_vcpu = timer_find_vcpu_nearest_deadline(cpu0); |
| 199 | EXPECT_TRUE(deadline_vcpu == NULL); |
| 200 | deadline_vcpu = timer_find_vcpu_nearest_deadline(cpu1); |
| 201 | EXPECT_TRUE(deadline_vcpu == NULL); |
| 202 | |
| 203 | /* vCPU of VM0 and VM1 are being added to the list. */ |
| 204 | timer_vcpu_manage(vm0_vcpu); |
| 205 | timer_vcpu_manage(vm1_vcpu); |
| 206 | |
| 207 | deadline_vcpu = timer_find_vcpu_nearest_deadline(cpu0); |
| 208 | EXPECT_EQ(deadline_vcpu, vm0_vcpu); |
| 209 | |
| 210 | /* Remove one of the vCPUs from the CPU0 list. */ |
| 211 | vm0_vcpu->regs.arch_timer.cval = 0; |
| 212 | vm0_vcpu->regs.arch_timer.ctl = 0; |
| 213 | timer_vcpu_manage(vm0_vcpu); |
| 214 | |
| 215 | /* This leaves one vCPU entry on CPU0 list. */ |
| 216 | deadline_vcpu = timer_find_vcpu_nearest_deadline(cpu0); |
| 217 | EXPECT_EQ(deadline_vcpu, vm1_vcpu); |
| 218 | |
| 219 | /* Attempt to migrate VM1 vCPU from CPU0 to CPU1. */ |
| 220 | vcpu_locked = vcpu_lock(vm1_vcpu); |
| 221 | timer_migrate_to_other_cpu(cpu1, vcpu_locked); |
| 222 | vcpu_unlock(&vcpu_locked); |
| 223 | |
| 224 | /* |
| 225 | * After migration, ensure the list is empty on CPU0 but non-empty on |
| 226 | * CPU1. |
| 227 | */ |
| 228 | deadline_vcpu = timer_find_vcpu_nearest_deadline(cpu0); |
| 229 | EXPECT_TRUE(deadline_vcpu == NULL); |
| 230 | |
| 231 | /* |
| 232 | * vCPU of VM1 is now running on CPU1. It must be the target vCPU when |
| 233 | * the timer has expired. |
| 234 | */ |
| 235 | target_vcpu = timer_find_target_vcpu(vm1_vcpu); |
| 236 | EXPECT_EQ(target_vcpu, vm1_vcpu); |
| 237 | } |
| 238 | |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 239 | /** |
| 240 | * Validates updates and check functions for binding notifications to endpoints. |
| 241 | */ |
| 242 | TEST_F(vm, vm_notifications_bind_diff_senders) |
| 243 | { |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 244 | struct_vm *current_vm = nullptr; |
| 245 | struct vm_locked current_vm_locked; |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 246 | std::vector<struct_vm *> dummy_senders; |
| 247 | ffa_notifications_bitmap_t bitmaps[] = { |
| 248 | 0x00000000FFFFFFFFU, 0xFFFFFFFF00000000U, 0x0000FFFFFFFF0000U}; |
| 249 | bool is_from_vm = true; |
| 250 | |
| 251 | /* For the subsequent tests three VMs are used. */ |
| 252 | CHECK(vm_get_count() >= 3); |
| 253 | |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 254 | current_vm = vm_find_index(0); |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 255 | |
| 256 | dummy_senders.push_back(vm_find_index(1)); |
| 257 | dummy_senders.push_back(vm_find_index(2)); |
| 258 | |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 259 | current_vm_locked = vm_lock(current_vm); |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 260 | |
| 261 | for (unsigned int i = 0; i < 2; i++) { |
| 262 | /* Validate bindings condition after initialization. */ |
| 263 | EXPECT_TRUE(vm_notifications_validate_binding( |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 264 | current_vm_locked, is_from_vm, HF_INVALID_VM_ID, |
| 265 | bitmaps[i], false)); |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 266 | |
| 267 | /* |
| 268 | * Validate bind related operations. For this test considering |
| 269 | * only global notifications. |
| 270 | */ |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 271 | vm_notifications_update_bindings(current_vm_locked, is_from_vm, |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 272 | dummy_senders[i]->id, |
| 273 | bitmaps[i], false); |
| 274 | |
| 275 | EXPECT_TRUE(vm_notifications_validate_binding( |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 276 | current_vm_locked, is_from_vm, dummy_senders[i]->id, |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 277 | bitmaps[i], false)); |
| 278 | |
| 279 | EXPECT_FALSE(vm_notifications_validate_binding( |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 280 | current_vm_locked, is_from_vm, dummy_senders[1 - i]->id, |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 281 | bitmaps[i], false)); |
| 282 | |
| 283 | EXPECT_FALSE(vm_notifications_validate_binding( |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 284 | current_vm_locked, is_from_vm, dummy_senders[i]->id, |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 285 | bitmaps[1 - i], false)); |
| 286 | |
| 287 | EXPECT_FALSE(vm_notifications_validate_binding( |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 288 | current_vm_locked, is_from_vm, dummy_senders[i]->id, |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 289 | bitmaps[2], false)); |
| 290 | } |
| 291 | |
| 292 | /** Clean up bind for other tests. */ |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 293 | vm_notifications_update_bindings(current_vm_locked, is_from_vm, 0, |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 294 | bitmaps[0], false); |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 295 | vm_notifications_update_bindings(current_vm_locked, is_from_vm, 0, |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 296 | bitmaps[1], false); |
| 297 | |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 298 | vm_unlock(¤t_vm_locked); |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Validates updates and check functions for binding notifications, namely the |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 303 | * configuration of bindings of global and per-vCPU notifications. |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 304 | */ |
| 305 | TEST_F(vm, vm_notification_bind_per_vcpu_vs_global) |
| 306 | { |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 307 | struct_vm *current_vm; |
| 308 | struct vm_locked current_vm_locked; |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 309 | struct_vm *dummy_sender; |
| 310 | ffa_notifications_bitmap_t global = 0x00000000FFFFFFFFU; |
| 311 | ffa_notifications_bitmap_t per_vcpu = ~global; |
| 312 | bool is_from_vm = true; |
| 313 | |
| 314 | CHECK(vm_get_count() >= 2); |
| 315 | |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 316 | current_vm = vm_find_index(0); |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 317 | |
| 318 | dummy_sender = vm_find_index(1); |
| 319 | |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 320 | current_vm_locked = vm_lock(current_vm); |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 321 | |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 322 | vm_notifications_update_bindings(current_vm_locked, is_from_vm, |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 323 | dummy_sender->id, global, false); |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 324 | vm_notifications_update_bindings(current_vm_locked, is_from_vm, |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 325 | dummy_sender->id, per_vcpu, true); |
| 326 | |
| 327 | /* Check validation of global notifications bindings. */ |
| 328 | EXPECT_TRUE(vm_notifications_validate_binding( |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 329 | current_vm_locked, is_from_vm, dummy_sender->id, global, |
| 330 | false)); |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 331 | |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 332 | /* Check validation of per-vCPU notifications bindings. */ |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 333 | EXPECT_TRUE(vm_notifications_validate_binding( |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 334 | current_vm_locked, is_from_vm, dummy_sender->id, per_vcpu, |
| 335 | true)); |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 336 | |
| 337 | /** |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 338 | * Check that global notifications are not validated as per-vCPU, and |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 339 | * vice-versa. |
| 340 | */ |
| 341 | EXPECT_FALSE(vm_notifications_validate_binding( |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 342 | current_vm_locked, is_from_vm, dummy_sender->id, global, true)); |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 343 | EXPECT_FALSE(vm_notifications_validate_binding( |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 344 | current_vm_locked, is_from_vm, dummy_sender->id, per_vcpu, |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 345 | false)); |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 346 | EXPECT_FALSE(vm_notifications_validate_binding( |
| 347 | current_vm_locked, is_from_vm, dummy_sender->id, |
| 348 | global | per_vcpu, true)); |
| 349 | EXPECT_FALSE(vm_notifications_validate_binding( |
| 350 | current_vm_locked, is_from_vm, dummy_sender->id, |
| 351 | global | per_vcpu, false)); |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 352 | |
| 353 | /** Undo the bindings */ |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 354 | vm_notifications_update_bindings(current_vm_locked, is_from_vm, 0, |
| 355 | global, false); |
| 356 | EXPECT_TRUE(vm_notifications_validate_binding( |
| 357 | current_vm_locked, is_from_vm, 0, global, false)); |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 358 | |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 359 | vm_notifications_update_bindings(current_vm_locked, is_from_vm, 0, |
| 360 | per_vcpu, false); |
| 361 | EXPECT_TRUE(vm_notifications_validate_binding( |
| 362 | current_vm_locked, is_from_vm, 0, per_vcpu, false)); |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 363 | |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 364 | vm_unlock(¤t_vm_locked); |
J-Alves | 60eaff9 | 2021-05-27 14:54:41 +0100 | [diff] [blame] | 365 | } |
| 366 | |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 367 | /** |
| 368 | * Validates accesses to notifications bitmaps. |
| 369 | */ |
| 370 | TEST_F(vm, vm_notifications_set_and_get) |
| 371 | { |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 372 | struct_vm *current_vm; |
| 373 | struct vm_locked current_vm_locked; |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 374 | struct_vm *dummy_sender; |
| 375 | ffa_notifications_bitmap_t global = 0x00000000FFFFFFFFU; |
| 376 | ffa_notifications_bitmap_t per_vcpu = ~global; |
| 377 | ffa_notifications_bitmap_t ret; |
Raghu Krishnamurthy | 30aabd6 | 2022-09-17 21:41:00 -0700 | [diff] [blame] | 378 | const unsigned int vcpu_idx = 0; |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 379 | struct notifications *notifications; |
| 380 | const bool is_from_vm = true; |
| 381 | |
| 382 | CHECK(vm_get_count() >= 2); |
| 383 | |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 384 | current_vm = vm_find_index(0); |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 385 | dummy_sender = vm_find_index(1); |
| 386 | |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 387 | notifications = ¤t_vm->notifications.from_vm; |
| 388 | current_vm_locked = vm_lock(current_vm); |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 389 | |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 390 | vm_notifications_update_bindings(current_vm_locked, is_from_vm, |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 391 | dummy_sender->id, global, false); |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 392 | vm_notifications_update_bindings(current_vm_locked, is_from_vm, |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 393 | dummy_sender->id, per_vcpu, true); |
| 394 | |
| 395 | /* |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 396 | * Validate get notifications bitmap for global notifications. |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 397 | */ |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 398 | vm_notifications_partition_set_pending(current_vm_locked, is_from_vm, |
| 399 | global, 0ull, false); |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 400 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 401 | ret = vm_notifications_partition_get_pending(current_vm_locked, |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 402 | is_from_vm, 0ull); |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 403 | EXPECT_EQ(ret, global); |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 404 | EXPECT_EQ(notifications->global.pending, 0ull); |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 405 | |
| 406 | /* |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 407 | * Validate get notifications bitmap for per-vCPU notifications. |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 408 | */ |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 409 | vm_notifications_partition_set_pending(current_vm_locked, is_from_vm, |
| 410 | per_vcpu, vcpu_idx, true); |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 411 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 412 | ret = vm_notifications_partition_get_pending(current_vm_locked, |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 413 | is_from_vm, vcpu_idx); |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 414 | EXPECT_EQ(ret, per_vcpu); |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 415 | EXPECT_EQ(notifications->per_vcpu[vcpu_idx].pending, 0ull); |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 416 | |
| 417 | /* |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 418 | * Validate that getting notifications for a specific vCPU also returns |
| 419 | * global notifications. |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 420 | */ |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 421 | vm_notifications_partition_set_pending(current_vm_locked, is_from_vm, |
| 422 | per_vcpu, vcpu_idx, true); |
| 423 | vm_notifications_partition_set_pending(current_vm_locked, is_from_vm, |
| 424 | global, 0ull, false); |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 425 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 426 | ret = vm_notifications_partition_get_pending(current_vm_locked, |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 427 | is_from_vm, vcpu_idx); |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 428 | EXPECT_EQ(ret, per_vcpu | global); |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 429 | EXPECT_EQ(notifications->per_vcpu[vcpu_idx].pending, 0ull); |
| 430 | EXPECT_EQ(notifications->global.pending, 0ull); |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 431 | |
| 432 | /** Undo the binding */ |
J-Alves | d3e8162 | 2021-10-05 14:55:57 +0100 | [diff] [blame] | 433 | vm_notifications_update_bindings(current_vm_locked, is_from_vm, 0ull, |
| 434 | global, false); |
| 435 | vm_notifications_update_bindings(current_vm_locked, is_from_vm, 0ull, |
| 436 | per_vcpu, true); |
| 437 | vm_unlock(¤t_vm_locked); |
J-Alves | ce2f8d3 | 2021-06-10 18:30:21 +0100 | [diff] [blame] | 438 | } |
| 439 | |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 440 | /** |
| 441 | * Validates simple getting of notifications info for global notifications. |
| 442 | */ |
| 443 | TEST_F(vm, vm_notifications_info_get_global) |
| 444 | { |
| 445 | ffa_notifications_bitmap_t to_set = 0xFU; |
| 446 | ffa_notifications_bitmap_t got; |
| 447 | |
| 448 | /** |
| 449 | * Following set of variables that are also expected to be used when |
| 450 | * handling FFA_NOTIFICATION_INFO_GET. |
| 451 | */ |
| 452 | uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 453 | uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 454 | uint32_t ids_count = 0; |
| 455 | uint32_t lists_count = 0; |
| 456 | enum notifications_info_get_state current_state = INIT; |
| 457 | |
| 458 | CHECK(vm_get_count() >= 2); |
| 459 | |
| 460 | for (unsigned int i = 0; i < 2; i++) { |
| 461 | struct_vm *current_vm = vm_find_index(0); |
| 462 | struct vm_locked current_vm_locked = vm_lock(current_vm); |
| 463 | struct notifications *notifications = |
| 464 | ¤t_vm->notifications.from_sp; |
| 465 | const bool is_from_vm = false; |
| 466 | |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 467 | vm_notifications_partition_set_pending( |
| 468 | current_vm_locked, is_from_vm, to_set, 0, false); |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 469 | |
| 470 | vm_notifications_info_get_pending( |
| 471 | current_vm_locked, is_from_vm, ids, &ids_count, |
| 472 | lists_sizes, &lists_count, |
| 473 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS, ¤t_state); |
| 474 | |
| 475 | /* |
| 476 | * Here the number of IDs and list count should be the same. |
| 477 | * As we are testing with Global notifications, this is |
| 478 | * expected. |
| 479 | */ |
| 480 | EXPECT_EQ(ids_count, i + 1); |
| 481 | EXPECT_EQ(lists_count, i + 1); |
| 482 | EXPECT_EQ(lists_sizes[i], 0); |
| 483 | EXPECT_EQ(to_set, notifications->global.info_get_retrieved); |
| 484 | |
| 485 | /* Action must be reset to initial state for each VM. */ |
| 486 | current_state = INIT; |
| 487 | |
| 488 | /* |
| 489 | * Check that getting pending notifications gives the expected |
| 490 | * return and cleans the 'pending' and 'info_get_retrieved' |
| 491 | * bitmaps. |
| 492 | */ |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 493 | got = vm_notifications_partition_get_pending(current_vm_locked, |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 494 | is_from_vm, 0); |
| 495 | EXPECT_EQ(got, to_set); |
| 496 | |
| 497 | EXPECT_EQ(notifications->global.info_get_retrieved, 0U); |
| 498 | EXPECT_EQ(notifications->global.pending, 0U); |
| 499 | |
| 500 | vm_unlock(¤t_vm_locked); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Validates simple getting of notifications info for per-vCPU notifications. |
| 506 | */ |
| 507 | TEST_F(vm, vm_notifications_info_get_per_vcpu) |
| 508 | { |
| 509 | const ffa_notifications_bitmap_t per_vcpu = 0xFU; |
| 510 | ffa_notifications_bitmap_t got; |
| 511 | |
| 512 | /* |
| 513 | * Following set of variables that are also expected to be used when |
| 514 | * handling ffa_notification_info_get. |
| 515 | */ |
| 516 | uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 517 | uint32_t ids_count = 0; |
| 518 | uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 519 | uint32_t lists_count = 0; |
| 520 | enum notifications_info_get_state current_state = INIT; |
| 521 | |
| 522 | CHECK(vm_get_count() >= 2); |
| 523 | |
| 524 | for (unsigned int i = 0; i < 2; i++) { |
| 525 | struct_vm *current_vm = vm_find_index(0); |
| 526 | struct vm_locked current_vm_locked = vm_lock(current_vm); |
| 527 | struct notifications *notifications = |
| 528 | ¤t_vm->notifications.from_sp; |
| 529 | const bool is_from_vm = false; |
| 530 | |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 531 | vm_notifications_partition_set_pending( |
| 532 | current_vm_locked, is_from_vm, per_vcpu, 0, true); |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 533 | |
| 534 | vm_notifications_info_get_pending( |
| 535 | current_vm_locked, is_from_vm, ids, &ids_count, |
| 536 | lists_sizes, &lists_count, |
| 537 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS, ¤t_state); |
| 538 | |
| 539 | /* |
| 540 | * Here the number of IDs and list count should be the same. |
| 541 | * As we are testing with Global notifications, this is |
| 542 | * expected. |
| 543 | */ |
| 544 | EXPECT_EQ(ids_count, (i + 1) * 2); |
| 545 | EXPECT_EQ(lists_count, i + 1); |
| 546 | EXPECT_EQ(lists_sizes[i], 1); |
| 547 | EXPECT_EQ(per_vcpu, |
| 548 | notifications->per_vcpu[0].info_get_retrieved); |
| 549 | |
| 550 | /* Action must be reset to initial state for each VM. */ |
| 551 | current_state = INIT; |
| 552 | |
| 553 | /* |
| 554 | * Check that getting pending notifications gives the expected |
| 555 | * return and cleans the 'pending' and 'info_get_retrieved' |
| 556 | * bitmaps. |
| 557 | */ |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 558 | got = vm_notifications_partition_get_pending(current_vm_locked, |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 559 | is_from_vm, 0); |
| 560 | EXPECT_EQ(got, per_vcpu); |
| 561 | |
| 562 | EXPECT_EQ(notifications->per_vcpu[0].info_get_retrieved, 0U); |
| 563 | EXPECT_EQ(notifications->per_vcpu[0].pending, 0U); |
| 564 | |
| 565 | vm_unlock(¤t_vm_locked); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Validate getting of notifications information if all VCPUs have notifications |
| 571 | * pending. |
| 572 | */ |
| 573 | TEST_F(vm, vm_notifications_info_get_per_vcpu_all_vcpus) |
| 574 | { |
| 575 | struct_vm *current_vm = nullptr; |
| 576 | struct vm_locked current_vm_locked; |
| 577 | const ffa_vcpu_count_t vcpu_count = MAX_CPUS; |
| 578 | ffa_notifications_bitmap_t got; |
| 579 | const ffa_notifications_bitmap_t global = 0xF0000; |
| 580 | |
| 581 | /* |
| 582 | * Following set of variables that are also expected to be used when |
| 583 | * handling ffa_notification_info_get. |
| 584 | */ |
| 585 | struct notifications *notifications; |
| 586 | const bool is_from_sp = false; |
| 587 | uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 588 | uint32_t ids_count = 0; |
| 589 | uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 590 | uint32_t lists_count = 0; |
| 591 | enum notifications_info_get_state current_state = INIT; |
| 592 | |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 593 | EXPECT_TRUE(vm_init_next(vcpu_count, &ppool, ¤t_vm, false, 0)); |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 594 | current_vm_locked = vm_lock(current_vm); |
| 595 | notifications = ¤t_vm->notifications.from_sp; |
| 596 | |
| 597 | for (unsigned int i = 0; i < vcpu_count; i++) { |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 598 | vm_notifications_partition_set_pending( |
| 599 | current_vm_locked, is_from_sp, FFA_NOTIFICATION_MASK(i), |
| 600 | i, true); |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /* |
| 604 | * Adding a global notification should not change the list of IDs, |
| 605 | * because global notifications only require the VM ID to be included in |
| 606 | * the list, at least once. |
| 607 | */ |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 608 | vm_notifications_partition_set_pending(current_vm_locked, is_from_sp, |
| 609 | global, 0, false); |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 610 | |
| 611 | vm_notifications_info_get_pending(current_vm_locked, is_from_sp, ids, |
| 612 | &ids_count, lists_sizes, &lists_count, |
| 613 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS, |
| 614 | ¤t_state); |
| 615 | |
| 616 | /* |
| 617 | * This test has been conceived for the expected MAX_CPUS 4. |
| 618 | * All VCPUs have notifications of the same VM, to be broken down in 2 |
| 619 | * lists with 3 VCPU IDs, and 1 VCPU ID respectively. |
| 620 | * The list of IDs should look like: {<vm_id>, 0, 1, 2, <vm_id>, 3}. |
| 621 | */ |
| 622 | CHECK(MAX_CPUS == 4); |
| 623 | EXPECT_EQ(ids_count, 6U); |
| 624 | EXPECT_EQ(lists_count, 2U); |
| 625 | EXPECT_EQ(lists_sizes[0], 3); |
| 626 | EXPECT_EQ(lists_sizes[1], 1); |
| 627 | |
| 628 | for (unsigned int i = 0; i < vcpu_count; i++) { |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 629 | got = vm_notifications_partition_get_pending(current_vm_locked, |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 630 | is_from_sp, i); |
| 631 | |
| 632 | /* |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 633 | * The first call to |
| 634 | * vm_notifications_partition_get_pending should also |
| 635 | * include the global notifications on the return. |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 636 | */ |
| 637 | ffa_notifications_bitmap_t to_check = |
| 638 | (i != 0) ? FFA_NOTIFICATION_MASK(i) |
| 639 | : FFA_NOTIFICATION_MASK(i) | global; |
| 640 | |
| 641 | EXPECT_EQ(got, to_check); |
| 642 | |
| 643 | EXPECT_EQ(notifications->per_vcpu[i].pending, 0); |
| 644 | EXPECT_EQ(notifications->per_vcpu[i].info_get_retrieved, 0); |
| 645 | } |
| 646 | |
| 647 | vm_unlock(¤t_vm_locked); |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * Validate change of state from 'vm_notifications_info_get_pending', when the |
| 652 | * list of IDs is full. |
| 653 | */ |
| 654 | TEST_F(vm, vm_notifications_info_get_full_per_vcpu) |
| 655 | { |
| 656 | struct_vm *current_vm = vm_find_index(0); |
| 657 | struct vm_locked current_vm_locked = vm_lock(current_vm); |
| 658 | struct notifications *notifications = |
| 659 | ¤t_vm->notifications.from_sp; |
| 660 | const bool is_from_vm = false; |
| 661 | ffa_notifications_bitmap_t got = 0; |
| 662 | |
| 663 | /* |
| 664 | * Following set of variables that are also expected to be used when |
| 665 | * handling ffa_notification_info_get. |
| 666 | * For this 'ids_count' has been initialized such that it indicates |
| 667 | * there is no space in the list for a per-vCPU notification (VM ID and |
| 668 | * VCPU ID). |
| 669 | */ |
| 670 | uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 671 | uint32_t ids_count = FFA_NOTIFICATIONS_INFO_GET_MAX_IDS - 1; |
| 672 | uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 673 | uint32_t lists_count = 10; |
| 674 | enum notifications_info_get_state current_state = INIT; |
| 675 | CHECK(vm_get_count() >= 2); |
| 676 | |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 677 | vm_notifications_partition_set_pending(current_vm_locked, is_from_vm, |
| 678 | FFA_NOTIFICATION_MASK(1), 0, |
| 679 | true); |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 680 | |
| 681 | /* Call function to get notifications info, with only per-vCPU set. */ |
| 682 | vm_notifications_info_get_pending(current_vm_locked, is_from_vm, ids, |
| 683 | &ids_count, lists_sizes, &lists_count, |
| 684 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS, |
| 685 | ¤t_state); |
| 686 | |
| 687 | /* |
| 688 | * Verify that as soon as there isn't space to do the required |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 689 | * insertion in the list, the |
| 690 | * 'vm_notifications_partition_get_pending' returns and changes |
| 691 | * list state to FULL. In this case returning, because it would need to |
| 692 | * add two IDs (VM ID and VCPU ID). |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 693 | */ |
| 694 | EXPECT_EQ(current_state, FULL); |
| 695 | EXPECT_EQ(ids_count, FFA_NOTIFICATIONS_INFO_GET_MAX_IDS - 1); |
| 696 | EXPECT_EQ(notifications->per_vcpu[0].info_get_retrieved, 0U); |
| 697 | |
| 698 | /* |
| 699 | * At this point there is still room for the information of a global |
| 700 | * notification (only VM ID to be added). Reset 'current_state' |
| 701 | * for the insertion to happen at the last position of the array. |
| 702 | */ |
| 703 | current_state = INIT; |
| 704 | |
| 705 | /* Setting global notification */ |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 706 | vm_notifications_partition_set_pending(current_vm_locked, is_from_vm, |
| 707 | FFA_NOTIFICATION_MASK(2), 0, |
| 708 | false); |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 709 | |
| 710 | vm_notifications_info_get_pending(current_vm_locked, is_from_vm, ids, |
| 711 | &ids_count, lists_sizes, &lists_count, |
| 712 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS, |
| 713 | ¤t_state); |
| 714 | |
| 715 | /* |
| 716 | * Now List must be full, the set global notification must be part of |
| 717 | * 'info_get_retrieved', and the 'current_state' should be set to FULL |
| 718 | * due to the pending per-vCPU notification in VCPU 0. |
| 719 | */ |
| 720 | EXPECT_EQ(ids_count, FFA_NOTIFICATIONS_INFO_GET_MAX_IDS); |
| 721 | EXPECT_EQ(current_state, FULL); |
| 722 | EXPECT_EQ(notifications->global.info_get_retrieved, |
| 723 | FFA_NOTIFICATION_MASK(2)); |
| 724 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 725 | got = vm_notifications_partition_get_pending(current_vm_locked, |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 726 | is_from_vm, 0); |
| 727 | EXPECT_EQ(got, FFA_NOTIFICATION_MASK(1) | FFA_NOTIFICATION_MASK(2)); |
| 728 | |
| 729 | vm_unlock(¤t_vm_locked); |
| 730 | } |
| 731 | |
| 732 | TEST_F(vm, vm_notifications_info_get_full_global) |
| 733 | { |
| 734 | struct_vm *current_vm = vm_find_index(0); |
| 735 | struct vm_locked current_vm_locked = vm_lock(current_vm); |
| 736 | ffa_notifications_bitmap_t got; |
| 737 | struct notifications *notifications; |
| 738 | const bool is_from_vm = false; |
| 739 | /* |
| 740 | * Following set of variables that are also expected to be used when |
| 741 | * handling ffa_notification_info_get. |
| 742 | * For this 'ids_count' has been initialized such that it indicates |
| 743 | * there is no space in the list for a global notification (VM ID only). |
| 744 | */ |
| 745 | uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 746 | uint32_t ids_count = FFA_NOTIFICATIONS_INFO_GET_MAX_IDS; |
| 747 | uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 748 | uint32_t lists_count = 10; |
| 749 | enum notifications_info_get_state current_state = INIT; |
| 750 | |
| 751 | CHECK(vm_get_count() >= 1); |
| 752 | |
| 753 | current_vm = vm_find_index(0); |
| 754 | |
| 755 | notifications = ¤t_vm->notifications.from_sp; |
| 756 | |
| 757 | /* Set global notification. */ |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 758 | vm_notifications_partition_set_pending(current_vm_locked, is_from_vm, |
| 759 | FFA_NOTIFICATION_MASK(10), 0, |
| 760 | false); |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 761 | |
| 762 | /* Get notifications info for the given notifications. */ |
| 763 | vm_notifications_info_get_pending(current_vm_locked, is_from_vm, ids, |
| 764 | &ids_count, lists_sizes, &lists_count, |
| 765 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS, |
| 766 | ¤t_state); |
| 767 | |
| 768 | /* Expect 'info_get_retrieved' bitmap to be 0. */ |
| 769 | EXPECT_EQ(notifications->global.info_get_retrieved, 0U); |
| 770 | EXPECT_EQ(notifications->global.pending, FFA_NOTIFICATION_MASK(10)); |
| 771 | EXPECT_EQ(ids_count, FFA_NOTIFICATIONS_INFO_GET_MAX_IDS); |
| 772 | EXPECT_EQ(current_state, FULL); |
| 773 | |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 774 | got = vm_notifications_partition_get_pending(current_vm_locked, |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 775 | is_from_vm, 0); |
J-Alves | 9f74b93 | 2021-10-11 14:20:05 +0100 | [diff] [blame] | 776 | EXPECT_EQ(got, FFA_NOTIFICATION_MASK(10)); |
| 777 | |
J-Alves | 96f6e29 | 2021-06-08 17:32:40 +0100 | [diff] [blame] | 778 | vm_unlock(¤t_vm_locked); |
| 779 | } |
| 780 | |
J-Alves | f31940e | 2022-03-25 17:24:00 +0000 | [diff] [blame] | 781 | TEST_F(vm, vm_notifications_info_get_from_framework) |
| 782 | { |
| 783 | struct vm_locked vm_locked = vm_lock(vm_find_index(0)); |
| 784 | uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 785 | uint32_t ids_count = 0; |
| 786 | uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 787 | uint32_t lists_count = 0; |
| 788 | |
| 789 | vm_notifications_framework_set_pending(vm_locked, 0x1U); |
| 790 | |
| 791 | /* Get notifications info for the given notifications. */ |
| 792 | vm_notifications_info_get(vm_locked, ids, &ids_count, lists_sizes, |
| 793 | &lists_count, |
| 794 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS); |
| 795 | |
| 796 | EXPECT_EQ(ids[0], vm_locked.vm->id); |
| 797 | EXPECT_EQ(ids_count, 1); |
| 798 | EXPECT_EQ(lists_sizes[0], 0); |
| 799 | EXPECT_EQ(lists_count, 1); |
| 800 | |
| 801 | EXPECT_EQ(vm_notifications_framework_get_pending(vm_locked), 0x1U); |
| 802 | |
| 803 | vm_unlock(&vm_locked); |
| 804 | } |
| 805 | |
Daniel Boulby | 8be2651 | 2024-09-03 19:41:11 +0100 | [diff] [blame] | 806 | /** |
| 807 | * Validates simple getting of notifications info for pending IPI. |
Daniel Boulby | 6c2aa33 | 2024-11-13 13:54:08 +0000 | [diff] [blame^] | 808 | * Also checks that vCPUs with pending IPIs are only reported if the |
| 809 | * vCPU is in the waiting state. |
Daniel Boulby | 8be2651 | 2024-09-03 19:41:11 +0100 | [diff] [blame] | 810 | */ |
| 811 | TEST_F(vm, vm_notifications_info_get_ipi) |
| 812 | { |
| 813 | /* |
| 814 | * Following set of variables that are also expected to be used when |
| 815 | * handling ffa_notification_info_get. |
| 816 | */ |
| 817 | uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 818 | uint32_t ids_count = 0; |
| 819 | uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 820 | uint32_t lists_count = 0; |
| 821 | enum notifications_info_get_state current_state = INIT; |
Daniel Boulby | 6c2aa33 | 2024-11-13 13:54:08 +0000 | [diff] [blame^] | 822 | struct_vm *current_vm = vm_find_index(4); |
Daniel Boulby | 8be2651 | 2024-09-03 19:41:11 +0100 | [diff] [blame] | 823 | struct vcpu *target_vcpu = vm_get_vcpu(current_vm, 1); |
| 824 | struct interrupts *interrupts = &target_vcpu->interrupts; |
| 825 | const bool is_from_vm = false; |
| 826 | struct vm_locked current_vm_locked = vm_lock(current_vm); |
| 827 | |
| 828 | EXPECT_TRUE(current_vm->vcpu_count >= 2); |
| 829 | |
| 830 | vcpu_virt_interrupt_set_pending(interrupts, HF_IPI_INTID); |
| 831 | |
| 832 | vm_notifications_info_get_pending(current_vm_locked, is_from_vm, ids, |
| 833 | &ids_count, lists_sizes, &lists_count, |
| 834 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS, |
| 835 | ¤t_state); |
| 836 | |
Daniel Boulby | 6c2aa33 | 2024-11-13 13:54:08 +0000 | [diff] [blame^] | 837 | EXPECT_EQ(ids_count, 0); |
| 838 | EXPECT_EQ(lists_count, 0); |
| 839 | |
| 840 | target_vcpu->state = VCPU_STATE_WAITING; |
| 841 | |
| 842 | vm_notifications_info_get_pending(current_vm_locked, is_from_vm, ids, |
| 843 | &ids_count, lists_sizes, &lists_count, |
| 844 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS, |
| 845 | ¤t_state); |
| 846 | |
Daniel Boulby | 8be2651 | 2024-09-03 19:41:11 +0100 | [diff] [blame] | 847 | EXPECT_EQ(ids_count, 2); |
| 848 | EXPECT_EQ(lists_count, 1); |
| 849 | EXPECT_EQ(lists_sizes[0], 1); |
| 850 | EXPECT_EQ(ids[0], current_vm->id); |
| 851 | EXPECT_EQ(ids[1], 1); |
| 852 | EXPECT_EQ(target_vcpu->ipi_info_get_retrieved, true); |
| 853 | |
| 854 | /* Check it is not retrieved multiple times. */ |
| 855 | current_state = INIT; |
| 856 | ids[0] = 0; |
| 857 | ids[1] = 0; |
| 858 | ids_count = 0; |
| 859 | lists_sizes[0] = 0; |
| 860 | lists_count = 0; |
| 861 | |
| 862 | vm_notifications_info_get_pending(current_vm_locked, is_from_vm, ids, |
| 863 | &ids_count, lists_sizes, &lists_count, |
| 864 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS, |
| 865 | ¤t_state); |
| 866 | EXPECT_EQ(ids_count, 0); |
| 867 | EXPECT_EQ(lists_count, 0); |
| 868 | EXPECT_EQ(lists_sizes[0], 0); |
| 869 | |
| 870 | vm_unlock(¤t_vm_locked); |
| 871 | } |
| 872 | |
| 873 | /** |
| 874 | * Validates simple getting of notifications info for pending with IPI when |
| 875 | * notification for the same vcpu is also pending. |
| 876 | */ |
| 877 | TEST_F(vm, vm_notifications_info_get_ipi_with_per_vcpu) |
| 878 | { |
| 879 | /* |
| 880 | * Following set of variables that are also expected to be used when |
| 881 | * handling ffa_notification_info_get. |
| 882 | */ |
| 883 | uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 884 | uint32_t ids_count = 0; |
| 885 | uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 886 | uint32_t lists_count = 0; |
| 887 | enum notifications_info_get_state current_state = INIT; |
Daniel Boulby | 6c2aa33 | 2024-11-13 13:54:08 +0000 | [diff] [blame^] | 888 | struct_vm *current_vm = vm_find_index(4); |
Daniel Boulby | 8be2651 | 2024-09-03 19:41:11 +0100 | [diff] [blame] | 889 | struct vcpu *target_vcpu = vm_get_vcpu(current_vm, 1); |
| 890 | struct interrupts *interrupts = &target_vcpu->interrupts; |
| 891 | const bool is_from_vm = false; |
| 892 | struct vm_locked current_vm_locked = vm_lock(current_vm); |
| 893 | |
| 894 | EXPECT_TRUE(current_vm->vcpu_count >= 2); |
| 895 | |
| 896 | vcpu_virt_interrupt_set_pending(interrupts, HF_IPI_INTID); |
| 897 | |
| 898 | vm_notifications_partition_set_pending(current_vm_locked, is_from_vm, |
| 899 | true, 1, true); |
| 900 | vm_notifications_info_get_pending(current_vm_locked, is_from_vm, ids, |
| 901 | &ids_count, lists_sizes, &lists_count, |
| 902 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS, |
| 903 | ¤t_state); |
| 904 | |
| 905 | EXPECT_EQ(ids_count, 2); |
| 906 | EXPECT_EQ(lists_count, 1); |
| 907 | EXPECT_EQ(lists_sizes[0], 1); |
| 908 | EXPECT_EQ(ids[0], current_vm->id); |
| 909 | EXPECT_EQ(ids[1], 1); |
| 910 | EXPECT_EQ(target_vcpu->ipi_info_get_retrieved, true); |
| 911 | |
| 912 | /* Reset the state and values. */ |
| 913 | current_state = INIT; |
| 914 | ids[0] = 0; |
| 915 | ids[1] = 0; |
| 916 | ids_count = 0; |
| 917 | lists_sizes[0] = 0; |
| 918 | lists_count = 0; |
| 919 | |
| 920 | vm_notifications_info_get_pending(current_vm_locked, is_from_vm, ids, |
| 921 | &ids_count, lists_sizes, &lists_count, |
| 922 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS, |
| 923 | ¤t_state); |
| 924 | EXPECT_EQ(ids_count, 0); |
| 925 | EXPECT_EQ(lists_count, 0); |
| 926 | EXPECT_EQ(lists_sizes[0], 0); |
| 927 | |
| 928 | vm_unlock(¤t_vm_locked); |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * Validate that a mix of a pending IPI and notifcations are correctly |
| 933 | * reported across vcpus. |
| 934 | */ |
| 935 | TEST_F(vm, vm_notifications_info_get_per_vcpu_all_vcpus_and_ipi) |
| 936 | { |
Daniel Boulby | 6c2aa33 | 2024-11-13 13:54:08 +0000 | [diff] [blame^] | 937 | struct_vm *current_vm = vm_find_index(4); |
Daniel Boulby | 8be2651 | 2024-09-03 19:41:11 +0100 | [diff] [blame] | 938 | ffa_vcpu_count_t vcpu_count = current_vm->vcpu_count; |
| 939 | CHECK(vcpu_count > 1); |
| 940 | |
| 941 | struct vm_locked current_vm_locked = vm_lock(current_vm); |
| 942 | |
| 943 | /* |
| 944 | * Following set of variables that are also expected to be used when |
| 945 | * handling ffa_notification_info_get. |
| 946 | */ |
| 947 | const bool is_from_vm = false; |
| 948 | uint16_t ids[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 949 | uint32_t ids_count = 0; |
| 950 | uint32_t lists_sizes[FFA_NOTIFICATIONS_INFO_GET_MAX_IDS] = {0}; |
| 951 | uint32_t lists_count = 0; |
| 952 | enum notifications_info_get_state current_state = INIT; |
| 953 | struct vcpu *target_vcpu = vm_get_vcpu(current_vm, 0); |
| 954 | struct interrupts *interrupts = &target_vcpu->interrupts; |
| 955 | |
Daniel Boulby | 6c2aa33 | 2024-11-13 13:54:08 +0000 | [diff] [blame^] | 956 | target_vcpu->state = VCPU_STATE_WAITING; |
| 957 | |
Daniel Boulby | 8be2651 | 2024-09-03 19:41:11 +0100 | [diff] [blame] | 958 | vcpu_virt_interrupt_set_pending(interrupts, HF_IPI_INTID); |
| 959 | |
| 960 | for (unsigned int i = 1; i < vcpu_count; i++) { |
| 961 | vm_notifications_partition_set_pending( |
| 962 | current_vm_locked, is_from_vm, FFA_NOTIFICATION_MASK(i), |
| 963 | i, true); |
| 964 | } |
| 965 | |
| 966 | vm_notifications_info_get_pending(current_vm_locked, is_from_vm, ids, |
| 967 | &ids_count, lists_sizes, &lists_count, |
| 968 | FFA_NOTIFICATIONS_INFO_GET_MAX_IDS, |
| 969 | ¤t_state); |
| 970 | |
| 971 | /* |
| 972 | * This test has been conceived for the expected MAX_CPUS 4. |
| 973 | * All VCPUs have notifications of the same VM, to be broken down in 2 |
| 974 | * lists with 3 VCPU IDs, and 1 VCPU ID respectively. |
| 975 | * The list of IDs should look like: {<vm_id>, 0, 1, 2, <vm_id>, 3}. |
| 976 | */ |
| 977 | EXPECT_EQ(ids_count, 6U); |
| 978 | EXPECT_EQ(lists_count, 2U); |
| 979 | EXPECT_EQ(lists_sizes[0], 3); |
| 980 | EXPECT_EQ(lists_sizes[1], 1); |
| 981 | EXPECT_EQ(ids[0], current_vm->id); |
| 982 | EXPECT_EQ(ids[1], 0); |
| 983 | EXPECT_EQ(ids[2], 1); |
| 984 | EXPECT_EQ(ids[3], 2); |
| 985 | EXPECT_EQ(ids[4], current_vm->id); |
| 986 | EXPECT_EQ(ids[5], 3); |
| 987 | |
| 988 | vm_unlock(¤t_vm_locked); |
| 989 | } |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 990 | } /* namespace */ |