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