Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

456 lines
11 KiB

  1. /*++
  2. Copyright (c) 1985 - 1999, Microsoft Corporation
  3. Module Name:
  4. w32p.h
  5. Abstract:
  6. private header file for Win32 kernel mode driver
  7. Author:
  8. Mark Lucovsky (markl) 31-Oct-1994
  9. Revision History:
  10. --*/
  11. #ifndef _W32P_
  12. #define _W32P_
  13. #include "w32w64.h"
  14. //
  15. // Service Table description (from table.stb)
  16. //
  17. extern ULONG_PTR W32pServiceTable[];
  18. extern ULONG W32pServiceLimit;
  19. extern UCHAR W32pArgumentTable[];
  20. //
  21. // shared handle table
  22. //
  23. extern PVOID *gpHmgrSharedHandleTable;
  24. extern PVOID gpHmgrSharedHandleSection;
  25. #ifndef W32KAPI
  26. #define W32KAPI DECLSPEC_ADDRSAFE
  27. #endif
  28. #define W32_SERVICE_NUMBER WIN32K_SERVICE_INDEX
  29. W32KAPI
  30. VOID NtGdiFlushUserBatch(void);
  31. #if defined(_X86_)
  32. //
  33. // Keep our own copy of this to avoid double indirections on probing
  34. //
  35. extern ULONG Win32UserProbeAddress;
  36. #undef MM_USER_PROBE_ADDRESS
  37. #define MM_USER_PROBE_ADDRESS Win32UserProbeAddress
  38. #endif
  39. typedef struct _W32THREAD * KPTR_MODIFIER PW32THREAD;
  40. //
  41. // The following is a "thread lock structure". This structure lives on
  42. // the stack and is linked into a LIFO list that is rooted in the thread
  43. // information structure.
  44. //
  45. // In DBG, a shadow of it is copied in gThreadLocksArray where it will persist
  46. // The stack TL will have the ptl point to the global element while the
  47. // global will have ptl point to the stack TL.
  48. // A freed global TL will have TL_FREED_PATTERN in the HIWORD of uTLCount
  49. //
  50. typedef struct _TL {
  51. struct _TL *next;
  52. PVOID pobj;
  53. PFREE_FUNCTION pfnFree;
  54. #if DBG
  55. ULONG uTLCount;
  56. PW32THREAD pW32Thread;
  57. PVOID pfnCaller;
  58. struct _TL *ptl;
  59. #endif
  60. } TL, * KPTR_MODIFIER PTL;
  61. #define TL_FREED_PATTERN 0x4f000000
  62. #if DBG
  63. //
  64. // global array of TL structures
  65. //
  66. extern PTL gFreeTLList;
  67. extern void CreateShadowTL(PTL ptl);
  68. #endif
  69. /*
  70. * This is the header shared info for W32 threads. It is followed by the
  71. * NtUser per thread information.
  72. */
  73. typedef struct _W32THREAD {
  74. PETHREAD pEThread;
  75. ULONG RefCount;
  76. PTL ptlW32;
  77. KERNEL_PVOID pgdiDcattr;
  78. KERNEL_PVOID pgdiBrushAttr;
  79. KERNEL_PVOID pUMPDObjs;
  80. KERNEL_PVOID pUMPDHeap;
  81. #ifdef CHECK_SEMAPHORE_USAGE
  82. ULONG dwEngAcquireCount;
  83. #endif
  84. #ifdef VALIDATE_LOCKS
  85. KERNEL_PVOID pSemTable;
  86. #endif
  87. KERNEL_PVOID pUMPDObj;
  88. #if defined(_WIN64)
  89. KERNEL_PVOID pProxyPort;
  90. KERNEL_PVOID pClientID;
  91. #endif
  92. LIST_ENTRY GdiTmpAllocList;
  93. } W32THREAD, * KPTR_MODIFIER PW32THREAD;
  94. #define W32PF_CONSOLEAPPLICATION 0x00000001
  95. #define W32PF_FORCEOFFFEEDBACK 0x00000002
  96. #define W32PF_STARTGLASS 0x00000004
  97. #define W32PF_WOW 0x00000008
  98. #define W32PF_READSCREENACCESSGRANTED 0x00000010
  99. #define W32PF_INITIALIZED 0x00000020
  100. #define W32PF_APPSTARTING 0x00000040
  101. #define W32PF_WOW64 0x00000080 /* Process emulated 32bit */
  102. #define W32PF_ALLOWFOREGROUNDACTIVATE 0x00000100
  103. #define W32PF_OWNDCCLEANUP 0x00000200
  104. #define W32PF_SHOWSTARTGLASSCALLED 0x00000400
  105. #define W32PF_FORCEBACKGROUNDPRIORITY 0x00000800
  106. #define W32PF_TERMINATED 0x00001000
  107. #define W32PF_CLASSESREGISTERED 0x00002000
  108. #define W32PF_THREADCONNECTED 0x00004000
  109. #define W32PF_PROCESSCONNECTED 0x00008000
  110. #define W32PF_WAKEWOWEXEC 0x00010000
  111. #define W32PF_WAITFORINPUTIDLE 0x00020000
  112. #define W32PF_IOWINSTA 0x00040000
  113. #define W32PF_ALLOWSETFOREGROUND 0x00080000
  114. #define W32PF_OLELOADED 0x00100000
  115. #define W32PF_SCREENSAVER 0x00200000
  116. #define W32PF_IDLESCREENSAVER 0x00400000
  117. #define W32PF_DISABLEIME 0x00800000
  118. #define W32PF_SETUPAPP 0x01000000
  119. #define W32PF_RESTRICTED 0x02000000
  120. #define W32PF_CONSOLEHASFOCUS 0x04000000
  121. #define W32PF_DISABLEWINDOWSGHOSTING 0x08000000
  122. //
  123. // Process must be first element of structure to correctly handle
  124. // initialization. See PsConvertToGuiThread in ntos\ps\psquery.c.
  125. //
  126. typedef ULONG W32PID;
  127. typedef struct _W32PROCESS * KPTR_MODIFIER PW32PROCESS;
  128. typedef struct _W32PROCESS {
  129. PEPROCESS Process;
  130. ULONG RefCount;
  131. ULONG W32PF_Flags; // must hold USER lock while changing this
  132. PKEVENT InputIdleEvent;
  133. ULONG StartCursorHideTime;
  134. PW32PROCESS NextStart;
  135. KERNEL_PVOID pDCAttrList;
  136. KERNEL_PVOID pBrushAttrList;
  137. W32PID W32Pid;
  138. LONG GDIHandleCount;
  139. LONG UserHandleCount;
  140. LONG GDIEngUserMemAllocTableLock;
  141. RTL_AVL_TABLE GDIEngUserMemAllocTable;
  142. LIST_ENTRY GDIDcAttrFreeList;
  143. LIST_ENTRY GDIBrushAttrFreeList;
  144. } W32PROCESS;
  145. #define W32GetCurrentProcess() ((PW32PROCESS)PsGetCurrentProcessWin32Process())
  146. #define W32GetCurrentThread() ((PW32THREAD)PsGetCurrentThreadWin32Thread())
  147. #define PID_BITS 0xfffffffc // The actual bits used by the PID
  148. #define W32GetCurrentPID() (W32PID)(HandleToUlong(PsGetCurrentProcessId()) & PID_BITS)
  149. #define W32GetCurrentTID() (W32PID)HandleToUlong(PsGetCurrentThreadId())
  150. NTSTATUS
  151. UserPowerStateCallout(
  152. IN PKWIN32_POWERSTATE_PARAMETERS Parms);
  153. NTSTATUS
  154. UserPowerEventCallout(
  155. IN PKWIN32_POWEREVENT_PARAMETERS Parms);
  156. PVOID
  157. UserGlobalAtomTableCallout(
  158. VOID);
  159. NTSTATUS
  160. xxxUserProcessCallout(
  161. IN PW32PROCESS Process,
  162. IN BOOLEAN Initialize);
  163. NTSTATUS
  164. UserThreadCallout(
  165. IN PETHREAD pEThread,
  166. IN PSW32THREADCALLOUTTYPE CalloutType);
  167. VOID
  168. UserDeleteW32Process(
  169. IN PW32PROCESS pW32Process);
  170. VOID
  171. UserDeleteW32Thread(
  172. IN PW32THREAD pW32Thread);
  173. NTSTATUS
  174. GdiProcessCallout(
  175. IN PW32PROCESS Process,
  176. IN BOOLEAN Initialize);
  177. VOID
  178. GdiThreadCallout(
  179. IN PETHREAD pEThread,
  180. IN PSW32THREADCALLOUTTYPE CalloutType);
  181. BOOLEAN
  182. InitializeGre(
  183. VOID);
  184. NTSTATUS
  185. W32pProcessCallout(
  186. IN PEPROCESS Process,
  187. IN BOOLEAN Initialize);
  188. NTSTATUS
  189. W32pThreadCallout(
  190. IN PETHREAD pEThread,
  191. IN PSW32THREADCALLOUTTYPE CalloutType);
  192. //
  193. // Generic thread locking functions
  194. //
  195. #if DBG
  196. ULONG ValidateThreadLocks(
  197. PTL NewLock,
  198. PTL OldLock,
  199. ULONG_PTR dwLimit,
  200. BOOLEAN fHM);
  201. #else
  202. #define ValidateThreadLocks(NewLock, OldLock, dwLimit, fHM)
  203. #endif
  204. //
  205. // DO_INLINE controls whether we want to try inlining the thread locking
  206. // functions. We always want to try inlining on free builds. On debug builds,
  207. // we don't want to inline and we don't want to include multiple copies of
  208. // this code, so we define DO_INCLUDE as empty in w32init.c.
  209. //
  210. #if !DBG
  211. #undef DO_INLINE
  212. #define DO_INLINE __inline
  213. #endif
  214. #ifdef DO_INLINE
  215. DO_INLINE VOID
  216. PushW32ThreadLock(
  217. IN PVOID pobj,
  218. IN PTL ptl,
  219. IN PFREE_FUNCTION pfnFree
  220. )
  221. {
  222. #if DBG
  223. PVOID pfnT;
  224. #endif
  225. PW32THREAD pW32Thread = W32GetCurrentThread();
  226. ptl->next = (struct _TL *)(LONG_PTR)((W32THREAD *)(LONG_PTR)pW32Thread)->ptlW32;
  227. pW32Thread->ptlW32 = ptl;
  228. ptl->pobj = pobj;
  229. ptl->pfnFree = pfnFree;
  230. #if DBG
  231. ptl->pW32Thread = pW32Thread;
  232. RtlGetCallersAddress(&ptl->pfnCaller, &pfnT);
  233. CreateShadowTL(ptl);
  234. ValidateThreadLocks(ptl, ptl->next, (ULONG_PTR)&pW32Thread, FALSE);
  235. #endif
  236. }
  237. DO_INLINE VOID
  238. ExchangeW32ThreadLock(
  239. IN PVOID pobj,
  240. IN PTL ptl
  241. )
  242. {
  243. #if DBG
  244. PVOID pfnT;
  245. PW32THREAD pW32Thread = W32GetCurrentThread();
  246. #endif
  247. PVOID pobjOld;
  248. pobjOld = ptl->pobj;
  249. ptl->pobj = pobj;
  250. if (pobjOld) {
  251. ptl->pfnFree(pobjOld);
  252. }
  253. #if DBG
  254. ASSERT(ptl->pW32Thread == pW32Thread);
  255. RtlGetCallersAddress(&ptl->pfnCaller, &pfnT);
  256. ValidateThreadLocks(ptl, ptl->next, (ULONG_PTR)&pW32Thread, FALSE);
  257. /*
  258. * Maintain gFreeTLList
  259. */
  260. ptl->ptl->pobj = pobj;
  261. ptl->ptl->pfnCaller = ptl->pfnCaller;
  262. #endif
  263. }
  264. DO_INLINE VOID
  265. PopW32ThreadLock(
  266. IN PTL ptl
  267. )
  268. {
  269. PW32THREAD pW32Thread = W32GetCurrentThread();
  270. #if DBG
  271. ASSERT(ptl);
  272. ASSERT(ptl == pW32Thread->ptlW32);
  273. ValidateThreadLocks(NULL, ptl, (ULONG_PTR)&pW32Thread, FALSE);
  274. #endif
  275. pW32Thread->ptlW32 = ptl->next;
  276. #if DBG
  277. ptl->ptl->next = gFreeTLList;
  278. ptl->ptl->uTLCount += TL_FREED_PATTERN;
  279. gFreeTLList = ptl->ptl;
  280. #endif
  281. }
  282. DO_INLINE VOID
  283. PopAndFreeW32ThreadLock(
  284. IN PTL ptl
  285. )
  286. {
  287. PW32THREAD pW32Thread = W32GetCurrentThread();
  288. #if DBG
  289. ASSERT(ptl);
  290. ASSERT(ptl == pW32Thread->ptlW32);
  291. ValidateThreadLocks(NULL, ptl, (ULONG_PTR)&pW32Thread, FALSE);
  292. #endif
  293. pW32Thread->ptlW32 = ptl->next;
  294. if (ptl->pobj) {
  295. ptl->pfnFree(ptl->pobj);
  296. }
  297. #if DBG
  298. ptl->ptl->next = gFreeTLList;
  299. ptl->ptl->uTLCount += TL_FREED_PATTERN;
  300. gFreeTLList = ptl->ptl;
  301. #endif
  302. }
  303. DO_INLINE VOID
  304. PopAndFreeAlwaysW32ThreadLock(
  305. IN PTL ptl
  306. )
  307. {
  308. PW32THREAD pW32Thread = W32GetCurrentThread();
  309. #if DBG
  310. ASSERT(ptl);
  311. ASSERT(ptl == pW32Thread->ptlW32);
  312. ValidateThreadLocks(NULL, ptl, (ULONG_PTR)&pW32Thread, FALSE);
  313. ASSERT(ptl->pobj);
  314. #endif
  315. pW32Thread->ptlW32 = ptl->next;
  316. ptl->pfnFree(ptl->pobj);
  317. #if DBG
  318. ptl->ptl->next = gFreeTLList;
  319. ptl->ptl->uTLCount += TL_FREED_PATTERN;
  320. gFreeTLList = ptl->ptl;
  321. #endif
  322. }
  323. #else
  324. VOID PushW32ThreadLock(IN PVOID pobj, IN PTL ptl, IN PFREE_FUNCTION pfnFree);
  325. VOID ExchangeW32ThreadLock(IN PVOID pobj, IN PTL ptl);
  326. VOID PopW32ThreadLock(IN PTL ptl);
  327. VOID PopAndFreeW32ThreadLock(IN PTL ptl);
  328. VOID PopAndFreeAlwaysW32ThreadLock(IN PTL ptl);
  329. #endif
  330. VOID
  331. CleanupW32ThreadLocks(
  332. IN PW32THREAD pW32Thread
  333. );
  334. //
  335. // Process and thread manipulation functions
  336. //
  337. VOID
  338. DereferenceW32Thread(
  339. IN PW32THREAD pW32Thread);
  340. NTSTATUS
  341. AllocateW32Process(
  342. IN OUT PEPROCESS pEProcess);
  343. NTSTATUS
  344. AllocateW32Thread(
  345. IN OUT PETHREAD pEThread);
  346. __inline VOID
  347. FreeW32Thread(
  348. IN OUT PETHREAD pEThread)
  349. {
  350. ASSERT(pEThread == PsGetCurrentThread());
  351. ASSERT(PsGetThreadWin32Thread(pEThread) != NULL);
  352. DereferenceW32Thread((PW32THREAD)PsGetThreadWin32Thread(pEThread));
  353. }
  354. VOID
  355. LockW32Process(
  356. IN PW32PROCESS pW32Process,
  357. IN PTL ptl);
  358. #define UnlockW32Process(ptl) \
  359. PopAndFreeW32ThreadLock(ptl)
  360. VOID
  361. LockW32Thread(
  362. IN PW32THREAD pW32Thread,
  363. IN PTL ptl);
  364. VOID
  365. LockExchangeW32Thread(
  366. IN PW32THREAD pW32Thread,
  367. IN PTL ptl);
  368. #define UnlockW32Thread(ptl) \
  369. PopAndFreeW32ThreadLock(ptl)
  370. //
  371. // Base address where win32k.sys is loaded.
  372. //
  373. extern PVOID gpvWin32kImageBase;
  374. #endif // _W32P_