aboutsummaryrefslogtreecommitdiffstats
path: root/vxd_lib.c
blob: eade7b232fd7d24f1b4a3469bdb87d95a4d4a6c8 (plain) (blame)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*****************************************************************************

Copyright (c) 2024 Jaroslav Hensl <emulator@emulace.cz>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*****************************************************************************/

/* 32 bit RING-0 system calls and CRT function */

#include "winhack.h"
#include "vmm.h"
#include "vxd.h"

#include "svga_all.h"

#include "vxd_vdd.h"

#include "vxd_lib.h"

#include "code32.h"

/* some CRT */
void memset(void *dst, int c, unsigned int size)
{
	unsigned int par;
	unsigned int i;
	unsigned int dw_count;
	unsigned int dw_size;
	
	c &= 0xFF;
	par = (c << 24) | (c << 16) | (c << 8) | c;
	dw_count = size >> 2;
	dw_size = size & 0xFFFFFFFCUL;
	
	for(i = 0; i < dw_count; i++)
	{
		((unsigned int*)dst)[i] = par; 
	}
	
	for(i = dw_size; i < size; i++)
	{
		((unsigned char*)dst)[i] = c; 
	}
}

void *memcpy(void *dst, const void *src, unsigned int size)
{
	unsigned int dw_count;
	unsigned int dw_size;
	unsigned int i;
	
	dw_count = size >> 2;
	dw_size = size & 0xFFFFFFFCUL;
	
	for(i = 0; i < dw_count; i++)
	{
		((unsigned int*)dst)[i] = ((unsigned int*)src)[i];
	}
	
	for(i = dw_size; i < size; i++)
	{
		((unsigned char*)dst)[i] = ((unsigned char*)src)[i]; 
	}
	
	return dst;
}

/* temp for reading registry */
static union
{
	DWORD dw;
	char str[64];
} RegReadConfBuf = {0};

/**
 * Read integer value from registry
 *
 **/ 
BOOL RegReadConf(UINT root, const char *path, const char *name, DWORD *out)
{
	DWORD hKey;
	DWORD type;
	DWORD cbData = sizeof(RegReadConfBuf);
	BOOL rv = FALSE;
	
	if(_RegOpenKey(root, (char*)path, &hKey) == ERROR_SUCCESS)
	{
		if(_RegQueryValueEx(hKey, (char*)name, 0, &type, &(RegReadConfBuf.str), &cbData) == ERROR_SUCCESS)
		{
			if(type == REG_SZ)
			{
				DWORD cdw = 0;
				char *ptr = &(RegReadConfBuf.str);
				
				while(*ptr == ' '){ptr++;}
				
				while(*ptr >= '0' && *ptr <= '9')
				{
					cdw *= 10;
					cdw += *ptr - '0';
					ptr++;
				}
				
				*out = cdw;
			}
			else
			{
				*out = RegReadConfBuf.dw;
			}
			
			rv = TRUE;
		}
		
		_RegCloseKey(hKey);
	}
	
	return rv;
}

/**
 * VMM calls wrapers
 **/
DWORD Get_VMM_Version()
{
	static WORD ver = 0;
	
	_asm push ecx
	_asm push eax
	_asm xor eax, eax
	VMMCall(Get_VMM_Version);
	_asm mov [ver],ax
	_asm pop ecx
	_asm pop eax
	
	return ver;
}

volatile void __cdecl Begin_Critical_Section(ULONG Flags)
{
	static ULONG sFlags;
	sFlags = Flags;
	
	_asm push ecx
	_asm mov ecx, [sFlags]
	VMMCall(Begin_Critical_Section);
	_asm pop ecx	
}

volatile  void __cdecl End_Critical_Section()
{
	VMMCall(End_Critical_Section);
}

ULONG __cdecl Create_Semaphore(ULONG TokenCount)
{
	static ULONG sTokenCount;
	static ULONG sSemHandle;
	sTokenCount = TokenCount;
	sSemHandle = 0;
	
	_asm push eax
	_asm push ecx
	_asm mov ecx, [sTokenCount]
	VMMCall(Create_Semaphore);
	_asm {
		jc create_sem_error
		mov [sSemHandle], eax
		create_sem_error:
		pop ecx
		pop eax
	}
	
	return sSemHandle;
}

