1 | /* $Id: thread2-r0drv-nt.cpp 60278 2016-03-31 17:04:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Threads (Part 2), Ring-0 Driver, NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 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/err.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 | return VERR_NOT_IMPLEMENTED;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | DECLHIDDEN(void) rtThreadNativeWaitKludge(PRTTHREADINT pThread)
|
---|
92 | {
|
---|
93 | PVOID pvThreadObj = pThread->Core.Key;
|
---|
94 | NTSTATUS rcNt = KeWaitForSingleObject(pvThreadObj, Executive, KernelMode, FALSE, NULL);
|
---|
95 | AssertMsg(rcNt == STATUS_SUCCESS, ("rcNt=%#x\n", rcNt));
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | DECLHIDDEN(void) rtThreadNativeDestroy(PRTTHREADINT pThread)
|
---|
100 | {
|
---|
101 | NOREF(pThread);
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Native kernel thread wrapper function.
|
---|
107 | *
|
---|
108 | * This will forward to rtThreadMain and do termination upon return.
|
---|
109 | *
|
---|
110 | * @param pvArg Pointer to the argument package.
|
---|
111 | */
|
---|
112 | static VOID rtThreadNativeMain(PVOID pvArg)
|
---|
113 | {
|
---|
114 | PETHREAD Self = PsGetCurrentThread();
|
---|
115 | PRTTHREADINT pThread = (PRTTHREADINT)pvArg;
|
---|
116 |
|
---|
117 | rtThreadMain(pThread, (RTNATIVETHREAD)Self, &pThread->szName[0]);
|
---|
118 |
|
---|
119 | ObDereferenceObject(Self); /* the rtThreadNativeCreate ref. */
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | DECLHIDDEN(int) rtThreadNativeCreate(PRTTHREADINT pThreadInt, PRTNATIVETHREAD pNativeThread)
|
---|
124 | {
|
---|
125 | /*
|
---|
126 | * PsCreateSysemThread create a thread an give us a handle in return.
|
---|
127 | * We requests the object for that handle and then close it, so what
|
---|
128 | * we keep around is the pointer to the thread object and not a handle.
|
---|
129 | * The thread will dereference the object before returning.
|
---|
130 | */
|
---|
131 | HANDLE hThread = NULL;
|
---|
132 | OBJECT_ATTRIBUTES ObjAttr;
|
---|
133 | InitializeObjectAttributes(&ObjAttr, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
|
---|
134 | NTSTATUS rc = PsCreateSystemThread(&hThread,
|
---|
135 | THREAD_ALL_ACCESS,
|
---|
136 | &ObjAttr,
|
---|
137 | NULL /* ProcessHandle - kernel */,
|
---|
138 | NULL /* ClientID - kernel */,
|
---|
139 | rtThreadNativeMain,
|
---|
140 | pThreadInt);
|
---|
141 | if (NT_SUCCESS(rc))
|
---|
142 | {
|
---|
143 | PVOID pvThreadObj;
|
---|
144 | rc = ObReferenceObjectByHandle(hThread, THREAD_ALL_ACCESS, NULL /* object type */,
|
---|
145 | KernelMode, &pvThreadObj, NULL /* handle info */);
|
---|
146 | if (NT_SUCCESS(rc))
|
---|
147 | {
|
---|
148 | ZwClose(hThread);
|
---|
149 | *pNativeThread = (RTNATIVETHREAD)pvThreadObj;
|
---|
150 | }
|
---|
151 | else
|
---|
152 | AssertMsgFailed(("%#x\n", rc));
|
---|
153 | }
|
---|
154 | return RTErrConvertFromNtStatus(rc);
|
---|
155 | }
|
---|
156 |
|
---|