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