ReactOS 0.4.15-dev-5672-gf73ac17
kdmemsup.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
Include dependency graph for kdmemsup.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define HIGH_PHYS_MASK   0x80000000
 
#define PAGE_TABLE_MASK   0x3ff
 
#define BIG_PAGE_SIZE   (1<<22)
 
#define CR4_PAGE_SIZE_BIT   0x10
 
#define PDE_PRESENT_BIT   0x01
 
#define PDE_W_BIT   0x02
 
#define PDE_PWT_BIT   0x08
 
#define PDE_PCD_BIT   0x10
 
#define PDE_ACCESSED_BIT   0x20
 
#define PDE_DIRTY_BIT   0x40
 
#define PDE_PS_BIT   0x80
 
#define MI_KDBG_TMP_PAGE_1   (HYPER_SPACE + 0x400000 - PAGE_SIZE)
 
#define MI_KDBG_TMP_PAGE_0   (MI_KDBG_TMP_PAGE_1 - PAGE_SIZE)
 

Functions

static ULONG_PTR KdpPhysMap (ULONG_PTR PhysAddr, LONG Len)
 
static ULONGLONG KdpPhysRead (ULONG_PTR PhysAddr, LONG Len)
 
static VOID KdpPhysWrite (ULONG_PTR PhysAddr, LONG Len, ULONGLONG Value)
 
static BOOLEAN KdpTranslateAddress (ULONG_PTR Addr, PULONG_PTR ResultAddr)
 
BOOLEAN NTAPI KdpSafeReadMemory (ULONG_PTR Addr, LONG Len, PVOID Value)
 
BOOLEAN NTAPI KdpSafeWriteMemory (ULONG_PTR Addr, LONG Len, ULONGLONG Value)
 
VOID NTAPI KdpEnableSafeMem (VOID)
 

Variables

static BOOLEAN KdpPhysAccess = FALSE
 

Macro Definition Documentation

◆ BIG_PAGE_SIZE

#define BIG_PAGE_SIZE   (1<<22)

Definition at line 24 of file kdmemsup.c.

◆ CR4_PAGE_SIZE_BIT

#define CR4_PAGE_SIZE_BIT   0x10

Definition at line 25 of file kdmemsup.c.

◆ HIGH_PHYS_MASK

#define HIGH_PHYS_MASK   0x80000000

Definition at line 22 of file kdmemsup.c.

◆ MI_KDBG_TMP_PAGE_0

#define MI_KDBG_TMP_PAGE_0   (MI_KDBG_TMP_PAGE_1 - PAGE_SIZE)

Definition at line 35 of file kdmemsup.c.

◆ MI_KDBG_TMP_PAGE_1

#define MI_KDBG_TMP_PAGE_1   (HYPER_SPACE + 0x400000 - PAGE_SIZE)

Definition at line 34 of file kdmemsup.c.

◆ NDEBUG

#define NDEBUG

Definition at line 19 of file kdmemsup.c.

◆ PAGE_TABLE_MASK

#define PAGE_TABLE_MASK   0x3ff

Definition at line 23 of file kdmemsup.c.

◆ PDE_ACCESSED_BIT

#define PDE_ACCESSED_BIT   0x20

Definition at line 30 of file kdmemsup.c.

◆ PDE_DIRTY_BIT

#define PDE_DIRTY_BIT   0x40

Definition at line 31 of file kdmemsup.c.

◆ PDE_PCD_BIT

#define PDE_PCD_BIT   0x10

Definition at line 29 of file kdmemsup.c.

◆ PDE_PRESENT_BIT

#define PDE_PRESENT_BIT   0x01

Definition at line 26 of file kdmemsup.c.

◆ PDE_PS_BIT

#define PDE_PS_BIT   0x80

Definition at line 32 of file kdmemsup.c.

◆ PDE_PWT_BIT

#define PDE_PWT_BIT   0x08

Definition at line 28 of file kdmemsup.c.

◆ PDE_W_BIT

#define PDE_W_BIT   0x02

Definition at line 27 of file kdmemsup.c.

Function Documentation

◆ KdpEnableSafeMem()

VOID NTAPI KdpEnableSafeMem ( VOID  )

Definition at line 220 of file kdmemsup.c.

221{
223}
#define TRUE
Definition: types.h:120
static BOOLEAN KdpPhysAccess
Definition: kdmemsup.c:39

◆ KdpPhysMap()

static ULONG_PTR KdpPhysMap ( ULONG_PTR  PhysAddr,
LONG  Len 
)
static

Definition at line 43 of file kdmemsup.c.

