ReactOS  0.4.15-dev-2700-g4b4ffa9
fxcommonbuffer.cpp
Go to the documentation of this file.
1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 Module Name:
6 
7  FxCommonBuffer.cpp
8 
9 Abstract:
10 
11  WDF CommonBuffer Object
12 
13 Environment:
14 
15  Kernel mode only.
16 
17 Notes:
18 
19 
20 Revision History:
21 
22 --*/
23 
24 #include "fxdmapch.hpp"
25 
26 extern "C" {
27 // #include "FxCommonBuffer.tmh"
28 }
29 
31  __in PFX_DRIVER_GLOBALS FxDriverGlobals,
33  ) :
35 {
37  m_BufferRawVA = NULL; // allocated buffer base (unaligned)
38  m_BufferAlignedVA = NULL; // aligned buffer base
39  m_BufferAlignedLA.QuadPart = NULL; // aligned physical buffer base
40  m_BufferRawLA.QuadPart = NULL; // allocated buffer phy base (unaligned)
41  m_Length = 0;
42  m_RawLength = 0;
43 
45 
46  //
47  // By default use the alignment of the dma enabler.
48  //
50 }
51 
52 BOOLEAN
54 {
56  return TRUE;
57 }
58 
62  __in size_t Length
63  )
64 {
68  ULONG result;
69 
70  //
71  // Must be running at PASIVE_LEVEL
72  //
74  if (!NT_SUCCESS(status)) {
75  return status;
76  }
77 
78  m_Length = Length;
79 
80  //
81  // If required, add alignment to the length.
82  // If alignment is <= page-1, we actually don't need to do it b/c
83  // AllocateCommonBuffer allocates at least a page of memory, regardless
84  // of the requested Length. If driver version is < v1.11, we still add the
85  // alignment to the length for compatibility.
86  //
87  if (m_Alignment > PAGE_SIZE-1 ||
89 
90  status = RtlSizeTAdd(Length, m_Alignment, &m_RawLength);
91  if (!NT_SUCCESS(status)) {
93  "WDFDMAENABLER %p AllocateCommonBuffer: overflow when adding Length "
94  "%I64d + Alignment %I64d", GetObjectHandle(), Length, m_Alignment);
96  return status;
97  }
98  }
99  else {
101  }
102 
104  &m_BufferRawVA,
105  &m_BufferRawLA);
106  if (m_BufferRawVA) {
107 
110 
111  if (m_Alignment > PAGE_SIZE-1) {
112  //
113  // If the alignment mask is over a page-size then the aligned virtual
114  // and aligned logical could be pointing to different locations
115  // in memory. So ajdust the VA to match the LA address by adding
116  // the offset of alignedLA and RawLA to VA. By doing this we
117  // only guarantee alignment of LA when the page alignment exceeds PAGE_SIZE.
118  //
119  status = RtlULongLongSub(m_BufferAlignedLA.QuadPart,
121  &offset);
122  if (!NT_SUCCESS(status)) {
124  "WDFDMAENABLER %p AllocateCommonBuffer: overflow when subtracting "
125  "RawLA %I64x from AlignedLA %I64x",
128  return status;
129  }
130 
131  status = RtlULongLongToULong(offset, &result);
132  if (!NT_SUCCESS(status)) {
134  "WDFDMAENABLER %p AllocateCommonBuffer: overflow when "
135  "converting from ULongLong %I64d to ULong",
138  return status;
139  }
140 
142  }
143  else {
146  }
147  } else {
148  m_Length = 0;
149  m_RawLength = 0;
153  }
154 
155  return status;
156 }
157 
158 VOID
160  VOID
161  )
162 {
163  //
164  // Free this CommonBuffer per DmaEnabler
165  //
166  if (m_BufferRawVA != NULL) {
169  m_BufferRawLA);
170  }
171 }
172 
173 
#define _Must_inspect_result_
Definition: no_sal2.h:62
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
GLuint64EXT * result
Definition: glext.h:11304
ActualNumberDriverObjects * sizeof(PDRIVER_OBJECT)) PDRIVER_OBJECT *DriverObjectList
#define WDF_PTR_ADD_OFFSET(_ptr, _offset)
Definition: wdfcore.h:144
#define TRUE
Definition: types.h:120
LONG NTSTATUS
Definition: precomp.h:26
PHYSICAL_ADDRESS m_BufferAlignedLA
#define FALSE
Definition: types.h:117
#define TRACINGDMA
Definition: dbgtrace.h:71
VOID MarkDisposeOverride(__in FxObjectLockState State=ObjectLock)
Definition: fxobject.hpp:1101
PVOID __inline GetObjectHandle(VOID)
Definition: fxobject.hpp:603
__inline size_t GetAlignment(VOID)
unsigned char BOOLEAN
_Must_inspect_result_ BOOLEAN IsVersionGreaterThanOrEqualTo(__in ULONG Major, __in ULONG Minor)
Definition: globalskm.cpp:92
FORCEINLINE PVOID FX_ALIGN_VIRTUAL_ADDRESS(__in PVOID VA, __in size_t AlignTo)
virtual BOOLEAN Dispose(VOID)
VOID FreeCommonBuffer(VOID)
VOID AllocateCommonBuffer(__in size_t Length, __deref_out_opt PVOID *BufferVA, __out PHYSICAL_ADDRESS *BufferPA)
__inline NTSTATUS FxVerifierCheckIrqlLevel(__in PFX_DRIVER_GLOBALS FxDriverGlobals, __in KIRQL Irql)
Definition: fxverifier.h:158
VOID FreeCommonBuffer(__in size_t Length, __in PVOID BufferVA, __in PHYSICAL_ADDRESS BufferPA)
PFX_DRIVER_GLOBALS pFxDriverGlobals
PHYSICAL_ADDRESS m_BufferRawLA
#define ASSERT(a)
Definition: mode.c:44
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
FORCEINLINE ULONGLONG FX_ALIGN_LOGICAL_ADDRESS(__in PHYSICAL_ADDRESS LA, __in size_t AlignTo)
GLintptr offset
Definition: glext.h:5920
uint64_t ULONGLONG
Definition: typedefs.h:67
FxDmaEnabler * m_DmaEnabler
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define PASSIVE_LEVEL
Definition: env_spec_w32.h:693
#define TRACE_LEVEL_ERROR
Definition: storswtr.h:27
FxDmaEnabler * pDmaEnabler
__inline PFX_DRIVER_GLOBALS GetDriverGlobals(VOID)
Definition: fxobject.hpp:734
_Must_inspect_result_ NTSTATUS AllocateCommonBuffer(__in size_t Length)
DoTraceLevelMessage(pFxDriverGlobals, TRACE_LEVEL_VERBOSE, TRACINGPNP, "Enter, WDFDEVICE %p", Device)
#define NULL
Definition: types.h:112
FxCommonBuffer(__in PFX_DRIVER_GLOBALS FxDriverGlobals, __in FxDmaEnabler *pDmaEnabler)
unsigned int ULONG
Definition: retypes.h:1
#define __in
Definition: dbghelp.h:35
static SERVICE_STATUS status
Definition: service.c:31
FxVerifierDbgBreakPoint(pFxDriverGlobals)
LONGLONG QuadPart
Definition: typedefs.h:114
Definition: ps.c:97