My Project
Loading...
Searching...
No Matches
hypermap.c
Go to the documentation of this file.
1/*++
2
3Module Name:
4
5 hypermap.c
6
7Purpose:
8
9 This translation unit contains the implementation of the temporary mapping functions. (hyperspace)/s
10
11Author:
12
13 slep (Matanel) 2025.
14
15Revision History:
16
17--*/
18
19#include "../../includes/mm.h"
20#include "../../includes/mh.h"
21#include "../../assert.h"
22
23// The physical memory offset itself is the hypermap virtual address. This is ruled by not touching the 0x0 - 0x1000 physical addresses AT ALL (you may touch the physical addresses, but not map them with the PhysicalMemoryOffset virtual arithemtic.)
24#define HYPERMAP_VIRTUAL_ADDRESS PhysicalMemoryOffset
25
28
29#define LOCK_HYPERSPACE(PtrOldIrql) MsAcquireSpinlock(&HyperLock, PtrOldIrql)
30#define UNLOCK_HYPERSPACE(OldIrql) MsReleaseSpinlock(&HyperLock, OldIrql)
31
32void*
34 IN uint64_t PfnIndex,
35 OUT PIRQL OldIrql
36)
37
38/*++
39
40 Routine description:
41
42 Temporary maps the specified PFN Page into hyperspace and returns the virtual address mapped into.
43
44 ************************************
45 * *
46 * Returns with a spin lock held!!! * // thanks lou
47 * *
48 ************************************
49
50
51 Arguments:
52
53 [IN] PfnIndex - Page frame index to map.
54 [OUT] OldIrql - Pointer to store entry IRQL.
55
56 Return Values:
57
58 Valid Pointer to mapped region.
59
60--*/
61
62{
63 // First, lock the hyperspace.
64 LOCK_HYPERSPACE (OldIrql);
65
66 // Map the PFN into the page.
67 PPFN_ENTRY pfn = INDEX_TO_PPFN (PfnIndex);
68 uint64_t physAddr = PPFN_TO_PHYSICAL_ADDRESS (pfn);
71
72 // Set PFN metadata.
73 pfn->State = PfnStateActive;
74 pfn->Descriptor.Mapping.PteAddress = pte;
75 pfn->Descriptor.Mapping.Vad = NULL;
76 g_pfnInUse = pfn;
77
78 // Return the virtual address (now mapped)
79 return (void*)HYPERMAP_VIRTUAL_ADDRESS;
80}
81
82void
84 IN IRQL OldIrql
85)
86
87/*++
88
89 Routine description:
90
91 Unlocks the hyperspace, clears previous mapping.
92
93 Arguments:
94
95 [IN] OldIrql - Entry IRQL given by MiMapPageInHyperspace
96
97 Return Values:
98
99 None.
100
101 Notes:
102
103 Does not release the PFN that was given, caller must do so.
104
105--*/
106
107{
108 // Assertion that the hyperspace lock must be locked already (double unlock catch)
109 assert((HyperLock.locked) == 1, "Double hypermap unlock");
110 assert((g_pfnInUse) != 0, "No PFN when releasing hyperspace.");
112 // Clear the PTE present bit (to prevent use after free)
114
115 // After MiUnmapPte changed the pfn metadata, we change it once again to invalidate it.
116 pfn->Descriptor.Mapping.PteAddress = NULL;
117 pfn->Descriptor.Mapping.Vad = NULL;
119 g_pfnInUse = NULL;
120
121 // We do not release the PFN, caller must do so, because it might have other uses with it.
122
123 // Unlock the hyperspace.
124 UNLOCK_HYPERSPACE (OldIrql);
125}
#define IN
Definition annotations.h:7
#define OUT
Definition annotations.h:8
#define assert(...)
Definition assert.h:57
enum _IRQL IRQL
enum _IRQL * PIRQL
PPFN_ENTRY g_pfnInUse
Definition hypermap.c:27
#define LOCK_HYPERSPACE(PtrOldIrql)
Definition hypermap.c:29
#define HYPERMAP_VIRTUAL_ADDRESS
Definition hypermap.c:24
void * MiMapPageInHyperspace(IN uint64_t PfnIndex, OUT PIRQL OldIrql)
Definition hypermap.c:33
void MiUnmapHyperSpaceMap(IN IRQL OldIrql)
Definition hypermap.c:83
SPINLOCK HyperLock
Definition hypermap.c:26
#define UNLOCK_HYPERSPACE(OldIrql)
Definition hypermap.c:30
PMMPTE MiGetPtePointer(IN uintptr_t va)
Definition map.c:76
void MiUnmapPte(IN PMMPTE pte)
Definition map.c:385
@ PAGE_RW
Definition mm.h:272
@ PAGE_PRESENT
Definition mm.h:268
@ PfnStateTransition
Definition mm.h:244
@ PfnStateActive
Definition mm.h:239
struct _MMPTE * PMMPTE
#define PPFN_TO_PHYSICAL_ADDRESS(PPFN)
Definition mm.h:117
#define INDEX_TO_PPFN(Index)
Definition mm.h:62
#define MI_WRITE_PTE(_PtePointer, _Va, _Pa, _Flags)
Definition mm.h:90
struct _PFN_ENTRY * PPFN_ENTRY
struct _SPINLOCK SPINLOCK
union _PFN_ENTRY::@217024126340164016372152071216274230164113211246 Descriptor
struct _PFN_ENTRY::@217024126340164016372152071216274230164113211246::@301110335271023021153236134322146064331241142124 Mapping
uint8_t State
Definition mm.h:430
PMMPTE PteAddress
Definition mm.h:441
struct _MMVAD * Vad
Definition mm.h:440