44{
46 PMMPTE PointerPte;
47 ULONG_PTR VirtAddr;
48
51
52 if ((PhysAddr & (PAGE_SIZE - 1)) + Len > PAGE_SIZE)
53 {
54 TempPte.u.Hard.PageFrameNumber = (PhysAddr >> PAGE_SHIFT) + 1;
56 *PointerPte = TempPte;
57 VirtAddr = (ULONG_PTR)PointerPte << 10;
58 KeInvalidateTlbEntry((PVOID)VirtAddr);
59 }
60
61 TempPte.u.Hard.PageFrameNumber = PhysAddr >> PAGE_SHIFT;
63 *PointerPte = TempPte;
64 VirtAddr = (ULONG_PTR)PointerPte << 10;
65 KeInvalidateTlbEntry((PVOID)VirtAddr);
66
67 return VirtAddr + (PhysAddr & (PAGE_SIZE - 1));
68}
HARDWARE_PTE_ARMV6 TempPte
Definition: winldr.c:76
#define Len
Definition: deflate.h:82
#define ULONG_PTR
Definition: config.h:101
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
#define PDE_PRESENT_BIT
Definition: kdmemsup.c:26
#define PDE_PWT_BIT
Definition: kdmemsup.c:28
#define PDE_DIRTY_BIT
Definition: kdmemsup.c:31
#define MI_KDBG_TMP_PAGE_1
Definition: kdmemsup.c:34
#define PDE_ACCESSED_BIT
Definition: kdmemsup.c:30
#define PDE_W_BIT
Definition: kdmemsup.c:27
#define MI_KDBG_TMP_PAGE_0
Definition: kdmemsup.c:35
#define PDE_PCD_BIT
Definition: kdmemsup.c:29
#define MiAddressToPte(x)
Definition: mmx86.c:19
FORCEINLINE VOID KeInvalidateTlbEntry(IN PVOID Address)
Definition: ke.h:264
ULONG PageFrameNumber
Definition: mmtypes.h:109
uint32_t ULONG_PTR
Definition: typedefs.h:65

Referenced by KdpPhysRead(), and KdpPhysWrite().

◆ KdpPhysRead()

static ULONGLONG KdpPhysRead ( ULONG_PTR  PhysAddr,
LONG  Len 
)
static

Definition at line 72 of file kdmemsup.c.

73{
74 ULONG_PTR Addr;
75 ULONGLONG Result = 0;
76
77 Addr = KdpPhysMap(PhysAddr, Len);
78
79 switch (Len)
80 {
81 case 8:
82 Result = *((PULONGLONG)Addr);
83 break;
84 case 4:
85 Result = *((PULONG)Addr);
86 break;
87 case 2:
88 Result = *((PUSHORT)Addr);
89 break;
90 case 1:
91 Result = *((PUCHAR)Addr);
92 break;
93 }
94
95 return Result;
96}
static ULONG_PTR KdpPhysMap(ULONG_PTR PhysAddr, LONG Len)
Definition: kdmemsup.c:43
__GNU_EXTENSION typedef unsigned __int64 * PULONGLONG
Definition: ntbasedef.h:383
uint32_t * PULONG
Definition: typedefs.h:59
uint16_t * PUSHORT
Definition: typedefs.h:56
unsigned char * PUCHAR
Definition: typedefs.h:53
uint64_t ULONGLONG
Definition: typedefs.h:67
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426

Referenced by KdpSafeReadMemory(), and KdpTranslateAddress().

◆ KdpPhysWrite()

static VOID KdpPhysWrite ( ULONG_PTR  PhysAddr,
LONG  Len,
ULONGLONG  Value 
)
static

Definition at line 100 of file kdmemsup.c.

101{
102 ULONG_PTR Addr;
103
104 Addr = KdpPhysMap(PhysAddr, Len);
105
106 switch (Len)
107 {
108 case 8:
109 *((PULONGLONG)Addr) = Value;
110 break;
111 case 4:
112 *((PULONG)Addr) = Value;
113 break;
114 case 2:
115 *((PUSHORT)Addr) = Value;
116 break;
117 case 1:
118 *((PUCHAR)Addr) = Value;
119 break;
120 }
121}
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:413

Referenced by KdpSafeWriteMemory().

◆ KdpSafeReadMemory()

BOOLEAN NTAPI KdpSafeReadMemory ( ULONG_PTR  Addr,
LONG  Len,
PVOID  Value 
)

Definition at line 165 of file kdmemsup.c.

