Madhukar Pappireddy | 457af4c | 2022-12-22 16:13:37 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2023 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 | |
Madhukar Pappireddy | 6642118 | 2022-12-22 16:50:09 -0600 | [diff] [blame] | 9 | #include "hf/arch/irq.h" |
Madhukar Pappireddy | ace2ff7 | 2023-05-03 15:45:06 -0500 | [diff] [blame] | 10 | #include "hf/arch/vm/delay.h" |
Madhukar Pappireddy | 6642118 | 2022-12-22 16:50:09 -0600 | [diff] [blame] | 11 | #include "hf/arch/vm/interrupts_gicv3.h" |
Madhukar Pappireddy | 6c23d43 | 2023-07-24 16:39:41 -0500 | [diff] [blame] | 12 | #include "hf/arch/vm/power_mgmt.h" |
Madhukar Pappireddy | 6642118 | 2022-12-22 16:50:09 -0600 | [diff] [blame] | 13 | #include "hf/arch/vm/timer.h" |
Madhukar Pappireddy | 4d16db6 | 2022-12-22 16:38:10 -0600 | [diff] [blame] | 14 | |
Madhukar Pappireddy | 457af4c | 2022-12-22 16:13:37 -0600 | [diff] [blame] | 15 | #include "ffa_secure_partitions.h" |
Madhukar Pappireddy | 6642118 | 2022-12-22 16:50:09 -0600 | [diff] [blame] | 16 | #include "gicv3.h" |
Madhukar Pappireddy | 457af4c | 2022-12-22 16:13:37 -0600 | [diff] [blame] | 17 | #include "partition_services.h" |
Madhukar Pappireddy | 9a1b9c0 | 2024-01-04 17:14:16 -0600 | [diff] [blame^] | 18 | #include "sp805.h" |
Madhukar Pappireddy | 457af4c | 2022-12-22 16:13:37 -0600 | [diff] [blame] | 19 | #include "sp_helpers.h" |
| 20 | |
| 21 | #define SP_SLEEP_TIME 400U |
Madhukar Pappireddy | 4d16db6 | 2022-12-22 16:38:10 -0600 | [diff] [blame] | 22 | #define NS_SLEEP_TIME 200U |
| 23 | |
Madhukar Pappireddy | 6c23d43 | 2023-07-24 16:39:41 -0500 | [diff] [blame] | 24 | #define LAST_SECONDARY_VCPU_ID (MAX_CPUS - 1) |
| 25 | #define MID_SECONDARY_VCPU_ID (MAX_CPUS / 2) |
| 26 | |
| 27 | alignas(4096) static uint8_t secondary_ec_stack[MAX_CPUS - 1][PAGE_SIZE]; |
| 28 | |
| 29 | struct secondary_cpu_entry_args { |
| 30 | ffa_id_t receiver_id; |
| 31 | ffa_vcpu_count_t vcpu_count; |
| 32 | ffa_vcpu_index_t vcpu_id; |
| 33 | struct spinlock lock; |
| 34 | ffa_vcpu_index_t target_vcpu_id; |
| 35 | }; |
| 36 | |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 37 | static void configure_trusted_wdog_interrupt(ffa_id_t source, ffa_id_t dest, |
| 38 | bool enable) |
Madhukar Pappireddy | 457af4c | 2022-12-22 16:13:37 -0600 | [diff] [blame] | 39 | { |
| 40 | struct ffa_value res; |
| 41 | |
| 42 | res = sp_virtual_interrupt_cmd_send(source, dest, IRQ_TWDOG_INTID, |
| 43 | enable, 0); |
| 44 | |
| 45 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 46 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 47 | } |
| 48 | |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 49 | static void enable_trusted_wdog_interrupt(ffa_id_t source, ffa_id_t dest) |
Madhukar Pappireddy | 457af4c | 2022-12-22 16:13:37 -0600 | [diff] [blame] | 50 | { |
| 51 | configure_trusted_wdog_interrupt(source, dest, true); |
| 52 | } |
| 53 | |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 54 | static void disable_trusted_wdog_interrupt(ffa_id_t source, ffa_id_t dest) |
Madhukar Pappireddy | 457af4c | 2022-12-22 16:13:37 -0600 | [diff] [blame] | 55 | { |
| 56 | configure_trusted_wdog_interrupt(source, dest, false); |
| 57 | } |
| 58 | |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 59 | static void enable_trigger_trusted_wdog_timer(ffa_id_t own_id, |
| 60 | ffa_id_t receiver_id, |
Madhukar Pappireddy | 457af4c | 2022-12-22 16:13:37 -0600 | [diff] [blame] | 61 | uint32_t timer_ms) |
| 62 | { |
| 63 | struct ffa_value res; |
| 64 | |
| 65 | /* Enable trusted watchdog interrupt as vIRQ in the secure side. */ |
| 66 | enable_trusted_wdog_interrupt(own_id, receiver_id); |
| 67 | |
| 68 | res = sp_twdog_map_cmd_send(own_id, receiver_id); |
| 69 | |
| 70 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 71 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 72 | |
| 73 | /* |
| 74 | * Send a message to the SP through direct messaging requesting it to |
| 75 | * start the trusted watchdog timer. |
| 76 | */ |
| 77 | res = sp_twdog_cmd_send(own_id, receiver_id, timer_ms); |
| 78 | |
| 79 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 80 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 81 | } |
| 82 | |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 83 | static void check_and_disable_trusted_wdog_timer(ffa_id_t own_id, |
| 84 | ffa_id_t receiver_id) |
Madhukar Pappireddy | 457af4c | 2022-12-22 16:13:37 -0600 | [diff] [blame] | 85 | { |
| 86 | struct ffa_value res; |
| 87 | |
| 88 | /* Check for the last serviced secure virtual interrupt. */ |
| 89 | res = sp_get_last_interrupt_cmd_send(own_id, receiver_id); |
| 90 | |
| 91 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 92 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 93 | |
| 94 | /* Make sure Trusted Watchdog timer interrupt was serviced. */ |
| 95 | EXPECT_EQ(sp_resp_value(res), IRQ_TWDOG_INTID); |
| 96 | |
| 97 | /* Disable Trusted Watchdog interrupt. */ |
| 98 | disable_trusted_wdog_interrupt(own_id, receiver_id); |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Test secure interrupt handling while the Secure Partition is in RUNNING |
| 103 | * state. |
| 104 | */ |
| 105 | TEST(secure_interrupts, sp_running) |
| 106 | { |
| 107 | struct ffa_value res; |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 108 | ffa_id_t own_id = hf_vm_get_id(); |
J-Alves | 5d6926a | 2022-12-08 14:44:28 +0000 | [diff] [blame] | 109 | struct mailbox_buffers mb = set_up_mailbox(); |
| 110 | struct ffa_partition_info *service2_info = service2(mb.recv); |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 111 | const ffa_id_t receiver_id = service2_info->vm_id; |
Madhukar Pappireddy | 457af4c | 2022-12-22 16:13:37 -0600 | [diff] [blame] | 112 | |
| 113 | enable_trigger_trusted_wdog_timer(own_id, receiver_id, 400); |
| 114 | |
| 115 | /* Send request to the SP to sleep. */ |
Madhukar Pappireddy | 3aafcb6 | 2023-12-06 11:49:04 -0600 | [diff] [blame] | 116 | res = sp_sleep_cmd_send(own_id, receiver_id, SP_SLEEP_TIME, 0); |
Madhukar Pappireddy | 457af4c | 2022-12-22 16:13:37 -0600 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * Secure interrupt should trigger during this time, SP will handle the |
| 120 | * trusted watchdog timer interrupt. |
| 121 | */ |
| 122 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 123 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 124 | |
| 125 | /* Make sure elapsed time not less than sleep time. */ |
| 126 | EXPECT_GE(sp_resp_value(res), SP_SLEEP_TIME); |
| 127 | |
| 128 | check_and_disable_trusted_wdog_timer(own_id, receiver_id); |
| 129 | } |
Madhukar Pappireddy | 4d16db6 | 2022-12-22 16:38:10 -0600 | [diff] [blame] | 130 | |
| 131 | /* |
Madhukar Pappireddy | ed7dee8 | 2023-12-06 11:55:15 -0600 | [diff] [blame] | 132 | * Test secure interrupt handling while the Secure Partition runs with |
| 133 | * interrupts potentially masked. This test helps to validate the functionality |
| 134 | * of the SPMC to intercept a direct response message if there are pending |
| 135 | * virtual secure interrupts and reschedule the partition to handle the pending |
| 136 | * interrupt. |
| 137 | */ |
| 138 | TEST(secure_interrupts, sp_direct_response_intercepted) |
| 139 | { |
| 140 | struct ffa_value res; |
| 141 | ffa_id_t own_id = hf_vm_get_id(); |
| 142 | struct mailbox_buffers mb = set_up_mailbox(); |
| 143 | struct ffa_partition_info *service2_info = service2(mb.recv); |
| 144 | const ffa_id_t receiver_id = service2_info->vm_id; |
| 145 | |
| 146 | enable_trigger_trusted_wdog_timer(own_id, receiver_id, 400); |
| 147 | |
| 148 | /* Send request to the SP to sleep uninterrupted. */ |
| 149 | res = sp_sleep_cmd_send(own_id, receiver_id, SP_SLEEP_TIME, 1); |
| 150 | |
| 151 | /* |
| 152 | * Secure interrupt should trigger during this time, SP will handle the |
| 153 | * trusted watchdog timer interrupt. |
| 154 | */ |
| 155 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 156 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 157 | |
| 158 | /* Make sure elapsed time not less than sleep time. */ |
| 159 | EXPECT_GE(sp_resp_value(res), SP_SLEEP_TIME); |
| 160 | |
| 161 | check_and_disable_trusted_wdog_timer(own_id, receiver_id); |
| 162 | } |
| 163 | |
| 164 | /* |
Madhukar Pappireddy | 4d16db6 | 2022-12-22 16:38:10 -0600 | [diff] [blame] | 165 | * Test secure interrupt handling while the Secure Partition is in WAITING |
| 166 | * state. |
| 167 | */ |
| 168 | TEST(secure_interrupts, sp_waiting) |
| 169 | { |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 170 | ffa_id_t own_id = hf_vm_get_id(); |
J-Alves | 5d6926a | 2022-12-08 14:44:28 +0000 | [diff] [blame] | 171 | struct mailbox_buffers mb = set_up_mailbox(); |
| 172 | struct ffa_partition_info *service2_info = service2(mb.recv); |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 173 | const ffa_id_t receiver_id = service2_info->vm_id; |
Madhukar Pappireddy | 4d16db6 | 2022-12-22 16:38:10 -0600 | [diff] [blame] | 174 | uint64_t time1; |
| 175 | volatile uint64_t time_lapsed; |
| 176 | uint64_t timer_freq = read_msr(cntfrq_el0); |
| 177 | |
| 178 | enable_trigger_trusted_wdog_timer(own_id, receiver_id, 100); |
| 179 | time1 = syscounter_read(); |
| 180 | |
| 181 | /* |
| 182 | * Sleep for NS_SLEEP_TIME ms. This ensures secure wdog timer triggers |
| 183 | * during this time. |
| 184 | */ |
| 185 | waitms(NS_SLEEP_TIME); |
| 186 | |
| 187 | /* Lapsed time should be at least equal to sleep time. */ |
| 188 | time_lapsed = ((syscounter_read() - time1) * 1000) / timer_freq; |
| 189 | |
| 190 | EXPECT_GE(time_lapsed, NS_SLEEP_TIME); |
| 191 | |
| 192 | check_and_disable_trusted_wdog_timer(own_id, receiver_id); |
| 193 | } |
Madhukar Pappireddy | 8cc6deb | 2022-12-22 16:46:04 -0600 | [diff] [blame] | 194 | |
| 195 | /* |
| 196 | * Test secure interrupt handling while the Secure Partition is in BLOCKED |
| 197 | * state. |
| 198 | */ |
| 199 | TEST(secure_interrupts, sp_blocked) |
| 200 | { |
| 201 | struct ffa_value res; |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 202 | ffa_id_t own_id = hf_vm_get_id(); |
J-Alves | 5d6926a | 2022-12-08 14:44:28 +0000 | [diff] [blame] | 203 | struct mailbox_buffers mb = set_up_mailbox(); |
| 204 | struct ffa_partition_info *service1_info = service1(mb.recv); |
| 205 | struct ffa_partition_info *service2_info = service2(mb.recv); |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 206 | const ffa_id_t receiver_id = service2_info->vm_id; |
| 207 | const ffa_id_t companion_id = service1_info->vm_id; |
Madhukar Pappireddy | 8cc6deb | 2022-12-22 16:46:04 -0600 | [diff] [blame] | 208 | |
| 209 | enable_trigger_trusted_wdog_timer(own_id, receiver_id, 400); |
| 210 | |
| 211 | /* |
| 212 | * Send command to receiver SP to send command to companion SP to sleep |
| 213 | * there by putting receiver SP in BLOCKED state. |
| 214 | */ |
| 215 | res = sp_fwd_sleep_cmd_send(own_id, receiver_id, companion_id, |
| 216 | SP_SLEEP_TIME, false); |
| 217 | |
| 218 | /* |
| 219 | * Secure interrupt should trigger during this time, receiver SP will |
| 220 | * handle the trusted watchdog timer and sends direct response message. |
| 221 | */ |
| 222 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 223 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 224 | |
| 225 | check_and_disable_trusted_wdog_timer(own_id, receiver_id); |
| 226 | } |
Madhukar Pappireddy | 6642118 | 2022-12-22 16:50:09 -0600 | [diff] [blame] | 227 | |
| 228 | TEST(secure_interrupts, sp_preempted) |
| 229 | { |
| 230 | struct ffa_value res; |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 231 | ffa_id_t own_id = hf_vm_get_id(); |
J-Alves | 5d6926a | 2022-12-08 14:44:28 +0000 | [diff] [blame] | 232 | struct mailbox_buffers mb = set_up_mailbox(); |
| 233 | struct ffa_partition_info *service2_info = service2(mb.recv); |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 234 | const ffa_id_t receiver_id = service2_info->vm_id; |
Madhukar Pappireddy | 6642118 | 2022-12-22 16:50:09 -0600 | [diff] [blame] | 235 | |
| 236 | gicv3_system_setup(); |
| 237 | interrupt_enable(PHYSICAL_TIMER_IRQ, true); |
| 238 | interrupt_set_priority(PHYSICAL_TIMER_IRQ, 0x80); |
| 239 | interrupt_set_edge_triggered(PHYSICAL_TIMER_IRQ, true); |
| 240 | interrupt_set_priority_mask(0xff); |
| 241 | arch_irq_enable(); |
| 242 | |
| 243 | /* Set physical timer for 20 ms and enable. */ |
| 244 | write_msr(CNTP_TVAL_EL0, ns_to_ticks(20000000)); |
| 245 | write_msr(CNTP_CTL_EL0, CNTx_CTL_ENABLE_MASK); |
| 246 | |
| 247 | enable_trigger_trusted_wdog_timer(own_id, receiver_id, 200); |
| 248 | |
| 249 | /* Send request to receiver SP to sleep. */ |
Madhukar Pappireddy | 3aafcb6 | 2023-12-06 11:49:04 -0600 | [diff] [blame] | 250 | res = sp_sleep_cmd_send(own_id, receiver_id, 50, 0); |
Madhukar Pappireddy | 6642118 | 2022-12-22 16:50:09 -0600 | [diff] [blame] | 251 | |
| 252 | /* SP is pre-empted by the non-secure timer interrupt. */ |
| 253 | EXPECT_EQ(res.func, FFA_INTERRUPT_32); |
| 254 | |
| 255 | /* VM id/vCPU index are passed through arg1. */ |
| 256 | EXPECT_EQ(res.arg1, ffa_vm_vcpu(receiver_id, 0)); |
| 257 | |
| 258 | /* Waiting for interrupt to be serviced in normal world. */ |
| 259 | while (last_interrupt_id == 0) { |
| 260 | EXPECT_EQ(io_read32_array(GICD_ISPENDR, 0), 0); |
| 261 | EXPECT_EQ(io_read32(GICR_ISPENDR0), 0); |
| 262 | EXPECT_EQ(io_read32_array(GICD_ISACTIVER, 0), 0); |
| 263 | EXPECT_EQ(io_read32(GICR_ISACTIVER0), 0); |
| 264 | } |
| 265 | |
| 266 | /* Check that we got the interrupt. */ |
| 267 | EXPECT_EQ(last_interrupt_id, PHYSICAL_TIMER_IRQ); |
| 268 | |
| 269 | /* Check timer status. */ |
| 270 | EXPECT_EQ(read_msr(CNTP_CTL_EL0), |
| 271 | CNTx_CTL_ISTS_MASK | CNTx_CTL_ENABLE_MASK); |
| 272 | |
| 273 | /* |
| 274 | * NS Interrupt has been serviced and receiver SP is now in PREEMPTED |
| 275 | * state. Wait for trusted watchdog timer to be fired. SPMC queues |
| 276 | * the secure virtual interrupt. |
| 277 | */ |
| 278 | waitms(NS_SLEEP_TIME); |
| 279 | |
| 280 | /* |
| 281 | * Resume the SP to complete the busy loop, handle the secure virtual |
| 282 | * interrupt and return with success. |
| 283 | */ |
| 284 | res = ffa_run(ffa_vm_id(res), ffa_vcpu_index(res)); |
| 285 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 286 | EXPECT_EQ(res.arg3, SP_SUCCESS); |
| 287 | |
| 288 | check_and_disable_trusted_wdog_timer(own_id, receiver_id); |
| 289 | } |
Madhukar Pappireddy | 7adccf7 | 2023-01-31 15:16:47 -0600 | [diff] [blame] | 290 | |
| 291 | /* |
| 292 | * Test Secure Partition runs to completion if it specifies action in response |
| 293 | * to Other-S Interrupt as queued. |
| 294 | */ |
| 295 | TEST(secure_interrupts, sp_other_s_interrupt_queued) |
| 296 | { |
| 297 | struct ffa_value res; |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 298 | ffa_id_t own_id = hf_vm_get_id(); |
Madhukar Pappireddy | 7adccf7 | 2023-01-31 15:16:47 -0600 | [diff] [blame] | 299 | struct mailbox_buffers mb = set_up_mailbox(); |
| 300 | struct ffa_partition_info *service2_info = service2(mb.recv); |
| 301 | struct ffa_partition_info *service3_info = service3(mb.recv); |
| 302 | |
| 303 | /* |
| 304 | * Service2 SP is the target of trusted watchdog timer interrupt. |
| 305 | * Service3 SP specified action to Other-S Interrupt as queued. |
| 306 | */ |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 307 | const ffa_id_t target_id = service2_info->vm_id; |
| 308 | const ffa_id_t receiver_id = service3_info->vm_id; |
Madhukar Pappireddy | 7adccf7 | 2023-01-31 15:16:47 -0600 | [diff] [blame] | 309 | |
| 310 | enable_trigger_trusted_wdog_timer(own_id, target_id, 400); |
| 311 | |
| 312 | /* |
| 313 | * Send command to receiver SP(Service3) to sleep for SP_SLEEP_TIME |
| 314 | * ms. Secure interrupt should trigger while SP is busy in running the |
| 315 | * sleep command. SPMC queues the virtual interrupt and resumes the |
| 316 | * SP. |
| 317 | */ |
Madhukar Pappireddy | 3aafcb6 | 2023-12-06 11:49:04 -0600 | [diff] [blame] | 318 | res = sp_sleep_cmd_send(own_id, receiver_id, SP_SLEEP_TIME, 0); |
Madhukar Pappireddy | 7adccf7 | 2023-01-31 15:16:47 -0600 | [diff] [blame] | 319 | |
| 320 | /* Service3 SP finishes and sends direct response back. */ |
| 321 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 322 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 323 | |
| 324 | /* |
| 325 | * Allocate cycles to target SP for it to handle the virtual secure |
| 326 | * interrupt. |
| 327 | */ |
Madhukar Pappireddy | 3aafcb6 | 2023-12-06 11:49:04 -0600 | [diff] [blame] | 328 | res = sp_sleep_cmd_send(own_id, target_id, 10, 0); |
Madhukar Pappireddy | 7adccf7 | 2023-01-31 15:16:47 -0600 | [diff] [blame] | 329 | |
| 330 | /* |
| 331 | * Secure interrupt should trigger during this time, SP will handle the |
| 332 | * trusted watchdog timer interrupt. |
| 333 | */ |
| 334 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 335 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 336 | |
| 337 | /* |
| 338 | * Check if the trusted watchdog timer interrupt has been handled. |
| 339 | */ |
| 340 | check_and_disable_trusted_wdog_timer(own_id, target_id); |
| 341 | } |
Madhukar Pappireddy | ef06276 | 2023-05-23 17:46:57 -0500 | [diff] [blame] | 342 | |
| 343 | /* |
| 344 | * Test that an SP can attempt to yield CPU cycles while handling secure |
| 345 | * interrupt by invoking FFA_YIELD. |
| 346 | */ |
| 347 | TEST(secure_interrupts, sp_yield_sec_interrupt_handling) |
| 348 | { |
| 349 | struct ffa_value res; |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 350 | ffa_id_t own_id = hf_vm_get_id(); |
Madhukar Pappireddy | ef06276 | 2023-05-23 17:46:57 -0500 | [diff] [blame] | 351 | struct mailbox_buffers mb = set_up_mailbox(); |
| 352 | struct ffa_partition_info *service2_info = service2(mb.recv); |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 353 | const ffa_id_t receiver_id = service2_info->vm_id; |
Madhukar Pappireddy | ef06276 | 2023-05-23 17:46:57 -0500 | [diff] [blame] | 354 | uint64_t time1; |
| 355 | volatile uint64_t time_lapsed; |
| 356 | uint64_t timer_freq = read_msr(cntfrq_el0); |
| 357 | |
| 358 | /* |
| 359 | * Send command to SP asking it attempt to yield cycles while handling |
| 360 | * secure interrupt. |
| 361 | */ |
| 362 | res = sp_yield_secure_interrupt_handling_cmd_send(own_id, receiver_id, |
| 363 | true); |
| 364 | |
| 365 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 366 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 367 | |
| 368 | enable_trigger_trusted_wdog_timer(own_id, receiver_id, 75); |
| 369 | time1 = syscounter_read(); |
| 370 | |
| 371 | /* |
| 372 | * Sleep for 100ms. This ensures secure wdog timer triggers |
| 373 | * during this time. SP starts handling secure interrupt but attempts |
| 374 | * to yields cycles. However, SPMC just resumes the SP to complete |
| 375 | * interrupt handling. |
| 376 | */ |
| 377 | waitms(100); |
| 378 | |
| 379 | /* Lapsed time should be at least equal to sleep time. */ |
| 380 | time_lapsed = ((syscounter_read() - time1) * 1000) / timer_freq; |
| 381 | |
| 382 | EXPECT_GE(time_lapsed, 100); |
| 383 | |
| 384 | res = sp_yield_secure_interrupt_handling_cmd_send(own_id, receiver_id, |
| 385 | false); |
| 386 | |
| 387 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 388 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 389 | check_and_disable_trusted_wdog_timer(own_id, receiver_id); |
| 390 | } |
Madhukar Pappireddy | 6c23d43 | 2023-07-24 16:39:41 -0500 | [diff] [blame] | 391 | |
| 392 | static void cpu_entry_sp_sleep_loop(uintptr_t arg) |
| 393 | { |
| 394 | ffa_id_t own_id = hf_vm_get_id(); |
| 395 | struct ffa_value res; |
| 396 | struct secondary_cpu_entry_args *args = |
| 397 | // NOLINTNEXTLINE(performance-no-int-to-ptr) |
| 398 | (struct secondary_cpu_entry_args *)arg; |
| 399 | bool is_receiver_up_sp = args->vcpu_count == 1; |
| 400 | |
| 401 | /* |
| 402 | * Execution context(s) of secondary Secure Partitions need CPU cycles |
| 403 | * to be allocated through FFA_RUN interface to reach message loop. |
| 404 | */ |
| 405 | if (is_receiver_up_sp) { |
| 406 | res = ffa_run(args->receiver_id, (ffa_vcpu_index_t)0); |
| 407 | } else { |
| 408 | res = ffa_run(args->receiver_id, args->vcpu_id); |
| 409 | } |
| 410 | |
| 411 | EXPECT_EQ(ffa_func_id(res), FFA_MSG_WAIT_32); |
| 412 | |
| 413 | /* Prepare for the trusted watchdog interrupt routed to target vCPU. */ |
| 414 | if (args->vcpu_id == args->target_vcpu_id) { |
| 415 | res = sp_route_interrupt_to_target_vcpu_cmd_send( |
| 416 | own_id, args->receiver_id, args->target_vcpu_id, |
| 417 | IRQ_TWDOG_INTID); |
| 418 | |
| 419 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 420 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 421 | |
| 422 | /* |
| 423 | * Make sure that twdog timer triggers shortly before the |
| 424 | * sleep duration ends. |
| 425 | */ |
| 426 | enable_trigger_trusted_wdog_timer(own_id, args->receiver_id, |
| 427 | SP_SLEEP_TIME - 50); |
| 428 | } |
| 429 | |
| 430 | /* Send request to the SP to sleep. */ |
Madhukar Pappireddy | 3aafcb6 | 2023-12-06 11:49:04 -0600 | [diff] [blame] | 431 | res = sp_sleep_cmd_send(own_id, args->receiver_id, SP_SLEEP_TIME, 0); |
Madhukar Pappireddy | 6c23d43 | 2023-07-24 16:39:41 -0500 | [diff] [blame] | 432 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 433 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 434 | |
| 435 | /* Make sure elapsed time not less than sleep time. */ |
| 436 | EXPECT_GE(sp_resp_value(res), SP_SLEEP_TIME); |
| 437 | |
| 438 | /* Check for the last serviced secure virtual interrupt. */ |
| 439 | res = sp_get_last_interrupt_cmd_send(own_id, args->receiver_id); |
| 440 | |
| 441 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 442 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 443 | |
| 444 | /* |
| 445 | * Expect the target execution context of Service2 SP to handle the |
| 446 | * trusted watchdog interrupt succesfully. |
| 447 | */ |
| 448 | if (args->vcpu_id == args->target_vcpu_id) { |
| 449 | EXPECT_EQ(sp_resp_value(res), IRQ_TWDOG_INTID); |
| 450 | } else { |
| 451 | /* |
| 452 | * Make sure Trusted Watchdog timer interrupt was not serviced |
| 453 | * by this execution context. |
| 454 | */ |
| 455 | EXPECT_NE(sp_resp_value(res), IRQ_TWDOG_INTID); |
| 456 | } |
| 457 | |
| 458 | /* Clear last serviced secure virtual interrupt. */ |
| 459 | res = sp_clear_last_interrupt_cmd_send(own_id, args->receiver_id); |
| 460 | |
| 461 | EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32); |
| 462 | EXPECT_EQ(sp_resp(res), SP_SUCCESS); |
| 463 | |
| 464 | /* Releases the lock passed in. */ |
| 465 | sl_unlock(&args->lock); |
| 466 | arch_cpu_stop(); |
| 467 | } |
| 468 | |
| 469 | static void sp_route_interrupt_to_secondary_vcpu_base( |
| 470 | struct secondary_cpu_entry_args args) |
| 471 | { |
| 472 | /* Start secondary EC while holding lock. */ |
| 473 | sl_lock(&args.lock); |
| 474 | |
| 475 | for (ffa_vcpu_index_t i = 1; i < MAX_CPUS; i++) { |
| 476 | uintptr_t cpu_id; |
| 477 | ffa_vcpu_index_t hftest_cpu_index = MAX_CPUS - i; |
| 478 | |
| 479 | cpu_id = hftest_get_cpu_id(hftest_cpu_index); |
| 480 | args.vcpu_id = i; |
| 481 | HFTEST_LOG("Booting CPU %u - %x", i, cpu_id); |
| 482 | |
| 483 | EXPECT_EQ(hftest_cpu_start(cpu_id, secondary_ec_stack[i - 1], |
| 484 | sizeof(secondary_ec_stack[0]), |
| 485 | cpu_entry_sp_sleep_loop, |
| 486 | (uintptr_t)&args), |
| 487 | true); |
| 488 | |
| 489 | /* Wait for CPU to release the lock. */ |
| 490 | sl_lock(&args.lock); |
| 491 | |
| 492 | HFTEST_LOG("Done with CPU %u", i); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * Test a Secure Partition can request the SPMC to reconfigure an interrupt to |
| 498 | * be routed to a secondary vCPU. |
| 499 | */ |
| 500 | TEST(secure_interrupts, sp_route_interrupt_to_secondary_vcpu) |
| 501 | { |
| 502 | struct secondary_cpu_entry_args args = {.lock = SPINLOCK_INIT}; |
| 503 | struct mailbox_buffers mb = set_up_mailbox(); |
| 504 | struct ffa_partition_info *service2_info = service2(mb.recv); |
| 505 | const ffa_id_t receiver_id = service2_info->vm_id; |
| 506 | |
| 507 | args.receiver_id = receiver_id; |
| 508 | args.vcpu_count = service2_info->vcpu_count; |
| 509 | |
| 510 | /* |
| 511 | * Reconfigure the twdog interrupt to be routed to last secondary |
| 512 | * execution context of SP. |
| 513 | */ |
| 514 | args.target_vcpu_id = LAST_SECONDARY_VCPU_ID; |
| 515 | sp_route_interrupt_to_secondary_vcpu_base(args); |
| 516 | |
| 517 | /* |
| 518 | * Reconfigure the twdog interrupt to be routed to mid secondary |
| 519 | * execution context of SP. |
| 520 | */ |
| 521 | args.target_vcpu_id = MID_SECONDARY_VCPU_ID; |
| 522 | sp_route_interrupt_to_secondary_vcpu_base(args); |
| 523 | } |