blob: f3b984ca27686b6aff3d189fb764eb944d1e678c [file] [log] [blame]
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -06001/*
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 Pappireddy66421182022-12-22 16:50:09 -06009#include "hf/arch/irq.h"
Madhukar Pappireddyace2ff72023-05-03 15:45:06 -050010#include "hf/arch/vm/delay.h"
Madhukar Pappireddy66421182022-12-22 16:50:09 -060011#include "hf/arch/vm/interrupts_gicv3.h"
Madhukar Pappireddy6c23d432023-07-24 16:39:41 -050012#include "hf/arch/vm/power_mgmt.h"
Madhukar Pappireddy66421182022-12-22 16:50:09 -060013#include "hf/arch/vm/timer.h"
Madhukar Pappireddy4d16db62022-12-22 16:38:10 -060014
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060015#include "ffa_secure_partitions.h"
Madhukar Pappireddy66421182022-12-22 16:50:09 -060016#include "gicv3.h"
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060017#include "partition_services.h"
Madhukar Pappireddy9a1b9c02024-01-04 17:14:16 -060018#include "sp805.h"
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060019#include "sp_helpers.h"
20
21#define SP_SLEEP_TIME 400U
Madhukar Pappireddy4d16db62022-12-22 16:38:10 -060022#define NS_SLEEP_TIME 200U
23
Madhukar Pappireddy6c23d432023-07-24 16:39:41 -050024#define LAST_SECONDARY_VCPU_ID (MAX_CPUS - 1)
25#define MID_SECONDARY_VCPU_ID (MAX_CPUS / 2)
26
27alignas(4096) static uint8_t secondary_ec_stack[MAX_CPUS - 1][PAGE_SIZE];
28
29struct 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-Alves19e20cf2023-08-02 12:48:55 +010037static void configure_trusted_wdog_interrupt(ffa_id_t source, ffa_id_t dest,
38 bool enable)
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060039{
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-Alves19e20cf2023-08-02 12:48:55 +010049static void enable_trusted_wdog_interrupt(ffa_id_t source, ffa_id_t dest)
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060050{
51 configure_trusted_wdog_interrupt(source, dest, true);
52}
53
J-Alves19e20cf2023-08-02 12:48:55 +010054static void disable_trusted_wdog_interrupt(ffa_id_t source, ffa_id_t dest)
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060055{
56 configure_trusted_wdog_interrupt(source, dest, false);
57}
58
J-Alves19e20cf2023-08-02 12:48:55 +010059static void enable_trigger_trusted_wdog_timer(ffa_id_t own_id,
60 ffa_id_t receiver_id,
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060061 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-Alves19e20cf2023-08-02 12:48:55 +010083static void check_and_disable_trusted_wdog_timer(ffa_id_t own_id,
84 ffa_id_t receiver_id)
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -060085{
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 */
105TEST(secure_interrupts, sp_running)
106{
107 struct ffa_value res;
J-Alves19e20cf2023-08-02 12:48:55 +0100108 ffa_id_t own_id = hf_vm_get_id();
J-Alves5d6926a2022-12-08 14:44:28 +0000109 struct mailbox_buffers mb = set_up_mailbox();
110 struct ffa_partition_info *service2_info = service2(mb.recv);
J-Alves19e20cf2023-08-02 12:48:55 +0100111 const ffa_id_t receiver_id = service2_info->vm_id;
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -0600112
113 enable_trigger_trusted_wdog_timer(own_id, receiver_id, 400);
114
115 /* Send request to the SP to sleep. */
Madhukar Pappireddy3aafcb62023-12-06 11:49:04 -0600116 res = sp_sleep_cmd_send(own_id, receiver_id, SP_SLEEP_TIME, 0);
Madhukar Pappireddy457af4c2022-12-22 16:13:37 -0600117
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 Pappireddy4d16db62022-12-22 16:38:10 -0600130
131/*
Madhukar Pappireddyed7dee82023-12-06 11:55:15 -0600132 * 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 */
138TEST(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. */
Madhukar Pappireddyca2f92d2024-02-15 16:39:23 -0600149 res = sp_sleep_cmd_send(own_id, receiver_id, SP_SLEEP_TIME,
150 OPTIONS_MASK_INTERRUPTS);
Madhukar Pappireddyed7dee82023-12-06 11:55:15 -0600151
152 /*
153 * Secure interrupt should trigger during this time, SP will handle the
154 * trusted watchdog timer interrupt.
155 */
156 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
157 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
158
159 /* Make sure elapsed time not less than sleep time. */
160 EXPECT_GE(sp_resp_value(res), SP_SLEEP_TIME);
161
162 check_and_disable_trusted_wdog_timer(own_id, receiver_id);
163}
164
165/*
Madhukar Pappireddy4d16db62022-12-22 16:38:10 -0600166 * Test secure interrupt handling while the Secure Partition is in WAITING
167 * state.
168 */
169TEST(secure_interrupts, sp_waiting)
170{
J-Alves19e20cf2023-08-02 12:48:55 +0100171 ffa_id_t own_id = hf_vm_get_id();
J-Alves5d6926a2022-12-08 14:44:28 +0000172 struct mailbox_buffers mb = set_up_mailbox();
173 struct ffa_partition_info *service2_info = service2(mb.recv);
J-Alves19e20cf2023-08-02 12:48:55 +0100174 const ffa_id_t receiver_id = service2_info->vm_id;
Madhukar Pappireddy4d16db62022-12-22 16:38:10 -0600175 uint64_t time1;
176 volatile uint64_t time_lapsed;
177 uint64_t timer_freq = read_msr(cntfrq_el0);
178
179 enable_trigger_trusted_wdog_timer(own_id, receiver_id, 100);
180 time1 = syscounter_read();
181
182 /*
183 * Sleep for NS_SLEEP_TIME ms. This ensures secure wdog timer triggers
184 * during this time.
185 */
186 waitms(NS_SLEEP_TIME);
187
188 /* Lapsed time should be at least equal to sleep time. */
189 time_lapsed = ((syscounter_read() - time1) * 1000) / timer_freq;
190
191 EXPECT_GE(time_lapsed, NS_SLEEP_TIME);
192
193 check_and_disable_trusted_wdog_timer(own_id, receiver_id);
194}
Madhukar Pappireddy8cc6deb2022-12-22 16:46:04 -0600195
196/*
197 * Test secure interrupt handling while the Secure Partition is in BLOCKED
198 * state.
199 */
200TEST(secure_interrupts, sp_blocked)
201{
202 struct ffa_value res;
J-Alves19e20cf2023-08-02 12:48:55 +0100203 ffa_id_t own_id = hf_vm_get_id();
J-Alves5d6926a2022-12-08 14:44:28 +0000204 struct mailbox_buffers mb = set_up_mailbox();
205 struct ffa_partition_info *service1_info = service1(mb.recv);
206 struct ffa_partition_info *service2_info = service2(mb.recv);
J-Alves19e20cf2023-08-02 12:48:55 +0100207 const ffa_id_t receiver_id = service2_info->vm_id;
208 const ffa_id_t companion_id = service1_info->vm_id;
Madhukar Pappireddy8cc6deb2022-12-22 16:46:04 -0600209
210 enable_trigger_trusted_wdog_timer(own_id, receiver_id, 400);
211
212 /*
213 * Send command to receiver SP to send command to companion SP to sleep
214 * there by putting receiver SP in BLOCKED state.
215 */
216 res = sp_fwd_sleep_cmd_send(own_id, receiver_id, companion_id,
Madhukar Pappireddyca2f92d2024-02-15 16:39:23 -0600217 SP_SLEEP_TIME, 0);
Madhukar Pappireddy8cc6deb2022-12-22 16:46:04 -0600218
219 /*
220 * Secure interrupt should trigger during this time, receiver SP will
221 * handle the trusted watchdog timer and sends direct response message.
222 */
223 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
224 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
225
226 check_and_disable_trusted_wdog_timer(own_id, receiver_id);
227}
Madhukar Pappireddy66421182022-12-22 16:50:09 -0600228
229TEST(secure_interrupts, sp_preempted)
230{
231 struct ffa_value res;
J-Alves19e20cf2023-08-02 12:48:55 +0100232 ffa_id_t own_id = hf_vm_get_id();
J-Alves5d6926a2022-12-08 14:44:28 +0000233 struct mailbox_buffers mb = set_up_mailbox();
234 struct ffa_partition_info *service2_info = service2(mb.recv);
J-Alves19e20cf2023-08-02 12:48:55 +0100235 const ffa_id_t receiver_id = service2_info->vm_id;
Madhukar Pappireddy66421182022-12-22 16:50:09 -0600236
237 gicv3_system_setup();
238 interrupt_enable(PHYSICAL_TIMER_IRQ, true);
239 interrupt_set_priority(PHYSICAL_TIMER_IRQ, 0x80);
240 interrupt_set_edge_triggered(PHYSICAL_TIMER_IRQ, true);
241 interrupt_set_priority_mask(0xff);
242 arch_irq_enable();
243
244 /* Set physical timer for 20 ms and enable. */
245 write_msr(CNTP_TVAL_EL0, ns_to_ticks(20000000));
246 write_msr(CNTP_CTL_EL0, CNTx_CTL_ENABLE_MASK);
247
248 enable_trigger_trusted_wdog_timer(own_id, receiver_id, 200);
249
250 /* Send request to receiver SP to sleep. */
Madhukar Pappireddy3aafcb62023-12-06 11:49:04 -0600251 res = sp_sleep_cmd_send(own_id, receiver_id, 50, 0);
Madhukar Pappireddy66421182022-12-22 16:50:09 -0600252
253 /* SP is pre-empted by the non-secure timer interrupt. */
254 EXPECT_EQ(res.func, FFA_INTERRUPT_32);
255
256 /* VM id/vCPU index are passed through arg1. */
257 EXPECT_EQ(res.arg1, ffa_vm_vcpu(receiver_id, 0));
258
259 /* Waiting for interrupt to be serviced in normal world. */
260 while (last_interrupt_id == 0) {
261 EXPECT_EQ(io_read32_array(GICD_ISPENDR, 0), 0);
262 EXPECT_EQ(io_read32(GICR_ISPENDR0), 0);
263 EXPECT_EQ(io_read32_array(GICD_ISACTIVER, 0), 0);
264 EXPECT_EQ(io_read32(GICR_ISACTIVER0), 0);
265 }
266
267 /* Check that we got the interrupt. */
268 EXPECT_EQ(last_interrupt_id, PHYSICAL_TIMER_IRQ);
269
270 /* Check timer status. */
271 EXPECT_EQ(read_msr(CNTP_CTL_EL0),
272 CNTx_CTL_ISTS_MASK | CNTx_CTL_ENABLE_MASK);
273
274 /*
275 * NS Interrupt has been serviced and receiver SP is now in PREEMPTED
276 * state. Wait for trusted watchdog timer to be fired. SPMC queues
277 * the secure virtual interrupt.
278 */
279 waitms(NS_SLEEP_TIME);
280
281 /*
282 * Resume the SP to complete the busy loop, handle the secure virtual
283 * interrupt and return with success.
284 */
285 res = ffa_run(ffa_vm_id(res), ffa_vcpu_index(res));
286 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
287 EXPECT_EQ(res.arg3, SP_SUCCESS);
288
289 check_and_disable_trusted_wdog_timer(own_id, receiver_id);
290}
Madhukar Pappireddy7adccf72023-01-31 15:16:47 -0600291
292/*
293 * Test Secure Partition runs to completion if it specifies action in response
294 * to Other-S Interrupt as queued.
295 */
296TEST(secure_interrupts, sp_other_s_interrupt_queued)
297{
298 struct ffa_value res;
J-Alves19e20cf2023-08-02 12:48:55 +0100299 ffa_id_t own_id = hf_vm_get_id();
Madhukar Pappireddy7adccf72023-01-31 15:16:47 -0600300 struct mailbox_buffers mb = set_up_mailbox();
301 struct ffa_partition_info *service2_info = service2(mb.recv);
302 struct ffa_partition_info *service3_info = service3(mb.recv);
303
304 /*
305 * Service2 SP is the target of trusted watchdog timer interrupt.
306 * Service3 SP specified action to Other-S Interrupt as queued.
307 */
J-Alves19e20cf2023-08-02 12:48:55 +0100308 const ffa_id_t target_id = service2_info->vm_id;
309 const ffa_id_t receiver_id = service3_info->vm_id;
Madhukar Pappireddy7adccf72023-01-31 15:16:47 -0600310
311 enable_trigger_trusted_wdog_timer(own_id, target_id, 400);
312
313 /*
314 * Send command to receiver SP(Service3) to sleep for SP_SLEEP_TIME
315 * ms. Secure interrupt should trigger while SP is busy in running the
316 * sleep command. SPMC queues the virtual interrupt and resumes the
317 * SP.
318 */
Madhukar Pappireddy3aafcb62023-12-06 11:49:04 -0600319 res = sp_sleep_cmd_send(own_id, receiver_id, SP_SLEEP_TIME, 0);
Madhukar Pappireddy7adccf72023-01-31 15:16:47 -0600320
321 /* Service3 SP finishes and sends direct response back. */
322 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
323 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
324
325 /*
326 * Allocate cycles to target SP for it to handle the virtual secure
327 * interrupt.
328 */
Madhukar Pappireddy3aafcb62023-12-06 11:49:04 -0600329 res = sp_sleep_cmd_send(own_id, target_id, 10, 0);
Madhukar Pappireddy7adccf72023-01-31 15:16:47 -0600330
331 /*
332 * Secure interrupt should trigger during this time, SP will handle the
333 * trusted watchdog timer interrupt.
334 */
335 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
336 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
337
338 /*
339 * Check if the trusted watchdog timer interrupt has been handled.
340 */
341 check_and_disable_trusted_wdog_timer(own_id, target_id);
342}
Madhukar Pappireddyef062762023-05-23 17:46:57 -0500343
344/*
345 * Test that an SP can attempt to yield CPU cycles while handling secure
346 * interrupt by invoking FFA_YIELD.
347 */
348TEST(secure_interrupts, sp_yield_sec_interrupt_handling)
349{
350 struct ffa_value res;
J-Alves19e20cf2023-08-02 12:48:55 +0100351 ffa_id_t own_id = hf_vm_get_id();
Madhukar Pappireddyef062762023-05-23 17:46:57 -0500352 struct mailbox_buffers mb = set_up_mailbox();
353 struct ffa_partition_info *service2_info = service2(mb.recv);
J-Alves19e20cf2023-08-02 12:48:55 +0100354 const ffa_id_t receiver_id = service2_info->vm_id;
Madhukar Pappireddyef062762023-05-23 17:46:57 -0500355 uint64_t time1;
356 volatile uint64_t time_lapsed;
357 uint64_t timer_freq = read_msr(cntfrq_el0);
358
359 /*
360 * Send command to SP asking it attempt to yield cycles while handling
361 * secure interrupt.
362 */
363 res = sp_yield_secure_interrupt_handling_cmd_send(own_id, receiver_id,
364 true);
365
366 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
367 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
368
369 enable_trigger_trusted_wdog_timer(own_id, receiver_id, 75);
370 time1 = syscounter_read();
371
372 /*
373 * Sleep for 100ms. This ensures secure wdog timer triggers
374 * during this time. SP starts handling secure interrupt but attempts
375 * to yields cycles. However, SPMC just resumes the SP to complete
376 * interrupt handling.
377 */
378 waitms(100);
379
380 /* Lapsed time should be at least equal to sleep time. */
381 time_lapsed = ((syscounter_read() - time1) * 1000) / timer_freq;
382
383 EXPECT_GE(time_lapsed, 100);
384
385 res = sp_yield_secure_interrupt_handling_cmd_send(own_id, receiver_id,
386 false);
387
388 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
389 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
390 check_and_disable_trusted_wdog_timer(own_id, receiver_id);
391}
Madhukar Pappireddy6c23d432023-07-24 16:39:41 -0500392
393static void cpu_entry_sp_sleep_loop(uintptr_t arg)
394{
395 ffa_id_t own_id = hf_vm_get_id();
396 struct ffa_value res;
397 struct secondary_cpu_entry_args *args =
398 // NOLINTNEXTLINE(performance-no-int-to-ptr)
399 (struct secondary_cpu_entry_args *)arg;
400 bool is_receiver_up_sp = args->vcpu_count == 1;
401
402 /*
403 * Execution context(s) of secondary Secure Partitions need CPU cycles
404 * to be allocated through FFA_RUN interface to reach message loop.
405 */
406 if (is_receiver_up_sp) {
407 res = ffa_run(args->receiver_id, (ffa_vcpu_index_t)0);
408 } else {
409 res = ffa_run(args->receiver_id, args->vcpu_id);
410 }
411
412 EXPECT_EQ(ffa_func_id(res), FFA_MSG_WAIT_32);
413
414 /* Prepare for the trusted watchdog interrupt routed to target vCPU. */
415 if (args->vcpu_id == args->target_vcpu_id) {
416 res = sp_route_interrupt_to_target_vcpu_cmd_send(
417 own_id, args->receiver_id, args->target_vcpu_id,
418 IRQ_TWDOG_INTID);
419
420 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
421 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
422
423 /*
424 * Make sure that twdog timer triggers shortly before the
425 * sleep duration ends.
426 */
427 enable_trigger_trusted_wdog_timer(own_id, args->receiver_id,
428 SP_SLEEP_TIME - 50);
429 }
430
431 /* Send request to the SP to sleep. */
Madhukar Pappireddy3aafcb62023-12-06 11:49:04 -0600432 res = sp_sleep_cmd_send(own_id, args->receiver_id, SP_SLEEP_TIME, 0);
Madhukar Pappireddy6c23d432023-07-24 16:39:41 -0500433 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
434 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
435
436 /* Make sure elapsed time not less than sleep time. */
437 EXPECT_GE(sp_resp_value(res), SP_SLEEP_TIME);
438
439 /* Check for the last serviced secure virtual interrupt. */
440 res = sp_get_last_interrupt_cmd_send(own_id, args->receiver_id);
441
442 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
443 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
444
445 /*
446 * Expect the target execution context of Service2 SP to handle the
447 * trusted watchdog interrupt succesfully.
448 */
449 if (args->vcpu_id == args->target_vcpu_id) {
450 EXPECT_EQ(sp_resp_value(res), IRQ_TWDOG_INTID);
451 } else {
452 /*
453 * Make sure Trusted Watchdog timer interrupt was not serviced
454 * by this execution context.
455 */
456 EXPECT_NE(sp_resp_value(res), IRQ_TWDOG_INTID);
457 }
458
459 /* Clear last serviced secure virtual interrupt. */
460 res = sp_clear_last_interrupt_cmd_send(own_id, args->receiver_id);
461
462 EXPECT_EQ(res.func, FFA_MSG_SEND_DIRECT_RESP_32);
463 EXPECT_EQ(sp_resp(res), SP_SUCCESS);
464
465 /* Releases the lock passed in. */
466 sl_unlock(&args->lock);
467 arch_cpu_stop();
468}
469
470static void sp_route_interrupt_to_secondary_vcpu_base(
471 struct secondary_cpu_entry_args args)
472{
473 /* Start secondary EC while holding lock. */
474 sl_lock(&args.lock);
475
476 for (ffa_vcpu_index_t i = 1; i < MAX_CPUS; i++) {
477 uintptr_t cpu_id;
478 ffa_vcpu_index_t hftest_cpu_index = MAX_CPUS - i;
479
480 cpu_id = hftest_get_cpu_id(hftest_cpu_index);
481 args.vcpu_id = i;
482 HFTEST_LOG("Booting CPU %u - %x", i, cpu_id);
483
484 EXPECT_EQ(hftest_cpu_start(cpu_id, secondary_ec_stack[i - 1],
485 sizeof(secondary_ec_stack[0]),
486 cpu_entry_sp_sleep_loop,
487 (uintptr_t)&args),
488 true);
489
490 /* Wait for CPU to release the lock. */
491 sl_lock(&args.lock);
492
493 HFTEST_LOG("Done with CPU %u", i);
494 }
495}
496
497/*
498 * Test a Secure Partition can request the SPMC to reconfigure an interrupt to
499 * be routed to a secondary vCPU.
500 */
501TEST(secure_interrupts, sp_route_interrupt_to_secondary_vcpu)
502{
503 struct secondary_cpu_entry_args args = {.lock = SPINLOCK_INIT};
504 struct mailbox_buffers mb = set_up_mailbox();
505 struct ffa_partition_info *service2_info = service2(mb.recv);
506 const ffa_id_t receiver_id = service2_info->vm_id;
507
508 args.receiver_id = receiver_id;
509 args.vcpu_count = service2_info->vcpu_count;
510
511 /*
512 * Reconfigure the twdog interrupt to be routed to last secondary
513 * execution context of SP.
514 */
515 args.target_vcpu_id = LAST_SECONDARY_VCPU_ID;
516 sp_route_interrupt_to_secondary_vcpu_base(args);
517
518 /*
519 * Reconfigure the twdog interrupt to be routed to mid secondary
520 * execution context of SP.
521 */
522 args.target_vcpu_id = MID_SECONDARY_VCPU_ID;
523 sp_route_interrupt_to_secondary_vcpu_base(args);
524}