1 | /* $Id: thread2-r0drv-linux.c 85698 2020-08-11 17:05:29Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 2), Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 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 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "the-linux-kernel.h"
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/thread.h>
|
---|
36 | #include <iprt/errcore.h>
|
---|
37 | #include "internal/thread.h"
|
---|
38 |
|
---|
39 | #if RTLNX_VER_MIN(4,11,0)
|
---|
40 | #include <uapi/linux/sched/types.h>
|
---|
41 | #endif /* >= KERNEL_VERSION(4, 11, 0) */
|
---|
42 |
|
---|
43 | RTDECL(RTTHREAD) RTThreadSelf(void)
|
---|
44 | {
|
---|
45 | return rtThreadGetByNative((RTNATIVETHREAD)current);
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | DECLHIDDEN(int) rtThreadNativeInit(void)
|
---|
50 | {
|
---|
51 | return VINF_SUCCESS;
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
|
---|
56 | {
|
---|
57 | #if RTLNX_VER_MIN(2,6,11)
|
---|
58 | /* See comment near MAX_RT_PRIO in linux/sched.h for details on
|
---|
59 | sched_priority. */
|
---|
60 | int iSchedClass = SCHED_NORMAL;
|
---|
61 | struct sched_param Param = { .sched_priority = MAX_PRIO - 1 };
|
---|
62 | switch (enmType)
|
---|
63 | {
|
---|
64 | case RTTHREADTYPE_INFREQUENT_POLLER:
|
---|
65 | Param.sched_priority = MAX_RT_PRIO + 5;
|
---|
66 | break;
|
---|
67 |
|
---|
68 | case RTTHREADTYPE_EMULATION:
|
---|
69 | Param.sched_priority = MAX_RT_PRIO + 4;
|
---|
70 | break;
|
---|
71 |
|
---|
72 | case RTTHREADTYPE_DEFAULT:
|
---|
73 | Param.sched_priority = MAX_RT_PRIO + 3;
|
---|
74 | break;
|
---|
75 |
|
---|
76 | case RTTHREADTYPE_MSG_PUMP:
|
---|
77 | Param.sched_priority = MAX_RT_PRIO + 2;
|
---|
78 | break;
|
---|
79 |
|
---|
80 | case RTTHREADTYPE_IO:
|
---|
81 | iSchedClass = SCHED_FIFO;
|
---|
82 | Param.sched_priority = MAX_RT_PRIO - 1;
|
---|
83 | break;
|
---|
84 |
|
---|
85 | case RTTHREADTYPE_TIMER:
|
---|
86 | iSchedClass = SCHED_FIFO;
|
---|
87 | Param.sched_priority = 1; /* not 0 just in case */
|
---|
88 | break;
|
---|
89 |
|
---|
90 | default:
|
---|
91 | AssertMsgFailed(("enmType=%d\n", enmType));
|
---|
92 | return VERR_INVALID_PARAMETER;
|
---|
93 | }
|
---|
94 |
|
---|
95 | sched_setscheduler(current, iSchedClass, &Param);
|
---|
96 | #else
|
---|
97 | RT_NOREF_PV(enmType);
|
---|
98 | #endif
|
---|
99 | RT_NOREF_PV(pThread);
|
---|
100 |
|
---|
101 | return VINF_SUCCESS;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread)
|
---|
106 | {
|
---|
107 | RT_NOREF_PV(pThread);
|
---|
108 | return VERR_NOT_IMPLEMENTED;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | DECLHIDDEN(void) rtThreadNativeWaitKludge(PRTTHREADINT pThread)
|
---|
113 | {
|
---|
114 | /** @todo fix RTThreadWait/RTR0Term race on linux. */
|
---|
115 | RTThreadSleep(1); NOREF(pThread);
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread)
|
---|
120 | {
|
---|
121 | NOREF(pThread);
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | #if RTLNX_VER_MIN(2,6,4)
|
---|
126 | /**
|
---|
127 | * Native kernel thread wrapper function.
|
---|
128 | *
|
---|
129 | * This will forward to rtThreadMain and do termination upon return.
|
---|
130 | *
|
---|
131 | * @param pvArg Pointer to the argument package.
|
---|
132 | */
|
---|
133 | static int rtThreadNativeMain(void *pvArg)
|
---|
134 | {
|
---|
135 | PRTTHREADINT pThread = (PRTTHREADINT)pvArg;
|
---|
136 |
|
---|
137 | rtThreadMain(pThread, (RTNATIVETHREAD)current, &pThread->szName[0]);
|
---|
138 | return 0;
|
---|
139 | }
|
---|
140 | #endif
|
---|
141 |
|
---|
142 |
|
---|
143 | DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread)
|
---|
144 | {
|
---|
145 | #if RTLNX_VER_MIN(2,6,4)
|
---|
146 | struct task_struct *NativeThread;
|
---|
147 | IPRT_LINUX_SAVE_EFL_AC();
|
---|
148 |
|
---|
149 | RT_ASSERT_PREEMPTIBLE();
|
---|
150 |
|
---|
151 | NativeThread = kthread_run(rtThreadNativeMain, pThreadInt, "iprt-%s", pThreadInt->szName);
|
---|
152 |
|
---|
153 | if (!IS_ERR(NativeThread))
|
---|
154 | {
|
---|
155 | *pNativeThread = (RTNATIVETHREAD)NativeThread;
|
---|
156 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
157 | return VINF_SUCCESS;
|
---|
158 | }
|
---|
159 | IPRT_LINUX_RESTORE_EFL_AC();
|
---|
160 | return VERR_GENERAL_FAILURE;
|
---|
161 | #else
|
---|
162 | return VERR_NOT_IMPLEMENTED;
|
---|
163 | #endif
|
---|
164 | }
|
---|
165 |
|
---|