void __cdecl Destroy_Semaphore(ULONG SemHandle)
{
	static ULONG sSemHandle;
	sSemHandle = SemHandle;
	
	_asm push eax
	_asm mov eax, [sSemHandle]
	VMMCall(Destroy_Semaphore);
	_asm pop eax
}

void __cdecl Wait_Semaphore(ULONG semHandle, ULONG flags)
{
	static ULONG sSemHandle;
	static ULONG sFlags;
	sSemHandle = semHandle;
	sFlags     = flags;
	
	_asm push eax
	_asm push ecx
	_asm mov eax, [sSemHandle]
	_asm mov ecx, [sFlags]
	VMMCall(Wait_Semaphore);
	_asm pop ecx
	_asm pop eax
}

void __cdecl Signal_Semaphore(ULONG SemHandle)
{
	static ULONG sSemHandle;
	sSemHandle = SemHandle;
	
	_asm push eax
	_asm mov eax, [sSemHandle]
	VMMCall(Signal_Semaphore);
	_asm pop eax
}

void __cdecl Resume_VM(ULONG VM)
{
	static ULONG sVM;
	sVM = VM;
	
	_asm push ebx
	_asm mov ebx, [sVM]
	VMMCall(Resume_VM);
	_asm pop ebx	
}

void Release_Time_Slice()
{
	VMMCall(Release_Time_Slice);
}

void __cdecl *Map_Flat(BYTE SegOffset, BYTE OffOffset)
{
	static BYTE sSegOffset;
	static BYTE sOffOffset;
	static void *result;
	
	sSegOffset = SegOffset;
	sOffOffset = OffOffset;
	
	_asm
	{
		push eax
		xor eax, eax
		mov al, [sOffOffset]
		mov ah, [sSegOffset]
	}
	VMMCall(Map_Flat);
	_asm
	{
		mov [result],eax
		pop eax
	}
	
	return result;
}

void __cdecl Install_IO_Handler(DWORD port, DWORD callback)
{
	static DWORD sPort = 0;
	static DWORD sCallback = 0;

	sPort = port;
	sCallback = callback;
	
/*
 * esi <- IOCallback
 * edx <- I/O port numbers
 */
	_asm
	{
		push esi
		push edx
		mov esi, [sCallback]
		mov edx, [sPort]
	}
	VMMCall(Install_IO_Handler);
	_asm
	{
		pop edx
		pop esi
	}
}

DWORD __cdecl _GetFreePageCount(DWORD *pLockablePages)
{
	static DWORD sLockablePages = 0;
	static DWORD sFreePages = 0;
	_asm
	{
		push edx
		push ecx
		push 0 ; flags - must be zero
	}
	VMMCall(_GetFreePageCount);
	_asm
	{
		mov [sLockablePages], edx
		mov [sFreePages], eax
		pop eax
		pop ecx
		pop edx
	}
	
	if(pLockablePages != NULL)
	{
		*pLockablePages = sLockablePages;
	}
	
	return sFreePages;
}

static void __declspec(naked) __cdecl _BuildDescriptorDWORDs_(ULONG DESCBase, ULONG DESCLimit, ULONG DESCType, ULONG DESCSize, ULONG flags)
{
	VMMJmp(_BuildDescriptorDWORDs);
}

void __cdecl _BuildDescriptorDWORDs(ULONG DESCBase, ULONG DESCLimit, ULONG DESCType, ULONG DESCSize, ULONG flags, DWORD *outDescHigh, DWORD *outDescLow)
{
	static DWORD sHigh;
	static DWORD sLow;
	
	_BuildDescriptorDWORDs_(DESCBase, DESCLimit, DESCType, DESCSize, flags);
	_asm
	{
		mov [sHigh], edx
		mov [sLow],  eax
	}
	
	*outDescHigh = sHigh;
	*outDescLow  = sLow;
}

static void __declspec(naked) __cdecl _Allocate_LDT_Selector_(ULONG vm, ULONG DescHigh, ULONG DescLow, ULONG Count, ULONG flags)
{
	VMMJmp(_Allocate_LDT_Selector);
}

