1 | /* $Id: thread2-r0drv-nt.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 2), Ring-0 Driver, NT.
|
---|
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-nt-kernel.h"
|
---|
42 |
|
---|
43 | #include <iprt/thread.h>
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/errcore.h>
|
---|
46 |
|
---|
47 | #include "internal/thread.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | DECLHIDDEN(int) rtThreadNativeInit(void)
|
---|
51 | {
|
---|
52 | /* No TLS in Ring-0. :-/ */
|
---|
53 | return VINF_SUCCESS;
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | RTDECL(RTTHREAD) RTThreadSelf(void)
|
---|
58 | {
|
---|
59 | return rtThreadGetByNative((RTNATIVETHREAD)PsGetCurrentThread());
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | DECLHIDDEN(int) rtThreadNativeSetPriority(PRTTHREADINT pThread, RTTHREADTYPE enmType)
|
---|
64 | {
|
---|
65 | /*
|
---|
66 | * Convert the IPRT priority type to NT priority.
|
---|
67 | *
|
---|
68 | * The NT priority is in the range 0..32, with realtime starting
|
---|
69 | * at 16 and the default for user processes at 8. (Should try find
|
---|
70 | * the appropriate #defines for some of this...)
|
---|
71 | */
|
---|
72 | KPRIORITY Priority;
|
---|
73 | switch (enmType)
|
---|
74 | {
|
---|
75 | case RTTHREADTYPE_INFREQUENT_POLLER: Priority = 6; break;
|
---|
76 | case RTTHREADTYPE_EMULATION: Priority = 7; break;
|
---|
77 | case RTTHREADTYPE_DEFAULT: Priority = 8; break;
|
---|
78 | case RTTHREADTYPE_MSG_PUMP: Priority = 9; break;
|
---|
79 | case RTTHREADTYPE_IO: Priority = LOW_REALTIME_PRIORITY; break;
|
---|
80 | case RTTHREADTYPE_TIMER: Priority = MAXIMUM_PRIORITY; break;
|
---|
81 |
|
---|
82 | default:
|
---|
83 | AssertMsgFailed(("enmType=%d\n", enmType));
|
---|
84 | return VERR_INVALID_PARAMETER;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /*
|
---|
88 | * Do the actual modification.
|
---|
89 | */
|
---|
90 | /*KPRIORITY oldPririty = */KeSetPriorityThread((PKTHREAD)pThread->Core.Key, Priority);
|
---|
91 | return VINF_SUCCESS;
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | DECLHIDDEN(int) rtThreadNativeAdopt(PRTTHREADINT pThread)
|
---|
96 | {
|
---|
97 | RT_NOREF1(pThread);
|
---|
98 | return VERR_NOT_IMPLEMENTED;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | DECLHIDDEN(void) rtThreadNativeWaitKludge(PRTTHREADINT pThread)
|
---|
103 | {
|
---|
104 | PVOID pvThreadObj = pThread->Core.Key;
|
---|
105 | NTSTATUS rcNt = KeWaitForSingleObject(pvThreadObj, Executive, KernelMode, FALSE, NULL);
|
---|
106 | AssertMsg(rcNt == STATUS_SUCCESS, ("rcNt=%#x\n", rcNt)); RT_NOREF1(rcNt);
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread)
|
---|
111 | {
|
---|
112 | NOREF(pThread);
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Native kernel thread wrapper function.
|
---|
118 | *
|
---|
119 | * This will forward to rtThreadMain and do termination upon return.
|
---|
120 | *
|
---|
121 | * @param pvArg Pointer to the argument package.
|
---|
122 | */
|
---|
123 | static VOID rtThreadNativeMain(PVOID pvArg)
|
---|
124 | {
|
---|
125 | PETHREAD Self = PsGetCurrentThread();
|
---|
126 | PRTTHREADINT pThread = (PRTTHREADINT)pvArg;
|
---|
127 |
|
---|
128 | rtThreadMain(pThread, (RTNATIVETHREAD)Self, &pThread->szName[0]);
|
---|
129 |
|
---|
130 | ObDereferenceObject(Self); /* the rtThreadNativeCreate ref. */
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread)
|
---|
135 | {
|
---|
136 | /*
|
---|
137 | * PsCreateSysemThread create a thread an give us a handle in return.
|
---|
138 | * We requests the object for that handle and then close it, so what
|
---|
139 | * we keep around is the pointer to the thread object and not a handle.
|
---|
140 | * The thread will dereference the object before returning.
|
---|
141 | */
|
---|
142 | HANDLE hThread = NULL;
|
---|
143 | OBJECT_ATTRIBUTES ObjAttr;
|
---|
144 | InitializeObjectAttributes(&ObjAttr, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
|
---|
145 | NTSTATUS rc = PsCreateSystemThread(&hThread,
|
---|
146 | THREAD_ALL_ACCESS,
|
---|
147 | &ObjAttr,
|
---|
148 | NULL /* ProcessHandle - kernel */,
|
---|
149 | NULL /* ClientID - kernel */,
|
---|
150 | rtThreadNativeMain,
|
---|
151 | pThreadInt);
|
---|
152 | if (NT_SUCCESS(rc))
|
---|
153 | {
|
---|
154 | PVOID pvThreadObj;
|
---|
155 | rc = ObReferenceObjectByHandle(hThread, THREAD_ALL_ACCESS, NULL /* object type */,
|
---|
156 | KernelMode, &pvThreadObj, NULL /* handle info */);
|
---|
157 | if (NT_SUCCESS(rc))
|
---|
158 | {
|
---|
159 | ZwClose(hThread);
|
---|
160 | *pNativeThread = (RTNATIVETHREAD)pvThreadObj;
|
---|
161 | }
|
---|
162 | else
|
---|
163 | AssertMsgFailed(("%#x\n", rc));
|
---|
164 | }
|
---|
165 | return RTErrConvertFromNtStatus(rc);
|
---|
166 | }
|
---|
167 |
|
---|