-
Notifications
You must be signed in to change notification settings - Fork 49
/
virtual_asm_windows.cpp
143 lines (116 loc) · 3.18 KB
/
virtual_asm_windows.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "virtual_asm.h"
#include <Windows.h>
namespace assembler {
unsigned Processor::maxIntArgs64() {
return 4;
}
unsigned Processor::maxFloatArgs64() {
return 4;
}
bool Processor::isIntArg64Register(unsigned char number, unsigned char arg) {
return arg < 4;
}
bool Processor::isFloatArg64Register(unsigned char number, unsigned char arg) {
return arg < 4;
}
Register Processor::intArg64(unsigned char number, unsigned char arg) {
switch(arg) {
case 0:
return Register(*this, ECX);
case 1:
return Register(*this, EDX);
case 2:
return Register(*this, R8);
case 3:
return Register(*this, R9);
default:
throw "Integer64 argument index out of bounds";
}
}
Register Processor::floatArg64(unsigned char number, unsigned char arg) {
switch(arg) {
case 0:
return Register(*this, XMM0);
case 1:
return Register(*this, XMM1);
case 2:
return Register(*this, XMM2);
case 3:
return Register(*this, XMM3);
default:
throw "Float64 argument index out of bounds";
}
}
Register Processor::intArg64(unsigned char number, unsigned char arg, Register defaultReg) {
if(isIntArg64Register(number, arg))
return intArg64(number, arg);
return defaultReg;
}
Register Processor::floatArg64(unsigned char number, unsigned char arg, Register defaultReg) {
if(isFloatArg64Register(number, arg))
return floatArg64(number, arg);
return defaultReg;
}
Register Processor::intReturn64() {
return Register(*this, EAX);
}
Register Processor::floatReturn64() {
return Register(*this, XMM0);
}
CodePage::CodePage(unsigned int Size, void* requestedStart) : used(0), final(false), references(1) {
SYSTEM_INFO info;
GetSystemInfo(&info);
unsigned minPageSize = info.dwPageSize;
size_t pageStep = (size_t)info.dwAllocationGranularity * 2;
if((size_t)Size > pageStep)
pageStep = (size_t)Size;
unsigned pages = Size / minPageSize;
if(Size % minPageSize != 0)
pages += 1;
size = (pages * minPageSize) - 2;
//Search for progressively more distant possible page locations, then just get any available one
for(int i = 1; i < 256; ++i) {
void* request = (char*)requestedStart + i*pageStep;
page = VirtualAlloc(request, size, MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if(page != 0)
return;
}
page = VirtualAlloc(0, size, MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE);
}
void CodePage::grab() {
++references;
}
void CodePage::drop() {
if(--references == 0)
delete this;
}
CodePage::~CodePage() {
VirtualFree(page,0,MEM_RELEASE);
}
void CodePage::finalize() {
FlushInstructionCache(GetCurrentProcess(),page,size);
DWORD oldProtect = PAGE_EXECUTE_READWRITE;
VirtualProtect(page,size,PAGE_EXECUTE_READ,&oldProtect);
final = true;
}
unsigned int CodePage::getMinimumPageSize() {
SYSTEM_INFO info;
GetSystemInfo(&info);
return info.dwPageSize;
}
void CriticalSection::enter() {
EnterCriticalSection((CRITICAL_SECTION*)pLock);
}
void CriticalSection::leave() {
LeaveCriticalSection((CRITICAL_SECTION*)pLock);
}
CriticalSection::CriticalSection() {
auto* section = new CRITICAL_SECTION;
InitializeCriticalSection(section);
pLock = section;
}
CriticalSection::~CriticalSection() {
DeleteCriticalSection((CRITICAL_SECTION*)pLock);
delete (CRITICAL_SECTION*)pLock;
}
};