166{
167 ULONG_PTR ResultPhysAddr;
168
169 if (!KdpPhysAccess)
170 {
171 memcpy(Value, (PVOID)Addr, Len);
172 return TRUE;
173 }
174
175 memset(Value, 0, Len);
176
177 if (!KdpTranslateAddress(Addr, &ResultPhysAddr))
178 return FALSE;
179
180 switch (Len)
181 {
182 case 8:
183 *((PULONGLONG)Value) = KdpPhysRead(ResultPhysAddr, Len);
184 break;
185 case 4:
186 *((PULONG)Value) = KdpPhysRead(ResultPhysAddr, Len);
187 break;
188 case 2:
189 *((PUSHORT)Value) = KdpPhysRead(ResultPhysAddr, Len);
190 break;
191 case 1:
192 *((PUCHAR)Value) = KdpPhysRead(ResultPhysAddr, Len);
193 break;
194 }
195
196 return TRUE;
197}
#define FALSE
Definition: types.h:117
static BOOLEAN KdpTranslateAddress(ULONG_PTR Addr, PULONG_PTR ResultAddr)
Definition: kdmemsup.c:125
static ULONGLONG KdpPhysRead(ULONG_PTR PhysAddr, LONG Len)
Definition: kdmemsup.c:72
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define memset(x, y, z)
Definition: compat.h:39

◆ KdpSafeWriteMemory()

BOOLEAN NTAPI KdpSafeWriteMemory ( ULONG_PTR  Addr,
LONG  Len,
ULONGLONG  Value 
)

Definition at line 201 of file kdmemsup.c.

202{
203 ULONG_PTR ResultPhysAddr;
204
205 if (!KdpPhysAccess)
206 {
207 memcpy((PVOID)Addr, &Value, Len);
208 return TRUE;
209 }
210
211 if (!KdpTranslateAddress(Addr, &ResultPhysAddr))
212 return FALSE;
213
214 KdpPhysWrite(ResultPhysAddr, Len, Value);
215 return TRUE;
216}
static VOID KdpPhysWrite(ULONG_PTR PhysAddr, LONG Len, ULONGLONG Value)
Definition: kdmemsup.c:100

◆ KdpTranslateAddress()

static BOOLEAN KdpTranslateAddress ( ULONG_PTR  Addr,
PULONG_PTR  ResultAddr 
)
static

Definition at line 125 of file kdmemsup.c.

126{
127 ULONG_PTR CR3Value = __readcr3();
128 ULONG_PTR CR4Value = __readcr4();
129 ULONG_PTR PageDirectory = (CR3Value & ~(PAGE_SIZE-1)) +
130 ((Addr >> 22) * sizeof(ULONG));
131 ULONG_PTR PageDirectoryEntry = KdpPhysRead(PageDirectory, sizeof(ULONG));
132
133 /* Not present -> fail */
134 if (!(PageDirectoryEntry & PDE_PRESENT_BIT))
135 {
136 return FALSE;
137 }
138
139 /* Big Page? */
140 if ((PageDirectoryEntry & PDE_PS_BIT) && (CR4Value & CR4_PAGE_SIZE_BIT))
141 {
142 *ResultAddr = (PageDirectoryEntry & ~(BIG_PAGE_SIZE-1)) +
143 (Addr & (BIG_PAGE_SIZE-1));
144 return TRUE;
145 }
146 else
147 {
148 ULONG_PTR PageTableAddr =
149 (PageDirectoryEntry & ~(PAGE_SIZE-1)) +
150 ((Addr >> PAGE_SHIFT) & PAGE_TABLE_MASK) * sizeof(ULONG);
151 ULONG_PTR PageTableEntry = KdpPhysRead(PageTableAddr, sizeof(ULONG));
152 if (PageTableEntry & PDE_PRESENT_BIT)
153 {
154 *ResultAddr = (PageTableEntry & ~(PAGE_SIZE-1)) +
155 (Addr & (PAGE_SIZE-1));
156 return TRUE;
157 }
158 }
159
160 return FALSE;
161}
__INTRIN_INLINE unsigned long __readcr3(void)
Definition: intrin_x86.h:1818
__INTRIN_INLINE unsigned long __readcr4(void)
Definition: intrin_x86.h:1825
#define CR4_PAGE_SIZE_BIT
Definition: kdmemsup.c:25
#define PAGE_TABLE_MASK
Definition: kdmemsup.c:23
#define BIG_PAGE_SIZE
Definition: kdmemsup.c:24
#define PDE_PS_BIT
Definition: kdmemsup.c:32
uint32_t ULONG
Definition: typedefs.h:59

Referenced by KdpSafeReadMemory(), and KdpSafeWriteMemory().

Variable Documentation

◆ KdpPhysAccess

BOOLEAN KdpPhysAccess = FALSE
static

Definition at line 39 of file kdmemsup.c.

Referenced by KdpEnableSafeMem(), KdpSafeReadMemory(), and KdpSafeWriteMemory().