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