xref: /aosp_15_r20/trusty/kernel/app/smptest/smptest.c (revision 344aa361028b423587d4ef3fa52a23d194628137)
1*344aa361SAndroid Build Coastguard Worker /*
2*344aa361SAndroid Build Coastguard Worker  * Copyright (c) 2018, Google Inc. All rights reserved
3*344aa361SAndroid Build Coastguard Worker  *
4*344aa361SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining
5*344aa361SAndroid Build Coastguard Worker  * a copy of this software and associated documentation files
6*344aa361SAndroid Build Coastguard Worker  * (the "Software"), to deal in the Software without restriction,
7*344aa361SAndroid Build Coastguard Worker  * including without limitation the rights to use, copy, modify, merge,
8*344aa361SAndroid Build Coastguard Worker  * publish, distribute, sublicense, and/or sell copies of the Software,
9*344aa361SAndroid Build Coastguard Worker  * and to permit persons to whom the Software is furnished to do so,
10*344aa361SAndroid Build Coastguard Worker  * subject to the following conditions:
11*344aa361SAndroid Build Coastguard Worker  *
12*344aa361SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice shall be
13*344aa361SAndroid Build Coastguard Worker  * included in all copies or substantial portions of the Software.
14*344aa361SAndroid Build Coastguard Worker  *
15*344aa361SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*344aa361SAndroid Build Coastguard Worker  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*344aa361SAndroid Build Coastguard Worker  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18*344aa361SAndroid Build Coastguard Worker  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19*344aa361SAndroid Build Coastguard Worker  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20*344aa361SAndroid Build Coastguard Worker  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21*344aa361SAndroid Build Coastguard Worker  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22*344aa361SAndroid Build Coastguard Worker  */
23*344aa361SAndroid Build Coastguard Worker 
24*344aa361SAndroid Build Coastguard Worker #include <kernel/mp.h>
25*344aa361SAndroid Build Coastguard Worker #include <kernel/thread.h>
26*344aa361SAndroid Build Coastguard Worker #include <lib/unittest/unittest.h>
27*344aa361SAndroid Build Coastguard Worker #include <lk/init.h>
28*344aa361SAndroid Build Coastguard Worker #include <stdbool.h>
29*344aa361SAndroid Build Coastguard Worker #include <stdio.h>
30*344aa361SAndroid Build Coastguard Worker 
31*344aa361SAndroid Build Coastguard Worker #define THREAD_DELAY_MS 1
32*344aa361SAndroid Build Coastguard Worker 
33*344aa361SAndroid Build Coastguard Worker #define SMPTEST_CYCLES 16
34*344aa361SAndroid Build Coastguard Worker 
35*344aa361SAndroid Build Coastguard Worker static struct smptest_thread {
36*344aa361SAndroid Build Coastguard Worker     thread_t* thread;
37*344aa361SAndroid Build Coastguard Worker 
38*344aa361SAndroid Build Coastguard Worker     volatile bool started;
39*344aa361SAndroid Build Coastguard Worker     volatile uint unblock_count;
40*344aa361SAndroid Build Coastguard Worker     volatile uint error_count;
41*344aa361SAndroid Build Coastguard Worker     volatile uint done_count;
42*344aa361SAndroid Build Coastguard Worker 
43*344aa361SAndroid Build Coastguard Worker } smptest_thread[SMP_MAX_CPUS];
44*344aa361SAndroid Build Coastguard Worker 
45*344aa361SAndroid Build Coastguard Worker /* Check if a thread is blocked, using volatile to ensure re-read */
thread_is_blocked(volatile thread_t * thread)46*344aa361SAndroid Build Coastguard Worker static bool thread_is_blocked(volatile thread_t* thread) {
47*344aa361SAndroid Build Coastguard Worker     return thread->state == THREAD_BLOCKED;
48*344aa361SAndroid Build Coastguard Worker }
49*344aa361SAndroid Build Coastguard Worker 
smptest_thread_func(void * arg)50*344aa361SAndroid Build Coastguard Worker static int smptest_thread_func(void* arg) {
51*344aa361SAndroid Build Coastguard Worker     const uint i = (uintptr_t)arg;
52*344aa361SAndroid Build Coastguard Worker     const uint expected_cpu = i;
53*344aa361SAndroid Build Coastguard Worker     struct smptest_thread* const smpt = &smptest_thread[i];
54*344aa361SAndroid Build Coastguard Worker 
55*344aa361SAndroid Build Coastguard Worker     /* Note thread as started so main thread sees which CPUs are available */
56*344aa361SAndroid Build Coastguard Worker     smpt->started = true;
57*344aa361SAndroid Build Coastguard Worker 
58*344aa361SAndroid Build Coastguard Worker     uint cpu = arch_curr_cpu_num();
59*344aa361SAndroid Build Coastguard Worker     if (cpu != expected_cpu) {
60*344aa361SAndroid Build Coastguard Worker         /* Warn if the thread starts on another CPU than it was pinned to */
61*344aa361SAndroid Build Coastguard Worker         printf("%s: thread %d started on wrong cpu: %d\n", __func__, i, cpu);
62*344aa361SAndroid Build Coastguard Worker         smpt->error_count++;
63*344aa361SAndroid Build Coastguard Worker     }
64*344aa361SAndroid Build Coastguard Worker 
65*344aa361SAndroid Build Coastguard Worker     while (true) {
66*344aa361SAndroid Build Coastguard Worker         THREAD_LOCK(state1);
67*344aa361SAndroid Build Coastguard Worker         get_current_thread()->state = THREAD_BLOCKED;
68*344aa361SAndroid Build Coastguard Worker         thread_block();
69*344aa361SAndroid Build Coastguard Worker 
70*344aa361SAndroid Build Coastguard Worker         cpu = arch_curr_cpu_num();
71*344aa361SAndroid Build Coastguard Worker         if (cpu != expected_cpu) {
72*344aa361SAndroid Build Coastguard Worker             /* Don't update any state if the thread runs on the wrong CPU. */
73*344aa361SAndroid Build Coastguard Worker             printf("%s: thread %d ran on wrong cpu: %d\n", __func__, i, cpu);
74*344aa361SAndroid Build Coastguard Worker             smpt->error_count++;
75*344aa361SAndroid Build Coastguard Worker             continue;
76*344aa361SAndroid Build Coastguard Worker         }
77*344aa361SAndroid Build Coastguard Worker 
78*344aa361SAndroid Build Coastguard Worker         /*
79*344aa361SAndroid Build Coastguard Worker          * Update unblock count for this cpu so the main test thread can see
80*344aa361SAndroid Build Coastguard Worker          * that it ran.
81*344aa361SAndroid Build Coastguard Worker          */
82*344aa361SAndroid Build Coastguard Worker         smpt->unblock_count++;
83*344aa361SAndroid Build Coastguard Worker         THREAD_UNLOCK(state1);
84*344aa361SAndroid Build Coastguard Worker 
85*344aa361SAndroid Build Coastguard Worker         /* Sleep to allow other threads to block */
86*344aa361SAndroid Build Coastguard Worker         thread_sleep(THREAD_DELAY_MS);
87*344aa361SAndroid Build Coastguard Worker 
88*344aa361SAndroid Build Coastguard Worker         THREAD_LOCK(state2);
89*344aa361SAndroid Build Coastguard Worker 
90*344aa361SAndroid Build Coastguard Worker         /* Find and unblock the next started cpu */
91*344aa361SAndroid Build Coastguard Worker         for (uint next_cpu = i + 1; next_cpu < SMP_MAX_CPUS; next_cpu++) {
92*344aa361SAndroid Build Coastguard Worker             if (smptest_thread[next_cpu].started) {
93*344aa361SAndroid Build Coastguard Worker                 thread_t* next = smptest_thread[next_cpu].thread;
94*344aa361SAndroid Build Coastguard Worker 
95*344aa361SAndroid Build Coastguard Worker                 /* Next CPU should be blocked; wake it up */
96*344aa361SAndroid Build Coastguard Worker                 if (thread_is_blocked(next)) {
97*344aa361SAndroid Build Coastguard Worker                     thread_unblock(next, false);
98*344aa361SAndroid Build Coastguard Worker                 } else {
99*344aa361SAndroid Build Coastguard Worker                     printf("%s: thread %d not blocked\n", __func__, i + 1);
100*344aa361SAndroid Build Coastguard Worker                     smpt->error_count++;
101*344aa361SAndroid Build Coastguard Worker                 }
102*344aa361SAndroid Build Coastguard Worker 
103*344aa361SAndroid Build Coastguard Worker                 break;
104*344aa361SAndroid Build Coastguard Worker             }
105*344aa361SAndroid Build Coastguard Worker         }
106*344aa361SAndroid Build Coastguard Worker 
107*344aa361SAndroid Build Coastguard Worker         /*
108*344aa361SAndroid Build Coastguard Worker          * Update unblock count for this cpu so the main test thread can see
109*344aa361SAndroid Build Coastguard Worker          * that it completed.
110*344aa361SAndroid Build Coastguard Worker          */
111*344aa361SAndroid Build Coastguard Worker         smpt->done_count++;
112*344aa361SAndroid Build Coastguard Worker         THREAD_UNLOCK(state2);
113*344aa361SAndroid Build Coastguard Worker     }
114*344aa361SAndroid Build Coastguard Worker     return 0;
115*344aa361SAndroid Build Coastguard Worker }
116*344aa361SAndroid Build Coastguard Worker 
TEST(smptest,run)117*344aa361SAndroid Build Coastguard Worker TEST(smptest, run) {
118*344aa361SAndroid Build Coastguard Worker     bool wait_for_cpus = false;
119*344aa361SAndroid Build Coastguard Worker 
120*344aa361SAndroid Build Coastguard Worker     for (uint i = 0; i < SMP_MAX_CPUS; i++) {
121*344aa361SAndroid Build Coastguard Worker         if (!thread_is_blocked(smptest_thread[i].thread)) {
122*344aa361SAndroid Build Coastguard Worker             unittest_printf("[   INFO   ] thread %d not ready\n", i);
123*344aa361SAndroid Build Coastguard Worker             wait_for_cpus = true;
124*344aa361SAndroid Build Coastguard Worker         }
125*344aa361SAndroid Build Coastguard Worker     }
126*344aa361SAndroid Build Coastguard Worker 
127*344aa361SAndroid Build Coastguard Worker     /*
128*344aa361SAndroid Build Coastguard Worker      * test-runner can start the test before all CPUs have finished booting.
129*344aa361SAndroid Build Coastguard Worker      * Wait another second for all the CPUs we need to be ready if needed.
130*344aa361SAndroid Build Coastguard Worker      */
131*344aa361SAndroid Build Coastguard Worker     if (wait_for_cpus) {
132*344aa361SAndroid Build Coastguard Worker         unittest_printf("[   INFO   ] waiting for threads to be ready\n");
133*344aa361SAndroid Build Coastguard Worker         thread_sleep(1000);
134*344aa361SAndroid Build Coastguard Worker     }
135*344aa361SAndroid Build Coastguard Worker 
136*344aa361SAndroid Build Coastguard Worker     for (uint i = 0; i < SMP_MAX_CPUS; i++) {
137*344aa361SAndroid Build Coastguard Worker         ASSERT_EQ(!mp_is_cpu_active(i) ||
138*344aa361SAndroid Build Coastguard Worker                           thread_is_blocked(smptest_thread[i].thread),
139*344aa361SAndroid Build Coastguard Worker                   true, "thread %d not ready\n", i);
140*344aa361SAndroid Build Coastguard Worker     }
141*344aa361SAndroid Build Coastguard Worker 
142*344aa361SAndroid Build Coastguard Worker     for (uint i = 0; i < SMP_MAX_CPUS; i++) {
143*344aa361SAndroid Build Coastguard Worker         smptest_thread[i].unblock_count = 0;
144*344aa361SAndroid Build Coastguard Worker         smptest_thread[i].error_count = 0;
145*344aa361SAndroid Build Coastguard Worker         smptest_thread[i].done_count = 0;
146*344aa361SAndroid Build Coastguard Worker     }
147*344aa361SAndroid Build Coastguard Worker 
148*344aa361SAndroid Build Coastguard Worker     /*
149*344aa361SAndroid Build Coastguard Worker      * Repeat the test, in case the CPUs don't go back to the same state
150*344aa361SAndroid Build Coastguard Worker      * after the first wake-up
151*344aa361SAndroid Build Coastguard Worker      */
152*344aa361SAndroid Build Coastguard Worker     for (uint j = 1; j < SMPTEST_CYCLES; j++) {
153*344aa361SAndroid Build Coastguard Worker         THREAD_LOCK(state);
154*344aa361SAndroid Build Coastguard Worker         /*
155*344aa361SAndroid Build Coastguard Worker          * Wake up thread on CPU 0 to start a test run. Each thread 'n' should
156*344aa361SAndroid Build Coastguard Worker          * wake-up thread 'n+1' until the last thread stops.
157*344aa361SAndroid Build Coastguard Worker          * Check thread is blocked before unblocking to avoid asserts.
158*344aa361SAndroid Build Coastguard Worker          */
159*344aa361SAndroid Build Coastguard Worker         if (thread_is_blocked(smptest_thread[0].thread)) {
160*344aa361SAndroid Build Coastguard Worker             thread_unblock(smptest_thread[0].thread, false);
161*344aa361SAndroid Build Coastguard Worker         }
162*344aa361SAndroid Build Coastguard Worker 
163*344aa361SAndroid Build Coastguard Worker         THREAD_UNLOCK(state);
164*344aa361SAndroid Build Coastguard Worker 
165*344aa361SAndroid Build Coastguard Worker         /* Sleep to allow all CPUs to run with some margin */
166*344aa361SAndroid Build Coastguard Worker         thread_sleep((THREAD_DELAY_MS + 5) * SMP_MAX_CPUS);
167*344aa361SAndroid Build Coastguard Worker 
168*344aa361SAndroid Build Coastguard Worker         /*
169*344aa361SAndroid Build Coastguard Worker          * Check that every CPU-thread ran exactly once each time we woke up the
170*344aa361SAndroid Build Coastguard Worker          * first thread.
171*344aa361SAndroid Build Coastguard Worker          */
172*344aa361SAndroid Build Coastguard Worker         for (uint cpu = 0; cpu < SMP_MAX_CPUS; cpu++) {
173*344aa361SAndroid Build Coastguard Worker             const struct smptest_thread* const smpt = &smptest_thread[cpu];
174*344aa361SAndroid Build Coastguard Worker 
175*344aa361SAndroid Build Coastguard Worker             /*
176*344aa361SAndroid Build Coastguard Worker              * Some cpus can still execute the thread body (e.g. if they are
177*344aa361SAndroid Build Coastguard Worker              * interrupted by some other jobs), let them time to finish
178*344aa361SAndroid Build Coastguard Worker              * (up to 1 sec, then think they got stuck).
179*344aa361SAndroid Build Coastguard Worker              */
180*344aa361SAndroid Build Coastguard Worker             for (int i = 0; i < 10; i++) {
181*344aa361SAndroid Build Coastguard Worker                 if (smpt->unblock_count != j || smpt->done_count != j) {
182*344aa361SAndroid Build Coastguard Worker                     thread_sleep(100);
183*344aa361SAndroid Build Coastguard Worker                 }
184*344aa361SAndroid Build Coastguard Worker             }
185*344aa361SAndroid Build Coastguard Worker 
186*344aa361SAndroid Build Coastguard Worker             const int unblock_count = smpt->unblock_count;
187*344aa361SAndroid Build Coastguard Worker             const int error_count = smpt->error_count;
188*344aa361SAndroid Build Coastguard Worker             const int done_count = smpt->done_count;
189*344aa361SAndroid Build Coastguard Worker 
190*344aa361SAndroid Build Coastguard Worker             if (smpt->started) {
191*344aa361SAndroid Build Coastguard Worker                 EXPECT_EQ(unblock_count, j, "cpu %d FAILED block count\n", cpu);
192*344aa361SAndroid Build Coastguard Worker                 EXPECT_EQ(error_count, 0, "cpu %d FAILED error count\n", cpu);
193*344aa361SAndroid Build Coastguard Worker                 EXPECT_EQ(done_count, j, "cpu %d FAILED done count\n", cpu);
194*344aa361SAndroid Build Coastguard Worker 
195*344aa361SAndroid Build Coastguard Worker                 if (j == SMPTEST_CYCLES - 1) {
196*344aa361SAndroid Build Coastguard Worker                     unittest_printf(
197*344aa361SAndroid Build Coastguard Worker                             "[   INFO   ] smptest cpu %d ran %d times\n", cpu,
198*344aa361SAndroid Build Coastguard Worker                             SMPTEST_CYCLES);
199*344aa361SAndroid Build Coastguard Worker                 }
200*344aa361SAndroid Build Coastguard Worker             } else {
201*344aa361SAndroid Build Coastguard Worker                 EXPECT_EQ(mp_is_cpu_active(cpu), false,
202*344aa361SAndroid Build Coastguard Worker                           "cpu %d active but not running", cpu);
203*344aa361SAndroid Build Coastguard Worker                 EXPECT_EQ(unblock_count, 0, "cpu %d FAILED block count\n", cpu);
204*344aa361SAndroid Build Coastguard Worker                 EXPECT_EQ(error_count, 0, "cpu %d FAILED error count\n", cpu);
205*344aa361SAndroid Build Coastguard Worker                 EXPECT_EQ(done_count, 0, "cpu %d FAILED done count\n", cpu);
206*344aa361SAndroid Build Coastguard Worker             }
207*344aa361SAndroid Build Coastguard Worker         }
208*344aa361SAndroid Build Coastguard Worker     }
209*344aa361SAndroid Build Coastguard Worker 
210*344aa361SAndroid Build Coastguard Worker test_abort:;
211*344aa361SAndroid Build Coastguard Worker }
212*344aa361SAndroid Build Coastguard Worker 
smptest_setup(uint level)213*344aa361SAndroid Build Coastguard Worker static void smptest_setup(uint level) {
214*344aa361SAndroid Build Coastguard Worker     /* Create a thread for each possible CPU */
215*344aa361SAndroid Build Coastguard Worker     for (uint cpu = 0; cpu < SMP_MAX_CPUS; cpu++) {
216*344aa361SAndroid Build Coastguard Worker         struct smptest_thread* smpt = &smptest_thread[cpu];
217*344aa361SAndroid Build Coastguard Worker         char thread_name[32];
218*344aa361SAndroid Build Coastguard Worker 
219*344aa361SAndroid Build Coastguard Worker         snprintf(thread_name, sizeof(thread_name), "smptest-%u", cpu);
220*344aa361SAndroid Build Coastguard Worker         smpt->thread = thread_create(thread_name, smptest_thread_func,
221*344aa361SAndroid Build Coastguard Worker                                      (void*)(uintptr_t)cpu, HIGH_PRIORITY,
222*344aa361SAndroid Build Coastguard Worker                                      DEFAULT_STACK_SIZE);
223*344aa361SAndroid Build Coastguard Worker         thread_set_pinned_cpu(smpt->thread, cpu);
224*344aa361SAndroid Build Coastguard Worker     }
225*344aa361SAndroid Build Coastguard Worker 
226*344aa361SAndroid Build Coastguard Worker     /* Allow threads to run */
227*344aa361SAndroid Build Coastguard Worker     for (uint cpu = 0; cpu < SMP_MAX_CPUS; cpu++) {
228*344aa361SAndroid Build Coastguard Worker         thread_resume(smptest_thread[cpu].thread);
229*344aa361SAndroid Build Coastguard Worker     }
230*344aa361SAndroid Build Coastguard Worker }
231*344aa361SAndroid Build Coastguard Worker 
232*344aa361SAndroid Build Coastguard Worker LK_INIT_HOOK(smptest_hook, smptest_setup, LK_INIT_LEVEL_APPS);
233*344aa361SAndroid Build Coastguard Worker 
234*344aa361SAndroid Build Coastguard Worker PORT_TEST(smptest, "com.android.kernel.smp-unittest");
235