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