1 | /* $Id: tstR0ThreadPreemptionDriver.cpp 51906 2014-07-07 16:28:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT R0 Testcase - Thread Preemption, driver program.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2013 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #include <iprt/initterm.h>
|
---|
31 |
|
---|
32 | #include <iprt/asm.h>
|
---|
33 | #include <iprt/cpuset.h>
|
---|
34 | #include <iprt/err.h>
|
---|
35 | #include <iprt/path.h>
|
---|
36 | #include <iprt/param.h>
|
---|
37 | #include <iprt/stream.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/test.h>
|
---|
40 | #include <iprt/time.h>
|
---|
41 | #include <iprt/thread.h>
|
---|
42 | #ifdef VBOX
|
---|
43 | # include <VBox/sup.h>
|
---|
44 | # include "tstR0ThreadPreemption.h"
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | /*******************************************************************************
|
---|
48 | * Global Variables *
|
---|
49 | *******************************************************************************/
|
---|
50 | static bool volatile g_fTerminate = false;
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Try make sure all online CPUs will be engaged.
|
---|
55 | */
|
---|
56 | static DECLCALLBACK(int) MyThreadProc(RTTHREAD hSelf, void *pvCpuIdx)
|
---|
57 | {
|
---|
58 | RTCPUSET Affinity;
|
---|
59 | RTCpuSetEmpty(&Affinity);
|
---|
60 | RTCpuSetAddByIndex(&Affinity, (intptr_t)pvCpuIdx);
|
---|
61 | RTThreadSetAffinity(&Affinity); /* ignore return code as it's not supported on all hosts. */
|
---|
62 |
|
---|
63 | while (!g_fTerminate)
|
---|
64 | {
|
---|
65 | uint64_t tsStart = RTTimeMilliTS();
|
---|
66 | do
|
---|
67 | {
|
---|
68 | ASMNopPause();
|
---|
69 | } while (RTTimeMilliTS() - tsStart < 8);
|
---|
70 | RTThreadSleep(4);
|
---|
71 | }
|
---|
72 |
|
---|
73 | return VINF_SUCCESS;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Entry point.
|
---|
79 | */
|
---|
80 | extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
|
---|
81 | {
|
---|
82 | #ifndef VBOX
|
---|
83 | RTPrintf("tstSup: SKIPPED\n");
|
---|
84 | return 0;
|
---|
85 | #else
|
---|
86 | /*
|
---|
87 | * Init.
|
---|
88 | */
|
---|
89 | RTTEST hTest;
|
---|
90 | int rc = RTTestInitAndCreate("tstR0ThreadPreemption", &hTest);
|
---|
91 | if (rc)
|
---|
92 | return rc;
|
---|
93 | RTTestBanner(hTest);
|
---|
94 |
|
---|
95 | PSUPDRVSESSION pSession;
|
---|
96 | rc = SUPR3Init(&pSession);
|
---|
97 | if (RT_FAILURE(rc))
|
---|
98 | {
|
---|
99 | RTTestFailed(hTest, "SUPR3Init failed with rc=%Rrc\n", rc);
|
---|
100 | return RTTestSummaryAndDestroy(hTest);
|
---|
101 | }
|
---|
102 |
|
---|
103 | char szPath[RTPATH_MAX];
|
---|
104 | rc = RTPathExecDir(szPath, sizeof(szPath));
|
---|
105 | if (RT_SUCCESS(rc))
|
---|
106 | rc = RTPathAppend(szPath, sizeof(szPath), "tstR0ThreadPreemption.r0");
|
---|
107 | if (RT_FAILURE(rc))
|
---|
108 | {
|
---|
109 | RTTestFailed(hTest, "Failed constructing .r0 filename (rc=%Rrc)", rc);
|
---|
110 | return RTTestSummaryAndDestroy(hTest);
|
---|
111 | }
|
---|
112 |
|
---|
113 | void *pvImageBase;
|
---|
114 | rc = SUPR3LoadServiceModule(szPath, "tstR0ThreadPreemption",
|
---|
115 | "TSTR0ThreadPreemptionSrvReqHandler",
|
---|
116 | &pvImageBase);
|
---|
117 | if (RT_FAILURE(rc))
|
---|
118 | {
|
---|
119 | RTTestFailed(hTest, "SUPR3LoadServiceModule(%s,,,) failed with rc=%Rrc\n", szPath, rc);
|
---|
120 | return RTTestSummaryAndDestroy(hTest);
|
---|
121 | }
|
---|
122 |
|
---|
123 | /* test request */
|
---|
124 | struct
|
---|
125 | {
|
---|
126 | SUPR0SERVICEREQHDR Hdr;
|
---|
127 | char szMsg[256];
|
---|
128 | } Req;
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * Sanity checks.
|
---|
132 | */
|
---|
133 | RTTestSub(hTest, "Sanity");
|
---|
134 | Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
|
---|
135 | Req.Hdr.cbReq = sizeof(Req);
|
---|
136 | Req.szMsg[0] = '\0';
|
---|
137 | RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
|
---|
138 | TSTR0THREADPREMEPTION_SANITY_OK, 0, &Req.Hdr), VINF_SUCCESS);
|
---|
139 | if (RT_FAILURE(rc))
|
---|
140 | return RTTestSummaryAndDestroy(hTest);
|
---|
141 | RTTESTI_CHECK_MSG(Req.szMsg[0] == '\0', ("%s", Req.szMsg));
|
---|
142 | if (Req.szMsg[0] != '\0')
|
---|
143 | return RTTestSummaryAndDestroy(hTest);
|
---|
144 |
|
---|
145 | Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
|
---|
146 | Req.Hdr.cbReq = sizeof(Req);
|
---|
147 | Req.szMsg[0] = '\0';
|
---|
148 | RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
|
---|
149 | TSTR0THREADPREMEPTION_SANITY_FAILURE, 0, &Req.Hdr), VINF_SUCCESS);
|
---|
150 | if (RT_FAILURE(rc))
|
---|
151 | return RTTestSummaryAndDestroy(hTest);
|
---|
152 | RTTESTI_CHECK_MSG(!strncmp(Req.szMsg, RT_STR_TUPLE("!42failure42")), ("%s", Req.szMsg));
|
---|
153 | if (strncmp(Req.szMsg, RT_STR_TUPLE("!42failure42")))
|
---|
154 | return RTTestSummaryAndDestroy(hTest);
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * Basic tests, bail out on failure.
|
---|
158 | */
|
---|
159 | RTTestSub(hTest, "Basics");
|
---|
160 | Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
|
---|
161 | Req.Hdr.cbReq = sizeof(Req);
|
---|
162 | Req.szMsg[0] = '\0';
|
---|
163 | RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
|
---|
164 | TSTR0THREADPREMEPTION_BASIC, 0, &Req.Hdr), VINF_SUCCESS);
|
---|
165 | if (RT_FAILURE(rc))
|
---|
166 | return RTTestSummaryAndDestroy(hTest);
|
---|
167 | if (Req.szMsg[0] == '!')
|
---|
168 | {
|
---|
169 | RTTestIFailed("%s", &Req.szMsg[1]);
|
---|
170 | return RTTestSummaryAndDestroy(hTest);
|
---|
171 | }
|
---|
172 | if (Req.szMsg[0])
|
---|
173 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
|
---|
174 |
|
---|
175 | /*
|
---|
176 | * Is it trusty.
|
---|
177 | */
|
---|
178 | RTTestSub(hTest, "RTThreadPreemptIsPendingTrusty");
|
---|
179 | Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
|
---|
180 | Req.Hdr.cbReq = sizeof(Req);
|
---|
181 | Req.szMsg[0] = '\0';
|
---|
182 | RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
|
---|
183 | TSTR0THREADPREMEPTION_IS_TRUSTY, 0, &Req.Hdr), VINF_SUCCESS);
|
---|
184 | if (RT_FAILURE(rc))
|
---|
185 | return RTTestSummaryAndDestroy(hTest);
|
---|
186 | if (Req.szMsg[0] == '!')
|
---|
187 | RTTestIFailed("%s", &Req.szMsg[1]);
|
---|
188 | else if (Req.szMsg[0])
|
---|
189 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
|
---|
190 |
|
---|
191 | /*
|
---|
192 | * Stay in ring-0 until preemption is pending.
|
---|
193 | */
|
---|
194 | RTTHREAD ahThreads[RTCPUSET_MAX_CPUS];
|
---|
195 | uint32_t cThreads = RTMpGetCount();
|
---|
196 | RTCPUSET OnlineSet;
|
---|
197 | RTMpGetOnlineSet(&OnlineSet);
|
---|
198 | for (uint32_t i = 0; i < RT_ELEMENTS(ahThreads); i++)
|
---|
199 | {
|
---|
200 | ahThreads[i] = NIL_RTTHREAD;
|
---|
201 | if (RTCpuSetIsMemberByIndex(&OnlineSet, i))
|
---|
202 | RTThreadCreateF(&ahThreads[i], MyThreadProc, (void *)(uintptr_t)i, 0, RTTHREADTYPE_DEFAULT,
|
---|
203 | RTTHREADFLAGS_WAITABLE, "cpu=%u", i);
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | RTTestSub(hTest, "Pending Preemption");
|
---|
208 | RTThreadSleep(250); /** @todo fix GIP initialization? */
|
---|
209 | for (int i = 0; ; i++)
|
---|
210 | {
|
---|
211 | Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
|
---|
212 | Req.Hdr.cbReq = sizeof(Req);
|
---|
213 | Req.szMsg[0] = '\0';
|
---|
214 | RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
|
---|
215 | TSTR0THREADPREMEPTION_IS_PENDING, 0, &Req.Hdr), VINF_SUCCESS);
|
---|
216 | if ( strcmp(Req.szMsg, "!cLoops=1\n")
|
---|
217 | || i >= 64)
|
---|
218 | {
|
---|
219 | if (Req.szMsg[0] == '!')
|
---|
220 | RTTestIFailed("%s", &Req.szMsg[1]);
|
---|
221 | else if (Req.szMsg[0])
|
---|
222 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
|
---|
223 | break;
|
---|
224 | }
|
---|
225 | if ((i % 3) == 0)
|
---|
226 | RTThreadYield();
|
---|
227 | else if ((i % 16) == 0)
|
---|
228 | RTThreadSleep(8);
|
---|
229 | }
|
---|
230 |
|
---|
231 | ASMAtomicWriteBool(&g_fTerminate, true);
|
---|
232 | for (uint32_t i = 0; i < RT_ELEMENTS(ahThreads); i++)
|
---|
233 | if (ahThreads[i] != NIL_RTTHREAD)
|
---|
234 | RTThreadWait(ahThreads[i], 5000, NULL);
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * Test nested RTThreadPreemptDisable calls.
|
---|
238 | */
|
---|
239 | RTTestSub(hTest, "Nested");
|
---|
240 | Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
|
---|
241 | Req.Hdr.cbReq = sizeof(Req);
|
---|
242 | Req.szMsg[0] = '\0';
|
---|
243 | RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
|
---|
244 | TSTR0THREADPREMEPTION_NESTED, 0, &Req.Hdr), VINF_SUCCESS);
|
---|
245 | if (Req.szMsg[0] == '!')
|
---|
246 | RTTestIFailed("%s", &Req.szMsg[1]);
|
---|
247 | else if (Req.szMsg[0])
|
---|
248 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
|
---|
249 |
|
---|
250 |
|
---|
251 | /*
|
---|
252 | * Test thread-context hooks.
|
---|
253 | */
|
---|
254 | RTTestSub(hTest, "RTThreadCtxHooks");
|
---|
255 | uint64_t u64StartTS = RTTimeMilliTS();
|
---|
256 | uint64_t cMsMax = 60000; /* ca. 1 minute timeout. */
|
---|
257 | uint64_t cMsElapsed;
|
---|
258 | for (unsigned i = 0; i < 50; i++)
|
---|
259 | {
|
---|
260 | Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
|
---|
261 | Req.Hdr.cbReq = sizeof(Req);
|
---|
262 | Req.szMsg[0] = '\0';
|
---|
263 | RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
|
---|
264 | TSTR0THREADPREEMPTION_CTXHOOKS, 0, &Req.Hdr), VINF_SUCCESS);
|
---|
265 | if (RT_FAILURE(rc))
|
---|
266 | return RTTestSummaryAndDestroy(hTest);
|
---|
267 | if (Req.szMsg[0] == '!')
|
---|
268 | RTTestIFailed("%s", &Req.szMsg[1]);
|
---|
269 | else if (Req.szMsg[0])
|
---|
270 | RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
|
---|
271 | if (!(i % 10))
|
---|
272 | RTTestIPrintf(RTTESTLVL_ALWAYS, "RTThreadCtxHooks passed %u iteration(s)\n", i);
|
---|
273 |
|
---|
274 | /* Check timeout and bail. */
|
---|
275 | cMsElapsed = RTTimeMilliTS() - u64StartTS;
|
---|
276 | if (cMsElapsed > cMsMax)
|
---|
277 | {
|
---|
278 | RTTestIPrintf(RTTESTLVL_INFO, "RTThreadCtxHooks Stopping iterations. %RU64 ms. for %u iterations.\n",
|
---|
279 | cMsElapsed, i);
|
---|
280 | break;
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | /*
|
---|
285 | * Done.
|
---|
286 | */
|
---|
287 | return RTTestSummaryAndDestroy(hTest);
|
---|
288 | #endif
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 | #if !defined(VBOX_WITH_HARDENING) || !defined(RT_OS_WINDOWS)
|
---|
293 | /**
|
---|
294 | * Main entry point.
|
---|
295 | */
|
---|
296 | int main(int argc, char **argv, char **envp)
|
---|
297 | {
|
---|
298 | return TrustedMain(argc, argv, envp);
|
---|
299 | }
|
---|
300 | #endif
|
---|
301 |
|
---|