Source code of Windows XP (NT5)
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.

131 lines
4.8 KiB

  1. This file describes the internals of ul\drv\nt4hack.h that enable building
  2. UL for NT5 and NT4.
  3. 1. The symbol TARGET_NT4 is defined when building for NT4; it is not
  4. defined when building for NT5.
  5. 2. A portion of the NT4 build environment must be used to build NT4-
  6. compatible drivers. This is necessary because:
  7. a. _except_handler3 is part of the kernel-mode exception
  8. handling/dispatching mechanism.
  9. b. All kernel-mode drivers link to ntoskrnl.lib. This is a
  10. hybrid link-library containing mostly import definitions
  11. for those routines exported by ntoskrnl.exe. This library
  12. also contains the actual implementations of a few routines.
  13. c. The NT4 version of ntoskrnl.lib contains the actual
  14. _except_handler3 implemenation. As a result, each NT4 driver
  15. contains a copy of this _except_handler3 implementation.
  16. d. The NT5 version of ntoskrnl.lib contains an import
  17. definition for _except_handler3. As a result, each NT5
  18. driver imports _except_handler3 from the kernel.
  19. e. Since NT5 drivers import _except_handler3 from the kernel,
  20. and _except_handler3 is not exported from the NT4 kernel,
  21. then NT5 drivers will not load under NT4.
  22. 3. When the build target is NT4, nt4hack.h manually #includes basetsd.h.
  23. This file is #included by ntdef.h in the NT5 build environment, but
  24. does not exist in the NT4 build environment. #including this file
  25. enables UL to use the pointer-size-neutral types (such as SIZE_T
  26. and ULONG_PTR), even on NT4 builds.
  27. 4. nt4hack.h defines a few macros & constants newly introduced in the
  28. NT5 build environment. These include C_ASSERT, EXTERN_C, and
  29. ANSI_NULL.
  30. 5. The function prototypes for ExInterlockedCompareExchange64() and
  31. InterlockedCompareExchange() differ between the NT4 and NT5 build
  32. environments. For NT4 they are defined as:
  33. NTKERNELAPI
  34. ULONGLONG
  35. FASTCALL
  36. ExInterlockedCompareExchange64(
  37. IN PULONGLONG Destination,
  38. IN PULONGLONG Exchange,
  39. IN PULONGLONG Comperand,
  40. IN PKSPIN_LOCK Lock
  41. );
  42. PVOID
  43. FASTCALL
  44. InterlockedCompareExchange(
  45. IN OUT PVOID *Destination,
  46. IN PVOID ExChange,
  47. IN PVOID Comperand
  48. );
  49. Under NT5 they are defined as:
  50. NTKERNELAPI
  51. LONGLONG
  52. FASTCALL
  53. ExInterlockedCompareExchange64(
  54. IN PLONGLONG Destination,
  55. IN PLONGLONG Exchange,
  56. IN PLONGLONG Comperand,
  57. IN PKSPIN_LOCK Lock
  58. );
  59. NTKERNELAPI
  60. LONG
  61. FASTCALL
  62. InterlockedCompareExchange(
  63. IN OUT PLONG Destination,
  64. IN LONG ExChange,
  65. IN LONG Comperand
  66. );
  67. nt4hack.h #defines UlInterlockedCompareExchange64() and
  68. UlInterlockedCompareExchange() with a few key type casts to ensure
  69. commonality between platforms.
  70. 6. NT5 introduced the OBJ_KERNEL_HANDLE flag. This flag may be set in
  71. the Attributes field of the OBJECT_ATTRIBUTES structure used as a
  72. parameter for many NT APIs that create named objects. When an
  73. object is opened or created with this flag set, the object is created
  74. or opened using the security context of the calling thread, but the
  75. resulting handle is only valid in the system process. Since the handle
  76. is only valid in the system process, it is impossible for a user-mode
  77. process to close, duplicate, or otherwise manipulate the handle or
  78. the underlying object.
  79. This is a Good Thing. Unfortunately, NT4 does not support this flag.
  80. Fortunately, NT4 supports the KeAttachProcess() and KeDetachProcess()
  81. APIs. KeAttachProcess() allows a thread in one process to attach to
  82. the address space & handle table of another process. Any handles
  83. created while attached to another process are only valid in the
  84. context of the attached process.
  85. nt4hack.h hides these differences by defining:
  86. UL_KERNEL_HANDLE
  87. UlAttachToSystemProcess()
  88. UlDetachFromSystemProcess()
  89. UlCloseSystemHandle()
  90. Under NT4, UL_KERNEL_HANDLE is 0; under NT5 it is OBJ_KERNEL_HANDLE.
  91. UL sets this flag whenever initializing an OBJECT_ATTRIBUTES structure.
  92. Under NT4, UlAttachToSystemProcess() calls KeAttachProcess() to attach
  93. to the system process; under NT5 it is a no-op.
  94. Under NT4, UlDetachFromSystemProcess() calls KeDetachProcess(); under
  95. NT5 it is a no-op.
  96. UlCloseSystemHandle() calls UlAttachToSystemProcess(), ZwClose() to
  97. close the specified handle, then UlDetachFromSystemProcess(). So,
  98. under NT5, UlCloseSystemHandle() is basically a #define to ZwClose().
  99. Under NT4, it must attach before the close and detach afterwards.