void __cdecl _Allocate_LDT_Selector(ULONG vm, ULONG DescHigh, ULONG DescLow, ULONG Count, ULONG flags, DWORD *outFirstSelector, DWORD *outSelectorTable)
{
	static DWORD firstSelector;
	static DWORD selectorTable;
	
	_Allocate_LDT_Selector_(vm, DescHigh, DescLow, Count, flags);
	_asm
	{
		mov [firstSelector], eax
		mov [selectorTable], edx
	}
	
	if(outFirstSelector != NULL)
	{
		*outFirstSelector = firstSelector;
	}
	
	if(outSelectorTable != NULL)
	{
		*outSelectorTable = selectorTable;
	}
}

static void __declspec(naked) __cdecl _Allocate_GDT_Selector_(ULONG DescHigh, ULONG DescLow, ULONG flags)
{
	VMMJmp(_Allocate_GDT_Selector);
}

void __cdecl _Allocate_GDT_Selector(ULONG DescHigh, ULONG DescLow, ULONG flags, DWORD *outFirstSelector, DWORD *outSelectorTable)
{
	static DWORD firstSelector;
	static DWORD selectorTable;
	
	_Allocate_GDT_Selector_(DescHigh, DescLow, flags);
	_asm
	{
		mov [firstSelector], eax
		mov [selectorTable], edx
	}
	
	if(outFirstSelector != NULL)
	{
		*outFirstSelector = firstSelector;
	}
	
	if(outSelectorTable != NULL)
	{
		*outSelectorTable = selectorTable;
	}
}

static ULONG __declspec(naked) __cdecl _PageAllocate_call(ULONG nPages, ULONG pType, ULONG VM, ULONG AlignMask, ULONG minPhys, ULONG maxPhys, ULONG *PhysAddr, ULONG flags)
{
	VMMJmp(_PageAllocate);
}

ULONG __cdecl _PageAllocate(ULONG nPages, ULONG pType, ULONG VM, ULONG AlignMask, ULONG minPhys, ULONG maxPhys, ULONG *PhysAddr, ULONG flags)
{
	ULONG r = _PageAllocate_call(nPages, pType, VM, AlignMask, minPhys, maxPhys, PhysAddr, flags);
	
	/* do examplicit TLB flush, help much for stability.
	 * There is probably another TLB bug in VMM...
	 */
	_asm
	{
		push eax
		mov eax, cr3
		mov cr3, eax
		pop eax
	};
	return r;
}

ULONG __declspec(naked) __cdecl _PageFree(PVOID hMem, DWORD flags)
{
	VMMJmp(_PageFree);
}

ULONG __declspec(naked) __cdecl _CopyPageTable(ULONG LinPgNum, ULONG nPages, DWORD *PageBuf, ULONG flags)
{
	VMMJmp(_CopyPageTable);
}

ULONG __declspec(naked) __cdecl _LinPageLock(ULONG page, ULONG npages, ULONG flags)
{
	VMMJmp(_LinPageLock);
}

ULONG __declspec(naked) __cdecl _LinPageUnLock(ULONG page, ULONG npages, ULONG flags)
{
	VMMJmp(_LinPageUnLock);
}

ULONG __declspec(naked) __cdecl _MapPhysToLinear(ULONG PhysAddr, ULONG nBytes, ULONG flags)
{
	VMMJmp(_MapPhysToLinear);
}

ULONG __declspec(naked) __cdecl GetNulPageHandle()
{
	VMMJmp(_MapPhysToLinear);
}

ULONG __declspec(naked) __cdecl _PhysIntoV86(ULONG PhysPage, ULONG VM, ULONG VMLinPgNum, ULONG nPages, ULONG flags)
{
	VMMJmp(_PhysIntoV86);
}

DWORD __declspec(naked) __cdecl _RegOpenKey(DWORD hKey, char *lpszSubKey, DWORD *lphKey)
{
	VMMJmp(_RegOpenKey);
}

DWORD __declspec(naked) __cdecl _RegCloseKey(DWORD hKey)
{
	VMMJmp(_RegCloseKey);
}

DWORD __declspec(naked) __cdecl _RegQueryValueEx(DWORD hKey, char *lpszValueName, DWORD *lpdwReserved, DWORD *lpdwType, BYTE *lpbData, DWORD *lpcbData)
{
	VMMJmp(_RegQueryValueEx);
}