1 | /* $Id: thread2-r0drv-linux.c 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 2), Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "the-linux-kernel.h"
|
---|
42 | #include "internal/iprt.h"
|
---|
43 |
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/thread.h>
|
---|
46 | #include <iprt/errcore.h>
|
---|
47 | #include "internal/thread.h"
|
---|
48 |
|
---|
49 | #if RTLNX_VER_MIN(4,11,0)
|
---|
50 | #include <uapi/linux/sched/types.h>
|
---|
51 | #endif /* >= KERNEL_VERSION(4, 11, 0) */
|
---|
52 |
|
---|
53 | RTDECL(RTTHREAD) RTThreadSelf(void)
|
---|
54 | {
|
---|
55 | return rtThreadGetByNative((RTNATIVETHREAD)current);
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | DECLHIDDEN(int) rtThreadNativeInit(void)
|
---|
60 | {
|
---|
61 | return VINF_SUCCESS;
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
|
---|
66 | {
|
---|
67 | #if RTLNX_VER_MIN(2,5,2)
|
---|
68 | /*
|
---|
69 | * Assignments are partially based on g_aTypesLinuxFree but
|
---|
70 | * scaled up in the high priority end.
|
---|
71 | *
|
---|
72 | * 5.9.0 - :
|
---|
73 | * The sched_set_normal interfaces does not really check the input,
|
---|
74 | * whereas sched_set_fifo & sched_set_fifo_low have fixed assignments.
|
---|
75 | * 2.6.11 - 5.9.0:
|
---|
76 | * Use sched_setscheduler to try effect FIFO scheduling
|
---|
77 | * for IO and TIMER threads, otherwise use set_user_nice.
|
---|
78 | * 2.5.2 - 5.9.0:
|
---|
79 | * Use set_user_nice to renice the thread.
|
---|
80 | */
|
---|
81 | int iNice = 0;
|
---|
82 | # if RTLNX_VER_MAX(5,9,0)
|
---|
83 | int rc;
|
---|
84 | # if RTLNX_VER_MIN(2,6,11)
|
---|
85 | int iSchedClass = SCHED_NORMAL;
|
---|
86 | struct sched_param Param = { .sched_priority = 0 };
|
---|
87 | # endif
|
---|
88 | # endif
|
---|
89 | switch (enmType)
|
---|
90 | {
|
---|
91 | case RTTHREADTYPE_INFREQUENT_POLLER:
|
---|
92 | iNice = +3;
|
---|
93 | break;
|
---|
94 |
|
---|
95 | case RTTHREADTYPE_MAIN_HEAVY_WORKER:
|
---|
96 | iNice = +2;
|
---|
97 | break;
|
---|
98 |
|
---|
99 | case RTTHREADTYPE_EMULATION:
|
---|
100 | iNice = +1;
|
---|
101 | break;
|
---|
102 |
|
---|
103 | case RTTHREADTYPE_DEFAULT:
|
---|
104 | case RTTHREADTYPE_GUI:
|
---|
105 | case RTTHREADTYPE_MAIN_WORKER:
|
---|
106 | iNice = 0;
|
---|
107 | break;
|
---|
108 |
|
---|
109 | case RTTHREADTYPE_VRDP_IO:
|
---|
110 | case RTTHREADTYPE_DEBUGGER:
|
---|
111 | iNice = -1;
|
---|
112 | break;
|
---|
113 |
|
---|
114 | case RTTHREADTYPE_MSG_PUMP:
|
---|
115 | iNice = -2;
|
---|
116 | break;
|
---|
117 |
|
---|
118 | case RTTHREADTYPE_IO:
|
---|
119 | # if RTLNX_VER_MIN(5,9,0)
|
---|
120 | sched_set_fifo_low(current);
|
---|
121 | return VINF_SUCCESS;
|
---|
122 | # else
|
---|
123 | iNice = -12;
|
---|
124 | # if RTLNX_VER_MIN(2,6,11)
|
---|
125 | iSchedClass = SCHED_FIFO;
|
---|
126 | Param.sched_priority = 1; /* => prio=98; */
|
---|
127 | # endif
|
---|
128 | break;
|
---|
129 | # endif
|
---|
130 |
|
---|
131 | case RTTHREADTYPE_TIMER:
|
---|
132 | # if RTLNX_VER_MIN(5,9,0)
|
---|
133 | sched_set_fifo(current);
|
---|
134 | return VINF_SUCCESS;
|
---|
135 | # else
|
---|
136 | iNice = -19;
|
---|
137 | # if RTLNX_VER_MIN(2,6,11)
|
---|
138 | iSchedClass = SCHED_FIFO;
|
---|
139 | Param.sched_priority = MAX_RT_PRIO / 2; /* => prio=49 */
|
---|
140 | # endif
|
---|
141 | break;
|
---|
142 | # endif
|
---|
143 | default:
|
---|
144 | AssertMsgFailedReturn(("enmType=%d\n", enmType), VERR_INVALID_PARAMETER);
|
---|
145 | }
|
---|
146 |
|
---|
147 | # if RTLNX_VER_MIN(5,9,0)
|
---|
148 | /*
|
---|
149 | * We only get here for renice work.
|
---|
150 | */
|
---|
151 | sched_set_normal(current, iNice);
|
---|
152 |
|
---|
153 | # else /* < 5.9.0 */
|
---|
154 | # if RTLNX_VER_MIN(2,6,11)
|
---|
155 | /*
|
---|
156 | * Try set scheduler parameters.
|
---|
157 | * Fall back on normal + nice if this fails for FIFO policy.*
|
---|
158 | */
|
---|
159 | rc = sched_setscheduler(current, iSchedClass, &Param);
|
---|
160 | if (rc)
|
---|
161 | {
|
---|
162 | Param.sched_priority = 0;
|
---|
163 | iSchedClass = SCHED_NORMAL;
|
---|
164 | rc = sched_setscheduler(current, iSchedClass, &Param);
|
---|
165 | }
|
---|
166 |
|
---|
167 | /*
|
---|
168 | * Renice if using normal scheduling class.
|
---|
169 | */
|
---|
170 | if (iSchedClass == SCHED_NORMAL)
|
---|
171 | # endif /* >= 2.6.11 */
|
---|
172 | set_user_nice(current, iNice);
|
---|
173 |
|
---|
174 | # endif /* < 5.9.0 */
|
---|
175 | #else /* < 2.5.2 */
|
---|
176 | RT_NOREF_PV(enmType);
|
---|
177 | #endif /* < 2.5.2 */
|
---|
178 | RT_NOREF_PV(pThread);
|
---|
179 | return VINF_SUCCESS;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread)
|
---|
184 | {
|
---|
185 | RT_NOREF_PV(pThread);
|
---|
186 | return VERR_NOT_IMPLEMENTED;
|
---|
187 | }
|
---|
188 |
|
---|
189 |
|
---|
190 | DECLHIDDEN(void) rtThreadNativeWaitKludge(PRTTHREADINT pThread)
|
---|
191 | {
|
---|
192 | /** @todo fix RTThreadWait/RTR0Term race on linux. */
|
---|
193 | RTThreadSleep(1); NOREF(pThread);
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread)
|
---|
198 | {
|
---|
199 | NOREF(pThread);
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | #if RTLNX_VER_MIN(2,6,4)
|
---|
204 | /**
|
---|
205 | * Native kernel thread wrapper function.
|
---|
206 | *
|
---|
207 | * This will forward to rtThreadMain and do termination upon return.
|
---|
208 | *
|
---|
209 | * @param pvArg Pointer to the argument package.
|
---|
210 | */
|
---|
211 | static int rtThreadNativeMain(void *pvArg)
|
---|
212 | {
|
---|
213 | PRTTHREADINT pThread = (PRTTHREADINT)pvArg;
|
---|
214 |
|
---|
215 | rtThreadMain(pThread, (RTNATIVETHREAD)current, &pThread->szName[0]);
|
---|
216 | return 0;
|
---|
217 | }
|
---|
218 | #endif
|
---|
219 |
|
---|
220 |
|
---|
221 | DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread)
|
---|
222 | {
|
---|
223 | #if RTLNX_VER_MIN(2,6,4)
|
---|
224 | struct task_struct *NativeThread;
|
---|
225 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
226 |
|
---|
227 | RT_ASSERT_PREEMPTIBLE();
|
---|
228 |
|
---|
229 | NativeThread = kthread_run(rtThreadNativeMain, pThreadInt, "iprt-%s", pThreadInt->szName);
|
---|
230 |
|
---|
231 | if (!IS_ERR(NativeThread))
|
---|
232 | {
|
---|
233 | *pNativeThread = (RTNATIVETHREAD)NativeThread;
|
---|
234 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
235 | return VINF_SUCCESS;
|
---|
236 | }
|
---|
237 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
238 | return VERR_GENERAL_FAILURE;
|
---|
239 | #else
|
---|
240 | return VERR_NOT_IMPLEMENTED;
|
---|
241 | #endif
|
---|
242 | }
|
---|
243 |
|
---|