1 | /* $Id: tstR0ThreadPreemption.cpp 19970 2009-05-24 16:22:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT R0 Testcase - Thread Preemption.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include <iprt/thread.h>
|
---|
35 |
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/time.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <VBox/sup.h>
|
---|
40 | #include <VBox/x86.h>
|
---|
41 | #include "tstR0ThreadPreemption.h"
|
---|
42 |
|
---|
43 |
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Service request callback function.
|
---|
47 | *
|
---|
48 | * @returns VBox status code.
|
---|
49 | * @param pSession The caller's session.
|
---|
50 | * @param u64Arg 64-bit integer argument.
|
---|
51 | * @param pReqHdr The request header. Input / Output. Optional.
|
---|
52 | */
|
---|
53 | DECLEXPORT(int) TSTR0ThreadPreemptionSrvReqHandler(PSUPDRVSESSION pSession, uint32_t uOperation,
|
---|
54 | uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr)
|
---|
55 | {
|
---|
56 | if (u64Arg)
|
---|
57 | return VERR_INVALID_PARAMETER;
|
---|
58 | if (!VALID_PTR(pReqHdr))
|
---|
59 | return VERR_INVALID_PARAMETER;
|
---|
60 | char *pszErr = (char *)(pReqHdr + 1);
|
---|
61 | size_t cchErr = pReqHdr->cbReq - sizeof(*pReqHdr);
|
---|
62 | if (cchErr < 32 || cchErr >= 0x10000)
|
---|
63 | return VERR_INVALID_PARAMETER;
|
---|
64 | *pszErr = '\0';
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * The big switch.
|
---|
68 | */
|
---|
69 | switch (uOperation)
|
---|
70 | {
|
---|
71 | case TSTR0THREADPREMEPTION_SANITY_OK:
|
---|
72 | break;
|
---|
73 |
|
---|
74 | case TSTR0THREADPREMEPTION_SANITY_FAILURE:
|
---|
75 | RTStrPrintf(pszErr, cchErr, "!42failure42%1024s", "");
|
---|
76 | break;
|
---|
77 |
|
---|
78 | case TSTR0THREADPREMEPTION_BASIC:
|
---|
79 | {
|
---|
80 | RTTHREADPREEMPTSTATE State = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
81 | if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
82 | RTStrPrintf(pszErr, cchErr, "RTThreadPreemptIsEnabled returns false by default");
|
---|
83 | RTThreadPreemptDisable(&State);
|
---|
84 | if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
85 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after RTThreadPreemptDisable");
|
---|
86 | else if (!(ASMGetFlags() & X86_EFL_IF))
|
---|
87 | RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
|
---|
88 | RTThreadPreemptRestore(&State);
|
---|
89 | break;
|
---|
90 | }
|
---|
91 |
|
---|
92 | case TSTR0THREADPREMEPTION_IS_PENDING:
|
---|
93 | {
|
---|
94 | RTTHREADPREEMPTSTATE State = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
95 | RTThreadPreemptDisable(&State);
|
---|
96 | if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
97 | {
|
---|
98 | if (ASMGetFlags() & X86_EFL_IF)
|
---|
99 | {
|
---|
100 | uint64_t u64StartSysTS = RTTimeSystemNanoTS();
|
---|
101 | uint64_t u64StartTS = RTTimeNanoTS();
|
---|
102 | uint64_t cLoops = 0;
|
---|
103 | uint64_t cNanosSysElapsed;
|
---|
104 | uint64_t cNanosElapsed;
|
---|
105 | bool fPending;
|
---|
106 | do
|
---|
107 | {
|
---|
108 | fPending = RTThreadPreemptIsPending(NIL_RTTHREAD);
|
---|
109 | cNanosElapsed = RTTimeNanoTS() - u64StartTS;
|
---|
110 | cNanosSysElapsed = RTTimeSystemNanoTS() - u64StartSysTS;
|
---|
111 | cLoops++;
|
---|
112 | } while ( !fPending
|
---|
113 | && cNanosElapsed < UINT64_C(2)*1000U*1000U*1000U
|
---|
114 | && cNanosSysElapsed < UINT64_C(2)*1000U*1000U*1000U
|
---|
115 | && cLoops < 100U*_1M);
|
---|
116 | if (!fPending)
|
---|
117 | RTStrPrintf(pszErr, cchErr, "!Preempt not pending after %'llu loops / %'llu ns / %'llu ns (sys)",
|
---|
118 | cLoops, cNanosElapsed, cNanosSysElapsed);
|
---|
119 | else if (cLoops == 1)
|
---|
120 | RTStrPrintf(pszErr, cchErr, "!cLoops=1\n");
|
---|
121 | else
|
---|
122 | RTStrPrintf(pszErr, cchErr, "RTThreadPreemptIsPending returned true after %'llu loops / %'llu ns / %'llu ns (sys)",
|
---|
123 | cLoops, cNanosElapsed, cNanosSysElapsed);
|
---|
124 | }
|
---|
125 | else
|
---|
126 | RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
|
---|
127 | }
|
---|
128 | else
|
---|
129 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after RTThreadPreemptDisable");
|
---|
130 | RTThreadPreemptRestore(&State);
|
---|
131 | break;
|
---|
132 | }
|
---|
133 |
|
---|
134 | case TSTR0THREADPREMEPTION_NESTED:
|
---|
135 | {
|
---|
136 | bool const fDefault = RTThreadPreemptIsEnabled(NIL_RTTHREAD);
|
---|
137 | RTTHREADPREEMPTSTATE State1 = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
138 | RTThreadPreemptDisable(&State1);
|
---|
139 | if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
140 | {
|
---|
141 | RTTHREADPREEMPTSTATE State2 = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
142 | RTThreadPreemptDisable(&State2);
|
---|
143 | if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
144 | {
|
---|
145 | RTTHREADPREEMPTSTATE State3 = RTTHREADPREEMPTSTATE_INITIALIZER;
|
---|
146 | RTThreadPreemptDisable(&State3);
|
---|
147 | if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
148 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 3rd RTThreadPreemptDisable");
|
---|
149 |
|
---|
150 | RTThreadPreemptRestore(&State3);
|
---|
151 | if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) && !*pszErr)
|
---|
152 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 1st RTThreadPreemptRestore");
|
---|
153 | }
|
---|
154 | else
|
---|
155 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 2nd RTThreadPreemptDisable");
|
---|
156 |
|
---|
157 | RTThreadPreemptRestore(&State2);
|
---|
158 | if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) && !*pszErr)
|
---|
159 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 2nd RTThreadPreemptRestore");
|
---|
160 | }
|
---|
161 | else
|
---|
162 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 1st RTThreadPreemptDisable");
|
---|
163 | RTThreadPreemptRestore(&State1);
|
---|
164 | if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) != fDefault && !*pszErr)
|
---|
165 | RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns false after 3rd RTThreadPreemptRestore");
|
---|
166 | break;
|
---|
167 | }
|
---|
168 |
|
---|
169 | default:
|
---|
170 | RTStrPrintf(pszErr, cchErr, "!Unknown test #%d", uOperation);
|
---|
171 | break;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /* The error indicator is the '!' in the message buffer. */
|
---|
175 | return VINF_SUCCESS;
|
---|
176 | }
|
---|
177 |
|
---|