aboutsummaryrefslogtreecommitdiffstats
path: root/vmwsvxd.c
blob: 72c282f5efe41eff9d9104df79a82e7ba17d35bc (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
/*****************************************************************************

Copyright (c) 2023 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.

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

#include "winhack.h"
#include "vmm.h"
#include "vmwsvxd.h"

#include "svga_all.h"

#include "minivdd32.h"

#include "version.h"

#if 0
/* dynamic VXDs don't using this init function in discardable segment  */

#pragma data_seg("_ITEXT", "ICODE")
#pragma code_seg("_ITEXT", "ICODE")

BOOL __cdecl VMWS_Sys_Critical_Init(DWORD hVM, DWORD dwRefData,
                                    PSTR pCmdTail, PCRS_32 pCRS)
{
  return TRUE;
}

BOOL __cdecl VMWS_Device_Init(DWORD hVM, PSTR pCmdTail, PCRS_32 pCRS)
{
  return TRUE;
}

#endif

#include "code32.h"

void VMWS_Control();
void VMWS_API_Entry();

/*
  VXD structure
  this variable must be in first address in code segment.
  In other cases VXD isn't loadable (WLINK bug?)
*/
DDB VMWS_DDB = {
	NULL,                       // must be NULL
	DDK_VERSION,                // DDK_Version
	VMWSVXD_DEVICE_ID,         // Device ID
	VMWSVXD_MAJOR_VER,         // Major Version
	VMWSVXD_MINOR_VER,         // Minor Version
	NULL,
	"VMWSVXD",
	VDD_Init_Order, //Undefined_Init_Order,
	(DWORD)VMWS_Control,
	(DWORD)VMWS_API_Entry,
	(DWORD)VMWS_API_Entry,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL, // DDB_Win32_Service_Table
	NULL, // prev
	sizeof(DDB)
};

/* string tables */
char dbg_hello[] = "Hello world!\n";
char dbg_version[] = "VMM version: ";
char dbg_region_err[] = "region create error\n";

char dbg_Device_Init_proc[] = "Device_Init_proc\n";
char dbg_Device_Init_proc_succ[] = "Device_Init_proc success\n";

DWORD *DispatchTable = 0;
DWORD DispatchTableLength = 0;

Bool svga_init_success = FALSE;

/**
 * VMM calls wrapers
 **/
DWORD Get_VMM_Version()
{
	static WORD ver;
	
	_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;
}
 
ULONG __declspec(naked) __cdecl _PageAllocate(ULONG nPages, ULONG pType, ULONG VM, ULONG AlignMask, ULONG minPhys, ULONG maxPhys, ULONG *PhysAddr, ULONG flags)
{
	VMMJmp(_PageAllocate);
}

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

/**
 * VDD calls wrapers
 **/
void VDD_Get_Mini_Dispatch_Table()
{
	VxDCall(VDD, Get_Mini_Dispatch_Table);
	_asm mov [DispatchTable],edi
	_asm mov [DispatchTableLength],ecx
}

/**
 * Control Handles
 **/
void Sys_Critical_Init_proc()
{
	// nop
}

void Device_Init_proc();

/*
 * service module calls (init, exit, deviceIoControl, ...)
 * clear carry if succes (this is always success)
 */
void __declspec(naked) VMWS_Control()
{
	// eax = 0x00 - Sys Critical Init
	// eax = 0x01 - sys dynamic init
	// eax = 0x1B - dynamic init
	// eax = 0x1C - dynamic exit
	// eax = 0x23 - device IO control
	
	_asm {
		pushad
		cmp eax,Sys_Critical_Init
		jnz control_1
		  call Sys_Critical_Init_proc
		control_1: 
		cmp eax,Device_Init
		jnz control_2
		  call Device_Init_proc
		control_2:
		cmp eax,0x1B
		jnz control_3
		  call Device_Init_proc
		control_3:
	  popad
		clc
	  ret
	};
}

/**
 * PM16 driver RING0 calls
 **/
BOOL CreateRegion(unsigned int nPages, ULONG *lpLAddr, ULONG *lpPPN)
{
	ULONG phy;
	ULONG laddr;
	SVGAGuestMemDescriptor *desc;
	laddr = _PageAllocate(nPages + 1, PG_SYS, 0, 0x00000000, 0, 0x100000, &phy, PAGECONTIG | PAGEUSEALIGN | PAGEFIXED);
	
	if(laddr)
	{
		desc = (SVGAGuestMemDescriptor*)laddr;
		desc->ppn = (phy/P_SIZE) + 1;
		desc->numPages = nPages;
		desc++;
		desc->ppn = 0;
		desc->numPages = 0;
		
		*lpLAddr = laddr;
		*lpPPN = (phy/P_SIZE);
		
		return TRUE;
	}
	
	return FALSE;
}

BOOL FreeRegion(ULONG LAddr)
{
	if(_PageFree((PVOID)LAddr, 0) != 0)
	{
		return TRUE;
	}
	
	return FALSE;
}

#define MOB_PER_PAGE_CNT (P_SIZE/sizeof(ULONG))

BOOL CreateMOB(unsigned int nPages, ULONG *lpLAddr, ULONG *lpPPN)
{
	unsigned int mobBasePages = (nPages + MOB_PER_PAGE_CNT - 1)/MOB_PER_PAGE_CNT;
	ULONG phy;
	ULONG laddr;
	
	laddr = _PageAllocate(nPages + mobBasePages, PG_SYS, 0, 0x00000000, 0, 0x100000, &phy, PAGECONTIG | PAGEUSEALIGN | PAGEFIXED);
	if(laddr)
	{
		unsigned int i;
		ULONG ppu = (phy/P_SIZE) + mobBasePages;
		ULONG *ptr = (ULONG*)laddr;
		
		for(i = 0; i < nPages; i++)
		{
			ptr[i] = ppu;
			ppu++;
		}

		*lpLAddr = laddr;
		*lpPPN = (phy/P_SIZE);
		return TRUE;
	}
	
	return FALSE;
}

BOOL FreeMOB(ULONG LAddr)
{
	if(_PageFree((PVOID)LAddr, 0) != 0)
	{
		return TRUE;
	}
	
	return FALSE;	
}

WORD __stdcall VMWS_API_Proc(PCRS_32 state)
{
	WORD rc = 0xFFFF;
	WORD service = state->Client_EDX & 0xFFFF;
	switch(service)
	{
		case VMWSVXD_PM16_VERSION:
			rc = (VMWSVXD_MAJOR_VER << 8) | VMWSVXD_MINOR_VER;
			break;
		case VMWSVXD_PM16_VMM_VERSION:
			rc = Get_VMM_Version();
			break;
		/* create region = input: ECX - num_pages; output: EDX - lin. address of descriptor, ECX - PPN of descriptor */
		case VMWSVXD_PM16_CREATE_REGION:
		{
			ULONG lAddr;
			ULONG PPN;
				
			if(CreateRegion(state->Client_ECX, &lAddr, &PPN))
			{
				state->Client_ECX = PPN;
				state->Client_EDX = lAddr;
				rc = 1;
			}
			else
			{
				state->Client_ECX = 0;
				state->Client_EDX = 0;
				
				dbg_printf(dbg_region_err);
				
				rc = 0;
			}
			break;
		}
		/* free region = input: ECX: lin. address */
		case VMWSVXD_PM16_DESTROY_REGION: 
		{
			if(FreeRegion(state->Client_ECX))
			{
				rc = 1;
			}
			else
			{
				rc = 0;
			}
			break;			
		}
		/* Create MOB = input: ECX - num_pages; output: EDX - lin. address of descriptor, ECX - PPN of descriptor */
		case VMWSVXD_PM16_CREATE_MOB:
		{
			ULONG lAddr;
			ULONG PPN;
				
			if(CreateMOB(state->Client_ECX, &lAddr, &PPN))
			{
				state->Client_ECX = PPN;
				state->Client_EDX = lAddr;
				rc = 1;
			}
			else
			{
				state->Client_ECX = 0;
				state->Client_EDX = 0;
				
				dbg_printf(dbg_region_err);
				
				rc = 0;
			}
			break;
		}
		/* Free MOB = input: ECX: lin. address */
		case VMWSVXD_PM16_DESTROY_MOB:
		{
			if(FreeMOB(state->Client_ECX))
			{
				rc = 1;
			}
			else
			{
				rc = 0;
			}
			break;
		}
		/* clear memory on linear address (ESI) by defined size (ECX) */
		case VMWSVXD_PM16_ZEROMEM:
		{
			ULONG i = 0;
			unsigned char *ptr = (unsigned char *)state->Client_ESI;
			for(i = 0; i < state->Client_ECX; i++)
			{
				ptr[i] = 0;
			}
			rc = 1;
			break;
		}
		/* set ECX to actual api version */
		case VMWSVXD_PM16_APIVER:
			state->Client_ECX = DRV_API_LEVEL;
			rc = 1;
			break;
	}
	
	if(rc == 0xFFFF)
	{
		state->Client_EFlags |= 0x1; // set carry
	}
	else
	{
		state->Client_EFlags &= 0xFFFFFFFEUL; // clear carry
	}
	
	return rc;
}

/*
 * Service calls from protected mode (usually 16 bit) and virtual 86 mode
 * CPU state before call is already on stack (EBP points it).
 * Converts the call to __stdcall and call 'VMWS_API_Proc', result of this
 * function is placed to saved AX register.
 *
 */
void __declspec(naked) VMWS_API_Entry()
{
	_asm {
		push ebp
		call VMWS_API_Proc
		mov [ebp+1Ch], ax
		retn
	}
}

/* generate all entry pro VDD function */
#define VDDFUNC(_fnname, _procname) void __declspec(naked) _procname ## _entry() { \
	_asm { push ebp }; \
	_asm { call _procname ## _proc }; \
	_asm { retn }; \
	}
#include "minivdd_func.h"
#undef VDDFUNC

#define VDDFUNC(id, procname) DispatchTable[id] = (DWORD)(procname ## _entry);

/* init device and fill dispatch table */
void Device_Init_proc()
{
	dbg_printf(dbg_Device_Init_proc);
	// VMMCall _Allocate_Device_CB_Area
	
	if(SVGA_Init(FALSE) == 0)
	{
		dbg_printf(dbg_Device_Init_proc_succ);
		svga_init_success = TRUE;
			
		VDD_Get_Mini_Dispatch_Table();
		if(DispatchTableLength >= 0x31)
		{
			#include "minivdd_func.h"
		}
	}
}
#undef VDDFUNC